zfcid0.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* Copyright (C) 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: zfcid0.c,v 1.25 2004/11/19 04:39:11 ray Exp $ */
  14. /* CIDFontType 0 operators */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "gsmatrix.h"
  19. #include "gsccode.h"
  20. #include "gsstruct.h"
  21. #include "gxfcid.h"
  22. #include "gxfont1.h"
  23. #include "gxalloc.h" /* for gs_ref_memory_t */
  24. #include "stream.h" /* for files.h */
  25. #include "bfont.h"
  26. #include "files.h"
  27. #include "ichar.h"
  28. #include "ichar1.h"
  29. #include "icid.h"
  30. #include "idict.h"
  31. #include "idparam.h"
  32. #include "ifcid.h"
  33. #include "ifont1.h"
  34. #include "ifont2.h"
  35. #include "ifont42.h"
  36. #include "store.h"
  37. /* Type 1 font procedures (defined in zchar1.c) */
  38. font_proc_glyph_outline(zcharstring_glyph_outline);
  39. /* ---------------- CIDFontType 0 (FontType 9) ---------------- */
  40. /* ------ Accessing ------ */
  41. /* Parse a multi-byte integer from a string. */
  42. private int
  43. get_index(gs_glyph_data_t *pgd, int count, ulong *pval)
  44. {
  45. int i;
  46. if (pgd->bits.size < count)
  47. return_error(e_rangecheck);
  48. *pval = 0;
  49. for (i = 0; i < count; ++i)
  50. *pval = (*pval << 8) + pgd->bits.data[i];
  51. pgd->bits.data += count;
  52. pgd->bits.size -= count;
  53. return 0;
  54. }
  55. /* Get bytes from GlyphData or DataSource. */
  56. private int
  57. cid0_read_bytes(gs_font_cid0 *pfont, ulong base, uint count, byte *buf,
  58. gs_glyph_data_t *pgd)
  59. {
  60. const font_data *pfdata = pfont_data(pfont);
  61. byte *data = buf;
  62. gs_font *gdfont = 0; /* pfont if newly allocated, 0 if not */
  63. int code = 0;
  64. /* Check for overflow. */
  65. if (base != (long)base || base > base + count)
  66. return_error(e_rangecheck);
  67. if (r_has_type(&pfdata->u.cid0.DataSource, t_null)) {
  68. /* Get the bytes from GlyphData (a string or array of strings). */
  69. const ref *pgdata = &pfdata->u.cid0.GlyphData;
  70. if (r_has_type(pgdata, t_string)) { /* single string */
  71. uint size = r_size(pgdata);
  72. if (base >= size || count > size - base)
  73. return_error(e_rangecheck);
  74. data = pgdata->value.bytes + base;
  75. } else { /* array of strings */
  76. /*
  77. * The algorithm is similar to the one in
  78. * string_array_access_proc in zfont42.c, but it also has to
  79. * deal with the case where the requested string crosses array
  80. * elements.
  81. */
  82. ulong skip = base;
  83. uint copied = 0;
  84. uint index = 0;
  85. ref rstr;
  86. uint size;
  87. for (;; skip -= size, ++index) {
  88. int code = array_get(pfont->memory, pgdata, index, &rstr);
  89. if (code < 0)
  90. return code;
  91. if (!r_has_type(&rstr, t_string))
  92. return_error(e_typecheck);
  93. size = r_size(&rstr);
  94. if (skip < size)
  95. break;
  96. }
  97. size -= skip;
  98. if (count <= size) {
  99. data = rstr.value.bytes + skip;
  100. } else { /* multiple strings needed */
  101. if (data == 0) { /* no buffer provided */
  102. data = gs_alloc_string(pfont->memory, count,
  103. "cid0_read_bytes");
  104. if (data == 0)
  105. return_error(e_VMerror);
  106. gdfont = (gs_font *)pfont; /* newly allocated */
  107. }
  108. memcpy(data, rstr.value.bytes + skip, size);
  109. copied = size;
  110. while (copied < count) {
  111. int code = array_get(pfont->memory, pgdata, ++index, &rstr);
  112. if (code < 0)
  113. goto err;
  114. if (!r_has_type(&rstr, t_string)) {
  115. code = gs_note_error(e_typecheck);
  116. goto err;
  117. }
  118. size = r_size(&rstr);
  119. if (size > count - copied)
  120. size = count - copied;
  121. memcpy(data + copied, rstr.value.bytes, size);
  122. copied += size;
  123. }
  124. }
  125. }
  126. } else {
  127. /* Get the bytes from DataSource (a stream). */
  128. stream *s;
  129. uint nread;
  130. check_read_known_file(s, &pfdata->u.cid0.DataSource, return_error);
  131. if (sseek(s, base) < 0)
  132. return_error(e_ioerror);
  133. if (data == 0) { /* no buffer provided */
  134. data = gs_alloc_string(pfont->memory, count, "cid0_read_bytes");
  135. if (data == 0)
  136. return_error(e_VMerror);
  137. gdfont = (gs_font *)pfont; /* newly allocated */
  138. }
  139. if (sgets(s, data, count, &nread) < 0 || nread != count) {
  140. code = gs_note_error(e_ioerror);
  141. goto err;
  142. }
  143. }
  144. gs_glyph_data_from_string(pgd, data, count, gdfont);
  145. return code;
  146. err:
  147. if (data != buf)
  148. gs_free_string(pfont->memory, data, count, "cid0_read_bytes");
  149. return code;
  150. }
  151. /* Get the CharString data for a CIDFontType 0 font. */
  152. /* This is the glyph_data procedure in the font itself. */
  153. /* Note that pgd may be NULL. */
  154. private int
  155. z9_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd,
  156. int *pfidx)
  157. {
  158. gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont;
  159. const font_data *pfdata = pfont_data(pfont);
  160. long glyph_index = (long)(glyph - gs_min_cid_glyph);
  161. gs_glyph_data_t gdata;
  162. ulong fidx;
  163. int code;
  164. gdata.memory = pfont->memory;
  165. if (!r_has_type(&pfdata->u.cid0.GlyphDirectory, t_null)) {
  166. code = font_gdir_get_outline(pfont->memory,
  167. &pfdata->u.cid0.GlyphDirectory,
  168. glyph_index, &gdata);
  169. if (code < 0)
  170. return code;
  171. /* Get the definition from GlyphDirectory. */
  172. if (!gdata.bits.data)
  173. return_error(e_rangecheck);
  174. code = get_index(&gdata, pfont->cidata.FDBytes, &fidx);
  175. if (code < 0)
  176. return code;
  177. if (fidx >= pfont->cidata.FDArray_size)
  178. return_error(e_rangecheck);
  179. if (pgd)
  180. *pgd = gdata;
  181. *pfidx = (int)fidx;
  182. return code;
  183. }
  184. /* Get the definition from the binary data (GlyphData or DataSource). */
  185. if (glyph_index < 0 || glyph_index >= pfont->cidata.common.CIDCount) {
  186. *pfidx = 0;
  187. if (pgd)
  188. gs_glyph_data_from_null(pgd);
  189. return_error(e_rangecheck);
  190. }
  191. {
  192. byte fd_gd[(MAX_FDBytes + MAX_GDBytes) * 2];
  193. uint num_bytes = pfont->cidata.FDBytes + pfont->cidata.common.GDBytes;
  194. ulong base = pfont->cidata.CIDMapOffset + glyph_index * num_bytes;
  195. ulong gidx, fidx_next, gidx_next;
  196. int rcode = cid0_read_bytes(pfont, base, (ulong)(num_bytes * 2), fd_gd,
  197. &gdata);
  198. gs_glyph_data_t orig_data;
  199. if (rcode < 0)
  200. return rcode;
  201. orig_data = gdata;
  202. if ((code = get_index(&gdata, pfont->cidata.FDBytes, &fidx)) < 0 ||
  203. (code = get_index(&gdata, pfont->cidata.common.GDBytes, &gidx)) < 0 ||
  204. (code = get_index(&gdata, pfont->cidata.FDBytes, &fidx_next)) < 0 ||
  205. (code = get_index(&gdata, pfont->cidata.common.GDBytes, &gidx_next)) < 0
  206. )
  207. DO_NOTHING;
  208. gs_glyph_data_free(&orig_data, "z9_glyph_data");
  209. if (code < 0)
  210. return code;
  211. /*
  212. * Some CID fonts (from Adobe!) have invalid font indexes for
  213. * missing glyphs. Handle this now.
  214. */
  215. if (gidx_next <= gidx) { /* missing glyph */
  216. *pfidx = 0;
  217. if (pgd)
  218. gs_glyph_data_from_null(pgd);
  219. return_error(e_undefined);
  220. }
  221. if (fidx >= pfont->cidata.FDArray_size)
  222. return_error(e_rangecheck);
  223. *pfidx = (int)fidx;
  224. if (pgd == 0)
  225. return 0;
  226. return cid0_read_bytes(pfont, gidx, gidx_next - gidx, NULL, pgd);
  227. }
  228. }
  229. /* Get the outline of a CIDFontType 0 glyph. */
  230. private int
  231. z9_glyph_outline(gs_font *font, int WMode, gs_glyph glyph, const gs_matrix *pmat,
  232. gx_path *ppath, double sbw[4])
  233. {
  234. gs_font_cid0 *const pfcid = (gs_font_cid0 *)font;
  235. ref gref;
  236. gs_glyph_data_t gdata;
  237. int code, fidx, ocode;
  238. gdata.memory = font->memory;
  239. code = pfcid->cidata.glyph_data((gs_font_base *)pfcid, glyph, &gdata,
  240. &fidx);
  241. if (code < 0)
  242. return code;
  243. glyph_ref(font->memory, glyph, &gref);
  244. ocode = zcharstring_outline(pfcid->cidata.FDArray[fidx], WMode, &gref, &gdata,
  245. pmat, ppath, sbw);
  246. gs_glyph_data_free(&gdata, "z9_glyph_outline");
  247. return ocode;
  248. }
  249. private int
  250. z9_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  251. int members, gs_glyph_info_t *info)
  252. { /* fixme : same as z11_glyph_info. */
  253. int wmode = (members & GLYPH_INFO_WIDTH0 ? 0 : 1);
  254. return z1_glyph_info_generic(font, glyph, pmat, members, info,
  255. &gs_default_glyph_info, wmode);
  256. }
  257. /*
  258. * The "fonts" in the FDArray don't have access to their outlines -- the
  259. * outlines are always provided externally. Replace the accessor procedures
  260. * with ones that will give an error if called.
  261. */
  262. private int
  263. z9_FDArray_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
  264. gs_glyph_data_t *pgd)
  265. {
  266. return_error(e_invalidfont);
  267. }
  268. private int
  269. z9_FDArray_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph,
  270. gs_const_string *gstr, gs_glyph_data_t *pgd)
  271. {
  272. return_error(e_invalidfont);
  273. }
  274. /* ------ Defining ------ */
  275. /* Get one element of a FDArray. */
  276. private int
  277. fd_array_element(i_ctx_t *i_ctx_p, gs_font_type1 **ppfont, ref *prfd)
  278. {
  279. charstring_font_refs_t refs;
  280. gs_type1_data data1;
  281. build_proc_refs build;
  282. gs_font_base *pbfont;
  283. gs_font_type1 *pfont;
  284. /*
  285. * Standard CIDFontType 0 fonts have Type 1 fonts in the FDArray, but
  286. * CFF CIDFontType 0 fonts have Type 2 fonts there.
  287. */
  288. int fonttype = 1; /* default */
  289. int code = charstring_font_get_refs(prfd, &refs);
  290. if (code < 0 ||
  291. (code = dict_int_param(prfd, "FontType", 1, 2, 1, &fonttype)) < 0
  292. )
  293. return code;
  294. /*
  295. * We don't handle the alternate Subr representation (SubrCount,
  296. * SDBytes, SubrMapOffset) here: currently that is handled in
  297. * PostScript code (lib/gs_cidfn.ps).
  298. */
  299. switch (fonttype) {
  300. case 1:
  301. data1.interpret = gs_type1_interpret;
  302. data1.subroutineNumberBias = 0;
  303. data1.lenIV = DEFAULT_LENIV_1;
  304. code = charstring_font_params(imemory, prfd, &refs, &data1);
  305. if (code < 0)
  306. return code;
  307. code = build_proc_name_refs(imemory, &build,
  308. "%Type1BuildChar", "%Type1BuildGlyph");
  309. break;
  310. case 2:
  311. code = type2_font_params(prfd, &refs, &data1);
  312. if (code < 0)
  313. return code;
  314. code = charstring_font_params(imemory, prfd, &refs, &data1);
  315. if (code < 0)
  316. return code;
  317. code = build_proc_name_refs(imemory, &build,
  318. "%Type2BuildChar", "%Type2BuildGlyph");
  319. break;
  320. default: /* can't happen */
  321. return_error(e_Fatal);
  322. }
  323. if (code < 0)
  324. return code;
  325. code = build_gs_FDArray_font(i_ctx_p, prfd, &pbfont, fonttype,
  326. &st_gs_font_type1, &build);
  327. if (code < 0)
  328. return code;
  329. pfont = (gs_font_type1 *)pbfont;
  330. pbfont->FAPI = NULL;
  331. pbfont->FAPI_font_data = NULL;
  332. charstring_font_init(pfont, &refs, &data1);
  333. pfont->data.procs.glyph_data = z9_FDArray_glyph_data;
  334. pfont->data.procs.seac_data = z9_FDArray_seac_data;
  335. *ppfont = pfont;
  336. return 0;
  337. }
  338. private int
  339. notify_remove_font_type9(void *proc_data, void *event_data)
  340. { /* Likely type 9 font descendents are never released explicitly.
  341. So releaseing a type 9 font we must reset pointers in descendents.
  342. */
  343. /* gs_font_finalize passes event_data == NULL, so check it here. */
  344. if (event_data == NULL) {
  345. gs_font_cid0 *pfcid = proc_data;
  346. int i;
  347. for (i = 0; i < pfcid->cidata.FDArray_size; ++i) {
  348. if (pfcid->cidata.FDArray[i]->data.parent == (gs_font_base *)pfcid)
  349. pfcid->cidata.FDArray[i]->data.parent = NULL;
  350. }
  351. }
  352. return 0;
  353. }
  354. /* <string|name> <font_dict> .buildfont9 <string|name> <font> */
  355. private int
  356. zbuildfont9(i_ctx_t *i_ctx_p)
  357. {
  358. os_ptr op = osp;
  359. build_proc_refs build;
  360. int code = build_proc_name_refs(imemory, &build, NULL, "%Type9BuildGlyph");
  361. gs_font_cid_data common;
  362. ref GlyphDirectory, GlyphData, DataSource;
  363. ref *prfda, cfnstr;
  364. ref *pCIDFontName, CIDFontName;
  365. gs_font_type1 **FDArray;
  366. uint FDArray_size;
  367. int FDBytes;
  368. uint CIDMapOffset;
  369. gs_font_base *pfont;
  370. gs_font_cid0 *pfcid;
  371. uint i;
  372. /*
  373. * If the CIDFont's data have been loaded into VM, GlyphData will be
  374. * a string or an array of strings; if they are loaded incrementally
  375. * from a file, GlyphData will be an integer, and DataSource will be
  376. * a (reusable) stream.
  377. */
  378. if (code < 0 ||
  379. (code = cid_font_data_param(op, &common, &GlyphDirectory)) < 0 ||
  380. (code = dict_find_string(op, "FDArray", &prfda)) < 0 ||
  381. (code = dict_find_string(op, "CIDFontName", &pCIDFontName)) <= 0 ||
  382. (code = dict_int_param(op, "FDBytes", 0, MAX_FDBytes, -1, &FDBytes)) < 0
  383. )
  384. return code;
  385. /*
  386. * Since build_gs_simple_font may resize the dictionary and cause
  387. * pointers to become invalid, save CIDFontName
  388. */
  389. CIDFontName = *pCIDFontName;
  390. if (r_has_type(&GlyphDirectory, t_null)) {
  391. /* Standard CIDFont, require GlyphData and CIDMapOffset. */
  392. ref *pGlyphData;
  393. if ((code = dict_find_string(op, "GlyphData", &pGlyphData)) < 0 ||
  394. (code = dict_uint_param(op, "CIDMapOffset", 0, max_uint - 1,
  395. max_uint, &CIDMapOffset)) < 0)
  396. return code;
  397. GlyphData = *pGlyphData;
  398. if (r_has_type(&GlyphData, t_integer)) {
  399. ref *pds;
  400. stream *ignore_s;
  401. if ((code = dict_find_string(op, "DataSource", &pds)) < 0)
  402. return code;
  403. check_read_file(ignore_s, pds);
  404. DataSource = *pds;
  405. } else {
  406. if (!r_has_type(&GlyphData, t_string) && !r_is_array(&GlyphData))
  407. return_error(e_typecheck);
  408. make_null(&DataSource);
  409. }
  410. } else {
  411. make_null(&GlyphData);
  412. make_null(&DataSource);
  413. CIDMapOffset = 0;
  414. }
  415. if (!r_is_array(prfda))
  416. return_error(e_invalidfont);
  417. FDArray_size = r_size(prfda);
  418. if (FDArray_size == 0)
  419. return_error(e_invalidfont);
  420. FDArray = ialloc_struct_array(FDArray_size, gs_font_type1 *,
  421. &st_gs_font_type1_ptr_element,
  422. "buildfont9(FDarray)");
  423. if (FDArray == 0)
  424. return_error(e_VMerror);
  425. memset(FDArray, 0, sizeof(gs_font_type1 *) * FDArray_size);
  426. for (i = 0; i < FDArray_size; ++i) {
  427. ref rfd;
  428. array_get(imemory, prfda, (long)i, &rfd);
  429. code = fd_array_element(i_ctx_p, &FDArray[i], &rfd);
  430. if (code < 0)
  431. goto fail;
  432. }
  433. code = build_gs_simple_font(i_ctx_p, op, &pfont, ft_CID_encrypted,
  434. &st_gs_font_cid0, &build,
  435. bf_Encoding_optional |
  436. bf_UniqueID_ignored);
  437. if (code < 0)
  438. goto fail;
  439. pfont->procs.enumerate_glyph = gs_font_cid0_enumerate_glyph;
  440. pfont->procs.glyph_outline = z9_glyph_outline;
  441. pfont->procs.glyph_info = z9_glyph_info;
  442. pfcid = (gs_font_cid0 *)pfont;
  443. pfcid->cidata.common = common;
  444. pfcid->cidata.CIDMapOffset = CIDMapOffset;
  445. pfcid->cidata.FDArray = FDArray;
  446. pfcid->cidata.FDArray_size = FDArray_size;
  447. pfcid->cidata.FDBytes = FDBytes;
  448. pfcid->cidata.glyph_data = z9_glyph_data;
  449. pfcid->cidata.proc_data = 0; /* for GC */
  450. get_font_name(imemory, &cfnstr, &CIDFontName);
  451. copy_font_name(&pfcid->font_name, &cfnstr);
  452. ref_assign(&pfont_data(pfont)->u.cid0.GlyphDirectory, &GlyphDirectory);
  453. ref_assign(&pfont_data(pfont)->u.cid0.GlyphData, &GlyphData);
  454. ref_assign(&pfont_data(pfont)->u.cid0.DataSource, &DataSource);
  455. code = define_gs_font((gs_font *)pfont);
  456. if (code >= 0)
  457. code = gs_notify_register(&pfont->notify_list, notify_remove_font_type9, pfont);
  458. if (code >= 0) {
  459. for (i = 0; i < FDArray_size; ++i) {
  460. FDArray[i]->dir = pfont->dir;
  461. FDArray[i]->data.parent = pfont;
  462. }
  463. return code;
  464. }
  465. fail:
  466. ifree_object(FDArray, "buildfont9(FDarray)");
  467. return code;
  468. }
  469. /* <cid9font> <cid> .type9mapcid <charstring> <font_index> */
  470. int
  471. ztype9mapcid(i_ctx_t *i_ctx_p)
  472. {
  473. os_ptr op = osp;
  474. gs_font *pfont;
  475. gs_font_cid0 *pfcid;
  476. int code = font_param(op - 1, &pfont);
  477. gs_glyph_data_t gdata;
  478. int fidx;
  479. if (code < 0)
  480. return code;
  481. if (pfont->FontType != ft_CID_encrypted)
  482. return_error(e_invalidfont);
  483. check_type(*op, t_integer);
  484. pfcid = (gs_font_cid0 *)pfont;
  485. gdata.memory = pfont->memory;
  486. code = pfcid->cidata.glyph_data((gs_font_base *)pfcid,
  487. (gs_glyph)(gs_min_cid_glyph + op->value.intval),
  488. &gdata, &fidx);
  489. /* return code; original error-sensitive & fragile code */
  490. if (code < 0) { /* failed to load glyph data, put CID 0 */
  491. int default_fallback_CID = 0 ;
  492. if_debug2('J', "[J]ztype9cidmap() use CID %d instead of glyph-missing CID %d\n", default_fallback_CID, op->value.intval);
  493. op->value.intval = default_fallback_CID;
  494. /* reload glyph for default_fallback_CID */
  495. code = pfcid->cidata.glyph_data((gs_font_base *)pfcid,
  496. (gs_glyph)(gs_min_cid_glyph + default_fallback_CID),
  497. &gdata, &fidx);
  498. if (code < 0) {
  499. if_debug1('J', "[J]ztype9cidmap() could not load default glyph (CID %d)\n", op->value.intval);
  500. return_error(e_invalidfont);
  501. }
  502. }
  503. /****** FOLLOWING IS NOT GENERAL W.R.T. ALLOCATION OF GLYPH DATA ******/
  504. make_const_string(op - 1,
  505. a_readonly | imemory_space((gs_ref_memory_t *)pfont->memory),
  506. gdata.bits.size,
  507. gdata.bits.data);
  508. make_int(op, fidx);
  509. return code;
  510. }
  511. /* ------ Initialization procedure ------ */
  512. const op_def zfcid0_op_defs[] =
  513. {
  514. {"2.buildfont9", zbuildfont9},
  515. {"2.type9mapcid", ztype9mapcid},
  516. op_def_end(0)
  517. };