pfrload.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /***************************************************************************/
  2. /* */
  3. /* pfrload.c */
  4. /* */
  5. /* FreeType PFR loader (body). */
  6. /* */
  7. /* Copyright 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 "pfrload.h"
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_STREAM_H
  20. #include "pfrerror.h"
  21. #undef FT_COMPONENT
  22. #define FT_COMPONENT trace_pfr
  23. /*************************************************************************/
  24. /*************************************************************************/
  25. /***** *****/
  26. /***** EXTRA ITEMS *****/
  27. /***** *****/
  28. /*************************************************************************/
  29. /*************************************************************************/
  30. FT_LOCAL_DEF( FT_Error )
  31. pfr_extra_items_skip( FT_Byte* *pp,
  32. FT_Byte* limit )
  33. {
  34. return pfr_extra_items_parse( pp, limit, NULL, NULL );
  35. }
  36. FT_LOCAL_DEF( FT_Error )
  37. pfr_extra_items_parse( FT_Byte* *pp,
  38. FT_Byte* limit,
  39. PFR_ExtraItem item_list,
  40. FT_Pointer item_data )
  41. {
  42. FT_Error error = 0;
  43. FT_Byte* p = *pp;
  44. FT_UInt num_items, item_type, item_size;
  45. PFR_CHECK( 1 );
  46. num_items = PFR_NEXT_BYTE( p );
  47. for ( ; num_items > 0; num_items-- )
  48. {
  49. PFR_CHECK( 2 );
  50. item_size = PFR_NEXT_BYTE( p );
  51. item_type = PFR_NEXT_BYTE( p );
  52. PFR_CHECK( item_size );
  53. if ( item_list )
  54. {
  55. PFR_ExtraItem extra = item_list;
  56. for ( extra = item_list; extra->parser != NULL; extra++ )
  57. {
  58. if ( extra->type == item_type )
  59. {
  60. error = extra->parser( p, p + item_size, item_data );
  61. if ( error ) goto Exit;
  62. break;
  63. }
  64. }
  65. }
  66. p += item_size;
  67. }
  68. Exit:
  69. *pp = p;
  70. return error;
  71. Too_Short:
  72. FT_ERROR(( "pfr_extra_items_parse: invalid extra items table\n" ));
  73. error = PFR_Err_Invalid_Table;
  74. goto Exit;
  75. }
  76. /*************************************************************************/
  77. /*************************************************************************/
  78. /***** *****/
  79. /***** PFR HEADER *****/
  80. /***** *****/
  81. /*************************************************************************/
  82. /*************************************************************************/
  83. static const FT_Frame_Field pfr_header_fields[] =
  84. {
  85. #undef FT_STRUCTURE
  86. #define FT_STRUCTURE PFR_HeaderRec
  87. FT_FRAME_START( 58 ),
  88. FT_FRAME_ULONG ( signature ),
  89. FT_FRAME_USHORT( version ),
  90. FT_FRAME_USHORT( signature2 ),
  91. FT_FRAME_USHORT( header_size ),
  92. FT_FRAME_USHORT( log_dir_size ),
  93. FT_FRAME_USHORT( log_dir_offset ),
  94. FT_FRAME_USHORT( log_font_max_size ),
  95. FT_FRAME_UOFF3 ( log_font_section_size ),
  96. FT_FRAME_UOFF3 ( log_font_section_offset ),
  97. FT_FRAME_USHORT( phy_font_max_size ),
  98. FT_FRAME_UOFF3 ( phy_font_section_size ),
  99. FT_FRAME_UOFF3 ( phy_font_section_offset ),
  100. FT_FRAME_USHORT( gps_max_size ),
  101. FT_FRAME_UOFF3 ( gps_section_size ),
  102. FT_FRAME_UOFF3 ( gps_section_offset ),
  103. FT_FRAME_BYTE ( max_blue_values ),
  104. FT_FRAME_BYTE ( max_x_orus ),
  105. FT_FRAME_BYTE ( max_y_orus ),
  106. FT_FRAME_BYTE ( phy_font_max_size_high ),
  107. FT_FRAME_BYTE ( color_flags ),
  108. FT_FRAME_UOFF3 ( bct_max_size ),
  109. FT_FRAME_UOFF3 ( bct_set_max_size ),
  110. FT_FRAME_UOFF3 ( phy_bct_set_max_size ),
  111. FT_FRAME_USHORT( num_phy_fonts ),
  112. FT_FRAME_BYTE ( max_vert_stem_snap ),
  113. FT_FRAME_BYTE ( max_horz_stem_snap ),
  114. FT_FRAME_USHORT( max_chars ),
  115. FT_FRAME_END
  116. };
  117. FT_LOCAL_DEF( FT_Error )
  118. pfr_header_load( PFR_Header header,
  119. FT_Stream stream )
  120. {
  121. FT_Error error;
  122. /* read header directly */
  123. if ( !FT_STREAM_SEEK( 0 ) &&
  124. !FT_STREAM_READ_FIELDS( pfr_header_fields, header ) )
  125. {
  126. /* make a few adjustments to the header */
  127. header->phy_font_max_size +=
  128. (FT_UInt32)header->phy_font_max_size_high << 16;
  129. }
  130. return error;
  131. }
  132. FT_LOCAL_DEF( FT_Bool )
  133. pfr_header_check( PFR_Header header )
  134. {
  135. FT_Bool result = 1;
  136. /* check signature and header size */
  137. if ( header->signature != 0x50465230L || /* "PFR0" */
  138. header->version > 4 ||
  139. header->header_size < 58 ||
  140. header->signature2 != 0x0d0a ) /* CR/LF */
  141. {
  142. result = 0;
  143. }
  144. return result;
  145. }
  146. /***********************************************************************/
  147. /***********************************************************************/
  148. /***** *****/
  149. /***** PFR LOGICAL FONTS *****/
  150. /***** *****/
  151. /***********************************************************************/
  152. /***********************************************************************/
  153. FT_LOCAL_DEF( FT_Error )
  154. pfr_log_font_count( FT_Stream stream,
  155. FT_UInt32 section_offset,
  156. FT_UInt *acount )
  157. {
  158. FT_Error error;
  159. FT_UInt count;
  160. FT_UInt result = 0;
  161. if ( FT_STREAM_SEEK( section_offset ) || FT_READ_USHORT( count ) )
  162. goto Exit;
  163. result = count;
  164. Exit:
  165. *acount = result;
  166. return error;
  167. }
  168. FT_LOCAL_DEF( FT_Error )
  169. pfr_log_font_load( PFR_LogFont log_font,
  170. FT_Stream stream,
  171. FT_UInt idx,
  172. FT_UInt32 section_offset,
  173. FT_Bool size_increment )
  174. {
  175. FT_UInt num_log_fonts;
  176. FT_UInt flags;
  177. FT_UInt32 offset;
  178. FT_UInt32 size;
  179. FT_Error error;
  180. if ( FT_STREAM_SEEK( section_offset ) ||
  181. FT_READ_USHORT( num_log_fonts ) )
  182. goto Exit;
  183. if ( idx >= num_log_fonts )
  184. return PFR_Err_Invalid_Argument;
  185. if ( FT_STREAM_SKIP( idx * 5 ) ||
  186. FT_READ_USHORT( size ) ||
  187. FT_READ_UOFF3 ( offset ) )
  188. goto Exit;
  189. /* save logical font size and offset */
  190. log_font->size = size;
  191. log_font->offset = offset;
  192. /* now, check the rest of the table before loading it */
  193. {
  194. FT_Byte* p;
  195. FT_Byte* limit;
  196. FT_UInt local;
  197. if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( size ) )
  198. goto Exit;
  199. p = stream->cursor;
  200. limit = p + size;
  201. PFR_CHECK(13);
  202. log_font->matrix[0] = PFR_NEXT_LONG( p );
  203. log_font->matrix[1] = PFR_NEXT_LONG( p );
  204. log_font->matrix[2] = PFR_NEXT_LONG( p );
  205. log_font->matrix[3] = PFR_NEXT_LONG( p );
  206. flags = PFR_NEXT_BYTE( p );
  207. local = 0;
  208. if ( flags & PFR_LOG_STROKE )
  209. {
  210. local++;
  211. if ( flags & PFR_LOG_2BYTE_STROKE )
  212. local++;
  213. if ( (flags & PFR_LINE_JOIN_MASK) == PFR_LINE_JOIN_MITER )
  214. local += 3;
  215. }
  216. if ( flags & PFR_LOG_BOLD )
  217. {
  218. local++;
  219. if ( flags & PFR_LOG_2BYTE_BOLD )
  220. local++;
  221. }
  222. PFR_CHECK( local );
  223. if ( flags & PFR_LOG_STROKE )
  224. {
  225. log_font->stroke_thickness = ( flags & PFR_LOG_2BYTE_STROKE )
  226. ? PFR_NEXT_SHORT( p )
  227. : PFR_NEXT_BYTE( p );
  228. if ( ( flags & PFR_LINE_JOIN_MASK ) == PFR_LINE_JOIN_MITER )
  229. log_font->miter_limit = PFR_NEXT_LONG( p );
  230. }
  231. if ( flags & PFR_LOG_BOLD )
  232. {
  233. log_font->bold_thickness = ( flags & PFR_LOG_2BYTE_BOLD )
  234. ? PFR_NEXT_SHORT( p )
  235. : PFR_NEXT_BYTE( p );
  236. }
  237. if ( flags & PFR_LOG_EXTRA_ITEMS )
  238. {
  239. error = pfr_extra_items_skip( &p, limit );
  240. if (error) goto Fail;
  241. }
  242. PFR_CHECK(5);
  243. log_font->phys_size = PFR_NEXT_USHORT( p );
  244. log_font->phys_offset = PFR_NEXT_ULONG( p );
  245. if ( size_increment )
  246. {
  247. PFR_CHECK( 1 );
  248. log_font->phys_size += (FT_UInt32)PFR_NEXT_BYTE( p ) << 16;
  249. }
  250. }
  251. Fail:
  252. FT_FRAME_EXIT();
  253. Exit:
  254. return error;
  255. Too_Short:
  256. FT_ERROR(( "pfr_log_font_load: invalid logical font table\n" ));
  257. error = PFR_Err_Invalid_Table;
  258. goto Fail;
  259. }
  260. /***********************************************************************/
  261. /***********************************************************************/
  262. /***** *****/
  263. /***** PFR PHYSICAL FONTS *****/
  264. /***** *****/
  265. /***********************************************************************/
  266. /***********************************************************************/
  267. /* load bitmap strikes lists */
  268. FT_CALLBACK_DEF( FT_Error )
  269. pfr_extra_item_load_bitmap_info( FT_Byte* p,
  270. FT_Byte* limit,
  271. PFR_PhyFont phy_font )
  272. {
  273. FT_Memory memory = phy_font->memory;
  274. PFR_Strike strike;
  275. FT_UInt flags0;
  276. FT_UInt n, count, size1;
  277. FT_Error error = 0;
  278. PFR_CHECK( 5 );
  279. p += 3; /* skip bctSize */
  280. flags0 = PFR_NEXT_BYTE( p );
  281. count = PFR_NEXT_BYTE( p );
  282. /* re-allocate when needed */
  283. if ( phy_font->num_strikes + count > phy_font->max_strikes )
  284. {
  285. FT_UInt new_max = (phy_font->num_strikes + count + 3) & -4;
  286. if ( FT_RENEW_ARRAY( phy_font->strikes,
  287. phy_font->num_strikes,
  288. new_max ) )
  289. goto Exit;
  290. phy_font->max_strikes = new_max;
  291. }
  292. size1 = 1 + 1 + 1 + 2 + 2 + 1;
  293. if ( flags0 & PFR_STRIKE_2BYTE_XPPM )
  294. size1++;
  295. if ( flags0 & PFR_STRIKE_2BYTE_YPPM )
  296. size1++;
  297. if ( flags0 & PFR_STRIKE_3BYTE_SIZE )
  298. size1++;
  299. if ( flags0 & PFR_STRIKE_3BYTE_OFFSET )
  300. size1++;
  301. if ( flags0 & PFR_STRIKE_2BYTE_COUNT )
  302. size1++;
  303. strike = phy_font->strikes + phy_font->num_strikes;
  304. PFR_CHECK( count * size1 );
  305. for ( n = 0; n < count; n++, strike++ )
  306. {
  307. strike->x_ppm = ( flags0 & PFR_STRIKE_2BYTE_XPPM )
  308. ? PFR_NEXT_USHORT( p )
  309. : PFR_NEXT_BYTE( p );
  310. strike->y_ppm = ( flags0 & PFR_STRIKE_2BYTE_YPPM )
  311. ? PFR_NEXT_USHORT( p )
  312. : PFR_NEXT_BYTE( p );
  313. strike->flags = PFR_NEXT_BYTE( p );
  314. strike->bct_size = ( flags0 & PFR_STRIKE_3BYTE_SIZE )
  315. ? PFR_NEXT_ULONG( p )
  316. : PFR_NEXT_USHORT( p );
  317. strike->bct_offset = ( flags0 & PFR_STRIKE_3BYTE_OFFSET )
  318. ? PFR_NEXT_ULONG( p )
  319. : PFR_NEXT_USHORT( p );
  320. strike->num_bitmaps = ( flags0 & PFR_STRIKE_2BYTE_COUNT )
  321. ? PFR_NEXT_USHORT( p )
  322. : PFR_NEXT_BYTE( p );
  323. }
  324. phy_font->num_strikes += count;
  325. Exit:
  326. return error;
  327. Too_Short:
  328. error = PFR_Err_Invalid_Table;
  329. FT_ERROR(( "pfr_extra_item_load_bitmap_info: invalid bitmap info table\n" ));
  330. goto Exit;
  331. }
  332. /* load font ID, i.e. name */
  333. FT_CALLBACK_DEF( FT_Error )
  334. pfr_extra_item_load_font_id( FT_Byte* p,
  335. FT_Byte* limit,
  336. PFR_PhyFont phy_font )
  337. {
  338. FT_Error error = 0;
  339. FT_Memory memory = phy_font->memory;
  340. FT_UInt len = (FT_UInt)( limit - p );
  341. if ( phy_font->font_id != NULL )
  342. goto Exit;
  343. if ( FT_ALLOC( phy_font->font_id, len+1 ) )
  344. goto Exit;
  345. /* copy font ID name, and terminate it for safety */
  346. FT_MEM_COPY( phy_font->font_id, p, len );
  347. phy_font->font_id[len] = 0;
  348. Exit:
  349. return error;
  350. }
  351. /* load stem snap tables */
  352. FT_CALLBACK_DEF( FT_Error )
  353. pfr_extra_item_load_stem_snaps( FT_Byte* p,
  354. FT_Byte* limit,
  355. PFR_PhyFont phy_font )
  356. {
  357. FT_UInt count, num_vert, num_horz;
  358. FT_Int* snaps;
  359. FT_Error error = 0;
  360. FT_Memory memory = phy_font->memory;
  361. if ( phy_font->vertical.stem_snaps != NULL )
  362. goto Exit;
  363. PFR_CHECK( 1 );
  364. count = PFR_NEXT_BYTE( p );
  365. num_vert = count & 15;
  366. num_horz = count >> 4;
  367. count = num_vert + num_horz;
  368. PFR_CHECK( count * 2 );
  369. if ( FT_NEW_ARRAY( snaps, count ) )
  370. goto Exit;
  371. phy_font->vertical.stem_snaps = snaps;
  372. phy_font->horizontal.stem_snaps = snaps + num_vert;
  373. for ( ; count > 0; count--, snaps++ )
  374. *snaps = FT_NEXT_SHORT( p );
  375. Exit:
  376. return error;
  377. Too_Short:
  378. error = PFR_Err_Invalid_Table;
  379. FT_ERROR(( "pfr_exta_item_load_stem_snaps: invalid stem snaps table\n" ));
  380. goto Exit;
  381. }
  382. #if 0
  383. /* load kerning pair data */
  384. FT_CALLBACK_DEF( FT_Error )
  385. pfr_extra_item_load_kerning_pairs( FT_Byte* p,
  386. FT_Byte* limit,
  387. PFR_PhyFont phy_font )
  388. {
  389. FT_Int count;
  390. FT_UShort base_adj;
  391. FT_UInt flags;
  392. FT_UInt num_pairs;
  393. PFR_KernPair pairs;
  394. FT_Error error = 0;
  395. FT_Memory memory = phy_font->memory;
  396. /* allocate a new kerning item */
  397. /* XXX: there may be multiple extra items for kerning */
  398. if ( phy_font->kern_pairs != NULL )
  399. goto Exit;
  400. FT_TRACE2(( "pfr_extra_item_load_kerning_pairs()\n" ));
  401. PFR_CHECK( 4 );
  402. num_pairs = PFR_NEXT_BYTE( p );
  403. base_adj = PFR_NEXT_SHORT( p );
  404. flags = PFR_NEXT_BYTE( p );
  405. #ifndef PFR_CONFIG_NO_CHECKS
  406. count = 3;
  407. if ( flags & PFR_KERN_2BYTE_CHAR )
  408. count += 2;
  409. if ( flags & PFR_KERN_2BYTE_ADJ )
  410. count += 1;
  411. PFR_CHECK( num_pairs * count );
  412. #endif
  413. if ( FT_NEW_ARRAY( pairs, num_pairs ) )
  414. goto Exit;
  415. phy_font->num_kern_pairs = num_pairs;
  416. phy_font->kern_pairs = pairs;
  417. for (count = num_pairs ; count > 0; count--, pairs++ )
  418. {
  419. if ( flags & PFR_KERN_2BYTE_CHAR )
  420. {
  421. pairs->glyph1 = PFR_NEXT_USHORT( p );
  422. pairs->glyph2 = PFR_NEXT_USHORT( p );
  423. }
  424. else
  425. {
  426. pairs->glyph1 = PFR_NEXT_BYTE( p );
  427. pairs->glyph2 = PFR_NEXT_BYTE( p );
  428. }
  429. if ( flags & PFR_KERN_2BYTE_ADJ )
  430. pairs->kerning.x = base_adj + PFR_NEXT_SHORT( p );
  431. else
  432. pairs->kerning.x = base_adj + PFR_NEXT_INT8( p );
  433. pairs->kerning.y = 0;
  434. FT_TRACE2(( "kerning %d <-> %d : %ld\n",
  435. pairs->glyph1, pairs->glyph2, pairs->kerning.x ));
  436. }
  437. Exit:
  438. return error;
  439. Too_Short:
  440. error = PFR_Err_Invalid_Table;
  441. FT_ERROR(( "pfr_extra_item_load_kerning_pairs: "
  442. "invalid kerning pairs table\n" ));
  443. goto Exit;
  444. }
  445. #else /* 0 */
  446. /* load kerning pair data */
  447. FT_CALLBACK_DEF( FT_Error )
  448. pfr_extra_item_load_kerning_pairs( FT_Byte* p,
  449. FT_Byte* limit,
  450. PFR_PhyFont phy_font )
  451. {
  452. PFR_KernItem item;
  453. FT_Error error = 0;
  454. FT_Memory memory = phy_font->memory;
  455. FT_TRACE2(( "pfr_extra_item_load_kerning_pairs()\n" ));
  456. if ( FT_NEW( item ) )
  457. goto Exit;
  458. PFR_CHECK( 4 );
  459. item->pair_count = PFR_NEXT_BYTE( p );
  460. item->base_adj = PFR_NEXT_SHORT( p );
  461. item->flags = PFR_NEXT_BYTE( p );
  462. item->offset = phy_font->offset + ( p - phy_font->cursor );
  463. #ifndef PFR_CONFIG_NO_CHECKS
  464. item->pair_size = 3;
  465. if ( item->flags & PFR_KERN_2BYTE_CHAR )
  466. item->pair_size += 2;
  467. if ( item->flags & PFR_KERN_2BYTE_ADJ )
  468. item->pair_size += 1;
  469. PFR_CHECK( item->pair_count * item->pair_size );
  470. #endif
  471. /* load first and last pairs into the item to speed up */
  472. /* lookup later... */
  473. if ( item->pair_count > 0 )
  474. {
  475. FT_UInt char1, char2;
  476. FT_Byte* q;
  477. if ( item->flags & PFR_KERN_2BYTE_CHAR )
  478. {
  479. q = p;
  480. char1 = PFR_NEXT_USHORT( q );
  481. char2 = PFR_NEXT_USHORT( q );
  482. item->pair1 = PFR_KERN_INDEX( char1, char2 );
  483. q = p + item->pair_size * ( item->pair_count - 1 );
  484. char1 = PFR_NEXT_USHORT( q );
  485. char2 = PFR_NEXT_USHORT( q );
  486. item->pair2 = PFR_KERN_INDEX( char1, char2 );
  487. }
  488. else
  489. {
  490. q = p;
  491. char1 = PFR_NEXT_BYTE( q );
  492. char2 = PFR_NEXT_BYTE( q );
  493. item->pair1 = PFR_KERN_INDEX( char1, char2 );
  494. q = p + item->pair_size * ( item->pair_count - 1 );
  495. char1 = PFR_NEXT_BYTE( q );
  496. char2 = PFR_NEXT_BYTE( q );
  497. item->pair2 = PFR_KERN_INDEX( char1, char2 );
  498. }
  499. /* add new item to the current list */
  500. item->next = NULL;
  501. *phy_font->kern_items_tail = item;
  502. phy_font->kern_items_tail = &item->next;
  503. phy_font->num_kern_pairs += item->pair_count;
  504. }
  505. else
  506. {
  507. /* empty item! */
  508. FT_FREE( item );
  509. }
  510. Exit:
  511. return error;
  512. Too_Short:
  513. FT_FREE( item );
  514. error = PFR_Err_Invalid_Table;
  515. FT_ERROR(( "pfr_extra_item_load_kerning_pairs: "
  516. "invalid kerning pairs table\n" ));
  517. goto Exit;
  518. }
  519. #endif /* 0 */
  520. static const PFR_ExtraItemRec pfr_phy_font_extra_items[] =
  521. {
  522. { 1, (PFR_ExtraItem_ParseFunc) pfr_extra_item_load_bitmap_info },
  523. { 2, (PFR_ExtraItem_ParseFunc) pfr_extra_item_load_font_id },
  524. { 3, (PFR_ExtraItem_ParseFunc) pfr_extra_item_load_stem_snaps },
  525. { 4, (PFR_ExtraItem_ParseFunc) pfr_extra_item_load_kerning_pairs },
  526. { 0, NULL }
  527. };
  528. FT_LOCAL_DEF( void )
  529. pfr_phy_font_done( PFR_PhyFont phy_font,
  530. FT_Memory memory )
  531. {
  532. if ( phy_font->font_id )
  533. FT_FREE( phy_font->font_id );
  534. FT_FREE( phy_font->vertical.stem_snaps );
  535. phy_font->vertical.num_stem_snaps = 0;
  536. phy_font->horizontal.stem_snaps = NULL;
  537. phy_font->horizontal.num_stem_snaps = 0;
  538. FT_FREE( phy_font->strikes );
  539. phy_font->num_strikes = 0;
  540. phy_font->max_strikes = 0;
  541. FT_FREE( phy_font->chars );
  542. phy_font->num_chars = 0;
  543. phy_font->chars_offset = 0;
  544. FT_FREE( phy_font->blue_values );
  545. phy_font->num_blue_values = 0;
  546. {
  547. PFR_KernItem item, next;
  548. item = phy_font->kern_items;
  549. while ( item )
  550. {
  551. next = item->next;
  552. FT_FREE( item );
  553. item = next;
  554. }
  555. phy_font->kern_items = NULL;
  556. phy_font->kern_items_tail = NULL;
  557. }
  558. phy_font->num_kern_pairs = 0;
  559. }
  560. FT_LOCAL_DEF( FT_Error )
  561. pfr_phy_font_load( PFR_PhyFont phy_font,
  562. FT_Stream stream,
  563. FT_UInt32 offset,
  564. FT_UInt32 size )
  565. {
  566. FT_Error error;
  567. FT_Memory memory = stream->memory;
  568. FT_UInt flags, num_aux;
  569. FT_Byte* p;
  570. FT_Byte* limit;
  571. phy_font->memory = memory;
  572. phy_font->offset = offset;
  573. phy_font->kern_items = NULL;
  574. phy_font->kern_items_tail = &phy_font->kern_items;
  575. if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( size ) )
  576. goto Exit;
  577. phy_font->cursor = stream->cursor;
  578. p = stream->cursor;
  579. limit = p + size;
  580. PFR_CHECK( 15 );
  581. phy_font->font_ref_number = PFR_NEXT_USHORT( p );
  582. phy_font->outline_resolution = PFR_NEXT_USHORT( p );
  583. phy_font->metrics_resolution = PFR_NEXT_USHORT( p );
  584. phy_font->bbox.xMin = PFR_NEXT_SHORT( p );
  585. phy_font->bbox.yMin = PFR_NEXT_SHORT( p );
  586. phy_font->bbox.xMax = PFR_NEXT_SHORT( p );
  587. phy_font->bbox.yMax = PFR_NEXT_SHORT( p );
  588. phy_font->flags = flags = PFR_NEXT_BYTE( p );
  589. /* get the standard advance for non-proprotional fonts */
  590. if ( !(flags & PFR_PHY_PROPORTIONAL) )
  591. {
  592. PFR_CHECK( 2 );
  593. phy_font->standard_advance = PFR_NEXT_SHORT( p );
  594. }
  595. /* load the extra items when present */
  596. if ( flags & PFR_PHY_EXTRA_ITEMS )
  597. {
  598. error = pfr_extra_items_parse( &p, limit,
  599. pfr_phy_font_extra_items, phy_font );
  600. if ( error )
  601. goto Fail;
  602. }
  603. /* skip the aux bytes */
  604. PFR_CHECK( 3 );
  605. num_aux = PFR_NEXT_ULONG( p );
  606. PFR_CHECK( num_aux );
  607. p += num_aux;
  608. /* read the blue values */
  609. {
  610. FT_UInt n, count;
  611. PFR_CHECK( 1 );
  612. phy_font->num_blue_values = count = PFR_NEXT_BYTE( p );
  613. PFR_CHECK( count * 2 );
  614. if ( FT_NEW_ARRAY( phy_font->blue_values, count ) )
  615. goto Fail;
  616. for ( n = 0; n < count; n++ )
  617. phy_font->blue_values[n] = PFR_NEXT_SHORT( p );
  618. }
  619. PFR_CHECK( 8 );
  620. phy_font->blue_fuzz = PFR_NEXT_BYTE( p );
  621. phy_font->blue_scale = PFR_NEXT_BYTE( p );
  622. phy_font->vertical.standard = PFR_NEXT_USHORT( p );
  623. phy_font->horizontal.standard = PFR_NEXT_USHORT( p );
  624. /* read the character descriptors */
  625. {
  626. FT_UInt n, count, Size;
  627. phy_font->num_chars = count = PFR_NEXT_USHORT( p );
  628. phy_font->chars_offset = offset + ( p - stream->cursor );
  629. if ( FT_NEW_ARRAY( phy_font->chars, count ) )
  630. goto Fail;
  631. Size = 1 + 1 + 2;
  632. if ( flags & PFR_PHY_2BYTE_CHARCODE )
  633. Size += 1;
  634. if ( flags & PFR_PHY_PROPORTIONAL )
  635. Size += 2;
  636. if ( flags & PFR_PHY_ASCII_CODE )
  637. Size += 1;
  638. if ( flags & PFR_PHY_2BYTE_GPS_SIZE )
  639. Size += 1;
  640. if ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
  641. Size += 1;
  642. PFR_CHECK( count * Size );
  643. for ( n = 0; n < count; n++ )
  644. {
  645. PFR_Char cur = &phy_font->chars[n];
  646. cur->char_code = ( flags & PFR_PHY_2BYTE_CHARCODE )
  647. ? PFR_NEXT_USHORT( p )
  648. : PFR_NEXT_BYTE( p );
  649. cur->advance = ( flags & PFR_PHY_PROPORTIONAL )
  650. ? PFR_NEXT_SHORT( p )
  651. : (FT_Int) phy_font->standard_advance;
  652. #if 0
  653. cur->ascii = ( flags & PFR_PHY_ASCII_CODE )
  654. ? PFR_NEXT_BYTE( p )
  655. : 0;
  656. #else
  657. if ( flags & PFR_PHY_ASCII_CODE )
  658. p += 1;
  659. #endif
  660. cur->gps_size = ( flags & PFR_PHY_2BYTE_GPS_SIZE )
  661. ? PFR_NEXT_USHORT( p )
  662. : PFR_NEXT_BYTE( p );
  663. cur->gps_offset = ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
  664. ? PFR_NEXT_ULONG( p )
  665. : PFR_NEXT_USHORT( p );
  666. }
  667. }
  668. /* that's it !! */
  669. Fail:
  670. FT_FRAME_EXIT();
  671. /* save position of bitmap info */
  672. phy_font->bct_offset = FT_STREAM_POS();
  673. phy_font->cursor = NULL;
  674. Exit:
  675. return error;
  676. Too_Short:
  677. error = PFR_Err_Invalid_Table;
  678. FT_ERROR(( "pfr_phy_font_load: invalid physical font table\n" ));
  679. goto Fail;
  680. }
  681. /* END */