PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/fxge/fx_freetype/fxft2.5.01/src/cff/cffparse.c

https://github.com/hunslater/pdfium
C | 1177 lines | 855 code | 242 blank | 80 comment | 126 complexity | b99a18f085e125e324d1c1cf6c6f99ec 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-2004, 2007-2013 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 "../../include/ft2build.h"
  18. #include "cffparse.h"
  19. #include "../../include/freetype/internal/ftstream.h"
  20. #include "../../include/freetype/internal/ftdebug.h"
  21. #include "cfferrs.h"
  22. #include "cffpic.h"
  23. /*************************************************************************/
  24. /* */
  25. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  26. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  27. /* messages during execution. */
  28. /* */
  29. #undef FT_COMPONENT
  30. #define FT_COMPONENT trace_cffparse
  31. FT_LOCAL_DEF( void )
  32. cff_parser_init( CFF_Parser parser,
  33. FT_UInt code,
  34. void* object,
  35. FT_Library library)
  36. {
  37. FT_MEM_ZERO( parser, sizeof ( *parser ) );
  38. parser->top = parser->stack;
  39. parser->object_code = code;
  40. parser->object = object;
  41. parser->library = library;
  42. }
  43. /* read an integer */
  44. static FT_Long
  45. cff_parse_integer( FT_Byte* start,
  46. FT_Byte* limit )
  47. {
  48. FT_Byte* p = start;
  49. FT_Int v = *p++;
  50. FT_Long val = 0;
  51. if ( v == 28 )
  52. {
  53. if ( p + 2 > limit )
  54. goto Bad;
  55. val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) | p[1] );
  56. p += 2;
  57. }
  58. else if ( v == 29 )
  59. {
  60. if ( p + 4 > limit )
  61. goto Bad;
  62. val = (FT_Long)( ( (FT_ULong)p[0] << 24 ) |
  63. ( (FT_ULong)p[1] << 16 ) |
  64. ( (FT_ULong)p[2] << 8 ) |
  65. (FT_ULong)p[3] );
  66. p += 4;
  67. }
  68. else if ( v < 247 )
  69. {
  70. val = v - 139;
  71. }
  72. else if ( v < 251 )
  73. {
  74. if ( p + 1 > limit )
  75. goto Bad;
  76. val = ( v - 247 ) * 256 + p[0] + 108;
  77. p++;
  78. }
  79. else
  80. {
  81. if ( p + 1 > limit )
  82. goto Bad;
  83. val = -( v - 251 ) * 256 - p[0] - 108;
  84. p++;
  85. }
  86. Exit:
  87. return val;
  88. Bad:
  89. val = 0;
  90. FT_TRACE4(( "!!!END OF DATA:!!!" ));
  91. goto Exit;
  92. }
  93. static const FT_Long power_tens[] =
  94. {
  95. 1L,
  96. 10L,
  97. 100L,
  98. 1000L,
  99. 10000L,
  100. 100000L,
  101. 1000000L,
  102. 10000000L,
  103. 100000000L,
  104. 1000000000L
  105. };
  106. /* read a real */
  107. static FT_Fixed
  108. cff_parse_real( FT_Byte* start,
  109. FT_Byte* limit,
  110. FT_Long power_ten,
  111. FT_Long* scaling )
  112. {
  113. FT_Byte* p = start;
  114. FT_UInt nib;
  115. FT_UInt phase;
  116. FT_Long result, number, exponent;
  117. FT_Int sign = 0, exponent_sign = 0, have_overflow = 0;
  118. FT_Long exponent_add, integer_length, fraction_length;
  119. if ( scaling )
  120. *scaling = 0;
  121. result = 0;
  122. number = 0;
  123. exponent = 0;
  124. exponent_add = 0;
  125. integer_length = 0;
  126. fraction_length = 0;
  127. /* First of all, read the integer part. */
  128. phase = 4;
  129. for (;;)
  130. {
  131. /* If we entered this iteration with phase == 4, we need to */
  132. /* read a new byte. This also skips past the initial 0x1E. */
  133. if ( phase )
  134. {
  135. p++;
  136. /* Make sure we don't read past the end. */
  137. if ( p >= limit )
  138. goto Bad;
  139. }
  140. /* Get the nibble. */
  141. nib = ( p[0] >> phase ) & 0xF;
  142. phase = 4 - phase;
  143. if ( nib == 0xE )
  144. sign = 1;
  145. else if ( nib > 9 )
  146. break;
  147. else
  148. {
  149. /* Increase exponent if we can't add the digit. */
  150. if ( number >= 0xCCCCCCCL )
  151. exponent_add++;
  152. /* Skip leading zeros. */
  153. else if ( nib || number )
  154. {
  155. integer_length++;
  156. number = number * 10 + nib;
  157. }
  158. }
  159. }
  160. /* Read fraction part, if any. */
  161. if ( nib == 0xa )
  162. for (;;)
  163. {
  164. /* If we entered this iteration with phase == 4, we need */
  165. /* to read a new byte. */
  166. if ( phase )
  167. {
  168. p++;
  169. /* Make sure we don't read past the end. */
  170. if ( p >= limit )
  171. goto Bad;
  172. }
  173. /* Get the nibble. */
  174. nib = ( p[0] >> phase ) & 0xF;
  175. phase = 4 - phase;
  176. if ( nib >= 10 )
  177. break;
  178. /* Skip leading zeros if possible. */
  179. if ( !nib && !number )
  180. exponent_add--;
  181. /* Only add digit if we don't overflow. */
  182. else if ( number < 0xCCCCCCCL && fraction_length < 9 )
  183. {
  184. fraction_length++;
  185. number = number * 10 + nib;
  186. }
  187. }
  188. /* Read exponent, if any. */
  189. if ( nib == 12 )
  190. {
  191. exponent_sign = 1;
  192. nib = 11;
  193. }
  194. if ( nib == 11 )
  195. {
  196. for (;;)
  197. {
  198. /* If we entered this iteration with phase == 4, */
  199. /* we need to read a new byte. */
  200. if ( phase )
  201. {
  202. p++;
  203. /* Make sure we don't read past the end. */
  204. if ( p >= limit )
  205. goto Bad;
  206. }
  207. /* Get the nibble. */
  208. nib = ( p[0] >> phase ) & 0xF;
  209. phase = 4 - phase;
  210. if ( nib >= 10 )
  211. break;
  212. /* Arbitrarily limit exponent. */
  213. if ( exponent > 1000 )
  214. have_overflow = 1;
  215. else
  216. exponent = exponent * 10 + nib;
  217. }
  218. if ( exponent_sign )
  219. exponent = -exponent;
  220. }
  221. if ( !number )
  222. goto Exit;
  223. if ( have_overflow )
  224. {
  225. if ( exponent_sign )
  226. goto Underflow;
  227. else
  228. goto Overflow;
  229. }
  230. /* We don't check `power_ten' and `exponent_add'. */
  231. exponent += power_ten + exponent_add;
  232. if ( scaling )
  233. {
  234. /* Only use `fraction_length'. */
  235. fraction_length += integer_length;
  236. exponent += integer_length;
  237. if ( fraction_length <= 5 )
  238. {
  239. if ( number > 0x7FFFL )
  240. {
  241. result = FT_DivFix( number, 10 );
  242. *scaling = exponent - fraction_length + 1;
  243. }
  244. else
  245. {
  246. if ( exponent > 0 )
  247. {
  248. FT_Long new_fraction_length, shift;
  249. /* Make `scaling' as small as possible. */
  250. new_fraction_length = FT_MIN( exponent, 5 );
  251. shift = new_fraction_length - fraction_length;
  252. if ( shift > 0 )
  253. {
  254. exponent -= new_fraction_length;
  255. number *= power_tens[shift];
  256. if ( number > 0x7FFFL )
  257. {
  258. number /= 10;
  259. exponent += 1;
  260. }
  261. }
  262. else
  263. exponent -= fraction_length;
  264. }
  265. else
  266. exponent -= fraction_length;
  267. result = (FT_Long)( (FT_ULong)number << 16 );
  268. *scaling = exponent;
  269. }
  270. }
  271. else
  272. {
  273. if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
  274. {
  275. result = FT_DivFix( number, power_tens[fraction_length - 4] );
  276. *scaling = exponent - 4;
  277. }
  278. else
  279. {
  280. result = FT_DivFix( number, power_tens[fraction_length - 5] );
  281. *scaling = exponent - 5;
  282. }
  283. }
  284. }
  285. else
  286. {
  287. integer_length += exponent;
  288. fraction_length -= exponent;
  289. if ( integer_length > 5 )
  290. goto Overflow;
  291. if ( integer_length < -5 )
  292. goto Underflow;
  293. /* Remove non-significant digits. */
  294. if ( integer_length < 0 )
  295. {
  296. number /= power_tens[-integer_length];
  297. fraction_length += integer_length;
  298. }
  299. /* this can only happen if exponent was non-zero */
  300. if ( fraction_length == 10 )
  301. {
  302. number /= 10;
  303. fraction_length -= 1;
  304. }
  305. /* Convert into 16.16 format. */
  306. if ( fraction_length > 0 )
  307. {
  308. if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
  309. goto Exit;
  310. result = FT_DivFix( number, power_tens[fraction_length] );
  311. }
  312. else
  313. {
  314. number *= power_tens[-fraction_length];
  315. if ( number > 0x7FFFL )
  316. goto Overflow;
  317. result = (FT_Long)( (FT_ULong)number << 16 );
  318. }
  319. }
  320. Exit:
  321. if ( sign )
  322. result = -result;
  323. return result;
  324. Overflow:
  325. result = 0x7FFFFFFFL;
  326. FT_TRACE4(( "!!!OVERFLOW:!!!" ));
  327. goto Exit;
  328. Underflow:
  329. result = 0;
  330. FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
  331. goto Exit;
  332. Bad:
  333. result = 0;
  334. FT_TRACE4(( "!!!END OF DATA:!!!" ));
  335. goto Exit;
  336. }
  337. /* read a number, either integer or real */
  338. static FT_Long
  339. cff_parse_num( FT_Byte** d )
  340. {
  341. return **d == 30 ? ( cff_parse_real( d[0], d[1], 0, NULL ) >> 16 )
  342. : cff_parse_integer( d[0], d[1] );
  343. }
  344. /* read a floating point number, either integer or real */
  345. static FT_Fixed
  346. do_fixed( FT_Byte** d,
  347. FT_Long scaling )
  348. {
  349. if ( **d == 30 )
  350. return cff_parse_real( d[0], d[1], scaling, NULL );
  351. else
  352. {
  353. FT_Long val = cff_parse_integer( d[0], d[1] );
  354. if ( scaling )
  355. val *= power_tens[scaling];
  356. if ( val > 0x7FFF )
  357. {
  358. val = 0x7FFFFFFFL;
  359. goto Overflow;
  360. }
  361. else if ( val < -0x7FFF )
  362. {
  363. val = -0x7FFFFFFFL;
  364. goto Overflow;
  365. }
  366. return (FT_Long)( (FT_ULong)val << 16 );
  367. Overflow:
  368. FT_TRACE4(( "!!!OVERFLOW:!!!" ));
  369. return val;
  370. }
  371. }
  372. /* read a floating point number, either integer or real */
  373. static FT_Fixed
  374. cff_parse_fixed( FT_Byte** d )
  375. {
  376. return do_fixed( d, 0 );
  377. }
  378. /* read a floating point number, either integer or real, */
  379. /* but return `10^scaling' times the number read in */
  380. static FT_Fixed
  381. cff_parse_fixed_scaled( FT_Byte** d,
  382. FT_Long scaling )
  383. {
  384. return do_fixed( d, scaling );
  385. }
  386. /* read a floating point number, either integer or real, */
  387. /* and return it as precise as possible -- `scaling' returns */
  388. /* the scaling factor (as a power of 10) */
  389. static FT_Fixed
  390. cff_parse_fixed_dynamic( FT_Byte** d,
  391. FT_Long* scaling )
  392. {
  393. FT_ASSERT( scaling );
  394. if ( **d == 30 )
  395. return cff_parse_real( d[0], d[1], 0, scaling );
  396. else
  397. {
  398. FT_Long number;
  399. FT_Int integer_length;
  400. number = cff_parse_integer( d[0], d[1] );
  401. if ( number > 0x7FFFL )
  402. {
  403. for ( integer_length = 5; integer_length < 10; integer_length++ )
  404. if ( number < power_tens[integer_length] )
  405. break;
  406. if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
  407. {
  408. *scaling = integer_length - 4;
  409. return FT_DivFix( number, power_tens[integer_length - 4] );
  410. }
  411. else
  412. {
  413. *scaling = integer_length - 5;
  414. return FT_DivFix( number, power_tens[integer_length - 5] );
  415. }
  416. }
  417. else
  418. {
  419. *scaling = 0;
  420. return (FT_Long)( (FT_ULong)number << 16 );
  421. }
  422. }
  423. }
  424. static FT_Error
  425. cff_parse_font_matrix( CFF_Parser parser )
  426. {
  427. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  428. FT_Matrix* matrix = &dict->font_matrix;
  429. FT_Vector* offset = &dict->font_offset;
  430. FT_ULong* upm = &dict->units_per_em;
  431. FT_Byte** data = parser->stack;
  432. FT_Error error = FT_ERR( Stack_Underflow );
  433. if ( parser->top >= parser->stack + 6 )
  434. {
  435. FT_Long scaling;
  436. error = FT_Err_Ok;
  437. dict->has_font_matrix = TRUE;
  438. /* We expect a well-formed font matrix, this is, the matrix elements */
  439. /* `xx' and `yy' are of approximately the same magnitude. To avoid */
  440. /* loss of precision, we use the magnitude of element `xx' to scale */
  441. /* all other elements. The scaling factor is then contained in the */
  442. /* `units_per_em' value. */
  443. matrix->xx = cff_parse_fixed_dynamic( data++, &scaling );
  444. scaling = -scaling;
  445. if ( scaling < 0 || scaling > 9 )
  446. {
  447. /* Return default matrix in case of unlikely values. */
  448. FT_TRACE1(( "cff_parse_font_matrix:"
  449. " strange scaling value for xx element (%d),\n"
  450. " "
  451. " using default matrix\n", scaling ));
  452. matrix->xx = 0x10000L;
  453. matrix->yx = 0;
  454. matrix->xy = 0;
  455. matrix->yy = 0x10000L;
  456. offset->x = 0;
  457. offset->y = 0;
  458. *upm = 1;
  459. goto Exit;
  460. }
  461. matrix->yx = cff_parse_fixed_scaled( data++, scaling );
  462. matrix->xy = cff_parse_fixed_scaled( data++, scaling );
  463. matrix->yy = cff_parse_fixed_scaled( data++, scaling );
  464. offset->x = cff_parse_fixed_scaled( data++, scaling );
  465. offset->y = cff_parse_fixed_scaled( data, scaling );
  466. *upm = power_tens[scaling];
  467. FT_TRACE4(( " [%f %f %f %f %f %f]\n",
  468. (double)matrix->xx / *upm / 65536,
  469. (double)matrix->xy / *upm / 65536,
  470. (double)matrix->yx / *upm / 65536,
  471. (double)matrix->yy / *upm / 65536,
  472. (double)offset->x / *upm / 65536,
  473. (double)offset->y / *upm / 65536 ));
  474. }
  475. Exit:
  476. return error;
  477. }
  478. static FT_Error
  479. cff_parse_font_bbox( CFF_Parser parser )
  480. {
  481. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  482. FT_BBox* bbox = &dict->font_bbox;
  483. FT_Byte** data = parser->stack;
  484. FT_Error error;
  485. error = FT_ERR( Stack_Underflow );
  486. if ( parser->top >= parser->stack + 4 )
  487. {
  488. bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  489. bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  490. bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
  491. bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
  492. error = FT_Err_Ok;
  493. FT_TRACE4(( " [%d %d %d %d]\n",
  494. bbox->xMin / 65536,
  495. bbox->yMin / 65536,
  496. bbox->xMax / 65536,
  497. bbox->yMax / 65536 ));
  498. }
  499. return error;
  500. }
  501. static FT_Error
  502. cff_parse_private_dict( CFF_Parser parser )
  503. {
  504. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  505. FT_Byte** data = parser->stack;
  506. FT_Error error;
  507. error = FT_ERR( Stack_Underflow );
  508. if ( parser->top >= parser->stack + 2 )
  509. {
  510. dict->private_size = cff_parse_num( data++ );
  511. dict->private_offset = cff_parse_num( data );
  512. FT_TRACE4(( " %lu %lu\n",
  513. dict->private_size, dict->private_offset ));
  514. error = FT_Err_Ok;
  515. }
  516. return error;
  517. }
  518. static FT_Error
  519. cff_parse_cid_ros( CFF_Parser parser )
  520. {
  521. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  522. FT_Byte** data = parser->stack;
  523. FT_Error error;
  524. error = FT_ERR( Stack_Underflow );
  525. if ( parser->top >= parser->stack + 3 )
  526. {
  527. dict->cid_registry = (FT_UInt)cff_parse_num( data++ );
  528. dict->cid_ordering = (FT_UInt)cff_parse_num( data++ );
  529. if ( **data == 30 )
  530. FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
  531. dict->cid_supplement = cff_parse_num( data );
  532. if ( dict->cid_supplement < 0 )
  533. FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
  534. dict->cid_supplement ));
  535. error = FT_Err_Ok;
  536. FT_TRACE4(( " %d %d %d\n",
  537. dict->cid_registry,
  538. dict->cid_ordering,
  539. dict->cid_supplement ));
  540. }
  541. return error;
  542. }
  543. #define CFF_FIELD_NUM( code, name, id ) \
  544. CFF_FIELD( code, name, id, cff_kind_num )
  545. #define CFF_FIELD_FIXED( code, name, id ) \
  546. CFF_FIELD( code, name, id, cff_kind_fixed )
  547. #define CFF_FIELD_FIXED_1000( code, name, id ) \
  548. CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
  549. #define CFF_FIELD_STRING( code, name, id ) \
  550. CFF_FIELD( code, name, id, cff_kind_string )
  551. #define CFF_FIELD_BOOL( code, name, id ) \
  552. CFF_FIELD( code, name, id, cff_kind_bool )
  553. #define CFFCODE_TOPDICT 0x1000
  554. #define CFFCODE_PRIVATE 0x2000
  555. #ifndef FT_CONFIG_OPTION_PIC
  556. #undef CFF_FIELD
  557. #undef CFF_FIELD_DELTA
  558. #ifndef FT_DEBUG_LEVEL_TRACE
  559. #define CFF_FIELD_CALLBACK( code, name, id ) \
  560. { \
  561. cff_kind_callback, \
  562. code | CFFCODE, \
  563. 0, 0, \
  564. cff_parse_ ## name, \
  565. 0, 0 \
  566. },
  567. #define CFF_FIELD( code, name, id, kind ) \
  568. { \
  569. kind, \
  570. code | CFFCODE, \
  571. FT_FIELD_OFFSET( name ), \
  572. FT_FIELD_SIZE( name ), \
  573. 0, 0, 0 \
  574. },
  575. #define CFF_FIELD_DELTA( code, name, max, id ) \
  576. { \
  577. cff_kind_delta, \
  578. code | CFFCODE, \
  579. FT_FIELD_OFFSET( name ), \
  580. FT_FIELD_SIZE_DELTA( name ), \
  581. 0, \
  582. max, \
  583. FT_FIELD_OFFSET( num_ ## name ) \
  584. },
  585. static const CFF_Field_Handler cff_field_handlers[] =
  586. {
  587. #include "cfftoken.h"
  588. { 0, 0, 0, 0, 0, 0, 0 }
  589. };
  590. #else /* FT_DEBUG_LEVEL_TRACE */
  591. #define CFF_FIELD_CALLBACK( code, name, id ) \
  592. { \
  593. cff_kind_callback, \
  594. code | CFFCODE, \
  595. 0, 0, \
  596. cff_parse_ ## name, \
  597. 0, 0, \
  598. id \
  599. },
  600. #define CFF_FIELD( code, name, id, kind ) \
  601. { \
  602. kind, \
  603. code | CFFCODE, \
  604. FT_FIELD_OFFSET( name ), \
  605. FT_FIELD_SIZE( name ), \
  606. 0, 0, 0, \
  607. id \
  608. },
  609. #define CFF_FIELD_DELTA( code, name, max, id ) \
  610. { \
  611. cff_kind_delta, \
  612. code | CFFCODE, \
  613. FT_FIELD_OFFSET( name ), \
  614. FT_FIELD_SIZE_DELTA( name ), \
  615. 0, \
  616. max, \
  617. FT_FIELD_OFFSET( num_ ## name ), \
  618. id \
  619. },
  620. static const CFF_Field_Handler cff_field_handlers[] =
  621. {
  622. #include "cfftoken.h"
  623. { 0, 0, 0, 0, 0, 0, 0, 0 }
  624. };
  625. #endif /* FT_DEBUG_LEVEL_TRACE */
  626. #else /* FT_CONFIG_OPTION_PIC */
  627. void
  628. FT_Destroy_Class_cff_field_handlers( FT_Library library,
  629. CFF_Field_Handler* clazz )
  630. {
  631. FT_Memory memory = library->memory;
  632. if ( clazz )
  633. FT_FREE( clazz );
  634. }
  635. FT_Error
  636. FT_Create_Class_cff_field_handlers( FT_Library library,
  637. CFF_Field_Handler** output_class )
  638. {
  639. CFF_Field_Handler* clazz = NULL;
  640. FT_Error error;
  641. FT_Memory memory = library->memory;
  642. int i = 0;
  643. #undef CFF_FIELD
  644. #define CFF_FIELD( code, name, id, kind ) i++;
  645. #undef CFF_FIELD_DELTA
  646. #define CFF_FIELD_DELTA( code, name, max, id ) i++;
  647. #undef CFF_FIELD_CALLBACK
  648. #define CFF_FIELD_CALLBACK( code, name, id ) i++;
  649. #include "cfftoken.h"
  650. i++; /* { 0, 0, 0, 0, 0, 0, 0 } */
  651. if ( FT_ALLOC( clazz, sizeof ( CFF_Field_Handler ) * i ) )
  652. return error;
  653. i = 0;
  654. #ifndef FT_DEBUG_LEVEL_TRACE
  655. #undef CFF_FIELD_CALLBACK
  656. #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
  657. clazz[i].kind = cff_kind_callback; \
  658. clazz[i].code = code_ | CFFCODE; \
  659. clazz[i].offset = 0; \
  660. clazz[i].size = 0; \
  661. clazz[i].reader = cff_parse_ ## name_; \
  662. clazz[i].array_max = 0; \
  663. clazz[i].count_offset = 0; \
  664. i++;
  665. #undef CFF_FIELD
  666. #define CFF_FIELD( code_, name_, id_, kind_ ) \
  667. clazz[i].kind = kind_; \
  668. clazz[i].code = code_ | CFFCODE; \
  669. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  670. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  671. clazz[i].reader = 0; \
  672. clazz[i].array_max = 0; \
  673. clazz[i].count_offset = 0; \
  674. i++; \
  675. #undef CFF_FIELD_DELTA
  676. #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
  677. clazz[i].kind = cff_kind_delta; \
  678. clazz[i].code = code_ | CFFCODE; \
  679. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  680. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  681. clazz[i].reader = 0; \
  682. clazz[i].array_max = max_; \
  683. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  684. i++;
  685. #include "cfftoken.h"
  686. clazz[i].kind = 0;
  687. clazz[i].code = 0;
  688. clazz[i].offset = 0;
  689. clazz[i].size = 0;
  690. clazz[i].reader = 0;
  691. clazz[i].array_max = 0;
  692. clazz[i].count_offset = 0;
  693. #else /* FT_DEBUG_LEVEL_TRACE */
  694. #undef CFF_FIELD_CALLBACK
  695. #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
  696. clazz[i].kind = cff_kind_callback; \
  697. clazz[i].code = code_ | CFFCODE; \
  698. clazz[i].offset = 0; \
  699. clazz[i].size = 0; \
  700. clazz[i].reader = cff_parse_ ## name_; \
  701. clazz[i].array_max = 0; \
  702. clazz[i].count_offset = 0; \
  703. clazz[i].id = id_; \
  704. i++;
  705. #undef CFF_FIELD
  706. #define CFF_FIELD( code_, name_, id_, kind_ ) \
  707. clazz[i].kind = kind_; \
  708. clazz[i].code = code_ | CFFCODE; \
  709. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  710. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  711. clazz[i].reader = 0; \
  712. clazz[i].array_max = 0; \
  713. clazz[i].count_offset = 0; \
  714. clazz[i].id = id_; \
  715. i++; \
  716. #undef CFF_FIELD_DELTA
  717. #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
  718. clazz[i].kind = cff_kind_delta; \
  719. clazz[i].code = code_ | CFFCODE; \
  720. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  721. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  722. clazz[i].reader = 0; \
  723. clazz[i].array_max = max_; \
  724. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  725. clazz[i].id = id_; \
  726. i++;
  727. #include "cfftoken.h"
  728. clazz[i].kind = 0;
  729. clazz[i].code = 0;
  730. clazz[i].offset = 0;
  731. clazz[i].size = 0;
  732. clazz[i].reader = 0;
  733. clazz[i].array_max = 0;
  734. clazz[i].count_offset = 0;
  735. clazz[i].id = 0;
  736. #endif /* FT_DEBUG_LEVEL_TRACE */
  737. *output_class = clazz;
  738. return FT_Err_Ok;
  739. }
  740. #endif /* FT_CONFIG_OPTION_PIC */
  741. FT_LOCAL_DEF( FT_Error )
  742. cff_parser_run( CFF_Parser parser,
  743. FT_Byte* start,
  744. FT_Byte* limit )
  745. {
  746. FT_Byte* p = start;
  747. FT_Error error = FT_Err_Ok;
  748. FT_Library library = parser->library;
  749. FT_UNUSED( library );
  750. parser->top = parser->stack;
  751. parser->start = start;
  752. parser->limit = limit;
  753. parser->cursor = start;
  754. while ( p < limit )
  755. {
  756. FT_UInt v = *p;
  757. if ( v >= 27 && v != 31 )
  758. {
  759. /* it's a number; we will push its position on the stack */
  760. if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
  761. goto Stack_Overflow;
  762. *parser->top ++ = p;
  763. /* now, skip it */
  764. if ( v == 30 )
  765. {
  766. /* skip real number */
  767. p++;
  768. for (;;)
  769. {
  770. /* An unterminated floating point number at the */
  771. /* end of a dictionary is invalid but harmless. */
  772. if ( p >= limit )
  773. goto Exit;
  774. v = p[0] >> 4;
  775. if ( v == 15 )
  776. break;
  777. v = p[0] & 0xF;
  778. if ( v == 15 )
  779. break;
  780. p++;
  781. }
  782. }
  783. else if ( v == 28 )
  784. p += 2;
  785. else if ( v == 29 )
  786. p += 4;
  787. else if ( v > 246 )
  788. p += 1;
  789. }
  790. else
  791. {
  792. /* This is not a number, hence it's an operator. Compute its code */
  793. /* and look for it in our current list. */
  794. FT_UInt code;
  795. FT_UInt num_args = (FT_UInt)
  796. ( parser->top - parser->stack );
  797. const CFF_Field_Handler* field;
  798. *parser->top = p;
  799. code = v;
  800. if ( v == 12 )
  801. {
  802. /* two byte operator */
  803. p++;
  804. if ( p >= limit )
  805. goto Syntax_Error;
  806. code = 0x100 | p[0];
  807. }
  808. code = code | parser->object_code;
  809. for ( field = CFF_FIELD_HANDLERS_GET; field->kind; field++ )
  810. {
  811. if ( field->code == (FT_Int)code )
  812. {
  813. /* we found our field's handler; read it */
  814. FT_Long val;
  815. FT_Byte* q = (FT_Byte*)parser->object + field->offset;
  816. #ifdef FT_DEBUG_LEVEL_TRACE
  817. FT_TRACE4(( " %s", field->id ));
  818. #endif
  819. /* check that we have enough arguments -- except for */
  820. /* delta encoded arrays, which can be empty */
  821. if ( field->kind != cff_kind_delta && num_args < 1 )
  822. goto Stack_Underflow;
  823. switch ( field->kind )
  824. {
  825. case cff_kind_bool:
  826. case cff_kind_string:
  827. case cff_kind_num:
  828. val = cff_parse_num( parser->stack );
  829. goto Store_Number;
  830. case cff_kind_fixed:
  831. val = cff_parse_fixed( parser->stack );
  832. goto Store_Number;
  833. case cff_kind_fixed_thousand:
  834. val = cff_parse_fixed_scaled( parser->stack, 3 );
  835. Store_Number:
  836. switch ( field->size )
  837. {
  838. case (8 / FT_CHAR_BIT):
  839. *(FT_Byte*)q = (FT_Byte)val;
  840. break;
  841. case (16 / FT_CHAR_BIT):
  842. *(FT_Short*)q = (FT_Short)val;
  843. break;
  844. case (32 / FT_CHAR_BIT):
  845. *(FT_Int32*)q = (FT_Int)val;
  846. break;
  847. default: /* for 64-bit systems */
  848. *(FT_Long*)q = val;
  849. }
  850. #ifdef FT_DEBUG_LEVEL_TRACE
  851. switch ( field->kind )
  852. {
  853. case cff_kind_bool:
  854. FT_TRACE4(( " %s\n", val ? "true" : "false" ));
  855. break;
  856. case cff_kind_string:
  857. FT_TRACE4(( " %ld (SID)\n", val ));
  858. break;
  859. case cff_kind_num:
  860. FT_TRACE4(( " %ld\n", val ));
  861. break;
  862. case cff_kind_fixed:
  863. FT_TRACE4(( " %f\n", (double)val / 65536 ));
  864. break;
  865. case cff_kind_fixed_thousand:
  866. FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
  867. default:
  868. ; /* never reached */
  869. }
  870. #endif
  871. break;
  872. case cff_kind_delta:
  873. {
  874. FT_Byte* qcount = (FT_Byte*)parser->object +
  875. field->count_offset;
  876. FT_Byte** data = parser->stack;
  877. if ( num_args > field->array_max )
  878. num_args = field->array_max;
  879. FT_TRACE4(( " [" ));
  880. /* store count */
  881. *qcount = (FT_Byte)num_args;
  882. val = 0;
  883. while ( num_args > 0 )
  884. {
  885. val += cff_parse_num( data++ );
  886. switch ( field->size )
  887. {
  888. case (8 / FT_CHAR_BIT):
  889. *(FT_Byte*)q = (FT_Byte)val;
  890. break;
  891. case (16 / FT_CHAR_BIT):
  892. *(FT_Short*)q = (FT_Short)val;
  893. break;
  894. case (32 / FT_CHAR_BIT):
  895. *(FT_Int32*)q = (FT_Int)val;
  896. break;
  897. default: /* for 64-bit systems */
  898. *(FT_Long*)q = val;
  899. }
  900. FT_TRACE4(( " %ld", val ));
  901. q += field->size;
  902. num_args--;
  903. }
  904. FT_TRACE4(( "]\n" ));
  905. }
  906. break;
  907. default: /* callback */
  908. error = field->reader( parser );
  909. if ( error )
  910. goto Exit;
  911. }
  912. goto Found;
  913. }
  914. }
  915. /* this is an unknown operator, or it is unsupported; */
  916. /* we will ignore it for now. */
  917. Found:
  918. /* clear stack */
  919. parser->top = parser->stack;
  920. }
  921. p++;
  922. }
  923. Exit:
  924. return error;
  925. Stack_Overflow:
  926. error = FT_THROW( Invalid_Argument );
  927. goto Exit;
  928. Stack_Underflow:
  929. error = FT_THROW( Invalid_Argument );
  930. goto Exit;
  931. Syntax_Error:
  932. error = FT_THROW( Invalid_Argument );
  933. goto Exit;
  934. }
  935. /* END */