/src/compiler/android-ndk/jni/freetype/src/cff/cffload.c

http://ftk.googlecode.com/ · C · 1638 lines · 1148 code · 323 blank · 167 comment · 189 complexity · ee487f44a6a570579a23b67776d24860 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* cffload.c */
  4. /* */
  5. /* OpenType and CFF data/program tables loader (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include FT_SERVICE_POSTSCRIPT_CMAPS_H
  22. #include FT_TRUETYPE_TAGS_H
  23. #include FT_TYPE1_TABLES_H
  24. #include "cffload.h"
  25. #include "cffparse.h"
  26. #include "cfferrs.h"
  27. #if 1
  28. static const FT_UShort cff_isoadobe_charset[229] =
  29. {
  30. 0, 1, 2, 3, 4, 5, 6, 7,
  31. 8, 9, 10, 11, 12, 13, 14, 15,
  32. 16, 17, 18, 19, 20, 21, 22, 23,
  33. 24, 25, 26, 27, 28, 29, 30, 31,
  34. 32, 33, 34, 35, 36, 37, 38, 39,
  35. 40, 41, 42, 43, 44, 45, 46, 47,
  36. 48, 49, 50, 51, 52, 53, 54, 55,
  37. 56, 57, 58, 59, 60, 61, 62, 63,
  38. 64, 65, 66, 67, 68, 69, 70, 71,
  39. 72, 73, 74, 75, 76, 77, 78, 79,
  40. 80, 81, 82, 83, 84, 85, 86, 87,
  41. 88, 89, 90, 91, 92, 93, 94, 95,
  42. 96, 97, 98, 99, 100, 101, 102, 103,
  43. 104, 105, 106, 107, 108, 109, 110, 111,
  44. 112, 113, 114, 115, 116, 117, 118, 119,
  45. 120, 121, 122, 123, 124, 125, 126, 127,
  46. 128, 129, 130, 131, 132, 133, 134, 135,
  47. 136, 137, 138, 139, 140, 141, 142, 143,
  48. 144, 145, 146, 147, 148, 149, 150, 151,
  49. 152, 153, 154, 155, 156, 157, 158, 159,
  50. 160, 161, 162, 163, 164, 165, 166, 167,
  51. 168, 169, 170, 171, 172, 173, 174, 175,
  52. 176, 177, 178, 179, 180, 181, 182, 183,
  53. 184, 185, 186, 187, 188, 189, 190, 191,
  54. 192, 193, 194, 195, 196, 197, 198, 199,
  55. 200, 201, 202, 203, 204, 205, 206, 207,
  56. 208, 209, 210, 211, 212, 213, 214, 215,
  57. 216, 217, 218, 219, 220, 221, 222, 223,
  58. 224, 225, 226, 227, 228
  59. };
  60. static const FT_UShort cff_expert_charset[166] =
  61. {
  62. 0, 1, 229, 230, 231, 232, 233, 234,
  63. 235, 236, 237, 238, 13, 14, 15, 99,
  64. 239, 240, 241, 242, 243, 244, 245, 246,
  65. 247, 248, 27, 28, 249, 250, 251, 252,
  66. 253, 254, 255, 256, 257, 258, 259, 260,
  67. 261, 262, 263, 264, 265, 266, 109, 110,
  68. 267, 268, 269, 270, 271, 272, 273, 274,
  69. 275, 276, 277, 278, 279, 280, 281, 282,
  70. 283, 284, 285, 286, 287, 288, 289, 290,
  71. 291, 292, 293, 294, 295, 296, 297, 298,
  72. 299, 300, 301, 302, 303, 304, 305, 306,
  73. 307, 308, 309, 310, 311, 312, 313, 314,
  74. 315, 316, 317, 318, 158, 155, 163, 319,
  75. 320, 321, 322, 323, 324, 325, 326, 150,
  76. 164, 169, 327, 328, 329, 330, 331, 332,
  77. 333, 334, 335, 336, 337, 338, 339, 340,
  78. 341, 342, 343, 344, 345, 346, 347, 348,
  79. 349, 350, 351, 352, 353, 354, 355, 356,
  80. 357, 358, 359, 360, 361, 362, 363, 364,
  81. 365, 366, 367, 368, 369, 370, 371, 372,
  82. 373, 374, 375, 376, 377, 378
  83. };
  84. static const FT_UShort cff_expertsubset_charset[87] =
  85. {
  86. 0, 1, 231, 232, 235, 236, 237, 238,
  87. 13, 14, 15, 99, 239, 240, 241, 242,
  88. 243, 244, 245, 246, 247, 248, 27, 28,
  89. 249, 250, 251, 253, 254, 255, 256, 257,
  90. 258, 259, 260, 261, 262, 263, 264, 265,
  91. 266, 109, 110, 267, 268, 269, 270, 272,
  92. 300, 301, 302, 305, 314, 315, 158, 155,
  93. 163, 320, 321, 322, 323, 324, 325, 326,
  94. 150, 164, 169, 327, 328, 329, 330, 331,
  95. 332, 333, 334, 335, 336, 337, 338, 339,
  96. 340, 341, 342, 343, 344, 345, 346
  97. };
  98. static const FT_UShort cff_standard_encoding[256] =
  99. {
  100. 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0,
  104. 1, 2, 3, 4, 5, 6, 7, 8,
  105. 9, 10, 11, 12, 13, 14, 15, 16,
  106. 17, 18, 19, 20, 21, 22, 23, 24,
  107. 25, 26, 27, 28, 29, 30, 31, 32,
  108. 33, 34, 35, 36, 37, 38, 39, 40,
  109. 41, 42, 43, 44, 45, 46, 47, 48,
  110. 49, 50, 51, 52, 53, 54, 55, 56,
  111. 57, 58, 59, 60, 61, 62, 63, 64,
  112. 65, 66, 67, 68, 69, 70, 71, 72,
  113. 73, 74, 75, 76, 77, 78, 79, 80,
  114. 81, 82, 83, 84, 85, 86, 87, 88,
  115. 89, 90, 91, 92, 93, 94, 95, 0,
  116. 0, 0, 0, 0, 0, 0, 0, 0,
  117. 0, 0, 0, 0, 0, 0, 0, 0,
  118. 0, 0, 0, 0, 0, 0, 0, 0,
  119. 0, 0, 0, 0, 0, 0, 0, 0,
  120. 0, 96, 97, 98, 99, 100, 101, 102,
  121. 103, 104, 105, 106, 107, 108, 109, 110,
  122. 0, 111, 112, 113, 114, 0, 115, 116,
  123. 117, 118, 119, 120, 121, 122, 0, 123,
  124. 0, 124, 125, 126, 127, 128, 129, 130,
  125. 131, 0, 132, 133, 0, 134, 135, 136,
  126. 137, 0, 0, 0, 0, 0, 0, 0,
  127. 0, 0, 0, 0, 0, 0, 0, 0,
  128. 0, 138, 0, 139, 0, 0, 0, 0,
  129. 140, 141, 142, 143, 0, 0, 0, 0,
  130. 0, 144, 0, 0, 0, 145, 0, 0,
  131. 146, 147, 148, 149, 0, 0, 0, 0
  132. };
  133. static const FT_UShort cff_expert_encoding[256] =
  134. {
  135. 0, 0, 0, 0, 0, 0, 0, 0,
  136. 0, 0, 0, 0, 0, 0, 0, 0,
  137. 0, 0, 0, 0, 0, 0, 0, 0,
  138. 0, 0, 0, 0, 0, 0, 0, 0,
  139. 1, 229, 230, 0, 231, 232, 233, 234,
  140. 235, 236, 237, 238, 13, 14, 15, 99,
  141. 239, 240, 241, 242, 243, 244, 245, 246,
  142. 247, 248, 27, 28, 249, 250, 251, 252,
  143. 0, 253, 254, 255, 256, 257, 0, 0,
  144. 0, 258, 0, 0, 259, 260, 261, 262,
  145. 0, 0, 263, 264, 265, 0, 266, 109,
  146. 110, 267, 268, 269, 0, 270, 271, 272,
  147. 273, 274, 275, 276, 277, 278, 279, 280,
  148. 281, 282, 283, 284, 285, 286, 287, 288,
  149. 289, 290, 291, 292, 293, 294, 295, 296,
  150. 297, 298, 299, 300, 301, 302, 303, 0,
  151. 0, 0, 0, 0, 0, 0, 0, 0,
  152. 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0,
  155. 0, 304, 305, 306, 0, 0, 307, 308,
  156. 309, 310, 311, 0, 312, 0, 0, 312,
  157. 0, 0, 314, 315, 0, 0, 316, 317,
  158. 318, 0, 0, 0, 158, 155, 163, 319,
  159. 320, 321, 322, 323, 324, 325, 0, 0,
  160. 326, 150, 164, 169, 327, 328, 329, 330,
  161. 331, 332, 333, 334, 335, 336, 337, 338,
  162. 339, 340, 341, 342, 343, 344, 345, 346,
  163. 347, 348, 349, 350, 351, 352, 353, 354,
  164. 355, 356, 357, 358, 359, 360, 361, 362,
  165. 363, 364, 365, 366, 367, 368, 369, 370,
  166. 371, 372, 373, 374, 375, 376, 377, 378
  167. };
  168. #endif /* 1 */
  169. FT_LOCAL_DEF( FT_UShort )
  170. cff_get_standard_encoding( FT_UInt charcode )
  171. {
  172. return (FT_UShort)( charcode < 256 ? cff_standard_encoding[charcode]
  173. : 0 );
  174. }
  175. /*************************************************************************/
  176. /* */
  177. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  178. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  179. /* messages during execution. */
  180. /* */
  181. #undef FT_COMPONENT
  182. #define FT_COMPONENT trace_cffload
  183. /* read an offset from the index's stream current position */
  184. static FT_ULong
  185. cff_index_read_offset( CFF_Index idx,
  186. FT_Error *errorp )
  187. {
  188. FT_Error error;
  189. FT_Stream stream = idx->stream;
  190. FT_Byte tmp[4];
  191. FT_ULong result = 0;
  192. if ( !FT_STREAM_READ( tmp, idx->off_size ) )
  193. {
  194. FT_Int nn;
  195. for ( nn = 0; nn < idx->off_size; nn++ )
  196. result = ( result << 8 ) | tmp[nn];
  197. }
  198. *errorp = error;
  199. return result;
  200. }
  201. static FT_Error
  202. cff_index_init( CFF_Index idx,
  203. FT_Stream stream,
  204. FT_Bool load )
  205. {
  206. FT_Error error;
  207. FT_Memory memory = stream->memory;
  208. FT_UShort count;
  209. FT_MEM_ZERO( idx, sizeof ( *idx ) );
  210. idx->stream = stream;
  211. idx->start = FT_STREAM_POS();
  212. if ( !FT_READ_USHORT( count ) &&
  213. count > 0 )
  214. {
  215. FT_Byte offsize;
  216. FT_ULong size;
  217. /* there is at least one element; read the offset size, */
  218. /* then access the offset table to compute the index's total size */
  219. if ( FT_READ_BYTE( offsize ) )
  220. goto Exit;
  221. if ( offsize < 1 || offsize > 4 )
  222. {
  223. error = FT_Err_Invalid_Table;
  224. goto Exit;
  225. }
  226. idx->count = count;
  227. idx->off_size = offsize;
  228. size = (FT_ULong)( count + 1 ) * offsize;
  229. idx->data_offset = idx->start + 3 + size;
  230. if ( FT_STREAM_SKIP( size - offsize ) )
  231. goto Exit;
  232. size = cff_index_read_offset( idx, &error );
  233. if ( error )
  234. goto Exit;
  235. if ( size == 0 )
  236. {
  237. error = CFF_Err_Invalid_Table;
  238. goto Exit;
  239. }
  240. idx->data_size = --size;
  241. if ( load )
  242. {
  243. /* load the data */
  244. if ( FT_FRAME_EXTRACT( size, idx->bytes ) )
  245. goto Exit;
  246. }
  247. else
  248. {
  249. /* skip the data */
  250. if ( FT_STREAM_SKIP( size ) )
  251. goto Exit;
  252. }
  253. }
  254. Exit:
  255. if ( error )
  256. FT_FREE( idx->offsets );
  257. return error;
  258. }
  259. static void
  260. cff_index_done( CFF_Index idx )
  261. {
  262. if ( idx->stream )
  263. {
  264. FT_Stream stream = idx->stream;
  265. FT_Memory memory = stream->memory;
  266. if ( idx->bytes )
  267. FT_FRAME_RELEASE( idx->bytes );
  268. FT_FREE( idx->offsets );
  269. FT_MEM_ZERO( idx, sizeof ( *idx ) );
  270. }
  271. }
  272. static FT_Error
  273. cff_index_load_offsets( CFF_Index idx )
  274. {
  275. FT_Error error = CFF_Err_Ok;
  276. FT_Stream stream = idx->stream;
  277. FT_Memory memory = stream->memory;
  278. if ( idx->count > 0 && idx->offsets == NULL )
  279. {
  280. FT_Byte offsize = idx->off_size;
  281. FT_ULong data_size;
  282. FT_Byte* p;
  283. FT_Byte* p_end;
  284. FT_ULong* poff;
  285. data_size = (FT_ULong)( idx->count + 1 ) * offsize;
  286. if ( FT_NEW_ARRAY( idx->offsets, idx->count + 1 ) ||
  287. FT_STREAM_SEEK( idx->start + 3 ) ||
  288. FT_FRAME_ENTER( data_size ) )
  289. goto Exit;
  290. poff = idx->offsets;
  291. p = (FT_Byte*)stream->cursor;
  292. p_end = p + data_size;
  293. switch ( offsize )
  294. {
  295. case 1:
  296. for ( ; p < p_end; p++, poff++ )
  297. poff[0] = p[0];
  298. break;
  299. case 2:
  300. for ( ; p < p_end; p += 2, poff++ )
  301. poff[0] = FT_PEEK_USHORT( p );
  302. break;
  303. case 3:
  304. for ( ; p < p_end; p += 3, poff++ )
  305. poff[0] = FT_PEEK_OFF3( p );
  306. break;
  307. default:
  308. for ( ; p < p_end; p += 4, poff++ )
  309. poff[0] = FT_PEEK_ULONG( p );
  310. }
  311. FT_FRAME_EXIT();
  312. }
  313. Exit:
  314. if ( error )
  315. FT_FREE( idx->offsets );
  316. return error;
  317. }
  318. /* allocate a table containing pointers to an index's elements */
  319. static FT_Error
  320. cff_index_get_pointers( CFF_Index idx,
  321. FT_Byte*** table )
  322. {
  323. FT_Error error = CFF_Err_Ok;
  324. FT_Memory memory = idx->stream->memory;
  325. FT_ULong n, offset, old_offset;
  326. FT_Byte** t;
  327. *table = 0;
  328. if ( idx->offsets == NULL )
  329. {
  330. error = cff_index_load_offsets( idx );
  331. if ( error )
  332. goto Exit;
  333. }
  334. if ( idx->count > 0 && !FT_NEW_ARRAY( t, idx->count + 1 ) )
  335. {
  336. old_offset = 1;
  337. for ( n = 0; n <= idx->count; n++ )
  338. {
  339. /* at this point, `idx->offsets' can't be NULL */
  340. offset = idx->offsets[n];
  341. if ( !offset )
  342. offset = old_offset;
  343. /* two sanity checks for invalid offset tables */
  344. else if ( offset < old_offset )
  345. offset = old_offset;
  346. else if ( offset - 1 >= idx->data_size && n < idx->count )
  347. offset = old_offset;
  348. t[n] = idx->bytes + offset - 1;
  349. old_offset = offset;
  350. }
  351. *table = t;
  352. }
  353. Exit:
  354. return error;
  355. }
  356. FT_LOCAL_DEF( FT_Error )
  357. cff_index_access_element( CFF_Index idx,
  358. FT_UInt element,
  359. FT_Byte** pbytes,
  360. FT_ULong* pbyte_len )
  361. {
  362. FT_Error error = CFF_Err_Ok;
  363. if ( idx && idx->count > element )
  364. {
  365. /* compute start and end offsets */
  366. FT_Stream stream = idx->stream;
  367. FT_ULong off1, off2 = 0;
  368. /* load offsets from file or the offset table */
  369. if ( !idx->offsets )
  370. {
  371. FT_ULong pos = element * idx->off_size;
  372. if ( FT_STREAM_SEEK( idx->start + 3 + pos ) )
  373. goto Exit;
  374. off1 = cff_index_read_offset( idx, &error );
  375. if ( error )
  376. goto Exit;
  377. if ( off1 != 0 )
  378. {
  379. do
  380. {
  381. element++;
  382. off2 = cff_index_read_offset( idx, &error );
  383. }
  384. while ( off2 == 0 && element < idx->count );
  385. }
  386. }
  387. else /* use offsets table */
  388. {
  389. off1 = idx->offsets[element];
  390. if ( off1 )
  391. {
  392. do
  393. {
  394. element++;
  395. off2 = idx->offsets[element];
  396. } while ( off2 == 0 && element < idx->count );
  397. }
  398. }
  399. /* access element */
  400. if ( off1 && off2 > off1 )
  401. {
  402. *pbyte_len = off2 - off1;
  403. if ( idx->bytes )
  404. {
  405. /* this index was completely loaded in memory, that's easy */
  406. *pbytes = idx->bytes + off1 - 1;
  407. }
  408. else
  409. {
  410. /* this index is still on disk/file, access it through a frame */
  411. if ( FT_STREAM_SEEK( idx->data_offset + off1 - 1 ) ||
  412. FT_FRAME_EXTRACT( off2 - off1, *pbytes ) )
  413. goto Exit;
  414. }
  415. }
  416. else
  417. {
  418. /* empty index element */
  419. *pbytes = 0;
  420. *pbyte_len = 0;
  421. }
  422. }
  423. else
  424. error = CFF_Err_Invalid_Argument;
  425. Exit:
  426. return error;
  427. }
  428. FT_LOCAL_DEF( void )
  429. cff_index_forget_element( CFF_Index idx,
  430. FT_Byte** pbytes )
  431. {
  432. if ( idx->bytes == 0 )
  433. {
  434. FT_Stream stream = idx->stream;
  435. FT_FRAME_RELEASE( *pbytes );
  436. }
  437. }
  438. FT_LOCAL_DEF( FT_String* )
  439. cff_index_get_name( CFF_Index idx,
  440. FT_UInt element )
  441. {
  442. FT_Memory memory = idx->stream->memory;
  443. FT_Byte* bytes;
  444. FT_ULong byte_len;
  445. FT_Error error;
  446. FT_String* name = 0;
  447. error = cff_index_access_element( idx, element, &bytes, &byte_len );
  448. if ( error )
  449. goto Exit;
  450. if ( !FT_ALLOC( name, byte_len + 1 ) )
  451. {
  452. FT_MEM_COPY( name, bytes, byte_len );
  453. name[byte_len] = 0;
  454. }
  455. cff_index_forget_element( idx, &bytes );
  456. Exit:
  457. return name;
  458. }
  459. FT_LOCAL_DEF( FT_String* )
  460. cff_index_get_sid_string( CFF_Index idx,
  461. FT_UInt sid,
  462. FT_Service_PsCMaps psnames )
  463. {
  464. /* value 0xFFFFU indicates a missing dictionary entry */
  465. if ( sid == 0xFFFFU )
  466. return 0;
  467. /* if it is not a standard string, return it */
  468. if ( sid > 390 )
  469. return cff_index_get_name( idx, sid - 391 );
  470. /* CID-keyed CFF fonts don't have glyph names */
  471. if ( !psnames )
  472. return 0;
  473. /* that's a standard string, fetch a copy from the PSName module */
  474. {
  475. FT_String* name = 0;
  476. const char* adobe_name = psnames->adobe_std_strings( sid );
  477. if ( adobe_name )
  478. {
  479. FT_Memory memory = idx->stream->memory;
  480. FT_Error error;
  481. (void)FT_STRDUP( name, adobe_name );
  482. FT_UNUSED( error );
  483. }
  484. return name;
  485. }
  486. }
  487. /*************************************************************************/
  488. /*************************************************************************/
  489. /*** ***/
  490. /*** FD Select table support ***/
  491. /*** ***/
  492. /*************************************************************************/
  493. /*************************************************************************/
  494. static void
  495. CFF_Done_FD_Select( CFF_FDSelect fdselect,
  496. FT_Stream stream )
  497. {
  498. if ( fdselect->data )
  499. FT_FRAME_RELEASE( fdselect->data );
  500. fdselect->data_size = 0;
  501. fdselect->format = 0;
  502. fdselect->range_count = 0;
  503. }
  504. static FT_Error
  505. CFF_Load_FD_Select( CFF_FDSelect fdselect,
  506. FT_UInt num_glyphs,
  507. FT_Stream stream,
  508. FT_ULong offset )
  509. {
  510. FT_Error error;
  511. FT_Byte format;
  512. FT_UInt num_ranges;
  513. /* read format */
  514. if ( FT_STREAM_SEEK( offset ) || FT_READ_BYTE( format ) )
  515. goto Exit;
  516. fdselect->format = format;
  517. fdselect->cache_count = 0; /* clear cache */
  518. switch ( format )
  519. {
  520. case 0: /* format 0, that's simple */
  521. fdselect->data_size = num_glyphs;
  522. goto Load_Data;
  523. case 3: /* format 3, a tad more complex */
  524. if ( FT_READ_USHORT( num_ranges ) )
  525. goto Exit;
  526. fdselect->data_size = num_ranges * 3 + 2;
  527. Load_Data:
  528. if ( FT_FRAME_EXTRACT( fdselect->data_size, fdselect->data ) )
  529. goto Exit;
  530. break;
  531. default: /* hmm... that's wrong */
  532. error = CFF_Err_Invalid_File_Format;
  533. }
  534. Exit:
  535. return error;
  536. }
  537. FT_LOCAL_DEF( FT_Byte )
  538. cff_fd_select_get( CFF_FDSelect fdselect,
  539. FT_UInt glyph_index )
  540. {
  541. FT_Byte fd = 0;
  542. switch ( fdselect->format )
  543. {
  544. case 0:
  545. fd = fdselect->data[glyph_index];
  546. break;
  547. case 3:
  548. /* first, compare to cache */
  549. if ( (FT_UInt)( glyph_index - fdselect->cache_first ) <
  550. fdselect->cache_count )
  551. {
  552. fd = fdselect->cache_fd;
  553. break;
  554. }
  555. /* then, lookup the ranges array */
  556. {
  557. FT_Byte* p = fdselect->data;
  558. FT_Byte* p_limit = p + fdselect->data_size;
  559. FT_Byte fd2;
  560. FT_UInt first, limit;
  561. first = FT_NEXT_USHORT( p );
  562. do
  563. {
  564. if ( glyph_index < first )
  565. break;
  566. fd2 = *p++;
  567. limit = FT_NEXT_USHORT( p );
  568. if ( glyph_index < limit )
  569. {
  570. fd = fd2;
  571. /* update cache */
  572. fdselect->cache_first = first;
  573. fdselect->cache_count = limit-first;
  574. fdselect->cache_fd = fd2;
  575. break;
  576. }
  577. first = limit;
  578. } while ( p < p_limit );
  579. }
  580. break;
  581. default:
  582. ;
  583. }
  584. return fd;
  585. }
  586. /*************************************************************************/
  587. /*************************************************************************/
  588. /*** ***/
  589. /*** CFF font support ***/
  590. /*** ***/
  591. /*************************************************************************/
  592. /*************************************************************************/
  593. static FT_Error
  594. cff_charset_compute_cids( CFF_Charset charset,
  595. FT_UInt num_glyphs,
  596. FT_Memory memory )
  597. {
  598. FT_Error error = FT_Err_Ok;
  599. FT_UInt i;
  600. FT_Long j;
  601. FT_UShort max_cid = 0;
  602. if ( charset->max_cid > 0 )
  603. goto Exit;
  604. for ( i = 0; i < num_glyphs; i++ )
  605. if ( charset->sids[i] > max_cid )
  606. max_cid = charset->sids[i];
  607. max_cid++;
  608. if ( FT_NEW_ARRAY( charset->cids, max_cid ) )
  609. goto Exit;
  610. /* When multiple GIDs map to the same CID, we choose the lowest */
  611. /* GID. This is not described in any spec, but it matches the */
  612. /* behaviour of recent Acroread versions. */
  613. for ( j = num_glyphs - 1; j >= 0 ; j-- )
  614. charset->cids[charset->sids[j]] = (FT_UShort)j;
  615. charset->max_cid = max_cid;
  616. charset->num_glyphs = num_glyphs;
  617. Exit:
  618. return error;
  619. }
  620. FT_LOCAL_DEF( FT_UInt )
  621. cff_charset_cid_to_gindex( CFF_Charset charset,
  622. FT_UInt cid )
  623. {
  624. FT_UInt result = 0;
  625. if ( cid < charset->max_cid )
  626. result = charset->cids[cid];
  627. return result;
  628. }
  629. static void
  630. cff_charset_free_cids( CFF_Charset charset,
  631. FT_Memory memory )
  632. {
  633. FT_FREE( charset->cids );
  634. charset->max_cid = 0;
  635. }
  636. static void
  637. cff_charset_done( CFF_Charset charset,
  638. FT_Stream stream )
  639. {
  640. FT_Memory memory = stream->memory;
  641. cff_charset_free_cids( charset, memory );
  642. FT_FREE( charset->sids );
  643. charset->format = 0;
  644. charset->offset = 0;
  645. }
  646. static FT_Error
  647. cff_charset_load( CFF_Charset charset,
  648. FT_UInt num_glyphs,
  649. FT_Stream stream,
  650. FT_ULong base_offset,
  651. FT_ULong offset,
  652. FT_Bool invert )
  653. {
  654. FT_Memory memory = stream->memory;
  655. FT_Error error = CFF_Err_Ok;
  656. FT_UShort glyph_sid;
  657. /* If the the offset is greater than 2, we have to parse the */
  658. /* charset table. */
  659. if ( offset > 2 )
  660. {
  661. FT_UInt j;
  662. charset->offset = base_offset + offset;
  663. /* Get the format of the table. */
  664. if ( FT_STREAM_SEEK( charset->offset ) ||
  665. FT_READ_BYTE( charset->format ) )
  666. goto Exit;
  667. /* Allocate memory for sids. */
  668. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  669. goto Exit;
  670. /* assign the .notdef glyph */
  671. charset->sids[0] = 0;
  672. switch ( charset->format )
  673. {
  674. case 0:
  675. if ( num_glyphs > 0 )
  676. {
  677. if ( FT_FRAME_ENTER( ( num_glyphs - 1 ) * 2 ) )
  678. goto Exit;
  679. for ( j = 1; j < num_glyphs; j++ )
  680. {
  681. FT_UShort sid = FT_GET_USHORT();
  682. /* this constant is given in the CFF specification */
  683. if ( sid < 65000L )
  684. charset->sids[j] = sid;
  685. else
  686. {
  687. FT_TRACE0(( "cff_charset_load:"
  688. " invalid SID value %d set to zero\n", sid ));
  689. charset->sids[j] = 0;
  690. }
  691. }
  692. FT_FRAME_EXIT();
  693. }
  694. break;
  695. case 1:
  696. case 2:
  697. {
  698. FT_UInt nleft;
  699. FT_UInt i;
  700. j = 1;
  701. while ( j < num_glyphs )
  702. {
  703. /* Read the first glyph sid of the range. */
  704. if ( FT_READ_USHORT( glyph_sid ) )
  705. goto Exit;
  706. /* Read the number of glyphs in the range. */
  707. if ( charset->format == 2 )
  708. {
  709. if ( FT_READ_USHORT( nleft ) )
  710. goto Exit;
  711. }
  712. else
  713. {
  714. if ( FT_READ_BYTE( nleft ) )
  715. goto Exit;
  716. }
  717. /* check whether the range contains at least one valid glyph; */
  718. /* the constant is given in the CFF specification */
  719. if ( glyph_sid >= 65000L ) {
  720. FT_ERROR(( "cff_charset_load: invalid SID range\n" ));
  721. error = CFF_Err_Invalid_File_Format;
  722. goto Exit;
  723. }
  724. /* try to rescue some of the SIDs if `nleft' is too large */
  725. if ( nleft > 65000L - 1L || glyph_sid >= 65000L - nleft ) {
  726. FT_ERROR(( "cff_charset_load: invalid SID range trimmed\n" ));
  727. nleft = ( FT_UInt )( 65000L - 1L - glyph_sid );
  728. }
  729. /* Fill in the range of sids -- `nleft + 1' glyphs. */
  730. for ( i = 0; j < num_glyphs && i <= nleft; i++, j++, glyph_sid++ )
  731. charset->sids[j] = glyph_sid;
  732. }
  733. }
  734. break;
  735. default:
  736. FT_ERROR(( "cff_charset_load: invalid table format\n" ));
  737. error = CFF_Err_Invalid_File_Format;
  738. goto Exit;
  739. }
  740. }
  741. else
  742. {
  743. /* Parse default tables corresponding to offset == 0, 1, or 2. */
  744. /* CFF specification intimates the following: */
  745. /* */
  746. /* In order to use a predefined charset, the following must be */
  747. /* true: The charset constructed for the glyphs in the font's */
  748. /* charstrings dictionary must match the predefined charset in */
  749. /* the first num_glyphs. */
  750. charset->offset = offset; /* record charset type */
  751. switch ( (FT_UInt)offset )
  752. {
  753. case 0:
  754. if ( num_glyphs > 229 )
  755. {
  756. FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
  757. "predefined charset (Adobe ISO-Latin)\n" ));
  758. error = CFF_Err_Invalid_File_Format;
  759. goto Exit;
  760. }
  761. /* Allocate memory for sids. */
  762. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  763. goto Exit;
  764. /* Copy the predefined charset into the allocated memory. */
  765. FT_ARRAY_COPY( charset->sids, cff_isoadobe_charset, num_glyphs );
  766. break;
  767. case 1:
  768. if ( num_glyphs > 166 )
  769. {
  770. FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
  771. "predefined charset (Adobe Expert)\n" ));
  772. error = CFF_Err_Invalid_File_Format;
  773. goto Exit;
  774. }
  775. /* Allocate memory for sids. */
  776. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  777. goto Exit;
  778. /* Copy the predefined charset into the allocated memory. */
  779. FT_ARRAY_COPY( charset->sids, cff_expert_charset, num_glyphs );
  780. break;
  781. case 2:
  782. if ( num_glyphs > 87 )
  783. {
  784. FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
  785. "predefined charset (Adobe Expert Subset)\n" ));
  786. error = CFF_Err_Invalid_File_Format;
  787. goto Exit;
  788. }
  789. /* Allocate memory for sids. */
  790. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  791. goto Exit;
  792. /* Copy the predefined charset into the allocated memory. */
  793. FT_ARRAY_COPY( charset->sids, cff_expertsubset_charset, num_glyphs );
  794. break;
  795. default:
  796. error = CFF_Err_Invalid_File_Format;
  797. goto Exit;
  798. }
  799. }
  800. /* we have to invert the `sids' array for subsetted CID-keyed fonts */
  801. if ( invert )
  802. error = cff_charset_compute_cids( charset, num_glyphs, memory );
  803. Exit:
  804. /* Clean up if there was an error. */
  805. if ( error )
  806. {
  807. FT_FREE( charset->sids );
  808. FT_FREE( charset->cids );
  809. charset->format = 0;
  810. charset->offset = 0;
  811. charset->sids = 0;
  812. }
  813. return error;
  814. }
  815. static void
  816. cff_encoding_done( CFF_Encoding encoding )
  817. {
  818. encoding->format = 0;
  819. encoding->offset = 0;
  820. encoding->count = 0;
  821. }
  822. static FT_Error
  823. cff_encoding_load( CFF_Encoding encoding,
  824. CFF_Charset charset,
  825. FT_UInt num_glyphs,
  826. FT_Stream stream,
  827. FT_ULong base_offset,
  828. FT_ULong offset )
  829. {
  830. FT_Error error = CFF_Err_Ok;
  831. FT_UInt count;
  832. FT_UInt j;
  833. FT_UShort glyph_sid;
  834. FT_UInt glyph_code;
  835. /* Check for charset->sids. If we do not have this, we fail. */
  836. if ( !charset->sids )
  837. {
  838. error = CFF_Err_Invalid_File_Format;
  839. goto Exit;
  840. }
  841. /* Zero out the code to gid/sid mappings. */
  842. for ( j = 0; j < 256; j++ )
  843. {
  844. encoding->sids [j] = 0;
  845. encoding->codes[j] = 0;
  846. }
  847. /* Note: The encoding table in a CFF font is indexed by glyph index; */
  848. /* the first encoded glyph index is 1. Hence, we read the character */
  849. /* code (`glyph_code') at index j and make the assignment: */
  850. /* */
  851. /* encoding->codes[glyph_code] = j + 1 */
  852. /* */
  853. /* We also make the assignment: */
  854. /* */
  855. /* encoding->sids[glyph_code] = charset->sids[j + 1] */
  856. /* */
  857. /* This gives us both a code to GID and a code to SID mapping. */
  858. if ( offset > 1 )
  859. {
  860. encoding->offset = base_offset + offset;
  861. /* we need to parse the table to determine its size */
  862. if ( FT_STREAM_SEEK( encoding->offset ) ||
  863. FT_READ_BYTE( encoding->format ) ||
  864. FT_READ_BYTE( count ) )
  865. goto Exit;
  866. switch ( encoding->format & 0x7F )
  867. {
  868. case 0:
  869. {
  870. FT_Byte* p;
  871. /* By convention, GID 0 is always ".notdef" and is never */
  872. /* coded in the font. Hence, the number of codes found */
  873. /* in the table is `count+1'. */
  874. /* */
  875. encoding->count = count + 1;
  876. if ( FT_FRAME_ENTER( count ) )
  877. goto Exit;
  878. p = (FT_Byte*)stream->cursor;
  879. for ( j = 1; j <= count; j++ )
  880. {
  881. glyph_code = *p++;
  882. /* Make sure j is not too big. */
  883. if ( j < num_glyphs )
  884. {
  885. /* Assign code to GID mapping. */
  886. encoding->codes[glyph_code] = (FT_UShort)j;
  887. /* Assign code to SID mapping. */
  888. encoding->sids[glyph_code] = charset->sids[j];
  889. }
  890. }
  891. FT_FRAME_EXIT();
  892. }
  893. break;
  894. case 1:
  895. {
  896. FT_UInt nleft;
  897. FT_UInt i = 1;
  898. FT_UInt k;
  899. encoding->count = 0;
  900. /* Parse the Format1 ranges. */
  901. for ( j = 0; j < count; j++, i += nleft )
  902. {
  903. /* Read the first glyph code of the range. */
  904. if ( FT_READ_BYTE( glyph_code ) )
  905. goto Exit;
  906. /* Read the number of codes in the range. */
  907. if ( FT_READ_BYTE( nleft ) )
  908. goto Exit;
  909. /* Increment nleft, so we read `nleft + 1' codes/sids. */
  910. nleft++;
  911. /* compute max number of character codes */
  912. if ( (FT_UInt)nleft > encoding->count )
  913. encoding->count = nleft;
  914. /* Fill in the range of codes/sids. */
  915. for ( k = i; k < nleft + i; k++, glyph_code++ )
  916. {
  917. /* Make sure k is not too big. */
  918. if ( k < num_glyphs && glyph_code < 256 )
  919. {
  920. /* Assign code to GID mapping. */
  921. encoding->codes[glyph_code] = (FT_UShort)k;
  922. /* Assign code to SID mapping. */
  923. encoding->sids[glyph_code] = charset->sids[k];
  924. }
  925. }
  926. }
  927. /* simple check; one never knows what can be found in a font */
  928. if ( encoding->count > 256 )
  929. encoding->count = 256;
  930. }
  931. break;
  932. default:
  933. FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
  934. error = CFF_Err_Invalid_File_Format;
  935. goto Exit;
  936. }
  937. /* Parse supplemental encodings, if any. */
  938. if ( encoding->format & 0x80 )
  939. {
  940. FT_UInt gindex;
  941. /* count supplements */
  942. if ( FT_READ_BYTE( count ) )
  943. goto Exit;
  944. for ( j = 0; j < count; j++ )
  945. {
  946. /* Read supplemental glyph code. */
  947. if ( FT_READ_BYTE( glyph_code ) )
  948. goto Exit;
  949. /* Read the SID associated with this glyph code. */
  950. if ( FT_READ_USHORT( glyph_sid ) )
  951. goto Exit;
  952. /* Assign code to SID mapping. */
  953. encoding->sids[glyph_code] = glyph_sid;
  954. /* First, look up GID which has been assigned to */
  955. /* SID glyph_sid. */
  956. for ( gindex = 0; gindex < num_glyphs; gindex++ )
  957. {
  958. if ( charset->sids[gindex] == glyph_sid )
  959. {
  960. encoding->codes[glyph_code] = (FT_UShort)gindex;
  961. break;
  962. }
  963. }
  964. }
  965. }
  966. }
  967. else
  968. {
  969. /* We take into account the fact a CFF font can use a predefined */
  970. /* encoding without containing all of the glyphs encoded by this */
  971. /* encoding (see the note at the end of section 12 in the CFF */
  972. /* specification). */
  973. switch ( (FT_UInt)offset )
  974. {
  975. case 0:
  976. /* First, copy the code to SID mapping. */
  977. FT_ARRAY_COPY( encoding->sids, cff_standard_encoding, 256 );
  978. goto Populate;
  979. case 1:
  980. /* First, copy the code to SID mapping. */
  981. FT_ARRAY_COPY( encoding->sids, cff_expert_encoding, 256 );
  982. Populate:
  983. /* Construct code to GID mapping from code to SID mapping */
  984. /* and charset. */
  985. encoding->count = 0;
  986. error = cff_charset_compute_cids( charset, num_glyphs,
  987. stream->memory );
  988. if ( error )
  989. goto Exit;
  990. for ( j = 0; j < 256; j++ )
  991. {
  992. FT_UInt sid = encoding->sids[j];
  993. FT_UInt gid = 0;
  994. if ( sid )
  995. gid = cff_charset_cid_to_gindex( charset, sid );
  996. if ( gid != 0 )
  997. {
  998. encoding->codes[j] = (FT_UShort)gid;
  999. if ( encoding->count < j + 1 )
  1000. encoding->count = j + 1;
  1001. }
  1002. else
  1003. {
  1004. encoding->codes[j] = 0;
  1005. encoding->sids [j] = 0;
  1006. }
  1007. }
  1008. break;
  1009. default:
  1010. FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
  1011. error = CFF_Err_Invalid_File_Format;
  1012. goto Exit;
  1013. }
  1014. }
  1015. Exit:
  1016. /* Clean up if there was an error. */
  1017. return error;
  1018. }
  1019. static FT_Error
  1020. cff_subfont_load( CFF_SubFont font,
  1021. CFF_Index idx,
  1022. FT_UInt font_index,
  1023. FT_Stream stream,
  1024. FT_ULong base_offset,
  1025. FT_Library library )
  1026. {
  1027. FT_Error error;
  1028. CFF_ParserRec parser;
  1029. FT_Byte* dict = NULL;
  1030. FT_ULong dict_len;
  1031. CFF_FontRecDict top = &font->font_dict;
  1032. CFF_Private priv = &font->private_dict;
  1033. cff_parser_init( &parser, CFF_CODE_TOPDICT, &font->font_dict, library );
  1034. /* set defaults */
  1035. FT_MEM_ZERO( top, sizeof ( *top ) );
  1036. top->underline_position = -100L << 16;
  1037. top->underline_thickness = 50L << 16;
  1038. top->charstring_type = 2;
  1039. top->font_matrix.xx = 0x10000L;
  1040. top->font_matrix.yy = 0x10000L;
  1041. top->cid_count = 8720;
  1042. /* we use the implementation specific SID value 0xFFFF to indicate */
  1043. /* missing entries */
  1044. top->version = 0xFFFFU;
  1045. top->notice = 0xFFFFU;
  1046. top->copyright = 0xFFFFU;
  1047. top->full_name = 0xFFFFU;
  1048. top->family_name = 0xFFFFU;
  1049. top->weight = 0xFFFFU;
  1050. top->embedded_postscript = 0xFFFFU;
  1051. top->cid_registry = 0xFFFFU;
  1052. top->cid_ordering = 0xFFFFU;
  1053. top->cid_font_name = 0xFFFFU;
  1054. error = cff_index_access_element( idx, font_index, &dict, &dict_len );
  1055. if ( !error )
  1056. error = cff_parser_run( &parser, dict, dict + dict_len );
  1057. cff_index_forget_element( idx, &dict );
  1058. if ( error )
  1059. goto Exit;
  1060. /* if it is a CID font, we stop there */
  1061. if ( top->cid_registry != 0xFFFFU )
  1062. goto Exit;
  1063. /* parse the private dictionary, if any */
  1064. if ( top->private_offset && top->private_size )
  1065. {
  1066. /* set defaults */
  1067. FT_MEM_ZERO( priv, sizeof ( *priv ) );
  1068. priv->blue_shift = 7;
  1069. priv->blue_fuzz = 1;
  1070. priv->lenIV = -1;
  1071. priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
  1072. priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
  1073. cff_parser_init( &parser, CFF_CODE_PRIVATE, priv, library );
  1074. if ( FT_STREAM_SEEK( base_offset + font->font_dict.private_offset ) ||
  1075. FT_FRAME_ENTER( font->font_dict.private_size ) )
  1076. goto Exit;
  1077. error = cff_parser_run( &parser,
  1078. (FT_Byte*)stream->cursor,
  1079. (FT_Byte*)stream->limit );
  1080. FT_FRAME_EXIT();
  1081. if ( error )
  1082. goto Exit;
  1083. /* ensure that `num_blue_values' is even */
  1084. priv->num_blue_values &= ~1;
  1085. }
  1086. /* read the local subrs, if any */
  1087. if ( priv->local_subrs_offset )
  1088. {
  1089. if ( FT_STREAM_SEEK( base_offset + top->private_offset +
  1090. priv->local_subrs_offset ) )
  1091. goto Exit;
  1092. error = cff_index_init( &font->local_subrs_index, stream, 1 );
  1093. if ( error )
  1094. goto Exit;
  1095. font->num_local_subrs = font->local_subrs_index.count;
  1096. error = cff_index_get_pointers( &font->local_subrs_index,
  1097. &font->local_subrs );
  1098. if ( error )
  1099. goto Exit;
  1100. }
  1101. Exit:
  1102. return error;
  1103. }
  1104. static void
  1105. cff_subfont_done( FT_Memory memory,
  1106. CFF_SubFont subfont )
  1107. {
  1108. if ( subfont )
  1109. {
  1110. cff_index_done( &subfont->local_subrs_index );
  1111. FT_FREE( subfont->local_subrs );
  1112. }
  1113. }
  1114. FT_LOCAL_DEF( FT_Error )
  1115. cff_font_load( FT_Library library,
  1116. FT_Stream stream,
  1117. FT_Int face_index,
  1118. CFF_Font font,
  1119. FT_Bool pure_cff )
  1120. {
  1121. static const FT_Frame_Field cff_header_fields[] =
  1122. {
  1123. #undef FT_STRUCTURE
  1124. #define FT_STRUCTURE CFF_FontRec
  1125. FT_FRAME_START( 4 ),
  1126. FT_FRAME_BYTE( version_major ),
  1127. FT_FRAME_BYTE( version_minor ),
  1128. FT_FRAME_BYTE( header_size ),
  1129. FT_FRAME_BYTE( absolute_offsize ),
  1130. FT_FRAME_END
  1131. };
  1132. FT_Error error;
  1133. FT_Memory memory = stream->memory;
  1134. FT_ULong base_offset;
  1135. CFF_FontRecDict dict;
  1136. FT_ZERO( font );
  1137. font->stream = stream;
  1138. font->memory = memory;
  1139. dict = &font->top_font.font_dict;
  1140. base_offset = FT_STREAM_POS();
  1141. /* read CFF font header */
  1142. if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) )
  1143. goto Exit;
  1144. /* check format */
  1145. if ( font->version_major != 1 ||
  1146. font->header_size < 4 ||
  1147. font->absolute_offsize > 4 )
  1148. {
  1149. FT_TRACE2(( "[not a CFF font header]\n" ));
  1150. error = CFF_Err_Unknown_File_Format;
  1151. goto Exit;
  1152. }
  1153. /* skip the rest of the header */
  1154. if ( FT_STREAM_SKIP( font->header_size - 4 ) )
  1155. goto Exit;
  1156. /* read the name, top dict, string and global subrs index */
  1157. if ( FT_SET_ERROR( cff_index_init( &font->name_index,
  1158. stream, 0 ) ) ||
  1159. FT_SET_ERROR( cff_index_init( &font->font_dict_index,
  1160. stream, 0 ) ) ||
  1161. FT_SET_ERROR( cff_index_init( &font->string_index,
  1162. stream, 0 ) ) ||
  1163. FT_SET_ERROR( cff_index_init( &font->global_subrs_index,
  1164. stream, 1 ) ) )
  1165. goto Exit;
  1166. /* well, we don't really forget the `disabled' fonts... */
  1167. font->num_faces = font->name_index.count;
  1168. if ( face_index >= (FT_Int)font->num_faces )
  1169. {
  1170. FT_ERROR(( "cff_font_load: incorrect face index = %d\n",
  1171. face_index ));
  1172. error = CFF_Err_Invalid_Argument;
  1173. }
  1174. /* in case of a font format check, simply exit now */
  1175. if ( face_index < 0 )
  1176. goto Exit;
  1177. /* now, parse the top-level font dictionary */
  1178. error = cff_subfont_load( &font->top_font,
  1179. &font->font_dict_index,
  1180. face_index,
  1181. stream,
  1182. base_offset,
  1183. library );
  1184. if ( error )
  1185. goto Exit;
  1186. if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) )
  1187. goto Exit;
  1188. error = cff_index_init( &font->charstrings_index, stream, 0 );
  1189. if ( error )
  1190. goto Exit;
  1191. /* now, check for a CID font */
  1192. if ( dict->cid_registry != 0xFFFFU )
  1193. {
  1194. CFF_IndexRec fd_index;
  1195. CFF_SubFont sub;
  1196. FT_UInt idx;
  1197. /* this is a CID-keyed font, we must now allocate a table of */
  1198. /* sub-fonts, then load each of them separately */
  1199. if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) )
  1200. goto Exit;
  1201. error = cff_index_init( &fd_index, stream, 0 );
  1202. if ( error )
  1203. goto Exit;
  1204. if ( fd_index.count > CFF_MAX_CID_FONTS )
  1205. {
  1206. FT_TRACE0(( "cff_font_load: FD array too large in CID font\n" ));
  1207. goto Fail_CID;
  1208. }
  1209. /* allocate & read each font dict independently */
  1210. font->num_subfonts = fd_index.count;
  1211. if ( FT_NEW_ARRAY( sub, fd_index.count ) )
  1212. goto Fail_CID;
  1213. /* set up pointer table */
  1214. for ( idx = 0; idx < fd_index.count; idx++ )
  1215. font->subfonts[idx] = sub + idx;
  1216. /* now load each subfont independently */
  1217. for ( idx = 0; idx < fd_index.count; idx++ )
  1218. {
  1219. sub = font->subfonts[idx];
  1220. error = cff_subfont_load( sub, &fd_index, idx,
  1221. stream, base_offset, library );
  1222. if ( error )
  1223. goto Fail_CID;
  1224. }
  1225. /* now load the FD Select array */
  1226. error = CFF_Load_FD_Select( &font->fd_select,
  1227. font->charstrings_index.count,
  1228. stream,
  1229. base_offset + dict->cid_fd_select_offset );
  1230. Fail_CID:
  1231. cff_index_done( &fd_index );
  1232. if ( error )
  1233. goto Exit;
  1234. }
  1235. else
  1236. font->num_subfonts = 0;
  1237. /* read the charstrings index now */
  1238. if ( dict->charstrings_offset == 0 )
  1239. {
  1240. FT_ERROR(( "cff_font_load: no charstrings offset\n" ));
  1241. error = CFF_Err_Unknown_File_Format;
  1242. goto Exit;
  1243. }
  1244. /* explicit the global subrs */
  1245. font->num_global_subrs = font->global_subrs_index.count;
  1246. font->num_glyphs = font->charstrings_index.count;
  1247. error = cff_index_get_pointers( &font->global_subrs_index,
  1248. &font->global_subrs ) ;
  1249. if ( error )
  1250. goto Exit;
  1251. /* read the Charset and Encoding tables if available */
  1252. if ( font->num_glyphs > 0 )
  1253. {
  1254. FT_Bool invert = FT_BOOL( dict->cid_registry != 0xFFFFU && pure_cff );
  1255. error = cff_charset_load( &font->charset, font->num_glyphs, stream,
  1256. base_offset, dict->charset_offset, invert );
  1257. if ( error )
  1258. goto Exit;
  1259. /* CID-keyed CFFs don't have an encoding */
  1260. if ( dict->cid_registry == 0xFFFFU )
  1261. {
  1262. error = cff_encoding_load( &font->encoding,
  1263. &font->charset,
  1264. font->num_glyphs,
  1265. stream,
  1266. base_offset,
  1267. dict->encoding_offset );
  1268. if ( error )
  1269. goto Exit;
  1270. }
  1271. }
  1272. /* get the font name (/CIDFontName for CID-keyed fonts, */
  1273. /* /FontName otherwise) */
  1274. font->font_name = cff_index_get_name( &font->name_index, face_index );
  1275. Exit:
  1276. return error;
  1277. }
  1278. FT_LOCAL_DEF( void )
  1279. cff_font_done( CFF_Font font )
  1280. {
  1281. FT_Memory memory = font->memory;
  1282. FT_UInt idx;
  1283. cff_index_done( &font->global_subrs_index );
  1284. cff_index_done( &font->string_index );
  1285. cff_index_done( &font->font_dict_index );
  1286. cff_index_done( &font->name_index );
  1287. cff_index_done( &font->charstrings_index );
  1288. /* release font dictionaries, but only if working with */
  1289. /* a CID keyed CFF font */
  1290. if ( font->num_subfonts > 0 )
  1291. {
  1292. for ( idx = 0; idx < font->num_subfonts; idx++ )
  1293. cff_subfont_done( memory, font->subfonts[idx] );
  1294. /* the subfonts array has been allocated as a single block */
  1295. FT_FREE( font->subfonts[0] );
  1296. }
  1297. cff_encoding_done( &font->encoding );
  1298. cff_charset_done( &font->charset, font->stream );
  1299. cff_subfont_done( memory, &font->top_font );
  1300. CFF_Done_FD_Select( &font->fd_select, font->stream );
  1301. if (font->font_info != NULL)
  1302. {
  1303. FT_FREE( font->font_info->version );
  1304. FT_FREE( font->font_info->notice );
  1305. FT_FREE( font->font_info->full_name );
  1306. FT_FREE( font->font_info->family_name );
  1307. FT_FREE( font->font_info->weight );
  1308. FT_FREE( font->font_info );
  1309. }
  1310. FT_FREE( font->registry );
  1311. FT_FREE( font->ordering );
  1312. FT_FREE( font->global_subrs );
  1313. FT_FREE( font->font_name );
  1314. }
  1315. /* END */