1
0

iccfont.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Copyright (C) 1992-2004 artofcode LLC. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: iccfont.c,v 1.11 2004/10/26 02:50:56 giles Exp $ */
  14. /* Initialization support for compiled fonts */
  15. #include "string_.h"
  16. #include "ghost.h"
  17. #include "gsstruct.h" /* for iscan.h */
  18. #include "gscencs.h"
  19. #include "gsmatrix.h"
  20. #include "gxfont.h" /* for ifont.h */
  21. #include "ccfont.h"
  22. #include "ierrors.h"
  23. #include "ialloc.h"
  24. #include "idict.h"
  25. #include "ifont.h"
  26. #include "iname.h"
  27. #include "isave.h" /* for ialloc_ref_array */
  28. #include "iutil.h"
  29. #include "oper.h"
  30. #include "ostack.h" /* for iscan.h */
  31. #include "store.h"
  32. #include "stream.h" /* for iscan.h */
  33. #include "strimpl.h" /* for sfilter.h for picky compilers */
  34. #include "sfilter.h" /* for iscan.h */
  35. #include "iscan.h"
  36. /* ------ Private code ------ */
  37. /* Forward references */
  38. private int cfont_ref_from_string(i_ctx_t *, ref *, const char *, uint);
  39. typedef struct {
  40. i_ctx_t *i_ctx_p;
  41. const char *str_array;
  42. ref next;
  43. } str_enum;
  44. inline private void
  45. init_str_enum(str_enum *pse, i_ctx_t *i_ctx_p, const char *ksa)
  46. {
  47. pse->i_ctx_p = i_ctx_p;
  48. pse->str_array = ksa;
  49. }
  50. typedef struct {
  51. cfont_dict_keys keys;
  52. str_enum strings;
  53. } key_enum;
  54. inline private void
  55. init_key_enum(key_enum *pke, i_ctx_t *i_ctx_p, const cfont_dict_keys *pkeys,
  56. const char *ksa)
  57. {
  58. pke->keys = *pkeys;
  59. init_str_enum(&pke->strings, i_ctx_p, ksa);
  60. }
  61. /* Check for reaching the end of the keys. */
  62. inline private bool
  63. more_keys(const key_enum *pke)
  64. {
  65. return (pke->keys.num_enc_keys | pke->keys.num_str_keys);
  66. }
  67. /* Get the next string from a string array. */
  68. /* Return 1 if it was a string, 0 if it was something else, */
  69. /* or an error code. */
  70. private int
  71. cfont_next_string(str_enum * pse)
  72. {
  73. const byte *str = (const byte *)pse->str_array;
  74. uint len = (str[0] << 8) + str[1];
  75. if (len == 0xffff) {
  76. make_null(&pse->next);
  77. pse->str_array += 2;
  78. return 0;
  79. } else if (len >= 0xff00) {
  80. int code;
  81. len = ((len & 0xff) << 8) + str[2];
  82. code = cfont_ref_from_string(pse->i_ctx_p, &pse->next,
  83. pse->str_array + 3, len);
  84. if (code < 0)
  85. return code;
  86. pse->str_array += 3 + len;
  87. return 0;
  88. }
  89. make_const_string(&pse->next, avm_foreign, len, str + 2);
  90. pse->str_array += 2 + len;
  91. return 1;
  92. }
  93. /* Put the next entry into a dictionary. */
  94. /* We know that more_keys(kp) is true. */
  95. private int
  96. cfont_put_next(ref * pdict, key_enum * kep, const ref * pvalue)
  97. {
  98. i_ctx_t *i_ctx_p = kep->strings.i_ctx_p;
  99. cfont_dict_keys * const kp = &kep->keys;
  100. ref kname;
  101. int code;
  102. if (pdict->value.pdict == 0) {
  103. /* First time, create the dictionary. */
  104. code = dict_create(kp->num_enc_keys + kp->num_str_keys +
  105. kp->extra_slots, pdict);
  106. if (code < 0)
  107. return code;
  108. }
  109. if (kp->num_enc_keys) {
  110. const charindex *skp = kp->enc_keys++;
  111. gs_glyph glyph = gs_c_known_encode((gs_char)skp->charx, skp->encx);
  112. gs_const_string gstr;
  113. if (glyph == GS_NO_GLYPH)
  114. code = gs_note_error(e_undefined);
  115. else if ((code = gs_c_glyph_name(glyph, &gstr)) >= 0)
  116. code = name_ref(imemory, gstr.data, gstr.size, &kname, 0);
  117. kp->num_enc_keys--;
  118. } else { /* must have kp->num_str_keys != 0 */
  119. code = cfont_next_string(&kep->strings);
  120. if (code != 1)
  121. return (code < 0 ? code : gs_note_error(e_Fatal));
  122. code = name_ref(imemory, kep->strings.next.value.const_bytes,
  123. r_size(&kep->strings.next), &kname, 0);
  124. kp->num_str_keys--;
  125. }
  126. if (code < 0)
  127. return code;
  128. return dict_put(pdict, &kname, pvalue, &i_ctx_p->dict_stack);
  129. }
  130. /* ------ Routines called from compiled font initialization ------ */
  131. /* Create a dictionary with general ref values. */
  132. private int
  133. cfont_ref_dict_create(i_ctx_t *i_ctx_p, ref *pdict,
  134. const cfont_dict_keys *kp, cfont_string_array ksa,
  135. const ref *values)
  136. {
  137. key_enum kenum;
  138. const ref *vp = values;
  139. init_key_enum(&kenum, i_ctx_p, kp, ksa);
  140. pdict->value.pdict = 0;
  141. while (more_keys(&kenum)) {
  142. const ref *pvalue = vp++;
  143. int code = cfont_put_next(pdict, &kenum, pvalue);
  144. if (code < 0)
  145. return code;
  146. }
  147. r_store_attrs(dict_access_ref(pdict), a_all, kp->dict_attrs);
  148. return 0;
  149. }
  150. /* Create a dictionary with string/null values. */
  151. private int
  152. cfont_string_dict_create(i_ctx_t *i_ctx_p, ref *pdict,
  153. const cfont_dict_keys *kp, cfont_string_array ksa,
  154. cfont_string_array kva)
  155. {
  156. key_enum kenum;
  157. str_enum senum;
  158. uint attrs = kp->value_attrs;
  159. init_key_enum(&kenum, i_ctx_p, kp, ksa);
  160. init_str_enum(&senum, i_ctx_p, kva);
  161. pdict->value.pdict = 0;
  162. while (more_keys(&kenum)) {
  163. int code = cfont_next_string(&senum);
  164. switch (code) {
  165. default: /* error */
  166. return code;
  167. case 1: /* string */
  168. r_set_attrs(&senum.next, attrs);
  169. case 0: /* other */
  170. ;
  171. }
  172. code = cfont_put_next(pdict, &kenum, &senum.next);
  173. if (code < 0)
  174. return code;
  175. }
  176. r_store_attrs(dict_access_ref(pdict), a_all, kp->dict_attrs);
  177. return 0;
  178. }
  179. /* Create a dictionary with number values. */
  180. private int
  181. cfont_num_dict_create(i_ctx_t *i_ctx_p, ref * pdict,
  182. const cfont_dict_keys * kp, cfont_string_array ksa,
  183. const ref * values, const char *lengths)
  184. {
  185. key_enum kenum;
  186. const ref *vp = values;
  187. const char *lp = lengths;
  188. ref vnum;
  189. init_key_enum(&kenum, i_ctx_p, kp, ksa);
  190. pdict->value.pdict = 0;
  191. while (more_keys(&kenum)) {
  192. int len = (lp == 0 ? 0 : *lp++);
  193. int code;
  194. if (len == 0)
  195. vnum = *vp++;
  196. else {
  197. --len;
  198. make_const_array(&vnum, avm_foreign | a_readonly, len, vp);
  199. vp += len;
  200. }
  201. code = cfont_put_next(pdict, &kenum, &vnum);
  202. if (code < 0)
  203. return code;
  204. }
  205. r_store_attrs(dict_access_ref(pdict), a_all, kp->dict_attrs);
  206. return 0;
  207. }
  208. /* Create an array with name values. */
  209. private int
  210. cfont_name_array_create(i_ctx_t *i_ctx_p, ref * parray, cfont_string_array ksa,
  211. int size)
  212. {
  213. int code = ialloc_ref_array(parray, a_readonly, size,
  214. "cfont_name_array_create");
  215. ref *aptr = parray->value.refs;
  216. int i;
  217. str_enum senum;
  218. if (code < 0)
  219. return code;
  220. init_str_enum(&senum, i_ctx_p, ksa);
  221. for (i = 0; i < size; i++, aptr++) {
  222. ref nref;
  223. int code = cfont_next_string(&senum);
  224. if (code != 1)
  225. return (code < 0 ? code : gs_note_error(e_Fatal));
  226. code = name_ref(imemory, senum.next.value.const_bytes,
  227. r_size(&senum.next), &nref, 0);
  228. if (code < 0)
  229. return code;
  230. ref_assign_new(aptr, &nref);
  231. }
  232. return 0;
  233. }
  234. /* Create an array with string/null values. */
  235. private int
  236. cfont_string_array_create(i_ctx_t *i_ctx_p, ref * parray,
  237. cfont_string_array ksa, int size, uint attrs)
  238. {
  239. int code = ialloc_ref_array(parray, a_readonly, size,
  240. "cfont_string_array_create");
  241. ref *aptr = parray->value.refs;
  242. int i;
  243. str_enum senum;
  244. if (code < 0)
  245. return code;
  246. init_str_enum(&senum, i_ctx_p, ksa);
  247. for (i = 0; i < size; i++, aptr++) {
  248. int code = cfont_next_string(&senum);
  249. switch (code) {
  250. default: /* error */
  251. return code;
  252. case 1: /* string */
  253. r_set_attrs(&senum.next, attrs);
  254. case 0: /* other */
  255. ;
  256. }
  257. ref_mark_new(&senum.next);
  258. *aptr = senum.next;
  259. }
  260. return 0;
  261. }
  262. /* Create an array with scalar values. */
  263. private int
  264. cfont_scalar_array_create(i_ctx_t *i_ctx_p, ref * parray,
  265. const ref *va, int size, uint attrs)
  266. {
  267. int code = ialloc_ref_array(parray, attrs, size,
  268. "cfont_scalar_array_create");
  269. ref *aptr = parray->value.refs;
  270. uint elt_attrs = attrs | ialloc_new_mask;
  271. int i;
  272. if (code < 0)
  273. return code;
  274. memcpy(aptr, va, size * sizeof(ref));
  275. for (i = 0; i < size; i++, aptr++)
  276. r_set_attrs(aptr, elt_attrs);
  277. return 0;
  278. }
  279. /* Create a name. */
  280. private int
  281. cfont_name_create(i_ctx_t *i_ctx_p, ref * pnref, const char *str)
  282. {
  283. return name_ref(imemory, (const byte *)str, strlen(str), pnref, 0);
  284. }
  285. /* Create an object by parsing a string. */
  286. private int
  287. cfont_ref_from_string(i_ctx_t *i_ctx_p, ref * pref, const char *str, uint len)
  288. {
  289. scanner_state sstate;
  290. stream s;
  291. int code;
  292. scanner_state_init(&sstate, false);
  293. s_init(&s, imemory);
  294. sread_string(&s, (const byte *)str, len);
  295. code = scan_token(i_ctx_p, &s, pref, &sstate);
  296. return (code <= 0 ? code : gs_note_error(e_Fatal));
  297. }
  298. /* ------ Initialization ------ */
  299. /* Procedure vector passed to font initialization procedures. */
  300. private const cfont_procs ccfont_procs = {
  301. cfont_ref_dict_create,
  302. cfont_string_dict_create,
  303. cfont_num_dict_create,
  304. cfont_name_array_create,
  305. cfont_string_array_create,
  306. cfont_scalar_array_create,
  307. cfont_name_create,
  308. cfont_ref_from_string
  309. };
  310. /* null .getccfont <number-of-fonts> */
  311. /* <int> .getccfont <font-object> */
  312. private int
  313. zgetccfont(i_ctx_t *i_ctx_p)
  314. {
  315. os_ptr op = osp;
  316. int code;
  317. const ccfont_fproc *fprocs;
  318. int nfonts;
  319. int index;
  320. code = ccfont_fprocs(&nfonts, &fprocs);
  321. if (code != ccfont_version)
  322. return_error(e_invalidfont);
  323. if (r_has_type(op, t_null)) {
  324. make_int(op, nfonts);
  325. return 0;
  326. }
  327. check_type(*op, t_integer);
  328. index = op->value.intval;
  329. if (index < 0 || index >= nfonts)
  330. return_error(e_rangecheck);
  331. return (*fprocs[index]) (i_ctx_p, &ccfont_procs, op);
  332. }
  333. /* Operator table initialization */
  334. const op_def ccfonts_op_defs[] =
  335. {
  336. {"0.getccfont", zgetccfont},
  337. op_def_end(0)
  338. };