/modules/freetype2/src/pfr/pfrload.c

http://github.com/zpao/v8monkey · C · 941 lines · 626 code · 225 blank · 90 comment · 85 complexity · 151323b17571db222a0ffc9831b6aa1a MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* pfrload.c */
  4. /* */
  5. /* FreeType PFR loader (body). */
  6. /* */
  7. /* Copyright 2002, 2003, 2004, 2005, 2007, 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 "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 = PFR_Err_Ok;
  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 = PFR_Err_Ok;
  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 = FT_PAD_CEIL( phy_font->num_strikes + count, 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:"
  330. " invalid bitmap info table\n" ));
  331. goto Exit;
  332. }
  333. /* Load font ID. This is a so-called "unique" name that is rather
  334. * long and descriptive (like "Tiresias ScreenFont v7.51").
  335. *
  336. * Note that a PFR font's family name is contained in an *undocumented*
  337. * string of the "auxiliary data" portion of a physical font record. This
  338. * may also contain the "real" style name!
  339. *
  340. * If no family name is present, the font ID is used instead for the
  341. * family.
  342. */
  343. FT_CALLBACK_DEF( FT_Error )
  344. pfr_extra_item_load_font_id( FT_Byte* p,
  345. FT_Byte* limit,
  346. PFR_PhyFont phy_font )
  347. {
  348. FT_Error error = PFR_Err_Ok;
  349. FT_Memory memory = phy_font->memory;
  350. FT_PtrDist len = limit - p;
  351. if ( phy_font->font_id != NULL )
  352. goto Exit;
  353. if ( FT_ALLOC( phy_font->font_id, len + 1 ) )
  354. goto Exit;
  355. /* copy font ID name, and terminate it for safety */
  356. FT_MEM_COPY( phy_font->font_id, p, len );
  357. phy_font->font_id[len] = 0;
  358. Exit:
  359. return error;
  360. }
  361. /* load stem snap tables */
  362. FT_CALLBACK_DEF( FT_Error )
  363. pfr_extra_item_load_stem_snaps( FT_Byte* p,
  364. FT_Byte* limit,
  365. PFR_PhyFont phy_font )
  366. {
  367. FT_UInt count, num_vert, num_horz;
  368. FT_Int* snaps;
  369. FT_Error error = PFR_Err_Ok;
  370. FT_Memory memory = phy_font->memory;
  371. if ( phy_font->vertical.stem_snaps != NULL )
  372. goto Exit;
  373. PFR_CHECK( 1 );
  374. count = PFR_NEXT_BYTE( p );
  375. num_vert = count & 15;
  376. num_horz = count >> 4;
  377. count = num_vert + num_horz;
  378. PFR_CHECK( count * 2 );
  379. if ( FT_NEW_ARRAY( snaps, count ) )
  380. goto Exit;
  381. phy_font->vertical.stem_snaps = snaps;
  382. phy_font->horizontal.stem_snaps = snaps + num_vert;
  383. for ( ; count > 0; count--, snaps++ )
  384. *snaps = FT_NEXT_SHORT( p );
  385. Exit:
  386. return error;
  387. Too_Short:
  388. error = PFR_Err_Invalid_Table;
  389. FT_ERROR(( "pfr_exta_item_load_stem_snaps:"
  390. " invalid stem snaps table\n" ));
  391. goto Exit;
  392. }
  393. /* load kerning pair data */
  394. FT_CALLBACK_DEF( FT_Error )
  395. pfr_extra_item_load_kerning_pairs( FT_Byte* p,
  396. FT_Byte* limit,
  397. PFR_PhyFont phy_font )
  398. {
  399. PFR_KernItem item = NULL;
  400. FT_Error error = PFR_Err_Ok;
  401. FT_Memory memory = phy_font->memory;
  402. FT_TRACE2(( "pfr_extra_item_load_kerning_pairs()\n" ));
  403. if ( FT_NEW( item ) )
  404. goto Exit;
  405. PFR_CHECK( 4 );
  406. item->pair_count = PFR_NEXT_BYTE( p );
  407. item->base_adj = PFR_NEXT_SHORT( p );
  408. item->flags = PFR_NEXT_BYTE( p );
  409. item->offset = phy_font->offset + ( p - phy_font->cursor );
  410. #ifndef PFR_CONFIG_NO_CHECKS
  411. item->pair_size = 3;
  412. if ( item->flags & PFR_KERN_2BYTE_CHAR )
  413. item->pair_size += 2;
  414. if ( item->flags & PFR_KERN_2BYTE_ADJ )
  415. item->pair_size += 1;
  416. PFR_CHECK( item->pair_count * item->pair_size );
  417. #endif
  418. /* load first and last pairs into the item to speed up */
  419. /* lookup later... */
  420. if ( item->pair_count > 0 )
  421. {
  422. FT_UInt char1, char2;
  423. FT_Byte* q;
  424. if ( item->flags & PFR_KERN_2BYTE_CHAR )
  425. {
  426. q = p;
  427. char1 = PFR_NEXT_USHORT( q );
  428. char2 = PFR_NEXT_USHORT( q );
  429. item->pair1 = PFR_KERN_INDEX( char1, char2 );
  430. q = p + item->pair_size * ( item->pair_count - 1 );
  431. char1 = PFR_NEXT_USHORT( q );
  432. char2 = PFR_NEXT_USHORT( q );
  433. item->pair2 = PFR_KERN_INDEX( char1, char2 );
  434. }
  435. else
  436. {
  437. q = p;
  438. char1 = PFR_NEXT_BYTE( q );
  439. char2 = PFR_NEXT_BYTE( q );
  440. item->pair1 = PFR_KERN_INDEX( char1, char2 );
  441. q = p + item->pair_size * ( item->pair_count - 1 );
  442. char1 = PFR_NEXT_BYTE( q );
  443. char2 = PFR_NEXT_BYTE( q );
  444. item->pair2 = PFR_KERN_INDEX( char1, char2 );
  445. }
  446. /* add new item to the current list */
  447. item->next = NULL;
  448. *phy_font->kern_items_tail = item;
  449. phy_font->kern_items_tail = &item->next;
  450. phy_font->num_kern_pairs += item->pair_count;
  451. }
  452. else
  453. {
  454. /* empty item! */
  455. FT_FREE( item );
  456. }
  457. Exit:
  458. return error;
  459. Too_Short:
  460. FT_FREE( item );
  461. error = PFR_Err_Invalid_Table;
  462. FT_ERROR(( "pfr_extra_item_load_kerning_pairs:"
  463. " invalid kerning pairs table\n" ));
  464. goto Exit;
  465. }
  466. static const PFR_ExtraItemRec pfr_phy_font_extra_items[] =
  467. {
  468. { 1, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_bitmap_info },
  469. { 2, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_font_id },
  470. { 3, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_stem_snaps },
  471. { 4, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_kerning_pairs },
  472. { 0, NULL }
  473. };
  474. /* Loads a name from the auxiliary data. Since this extracts undocumented
  475. * strings from the font file, we need to be careful here.
  476. */
  477. static FT_Error
  478. pfr_aux_name_load( FT_Byte* p,
  479. FT_UInt len,
  480. FT_Memory memory,
  481. FT_String* *astring )
  482. {
  483. FT_Error error = PFR_Err_Ok;
  484. FT_String* result = NULL;
  485. FT_UInt n, ok;
  486. if ( len > 0 && p[len - 1] == 0 )
  487. len--;
  488. /* check that each character is ASCII for making sure not to
  489. load garbage
  490. */
  491. ok = ( len > 0 );
  492. for ( n = 0; n < len; n++ )
  493. if ( p[n] < 32 || p[n] > 127 )
  494. {
  495. ok = 0;
  496. break;
  497. }
  498. if ( ok )
  499. {
  500. if ( FT_ALLOC( result, len + 1 ) )
  501. goto Exit;
  502. FT_MEM_COPY( result, p, len );
  503. result[len] = 0;
  504. }
  505. Exit:
  506. *astring = result;
  507. return error;
  508. }
  509. FT_LOCAL_DEF( void )
  510. pfr_phy_font_done( PFR_PhyFont phy_font,
  511. FT_Memory memory )
  512. {
  513. FT_FREE( phy_font->font_id );
  514. FT_FREE( phy_font->family_name );
  515. FT_FREE( phy_font->style_name );
  516. FT_FREE( phy_font->vertical.stem_snaps );
  517. phy_font->vertical.num_stem_snaps = 0;
  518. phy_font->horizontal.stem_snaps = NULL;
  519. phy_font->horizontal.num_stem_snaps = 0;
  520. FT_FREE( phy_font->strikes );
  521. phy_font->num_strikes = 0;
  522. phy_font->max_strikes = 0;
  523. FT_FREE( phy_font->chars );
  524. phy_font->num_chars = 0;
  525. phy_font->chars_offset = 0;
  526. FT_FREE( phy_font->blue_values );
  527. phy_font->num_blue_values = 0;
  528. {
  529. PFR_KernItem item, next;
  530. item = phy_font->kern_items;
  531. while ( item )
  532. {
  533. next = item->next;
  534. FT_FREE( item );
  535. item = next;
  536. }
  537. phy_font->kern_items = NULL;
  538. phy_font->kern_items_tail = NULL;
  539. }
  540. phy_font->num_kern_pairs = 0;
  541. }
  542. FT_LOCAL_DEF( FT_Error )
  543. pfr_phy_font_load( PFR_PhyFont phy_font,
  544. FT_Stream stream,
  545. FT_UInt32 offset,
  546. FT_UInt32 size )
  547. {
  548. FT_Error error;
  549. FT_Memory memory = stream->memory;
  550. FT_UInt flags;
  551. FT_ULong num_aux;
  552. FT_Byte* p;
  553. FT_Byte* limit;
  554. phy_font->memory = memory;
  555. phy_font->offset = offset;
  556. phy_font->kern_items = NULL;
  557. phy_font->kern_items_tail = &phy_font->kern_items;
  558. if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( size ) )
  559. goto Exit;
  560. phy_font->cursor = stream->cursor;
  561. p = stream->cursor;
  562. limit = p + size;
  563. PFR_CHECK( 15 );
  564. phy_font->font_ref_number = PFR_NEXT_USHORT( p );
  565. phy_font->outline_resolution = PFR_NEXT_USHORT( p );
  566. phy_font->metrics_resolution = PFR_NEXT_USHORT( p );
  567. phy_font->bbox.xMin = PFR_NEXT_SHORT( p );
  568. phy_font->bbox.yMin = PFR_NEXT_SHORT( p );
  569. phy_font->bbox.xMax = PFR_NEXT_SHORT( p );
  570. phy_font->bbox.yMax = PFR_NEXT_SHORT( p );
  571. phy_font->flags = flags = PFR_NEXT_BYTE( p );
  572. /* get the standard advance for non-proportional fonts */
  573. if ( !(flags & PFR_PHY_PROPORTIONAL) )
  574. {
  575. PFR_CHECK( 2 );
  576. phy_font->standard_advance = PFR_NEXT_SHORT( p );
  577. }
  578. /* load the extra items when present */
  579. if ( flags & PFR_PHY_EXTRA_ITEMS )
  580. {
  581. error = pfr_extra_items_parse( &p, limit,
  582. pfr_phy_font_extra_items, phy_font );
  583. if ( error )
  584. goto Fail;
  585. }
  586. /* In certain fonts, the auxiliary bytes contain interesting */
  587. /* information. These are not in the specification but can be */
  588. /* guessed by looking at the content of a few PFR0 fonts. */
  589. PFR_CHECK( 3 );
  590. num_aux = PFR_NEXT_ULONG( p );
  591. if ( num_aux > 0 )
  592. {
  593. FT_Byte* q = p;
  594. FT_Byte* q2;
  595. PFR_CHECK( num_aux );
  596. p += num_aux;
  597. while ( num_aux > 0 )
  598. {
  599. FT_UInt length, type;
  600. if ( q + 4 > p )
  601. break;
  602. length = PFR_NEXT_USHORT( q );
  603. if ( length < 4 || length > num_aux )
  604. break;
  605. q2 = q + length - 2;
  606. type = PFR_NEXT_USHORT( q );
  607. switch ( type )
  608. {
  609. case 1:
  610. /* this seems to correspond to the font's family name,
  611. * padded to 16-bits with one zero when necessary
  612. */
  613. error = pfr_aux_name_load( q, length - 4U, memory,
  614. &phy_font->family_name );
  615. if ( error )
  616. goto Exit;
  617. break;
  618. case 2:
  619. if ( q + 32 > q2 )
  620. break;
  621. q += 10;
  622. phy_font->ascent = PFR_NEXT_SHORT( q );
  623. phy_font->descent = PFR_NEXT_SHORT( q );
  624. phy_font->leading = PFR_NEXT_SHORT( q );
  625. q += 16;
  626. break;
  627. case 3:
  628. /* this seems to correspond to the font's style name,
  629. * padded to 16-bits with one zero when necessary
  630. */
  631. error = pfr_aux_name_load( q, length - 4U, memory,
  632. &phy_font->style_name );
  633. if ( error )
  634. goto Exit;
  635. break;
  636. default:
  637. ;
  638. }
  639. q = q2;
  640. num_aux -= length;
  641. }
  642. }
  643. /* read the blue values */
  644. {
  645. FT_UInt n, count;
  646. PFR_CHECK( 1 );
  647. phy_font->num_blue_values = count = PFR_NEXT_BYTE( p );
  648. PFR_CHECK( count * 2 );
  649. if ( FT_NEW_ARRAY( phy_font->blue_values, count ) )
  650. goto Fail;
  651. for ( n = 0; n < count; n++ )
  652. phy_font->blue_values[n] = PFR_NEXT_SHORT( p );
  653. }
  654. PFR_CHECK( 8 );
  655. phy_font->blue_fuzz = PFR_NEXT_BYTE( p );
  656. phy_font->blue_scale = PFR_NEXT_BYTE( p );
  657. phy_font->vertical.standard = PFR_NEXT_USHORT( p );
  658. phy_font->horizontal.standard = PFR_NEXT_USHORT( p );
  659. /* read the character descriptors */
  660. {
  661. FT_UInt n, count, Size;
  662. phy_font->num_chars = count = PFR_NEXT_USHORT( p );
  663. phy_font->chars_offset = offset + ( p - stream->cursor );
  664. if ( FT_NEW_ARRAY( phy_font->chars, count ) )
  665. goto Fail;
  666. Size = 1 + 1 + 2;
  667. if ( flags & PFR_PHY_2BYTE_CHARCODE )
  668. Size += 1;
  669. if ( flags & PFR_PHY_PROPORTIONAL )
  670. Size += 2;
  671. if ( flags & PFR_PHY_ASCII_CODE )
  672. Size += 1;
  673. if ( flags & PFR_PHY_2BYTE_GPS_SIZE )
  674. Size += 1;
  675. if ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
  676. Size += 1;
  677. PFR_CHECK( count * Size );
  678. for ( n = 0; n < count; n++ )
  679. {
  680. PFR_Char cur = &phy_font->chars[n];
  681. cur->char_code = ( flags & PFR_PHY_2BYTE_CHARCODE )
  682. ? PFR_NEXT_USHORT( p )
  683. : PFR_NEXT_BYTE( p );
  684. cur->advance = ( flags & PFR_PHY_PROPORTIONAL )
  685. ? PFR_NEXT_SHORT( p )
  686. : (FT_Int) phy_font->standard_advance;
  687. #if 0
  688. cur->ascii = ( flags & PFR_PHY_ASCII_CODE )
  689. ? PFR_NEXT_BYTE( p )
  690. : 0;
  691. #else
  692. if ( flags & PFR_PHY_ASCII_CODE )
  693. p += 1;
  694. #endif
  695. cur->gps_size = ( flags & PFR_PHY_2BYTE_GPS_SIZE )
  696. ? PFR_NEXT_USHORT( p )
  697. : PFR_NEXT_BYTE( p );
  698. cur->gps_offset = ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
  699. ? PFR_NEXT_ULONG( p )
  700. : PFR_NEXT_USHORT( p );
  701. }
  702. }
  703. /* that's it! */
  704. Fail:
  705. FT_FRAME_EXIT();
  706. /* save position of bitmap info */
  707. phy_font->bct_offset = FT_STREAM_POS();
  708. phy_font->cursor = NULL;
  709. Exit:
  710. return error;
  711. Too_Short:
  712. error = PFR_Err_Invalid_Table;
  713. FT_ERROR(( "pfr_phy_font_load: invalid physical font table\n" ));
  714. goto Fail;
  715. }
  716. /* END */