zfont42.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Copyright (C) 1996, 1998, 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zfont42.c,v 1.5 2000/09/19 19:00:53 lpd Exp $ */
  16. /* Type 42 font creation operator */
  17. #include "memory_.h"
  18. #include "ghost.h"
  19. #include "oper.h"
  20. #include "gsccode.h"
  21. #include "gsmatrix.h"
  22. #include "gxfont.h"
  23. #include "gxfont42.h"
  24. #include "bfont.h"
  25. #include "icharout.h"
  26. #include "idict.h"
  27. #include "idparam.h"
  28. #include "ifont42.h"
  29. #include "iname.h"
  30. #include "store.h"
  31. /* Forward references */
  32. private int z42_string_proc(P4(gs_font_type42 *, ulong, uint, const byte **));
  33. private int z42_gdir_get_outline(P3(gs_font_type42 *, uint, gs_const_string *));
  34. private font_proc_enumerate_glyph(z42_enumerate_glyph);
  35. private font_proc_enumerate_glyph(z42_gdir_enumerate_glyph);
  36. private font_proc_encode_char(z42_encode_char);
  37. private font_proc_glyph_info(z42_glyph_info);
  38. private font_proc_glyph_outline(z42_glyph_outline);
  39. /* <string|name> <font_dict> .buildfont11/42 <string|name> <font> */
  40. /* Build a type 11 (TrueType CID-keyed) or 42 (TrueType) font. */
  41. int
  42. build_gs_TrueType_font(i_ctx_t *i_ctx_p, os_ptr op, gs_font_type42 **ppfont,
  43. font_type ftype, gs_memory_type_ptr_t pstype,
  44. const char *bcstr, const char *bgstr,
  45. build_font_options_t options)
  46. {
  47. build_proc_refs build;
  48. ref sfnts, GlyphDirectory;
  49. gs_font_type42 *pfont;
  50. font_data *pdata;
  51. int code;
  52. code = build_proc_name_refs(&build, bcstr, bgstr);
  53. if (code < 0)
  54. return code;
  55. check_type(*op, t_dictionary);
  56. /*
  57. * Since build_gs_primitive_font may resize the dictionary and cause
  58. * pointers to become invalid, we save sfnts and GlyphDirectory.
  59. */
  60. if ((code = font_string_array_param(op, "sfnts", &sfnts)) < 0 ||
  61. (code = font_GlyphDirectory_param(op, &GlyphDirectory)) < 0
  62. )
  63. return code;
  64. code = build_gs_primitive_font(i_ctx_p, op, (gs_font_base **)ppfont,
  65. ftype, pstype, &build, options);
  66. if (code != 0)
  67. return code;
  68. pfont = *ppfont;
  69. pdata = pfont_data(pfont);
  70. ref_assign(&pdata->u.type42.sfnts, &sfnts);
  71. make_null_new(&pdata->u.type42.CIDMap);
  72. ref_assign(&pdata->u.type42.GlyphDirectory, &GlyphDirectory);
  73. pfont->data.string_proc = z42_string_proc;
  74. pfont->data.proc_data = (char *)pdata;
  75. code = gs_type42_font_init(pfont);
  76. if (code < 0)
  77. return code;
  78. /*
  79. * If the font has a GlyphDictionary, this replaces loca and glyf for
  80. * accessing character outlines. In this case, we use alternate
  81. * get_outline and enumerate_glyph procedures.
  82. */
  83. if (!r_has_type(&GlyphDirectory, t_null)) {
  84. pfont->data.get_outline = z42_gdir_get_outline;
  85. pfont->procs.enumerate_glyph = z42_gdir_enumerate_glyph;
  86. } else
  87. pfont->procs.enumerate_glyph = z42_enumerate_glyph;
  88. /*
  89. * The procedures that access glyph information must accept either
  90. * glyph names or glyph indexes.
  91. */
  92. pfont->procs.encode_char = z42_encode_char;
  93. pfont->procs.glyph_info = z42_glyph_info;
  94. pfont->procs.glyph_outline = z42_glyph_outline;
  95. return 0;
  96. }
  97. private int
  98. zbuildfont42(i_ctx_t *i_ctx_p)
  99. {
  100. os_ptr op = osp;
  101. gs_font_type42 *pfont;
  102. int code = build_gs_TrueType_font(i_ctx_p, op, &pfont, ft_TrueType,
  103. &st_gs_font_type42, "%Type42BuildChar",
  104. "%Type42BuildGlyph", bf_options_none);
  105. if (code < 0)
  106. return code;
  107. return define_gs_font((gs_font *)pfont);
  108. }
  109. /*
  110. * Check a parameter for being an array of strings. Return the parameter
  111. * value even if it is of the wrong type.
  112. */
  113. int
  114. font_string_array_param(os_ptr op, const char *kstr, ref *psa)
  115. {
  116. ref *pvsa;
  117. ref rstr0;
  118. int code;
  119. if (dict_find_string(op, kstr, &pvsa) <= 0)
  120. return_error(e_invalidfont);
  121. *psa = *pvsa;
  122. /*
  123. * We only check the first element of the array now, as a sanity test;
  124. * elements are checked as needed by string_array_access_proc.
  125. */
  126. if ((code = array_get(pvsa, 0L, &rstr0)) < 0)
  127. return code;
  128. if (!r_has_type(&rstr0, t_string))
  129. return_error(e_typecheck);
  130. return 0;
  131. }
  132. /*
  133. * Get a GlyphDirectory if present. Return 0 if present, 1 if absent,
  134. * or an error code.
  135. */
  136. int
  137. font_GlyphDirectory_param(os_ptr op, ref *pGlyphDirectory)
  138. {
  139. ref *pgdir;
  140. if (dict_find_string(op, "GlyphDirectory", &pgdir) <= 0)
  141. make_null(pGlyphDirectory);
  142. else if (!r_has_type(pgdir, t_dictionary) && !r_is_array(pgdir))
  143. return_error(e_typecheck);
  144. else
  145. *pGlyphDirectory = *pgdir;
  146. return 0;
  147. }
  148. /*
  149. * Access a given byte offset and length in an array of strings.
  150. * This is used for sfnts and for CIDMap. The int argument is 2 for sfnts
  151. * (because of the strange behavior of odd-length strings), 1 for CIDMap.
  152. */
  153. int
  154. string_array_access_proc(const ref *psa, int modulus, ulong offset,
  155. uint length, const byte **pdata)
  156. {
  157. ulong left = offset;
  158. uint index = 0;
  159. for (;; ++index) {
  160. ref rstr;
  161. int code = array_get(psa, index, &rstr);
  162. uint size;
  163. if (code < 0)
  164. return code;
  165. if (!r_has_type(&rstr, t_string))
  166. return_error(e_typecheck);
  167. /*
  168. * NOTE: According to the Adobe documentation, each sfnts
  169. * string should have even length. If the length is odd,
  170. * the additional byte is padding and should be ignored.
  171. */
  172. size = r_size(&rstr) & -modulus;
  173. if (left < size) {
  174. if (left + length > size)
  175. return_error(e_rangecheck);
  176. *pdata = rstr.value.const_bytes + left;
  177. return 0;
  178. }
  179. left -= size;
  180. }
  181. }
  182. /* ------ Initialization procedure ------ */
  183. const op_def zfont42_op_defs[] =
  184. {
  185. {"2.buildfont42", zbuildfont42},
  186. op_def_end(0)
  187. };
  188. /*
  189. * Get a glyph outline from GlyphDirectory. Return an empty string if
  190. * the glyph is missing or out of range.
  191. */
  192. int
  193. font_gdir_get_outline(const ref *pgdir, long glyph_index,
  194. gs_const_string * pgstr)
  195. {
  196. ref iglyph;
  197. ref gdef;
  198. ref *pgdef;
  199. int code;
  200. if (r_has_type(pgdir, t_dictionary)) {
  201. make_int(&iglyph, glyph_index);
  202. code = dict_find(pgdir, &iglyph, &pgdef) - 1; /* 0 => not found */
  203. } else {
  204. code = array_get(pgdir, glyph_index, &gdef);
  205. pgdef = &gdef;
  206. }
  207. if (code < 0) {
  208. pgstr->data = 0;
  209. pgstr->size = 0;
  210. } else if (!r_has_type(pgdef, t_string)) {
  211. return_error(e_typecheck);
  212. } else {
  213. pgstr->data = pgdef->value.const_bytes;
  214. pgstr->size = r_size(pgdef);
  215. }
  216. return 0;
  217. }
  218. private int
  219. z42_gdir_get_outline(gs_font_type42 * pfont, uint glyph_index,
  220. gs_const_string * pgstr)
  221. {
  222. const font_data *pfdata = pfont_data(pfont);
  223. const ref *pgdir = &pfdata->u.type42.GlyphDirectory;
  224. return font_gdir_get_outline(pgdir, (long)glyph_index, pgstr);
  225. }
  226. /* Reduce a glyph name to a glyph index if needed. */
  227. private gs_glyph
  228. glyph_to_index(const gs_font *font, gs_glyph glyph)
  229. {
  230. ref gref;
  231. ref *pcstr;
  232. if (glyph >= gs_min_cid_glyph)
  233. return glyph;
  234. name_index_ref(glyph, &gref);
  235. if (dict_find(&pfont_data(font)->CharStrings, &gref, &pcstr) > 0 &&
  236. r_has_type(pcstr, t_integer)
  237. ) {
  238. gs_glyph index_glyph = pcstr->value.intval + gs_min_cid_glyph;
  239. if (index_glyph >= gs_min_cid_glyph && index_glyph <= gs_max_glyph)
  240. return index_glyph;
  241. }
  242. return gs_min_cid_glyph; /* glyph 0 is notdef */
  243. }
  244. /* Enumerate glyphs from CharStrings or loca / glyf. */
  245. private int
  246. z42_enumerate_glyph(gs_font *font, int *pindex, gs_glyph_space_t glyph_space,
  247. gs_glyph *pglyph)
  248. {
  249. if (glyph_space == GLYPH_SPACE_INDEX)
  250. return gs_type42_enumerate_glyph(font, pindex, glyph_space, pglyph);
  251. else {
  252. const ref *pcsdict = &pfont_data(font)->CharStrings;
  253. return zchar_enumerate_glyph(pcsdict, pindex, pglyph);
  254. }
  255. }
  256. /* Enumerate glyphs (keys) from GlyphDirectory instead of loca / glyf. */
  257. private int
  258. z42_gdir_enumerate_glyph(gs_font *font, int *pindex,
  259. gs_glyph_space_t glyph_space, gs_glyph *pglyph)
  260. {
  261. const ref *pgdict;
  262. if (glyph_space == GLYPH_SPACE_INDEX) {
  263. pgdict = &pfont_data(font)->u.type42.GlyphDirectory;
  264. if (!r_has_type(pgdict, t_dictionary)) {
  265. ref gdef;
  266. for (;; (*pindex)++) {
  267. if (array_get(pgdict, (long)*pindex, &gdef) < 0) {
  268. *pindex = 0;
  269. return 0;
  270. }
  271. if (!r_has_type(&gdef, t_null)) {
  272. *pglyph = gs_min_cid_glyph + (*pindex)++;
  273. return 0;
  274. }
  275. }
  276. }
  277. } else
  278. pgdict = &pfont_data(font)->CharStrings;
  279. return zchar_enumerate_glyph(pgdict, pindex, pglyph);
  280. }
  281. /*
  282. * Define font procedures that accept either a character name or a glyph
  283. * index as the glyph.
  284. */
  285. private gs_glyph
  286. z42_encode_char(gs_font *font, gs_char chr, gs_glyph_space_t glyph_space)
  287. {
  288. gs_glyph glyph = zfont_encode_char(font, chr, glyph_space);
  289. return (glyph_space == GLYPH_SPACE_INDEX && glyph != gs_no_glyph ?
  290. glyph_to_index(font, glyph) : glyph);
  291. }
  292. private int
  293. z42_glyph_outline(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  294. gx_path *ppath)
  295. {
  296. return gs_type42_glyph_outline(font, glyph_to_index(font, glyph),
  297. pmat, ppath);
  298. }
  299. private int
  300. z42_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  301. int members, gs_glyph_info_t *info)
  302. {
  303. return gs_type42_glyph_info(font, glyph_to_index(font, glyph),
  304. pmat, members, info);
  305. }
  306. /* Procedure for accessing the sfnts array. */
  307. private int
  308. z42_string_proc(gs_font_type42 * pfont, ulong offset, uint length,
  309. const byte ** pdata)
  310. {
  311. return string_array_access_proc(&pfont_data(pfont)->u.type42.sfnts, 2,
  312. offset, length, pdata);
  313. }