ttpost.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /***************************************************************************/
  2. /* */
  3. /* ttpost.c */
  4. /* */
  5. /* Postcript name table processing for TrueType and OpenType fonts */
  6. /* (body). */
  7. /* */
  8. /* Copyright 1996-2001, 2002 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. /*************************************************************************/
  19. /* */
  20. /* The post table is not completely loaded by the core engine. This */
  21. /* file loads the missing PS glyph names and implements an API to access */
  22. /* them. */
  23. /* */
  24. /*************************************************************************/
  25. #include <ft2build.h>
  26. #include FT_INTERNAL_STREAM_H
  27. #include FT_TRUETYPE_TAGS_H
  28. #include "ttpost.h"
  29. #include "ttload.h"
  30. #include "sferrors.h"
  31. /*************************************************************************/
  32. /* */
  33. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  34. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  35. /* messages during execution. */
  36. /* */
  37. #undef FT_COMPONENT
  38. #define FT_COMPONENT trace_ttpost
  39. /* If this configuration macro is defined, we rely on the `PSNames' */
  40. /* module to grab the glyph names. */
  41. #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
  42. #include FT_INTERNAL_POSTSCRIPT_NAMES_H
  43. #define MAC_NAME( x ) ( (FT_String*)psnames->macintosh_name( x ) )
  44. #else /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
  45. /* Otherwise, we ignore the `PSNames' module, and provide our own */
  46. /* table of Mac names. Thus, it is possible to build a version of */
  47. /* FreeType without the Type 1 driver & PSNames module. */
  48. #define MAC_NAME( x ) tt_post_default_names[x]
  49. /* the 258 default Mac PS glyph names */
  50. static const FT_String* tt_post_default_names[258] =
  51. {
  52. /* 0 */
  53. ".notdef", ".null", "CR", "space", "exclam",
  54. "quotedbl", "numbersign", "dollar", "percent", "ampersand",
  55. /* 10 */
  56. "quotesingle", "parenleft", "parenright", "asterisk", "plus",
  57. "comma", "hyphen", "period", "slash", "zero",
  58. /* 20 */
  59. "one", "two", "three", "four", "five",
  60. "six", "seven", "eight", "nine", "colon",
  61. /* 30 */
  62. "semicolon", "less", "equal", "greater", "question",
  63. "at", "A", "B", "C", "D",
  64. /* 40 */
  65. "E", "F", "G", "H", "I",
  66. "J", "K", "L", "M", "N",
  67. /* 50 */
  68. "O", "P", "Q", "R", "S",
  69. "T", "U", "V", "W", "X",
  70. /* 60 */
  71. "Y", "Z", "bracketleft", "backslash", "bracketright",
  72. "asciicircum", "underscore", "grave", "a", "b",
  73. /* 70 */
  74. "c", "d", "e", "f", "g",
  75. "h", "i", "j", "k", "l",
  76. /* 80 */
  77. "m", "n", "o", "p", "q",
  78. "r", "s", "t", "u", "v",
  79. /* 90 */
  80. "w", "x", "y", "z", "braceleft",
  81. "bar", "braceright", "asciitilde", "Adieresis", "Aring",
  82. /* 100 */
  83. "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis",
  84. "aacute", "agrave", "acircumflex", "adieresis", "atilde",
  85. /* 110 */
  86. "aring", "ccedilla", "eacute", "egrave", "ecircumflex",
  87. "edieresis", "iacute", "igrave", "icircumflex", "idieresis",
  88. /* 120 */
  89. "ntilde", "oacute", "ograve", "ocircumflex", "odieresis",
  90. "otilde", "uacute", "ugrave", "ucircumflex", "udieresis",
  91. /* 130 */
  92. "dagger", "degree", "cent", "sterling", "section",
  93. "bullet", "paragraph", "germandbls", "registered", "copyright",
  94. /* 140 */
  95. "trademark", "acute", "dieresis", "notequal", "AE",
  96. "Oslash", "infinity", "plusminus", "lessequal", "greaterequal",
  97. /* 150 */
  98. "yen", "mu", "partialdiff", "summation", "product",
  99. "pi", "integral", "ordfeminine", "ordmasculine", "Omega",
  100. /* 160 */
  101. "ae", "oslash", "questiondown", "exclamdown", "logicalnot",
  102. "radical", "florin", "approxequal", "Delta", "guillemotleft",
  103. /* 170 */
  104. "guillemotright", "ellipsis", "nbspace", "Agrave", "Atilde",
  105. "Otilde", "OE", "oe", "endash", "emdash",
  106. /* 180 */
  107. "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide",
  108. "lozenge", "ydieresis", "Ydieresis", "fraction", "currency",
  109. /* 190 */
  110. "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl",
  111. "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex",
  112. /* 200 */
  113. "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute",
  114. "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex",
  115. /* 210 */
  116. "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave",
  117. "dotlessi", "circumflex", "tilde", "macron", "breve",
  118. /* 220 */
  119. "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek",
  120. "caron", "Lslash", "lslash", "Scaron", "scaron",
  121. /* 230 */
  122. "Zcaron", "zcaron", "brokenbar", "Eth", "eth",
  123. "Yacute", "yacute", "Thorn", "thorn", "minus",
  124. /* 240 */
  125. "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf",
  126. "onequarter", "threequarters", "franc", "Gbreve", "gbreve",
  127. /* 250 */
  128. "Idot", "Scedilla", "scedilla", "Cacute", "cacute",
  129. "Ccaron", "ccaron", "dmacron",
  130. };
  131. #endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
  132. static FT_Error
  133. load_format_20( TT_Face face,
  134. FT_Stream stream )
  135. {
  136. FT_Memory memory = stream->memory;
  137. FT_Error error;
  138. FT_Int num_glyphs;
  139. FT_UShort num_names;
  140. FT_UShort* glyph_indices = 0;
  141. FT_Char** name_strings = 0;
  142. if ( FT_READ_USHORT( num_glyphs ) )
  143. goto Exit;
  144. /* UNDOCUMENTED! The number of glyphs in this table can be smaller */
  145. /* than the value in the maxp table (cf. cyberbit.ttf). */
  146. /* There already exist fonts which have more than 32768 glyph names */
  147. /* in this table, so the test for this threshold has been dropped. */
  148. if ( num_glyphs > face->root.num_glyphs )
  149. {
  150. error = SFNT_Err_Invalid_File_Format;
  151. goto Exit;
  152. }
  153. /* load the indices */
  154. {
  155. FT_Int n;
  156. if ( FT_NEW_ARRAY ( glyph_indices, num_glyphs ) ||
  157. FT_FRAME_ENTER( num_glyphs * 2L ) )
  158. goto Fail;
  159. for ( n = 0; n < num_glyphs; n++ )
  160. glyph_indices[n] = FT_GET_USHORT();
  161. FT_FRAME_EXIT();
  162. }
  163. /* compute number of names stored in table */
  164. {
  165. FT_Int n;
  166. num_names = 0;
  167. for ( n = 0; n < num_glyphs; n++ )
  168. {
  169. FT_Int idx;
  170. idx = glyph_indices[n];
  171. if ( idx >= 258 )
  172. {
  173. idx -= 257;
  174. if ( idx > num_names )
  175. num_names = (FT_UShort)idx;
  176. }
  177. }
  178. }
  179. /* now load the name strings */
  180. {
  181. FT_UShort n;
  182. if ( FT_NEW_ARRAY( name_strings, num_names ) )
  183. goto Fail;
  184. for ( n = 0; n < num_names; n++ )
  185. {
  186. FT_UInt len;
  187. if ( FT_READ_BYTE ( len ) ||
  188. FT_NEW_ARRAY( name_strings[n], len + 1 ) ||
  189. FT_STREAM_READ ( name_strings[n], len ) )
  190. goto Fail1;
  191. name_strings[n][len] = '\0';
  192. }
  193. }
  194. /* all right, set table fields and exit successfuly */
  195. {
  196. TT_Post_20 table = &face->postscript_names.names.format_20;
  197. table->num_glyphs = (FT_UShort)num_glyphs;
  198. table->num_names = (FT_UShort)num_names;
  199. table->glyph_indices = glyph_indices;
  200. table->glyph_names = name_strings;
  201. }
  202. return SFNT_Err_Ok;
  203. Fail1:
  204. {
  205. FT_UShort n;
  206. for ( n = 0; n < num_names; n++ )
  207. FT_FREE( name_strings[n] );
  208. }
  209. Fail:
  210. FT_FREE( name_strings );
  211. FT_FREE( glyph_indices );
  212. Exit:
  213. return error;
  214. }
  215. static FT_Error
  216. load_format_25( TT_Face face,
  217. FT_Stream stream )
  218. {
  219. FT_Memory memory = stream->memory;
  220. FT_Error error;
  221. FT_Int num_glyphs;
  222. FT_Char* offset_table = 0;
  223. /* UNDOCUMENTED! This value appears only in the Apple TT specs. */
  224. if ( FT_READ_USHORT( num_glyphs ) )
  225. goto Exit;
  226. /* check the number of glyphs */
  227. if ( num_glyphs > face->root.num_glyphs || num_glyphs > 258 )
  228. {
  229. error = SFNT_Err_Invalid_File_Format;
  230. goto Exit;
  231. }
  232. if ( FT_ALLOC( offset_table, num_glyphs ) ||
  233. FT_STREAM_READ( offset_table, num_glyphs ) )
  234. goto Fail;
  235. /* now check the offset table */
  236. {
  237. FT_Int n;
  238. for ( n = 0; n < num_glyphs; n++ )
  239. {
  240. FT_Long idx = (FT_Long)n + offset_table[n];
  241. if ( idx < 0 || idx > num_glyphs )
  242. {
  243. error = SFNT_Err_Invalid_File_Format;
  244. goto Fail;
  245. }
  246. }
  247. }
  248. /* OK, set table fields and exit successfuly */
  249. {
  250. TT_Post_25 table = &face->postscript_names.names.format_25;
  251. table->num_glyphs = (FT_UShort)num_glyphs;
  252. table->offsets = offset_table;
  253. }
  254. return SFNT_Err_Ok;
  255. Fail:
  256. FT_FREE( offset_table );
  257. Exit:
  258. return error;
  259. }
  260. static FT_Error
  261. load_post_names( TT_Face face )
  262. {
  263. FT_Stream stream;
  264. FT_Error error;
  265. FT_Fixed format;
  266. /* get a stream for the face's resource */
  267. stream = face->root.stream;
  268. /* seek to the beginning of the PS names table */
  269. error = face->goto_table( face, TTAG_post, stream, 0 );
  270. if ( error )
  271. goto Exit;
  272. format = face->postscript.FormatType;
  273. /* go to beginning of subtable */
  274. if ( FT_STREAM_SKIP( 32 ) )
  275. goto Exit;
  276. /* now read postscript table */
  277. if ( format == 0x00020000L )
  278. error = load_format_20( face, stream );
  279. else if ( format == 0x00028000L )
  280. error = load_format_25( face, stream );
  281. else
  282. error = SFNT_Err_Invalid_File_Format;
  283. face->postscript_names.loaded = 1;
  284. Exit:
  285. return error;
  286. }
  287. FT_LOCAL_DEF( void )
  288. tt_face_free_ps_names( TT_Face face )
  289. {
  290. FT_Memory memory = face->root.memory;
  291. TT_Post_Names names = &face->postscript_names;
  292. FT_Fixed format;
  293. if ( names->loaded )
  294. {
  295. format = face->postscript.FormatType;
  296. if ( format == 0x00020000L )
  297. {
  298. TT_Post_20 table = &names->names.format_20;
  299. FT_UShort n;
  300. FT_FREE( table->glyph_indices );
  301. table->num_glyphs = 0;
  302. for ( n = 0; n < table->num_names; n++ )
  303. FT_FREE( table->glyph_names[n] );
  304. FT_FREE( table->glyph_names );
  305. table->num_names = 0;
  306. }
  307. else if ( format == 0x00028000L )
  308. {
  309. TT_Post_25 table = &names->names.format_25;
  310. FT_FREE( table->offsets );
  311. table->num_glyphs = 0;
  312. }
  313. }
  314. names->loaded = 0;
  315. }
  316. /*************************************************************************/
  317. /* */
  318. /* <Function> */
  319. /* tt_face_get_ps_name */
  320. /* */
  321. /* <Description> */
  322. /* Gets the PostScript glyph name of a glyph. */
  323. /* */
  324. /* <Input> */
  325. /* face :: A handle to the parent face. */
  326. /* */
  327. /* idx :: The glyph index. */
  328. /* */
  329. /* PSname :: The address of a string pointer. Will be NULL in case */
  330. /* of error, otherwise it is a pointer to the glyph name. */
  331. /* */
  332. /* You must not modify the returned string! */
  333. /* */
  334. /* <Output> */
  335. /* FreeType error code. 0 means success. */
  336. /* */
  337. FT_LOCAL_DEF( FT_Error )
  338. tt_face_get_ps_name( TT_Face face,
  339. FT_UInt idx,
  340. FT_String** PSname )
  341. {
  342. FT_Error error;
  343. TT_Post_Names names;
  344. FT_Fixed format;
  345. #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
  346. PSNames_Service psnames;
  347. #endif
  348. if ( !face )
  349. return SFNT_Err_Invalid_Face_Handle;
  350. if ( idx >= (FT_UInt)face->root.num_glyphs )
  351. return SFNT_Err_Invalid_Glyph_Index;
  352. #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
  353. psnames = (PSNames_Service)face->psnames;
  354. if ( !psnames )
  355. return SFNT_Err_Unimplemented_Feature;
  356. #endif
  357. names = &face->postscript_names;
  358. /* `.notdef' by default */
  359. *PSname = MAC_NAME( 0 );
  360. format = face->postscript.FormatType;
  361. if ( format == 0x00010000L )
  362. {
  363. if ( idx < 258 ) /* paranoid checking */
  364. *PSname = MAC_NAME( idx );
  365. }
  366. else if ( format == 0x00020000L )
  367. {
  368. TT_Post_20 table = &names->names.format_20;
  369. if ( !names->loaded )
  370. {
  371. error = load_post_names( face );
  372. if ( error )
  373. goto End;
  374. }
  375. if ( idx < (FT_UInt)table->num_glyphs )
  376. {
  377. FT_UShort name_index = table->glyph_indices[idx];
  378. if ( name_index < 258 )
  379. *PSname = MAC_NAME( name_index );
  380. else
  381. *PSname = (FT_String*)table->glyph_names[name_index - 258];
  382. }
  383. }
  384. else if ( format == 0x00028000L )
  385. {
  386. TT_Post_25 table = &names->names.format_25;
  387. if ( !names->loaded )
  388. {
  389. error = load_post_names( face );
  390. if ( error )
  391. goto End;
  392. }
  393. if ( idx < (FT_UInt)table->num_glyphs ) /* paranoid checking */
  394. {
  395. idx += table->offsets[idx];
  396. *PSname = MAC_NAME( idx );
  397. }
  398. }
  399. /* nothing to do for format == 0x00030000L */
  400. End:
  401. return SFNT_Err_Ok;
  402. }
  403. /* END */