cffparse.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /***************************************************************************/
  2. /* */
  3. /* cffparse.c */
  4. /* */
  5. /* CFF token stream parser (body) */
  6. /* */
  7. /* Copyright 1996-2001, 2002 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 "cfferrs.h"
  21. /*************************************************************************/
  22. /* */
  23. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  24. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  25. /* messages during execution. */
  26. /* */
  27. #undef FT_COMPONENT
  28. #define FT_COMPONENT trace_cffparse
  29. enum
  30. {
  31. cff_kind_none = 0,
  32. cff_kind_num,
  33. cff_kind_fixed,
  34. cff_kind_string,
  35. cff_kind_bool,
  36. cff_kind_delta,
  37. cff_kind_callback,
  38. cff_kind_max /* do not remove */
  39. };
  40. /* now generate handlers for the most simple fields */
  41. typedef FT_Error (*CFF_Field_Reader)( CFF_Parser parser );
  42. typedef struct CFF_Field_Handler_
  43. {
  44. int kind;
  45. int code;
  46. FT_UInt offset;
  47. FT_Byte size;
  48. CFF_Field_Reader reader;
  49. FT_UInt array_max;
  50. FT_UInt count_offset;
  51. } CFF_Field_Handler;
  52. FT_LOCAL_DEF( void )
  53. cff_parser_init( CFF_Parser parser,
  54. FT_UInt code,
  55. void* object )
  56. {
  57. FT_MEM_ZERO( parser, sizeof ( *parser ) );
  58. parser->top = parser->stack;
  59. parser->object_code = code;
  60. parser->object = object;
  61. }
  62. /* read an integer */
  63. static FT_Long
  64. cff_parse_integer( FT_Byte* start,
  65. FT_Byte* limit )
  66. {
  67. FT_Byte* p = start;
  68. FT_Int v = *p++;
  69. FT_Long val = 0;
  70. if ( v == 28 )
  71. {
  72. if ( p + 2 > limit )
  73. goto Bad;
  74. val = (FT_Short)( ( (FT_Int)p[0] << 8 ) | p[1] );
  75. p += 2;
  76. }
  77. else if ( v == 29 )
  78. {
  79. if ( p + 4 > limit )
  80. goto Bad;
  81. val = ( (FT_Long)p[0] << 24 ) |
  82. ( (FT_Long)p[1] << 16 ) |
  83. ( (FT_Long)p[2] << 8 ) |
  84. p[3];
  85. p += 4;
  86. }
  87. else if ( v < 247 )
  88. {
  89. val = v - 139;
  90. }
  91. else if ( v < 251 )
  92. {
  93. if ( p + 1 > limit )
  94. goto Bad;
  95. val = ( v - 247 ) * 256 + p[0] + 108;
  96. p++;
  97. }
  98. else
  99. {
  100. if ( p + 1 > limit )
  101. goto Bad;
  102. val = -( v - 251 ) * 256 - p[0] - 108;
  103. p++;
  104. }
  105. Exit:
  106. return val;
  107. Bad:
  108. val = 0;
  109. goto Exit;
  110. }
  111. /* read a real */
  112. static FT_Fixed
  113. cff_parse_real( FT_Byte* start,
  114. FT_Byte* limit,
  115. FT_Int power_ten )
  116. {
  117. FT_Byte* p = start;
  118. FT_Long num, divider, result, exp;
  119. FT_Int sign = 0, exp_sign = 0;
  120. FT_UInt nib;
  121. FT_UInt phase;
  122. result = 0;
  123. num = 0;
  124. divider = 1;
  125. /* first of all, read the integer part */
  126. phase = 4;
  127. for (;;)
  128. {
  129. /* If we entered this iteration with phase == 4, we need to */
  130. /* read a new byte. This also skips past the intial 0x1E. */
  131. if ( phase )
  132. {
  133. p++;
  134. /* Make sure we don't read past the end. */
  135. if ( p >= limit )
  136. goto Bad;
  137. }
  138. /* Get the nibble. */
  139. nib = ( p[0] >> phase ) & 0xF;
  140. phase = 4 - phase;
  141. if ( nib == 0xE )
  142. sign = 1;
  143. else if ( nib > 9 )
  144. break;
  145. else
  146. result = result * 10 + nib;
  147. }
  148. /* read decimal part, if any */
  149. if ( nib == 0xa )
  150. for (;;)
  151. {
  152. /* If we entered this iteration with phase == 4, we need */
  153. /* to read a new byte. */
  154. if ( phase )
  155. {
  156. p++;
  157. /* Make sure we don't read past the end. */
  158. if ( p >= limit )
  159. goto Bad;
  160. }
  161. /* Get the nibble. */
  162. nib = ( p[0] >> phase ) & 0xF;
  163. phase = 4 - phase;
  164. if ( nib >= 10 )
  165. break;
  166. if ( divider < 10000000L )
  167. {
  168. num = num * 10 + nib;
  169. divider *= 10;
  170. }
  171. }
  172. /* read exponent, if any */
  173. if ( nib == 12 )
  174. {
  175. exp_sign = 1;
  176. nib = 11;
  177. }
  178. if ( nib == 11 )
  179. {
  180. exp = 0;
  181. for (;;)
  182. {
  183. /* If we entered this iteration with phase == 4, we need */
  184. /* to read a new byte. */
  185. if ( phase )
  186. {
  187. p++;
  188. /* Make sure we don't read past the end. */
  189. if ( p >= limit )
  190. goto Bad;
  191. }
  192. /* Get the nibble. */
  193. nib = ( p[0] >> phase ) & 0xF;
  194. phase = 4 - phase;
  195. if ( nib >= 10 )
  196. break;
  197. exp = exp * 10 + nib;
  198. }
  199. if ( exp_sign )
  200. exp = -exp;
  201. power_ten += (FT_Int)exp;
  202. }
  203. /* raise to power of ten if needed */
  204. while ( power_ten > 0 )
  205. {
  206. result = result * 10;
  207. num = num * 10;
  208. power_ten--;
  209. }
  210. while ( power_ten < 0 )
  211. {
  212. result = result / 10;
  213. divider = divider * 10;
  214. power_ten++;
  215. }
  216. /* Move the integer part into the high 16 bits. */
  217. result <<= 16;
  218. /* Place the decimal part into the low 16 bits. */
  219. if ( num )
  220. result |= FT_DivFix( num, divider );
  221. if ( sign )
  222. result = -result;
  223. Exit:
  224. return result;
  225. Bad:
  226. result = 0;
  227. goto Exit;
  228. }
  229. /* read a number, either integer or real */
  230. static FT_Long
  231. cff_parse_num( FT_Byte** d )
  232. {
  233. return ( **d == 30 ? ( cff_parse_real ( d[0], d[1], 0 ) >> 16 )
  234. : cff_parse_integer( d[0], d[1] ) );
  235. }
  236. /* read a floating point number, either integer or real */
  237. static FT_Fixed
  238. cff_parse_fixed( FT_Byte** d )
  239. {
  240. return ( **d == 30 ? cff_parse_real ( d[0], d[1], 0 )
  241. : cff_parse_integer( d[0], d[1] ) << 16 );
  242. }
  243. /* read a floating point number, either integer or real, */
  244. /* but return 1000 times the number read in. */
  245. static FT_Fixed
  246. cff_parse_fixed_thousand( FT_Byte** d )
  247. {
  248. return **d ==
  249. 30 ? cff_parse_real ( d[0], d[1], 3 )
  250. : (FT_Fixed)FT_MulFix( cff_parse_integer( d[0], d[1] ) << 16, 1000 );
  251. }
  252. static FT_Error
  253. cff_parse_font_matrix( CFF_Parser parser )
  254. {
  255. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  256. FT_Matrix* matrix = &dict->font_matrix;
  257. FT_Vector* offset = &dict->font_offset;
  258. FT_UShort* upm = &dict->units_per_em;
  259. FT_Byte** data = parser->stack;
  260. FT_Error error;
  261. FT_Fixed temp;
  262. error = CFF_Err_Stack_Underflow;
  263. if ( parser->top >= parser->stack + 6 )
  264. {
  265. matrix->xx = cff_parse_fixed_thousand( data++ );
  266. matrix->yx = cff_parse_fixed_thousand( data++ );
  267. matrix->xy = cff_parse_fixed_thousand( data++ );
  268. matrix->yy = cff_parse_fixed_thousand( data++ );
  269. offset->x = cff_parse_fixed_thousand( data++ );
  270. offset->y = cff_parse_fixed_thousand( data );
  271. temp = ABS( matrix->yy );
  272. *upm = (FT_UShort)FT_DivFix( 0x10000L, FT_DivFix( temp, 1000 ) );
  273. if ( temp != 0x10000L )
  274. {
  275. matrix->xx = FT_DivFix( matrix->xx, temp );
  276. matrix->yx = FT_DivFix( matrix->yx, temp );
  277. matrix->xy = FT_DivFix( matrix->xy, temp );
  278. matrix->yy = FT_DivFix( matrix->yy, temp );
  279. offset->x = FT_DivFix( offset->x, temp );
  280. offset->y = FT_DivFix( offset->y, temp );
  281. }
  282. /* note that the offsets must be expressed in integer font units */
  283. offset->x >>= 16;
  284. offset->y >>= 16;
  285. error = CFF_Err_Ok;
  286. }
  287. return error;
  288. }
  289. static FT_Error
  290. cff_parse_font_bbox( CFF_Parser parser )
  291. {
  292. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  293. FT_BBox* bbox = &dict->font_bbox;
  294. FT_Byte** data = parser->stack;
  295. FT_Error error;
  296. error = CFF_Err_Stack_Underflow;
  297. if ( parser->top >= parser->stack + 4 )
  298. {
  299. bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  300. bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  301. bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
  302. bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
  303. error = CFF_Err_Ok;
  304. }
  305. return error;
  306. }
  307. static FT_Error
  308. cff_parse_private_dict( CFF_Parser parser )
  309. {
  310. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  311. FT_Byte** data = parser->stack;
  312. FT_Error error;
  313. error = CFF_Err_Stack_Underflow;
  314. if ( parser->top >= parser->stack + 2 )
  315. {
  316. dict->private_size = cff_parse_num( data++ );
  317. dict->private_offset = cff_parse_num( data );
  318. error = CFF_Err_Ok;
  319. }
  320. return error;
  321. }
  322. static FT_Error
  323. cff_parse_cid_ros( CFF_Parser parser )
  324. {
  325. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  326. FT_Byte** data = parser->stack;
  327. FT_Error error;
  328. error = CFF_Err_Stack_Underflow;
  329. if ( parser->top >= parser->stack + 3 )
  330. {
  331. dict->cid_registry = (FT_UInt)cff_parse_num ( data++ );
  332. dict->cid_ordering = (FT_UInt)cff_parse_num ( data++ );
  333. dict->cid_supplement = (FT_ULong)cff_parse_num( data );
  334. error = CFF_Err_Ok;
  335. }
  336. return error;
  337. }
  338. #define CFF_FIELD_NUM( code, name ) \
  339. CFF_FIELD( code, name, cff_kind_num )
  340. #define CFF_FIELD_FIXED( code, name ) \
  341. CFF_FIELD( code, name, cff_kind_fixed )
  342. #define CFF_FIELD_STRING( code, name ) \
  343. CFF_FIELD( code, name, cff_kind_string )
  344. #define CFF_FIELD_BOOL( code, name ) \
  345. CFF_FIELD( code, name, cff_kind_bool )
  346. #define CFF_FIELD_DELTA( code, name, max ) \
  347. CFF_FIELD( code, name, cff_kind_delta )
  348. #define CFF_FIELD_CALLBACK( code, name ) \
  349. { \
  350. cff_kind_callback, \
  351. code | CFFCODE, \
  352. 0, 0, \
  353. cff_parse_ ## name, \
  354. 0, 0 \
  355. },
  356. #undef CFF_FIELD
  357. #define CFF_FIELD( code, name, kind ) \
  358. { \
  359. kind, \
  360. code | CFFCODE, \
  361. FT_FIELD_OFFSET( name ), \
  362. FT_FIELD_SIZE( name ), \
  363. 0, 0, 0 \
  364. },
  365. #undef CFF_FIELD_DELTA
  366. #define CFF_FIELD_DELTA( code, name, max ) \
  367. { \
  368. cff_kind_delta, \
  369. code | CFFCODE, \
  370. FT_FIELD_OFFSET( name ), \
  371. FT_FIELD_SIZE_DELTA( name ), \
  372. 0, \
  373. max, \
  374. FT_FIELD_OFFSET( num_ ## name ) \
  375. },
  376. #define CFFCODE_TOPDICT 0x1000
  377. #define CFFCODE_PRIVATE 0x2000
  378. static const CFF_Field_Handler cff_field_handlers[] =
  379. {
  380. #include "cfftoken.h"
  381. { 0, 0, 0, 0, 0, 0, 0 }
  382. };
  383. FT_LOCAL_DEF( FT_Error )
  384. cff_parser_run( CFF_Parser parser,
  385. FT_Byte* start,
  386. FT_Byte* limit )
  387. {
  388. FT_Byte* p = start;
  389. FT_Error error = CFF_Err_Ok;
  390. parser->top = parser->stack;
  391. parser->start = start;
  392. parser->limit = limit;
  393. parser->cursor = start;
  394. while ( p < limit )
  395. {
  396. FT_UInt v = *p;
  397. if ( v >= 27 && v != 31 )
  398. {
  399. /* it's a number; we will push its position on the stack */
  400. if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
  401. goto Stack_Overflow;
  402. *parser->top ++ = p;
  403. /* now, skip it */
  404. if ( v == 30 )
  405. {
  406. /* skip real number */
  407. p++;
  408. for (;;)
  409. {
  410. if ( p >= limit )
  411. goto Syntax_Error;
  412. v = p[0] >> 4;
  413. if ( v == 15 )
  414. break;
  415. v = p[0] & 0xF;
  416. if ( v == 15 )
  417. break;
  418. p++;
  419. }
  420. }
  421. else if ( v == 28 )
  422. p += 2;
  423. else if ( v == 29 )
  424. p += 4;
  425. else if ( v > 246 )
  426. p += 1;
  427. }
  428. else
  429. {
  430. /* This is not a number, hence it's an operator. Compute its code */
  431. /* and look for it in our current list. */
  432. FT_UInt code;
  433. FT_UInt num_args = (FT_UInt)
  434. ( parser->top - parser->stack );
  435. const CFF_Field_Handler* field;
  436. *parser->top = p;
  437. code = v;
  438. if ( v == 12 )
  439. {
  440. /* two byte operator */
  441. p++;
  442. if ( p >= limit )
  443. goto Syntax_Error;
  444. code = 0x100 | p[0];
  445. }
  446. code = code | parser->object_code;
  447. for ( field = cff_field_handlers; field->kind; field++ )
  448. {
  449. if ( field->code == (FT_Int)code )
  450. {
  451. /* we found our field's handler; read it */
  452. FT_Long val;
  453. FT_Byte* q = (FT_Byte*)parser->object + field->offset;
  454. /* check that we have enough arguments -- except for */
  455. /* delta encoded arrays, which can be empty */
  456. if ( field->kind != cff_kind_delta && num_args < 1 )
  457. goto Stack_Underflow;
  458. switch ( field->kind )
  459. {
  460. case cff_kind_bool:
  461. case cff_kind_string:
  462. case cff_kind_num:
  463. val = cff_parse_num( parser->stack );
  464. goto Store_Number;
  465. case cff_kind_fixed:
  466. val = cff_parse_fixed( parser->stack );
  467. Store_Number:
  468. switch ( field->size )
  469. {
  470. case 1:
  471. *(FT_Byte*)q = (FT_Byte)val;
  472. break;
  473. case 2:
  474. *(FT_Short*)q = (FT_Short)val;
  475. break;
  476. case 4:
  477. *(FT_Int32*)q = (FT_Int)val;
  478. break;
  479. default: /* for 64-bit systems where long is 8 bytes */
  480. *(FT_Long*)q = val;
  481. }
  482. break;
  483. case cff_kind_delta:
  484. {
  485. FT_Byte* qcount = (FT_Byte*)parser->object +
  486. field->count_offset;
  487. FT_Byte** data = parser->stack;
  488. if ( num_args > field->array_max )
  489. num_args = field->array_max;
  490. /* store count */
  491. *qcount = (FT_Byte)num_args;
  492. val = 0;
  493. while ( num_args > 0 )
  494. {
  495. val += cff_parse_num( data++ );
  496. switch ( field->size )
  497. {
  498. case 1:
  499. *(FT_Byte*)q = (FT_Byte)val;
  500. break;
  501. case 2:
  502. *(FT_Short*)q = (FT_Short)val;
  503. break;
  504. case 4:
  505. *(FT_Int32*)q = (FT_Int)val;
  506. break;
  507. default: /* for 64-bit systems */
  508. *(FT_Long*)q = val;
  509. }
  510. q += field->size;
  511. num_args--;
  512. }
  513. }
  514. break;
  515. default: /* callback */
  516. error = field->reader( parser );
  517. if ( error )
  518. goto Exit;
  519. }
  520. goto Found;
  521. }
  522. }
  523. /* this is an unknown operator, or it is unsupported; */
  524. /* we will ignore it for now. */
  525. Found:
  526. /* clear stack */
  527. parser->top = parser->stack;
  528. }
  529. p++;
  530. }
  531. Exit:
  532. return error;
  533. Stack_Overflow:
  534. error = CFF_Err_Invalid_Argument;
  535. goto Exit;
  536. Stack_Underflow:
  537. error = CFF_Err_Invalid_Argument;
  538. goto Exit;
  539. Syntax_Error:
  540. error = CFF_Err_Invalid_Argument;
  541. goto Exit;
  542. }
  543. /* END */