PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/freetype/cff/cffparse.c

http://github.com/cinder/Cinder
C | 1694 lines | 1247 code | 334 blank | 113 comment | 183 complexity | 3ec8b6c6fd6e11179ca5ea1627745ce9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /***************************************************************************/
  2. /* */
  3. /* cffparse.c */
  4. /* */
  5. /* CFF token stream parser (body) */
  6. /* */
  7. /* Copyright 1996-2018 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used, */
  11. /* modified, and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. #include <ft2build.h>
  18. #include "cffparse.h"
  19. #include FT_INTERNAL_STREAM_H
  20. #include FT_INTERNAL_DEBUG_H
  21. #include FT_INTERNAL_CALC_H
  22. #include FT_INTERNAL_POSTSCRIPT_AUX_H
  23. #include "cfferrs.h"
  24. #include "cffpic.h"
  25. #include "cffload.h"
  26. /*************************************************************************/
  27. /* */
  28. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  29. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  30. /* messages during execution. */
  31. /* */
  32. #undef FT_COMPONENT
  33. #define FT_COMPONENT trace_cffparse
  34. FT_LOCAL_DEF( FT_Error )
  35. cff_parser_init( CFF_Parser parser,
  36. FT_UInt code,
  37. void* object,
  38. FT_Library library,
  39. FT_UInt stackSize,
  40. FT_UShort num_designs,
  41. FT_UShort num_axes )
  42. {
  43. FT_Memory memory = library->memory; /* for FT_NEW_ARRAY */
  44. FT_Error error; /* for FT_NEW_ARRAY */
  45. FT_ZERO( parser );
  46. #if 0
  47. parser->top = parser->stack;
  48. #endif
  49. parser->object_code = code;
  50. parser->object = object;
  51. parser->library = library;
  52. parser->num_designs = num_designs;
  53. parser->num_axes = num_axes;
  54. /* allocate the stack buffer */
  55. if ( FT_NEW_ARRAY( parser->stack, stackSize ) )
  56. {
  57. FT_FREE( parser->stack );
  58. goto Exit;
  59. }
  60. parser->stackSize = stackSize;
  61. parser->top = parser->stack; /* empty stack */
  62. Exit:
  63. return error;
  64. }
  65. FT_LOCAL_DEF( void )
  66. cff_parser_done( CFF_Parser parser )
  67. {
  68. FT_Memory memory = parser->library->memory; /* for FT_FREE */
  69. FT_FREE( parser->stack );
  70. }
  71. /* read an integer */
  72. static FT_Long
  73. cff_parse_integer( FT_Byte* start,
  74. FT_Byte* limit )
  75. {
  76. FT_Byte* p = start;
  77. FT_Int v = *p++;
  78. FT_Long val = 0;
  79. if ( v == 28 )
  80. {
  81. if ( p + 2 > limit )
  82. goto Bad;
  83. val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) | p[1] );
  84. }
  85. else if ( v == 29 )
  86. {
  87. if ( p + 4 > limit )
  88. goto Bad;
  89. val = (FT_Long)( ( (FT_ULong)p[0] << 24 ) |
  90. ( (FT_ULong)p[1] << 16 ) |
  91. ( (FT_ULong)p[2] << 8 ) |
  92. (FT_ULong)p[3] );
  93. }
  94. else if ( v < 247 )
  95. {
  96. val = v - 139;
  97. }
  98. else if ( v < 251 )
  99. {
  100. if ( p + 1 > limit )
  101. goto Bad;
  102. val = ( v - 247 ) * 256 + p[0] + 108;
  103. }
  104. else
  105. {
  106. if ( p + 1 > limit )
  107. goto Bad;
  108. val = -( v - 251 ) * 256 - p[0] - 108;
  109. }
  110. Exit:
  111. return val;
  112. Bad:
  113. val = 0;
  114. FT_TRACE4(( "!!!END OF DATA:!!!" ));
  115. goto Exit;
  116. }
  117. static const FT_Long power_tens[] =
  118. {
  119. 1L,
  120. 10L,
  121. 100L,
  122. 1000L,
  123. 10000L,
  124. 100000L,
  125. 1000000L,
  126. 10000000L,
  127. 100000000L,
  128. 1000000000L
  129. };
  130. /* maximum values allowed for multiplying */
  131. /* with the corresponding `power_tens' element */
  132. static const FT_Long power_ten_limits[] =
  133. {
  134. FT_LONG_MAX / 1L,
  135. FT_LONG_MAX / 10L,
  136. FT_LONG_MAX / 100L,
  137. FT_LONG_MAX / 1000L,
  138. FT_LONG_MAX / 10000L,
  139. FT_LONG_MAX / 100000L,
  140. FT_LONG_MAX / 1000000L,
  141. FT_LONG_MAX / 10000000L,
  142. FT_LONG_MAX / 100000000L,
  143. FT_LONG_MAX / 1000000000L,
  144. };
  145. /* read a real */
  146. static FT_Fixed
  147. cff_parse_real( FT_Byte* start,
  148. FT_Byte* limit,
  149. FT_Long power_ten,
  150. FT_Long* scaling )
  151. {
  152. FT_Byte* p = start;
  153. FT_Int nib;
  154. FT_UInt phase;
  155. FT_Long result, number, exponent;
  156. FT_Int sign = 0, exponent_sign = 0, have_overflow = 0;
  157. FT_Long exponent_add, integer_length, fraction_length;
  158. if ( scaling )
  159. *scaling = 0;
  160. result = 0;
  161. number = 0;
  162. exponent = 0;
  163. exponent_add = 0;
  164. integer_length = 0;
  165. fraction_length = 0;
  166. /* First of all, read the integer part. */
  167. phase = 4;
  168. for (;;)
  169. {
  170. /* If we entered this iteration with phase == 4, we need to */
  171. /* read a new byte. This also skips past the initial 0x1E. */
  172. if ( phase )
  173. {
  174. p++;
  175. /* Make sure we don't read past the end. */
  176. if ( p >= limit )
  177. goto Bad;
  178. }
  179. /* Get the nibble. */
  180. nib = (FT_Int)( p[0] >> phase ) & 0xF;
  181. phase = 4 - phase;
  182. if ( nib == 0xE )
  183. sign = 1;
  184. else if ( nib > 9 )
  185. break;
  186. else
  187. {
  188. /* Increase exponent if we can't add the digit. */
  189. if ( number >= 0xCCCCCCCL )
  190. exponent_add++;
  191. /* Skip leading zeros. */
  192. else if ( nib || number )
  193. {
  194. integer_length++;
  195. number = number * 10 + nib;
  196. }
  197. }
  198. }
  199. /* Read fraction part, if any. */
  200. if ( nib == 0xA )
  201. for (;;)
  202. {
  203. /* If we entered this iteration with phase == 4, we need */
  204. /* to read a new byte. */
  205. if ( phase )
  206. {
  207. p++;
  208. /* Make sure we don't read past the end. */
  209. if ( p >= limit )
  210. goto Bad;
  211. }
  212. /* Get the nibble. */
  213. nib = ( p[0] >> phase ) & 0xF;
  214. phase = 4 - phase;
  215. if ( nib >= 10 )
  216. break;
  217. /* Skip leading zeros if possible. */
  218. if ( !nib && !number )
  219. exponent_add--;
  220. /* Only add digit if we don't overflow. */
  221. else if ( number < 0xCCCCCCCL && fraction_length < 9 )
  222. {
  223. fraction_length++;
  224. number = number * 10 + nib;
  225. }
  226. }
  227. /* Read exponent, if any. */
  228. if ( nib == 12 )
  229. {
  230. exponent_sign = 1;
  231. nib = 11;
  232. }
  233. if ( nib == 11 )
  234. {
  235. for (;;)
  236. {
  237. /* If we entered this iteration with phase == 4, */
  238. /* we need to read a new byte. */
  239. if ( phase )
  240. {
  241. p++;
  242. /* Make sure we don't read past the end. */
  243. if ( p >= limit )
  244. goto Bad;
  245. }
  246. /* Get the nibble. */
  247. nib = ( p[0] >> phase ) & 0xF;
  248. phase = 4 - phase;
  249. if ( nib >= 10 )
  250. break;
  251. /* Arbitrarily limit exponent. */
  252. if ( exponent > 1000 )
  253. have_overflow = 1;
  254. else
  255. exponent = exponent * 10 + nib;
  256. }
  257. if ( exponent_sign )
  258. exponent = -exponent;
  259. }
  260. if ( !number )
  261. goto Exit;
  262. if ( have_overflow )
  263. {
  264. if ( exponent_sign )
  265. goto Underflow;
  266. else
  267. goto Overflow;
  268. }
  269. /* We don't check `power_ten' and `exponent_add'. */
  270. exponent += power_ten + exponent_add;
  271. if ( scaling )
  272. {
  273. /* Only use `fraction_length'. */
  274. fraction_length += integer_length;
  275. exponent += integer_length;
  276. if ( fraction_length <= 5 )
  277. {
  278. if ( number > 0x7FFFL )
  279. {
  280. result = FT_DivFix( number, 10 );
  281. *scaling = exponent - fraction_length + 1;
  282. }
  283. else
  284. {
  285. if ( exponent > 0 )
  286. {
  287. FT_Long new_fraction_length, shift;
  288. /* Make `scaling' as small as possible. */
  289. new_fraction_length = FT_MIN( exponent, 5 );
  290. shift = new_fraction_length - fraction_length;
  291. if ( shift > 0 )
  292. {
  293. exponent -= new_fraction_length;
  294. number *= power_tens[shift];
  295. if ( number > 0x7FFFL )
  296. {
  297. number /= 10;
  298. exponent += 1;
  299. }
  300. }
  301. else
  302. exponent -= fraction_length;
  303. }
  304. else
  305. exponent -= fraction_length;
  306. result = (FT_Long)( (FT_ULong)number << 16 );
  307. *scaling = exponent;
  308. }
  309. }
  310. else
  311. {
  312. if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
  313. {
  314. result = FT_DivFix( number, power_tens[fraction_length - 4] );
  315. *scaling = exponent - 4;
  316. }
  317. else
  318. {
  319. result = FT_DivFix( number, power_tens[fraction_length - 5] );
  320. *scaling = exponent - 5;
  321. }
  322. }
  323. }
  324. else
  325. {
  326. integer_length += exponent;
  327. fraction_length -= exponent;
  328. if ( integer_length > 5 )
  329. goto Overflow;
  330. if ( integer_length < -5 )
  331. goto Underflow;
  332. /* Remove non-significant digits. */
  333. if ( integer_length < 0 )
  334. {
  335. number /= power_tens[-integer_length];
  336. fraction_length += integer_length;
  337. }
  338. /* this can only happen if exponent was non-zero */
  339. if ( fraction_length == 10 )
  340. {
  341. number /= 10;
  342. fraction_length -= 1;
  343. }
  344. /* Convert into 16.16 format. */
  345. if ( fraction_length > 0 )
  346. {
  347. if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
  348. goto Exit;
  349. result = FT_DivFix( number, power_tens[fraction_length] );
  350. }
  351. else
  352. {
  353. number *= power_tens[-fraction_length];
  354. if ( number > 0x7FFFL )
  355. goto Overflow;
  356. result = (FT_Long)( (FT_ULong)number << 16 );
  357. }
  358. }
  359. Exit:
  360. if ( sign )
  361. result = -result;
  362. return result;
  363. Overflow:
  364. result = 0x7FFFFFFFL;
  365. FT_TRACE4(( "!!!OVERFLOW:!!!" ));
  366. goto Exit;
  367. Underflow:
  368. result = 0;
  369. FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
  370. goto Exit;
  371. Bad:
  372. result = 0;
  373. FT_TRACE4(( "!!!END OF DATA:!!!" ));
  374. goto Exit;
  375. }
  376. /* read a number, either integer or real */
  377. FT_LOCAL_DEF( FT_Long )
  378. cff_parse_num( CFF_Parser parser,
  379. FT_Byte** d )
  380. {
  381. if ( **d == 30 )
  382. {
  383. /* binary-coded decimal is truncated to integer */
  384. return cff_parse_real( *d, parser->limit, 0, NULL ) >> 16;
  385. }
  386. else if ( **d == 255 )
  387. {
  388. /* 16.16 fixed point is used internally for CFF2 blend results. */
  389. /* Since these are trusted values, a limit check is not needed. */
  390. /* After the 255, 4 bytes give the number. */
  391. /* The blend value is converted to integer, with rounding; */
  392. /* due to the right-shift we don't need the lowest byte. */
  393. #if 0
  394. return (FT_Short)(
  395. ( ( ( (FT_UInt32)*( d[0] + 1 ) << 24 ) |
  396. ( (FT_UInt32)*( d[0] + 2 ) << 16 ) |
  397. ( (FT_UInt32)*( d[0] + 3 ) << 8 ) |
  398. (FT_UInt32)*( d[0] + 4 ) ) + 0x8000U ) >> 16 );
  399. #else
  400. return (FT_Short)(
  401. ( ( ( (FT_UInt32)*( d[0] + 1 ) << 16 ) |
  402. ( (FT_UInt32)*( d[0] + 2 ) << 8 ) |
  403. (FT_UInt32)*( d[0] + 3 ) ) + 0x80U ) >> 8 );
  404. #endif
  405. }
  406. else
  407. return cff_parse_integer( *d, parser->limit );
  408. }
  409. /* read a floating point number, either integer or real */
  410. static FT_Fixed
  411. do_fixed( CFF_Parser parser,
  412. FT_Byte** d,
  413. FT_Long scaling )
  414. {
  415. if ( **d == 30 )
  416. return cff_parse_real( *d, parser->limit, scaling, NULL );
  417. else
  418. {
  419. FT_Long val = cff_parse_integer( *d, parser->limit );
  420. if ( scaling )
  421. {
  422. if ( FT_ABS( val ) > power_ten_limits[scaling] )
  423. {
  424. val = val > 0 ? 0x7FFFFFFFL : -0x7FFFFFFFL;
  425. goto Overflow;
  426. }
  427. val *= power_tens[scaling];
  428. }
  429. if ( val > 0x7FFF )
  430. {
  431. val = 0x7FFFFFFFL;
  432. goto Overflow;
  433. }
  434. else if ( val < -0x7FFF )
  435. {
  436. val = -0x7FFFFFFFL;
  437. goto Overflow;
  438. }
  439. return (FT_Long)( (FT_ULong)val << 16 );
  440. Overflow:
  441. FT_TRACE4(( "!!!OVERFLOW:!!!" ));
  442. return val;
  443. }
  444. }
  445. /* read a floating point number, either integer or real */
  446. static FT_Fixed
  447. cff_parse_fixed( CFF_Parser parser,
  448. FT_Byte** d )
  449. {
  450. return do_fixed( parser, d, 0 );
  451. }
  452. /* read a floating point number, either integer or real, */
  453. /* but return `10^scaling' times the number read in */
  454. static FT_Fixed
  455. cff_parse_fixed_scaled( CFF_Parser parser,
  456. FT_Byte** d,
  457. FT_Long scaling )
  458. {
  459. return do_fixed( parser, d, scaling );
  460. }
  461. /* read a floating point number, either integer or real, */
  462. /* and return it as precise as possible -- `scaling' returns */
  463. /* the scaling factor (as a power of 10) */
  464. static FT_Fixed
  465. cff_parse_fixed_dynamic( CFF_Parser parser,
  466. FT_Byte** d,
  467. FT_Long* scaling )
  468. {
  469. FT_ASSERT( scaling );
  470. if ( **d == 30 )
  471. return cff_parse_real( *d, parser->limit, 0, scaling );
  472. else
  473. {
  474. FT_Long number;
  475. FT_Int integer_length;
  476. number = cff_parse_integer( d[0], d[1] );
  477. if ( number > 0x7FFFL )
  478. {
  479. for ( integer_length = 5; integer_length < 10; integer_length++ )
  480. if ( number < power_tens[integer_length] )
  481. break;
  482. if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
  483. {
  484. *scaling = integer_length - 4;
  485. return FT_DivFix( number, power_tens[integer_length - 4] );
  486. }
  487. else
  488. {
  489. *scaling = integer_length - 5;
  490. return FT_DivFix( number, power_tens[integer_length - 5] );
  491. }
  492. }
  493. else
  494. {
  495. *scaling = 0;
  496. return (FT_Long)( (FT_ULong)number << 16 );
  497. }
  498. }
  499. }
  500. static FT_Error
  501. cff_parse_font_matrix( CFF_Parser parser )
  502. {
  503. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  504. FT_Matrix* matrix = &dict->font_matrix;
  505. FT_Vector* offset = &dict->font_offset;
  506. FT_ULong* upm = &dict->units_per_em;
  507. FT_Byte** data = parser->stack;
  508. FT_Error error = FT_ERR( Stack_Underflow );
  509. if ( parser->top >= parser->stack + 6 )
  510. {
  511. FT_Fixed values[6];
  512. FT_Long scalings[6];
  513. FT_Long min_scaling, max_scaling;
  514. int i;
  515. error = FT_Err_Ok;
  516. dict->has_font_matrix = TRUE;
  517. /* We expect a well-formed font matrix, this is, the matrix elements */
  518. /* `xx' and `yy' are of approximately the same magnitude. To avoid */
  519. /* loss of precision, we use the magnitude of the largest matrix */
  520. /* element to scale all other elements. The scaling factor is then */
  521. /* contained in the `units_per_em' value. */
  522. max_scaling = FT_LONG_MIN;
  523. min_scaling = FT_LONG_MAX;
  524. for ( i = 0; i < 6; i++ )
  525. {
  526. values[i] = cff_parse_fixed_dynamic( parser, data++, &scalings[i] );
  527. if ( values[i] )
  528. {
  529. if ( scalings[i] > max_scaling )
  530. max_scaling = scalings[i];
  531. if ( scalings[i] < min_scaling )
  532. min_scaling = scalings[i];
  533. }
  534. }
  535. if ( max_scaling < -9 ||
  536. max_scaling > 0 ||
  537. ( max_scaling - min_scaling ) < 0 ||
  538. ( max_scaling - min_scaling ) > 9 )
  539. {
  540. /* Return default matrix in case of unlikely values. */
  541. FT_TRACE1(( "cff_parse_font_matrix:"
  542. " strange scaling values (minimum %d, maximum %d),\n"
  543. " "
  544. " using default matrix\n", min_scaling, max_scaling ));
  545. matrix->xx = 0x10000L;
  546. matrix->yx = 0;
  547. matrix->xy = 0;
  548. matrix->yy = 0x10000L;
  549. offset->x = 0;
  550. offset->y = 0;
  551. *upm = 1;
  552. goto Exit;
  553. }
  554. for ( i = 0; i < 6; i++ )
  555. {
  556. FT_Fixed value = values[i];
  557. FT_Long divisor, half_divisor;
  558. if ( !value )
  559. continue;
  560. divisor = power_tens[max_scaling - scalings[i]];
  561. half_divisor = divisor >> 1;
  562. if ( value < 0 )
  563. {
  564. if ( FT_LONG_MIN + half_divisor < value )
  565. values[i] = ( value - half_divisor ) / divisor;
  566. else
  567. values[i] = FT_LONG_MIN / divisor;
  568. }
  569. else
  570. {
  571. if ( FT_LONG_MAX - half_divisor > value )
  572. values[i] = ( value + half_divisor ) / divisor;
  573. else
  574. values[i] = FT_LONG_MAX / divisor;
  575. }
  576. }
  577. matrix->xx = values[0];
  578. matrix->yx = values[1];
  579. matrix->xy = values[2];
  580. matrix->yy = values[3];
  581. offset->x = values[4];
  582. offset->y = values[5];
  583. *upm = (FT_ULong)power_tens[-max_scaling];
  584. FT_TRACE4(( " [%f %f %f %f %f %f]\n",
  585. (double)matrix->xx / *upm / 65536,
  586. (double)matrix->xy / *upm / 65536,
  587. (double)matrix->yx / *upm / 65536,
  588. (double)matrix->yy / *upm / 65536,
  589. (double)offset->x / *upm / 65536,
  590. (double)offset->y / *upm / 65536 ));
  591. }
  592. Exit:
  593. return error;
  594. }
  595. static FT_Error
  596. cff_parse_font_bbox( CFF_Parser parser )
  597. {
  598. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  599. FT_BBox* bbox = &dict->font_bbox;
  600. FT_Byte** data = parser->stack;
  601. FT_Error error;
  602. error = FT_ERR( Stack_Underflow );
  603. if ( parser->top >= parser->stack + 4 )
  604. {
  605. bbox->xMin = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
  606. bbox->yMin = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
  607. bbox->xMax = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
  608. bbox->yMax = FT_RoundFix( cff_parse_fixed( parser, data ) );
  609. error = FT_Err_Ok;
  610. FT_TRACE4(( " [%d %d %d %d]\n",
  611. bbox->xMin / 65536,
  612. bbox->yMin / 65536,
  613. bbox->xMax / 65536,
  614. bbox->yMax / 65536 ));
  615. }
  616. return error;
  617. }
  618. static FT_Error
  619. cff_parse_private_dict( CFF_Parser parser )
  620. {
  621. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  622. FT_Byte** data = parser->stack;
  623. FT_Error error;
  624. error = FT_ERR( Stack_Underflow );
  625. if ( parser->top >= parser->stack + 2 )
  626. {
  627. FT_Long tmp;
  628. tmp = cff_parse_num( parser, data++ );
  629. if ( tmp < 0 )
  630. {
  631. FT_ERROR(( "cff_parse_private_dict: Invalid dictionary size\n" ));
  632. error = FT_THROW( Invalid_File_Format );
  633. goto Fail;
  634. }
  635. dict->private_size = (FT_ULong)tmp;
  636. tmp = cff_parse_num( parser, data );
  637. if ( tmp < 0 )
  638. {
  639. FT_ERROR(( "cff_parse_private_dict: Invalid dictionary offset\n" ));
  640. error = FT_THROW( Invalid_File_Format );
  641. goto Fail;
  642. }
  643. dict->private_offset = (FT_ULong)tmp;
  644. FT_TRACE4(( " %lu %lu\n",
  645. dict->private_size, dict->private_offset ));
  646. error = FT_Err_Ok;
  647. }
  648. Fail:
  649. return error;
  650. }
  651. /* The `MultipleMaster' operator comes before any */
  652. /* top DICT operators that contain T2 charstrings. */
  653. static FT_Error
  654. cff_parse_multiple_master( CFF_Parser parser )
  655. {
  656. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  657. FT_Error error;
  658. #ifdef FT_DEBUG_LEVEL_TRACE
  659. /* beautify tracing message */
  660. if ( ft_trace_levels[FT_COMPONENT] < 4 )
  661. FT_TRACE1(( "Multiple Master CFFs not supported yet,"
  662. " handling first master design only\n" ));
  663. else
  664. FT_TRACE1(( " (not supported yet,"
  665. " handling first master design only)\n" ));
  666. #endif
  667. error = FT_ERR( Stack_Underflow );
  668. /* currently, we handle only the first argument */
  669. if ( parser->top >= parser->stack + 5 )
  670. {
  671. FT_Long num_designs = cff_parse_num( parser, parser->stack );
  672. if ( num_designs > 16 || num_designs < 2 )
  673. {
  674. FT_ERROR(( "cff_parse_multiple_master:"
  675. " Invalid number of designs\n" ));
  676. error = FT_THROW( Invalid_File_Format );
  677. }
  678. else
  679. {
  680. dict->num_designs = (FT_UShort)num_designs;
  681. dict->num_axes = (FT_UShort)( parser->top - parser->stack - 4 );
  682. parser->num_designs = dict->num_designs;
  683. parser->num_axes = dict->num_axes;
  684. error = FT_Err_Ok;
  685. }
  686. }
  687. return error;
  688. }
  689. static FT_Error
  690. cff_parse_cid_ros( CFF_Parser parser )
  691. {
  692. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  693. FT_Byte** data = parser->stack;
  694. FT_Error error;
  695. error = FT_ERR( Stack_Underflow );
  696. if ( parser->top >= parser->stack + 3 )
  697. {
  698. dict->cid_registry = (FT_UInt)cff_parse_num( parser, data++ );
  699. dict->cid_ordering = (FT_UInt)cff_parse_num( parser, data++ );
  700. if ( **data == 30 )
  701. FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
  702. dict->cid_supplement = cff_parse_num( parser, data );
  703. if ( dict->cid_supplement < 0 )
  704. FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
  705. dict->cid_supplement ));
  706. error = FT_Err_Ok;
  707. FT_TRACE4(( " %d %d %d\n",
  708. dict->cid_registry,
  709. dict->cid_ordering,
  710. dict->cid_supplement ));
  711. }
  712. return error;
  713. }
  714. static FT_Error
  715. cff_parse_vsindex( CFF_Parser parser )
  716. {
  717. /* vsindex operator can only be used in a Private DICT */
  718. CFF_Private priv = (CFF_Private)parser->object;
  719. FT_Byte** data = parser->stack;
  720. CFF_Blend blend;
  721. FT_Error error;
  722. if ( !priv || !priv->subfont )
  723. {
  724. error = FT_THROW( Invalid_File_Format );
  725. goto Exit;
  726. }
  727. blend = &priv->subfont->blend;
  728. if ( blend->usedBV )
  729. {
  730. FT_ERROR(( " cff_parse_vsindex: vsindex not allowed after blend\n" ));
  731. error = FT_THROW( Syntax_Error );
  732. goto Exit;
  733. }
  734. priv->vsindex = (FT_UInt)cff_parse_num( parser, data++ );
  735. FT_TRACE4(( " %d\n", priv->vsindex ));
  736. error = FT_Err_Ok;
  737. Exit:
  738. return error;
  739. }
  740. static FT_Error
  741. cff_parse_blend( CFF_Parser parser )
  742. {
  743. /* blend operator can only be used in a Private DICT */
  744. CFF_Private priv = (CFF_Private)parser->object;
  745. CFF_SubFont subFont;
  746. CFF_Blend blend;
  747. FT_UInt numBlends;
  748. FT_Error error;
  749. if ( !priv || !priv->subfont )
  750. {
  751. error = FT_THROW( Invalid_File_Format );
  752. goto Exit;
  753. }
  754. subFont = priv->subfont;
  755. blend = &subFont->blend;
  756. if ( cff_blend_check_vector( blend,
  757. priv->vsindex,
  758. subFont->lenNDV,
  759. subFont->NDV ) )
  760. {
  761. error = cff_blend_build_vector( blend,
  762. priv->vsindex,
  763. subFont->lenNDV,
  764. subFont->NDV );
  765. if ( error )
  766. goto Exit;
  767. }
  768. numBlends = (FT_UInt)cff_parse_num( parser, parser->top - 1 );
  769. if ( numBlends > parser->stackSize )
  770. {
  771. FT_ERROR(( "cff_parse_blend: Invalid number of blends\n" ));
  772. error = FT_THROW( Invalid_File_Format );
  773. goto Exit;
  774. }
  775. FT_TRACE4(( " %d value%s blended\n",
  776. numBlends,
  777. numBlends == 1 ? "" : "s" ));
  778. error = cff_blend_doBlend( subFont, parser, numBlends );
  779. blend->usedBV = TRUE;
  780. Exit:
  781. return error;
  782. }
  783. /* maxstack operator increases parser and operand stacks for CFF2 */
  784. static FT_Error
  785. cff_parse_maxstack( CFF_Parser parser )
  786. {
  787. /* maxstack operator can only be used in a Top DICT */
  788. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  789. FT_Byte** data = parser->stack;
  790. FT_Error error = FT_Err_Ok;
  791. if ( !dict )
  792. {
  793. error = FT_THROW( Invalid_File_Format );
  794. goto Exit;
  795. }
  796. dict->maxstack = (FT_UInt)cff_parse_num( parser, data++ );
  797. if ( dict->maxstack > CFF2_MAX_STACK )
  798. dict->maxstack = CFF2_MAX_STACK;
  799. if ( dict->maxstack < CFF2_DEFAULT_STACK )
  800. dict->maxstack = CFF2_DEFAULT_STACK;
  801. FT_TRACE4(( " %d\n", dict->maxstack ));
  802. Exit:
  803. return error;
  804. }
  805. #define CFF_FIELD_NUM( code, name, id ) \
  806. CFF_FIELD( code, name, id, cff_kind_num )
  807. #define CFF_FIELD_FIXED( code, name, id ) \
  808. CFF_FIELD( code, name, id, cff_kind_fixed )
  809. #define CFF_FIELD_FIXED_1000( code, name, id ) \
  810. CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
  811. #define CFF_FIELD_STRING( code, name, id ) \
  812. CFF_FIELD( code, name, id, cff_kind_string )
  813. #define CFF_FIELD_BOOL( code, name, id ) \
  814. CFF_FIELD( code, name, id, cff_kind_bool )
  815. #ifndef FT_CONFIG_OPTION_PIC
  816. #undef CFF_FIELD
  817. #undef CFF_FIELD_DELTA
  818. #ifndef FT_DEBUG_LEVEL_TRACE
  819. #define CFF_FIELD_CALLBACK( code, name, id ) \
  820. { \
  821. cff_kind_callback, \
  822. code | CFFCODE, \
  823. 0, 0, \
  824. cff_parse_ ## name, \
  825. 0, 0 \
  826. },
  827. #define CFF_FIELD_BLEND( code, id ) \
  828. { \
  829. cff_kind_blend, \
  830. code | CFFCODE, \
  831. 0, 0, \
  832. cff_parse_blend, \
  833. 0, 0 \
  834. },
  835. #define CFF_FIELD( code, name, id, kind ) \
  836. { \
  837. kind, \
  838. code | CFFCODE, \
  839. FT_FIELD_OFFSET( name ), \
  840. FT_FIELD_SIZE( name ), \
  841. 0, 0, 0 \
  842. },
  843. #define CFF_FIELD_DELTA( code, name, max, id ) \
  844. { \
  845. cff_kind_delta, \
  846. code | CFFCODE, \
  847. FT_FIELD_OFFSET( name ), \
  848. FT_FIELD_SIZE_DELTA( name ), \
  849. 0, \
  850. max, \
  851. FT_FIELD_OFFSET( num_ ## name ) \
  852. },
  853. static const CFF_Field_Handler cff_field_handlers[] =
  854. {
  855. #include "cfftoken.h"
  856. { 0, 0, 0, 0, 0, 0, 0 }
  857. };
  858. #else /* FT_DEBUG_LEVEL_TRACE */
  859. #define CFF_FIELD_CALLBACK( code, name, id ) \
  860. { \
  861. cff_kind_callback, \
  862. code | CFFCODE, \
  863. 0, 0, \
  864. cff_parse_ ## name, \
  865. 0, 0, \
  866. id \
  867. },
  868. #define CFF_FIELD_BLEND( code, id ) \
  869. { \
  870. cff_kind_blend, \
  871. code | CFFCODE, \
  872. 0, 0, \
  873. cff_parse_blend, \
  874. 0, 0, \
  875. id \
  876. },
  877. #define CFF_FIELD( code, name, id, kind ) \
  878. { \
  879. kind, \
  880. code | CFFCODE, \
  881. FT_FIELD_OFFSET( name ), \
  882. FT_FIELD_SIZE( name ), \
  883. 0, 0, 0, \
  884. id \
  885. },
  886. #define CFF_FIELD_DELTA( code, name, max, id ) \
  887. { \
  888. cff_kind_delta, \
  889. code | CFFCODE, \
  890. FT_FIELD_OFFSET( name ), \
  891. FT_FIELD_SIZE_DELTA( name ), \
  892. 0, \
  893. max, \
  894. FT_FIELD_OFFSET( num_ ## name ), \
  895. id \
  896. },
  897. static const CFF_Field_Handler cff_field_handlers[] =
  898. {
  899. #include "cfftoken.h"
  900. { 0, 0, 0, 0, 0, 0, 0, 0 }
  901. };
  902. #endif /* FT_DEBUG_LEVEL_TRACE */
  903. #else /* FT_CONFIG_OPTION_PIC */
  904. void
  905. FT_Destroy_Class_cff_field_handlers( FT_Library library,
  906. CFF_Field_Handler* clazz )
  907. {
  908. FT_Memory memory = library->memory;
  909. if ( clazz )
  910. FT_FREE( clazz );
  911. }
  912. FT_Error
  913. FT_Create_Class_cff_field_handlers( FT_Library library,
  914. CFF_Field_Handler** output_class )
  915. {
  916. CFF_Field_Handler* clazz = NULL;
  917. FT_Error error;
  918. FT_Memory memory = library->memory;
  919. int i = 0;
  920. #undef CFF_FIELD
  921. #define CFF_FIELD( code, name, id, kind ) i++;
  922. #undef CFF_FIELD_DELTA
  923. #define CFF_FIELD_DELTA( code, name, max, id ) i++;
  924. #undef CFF_FIELD_CALLBACK
  925. #define CFF_FIELD_CALLBACK( code, name, id ) i++;
  926. #undef CFF_FIELD_BLEND
  927. #define CFF_FIELD_BLEND( code, id ) i++;
  928. #include "cfftoken.h"
  929. i++; /* { 0, 0, 0, 0, 0, 0, 0 } */
  930. if ( FT_ALLOC( clazz, sizeof ( CFF_Field_Handler ) * i ) )
  931. return error;
  932. i = 0;
  933. #ifndef FT_DEBUG_LEVEL_TRACE
  934. #undef CFF_FIELD_CALLBACK
  935. #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
  936. clazz[i].kind = cff_kind_callback; \
  937. clazz[i].code = code_ | CFFCODE; \
  938. clazz[i].offset = 0; \
  939. clazz[i].size = 0; \
  940. clazz[i].reader = cff_parse_ ## name_; \
  941. clazz[i].array_max = 0; \
  942. clazz[i].count_offset = 0; \
  943. i++;
  944. #undef CFF_FIELD
  945. #define CFF_FIELD( code_, name_, id_, kind_ ) \
  946. clazz[i].kind = kind_; \
  947. clazz[i].code = code_ | CFFCODE; \
  948. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  949. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  950. clazz[i].reader = 0; \
  951. clazz[i].array_max = 0; \
  952. clazz[i].count_offset = 0; \
  953. i++; \
  954. #undef CFF_FIELD_DELTA
  955. #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
  956. clazz[i].kind = cff_kind_delta; \
  957. clazz[i].code = code_ | CFFCODE; \
  958. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  959. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  960. clazz[i].reader = 0; \
  961. clazz[i].array_max = max_; \
  962. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  963. i++;
  964. #undef CFF_FIELD_BLEND
  965. #define CFF_FIELD_BLEND( code_, id_ ) \
  966. clazz[i].kind = cff_kind_blend; \
  967. clazz[i].code = code_ | CFFCODE; \
  968. clazz[i].offset = 0; \
  969. clazz[i].size = 0; \
  970. clazz[i].reader = cff_parse_blend; \
  971. clazz[i].array_max = 0; \
  972. clazz[i].count_offset = 0; \
  973. i++;
  974. #include "cfftoken.h"
  975. clazz[i].kind = 0;
  976. clazz[i].code = 0;
  977. clazz[i].offset = 0;
  978. clazz[i].size = 0;
  979. clazz[i].reader = 0;
  980. clazz[i].array_max = 0;
  981. clazz[i].count_offset = 0;
  982. #else /* FT_DEBUG_LEVEL_TRACE */
  983. #undef CFF_FIELD_CALLBACK
  984. #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
  985. clazz[i].kind = cff_kind_callback; \
  986. clazz[i].code = code_ | CFFCODE; \
  987. clazz[i].offset = 0; \
  988. clazz[i].size = 0; \
  989. clazz[i].reader = cff_parse_ ## name_; \
  990. clazz[i].array_max = 0; \
  991. clazz[i].count_offset = 0; \
  992. clazz[i].id = id_; \
  993. i++;
  994. #undef CFF_FIELD
  995. #define CFF_FIELD( code_, name_, id_, kind_ ) \
  996. clazz[i].kind = kind_; \
  997. clazz[i].code = code_ | CFFCODE; \
  998. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  999. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  1000. clazz[i].reader = 0; \
  1001. clazz[i].array_max = 0; \
  1002. clazz[i].count_offset = 0; \
  1003. clazz[i].id = id_; \
  1004. i++; \
  1005. #undef CFF_FIELD_DELTA
  1006. #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
  1007. clazz[i].kind = cff_kind_delta; \
  1008. clazz[i].code = code_ | CFFCODE; \
  1009. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  1010. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  1011. clazz[i].reader = 0; \
  1012. clazz[i].array_max = max_; \
  1013. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  1014. clazz[i].id = id_; \
  1015. i++;
  1016. #undef CFF_FIELD_BLEND
  1017. #define CFF_FIELD_BLEND( code_, id_ ) \
  1018. clazz[i].kind = cff_kind_blend; \
  1019. clazz[i].code = code_ | CFFCODE; \
  1020. clazz[i].offset = 0; \
  1021. clazz[i].size = 0; \
  1022. clazz[i].reader = cff_parse_blend; \
  1023. clazz[i].array_max = 0; \
  1024. clazz[i].count_offset = 0; \
  1025. clazz[i].id = id_; \
  1026. i++;
  1027. #include "cfftoken.h"
  1028. clazz[i].kind = 0;
  1029. clazz[i].code = 0;
  1030. clazz[i].offset = 0;
  1031. clazz[i].size = 0;
  1032. clazz[i].reader = 0;
  1033. clazz[i].array_max = 0;
  1034. clazz[i].count_offset = 0;
  1035. clazz[i].id = 0;
  1036. #endif /* FT_DEBUG_LEVEL_TRACE */
  1037. *output_class = clazz;
  1038. return FT_Err_Ok;
  1039. }
  1040. #endif /* FT_CONFIG_OPTION_PIC */
  1041. FT_LOCAL_DEF( FT_Error )
  1042. cff_parser_run( CFF_Parser parser,
  1043. FT_Byte* start,
  1044. FT_Byte* limit )
  1045. {
  1046. #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
  1047. PSAux_Service psaux;
  1048. #endif
  1049. FT_Byte* p = start;
  1050. FT_Error error = FT_Err_Ok;
  1051. FT_Library library = parser->library;
  1052. FT_UNUSED( library );
  1053. parser->top = parser->stack;
  1054. parser->start = start;
  1055. parser->limit = limit;
  1056. parser->cursor = start;
  1057. while ( p < limit )
  1058. {
  1059. FT_UInt v = *p;
  1060. /* Opcode 31 is legacy MM T2 operator, not a number. */
  1061. /* Opcode 255 is reserved and should not appear in fonts; */
  1062. /* it is used internally for CFF2 blends. */
  1063. if ( v >= 27 && v != 31 && v != 255 )
  1064. {
  1065. /* it's a number; we will push its position on the stack */
  1066. if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
  1067. goto Stack_Overflow;
  1068. *parser->top++ = p;
  1069. /* now, skip it */
  1070. if ( v == 30 )
  1071. {
  1072. /* skip real number */
  1073. p++;
  1074. for (;;)
  1075. {
  1076. /* An unterminated floating point number at the */
  1077. /* end of a dictionary is invalid but harmless. */
  1078. if ( p >= limit )
  1079. goto Exit;
  1080. v = p[0] >> 4;
  1081. if ( v == 15 )
  1082. break;
  1083. v = p[0] & 0xF;
  1084. if ( v == 15 )
  1085. break;
  1086. p++;
  1087. }
  1088. }
  1089. else if ( v == 28 )
  1090. p += 2;
  1091. else if ( v == 29 )
  1092. p += 4;
  1093. else if ( v > 246 )
  1094. p += 1;
  1095. }
  1096. #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
  1097. else if ( v == 31 )
  1098. {
  1099. /* a Type 2 charstring */
  1100. CFF_Decoder decoder;
  1101. CFF_FontRec cff_rec;
  1102. FT_Byte* charstring_base;
  1103. FT_ULong charstring_len;
  1104. FT_Fixed* stack;
  1105. FT_Byte* q;
  1106. charstring_base = ++p;
  1107. /* search `endchar' operator */
  1108. for (;;)
  1109. {
  1110. if ( p >= limit )
  1111. goto Exit;
  1112. if ( *p == 14 )
  1113. break;
  1114. p++;
  1115. }
  1116. charstring_len = (FT_ULong)( p - charstring_base ) + 1;
  1117. /* construct CFF_Decoder object */
  1118. FT_ZERO( &decoder );
  1119. FT_ZERO( &cff_rec );
  1120. cff_rec.top_font.font_dict.num_designs = parser->num_designs;
  1121. cff_rec.top_font.font_dict.num_axes = parser->num_axes;
  1122. decoder.cff = &cff_rec;
  1123. psaux = (PSAux_Service)FT_Get_Module_Interface( library, "psaux" );
  1124. if ( !psaux )
  1125. {
  1126. FT_ERROR(( "cff_parser_run: cannot access `psaux' module\n" ));
  1127. error = FT_THROW( Missing_Module );
  1128. goto Exit;
  1129. }
  1130. error = psaux->cff_decoder_funcs->parse_charstrings_old(
  1131. &decoder, charstring_base, charstring_len, 1 );
  1132. /* Now copy the stack data in the temporary decoder object, */
  1133. /* converting it back to charstring number representations */
  1134. /* (this is ugly, I know). */
  1135. /* */
  1136. /* We overwrite the original top DICT charstring under the */
  1137. /* assumption that the charstring representation of the result */
  1138. /* of `cff_decoder_parse_charstrings' is shorter, which should */
  1139. /* be always true. */
  1140. q = charstring_base - 1;
  1141. stack = decoder.stack;
  1142. while ( stack < decoder.top )
  1143. {
  1144. FT_ULong num;
  1145. FT_Bool neg;
  1146. if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
  1147. goto Stack_Overflow;
  1148. *parser->top++ = q;
  1149. if ( *stack < 0 )
  1150. {
  1151. num = (FT_ULong)-*stack;
  1152. neg = 1;
  1153. }
  1154. else
  1155. {
  1156. num = (FT_ULong)*stack;
  1157. neg = 0;
  1158. }
  1159. if ( num & 0xFFFFU )
  1160. {
  1161. if ( neg )
  1162. num = (FT_ULong)-num;
  1163. *q++ = 255;
  1164. *q++ = ( num & 0xFF000000U ) >> 24;
  1165. *q++ = ( num & 0x00FF0000U ) >> 16;
  1166. *q++ = ( num & 0x0000FF00U ) >> 8;
  1167. *q++ = num & 0x000000FFU;
  1168. }
  1169. else
  1170. {
  1171. num >>= 16;
  1172. if ( neg )
  1173. {
  1174. if ( num <= 107 )
  1175. *q++ = (FT_Byte)( 139 - num );
  1176. else if ( num <= 1131 )
  1177. {
  1178. *q++ = (FT_Byte)( ( ( num - 108 ) >> 8 ) + 251 );
  1179. *q++ = (FT_Byte)( ( num - 108 ) & 0xFF );
  1180. }
  1181. else
  1182. {
  1183. num = (FT_ULong)-num;
  1184. *q++ = 28;
  1185. *q++ = (FT_Byte)( num >> 8 );
  1186. *q++ = (FT_Byte)( num & 0xFF );
  1187. }
  1188. }
  1189. else
  1190. {
  1191. if ( num <= 107 )
  1192. *q++ = (FT_Byte)( num + 139 );
  1193. else if ( num <= 1131 )
  1194. {
  1195. *q++ = (FT_Byte)( ( ( num - 108 ) >> 8 ) + 247 );
  1196. *q++ = (FT_Byte)( ( num - 108 ) & 0xFF );
  1197. }
  1198. else
  1199. {
  1200. *q++ = 28;
  1201. *q++ = (FT_Byte)( num >> 8 );
  1202. *q++ = (FT_Byte)( num & 0xFF );
  1203. }
  1204. }
  1205. }
  1206. stack++;
  1207. }
  1208. }
  1209. #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
  1210. else
  1211. {
  1212. /* This is not a number, hence it's an operator. Compute its code */
  1213. /* and look for it in our current list. */
  1214. FT_UInt code;
  1215. FT_UInt num_args;
  1216. const CFF_Field_Handler* field;
  1217. if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
  1218. goto Stack_Overflow;
  1219. num_args = (FT_UInt)( parser->top - parser->stack );
  1220. *parser->top = p;
  1221. code = v;
  1222. if ( v == 12 )
  1223. {
  1224. /* two byte operator */
  1225. p++;
  1226. if ( p >= limit )
  1227. goto Syntax_Error;
  1228. code = 0x100 | p[0];
  1229. }
  1230. code = code | parser->object_code;
  1231. for ( field = CFF_FIELD_HANDLERS_GET; field->kind; field++ )
  1232. {
  1233. if ( field->code == (FT_Int)code )
  1234. {
  1235. /* we found our field's handler; read it */
  1236. FT_Long val;
  1237. FT_Byte* q = (FT_Byte*)parser->object + field->offset;
  1238. #ifdef FT_DEBUG_LEVEL_TRACE
  1239. FT_TRACE4(( " %s", field->id ));
  1240. #endif
  1241. /* check that we have enough arguments -- except for */
  1242. /* delta encoded arrays, which can be empty */
  1243. if ( field->kind != cff_kind_delta && num_args < 1 )
  1244. goto Stack_Underflow;
  1245. switch ( field->kind )
  1246. {
  1247. case cff_kind_bool:
  1248. case cff_kind_string:
  1249. case cff_kind_num:
  1250. val = cff_parse_num( parser, parser->stack );
  1251. goto Store_Number;
  1252. case cff_kind_fixed:
  1253. val = cff_parse_fixed( parser, parser->stack );
  1254. goto Store_Number;
  1255. case cff_kind_fixed_thousand:
  1256. val = cff_parse_fixed_scaled( parser, parser->stack, 3 );
  1257. Store_Number:
  1258. switch ( field->size )
  1259. {
  1260. case (8 / FT_CHAR_BIT):
  1261. *(FT_Byte*)q = (FT_Byte)val;
  1262. break;
  1263. case (16 / FT_CHAR_BIT):
  1264. *(FT_Short*)q = (FT_Short)val;
  1265. break;
  1266. case (32 / FT_CHAR_BIT):
  1267. *(FT_Int32*)q = (FT_Int)val;
  1268. break;
  1269. default: /* for 64-bit systems */
  1270. *(FT_Long*)q = val;
  1271. }
  1272. #ifdef FT_DEBUG_LEVEL_TRACE
  1273. switch ( field->kind )
  1274. {
  1275. case cff_kind_bool:
  1276. FT_TRACE4(( " %s\n", val ? "true" : "false" ));
  1277. break;
  1278. case cff_kind_string:
  1279. FT_TRACE4(( " %ld (SID)\n", val ));
  1280. break;
  1281. case cff_kind_num:
  1282. FT_TRACE4(( " %ld\n", val ));
  1283. break;
  1284. case cff_kind_fixed:
  1285. FT_TRACE4(( " %f\n", (double)val / 65536 ));
  1286. break;
  1287. case cff_kind_fixed_thousand:
  1288. FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
  1289. default:
  1290. ; /* never reached */
  1291. }
  1292. #endif
  1293. break;
  1294. case cff_kind_delta:
  1295. {
  1296. FT_Byte* qcount = (FT_Byte*)parser->object +
  1297. field->count_offset;
  1298. FT_Byte** data = parser->stack;
  1299. if ( num_args > field->array_max )
  1300. num_args = field->array_max;
  1301. FT_TRACE4(( " [" ));
  1302. /* store count */
  1303. *qcount = (FT_Byte)num_args;
  1304. val = 0;
  1305. while ( num_args > 0 )
  1306. {
  1307. val = ADD_LONG( val, cff_parse_num( parser, data++ ) );
  1308. switch ( field->size )
  1309. {
  1310. case (8 / FT_CHAR_BIT):
  1311. *(FT_Byte*)q = (FT_Byte)val;
  1312. break;
  1313. case (16 / FT_CHAR_BIT):
  1314. *(FT_Short*)q = (FT_Short)val;
  1315. break;
  1316. case (32 / FT_CHAR_BIT):
  1317. *(FT_Int32*)q = (FT_Int)val;
  1318. break;
  1319. default: /* for 64-bit systems */
  1320. *(FT_Long*)q = val;
  1321. }
  1322. FT_TRACE4(( " %ld", val ));
  1323. q += field->size;
  1324. num_args--;
  1325. }
  1326. FT_TRACE4(( "]\n" ));
  1327. }
  1328. break;
  1329. default: /* callback or blend */
  1330. error = field->reader( parser );
  1331. if ( error )
  1332. goto Exit;
  1333. }
  1334. goto Found;
  1335. }
  1336. }
  1337. /* this is an unknown operator, or it is unsupported; */
  1338. /* we will ignore it for now. */
  1339. Found:
  1340. /* clear stack */
  1341. /* TODO: could clear blend stack here, */
  1342. /* but we don't have access to subFont */
  1343. if ( field->kind != cff_kind_blend )
  1344. parser->top = parser->stack;
  1345. }
  1346. p++;
  1347. }
  1348. Exit:
  1349. return error;
  1350. Stack_Overflow:
  1351. error = FT_THROW( Invalid_Argument );
  1352. goto Exit;
  1353. Stack_Underflow:
  1354. error = FT_THROW( Invalid_Argument );
  1355. goto Exit;
  1356. Syntax_Error:
  1357. error = FT_THROW( Invalid_Argument );
  1358. goto Exit;
  1359. }
  1360. /* END */