/modules/freetype2/src/cff/cffload.c

http://github.com/zpao/v8monkey · C · 1661 lines · 1166 code · 324 blank · 171 comment · 186 complexity · d294f10e900094b816c41f02086bd488 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, */
  8. /* 2010 by */
  9. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  10. /* */
  11. /* This file is part of the FreeType project, and may only be used, */
  12. /* modified, and distributed under the terms of the FreeType project */
  13. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  14. /* this file you indicate that you have read the license and */
  15. /* understand and accept it fully. */
  16. /* */
  17. /***************************************************************************/
  18. #include <ft2build.h>
  19. #include FT_INTERNAL_DEBUG_H
  20. #include FT_INTERNAL_OBJECTS_H
  21. #include FT_INTERNAL_STREAM_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 = CFF_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. /* The `pool' argument makes this function convert the index */
  320. /* entries to C-style strings (this is, NULL-terminated). */
  321. static FT_Error
  322. cff_index_get_pointers( CFF_Index idx,
  323. FT_Byte*** table,
  324. FT_Byte** pool )
  325. {
  326. FT_Error error = CFF_Err_Ok;
  327. FT_Memory memory = idx->stream->memory;
  328. FT_Byte** t;
  329. FT_Byte* new_bytes = NULL;
  330. *table = NULL;
  331. if ( idx->offsets == NULL )
  332. {
  333. error = cff_index_load_offsets( idx );
  334. if ( error )
  335. goto Exit;
  336. }
  337. if ( idx->count > 0 &&
  338. !FT_NEW_ARRAY( t, idx->count + 1 ) &&
  339. ( !pool || !FT_ALLOC( new_bytes,
  340. idx->data_size + idx->count ) ) )
  341. {
  342. FT_ULong n, cur_offset;
  343. FT_ULong extra = 0;
  344. FT_Byte* org_bytes = idx->bytes;
  345. /* at this point, `idx->offsets' can't be NULL */
  346. cur_offset = idx->offsets[0] - 1;
  347. /* sanity check */
  348. if ( cur_offset >= idx->data_size )
  349. {
  350. FT_TRACE0(( "cff_index_get_pointers:"
  351. " invalid first offset value %d set to zero\n",
  352. cur_offset ));
  353. cur_offset = 0;
  354. }
  355. if ( !pool )
  356. t[0] = org_bytes + cur_offset;
  357. else
  358. t[0] = new_bytes + cur_offset;
  359. for ( n = 1; n <= idx->count; n++ )
  360. {
  361. FT_ULong next_offset = idx->offsets[n] - 1;
  362. /* empty slot + two sanity checks for invalid offset tables */
  363. if ( next_offset == 0 ||
  364. next_offset < cur_offset ||
  365. ( next_offset >= idx->data_size && n < idx->count ) )
  366. next_offset = cur_offset;
  367. if ( !pool )
  368. t[n] = org_bytes + next_offset;
  369. else
  370. {
  371. t[n] = new_bytes + next_offset + extra;
  372. if ( next_offset != cur_offset )
  373. {
  374. FT_MEM_COPY( t[n - 1], org_bytes + cur_offset, t[n] - t[n - 1] );
  375. t[n][0] = '\0';
  376. t[n] += 1;
  377. extra++;
  378. }
  379. }
  380. cur_offset = next_offset;
  381. }
  382. *table = t;
  383. if ( pool )
  384. *pool = new_bytes;
  385. }
  386. Exit:
  387. return error;
  388. }
  389. FT_LOCAL_DEF( FT_Error )
  390. cff_index_access_element( CFF_Index idx,
  391. FT_UInt element,
  392. FT_Byte** pbytes,
  393. FT_ULong* pbyte_len )
  394. {
  395. FT_Error error = CFF_Err_Ok;
  396. if ( idx && idx->count > element )
  397. {
  398. /* compute start and end offsets */
  399. FT_Stream stream = idx->stream;
  400. FT_ULong off1, off2 = 0;
  401. /* load offsets from file or the offset table */
  402. if ( !idx->offsets )
  403. {
  404. FT_ULong pos = element * idx->off_size;
  405. if ( FT_STREAM_SEEK( idx->start + 3 + pos ) )
  406. goto Exit;
  407. off1 = cff_index_read_offset( idx, &error );
  408. if ( error )
  409. goto Exit;
  410. if ( off1 != 0 )
  411. {
  412. do
  413. {
  414. element++;
  415. off2 = cff_index_read_offset( idx, &error );
  416. }
  417. while ( off2 == 0 && element < idx->count );
  418. }
  419. }
  420. else /* use offsets table */
  421. {
  422. off1 = idx->offsets[element];
  423. if ( off1 )
  424. {
  425. do
  426. {
  427. element++;
  428. off2 = idx->offsets[element];
  429. } while ( off2 == 0 && element < idx->count );
  430. }
  431. }
  432. /* XXX: should check off2 does not exceed the end of this entry; */
  433. /* at present, only truncate off2 at the end of this stream */
  434. if ( off2 > stream->size + 1 ||
  435. idx->data_offset > stream->size - off2 + 1 )
  436. {
  437. FT_ERROR(( "cff_index_access_element:"
  438. " offset to next entry (%d)"
  439. " exceeds the end of stream (%d)\n",
  440. off2, stream->size - idx->data_offset + 1 ));
  441. off2 = stream->size - idx->data_offset + 1;
  442. }
  443. /* access element */
  444. if ( off1 && off2 > off1 )
  445. {
  446. *pbyte_len = off2 - off1;
  447. if ( idx->bytes )
  448. {
  449. /* this index was completely loaded in memory, that's easy */
  450. *pbytes = idx->bytes + off1 - 1;
  451. }
  452. else
  453. {
  454. /* this index is still on disk/file, access it through a frame */
  455. if ( FT_STREAM_SEEK( idx->data_offset + off1 - 1 ) ||
  456. FT_FRAME_EXTRACT( off2 - off1, *pbytes ) )
  457. goto Exit;
  458. }
  459. }
  460. else
  461. {
  462. /* empty index element */
  463. *pbytes = 0;
  464. *pbyte_len = 0;
  465. }
  466. }
  467. else
  468. error = CFF_Err_Invalid_Argument;
  469. Exit:
  470. return error;
  471. }
  472. FT_LOCAL_DEF( void )
  473. cff_index_forget_element( CFF_Index idx,
  474. FT_Byte** pbytes )
  475. {
  476. if ( idx->bytes == 0 )
  477. {
  478. FT_Stream stream = idx->stream;
  479. FT_FRAME_RELEASE( *pbytes );
  480. }
  481. }
  482. /* get an entry from Name INDEX */
  483. FT_LOCAL_DEF( FT_String* )
  484. cff_index_get_name( CFF_Font font,
  485. FT_UInt element )
  486. {
  487. CFF_Index idx = &font->name_index;
  488. FT_Memory memory = idx->stream->memory;
  489. FT_Byte* bytes;
  490. FT_ULong byte_len;
  491. FT_Error error;
  492. FT_String* name = 0;
  493. error = cff_index_access_element( idx, element, &bytes, &byte_len );
  494. if ( error )
  495. goto Exit;
  496. if ( !FT_ALLOC( name, byte_len + 1 ) )
  497. {
  498. FT_MEM_COPY( name, bytes, byte_len );
  499. name[byte_len] = 0;
  500. }
  501. cff_index_forget_element( idx, &bytes );
  502. Exit:
  503. return name;
  504. }
  505. /* get an entry from String INDEX */
  506. FT_LOCAL_DEF( FT_String* )
  507. cff_index_get_string( CFF_Font font,
  508. FT_UInt element )
  509. {
  510. return ( element < font->num_strings )
  511. ? (FT_String*)font->strings[element]
  512. : NULL;
  513. }
  514. FT_LOCAL_DEF( FT_String* )
  515. cff_index_get_sid_string( CFF_Font font,
  516. FT_UInt sid )
  517. {
  518. /* value 0xFFFFU indicates a missing dictionary entry */
  519. if ( sid == 0xFFFFU )
  520. return NULL;
  521. /* if it is not a standard string, return it */
  522. if ( sid > 390 )
  523. return cff_index_get_string( font, sid - 391 );
  524. /* CID-keyed CFF fonts don't have glyph names */
  525. if ( !font->psnames )
  526. return NULL;
  527. /* this is a standard string */
  528. return (FT_String *)font->psnames->adobe_std_strings( sid );
  529. }
  530. /*************************************************************************/
  531. /*************************************************************************/
  532. /*** ***/
  533. /*** FD Select table support ***/
  534. /*** ***/
  535. /*************************************************************************/
  536. /*************************************************************************/
  537. static void
  538. CFF_Done_FD_Select( CFF_FDSelect fdselect,
  539. FT_Stream stream )
  540. {
  541. if ( fdselect->data )
  542. FT_FRAME_RELEASE( fdselect->data );
  543. fdselect->data_size = 0;
  544. fdselect->format = 0;
  545. fdselect->range_count = 0;
  546. }
  547. static FT_Error
  548. CFF_Load_FD_Select( CFF_FDSelect fdselect,
  549. FT_UInt num_glyphs,
  550. FT_Stream stream,
  551. FT_ULong offset )
  552. {
  553. FT_Error error;
  554. FT_Byte format;
  555. FT_UInt num_ranges;
  556. /* read format */
  557. if ( FT_STREAM_SEEK( offset ) || FT_READ_BYTE( format ) )
  558. goto Exit;
  559. fdselect->format = format;
  560. fdselect->cache_count = 0; /* clear cache */
  561. switch ( format )
  562. {
  563. case 0: /* format 0, that's simple */
  564. fdselect->data_size = num_glyphs;
  565. goto Load_Data;
  566. case 3: /* format 3, a tad more complex */
  567. if ( FT_READ_USHORT( num_ranges ) )
  568. goto Exit;
  569. fdselect->data_size = num_ranges * 3 + 2;
  570. Load_Data:
  571. if ( FT_FRAME_EXTRACT( fdselect->data_size, fdselect->data ) )
  572. goto Exit;
  573. break;
  574. default: /* hmm... that's wrong */
  575. error = CFF_Err_Invalid_File_Format;
  576. }
  577. Exit:
  578. return error;
  579. }
  580. FT_LOCAL_DEF( FT_Byte )
  581. cff_fd_select_get( CFF_FDSelect fdselect,
  582. FT_UInt glyph_index )
  583. {
  584. FT_Byte fd = 0;
  585. switch ( fdselect->format )
  586. {
  587. case 0:
  588. fd = fdselect->data[glyph_index];
  589. break;
  590. case 3:
  591. /* first, compare to cache */
  592. if ( (FT_UInt)( glyph_index - fdselect->cache_first ) <
  593. fdselect->cache_count )
  594. {
  595. fd = fdselect->cache_fd;
  596. break;
  597. }
  598. /* then, lookup the ranges array */
  599. {
  600. FT_Byte* p = fdselect->data;
  601. FT_Byte* p_limit = p + fdselect->data_size;
  602. FT_Byte fd2;
  603. FT_UInt first, limit;
  604. first = FT_NEXT_USHORT( p );
  605. do
  606. {
  607. if ( glyph_index < first )
  608. break;
  609. fd2 = *p++;
  610. limit = FT_NEXT_USHORT( p );
  611. if ( glyph_index < limit )
  612. {
  613. fd = fd2;
  614. /* update cache */
  615. fdselect->cache_first = first;
  616. fdselect->cache_count = limit-first;
  617. fdselect->cache_fd = fd2;
  618. break;
  619. }
  620. first = limit;
  621. } while ( p < p_limit );
  622. }
  623. break;
  624. default:
  625. ;
  626. }
  627. return fd;
  628. }
  629. /*************************************************************************/
  630. /*************************************************************************/
  631. /*** ***/
  632. /*** CFF font support ***/
  633. /*** ***/
  634. /*************************************************************************/
  635. /*************************************************************************/
  636. static FT_Error
  637. cff_charset_compute_cids( CFF_Charset charset,
  638. FT_UInt num_glyphs,
  639. FT_Memory memory )
  640. {
  641. FT_Error error = CFF_Err_Ok;
  642. FT_UInt i;
  643. FT_Long j;
  644. FT_UShort max_cid = 0;
  645. if ( charset->max_cid > 0 )
  646. goto Exit;
  647. for ( i = 0; i < num_glyphs; i++ )
  648. {
  649. if ( charset->sids[i] > max_cid )
  650. max_cid = charset->sids[i];
  651. }
  652. if ( FT_NEW_ARRAY( charset->cids, (FT_ULong)max_cid + 1 ) )
  653. goto Exit;
  654. /* When multiple GIDs map to the same CID, we choose the lowest */
  655. /* GID. This is not described in any spec, but it matches the */
  656. /* behaviour of recent Acroread versions. */
  657. for ( j = num_glyphs - 1; j >= 0 ; j-- )
  658. charset->cids[charset->sids[j]] = (FT_UShort)j;
  659. charset->max_cid = max_cid;
  660. charset->num_glyphs = num_glyphs;
  661. Exit:
  662. return error;
  663. }
  664. FT_LOCAL_DEF( FT_UInt )
  665. cff_charset_cid_to_gindex( CFF_Charset charset,
  666. FT_UInt cid )
  667. {
  668. FT_UInt result = 0;
  669. if ( cid <= charset->max_cid )
  670. result = charset->cids[cid];
  671. return result;
  672. }
  673. static void
  674. cff_charset_free_cids( CFF_Charset charset,
  675. FT_Memory memory )
  676. {
  677. FT_FREE( charset->cids );
  678. charset->max_cid = 0;
  679. }
  680. static void
  681. cff_charset_done( CFF_Charset charset,
  682. FT_Stream stream )
  683. {
  684. FT_Memory memory = stream->memory;
  685. cff_charset_free_cids( charset, memory );
  686. FT_FREE( charset->sids );
  687. charset->format = 0;
  688. charset->offset = 0;
  689. }
  690. static FT_Error
  691. cff_charset_load( CFF_Charset charset,
  692. FT_UInt num_glyphs,
  693. FT_Stream stream,
  694. FT_ULong base_offset,
  695. FT_ULong offset,
  696. FT_Bool invert )
  697. {
  698. FT_Memory memory = stream->memory;
  699. FT_Error error = CFF_Err_Ok;
  700. FT_UShort glyph_sid;
  701. /* If the the offset is greater than 2, we have to parse the */
  702. /* charset table. */
  703. if ( offset > 2 )
  704. {
  705. FT_UInt j;
  706. charset->offset = base_offset + offset;
  707. /* Get the format of the table. */
  708. if ( FT_STREAM_SEEK( charset->offset ) ||
  709. FT_READ_BYTE( charset->format ) )
  710. goto Exit;
  711. /* Allocate memory for sids. */
  712. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  713. goto Exit;
  714. /* assign the .notdef glyph */
  715. charset->sids[0] = 0;
  716. switch ( charset->format )
  717. {
  718. case 0:
  719. if ( num_glyphs > 0 )
  720. {
  721. if ( FT_FRAME_ENTER( ( num_glyphs - 1 ) * 2 ) )
  722. goto Exit;
  723. for ( j = 1; j < num_glyphs; j++ )
  724. charset->sids[j] = FT_GET_USHORT();
  725. FT_FRAME_EXIT();
  726. }
  727. break;
  728. case 1:
  729. case 2:
  730. {
  731. FT_UInt nleft;
  732. FT_UInt i;
  733. j = 1;
  734. while ( j < num_glyphs )
  735. {
  736. /* Read the first glyph sid of the range. */
  737. if ( FT_READ_USHORT( glyph_sid ) )
  738. goto Exit;
  739. /* Read the number of glyphs in the range. */
  740. if ( charset->format == 2 )
  741. {
  742. if ( FT_READ_USHORT( nleft ) )
  743. goto Exit;
  744. }
  745. else
  746. {
  747. if ( FT_READ_BYTE( nleft ) )
  748. goto Exit;
  749. }
  750. /* try to rescue some of the SIDs if `nleft' is too large */
  751. if ( glyph_sid > 0xFFFFL - nleft )
  752. {
  753. FT_ERROR(( "cff_charset_load: invalid SID range trimmed"
  754. " nleft=%d -> %d\n", nleft, 0xFFFFL - glyph_sid ));
  755. nleft = ( FT_UInt )( 0xFFFFL - glyph_sid );
  756. }
  757. /* Fill in the range of sids -- `nleft + 1' glyphs. */
  758. for ( i = 0; j < num_glyphs && i <= nleft; i++, j++, glyph_sid++ )
  759. charset->sids[j] = glyph_sid;
  760. }
  761. }
  762. break;
  763. default:
  764. FT_ERROR(( "cff_charset_load: invalid table format\n" ));
  765. error = CFF_Err_Invalid_File_Format;
  766. goto Exit;
  767. }
  768. }
  769. else
  770. {
  771. /* Parse default tables corresponding to offset == 0, 1, or 2. */
  772. /* CFF specification intimates the following: */
  773. /* */
  774. /* In order to use a predefined charset, the following must be */
  775. /* true: The charset constructed for the glyphs in the font's */
  776. /* charstrings dictionary must match the predefined charset in */
  777. /* the first num_glyphs. */
  778. charset->offset = offset; /* record charset type */
  779. switch ( (FT_UInt)offset )
  780. {
  781. case 0:
  782. if ( num_glyphs > 229 )
  783. {
  784. FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
  785. "predefined charset (Adobe ISO-Latin)\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_isoadobe_charset, num_glyphs );
  794. break;
  795. case 1:
  796. if ( num_glyphs > 166 )
  797. {
  798. FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
  799. "predefined charset (Adobe Expert)\n" ));
  800. error = CFF_Err_Invalid_File_Format;
  801. goto Exit;
  802. }
  803. /* Allocate memory for sids. */
  804. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  805. goto Exit;
  806. /* Copy the predefined charset into the allocated memory. */
  807. FT_ARRAY_COPY( charset->sids, cff_expert_charset, num_glyphs );
  808. break;
  809. case 2:
  810. if ( num_glyphs > 87 )
  811. {
  812. FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
  813. "predefined charset (Adobe Expert Subset)\n" ));
  814. error = CFF_Err_Invalid_File_Format;
  815. goto Exit;
  816. }
  817. /* Allocate memory for sids. */
  818. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  819. goto Exit;
  820. /* Copy the predefined charset into the allocated memory. */
  821. FT_ARRAY_COPY( charset->sids, cff_expertsubset_charset, num_glyphs );
  822. break;
  823. default:
  824. error = CFF_Err_Invalid_File_Format;
  825. goto Exit;
  826. }
  827. }
  828. /* we have to invert the `sids' array for subsetted CID-keyed fonts */
  829. if ( invert )
  830. error = cff_charset_compute_cids( charset, num_glyphs, memory );
  831. Exit:
  832. /* Clean up if there was an error. */
  833. if ( error )
  834. {
  835. FT_FREE( charset->sids );
  836. FT_FREE( charset->cids );
  837. charset->format = 0;
  838. charset->offset = 0;
  839. charset->sids = 0;
  840. }
  841. return error;
  842. }
  843. static void
  844. cff_encoding_done( CFF_Encoding encoding )
  845. {
  846. encoding->format = 0;
  847. encoding->offset = 0;
  848. encoding->count = 0;
  849. }
  850. static FT_Error
  851. cff_encoding_load( CFF_Encoding encoding,
  852. CFF_Charset charset,
  853. FT_UInt num_glyphs,
  854. FT_Stream stream,
  855. FT_ULong base_offset,
  856. FT_ULong offset )
  857. {
  858. FT_Error error = CFF_Err_Ok;
  859. FT_UInt count;
  860. FT_UInt j;
  861. FT_UShort glyph_sid;
  862. FT_UInt glyph_code;
  863. /* Check for charset->sids. If we do not have this, we fail. */
  864. if ( !charset->sids )
  865. {
  866. error = CFF_Err_Invalid_File_Format;
  867. goto Exit;
  868. }
  869. /* Zero out the code to gid/sid mappings. */
  870. for ( j = 0; j < 256; j++ )
  871. {
  872. encoding->sids [j] = 0;
  873. encoding->codes[j] = 0;
  874. }
  875. /* Note: The encoding table in a CFF font is indexed by glyph index; */
  876. /* the first encoded glyph index is 1. Hence, we read the character */
  877. /* code (`glyph_code') at index j and make the assignment: */
  878. /* */
  879. /* encoding->codes[glyph_code] = j + 1 */
  880. /* */
  881. /* We also make the assignment: */
  882. /* */
  883. /* encoding->sids[glyph_code] = charset->sids[j + 1] */
  884. /* */
  885. /* This gives us both a code to GID and a code to SID mapping. */
  886. if ( offset > 1 )
  887. {
  888. encoding->offset = base_offset + offset;
  889. /* we need to parse the table to determine its size */
  890. if ( FT_STREAM_SEEK( encoding->offset ) ||
  891. FT_READ_BYTE( encoding->format ) ||
  892. FT_READ_BYTE( count ) )
  893. goto Exit;
  894. switch ( encoding->format & 0x7F )
  895. {
  896. case 0:
  897. {
  898. FT_Byte* p;
  899. /* By convention, GID 0 is always ".notdef" and is never */
  900. /* coded in the font. Hence, the number of codes found */
  901. /* in the table is `count+1'. */
  902. /* */
  903. encoding->count = count + 1;
  904. if ( FT_FRAME_ENTER( count ) )
  905. goto Exit;
  906. p = (FT_Byte*)stream->cursor;
  907. for ( j = 1; j <= count; j++ )
  908. {
  909. glyph_code = *p++;
  910. /* Make sure j is not too big. */
  911. if ( j < num_glyphs )
  912. {
  913. /* Assign code to GID mapping. */
  914. encoding->codes[glyph_code] = (FT_UShort)j;
  915. /* Assign code to SID mapping. */
  916. encoding->sids[glyph_code] = charset->sids[j];
  917. }
  918. }
  919. FT_FRAME_EXIT();
  920. }
  921. break;
  922. case 1:
  923. {
  924. FT_UInt nleft;
  925. FT_UInt i = 1;
  926. FT_UInt k;
  927. encoding->count = 0;
  928. /* Parse the Format1 ranges. */
  929. for ( j = 0; j < count; j++, i += nleft )
  930. {
  931. /* Read the first glyph code of the range. */
  932. if ( FT_READ_BYTE( glyph_code ) )
  933. goto Exit;
  934. /* Read the number of codes in the range. */
  935. if ( FT_READ_BYTE( nleft ) )
  936. goto Exit;
  937. /* Increment nleft, so we read `nleft + 1' codes/sids. */
  938. nleft++;
  939. /* compute max number of character codes */
  940. if ( (FT_UInt)nleft > encoding->count )
  941. encoding->count = nleft;
  942. /* Fill in the range of codes/sids. */
  943. for ( k = i; k < nleft + i; k++, glyph_code++ )
  944. {
  945. /* Make sure k is not too big. */
  946. if ( k < num_glyphs && glyph_code < 256 )
  947. {
  948. /* Assign code to GID mapping. */
  949. encoding->codes[glyph_code] = (FT_UShort)k;
  950. /* Assign code to SID mapping. */
  951. encoding->sids[glyph_code] = charset->sids[k];
  952. }
  953. }
  954. }
  955. /* simple check; one never knows what can be found in a font */
  956. if ( encoding->count > 256 )
  957. encoding->count = 256;
  958. }
  959. break;
  960. default:
  961. FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
  962. error = CFF_Err_Invalid_File_Format;
  963. goto Exit;
  964. }
  965. /* Parse supplemental encodings, if any. */
  966. if ( encoding->format & 0x80 )
  967. {
  968. FT_UInt gindex;
  969. /* count supplements */
  970. if ( FT_READ_BYTE( count ) )
  971. goto Exit;
  972. for ( j = 0; j < count; j++ )
  973. {
  974. /* Read supplemental glyph code. */
  975. if ( FT_READ_BYTE( glyph_code ) )
  976. goto Exit;
  977. /* Read the SID associated with this glyph code. */
  978. if ( FT_READ_USHORT( glyph_sid ) )
  979. goto Exit;
  980. /* Assign code to SID mapping. */
  981. encoding->sids[glyph_code] = glyph_sid;
  982. /* First, look up GID which has been assigned to */
  983. /* SID glyph_sid. */
  984. for ( gindex = 0; gindex < num_glyphs; gindex++ )
  985. {
  986. if ( charset->sids[gindex] == glyph_sid )
  987. {
  988. encoding->codes[glyph_code] = (FT_UShort)gindex;
  989. break;
  990. }
  991. }
  992. }
  993. }
  994. }
  995. else
  996. {
  997. /* We take into account the fact a CFF font can use a predefined */
  998. /* encoding without containing all of the glyphs encoded by this */
  999. /* encoding (see the note at the end of section 12 in the CFF */
  1000. /* specification). */
  1001. switch ( (FT_UInt)offset )
  1002. {
  1003. case 0:
  1004. /* First, copy the code to SID mapping. */
  1005. FT_ARRAY_COPY( encoding->sids, cff_standard_encoding, 256 );
  1006. goto Populate;
  1007. case 1:
  1008. /* First, copy the code to SID mapping. */
  1009. FT_ARRAY_COPY( encoding->sids, cff_expert_encoding, 256 );
  1010. Populate:
  1011. /* Construct code to GID mapping from code to SID mapping */
  1012. /* and charset. */
  1013. encoding->count = 0;
  1014. error = cff_charset_compute_cids( charset, num_glyphs,
  1015. stream->memory );
  1016. if ( error )
  1017. goto Exit;
  1018. for ( j = 0; j < 256; j++ )
  1019. {
  1020. FT_UInt sid = encoding->sids[j];
  1021. FT_UInt gid = 0;
  1022. if ( sid )
  1023. gid = cff_charset_cid_to_gindex( charset, sid );
  1024. if ( gid != 0 )
  1025. {
  1026. encoding->codes[j] = (FT_UShort)gid;
  1027. encoding->count = j + 1;
  1028. }
  1029. else
  1030. {
  1031. encoding->codes[j] = 0;
  1032. encoding->sids [j] = 0;
  1033. }
  1034. }
  1035. break;
  1036. default:
  1037. FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
  1038. error = CFF_Err_Invalid_File_Format;
  1039. goto Exit;
  1040. }
  1041. }
  1042. Exit:
  1043. /* Clean up if there was an error. */
  1044. return error;
  1045. }
  1046. static FT_Error
  1047. cff_subfont_load( CFF_SubFont font,
  1048. CFF_Index idx,
  1049. FT_UInt font_index,
  1050. FT_Stream stream,
  1051. FT_ULong base_offset,
  1052. FT_Library library )
  1053. {
  1054. FT_Error error;
  1055. CFF_ParserRec parser;
  1056. FT_Byte* dict = NULL;
  1057. FT_ULong dict_len;
  1058. CFF_FontRecDict top = &font->font_dict;
  1059. CFF_Private priv = &font->private_dict;
  1060. cff_parser_init( &parser, CFF_CODE_TOPDICT, &font->font_dict, library );
  1061. /* set defaults */
  1062. FT_MEM_ZERO( top, sizeof ( *top ) );
  1063. top->underline_position = -100L << 16;
  1064. top->underline_thickness = 50L << 16;
  1065. top->charstring_type = 2;
  1066. top->font_matrix.xx = 0x10000L;
  1067. top->font_matrix.yy = 0x10000L;
  1068. top->cid_count = 8720;
  1069. /* we use the implementation specific SID value 0xFFFF to indicate */
  1070. /* missing entries */
  1071. top->version = 0xFFFFU;
  1072. top->notice = 0xFFFFU;
  1073. top->copyright = 0xFFFFU;
  1074. top->full_name = 0xFFFFU;
  1075. top->family_name = 0xFFFFU;
  1076. top->weight = 0xFFFFU;
  1077. top->embedded_postscript = 0xFFFFU;
  1078. top->cid_registry = 0xFFFFU;
  1079. top->cid_ordering = 0xFFFFU;
  1080. top->cid_font_name = 0xFFFFU;
  1081. error = cff_index_access_element( idx, font_index, &dict, &dict_len );
  1082. if ( !error )
  1083. error = cff_parser_run( &parser, dict, dict + dict_len );
  1084. cff_index_forget_element( idx, &dict );
  1085. if ( error )
  1086. goto Exit;
  1087. /* if it is a CID font, we stop there */
  1088. if ( top->cid_registry != 0xFFFFU )
  1089. goto Exit;
  1090. /* parse the private dictionary, if any */
  1091. if ( top->private_offset && top->private_size )
  1092. {
  1093. /* set defaults */
  1094. FT_MEM_ZERO( priv, sizeof ( *priv ) );
  1095. priv->blue_shift = 7;
  1096. priv->blue_fuzz = 1;
  1097. priv->lenIV = -1;
  1098. priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
  1099. priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
  1100. cff_parser_init( &parser, CFF_CODE_PRIVATE, priv, library );
  1101. if ( FT_STREAM_SEEK( base_offset + font->font_dict.private_offset ) ||
  1102. FT_FRAME_ENTER( font->font_dict.private_size ) )
  1103. goto Exit;
  1104. error = cff_parser_run( &parser,
  1105. (FT_Byte*)stream->cursor,
  1106. (FT_Byte*)stream->limit );
  1107. FT_FRAME_EXIT();
  1108. if ( error )
  1109. goto Exit;
  1110. /* ensure that `num_blue_values' is even */
  1111. priv->num_blue_values &= ~1;
  1112. }
  1113. /* read the local subrs, if any */
  1114. if ( priv->local_subrs_offset )
  1115. {
  1116. if ( FT_STREAM_SEEK( base_offset + top->private_offset +
  1117. priv->local_subrs_offset ) )
  1118. goto Exit;
  1119. error = cff_index_init( &font->local_subrs_index, stream, 1 );
  1120. if ( error )
  1121. goto Exit;
  1122. error = cff_index_get_pointers( &font->local_subrs_index,
  1123. &font->local_subrs, NULL );
  1124. if ( error )
  1125. goto Exit;
  1126. }
  1127. Exit:
  1128. return error;
  1129. }
  1130. static void
  1131. cff_subfont_done( FT_Memory memory,
  1132. CFF_SubFont subfont )
  1133. {
  1134. if ( subfont )
  1135. {
  1136. cff_index_done( &subfont->local_subrs_index );
  1137. FT_FREE( subfont->local_subrs );
  1138. }
  1139. }
  1140. FT_LOCAL_DEF( FT_Error )
  1141. cff_font_load( FT_Library library,
  1142. FT_Stream stream,
  1143. FT_Int face_index,
  1144. CFF_Font font,
  1145. FT_Bool pure_cff )
  1146. {
  1147. static const FT_Frame_Field cff_header_fields[] =
  1148. {
  1149. #undef FT_STRUCTURE
  1150. #define FT_STRUCTURE CFF_FontRec
  1151. FT_FRAME_START( 4 ),
  1152. FT_FRAME_BYTE( version_major ),
  1153. FT_FRAME_BYTE( version_minor ),
  1154. FT_FRAME_BYTE( header_size ),
  1155. FT_FRAME_BYTE( absolute_offsize ),
  1156. FT_FRAME_END
  1157. };
  1158. FT_Error error;
  1159. FT_Memory memory = stream->memory;
  1160. FT_ULong base_offset;
  1161. CFF_FontRecDict dict;
  1162. CFF_IndexRec string_index;
  1163. FT_ZERO( font );
  1164. FT_ZERO( &string_index );
  1165. font->stream = stream;
  1166. font->memory = memory;
  1167. dict = &font->top_font.font_dict;
  1168. base_offset = FT_STREAM_POS();
  1169. /* read CFF font header */
  1170. if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) )
  1171. goto Exit;
  1172. /* check format */
  1173. if ( font->version_major != 1 ||
  1174. font->header_size < 4 ||
  1175. font->absolute_offsize > 4 )
  1176. {
  1177. FT_TRACE2(( "[not a CFF font header]\n" ));
  1178. error = CFF_Err_Unknown_File_Format;
  1179. goto Exit;
  1180. }
  1181. /* skip the rest of the header */
  1182. if ( FT_STREAM_SKIP( font->header_size - 4 ) )
  1183. goto Exit;
  1184. /* read the name, top dict, string and global subrs index */
  1185. if ( FT_SET_ERROR( cff_index_init( &font->name_index,
  1186. stream, 0 ) ) ||
  1187. FT_SET_ERROR( cff_index_init( &font->font_dict_index,
  1188. stream, 0 ) ) ||
  1189. FT_SET_ERROR( cff_index_init( &string_index,
  1190. stream, 1 ) ) ||
  1191. FT_SET_ERROR( cff_index_init( &font->global_subrs_index,
  1192. stream, 1 ) ) ||
  1193. FT_SET_ERROR( cff_index_get_pointers( &string_index,
  1194. &font->strings,
  1195. &font->string_pool ) ) )
  1196. goto Exit;
  1197. font->num_strings = string_index.count;
  1198. /* well, we don't really forget the `disabled' fonts... */
  1199. font->num_faces = font->name_index.count;
  1200. if ( face_index >= (FT_Int)font->num_faces )
  1201. {
  1202. FT_ERROR(( "cff_font_load: incorrect face index = %d\n",
  1203. face_index ));
  1204. error = CFF_Err_Invalid_Argument;
  1205. }
  1206. /* in case of a font format check, simply exit now */
  1207. if ( face_index < 0 )
  1208. goto Exit;
  1209. /* now, parse the top-level font dictionary */
  1210. error = cff_subfont_load( &font->top_font,
  1211. &font->font_dict_index,
  1212. face_index,
  1213. stream,
  1214. base_offset,
  1215. library );
  1216. if ( error )
  1217. goto Exit;
  1218. if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) )
  1219. goto Exit;
  1220. error = cff_index_init( &font->charstrings_index, stream, 0 );
  1221. if ( error )
  1222. goto Exit;
  1223. /* now, check for a CID font */
  1224. if ( dict->cid_registry != 0xFFFFU )
  1225. {
  1226. CFF_IndexRec fd_index;
  1227. CFF_SubFont sub;
  1228. FT_UInt idx;
  1229. /* this is a CID-keyed font, we must now allocate a table of */
  1230. /* sub-fonts, then load each of them separately */
  1231. if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) )
  1232. goto Exit;
  1233. error = cff_index_init( &fd_index, stream, 0 );
  1234. if ( error )
  1235. goto Exit;
  1236. if ( fd_index.count > CFF_MAX_CID_FONTS )
  1237. {
  1238. FT_TRACE0(( "cff_font_load: FD array too large in CID font\n" ));
  1239. goto Fail_CID;
  1240. }
  1241. /* allocate & read each font dict independently */
  1242. font->num_subfonts = fd_index.count;
  1243. if ( FT_NEW_ARRAY( sub, fd_index.count ) )
  1244. goto Fail_CID;
  1245. /* set up pointer table */
  1246. for ( idx = 0; idx < fd_index.count; idx++ )
  1247. font->subfonts[idx] = sub + idx;
  1248. /* now load each subfont independently */
  1249. for ( idx = 0; idx < fd_index.count; idx++ )
  1250. {
  1251. sub = font->subfonts[idx];
  1252. error = cff_subfont_load( sub, &fd_index, idx,
  1253. stream, base_offset, library );
  1254. if ( error )
  1255. goto Fail_CID;
  1256. }
  1257. /* now load the FD Select array */
  1258. error = CFF_Load_FD_Select( &font->fd_select,
  1259. font->charstrings_index.count,
  1260. stream,
  1261. base_offset + dict->cid_fd_select_offset );
  1262. Fail_CID:
  1263. cff_index_done( &fd_index );
  1264. if ( error )
  1265. goto Exit;
  1266. }
  1267. else
  1268. font->num_subfonts = 0;
  1269. /* read the charstrings index now */
  1270. if ( dict->charstrings_offset == 0 )
  1271. {
  1272. FT_ERROR(( "cff_font_load: no charstrings offset\n" ));
  1273. error = CFF_Err_Unknown_File_Format;
  1274. goto Exit;
  1275. }
  1276. font->num_glyphs = font->charstrings_index.count;
  1277. error = cff_index_get_pointers( &font->global_subrs_index,
  1278. &font->global_subrs, NULL );
  1279. if ( error )
  1280. goto Exit;
  1281. /* read the Charset and Encoding tables if available */
  1282. if ( font->num_glyphs > 0 )
  1283. {
  1284. FT_Bool invert = FT_BOOL( dict->cid_registry != 0xFFFFU && pure_cff );
  1285. error = cff_charset_load( &font->charset, font->num_glyphs, stream,
  1286. base_offset, dict->charset_offset, invert );
  1287. if ( error )
  1288. goto Exit;
  1289. /* CID-keyed CFFs don't have an encoding */
  1290. if ( dict->cid_registry == 0xFFFFU )
  1291. {
  1292. error = cff_encoding_load( &font->encoding,
  1293. &font->charset,
  1294. font->num_glyphs,
  1295. stream,
  1296. base_offset,
  1297. dict->encoding_offset );
  1298. if ( error )
  1299. goto Exit;
  1300. }
  1301. }
  1302. /* get the font name (/CIDFontName for CID-keyed fonts, */
  1303. /* /FontName otherwise) */
  1304. font->font_name = cff_index_get_name( font, face_index );
  1305. Exit:
  1306. cff_index_done( &string_index );
  1307. return error;
  1308. }
  1309. FT_LOCAL_DEF( void )
  1310. cff_font_done( CFF_Font font )
  1311. {
  1312. FT_Memory memory = font->memory;
  1313. FT_UInt idx;
  1314. cff_index_done( &font->global_subrs_index );
  1315. cff_index_done( &font->font_dict_index );
  1316. cff_index_done( &font->name_index );
  1317. cff_index_done( &font->charstrings_index );
  1318. /* release font dictionaries, but only if working with */
  1319. /* a CID keyed CFF font */
  1320. if ( font->num_subfonts > 0 )
  1321. {
  1322. for ( idx = 0; idx < font->num_subfonts; idx++ )
  1323. cff_subfont_done( memory, font->subfonts[idx] );
  1324. /* the subfonts array has been allocated as a single block */
  1325. FT_FREE( font->subfonts[0] );
  1326. }
  1327. cff_encoding_done( &font->encoding );
  1328. cff_charset_done( &font->charset, font->stream );
  1329. cff_subfont_done( memory, &font->top_font );
  1330. CFF_Done_FD_Select( &font->fd_select, font->stream );
  1331. FT_FREE( font->font_info );
  1332. FT_FREE( font->font_name );
  1333. FT_FREE( font->global_subrs );
  1334. FT_FREE( font->strings );
  1335. FT_FREE( font->string_pool );
  1336. }
  1337. /* END */