iccfont.c 9.5 KB

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