PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/freetype2/src/cff/cffparse.c

http://github.com/zpao/v8monkey
C | 924 lines | 669 code | 175 blank | 80 comment | 116 complexity | 55bd232aa6fa77d2666be27dcaf9a206 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /***************************************************************************/
  2. /* */
  3. /* cffparse.c */
  4. /* */
  5. /* CFF token stream parser (body) */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010 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 "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_Int)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)p[0] << 24 ) |
  63. ( (FT_Long)p[1] << 16 ) |
  64. ( (FT_Long)p[2] << 8 ) |
  65. 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. goto Exit;
  91. }
  92. static const FT_Long power_tens[] =
  93. {
  94. 1L,
  95. 10L,
  96. 100L,
  97. 1000L,
  98. 10000L,
  99. 100000L,
  100. 1000000L,
  101. 10000000L,
  102. 100000000L,
  103. 1000000000L
  104. };
  105. /* read a real */
  106. static FT_Fixed
  107. cff_parse_real( FT_Byte* start,
  108. FT_Byte* limit,
  109. FT_Long power_ten,
  110. FT_Long* scaling )
  111. {
  112. FT_Byte* p = start;
  113. FT_UInt nib;
  114. FT_UInt phase;
  115. FT_Long result, number, exponent;
  116. FT_Int sign = 0, exponent_sign = 0;
  117. FT_Long exponent_add, integer_length, fraction_length;
  118. if ( scaling )
  119. *scaling = 0;
  120. result = 0;
  121. number = 0;
  122. exponent = 0;
  123. exponent_add = 0;
  124. integer_length = 0;
  125. fraction_length = 0;
  126. /* First of all, read the integer part. */
  127. phase = 4;
  128. for (;;)
  129. {
  130. /* If we entered this iteration with phase == 4, we need to */
  131. /* read a new byte. This also skips past the initial 0x1E. */
  132. if ( phase )
  133. {
  134. p++;
  135. /* Make sure we don't read past the end. */
  136. if ( p >= limit )
  137. goto Exit;
  138. }
  139. /* Get the nibble. */
  140. nib = ( p[0] >> phase ) & 0xF;
  141. phase = 4 - phase;
  142. if ( nib == 0xE )
  143. sign = 1;
  144. else if ( nib > 9 )
  145. break;
  146. else
  147. {
  148. /* Increase exponent if we can't add the digit. */
  149. if ( number >= 0xCCCCCCCL )
  150. exponent_add++;
  151. /* Skip leading zeros. */
  152. else if ( nib || number )
  153. {
  154. integer_length++;
  155. number = number * 10 + nib;
  156. }
  157. }
  158. }
  159. /* Read fraction part, if any. */
  160. if ( nib == 0xa )
  161. for (;;)
  162. {
  163. /* If we entered this iteration with phase == 4, we need */
  164. /* to read a new byte. */
  165. if ( phase )
  166. {
  167. p++;
  168. /* Make sure we don't read past the end. */
  169. if ( p >= limit )
  170. goto Exit;
  171. }
  172. /* Get the nibble. */
  173. nib = ( p[0] >> phase ) & 0xF;
  174. phase = 4 - phase;
  175. if ( nib >= 10 )
  176. break;
  177. /* Skip leading zeros if possible. */
  178. if ( !nib && !number )
  179. exponent_add--;
  180. /* Only add digit if we don't overflow. */
  181. else if ( number < 0xCCCCCCCL && fraction_length < 9 )
  182. {
  183. fraction_length++;
  184. number = number * 10 + nib;
  185. }
  186. }
  187. /* Read exponent, if any. */
  188. if ( nib == 12 )
  189. {
  190. exponent_sign = 1;
  191. nib = 11;
  192. }
  193. if ( nib == 11 )
  194. {
  195. for (;;)
  196. {
  197. /* If we entered this iteration with phase == 4, */
  198. /* we need to read a new byte. */
  199. if ( phase )
  200. {
  201. p++;
  202. /* Make sure we don't read past the end. */
  203. if ( p >= limit )
  204. goto Exit;
  205. }
  206. /* Get the nibble. */
  207. nib = ( p[0] >> phase ) & 0xF;
  208. phase = 4 - phase;
  209. if ( nib >= 10 )
  210. break;
  211. exponent = exponent * 10 + nib;
  212. /* Arbitrarily limit exponent. */
  213. if ( exponent > 1000 )
  214. goto Exit;
  215. }
  216. if ( exponent_sign )
  217. exponent = -exponent;
  218. }
  219. /* We don't check `power_ten' and `exponent_add'. */
  220. exponent += power_ten + exponent_add;
  221. if ( scaling )
  222. {
  223. /* Only use `fraction_length'. */
  224. fraction_length += integer_length;
  225. exponent += integer_length;
  226. if ( fraction_length <= 5 )
  227. {
  228. if ( number > 0x7FFFL )
  229. {
  230. result = FT_DivFix( number, 10 );
  231. *scaling = exponent - fraction_length + 1;
  232. }
  233. else
  234. {
  235. if ( exponent > 0 )
  236. {
  237. FT_Long new_fraction_length, shift;
  238. /* Make `scaling' as small as possible. */
  239. new_fraction_length = FT_MIN( exponent, 5 );
  240. exponent -= new_fraction_length;
  241. shift = new_fraction_length - fraction_length;
  242. number *= power_tens[shift];
  243. if ( number > 0x7FFFL )
  244. {
  245. number /= 10;
  246. exponent += 1;
  247. }
  248. }
  249. else
  250. exponent -= fraction_length;
  251. result = number << 16;
  252. *scaling = exponent;
  253. }
  254. }
  255. else
  256. {
  257. if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
  258. {
  259. result = FT_DivFix( number, power_tens[fraction_length - 4] );
  260. *scaling = exponent - 4;
  261. }
  262. else
  263. {
  264. result = FT_DivFix( number, power_tens[fraction_length - 5] );
  265. *scaling = exponent - 5;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. integer_length += exponent;
  272. fraction_length -= exponent;
  273. /* Check for overflow and underflow. */
  274. if ( FT_ABS( integer_length ) > 5 )
  275. goto Exit;
  276. /* Remove non-significant digits. */
  277. if ( integer_length < 0 )
  278. {
  279. number /= power_tens[-integer_length];
  280. fraction_length += integer_length;
  281. }
  282. /* this can only happen if exponent was non-zero */
  283. if ( fraction_length == 10 )
  284. {
  285. number /= 10;
  286. fraction_length -= 1;
  287. }
  288. /* Convert into 16.16 format. */
  289. if ( fraction_length > 0 )
  290. {
  291. if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
  292. goto Exit;
  293. result = FT_DivFix( number, power_tens[fraction_length] );
  294. }
  295. else
  296. {
  297. number *= power_tens[-fraction_length];
  298. if ( number > 0x7FFFL )
  299. goto Exit;
  300. result = number << 16;
  301. }
  302. }
  303. if ( sign )
  304. result = -result;
  305. Exit:
  306. return result;
  307. }
  308. /* read a number, either integer or real */
  309. static FT_Long
  310. cff_parse_num( FT_Byte** d )
  311. {
  312. return **d == 30 ? ( cff_parse_real( d[0], d[1], 0, NULL ) >> 16 )
  313. : cff_parse_integer( d[0], d[1] );
  314. }
  315. /* read a floating point number, either integer or real */
  316. static FT_Fixed
  317. cff_parse_fixed( FT_Byte** d )
  318. {
  319. return **d == 30 ? cff_parse_real( d[0], d[1], 0, NULL )
  320. : cff_parse_integer( d[0], d[1] ) << 16;
  321. }
  322. /* read a floating point number, either integer or real, */
  323. /* but return `10^scaling' times the number read in */
  324. static FT_Fixed
  325. cff_parse_fixed_scaled( FT_Byte** d,
  326. FT_Long scaling )
  327. {
  328. return **d == 30 ? cff_parse_real( d[0], d[1], scaling, NULL )
  329. : ( cff_parse_integer( d[0], d[1] ) *
  330. power_tens[scaling] ) << 16;
  331. }
  332. /* read a floating point number, either integer or real, */
  333. /* and return it as precise as possible -- `scaling' returns */
  334. /* the scaling factor (as a power of 10) */
  335. static FT_Fixed
  336. cff_parse_fixed_dynamic( FT_Byte** d,
  337. FT_Long* scaling )
  338. {
  339. FT_ASSERT( scaling );
  340. if ( **d == 30 )
  341. return cff_parse_real( d[0], d[1], 0, scaling );
  342. else
  343. {
  344. FT_Long number;
  345. FT_Int integer_length;
  346. number = cff_parse_integer( d[0], d[1] );
  347. if ( number > 0x7FFFL )
  348. {
  349. for ( integer_length = 5; integer_length < 10; integer_length++ )
  350. if ( number < power_tens[integer_length] )
  351. break;
  352. if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
  353. {
  354. *scaling = integer_length - 4;
  355. return FT_DivFix( number, power_tens[integer_length - 4] );
  356. }
  357. else
  358. {
  359. *scaling = integer_length - 5;
  360. return FT_DivFix( number, power_tens[integer_length - 5] );
  361. }
  362. }
  363. else
  364. {
  365. *scaling = 0;
  366. return number << 16;
  367. }
  368. }
  369. }
  370. static FT_Error
  371. cff_parse_font_matrix( CFF_Parser parser )
  372. {
  373. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  374. FT_Matrix* matrix = &dict->font_matrix;
  375. FT_Vector* offset = &dict->font_offset;
  376. FT_ULong* upm = &dict->units_per_em;
  377. FT_Byte** data = parser->stack;
  378. FT_Error error = CFF_Err_Stack_Underflow;
  379. if ( parser->top >= parser->stack + 6 )
  380. {
  381. FT_Long scaling;
  382. error = CFF_Err_Ok;
  383. /* We expect a well-formed font matrix, this is, the matrix elements */
  384. /* `xx' and `yy' are of approximately the same magnitude. To avoid */
  385. /* loss of precision, we use the magnitude of element `xx' to scale */
  386. /* all other elements. The scaling factor is then contained in the */
  387. /* `units_per_em' value. */
  388. matrix->xx = cff_parse_fixed_dynamic( data++, &scaling );
  389. scaling = -scaling;
  390. if ( scaling < 0 || scaling > 9 )
  391. {
  392. /* Return default matrix in case of unlikely values. */
  393. matrix->xx = 0x10000L;
  394. matrix->yx = 0;
  395. matrix->yx = 0;
  396. matrix->yy = 0x10000L;
  397. offset->x = 0;
  398. offset->y = 0;
  399. *upm = 1;
  400. goto Exit;
  401. }
  402. matrix->yx = cff_parse_fixed_scaled( data++, scaling );
  403. matrix->xy = cff_parse_fixed_scaled( data++, scaling );
  404. matrix->yy = cff_parse_fixed_scaled( data++, scaling );
  405. offset->x = cff_parse_fixed_scaled( data++, scaling );
  406. offset->y = cff_parse_fixed_scaled( data, scaling );
  407. *upm = power_tens[scaling];
  408. }
  409. Exit:
  410. return error;
  411. }
  412. static FT_Error
  413. cff_parse_font_bbox( CFF_Parser parser )
  414. {
  415. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  416. FT_BBox* bbox = &dict->font_bbox;
  417. FT_Byte** data = parser->stack;
  418. FT_Error error;
  419. error = CFF_Err_Stack_Underflow;
  420. if ( parser->top >= parser->stack + 4 )
  421. {
  422. bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  423. bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  424. bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
  425. bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
  426. error = CFF_Err_Ok;
  427. }
  428. return error;
  429. }
  430. static FT_Error
  431. cff_parse_private_dict( CFF_Parser parser )
  432. {
  433. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  434. FT_Byte** data = parser->stack;
  435. FT_Error error;
  436. error = CFF_Err_Stack_Underflow;
  437. if ( parser->top >= parser->stack + 2 )
  438. {
  439. dict->private_size = cff_parse_num( data++ );
  440. dict->private_offset = cff_parse_num( data );
  441. error = CFF_Err_Ok;
  442. }
  443. return error;
  444. }
  445. static FT_Error
  446. cff_parse_cid_ros( CFF_Parser parser )
  447. {
  448. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  449. FT_Byte** data = parser->stack;
  450. FT_Error error;
  451. error = CFF_Err_Stack_Underflow;
  452. if ( parser->top >= parser->stack + 3 )
  453. {
  454. dict->cid_registry = (FT_UInt)cff_parse_num ( data++ );
  455. dict->cid_ordering = (FT_UInt)cff_parse_num ( data++ );
  456. if ( **data == 30 )
  457. FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
  458. dict->cid_supplement = cff_parse_num( data );
  459. if ( dict->cid_supplement < 0 )
  460. FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
  461. dict->cid_supplement ));
  462. error = CFF_Err_Ok;
  463. }
  464. return error;
  465. }
  466. #define CFF_FIELD_NUM( code, name ) \
  467. CFF_FIELD( code, name, cff_kind_num )
  468. #define CFF_FIELD_FIXED( code, name ) \
  469. CFF_FIELD( code, name, cff_kind_fixed )
  470. #define CFF_FIELD_FIXED_1000( code, name ) \
  471. CFF_FIELD( code, name, cff_kind_fixed_thousand )
  472. #define CFF_FIELD_STRING( code, name ) \
  473. CFF_FIELD( code, name, cff_kind_string )
  474. #define CFF_FIELD_BOOL( code, name ) \
  475. CFF_FIELD( code, name, cff_kind_bool )
  476. #define CFF_FIELD_DELTA( code, name, max ) \
  477. CFF_FIELD( code, name, cff_kind_delta )
  478. #define CFFCODE_TOPDICT 0x1000
  479. #define CFFCODE_PRIVATE 0x2000
  480. #ifndef FT_CONFIG_OPTION_PIC
  481. #define CFF_FIELD_CALLBACK( code, name ) \
  482. { \
  483. cff_kind_callback, \
  484. code | CFFCODE, \
  485. 0, 0, \
  486. cff_parse_ ## name, \
  487. 0, 0 \
  488. },
  489. #undef CFF_FIELD
  490. #define CFF_FIELD( code, name, kind ) \
  491. { \
  492. kind, \
  493. code | CFFCODE, \
  494. FT_FIELD_OFFSET( name ), \
  495. FT_FIELD_SIZE( name ), \
  496. 0, 0, 0 \
  497. },
  498. #undef CFF_FIELD_DELTA
  499. #define CFF_FIELD_DELTA( code, name, max ) \
  500. { \
  501. cff_kind_delta, \
  502. code | CFFCODE, \
  503. FT_FIELD_OFFSET( name ), \
  504. FT_FIELD_SIZE_DELTA( name ), \
  505. 0, \
  506. max, \
  507. FT_FIELD_OFFSET( num_ ## name ) \
  508. },
  509. static const CFF_Field_Handler cff_field_handlers[] =
  510. {
  511. #include "cfftoken.h"
  512. { 0, 0, 0, 0, 0, 0, 0 }
  513. };
  514. #else /* FT_CONFIG_OPTION_PIC */
  515. void FT_Destroy_Class_cff_field_handlers(FT_Library library, CFF_Field_Handler* clazz)
  516. {
  517. FT_Memory memory = library->memory;
  518. if ( clazz )
  519. FT_FREE( clazz );
  520. }
  521. FT_Error FT_Create_Class_cff_field_handlers(FT_Library library, CFF_Field_Handler** output_class)
  522. {
  523. CFF_Field_Handler* clazz;
  524. FT_Error error;
  525. FT_Memory memory = library->memory;
  526. int i=0;
  527. #undef CFF_FIELD
  528. #undef CFF_FIELD_DELTA
  529. #undef CFF_FIELD_CALLBACK
  530. #define CFF_FIELD_CALLBACK( code, name ) i++;
  531. #define CFF_FIELD( code, name, kind ) i++;
  532. #define CFF_FIELD_DELTA( code, name, max ) i++;
  533. #include "cfftoken.h"
  534. i++;/*{ 0, 0, 0, 0, 0, 0, 0 }*/
  535. if ( FT_ALLOC( clazz, sizeof(CFF_Field_Handler)*i ) )
  536. return error;
  537. i=0;
  538. #undef CFF_FIELD
  539. #undef CFF_FIELD_DELTA
  540. #undef CFF_FIELD_CALLBACK
  541. #define CFF_FIELD_CALLBACK( code_, name_ ) \
  542. clazz[i].kind = cff_kind_callback; \
  543. clazz[i].code = code_ | CFFCODE; \
  544. clazz[i].offset = 0; \
  545. clazz[i].size = 0; \
  546. clazz[i].reader = cff_parse_ ## name_; \
  547. clazz[i].array_max = 0; \
  548. clazz[i].count_offset = 0; \
  549. i++;
  550. #undef CFF_FIELD
  551. #define CFF_FIELD( code_, name_, kind_ ) \
  552. clazz[i].kind = kind_; \
  553. clazz[i].code = code_ | CFFCODE; \
  554. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  555. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  556. clazz[i].reader = 0; \
  557. clazz[i].array_max = 0; \
  558. clazz[i].count_offset = 0; \
  559. i++; \
  560. #undef CFF_FIELD_DELTA
  561. #define CFF_FIELD_DELTA( code_, name_, max_ ) \
  562. clazz[i].kind = cff_kind_delta; \
  563. clazz[i].code = code_ | CFFCODE; \
  564. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  565. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  566. clazz[i].reader = 0; \
  567. clazz[i].array_max = max_; \
  568. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  569. i++;
  570. #include "cfftoken.h"
  571. clazz[i].kind = 0;
  572. clazz[i].code = 0;
  573. clazz[i].offset = 0;
  574. clazz[i].size = 0;
  575. clazz[i].reader = 0;
  576. clazz[i].array_max = 0;
  577. clazz[i].count_offset = 0;
  578. *output_class = clazz;
  579. return CFF_Err_Ok;
  580. }
  581. #endif /* FT_CONFIG_OPTION_PIC */
  582. FT_LOCAL_DEF( FT_Error )
  583. cff_parser_run( CFF_Parser parser,
  584. FT_Byte* start,
  585. FT_Byte* limit )
  586. {
  587. FT_Byte* p = start;
  588. FT_Error error = CFF_Err_Ok;
  589. FT_Library library = parser->library;
  590. FT_UNUSED(library);
  591. parser->top = parser->stack;
  592. parser->start = start;
  593. parser->limit = limit;
  594. parser->cursor = start;
  595. while ( p < limit )
  596. {
  597. FT_UInt v = *p;
  598. if ( v >= 27 && v != 31 )
  599. {
  600. /* it's a number; we will push its position on the stack */
  601. if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
  602. goto Stack_Overflow;
  603. *parser->top ++ = p;
  604. /* now, skip it */
  605. if ( v == 30 )
  606. {
  607. /* skip real number */
  608. p++;
  609. for (;;)
  610. {
  611. /* An unterminated floating point number at the */
  612. /* end of a dictionary is invalid but harmless. */
  613. if ( p >= limit )
  614. goto Exit;
  615. v = p[0] >> 4;
  616. if ( v == 15 )
  617. break;
  618. v = p[0] & 0xF;
  619. if ( v == 15 )
  620. break;
  621. p++;
  622. }
  623. }
  624. else if ( v == 28 )
  625. p += 2;
  626. else if ( v == 29 )
  627. p += 4;
  628. else if ( v > 246 )
  629. p += 1;
  630. }
  631. else
  632. {
  633. /* This is not a number, hence it's an operator. Compute its code */
  634. /* and look for it in our current list. */
  635. FT_UInt code;
  636. FT_UInt num_args = (FT_UInt)
  637. ( parser->top - parser->stack );
  638. const CFF_Field_Handler* field;
  639. *parser->top = p;
  640. code = v;
  641. if ( v == 12 )
  642. {
  643. /* two byte operator */
  644. p++;
  645. if ( p >= limit )
  646. goto Syntax_Error;
  647. code = 0x100 | p[0];
  648. }
  649. code = code | parser->object_code;
  650. for ( field = FT_CFF_FIELD_HANDLERS_GET; field->kind; field++ )
  651. {
  652. if ( field->code == (FT_Int)code )
  653. {
  654. /* we found our field's handler; read it */
  655. FT_Long val;
  656. FT_Byte* q = (FT_Byte*)parser->object + field->offset;
  657. /* check that we have enough arguments -- except for */
  658. /* delta encoded arrays, which can be empty */
  659. if ( field->kind != cff_kind_delta && num_args < 1 )
  660. goto Stack_Underflow;
  661. switch ( field->kind )
  662. {
  663. case cff_kind_bool:
  664. case cff_kind_string:
  665. case cff_kind_num:
  666. val = cff_parse_num( parser->stack );
  667. goto Store_Number;
  668. case cff_kind_fixed:
  669. val = cff_parse_fixed( parser->stack );
  670. goto Store_Number;
  671. case cff_kind_fixed_thousand:
  672. val = cff_parse_fixed_scaled( parser->stack, 3 );
  673. Store_Number:
  674. switch ( field->size )
  675. {
  676. case (8 / FT_CHAR_BIT):
  677. *(FT_Byte*)q = (FT_Byte)val;
  678. break;
  679. case (16 / FT_CHAR_BIT):
  680. *(FT_Short*)q = (FT_Short)val;
  681. break;
  682. case (32 / FT_CHAR_BIT):
  683. *(FT_Int32*)q = (FT_Int)val;
  684. break;
  685. default: /* for 64-bit systems */
  686. *(FT_Long*)q = val;
  687. }
  688. break;
  689. case cff_kind_delta:
  690. {
  691. FT_Byte* qcount = (FT_Byte*)parser->object +
  692. field->count_offset;
  693. FT_Byte** data = parser->stack;
  694. if ( num_args > field->array_max )
  695. num_args = field->array_max;
  696. /* store count */
  697. *qcount = (FT_Byte)num_args;
  698. val = 0;
  699. while ( num_args > 0 )
  700. {
  701. val += cff_parse_num( data++ );
  702. switch ( field->size )
  703. {
  704. case (8 / FT_CHAR_BIT):
  705. *(FT_Byte*)q = (FT_Byte)val;
  706. break;
  707. case (16 / FT_CHAR_BIT):
  708. *(FT_Short*)q = (FT_Short)val;
  709. break;
  710. case (32 / FT_CHAR_BIT):
  711. *(FT_Int32*)q = (FT_Int)val;
  712. break;
  713. default: /* for 64-bit systems */
  714. *(FT_Long*)q = val;
  715. }
  716. q += field->size;
  717. num_args--;
  718. }
  719. }
  720. break;
  721. default: /* callback */
  722. error = field->reader( parser );
  723. if ( error )
  724. goto Exit;
  725. }
  726. goto Found;
  727. }
  728. }
  729. /* this is an unknown operator, or it is unsupported; */
  730. /* we will ignore it for now. */
  731. Found:
  732. /* clear stack */
  733. parser->top = parser->stack;
  734. }
  735. p++;
  736. }
  737. Exit:
  738. return error;
  739. Stack_Overflow:
  740. error = CFF_Err_Invalid_Argument;
  741. goto Exit;
  742. Stack_Underflow:
  743. error = CFF_Err_Invalid_Argument;
  744. goto Exit;
  745. Syntax_Error:
  746. error = CFF_Err_Invalid_Argument;
  747. goto Exit;
  748. }
  749. /* END */