zfcid1.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (C) 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: zfcid1.c,v 1.4 2000/11/22 08:32:07 lpd Exp $ */
  16. /* CIDFontType 1 and 2 operators */
  17. #include "memory_.h"
  18. #include "ghost.h"
  19. #include "oper.h"
  20. #include "gsmatrix.h"
  21. #include "gsccode.h"
  22. #include "gsstruct.h"
  23. #include "gxfcid.h"
  24. #include "bfont.h"
  25. #include "icid.h"
  26. #include "idict.h"
  27. #include "idparam.h"
  28. #include "ifcid.h"
  29. #include "ifont42.h"
  30. #include "store.h"
  31. /* ---------------- CIDFontType 1 (FontType 10) ---------------- */
  32. /* <string|name> <font_dict> .buildfont10 <string|name> <font> */
  33. private int
  34. zbuildfont10(i_ctx_t *i_ctx_p)
  35. {
  36. os_ptr op = osp;
  37. build_proc_refs build;
  38. int code = build_gs_font_procs(op, &build);
  39. gs_cid_system_info_t cidsi;
  40. gs_font_base *pfont;
  41. if (code < 0)
  42. return code;
  43. code = cid_font_system_info_param(&cidsi, op);
  44. if (code < 0)
  45. return code;
  46. make_null(&build.BuildChar); /* only BuildGlyph */
  47. code = build_gs_simple_font(i_ctx_p, op, &pfont, ft_CID_user_defined,
  48. &st_gs_font_cid1, &build,
  49. bf_Encoding_optional |
  50. bf_FontBBox_required |
  51. bf_UniqueID_ignored);
  52. if (code < 0)
  53. return code;
  54. ((gs_font_cid1 *)pfont)->cidata.CIDSystemInfo = cidsi;
  55. return define_gs_font((gs_font *)pfont);
  56. }
  57. /* ---------------- CIDFontType 2 (FontType 11) ---------------- */
  58. /* ------ Accessing ------ */
  59. /* Map a glyph CID to a TrueType glyph number using the CIDMap. */
  60. private int
  61. z11_CIDMap_proc(gs_font_cid2 *pfont, gs_glyph glyph)
  62. {
  63. const ref *pcidmap = &pfont_data(pfont)->u.type42.CIDMap;
  64. ulong cid = glyph - gs_min_cid_glyph;
  65. int gdbytes = pfont->cidata.common.GDBytes;
  66. int gnum = 0;
  67. const byte *data;
  68. int i, code;
  69. ref rcid;
  70. ref *prgnum;
  71. switch (r_type(pcidmap)) {
  72. case t_string:
  73. if (cid >= r_size(pcidmap) / gdbytes)
  74. return_error(e_rangecheck);
  75. data = pcidmap->value.const_bytes + cid * gdbytes;
  76. break;
  77. case t_integer:
  78. return cid + pcidmap->value.intval;
  79. case t_dictionary:
  80. make_int(&rcid, cid);
  81. code = dict_find(pcidmap, &rcid, &prgnum);
  82. if (code <= 0)
  83. return (code < 0 ? code : gs_note_error(e_undefined));
  84. if (!r_has_type(prgnum, t_integer))
  85. return_error(e_typecheck);
  86. return prgnum->value.intval;
  87. default: /* array type */
  88. code = string_array_access_proc(pcidmap, 1, cid * gdbytes,
  89. gdbytes, &data);
  90. if (code < 0)
  91. return code;
  92. }
  93. for (i = 0; i < gdbytes; ++i)
  94. gnum = (gnum << 8) + data[i];
  95. return gnum;
  96. }
  97. /* Handle MetricsCount when accessing outline or metrics information. */
  98. private int
  99. z11_get_outline(gs_font_type42 * pfont, uint glyph_index,
  100. gs_const_string * pgstr)
  101. {
  102. gs_font_cid2 *const pfcid = (gs_font_cid2 *)pfont;
  103. int skip = pfcid->cidata.MetricsCount << 1;
  104. int code = pfcid->cidata.orig_procs.get_outline(pfont, glyph_index, pgstr);
  105. if (code >= 0) {
  106. byte *data = (byte *)pgstr->data; /* break const */
  107. uint size = pgstr->size;
  108. if (size <= skip) {
  109. if (code > 0 && size != 0)
  110. gs_free_string(pfont->memory, data, size, "z11_get_outline");
  111. pgstr->data = 0, pgstr->size = 0;
  112. } else {
  113. if (code > 0) {
  114. /* Newly allocated, freeble string. */
  115. memmove(data, data + skip, size - skip);
  116. pgstr->data = gs_resize_string(pfont->memory, data,
  117. size, size - skip,
  118. "z11_get_outline");
  119. } else {
  120. pgstr->data += skip;
  121. }
  122. pgstr->size = size - skip;
  123. }
  124. }
  125. return code;
  126. }
  127. private int
  128. z11_get_metrics(gs_font_type42 * pfont, uint glyph_index, int wmode,
  129. float sbw[4])
  130. {
  131. gs_font_cid2 *const pfcid = (gs_font_cid2 *)pfont;
  132. int skip = pfcid->cidata.MetricsCount << 1;
  133. gs_const_string gstr;
  134. const byte *pmetrics;
  135. int lsb, width;
  136. int code = 0;
  137. if (wmode > skip >> 2 ||
  138. (code = pfcid->cidata.orig_procs.get_outline(pfont, glyph_index, &gstr)) < 0 ||
  139. gstr.size < skip
  140. )
  141. return pfcid->cidata.orig_procs.get_metrics(pfont, glyph_index, wmode,
  142. sbw);
  143. pmetrics = gstr.data + skip - (wmode << 2);
  144. lsb = (pmetrics[2] << 8) + pmetrics[3];
  145. width = (pmetrics[0] << 8) + pmetrics[1];
  146. {
  147. double factor = 1.0 / pfont->data.unitsPerEm;
  148. if (wmode) {
  149. sbw[0] = 0, sbw[1] = -lsb * factor;
  150. sbw[2] = 0, sbw[3] = -width * factor;
  151. } else {
  152. sbw[0] = lsb * factor, sbw[1] = 0;
  153. sbw[2] = width * factor, sbw[3] = 0;
  154. }
  155. }
  156. if (code > 0)
  157. gs_free_const_string(pfont->memory, gstr.data, gstr.size,
  158. "z11_get_metrics");
  159. return 0;
  160. }
  161. /* ------ Defining ------ */
  162. /* <string|name> <font_dict> .buildfont11 <string|name> <font> */
  163. private int
  164. zbuildfont11(i_ctx_t *i_ctx_p)
  165. {
  166. os_ptr op = osp;
  167. gs_font_cid_data common;
  168. gs_font_type42 *pfont;
  169. gs_font_cid2 *pfcid;
  170. int MetricsCount;
  171. ref rcidmap, ignore_gdir;
  172. int code = cid_font_data_param(op, &common, &ignore_gdir);
  173. if (code < 0 ||
  174. (code = dict_int_param(op, "MetricsCount", 0, 4, 0, &MetricsCount)) < 0
  175. )
  176. return code;
  177. if (MetricsCount & 1) /* only allowable values are 0, 2, 4 */
  178. return_error(e_rangecheck);
  179. code = font_string_array_param(op, "CIDMap", &rcidmap);
  180. switch (code) {
  181. case 0: /* in PLRM3 */
  182. gdb:
  183. /* GDBytes is required for indexing a string or string array. */
  184. if (common.GDBytes == 0)
  185. return_error(e_rangecheck);
  186. break;
  187. default:
  188. return code;
  189. case e_typecheck:
  190. switch (r_type(&rcidmap)) {
  191. case t_string: /* in PLRM3 */
  192. goto gdb;
  193. case t_dictionary: /* added in 3011 */
  194. case t_integer: /* added in 3011 */
  195. break;
  196. default:
  197. return code;
  198. }
  199. break;
  200. }
  201. code = build_gs_TrueType_font(i_ctx_p, op, &pfont, ft_CID_TrueType,
  202. &st_gs_font_cid2,
  203. (const char *)0, "%Type11BuildGlyph",
  204. bf_Encoding_optional |
  205. bf_FontBBox_required |
  206. bf_UniqueID_ignored |
  207. bf_CharStrings_optional);
  208. if (code < 0)
  209. return code;
  210. pfcid = (gs_font_cid2 *)pfont;
  211. pfcid->cidata.common = common;
  212. pfcid->cidata.MetricsCount = MetricsCount;
  213. ref_assign(&pfont_data(pfont)->u.type42.CIDMap, &rcidmap);
  214. pfcid->cidata.CIDMap_proc = z11_CIDMap_proc;
  215. if (MetricsCount) {
  216. /* "Wrap" the glyph accessor procedures. */
  217. pfcid->cidata.orig_procs.get_outline = pfont->data.get_outline;
  218. pfont->data.get_outline = z11_get_outline;
  219. pfcid->cidata.orig_procs.get_metrics = pfont->data.get_metrics;
  220. pfont->data.get_metrics = z11_get_metrics;
  221. }
  222. return define_gs_font((gs_font *)pfont);
  223. }
  224. /* <cid11font> <cid> .type11mapcid <glyph_index> */
  225. private int
  226. ztype11mapcid(i_ctx_t *i_ctx_p)
  227. {
  228. os_ptr op = osp;
  229. gs_font *pfont;
  230. int code = font_param(op - 1, &pfont);
  231. if (code < 0)
  232. return code;
  233. if (pfont->FontType != ft_CID_TrueType)
  234. return_error(e_invalidfont);
  235. check_type(*op, t_integer);
  236. code = z11_CIDMap_proc((gs_font_cid2 *)pfont,
  237. (gs_glyph)(gs_min_cid_glyph + op->value.intval));
  238. if (code < 0)
  239. return code;
  240. make_int(op - 1, code);
  241. pop(1);
  242. return 0;
  243. }
  244. /* ------ Initialization procedure ------ */
  245. const op_def zfcid1_op_defs[] =
  246. {
  247. {"2.buildfont10", zbuildfont10},
  248. {"2.buildfont11", zbuildfont11},
  249. {"2.type11mapcid", ztype11mapcid},
  250. op_def_end(0)
  251. };