zcharout.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Copyright (C) 1996, 1997, 1999, 2000 Aladdin Enterprises. 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: zcharout.c,v 1.13 2004/09/22 13:52:33 igor Exp $ */
  14. /* Common code for outline (Type 1 / 4 / 42) fonts */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "gscrypt1.h"
  19. #include "gstext.h"
  20. #include "gxdevice.h" /* for gxfont.h */
  21. #include "gxfont.h"
  22. #include "gxfont1.h"
  23. #include "dstack.h" /* only for systemdict */
  24. #include "estack.h"
  25. #include "ichar.h"
  26. #include "icharout.h"
  27. #include "idict.h"
  28. #include "ifont.h"
  29. #include "igstate.h"
  30. #include "iname.h"
  31. #include "store.h"
  32. /*
  33. * Execute an outline defined by a PostScript procedure.
  34. * The top elements of the stack are:
  35. * <font> <code|name> <name> <outline_id>
  36. */
  37. int
  38. zchar_exec_char_proc(i_ctx_t *i_ctx_p)
  39. {
  40. os_ptr op = osp;
  41. /*
  42. * The definition is a PostScript procedure. Execute
  43. * <code|name> proc
  44. * within a systemdict begin/end and a font begin/end.
  45. */
  46. es_ptr ep;
  47. check_estack(5);
  48. ep = esp += 5;
  49. make_op_estack(ep - 4, zend);
  50. make_op_estack(ep - 3, zend);
  51. ref_assign(ep - 2, op);
  52. make_op_estack(ep - 1, zbegin);
  53. make_op_estack(ep, zbegin);
  54. ref_assign(op - 1, systemdict);
  55. {
  56. ref rfont;
  57. ref_assign(&rfont, op - 3);
  58. ref_assign(op - 3, op - 2);
  59. ref_assign(op - 2, &rfont);
  60. }
  61. pop(1);
  62. return o_push_estack;
  63. }
  64. /*
  65. * Get the metrics for a character from the Metrics dictionary of a base
  66. * font. If present, store the l.s.b. in psbw[0,1] and the width in
  67. * psbw[2,3].
  68. */
  69. int /*metrics_present*/
  70. zchar_get_metrics(const gs_font_base * pbfont, const ref * pcnref,
  71. double psbw[4])
  72. {
  73. const ref *pfdict = &pfont_data(gs_font_parent(pbfont))->dict;
  74. ref *pmdict;
  75. if (dict_find_string(pfdict, "Metrics", &pmdict) > 0) {
  76. ref *pmvalue;
  77. check_type_only(*pmdict, t_dictionary);
  78. check_dict_read(*pmdict);
  79. if (dict_find(pmdict, pcnref, &pmvalue) > 0) {
  80. if (num_params(pmvalue, 1, psbw + 2) >= 0) { /* <wx> only */
  81. psbw[3] = 0;
  82. return metricsWidthOnly;
  83. } else {
  84. int code;
  85. check_read_type_only(*pmvalue, t_array);
  86. switch (r_size(pmvalue)) {
  87. case 2: /* [<sbx> <wx>] */
  88. code = num_params(pmvalue->value.refs + 1,
  89. 2, psbw);
  90. psbw[2] = psbw[1];
  91. psbw[1] = psbw[3] = 0;
  92. break;
  93. case 4: /* [<sbx> <sby> <wx> <wy>] */
  94. code = num_params(pmvalue->value.refs + 3,
  95. 4, psbw);
  96. break;
  97. default:
  98. return_error(e_rangecheck);
  99. }
  100. if (code < 0)
  101. return code;
  102. return metricsSideBearingAndWidth;
  103. }
  104. }
  105. }
  106. return metricsNone;
  107. }
  108. /* Get the vertical metrics for a character from Metrics2, if present. */
  109. int
  110. zchar_get_metrics2(const gs_font_base * pbfont, const ref * pcnref,
  111. double pwv[4])
  112. {
  113. const ref *pfdict = &pfont_data(gs_font_parent(pbfont))->dict;
  114. ref *pmdict;
  115. if (dict_find_string(pfdict, "Metrics2", &pmdict) > 0) {
  116. ref *pmvalue;
  117. check_type_only(*pmdict, t_dictionary);
  118. check_dict_read(*pmdict);
  119. if (dict_find(pmdict, pcnref, &pmvalue) > 0) {
  120. check_read_type_only(*pmvalue, t_array);
  121. if (r_size(pmvalue) == 4) {
  122. int code = num_params(pmvalue->value.refs + 3, 4, pwv);
  123. return (code < 0 ? code : metricsSideBearingAndWidth);
  124. }
  125. }
  126. }
  127. return metricsNone;
  128. }
  129. /*
  130. * Get CDevProc.
  131. */
  132. bool
  133. zchar_get_CDevProc(const gs_font_base * pbfont, ref **ppcdevproc)
  134. {
  135. const ref *pfdict = &pfont_data(gs_font_parent(pbfont))->dict;
  136. return dict_find_string(pfdict, "CDevProc", ppcdevproc) > 0;
  137. }
  138. /*
  139. * Consult Metrics2 and CDevProc, and call setcachedevice[2]. Return
  140. * o_push_estack if we had to call a CDevProc, or if we are skipping the
  141. * rendering process (only getting the metrics).
  142. * Returns exec_cont - a function, which must be called by caller after this function.
  143. */
  144. int
  145. zchar_set_cache(i_ctx_t *i_ctx_p, const gs_font_base * pbfont,
  146. const ref * pcnref, const double psb[2],
  147. const double pwidth[2], const gs_rect * pbbox,
  148. op_proc_t cont, op_proc_t *exec_cont,
  149. const double Metrics2_sbw_default[4])
  150. {
  151. os_ptr op = osp;
  152. ref *pcdevproc;
  153. int have_cdevproc;
  154. ref rpop;
  155. bool metrics2;
  156. bool metrics2_use_default = false;
  157. double w2[10];
  158. gs_text_enum_t *penum = op_show_find(i_ctx_p);
  159. w2[0] = pwidth[0], w2[1] = pwidth[1];
  160. /* Adjust the bounding box for stroking if needed. */
  161. w2[2] = pbbox->p.x, w2[3] = pbbox->p.y;
  162. w2[4] = pbbox->q.x, w2[5] = pbbox->q.y;
  163. if (pbfont->PaintType != 0) {
  164. double expand = max(1.415, gs_currentmiterlimit(igs)) *
  165. gs_currentlinewidth(igs) / 2;
  166. w2[2] -= expand, w2[3] -= expand;
  167. w2[4] += expand, w2[5] += expand;
  168. }
  169. /* Check for Metrics2. */
  170. {
  171. int code = zchar_get_metrics2(pbfont, pcnref, w2 + 6);
  172. if (code < 0)
  173. return code;
  174. metrics2 = code > 0;
  175. }
  176. /*
  177. * For FontType 9 and 11, if Metrics2 is missing, the caller provides
  178. * default Metrics2 values derived from the FontBBox.
  179. */
  180. if (!metrics2 && Metrics2_sbw_default != NULL) {
  181. w2[6] = Metrics2_sbw_default[2];
  182. w2[7] = Metrics2_sbw_default[3];
  183. w2[8] = Metrics2_sbw_default[0];
  184. w2[9] = Metrics2_sbw_default[1];
  185. metrics2 = true;
  186. metrics2_use_default = true;
  187. }
  188. /* Check for CDevProc or "short-circuiting". */
  189. have_cdevproc = zchar_get_CDevProc(pbfont, &pcdevproc);
  190. if (have_cdevproc || zchar_show_width_only(penum)) {
  191. int i;
  192. op_proc_t zsetc;
  193. int nparams;
  194. if (have_cdevproc) {
  195. check_proc_only(*pcdevproc);
  196. zsetc = zsetcachedevice2;
  197. /* If we have cdevproc and the font type is CID type 0,
  198. we'll throw away Metrics2_sbw_default that is calculated
  199. from FontBBox. */
  200. if (!metrics2
  201. || (penum->current_font->FontType == ft_CID_encrypted
  202. && metrics2_use_default)) {
  203. w2[6] = w2[0], w2[7] = w2[1];
  204. w2[8] = w2[9] = 0;
  205. }
  206. nparams = 10;
  207. } else {
  208. make_oper(&rpop, 0, zpop);
  209. pcdevproc = &rpop;
  210. if (metrics2)
  211. zsetc = zsetcachedevice2, nparams = 10;
  212. else
  213. zsetc = zsetcachedevice, nparams = 6;
  214. }
  215. check_estack(3);
  216. /* Push the l.s.b. for .type1addpath if necessary. */
  217. if (psb != 0) {
  218. push(nparams + 3);
  219. make_real(op - (nparams + 2), psb[0]);
  220. make_real(op - (nparams + 1), psb[1]);
  221. } else {
  222. push(nparams + 1);
  223. }
  224. for (i = 0; i < nparams; ++i)
  225. make_real(op - nparams + i, w2[i]);
  226. ref_assign(op, pcnref);
  227. push_op_estack(cont);
  228. push_op_estack(zsetc);
  229. ++esp;
  230. ref_assign(esp, pcdevproc);
  231. return o_push_estack;
  232. } {
  233. int code =
  234. (metrics2 ? gs_text_setcachedevice2(penum, w2) :
  235. gs_text_setcachedevice(penum, w2));
  236. if (code < 0)
  237. return code;
  238. }
  239. /* No metrics modification, do the stroke or fill now. */
  240. /* Push the l.s.b. for .type1addpath if necessary. */
  241. if (psb != 0) {
  242. push(2);
  243. make_real(op - 1, psb[0]);
  244. make_real(op, psb[1]);
  245. }
  246. *exec_cont = cont;
  247. return 0;
  248. }
  249. /*
  250. * Get the CharString data corresponding to a glyph. Return typecheck
  251. * if it isn't a string.
  252. */
  253. private bool charstring_is_notdef_proc(const gs_memory_t *mem, const ref *);
  254. private int charstring_make_notdef(gs_glyph_data_t *, gs_font *);
  255. int
  256. zchar_charstring_data(gs_font *font, const ref *pgref, gs_glyph_data_t *pgd)
  257. {
  258. ref *pcstr;
  259. if (dict_find(&pfont_data(font)->CharStrings, pgref, &pcstr) <= 0)
  260. return_error(e_undefined);
  261. if (!r_has_type(pcstr, t_string)) {
  262. /*
  263. * The ADOBEPS4 Windows driver replaces the .notdef entry of
  264. * otherwise normal Type 1 fonts with the procedure
  265. * {pop 0 0 setcharwidth}
  266. * To prevent this from making the font unembeddable in PDF files
  267. * (with our present font-writing code), we recognize this as a
  268. * special case and return a Type 1 CharString consisting of
  269. * 0 0 hsbw endchar
  270. */
  271. if (font->FontType == ft_encrypted &&
  272. charstring_is_notdef_proc(font->memory, pcstr)
  273. )
  274. return charstring_make_notdef(pgd, font);
  275. else
  276. return_error(e_typecheck);
  277. }
  278. gs_glyph_data_from_string(pgd, pcstr->value.const_bytes, r_size(pcstr),
  279. NULL);
  280. return 0;
  281. }
  282. private bool
  283. charstring_is_notdef_proc(const gs_memory_t *mem, const ref *pcstr)
  284. {
  285. if (r_is_array(pcstr) && r_size(pcstr) == 4) {
  286. ref elts[4];
  287. long i;
  288. for (i = 0; i < 4; ++i)
  289. array_get(mem, pcstr, i, &elts[i]);
  290. if (r_has_type(&elts[0], t_name) &&
  291. r_has_type(&elts[1], t_integer) && elts[1].value.intval == 0 &&
  292. r_has_type(&elts[2], t_integer) && elts[2].value.intval == 0 &&
  293. r_has_type(&elts[3], t_name)
  294. ) {
  295. ref nref;
  296. name_enter_string(mem, "pop", &nref);
  297. if (name_eq(&elts[0], &nref)) {
  298. name_enter_string(mem, "setcharwidth", &nref);
  299. if (name_eq(&elts[3], &nref))
  300. return true;
  301. }
  302. }
  303. }
  304. return false;
  305. }
  306. private int
  307. charstring_make_notdef(gs_glyph_data_t *pgd, gs_font *font)
  308. {
  309. gs_font_type1 *const pfont = (gs_font_type1 *)font;
  310. static const byte char_data[4] = {
  311. 139, /* 0 */
  312. 139, /* 0 */
  313. c1_hsbw,
  314. cx_endchar
  315. };
  316. uint len = max(pfont->data.lenIV, 0) + sizeof(char_data);
  317. byte *chars = gs_alloc_string(font->memory, len, "charstring_make_notdef");
  318. if (chars == 0)
  319. return_error(e_VMerror);
  320. gs_glyph_data_from_string(pgd, chars, len, font);
  321. if (pfont->data.lenIV < 0)
  322. memcpy(chars, char_data, sizeof(char_data));
  323. else {
  324. crypt_state state = crypt_charstring_seed;
  325. memcpy(chars + pfont->data.lenIV, char_data, sizeof(char_data));
  326. gs_type1_encrypt(chars, chars, len, &state);
  327. }
  328. return 0;
  329. }
  330. /*
  331. * Enumerate the next glyph from a directory. This is essentially a
  332. * wrapper around dict_first/dict_next to implement the enumerate_glyph
  333. * font procedure.
  334. *
  335. * Note that *prdict will be null if the font is a subfont of a
  336. * CIDFontType 0 CIDFont.
  337. */
  338. int
  339. zchar_enumerate_glyph(const gs_memory_t *mem, const ref *prdict, int *pindex, gs_glyph *pglyph)
  340. {
  341. int index = *pindex - 1;
  342. ref elt[2];
  343. if (!r_has_type(prdict, t_dictionary))
  344. return 0; /* *pindex was 0, is still 0 */
  345. if (index < 0)
  346. index = dict_first(prdict);
  347. next:
  348. index = dict_next(prdict, index, elt);
  349. *pindex = index + 1;
  350. if (index >= 0) {
  351. switch (r_type(elt)) {
  352. case t_integer:
  353. *pglyph = gs_min_cid_glyph + elt[0].value.intval;
  354. break;
  355. case t_name:
  356. *pglyph = name_index(mem, elt);
  357. break;
  358. default: /* can't handle it */
  359. goto next;
  360. }
  361. }
  362. return 0;
  363. }