gdevpdti.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* Copyright (C) 2002 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: gdevpdti.c,v 1.53 2005/10/12 08:16:50 leonardo Exp $ */
  14. /* Bitmap font implementation for pdfwrite */
  15. #include "memory_.h"
  16. #include "string_.h"
  17. #include "gx.h"
  18. #include "gxpath.h"
  19. #include "gserrors.h"
  20. #include "gsutil.h"
  21. #include "gdevpdfx.h"
  22. #include "gdevpdfg.h"
  23. #include "gdevpdtf.h"
  24. #include "gdevpdti.h"
  25. #include "gdevpdts.h"
  26. #include "gdevpdtw.h"
  27. #include "gdevpdtt.h"
  28. #include "gdevpdfo.h"
  29. /* ---------------- Private ---------------- */
  30. /* Define the structure for a CharProc pseudo-resource. */
  31. /*typedef struct pdf_char_proc_s pdf_char_proc_t;*/ /* gdevpdfx.h */
  32. struct pdf_char_proc_s {
  33. pdf_resource_common(pdf_char_proc_t);
  34. pdf_font_resource_t *font;
  35. pdf_char_proc_t *char_next; /* next char_proc for same font */
  36. int y_offset; /* of character (0,0) */
  37. gs_char char_code;
  38. gs_const_string char_name;
  39. gs_point real_width; /* Not used with synthesised bitmap fonts. */
  40. gs_point v; /* Not used with synthesised bitmap fonts. */
  41. };
  42. /* The descriptor is public for pdf_resource_type_structs. */
  43. gs_public_st_suffix_add2_string1(st_pdf_char_proc, pdf_char_proc_t,
  44. "pdf_char_proc_t", pdf_char_proc_enum_ptrs, pdf_char_proc_reloc_ptrs,
  45. st_pdf_resource, font, char_next, char_name);
  46. /* Define the state structure for tracking bitmap fonts. */
  47. /*typedef struct pdf_bitmap_fonts_s pdf_bitmap_fonts_t;*/
  48. struct pdf_bitmap_fonts_s {
  49. pdf_font_resource_t *open_font; /* current Type 3 synthesized font */
  50. bool use_open_font; /* if false, start new open_font */
  51. long bitmap_encoding_id;
  52. int max_embedded_code; /* max Type 3 code used */
  53. };
  54. gs_private_st_ptrs1(st_pdf_bitmap_fonts, pdf_bitmap_fonts_t,
  55. "pdf_bitmap_fonts_t", pdf_bitmap_fonts_enum_ptrs,
  56. pdf_bitmap_fonts_reloc_ptrs, open_font);
  57. inline private long
  58. pdf_char_proc_id(const pdf_char_proc_t *pcp)
  59. {
  60. return pdf_resource_id((const pdf_resource_t *)pcp);
  61. }
  62. /* Assign a code for a char_proc. */
  63. private int
  64. assign_char_code(gx_device_pdf * pdev, int width)
  65. {
  66. pdf_bitmap_fonts_t *pbfs = pdev->text->bitmap_fonts;
  67. pdf_font_resource_t *pdfont = pbfs->open_font; /* Type 3 */
  68. int c, code;
  69. if (pbfs->bitmap_encoding_id == 0)
  70. pbfs->bitmap_encoding_id = pdf_obj_ref(pdev);
  71. if (pdfont == 0 || pdfont->u.simple.LastChar == 255 ||
  72. !pbfs->use_open_font
  73. ) {
  74. /* Start a new synthesized font. */
  75. char *pc;
  76. code = pdf_font_type3_alloc(pdev, &pdfont, pdf_write_contents_bitmap);
  77. if (code < 0)
  78. return code;
  79. pdfont->u.simple.s.type3.bitmap_font = true;
  80. if (pbfs->open_font == 0)
  81. pdfont->rname[0] = 0;
  82. else
  83. strcpy(pdfont->rname, pbfs->open_font->rname);
  84. pdfont->u.simple.s.type3.FontBBox.p.x = 0;
  85. pdfont->u.simple.s.type3.FontBBox.p.y = 0;
  86. pdfont->u.simple.s.type3.FontBBox.q.x = 1000;
  87. pdfont->u.simple.s.type3.FontBBox.q.y = 1000;
  88. gs_make_identity(&pdfont->u.simple.s.type3.FontMatrix);
  89. /*
  90. * We "increment" the font name as a radix-26 "number".
  91. * This cannot possibly overflow.
  92. */
  93. for (pc = pdfont->rname; *pc == 'Z'; ++pc)
  94. *pc = '@';
  95. if ((*pc)++ == 0)
  96. *pc = 'A', pc[1] = 0;
  97. pbfs->open_font = pdfont;
  98. pbfs->use_open_font = true;
  99. pdfont->u.simple.FirstChar = 0;
  100. }
  101. c = ++(pdfont->u.simple.LastChar);
  102. pdfont->Widths[c] = psdf_round(pdev->char_width.x, 100, 10); /* See
  103. pdf_write_Widths about rounding. We need to provide
  104. a compatible data for Tj. */
  105. if (c > pbfs->max_embedded_code)
  106. pbfs->max_embedded_code = c;
  107. /* Synthezise ToUnicode CMap :*/
  108. { gs_text_enum_t *pte = pdev->pte;
  109. gs_font *font = pte->current_font;
  110. code = pdf_add_ToUnicode(pdev, font, pdfont, pte->returned.current_glyph, c);
  111. if (code < 0)
  112. return code;
  113. }
  114. return c;
  115. }
  116. /* Write the contents of a Type 3 bitmap or vector font resource. */
  117. int
  118. pdf_write_contents_bitmap(gx_device_pdf *pdev, pdf_font_resource_t *pdfont)
  119. {
  120. stream *s = pdev->strm;
  121. const pdf_char_proc_t *pcp;
  122. long diff_id = 0;
  123. int code;
  124. if (pdfont->u.simple.s.type3.bitmap_font)
  125. diff_id = pdev->text->bitmap_fonts->bitmap_encoding_id;
  126. else {
  127. /* See comment in pdf_write_encoding. */
  128. diff_id = pdf_obj_ref(pdev);
  129. }
  130. code = pdf_write_encoding_ref(pdev, pdfont, diff_id);
  131. if (code < 0)
  132. return code;
  133. stream_puts(s, "/CharProcs <<");
  134. /* Write real characters. */
  135. for (pcp = pdfont->u.simple.s.type3.char_procs; pcp;
  136. pcp = pcp->char_next
  137. ) {
  138. if (pdfont->u.simple.s.type3.bitmap_font)
  139. pprintld2(s, "/a%ld %ld 0 R\n", (long)pcp->char_code,
  140. pdf_char_proc_id(pcp));
  141. else {
  142. pdf_put_name(pdev, pcp->char_name.data, pcp->char_name.size);
  143. pprintld1(s, " %ld 0 R\n", pdf_char_proc_id(pcp));
  144. }
  145. }
  146. stream_puts(s, ">>");
  147. pprintg6(s, "/FontMatrix[%g %g %g %g %g %g]",
  148. (float)pdfont->u.simple.s.type3.FontMatrix.xx,
  149. (float)pdfont->u.simple.s.type3.FontMatrix.xy,
  150. (float)pdfont->u.simple.s.type3.FontMatrix.yx,
  151. (float)pdfont->u.simple.s.type3.FontMatrix.yy,
  152. (float)pdfont->u.simple.s.type3.FontMatrix.tx,
  153. (float)pdfont->u.simple.s.type3.FontMatrix.ty);
  154. code = pdf_finish_write_contents_type3(pdev, pdfont);
  155. if (code < 0)
  156. return code;
  157. s = pdev->strm; /* pdf_finish_write_contents_type3 changes pdev->strm . */
  158. if (!pdfont->u.simple.s.type3.bitmap_font && diff_id > 0) {
  159. code = pdf_write_encoding(pdev, pdfont, diff_id, 0);
  160. if (code < 0)
  161. return code;
  162. }
  163. return 0;
  164. }
  165. /* ---------------- Public ---------------- */
  166. /*
  167. * Allocate and initialize bookkeeping for bitmap fonts.
  168. */
  169. pdf_bitmap_fonts_t *
  170. pdf_bitmap_fonts_alloc(gs_memory_t *mem)
  171. {
  172. pdf_bitmap_fonts_t *pbfs =
  173. gs_alloc_struct(mem, pdf_bitmap_fonts_t, &st_pdf_bitmap_fonts,
  174. "pdf_bitmap_fonts_alloc");
  175. if (pbfs == 0)
  176. return 0;
  177. memset(pbfs, 0, sizeof(*pbfs));
  178. pbfs->max_embedded_code = -1;
  179. return pbfs;
  180. }
  181. /*
  182. * Update text state at the end of a page.
  183. */
  184. void
  185. pdf_close_text_page(gx_device_pdf *pdev)
  186. {
  187. /*
  188. * When Acrobat Reader 3 prints a file containing a Type 3 font with a
  189. * non-standard Encoding, it apparently only emits the subset of the
  190. * font actually used on the page. Thus, if the "Download Fonts Once"
  191. * option is selected, characters not used on the page where the font
  192. * first appears will not be defined, and hence will print as blank if
  193. * used on subsequent pages. Thus, we can't allow a Type 3 font to
  194. * add additional characters on subsequent pages.
  195. */
  196. if (pdev->CompatibilityLevel <= 1.2)
  197. pdev->text->bitmap_fonts->use_open_font = false;
  198. }
  199. /* Return the Y offset for a bitmap character image. */
  200. int
  201. pdf_char_image_y_offset(const gx_device_pdf *pdev, int x, int y, int h)
  202. {
  203. const pdf_text_data_t *const ptd = pdev->text;
  204. gs_point pt;
  205. int max_off, off;
  206. pdf_text_position(pdev, &pt);
  207. if (x < pt.x)
  208. return 0;
  209. max_off = (ptd->bitmap_fonts->open_font == 0 ? 0 :
  210. ptd->bitmap_fonts->open_font->u.simple.s.type3.max_y_offset);
  211. off = (y + h) - (int)(pt.y + 0.5);
  212. if (off < -max_off || off > max_off)
  213. off = 0;
  214. return off;
  215. }
  216. /* Begin a CharProc for a synthesized font. */
  217. private int
  218. pdf_begin_char_proc_generic(gx_device_pdf * pdev, pdf_font_resource_t *pdfont,
  219. gs_id id, gs_char char_code,
  220. pdf_char_proc_t ** ppcp, pdf_stream_position_t * ppos)
  221. {
  222. pdf_resource_t *pres;
  223. pdf_char_proc_t *pcp;
  224. int code;
  225. code = pdf_begin_resource(pdev, resourceCharProc, id, &pres);
  226. if (code < 0)
  227. return code;
  228. pcp = (pdf_char_proc_t *) pres;
  229. pcp->font = pdfont;
  230. pcp->char_next = pdfont->u.simple.s.type3.char_procs;
  231. pdfont->u.simple.s.type3.char_procs = pcp;
  232. pcp->char_code = char_code;
  233. pres->object->written = true;
  234. pcp->char_name.data = 0;
  235. pcp->char_name.size = 0;
  236. {
  237. stream *s = pdev->strm;
  238. /*
  239. * The resource file is positionable, so rather than use an
  240. * object reference for the length, we'll go back and fill it in
  241. * at the end of the definition. Take 1M as the longest
  242. * definition we can handle. (This used to be 10K, but there was
  243. * a real file that exceeded this limit.)
  244. */
  245. stream_puts(s, "<</Length >>stream\n");
  246. ppos->start_pos = stell(s);
  247. }
  248. code = pdf_begin_encrypt(pdev, &pdev->strm, pres->object->id);
  249. if (code < 0)
  250. return code;
  251. *ppcp = pcp;
  252. return 0;
  253. }
  254. /* Begin a CharProc for a synthesized (bitmap) font. */
  255. int
  256. pdf_begin_char_proc(gx_device_pdf * pdev, int w, int h, int x_width,
  257. int y_offset, gs_id id, pdf_char_proc_t ** ppcp,
  258. pdf_stream_position_t * ppos)
  259. {
  260. int char_code = assign_char_code(pdev, x_width);
  261. pdf_bitmap_fonts_t *const pbfs = pdev->text->bitmap_fonts;
  262. pdf_font_resource_t *font = pbfs->open_font; /* Type 3 */
  263. int code = pdf_begin_char_proc_generic(pdev, font, id, char_code, ppcp, ppos);
  264. if (code < 0)
  265. return code;
  266. (*ppcp)->y_offset = y_offset;
  267. font->u.simple.s.type3.FontBBox.p.y =
  268. min(font->u.simple.s.type3.FontBBox.p.y, y_offset);
  269. font->u.simple.s.type3.FontBBox.q.x =
  270. max(font->u.simple.s.type3.FontBBox.q.x, w);
  271. font->u.simple.s.type3.FontBBox.q.y =
  272. max(font->u.simple.s.type3.FontBBox.q.y, y_offset + h);
  273. font->u.simple.s.type3.max_y_offset =
  274. max(font->u.simple.s.type3.max_y_offset, h + (h >> 2));
  275. return 0;
  276. }
  277. /* End a CharProc. */
  278. int
  279. pdf_end_char_proc(gx_device_pdf * pdev, pdf_stream_position_t * ppos)
  280. {
  281. stream *s;
  282. long start_pos, end_pos, length;
  283. pdf_end_encrypt(pdev);
  284. s = pdev->strm;
  285. start_pos = ppos->start_pos;
  286. end_pos = stell(s);
  287. length = end_pos - start_pos;
  288. if (length > 999999)
  289. return_error(gs_error_limitcheck);
  290. sseek(s, start_pos - 15);
  291. pprintd1(s, "%d", length);
  292. sseek(s, end_pos);
  293. stream_puts(s, "endstream\n");
  294. pdf_end_separate(pdev);
  295. return 0;
  296. }
  297. /* Put out a reference to an image as a character in a synthesized font. */
  298. int
  299. pdf_do_char_image(gx_device_pdf * pdev, const pdf_char_proc_t * pcp,
  300. const gs_matrix * pimat)
  301. {
  302. pdf_font_resource_t *pdfont = pcp->font;
  303. byte ch = pcp->char_code;
  304. pdf_text_state_values_t values;
  305. values.character_spacing = 0;
  306. values.pdfont = pdfont;
  307. values.size = 1;
  308. values.matrix = *pimat;
  309. values.matrix.ty -= pcp->y_offset;
  310. values.render_mode = 0;
  311. values.word_spacing = 0;
  312. pdf_set_text_state_values(pdev, &values);
  313. pdf_append_chars(pdev, &ch, 1, pdfont->Widths[ch] * pimat->xx, 0.0, false);
  314. return 0;
  315. }
  316. /*
  317. * Write the Encoding for bitmap fonts, if needed.
  318. */
  319. int
  320. pdf_write_bitmap_fonts_Encoding(gx_device_pdf *pdev)
  321. {
  322. pdf_bitmap_fonts_t *pbfs = pdev->text->bitmap_fonts;
  323. if (pbfs->bitmap_encoding_id) {
  324. stream *s;
  325. int i;
  326. pdf_open_separate(pdev, pbfs->bitmap_encoding_id);
  327. s = pdev->strm;
  328. /*
  329. * Even though the PDF reference documentation says that a
  330. * BaseEncoding key is required unless the encoding is
  331. * "based on the base font's encoding" (and there is no base
  332. * font in this case), Acrobat 2.1 gives an error if the
  333. * BaseEncoding key is present.
  334. */
  335. stream_puts(s, "<</Type/Encoding/Differences[0");
  336. for (i = 0; i <= pbfs->max_embedded_code; ++i) {
  337. if (!(i & 15))
  338. stream_puts(s, "\n");
  339. pprintd1(s, "/a%d", i);
  340. }
  341. stream_puts(s, "\n] >>\n");
  342. pdf_end_separate(pdev);
  343. pbfs->bitmap_encoding_id = 0;
  344. }
  345. return 0;
  346. }
  347. /*
  348. * Start charproc accumulation for a Type 3 font.
  349. */
  350. int
  351. pdf_start_charproc_accum(gx_device_pdf *pdev)
  352. {
  353. pdf_char_proc_t *pcp;
  354. pdf_resource_t *pres;
  355. int code = pdf_enter_substream(pdev, resourceCharProc, gs_next_ids(pdev->memory, 1),
  356. &pres, false, pdev->CompressFonts);
  357. if (code < 0)
  358. return code;
  359. pcp = (pdf_char_proc_t *)pres;
  360. pcp->char_next = NULL;
  361. pcp->font = NULL;
  362. pcp->char_code = GS_NO_CHAR;
  363. pcp->char_name.data = NULL;
  364. pcp->char_name.size = 0;
  365. return 0;
  366. }
  367. /*
  368. * Install charproc accumulator for a Type 3 font.
  369. */
  370. int
  371. pdf_set_charproc_attrs(gx_device_pdf *pdev, gs_font *font, const double *pw, int narg,
  372. gs_text_cache_control_t control, gs_char ch, gs_const_string *gnstr)
  373. {
  374. pdf_font_resource_t *pdfont;
  375. pdf_resource_t *pres = pdev->accumulating_substream_resource;
  376. pdf_char_proc_t *pcp;
  377. int code;
  378. code = pdf_attached_font_resource(pdev, font, &pdfont, NULL, NULL, NULL, NULL);
  379. if (code < 0)
  380. return code;
  381. pcp = (pdf_char_proc_t *)pres;
  382. pcp->char_next = NULL;
  383. pcp->font = pdfont;
  384. pcp->char_code = ch;
  385. pcp->char_name = *gnstr;
  386. pcp->real_width.x = pw[font->WMode && narg > 6 ? 6 : 0];
  387. pcp->real_width.y = pw[font->WMode && narg > 6 ? 7 : 1];
  388. pcp->v.x = (narg > 8 ? pw[8] : 0);
  389. pcp->v.y = (narg > 8 ? pw[9] : 0);
  390. if (control == TEXT_SET_CHAR_WIDTH) {
  391. /* PLRM 5.7.1 "BuildGlyph" reads : "Normally, it is unnecessary and
  392. undesirable to initialize the current color parameter, because show
  393. is defined to paint glyphs with the current color."
  394. However comparefiles/Bug687044.ps doesn't follow that. */
  395. pdev->skip_colors = false;
  396. pprintg2(pdev->strm, "%g %g d0\n", (float)pw[0], (float)pw[1]);
  397. } else {
  398. pdev->skip_colors = true;
  399. pprintg6(pdev->strm, "%g %g %g %g %g %g d1\n",
  400. (float)pw[0], (float)pw[1], (float)pw[2],
  401. (float)pw[3], (float)pw[4], (float)pw[5]);
  402. pdfont->u.simple.s.type3.cached[ch >> 3] |= 0x80 >> (ch & 7);
  403. }
  404. pdfont->used[ch >> 3] |= 0x80 >> (ch & 7);
  405. pdev->font3 = (pdf_resource_t *)pdfont;
  406. return 0;
  407. }
  408. /*
  409. * Open a stream object in the temporary file.
  410. */
  411. int
  412. pdf_open_aside(gx_device_pdf *pdev, pdf_resource_type_t rtype,
  413. gs_id id, pdf_resource_t **ppres, bool reserve_object_id, int options)
  414. {
  415. int code;
  416. pdf_resource_t *pres;
  417. stream *s, *save_strm = pdev->strm;
  418. pdf_data_writer_t writer;
  419. static const pdf_filter_names_t fnames = {
  420. PDF_FILTER_NAMES
  421. };
  422. pdev->streams.save_strm = pdev->strm;
  423. code = pdf_alloc_aside(pdev, PDF_RESOURCE_CHAIN(pdev, rtype, id),
  424. pdf_resource_type_structs[rtype], &pres, reserve_object_id ? 0 : -1);
  425. if (code < 0)
  426. return code;
  427. cos_become(pres->object, cos_type_stream);
  428. s = cos_write_stream_alloc((cos_stream_t *)pres->object, pdev, "pdf_enter_substream");
  429. if (s == 0)
  430. return_error(gs_error_VMerror);
  431. pdev->strm = s;
  432. code = pdf_append_data_stream_filters(pdev, &writer,
  433. options | DATA_STREAM_NOLENGTH, pres->object->id);
  434. if (code < 0) {
  435. pdev->strm = save_strm;
  436. return code;
  437. }
  438. code = pdf_put_filters((cos_dict_t *)pres->object, pdev, writer.binary.strm, &fnames);
  439. if (code < 0) {
  440. pdev->strm = save_strm;
  441. return code;
  442. }
  443. pdev->strm = writer.binary.strm;
  444. *ppres = pres;
  445. return 0;
  446. }
  447. /*
  448. * Close a stream object in the temporary file.
  449. */
  450. int
  451. pdf_close_aside(gx_device_pdf *pdev)
  452. {
  453. /* We should call pdf_end_data here, but we don't want to put pdf_data_writer_t
  454. into pdf_substream_save stack to simplify garbager descriptors.
  455. Use a lower level functions instead that. */
  456. stream *s = pdev->strm;
  457. int status = s_close_filters(&s, cos_write_stream_from_pipeline(s));
  458. cos_stream_t *pcs = cos_stream_from_pipeline(s);
  459. int code = 0;
  460. if (status < 0)
  461. code = gs_note_error(gs_error_ioerror);
  462. pcs->is_open = false;
  463. sclose(s);
  464. pdev->strm = pdev->streams.save_strm;
  465. return code;
  466. }
  467. /*
  468. * Enter the substream accumulation mode.
  469. */
  470. int
  471. pdf_enter_substream(gx_device_pdf *pdev, pdf_resource_type_t rtype,
  472. gs_id id, pdf_resource_t **ppres, bool reserve_object_id, bool compress)
  473. {
  474. int sbstack_ptr = pdev->sbstack_depth;
  475. pdf_resource_t *pres;
  476. stream *save_strm = pdev->strm;
  477. int code;
  478. if (pdev->sbstack_depth >= pdev->sbstack_size)
  479. return_error(gs_error_unregistered); /* Must not happen. */
  480. if (pdev->sbstack[sbstack_ptr].text_state == 0) {
  481. pdev->sbstack[sbstack_ptr].text_state = pdf_text_state_alloc(pdev->pdf_memory);
  482. if (pdev->sbstack[sbstack_ptr].text_state == 0)
  483. return_error(gs_error_VMerror);
  484. }
  485. code = pdf_open_aside(pdev, rtype, id, &pres, reserve_object_id,
  486. (compress ? DATA_STREAM_COMPRESS : 0));
  487. if (code < 0)
  488. return code;
  489. code = pdf_save_viewer_state(pdev, NULL);
  490. if (code < 0) {
  491. pdev->strm = save_strm;
  492. return code;
  493. }
  494. pdev->sbstack[sbstack_ptr].context = pdev->context;
  495. pdf_text_state_copy(pdev->sbstack[sbstack_ptr].text_state, pdev->text->text_state);
  496. pdf_set_text_state_default(pdev->text->text_state);
  497. pdev->sbstack[sbstack_ptr].clip_path = pdev->clip_path;
  498. pdev->clip_path = 0;
  499. pdev->sbstack[sbstack_ptr].clip_path_id = pdev->clip_path_id;
  500. pdev->clip_path_id = pdev->no_clip_path_id;
  501. pdev->sbstack[sbstack_ptr].vgstack_bottom = pdev->vgstack_bottom;
  502. pdev->vgstack_bottom = pdev->vgstack_depth;
  503. pdev->sbstack[sbstack_ptr].strm = save_strm;
  504. pdev->sbstack[sbstack_ptr].procsets = pdev->procsets;
  505. pdev->sbstack[sbstack_ptr].substream_Resources = pdev->substream_Resources;
  506. pdev->sbstack[sbstack_ptr].skip_colors = pdev->skip_colors;
  507. pdev->sbstack[sbstack_ptr].font3 = pdev->font3;
  508. pdev->sbstack[sbstack_ptr].accumulating_substream_resource = pdev->accumulating_substream_resource;
  509. pdev->sbstack[sbstack_ptr].charproc_just_accumulated = pdev->charproc_just_accumulated;
  510. pdev->sbstack[sbstack_ptr].accumulating_a_global_object = pdev->accumulating_a_global_object;
  511. pdev->sbstack[sbstack_ptr].pres_soft_mask_dict = pdev->pres_soft_mask_dict;
  512. pdev->sbstack[sbstack_ptr].objname = pdev->objname;
  513. pdev->skip_colors = false;
  514. pdev->charproc_just_accumulated = false;
  515. pdev->pres_soft_mask_dict = NULL;
  516. pdev->objname.data = NULL;
  517. pdev->objname.size = 0;
  518. /* Do not reset pdev->accumulating_a_global_object - it inherits. */
  519. pdev->sbstack_depth++;
  520. pdev->procsets = 0;
  521. pdev->font3 = 0;
  522. pdev->context = PDF_IN_STREAM;
  523. pdev->accumulating_substream_resource = pres;
  524. pdf_reset_graphics(pdev);
  525. *ppres = pres;
  526. return 0;
  527. }
  528. /*
  529. * Exit the substream accumulation mode.
  530. */
  531. int
  532. pdf_exit_substream(gx_device_pdf *pdev)
  533. {
  534. int code, code1;
  535. int sbstack_ptr;
  536. if (pdev->sbstack_depth <= 0)
  537. return_error(gs_error_unregistered); /* Must not happen. */
  538. code = pdf_open_contents(pdev, PDF_IN_STREAM);
  539. sbstack_ptr = pdev->sbstack_depth - 1;
  540. while (pdev->vgstack_depth > pdev->vgstack_bottom) {
  541. code1 = pdf_restore_viewer_state(pdev, pdev->strm);
  542. if (code >= 0)
  543. code = code1;
  544. }
  545. if (pdev->clip_path != 0)
  546. gx_path_free(pdev->clip_path, "pdf_end_charproc_accum");
  547. code1 = pdf_close_aside(pdev);
  548. if (code1 < 0 && code >= 0)
  549. code = code1;
  550. pdev->context = pdev->sbstack[sbstack_ptr].context;
  551. pdf_text_state_copy(pdev->text->text_state, pdev->sbstack[sbstack_ptr].text_state);
  552. pdev->clip_path = pdev->sbstack[sbstack_ptr].clip_path;
  553. pdev->sbstack[sbstack_ptr].clip_path = 0;
  554. pdev->clip_path_id = pdev->sbstack[sbstack_ptr].clip_path_id;
  555. pdev->vgstack_bottom = pdev->sbstack[sbstack_ptr].vgstack_bottom;
  556. pdev->strm = pdev->sbstack[sbstack_ptr].strm;
  557. pdev->sbstack[sbstack_ptr].strm = 0;
  558. pdev->procsets = pdev->sbstack[sbstack_ptr].procsets;
  559. pdev->substream_Resources = pdev->sbstack[sbstack_ptr].substream_Resources;
  560. pdev->sbstack[sbstack_ptr].substream_Resources = 0;
  561. pdev->skip_colors = pdev->sbstack[sbstack_ptr].skip_colors;
  562. pdev->font3 = pdev->sbstack[sbstack_ptr].font3;
  563. pdev->sbstack[sbstack_ptr].font3 = 0;
  564. pdev->accumulating_substream_resource = pdev->sbstack[sbstack_ptr].accumulating_substream_resource;
  565. pdev->sbstack[sbstack_ptr].accumulating_substream_resource = 0;
  566. pdev->charproc_just_accumulated = pdev->sbstack[sbstack_ptr].charproc_just_accumulated;
  567. pdev->accumulating_a_global_object = pdev->sbstack[sbstack_ptr].accumulating_a_global_object;
  568. pdev->pres_soft_mask_dict = pdev->sbstack[sbstack_ptr].pres_soft_mask_dict;
  569. pdev->objname = pdev->sbstack[sbstack_ptr].objname;
  570. pdev->sbstack_depth = sbstack_ptr;
  571. code1 = pdf_restore_viewer_state(pdev, NULL);
  572. if (code1 < 0 && code >= 0)
  573. code = code1;
  574. return code;
  575. }
  576. private bool
  577. pdf_is_same_charproc1(gx_device_pdf * pdev, pdf_char_proc_t *pcp0, pdf_char_proc_t *pcp1)
  578. {
  579. if (pcp0->char_code != pcp1->char_code)
  580. return false; /* We need same encoding. */
  581. if (pcp0->font->u.simple.Encoding[pcp0->char_code].glyph !=
  582. pcp1->font->u.simple.Encoding[pcp1->char_code].glyph)
  583. return false; /* We need same encoding. */
  584. if (bytes_compare(pcp0->char_name.data, pcp0->char_name.size,
  585. pcp1->char_name.data, pcp1->char_name.size))
  586. return false; /* We need same encoding. */
  587. if (pcp0->real_width.x != pcp1->real_width.x)
  588. return false;
  589. if (pcp0->real_width.y != pcp1->real_width.y)
  590. return false;
  591. if (pcp0->v.x != pcp1->v.x)
  592. return false;
  593. if (pcp0->v.y != pcp1->v.y)
  594. return false;
  595. if (pcp0->font->u.simple.s.type3.bitmap_font != pcp1->font->u.simple.s.type3.bitmap_font)
  596. return false;
  597. if (memcmp(&pcp0->font->u.simple.s.type3.FontMatrix, &pcp1->font->u.simple.s.type3.FontMatrix,
  598. sizeof(pcp0->font->u.simple.s.type3.FontMatrix)))
  599. return false;
  600. return pdf_check_encoding_compatibility(pcp1->font, pdev->cgp->s, pdev->cgp->num_all_chars);
  601. }
  602. private int
  603. pdf_is_same_charproc(gx_device_pdf * pdev, pdf_resource_t *pres0, pdf_resource_t *pres1)
  604. {
  605. return pdf_is_same_charproc1(pdev, (pdf_char_proc_t *)pres0, (pdf_char_proc_t *)pres1);
  606. }
  607. private int
  608. pdf_find_same_charproc(gx_device_pdf *pdev,
  609. pdf_font_resource_t *pdfont, const pdf_char_glyph_pairs_t *cgp,
  610. pdf_char_proc_t **ppcp)
  611. {
  612. pdf_char_proc_t *pcp;
  613. int code;
  614. pdev->cgp = cgp;
  615. for (pcp = pdfont->u.simple.s.type3.char_procs; pcp != NULL; pcp = pcp->char_next) {
  616. if (*ppcp != pcp && pdf_is_same_charproc1(pdev, *ppcp, pcp)) {
  617. cos_object_t *pco0 = pcp->object;
  618. cos_object_t *pco1 = (*ppcp)->object;
  619. code = pco0->cos_procs->equal(pco0, pco1, pdev);
  620. if (code < 0)
  621. return code;
  622. if (code) {
  623. *ppcp = pcp;
  624. pdev->cgp = NULL;
  625. return 1;
  626. }
  627. }
  628. }
  629. pcp = *ppcp;
  630. code = pdf_find_same_resource(pdev, resourceCharProc, (pdf_resource_t **)ppcp, pdf_is_same_charproc);
  631. pdev->cgp = NULL;
  632. if (code <= 0)
  633. return code;
  634. /* fixme : do we need more checks here ? */
  635. return 1;
  636. }
  637. private bool
  638. pdf_is_charproc_defined(gx_device_pdf *pdev, pdf_font_resource_t *pdfont, gs_char ch)
  639. {
  640. pdf_char_proc_t *pcp;
  641. for (pcp = pdfont->u.simple.s.type3.char_procs; pcp != NULL; pcp = pcp->char_next) {
  642. if (pcp->char_code == ch) {
  643. return true;
  644. }
  645. }
  646. return false;
  647. }
  648. /*
  649. * Complete charproc accumulation for a Type 3 font.
  650. */
  651. int
  652. pdf_end_charproc_accum(gx_device_pdf *pdev, gs_font *font, const pdf_char_glyph_pairs_t *cgp)
  653. {
  654. int code;
  655. pdf_resource_t *pres = (pdf_resource_t *)pdev->accumulating_substream_resource;
  656. /* We could use pdfont->u.simple.s.type3.char_procs insted the thing above
  657. unless the font is defined recursively.
  658. But we don't want such assumption. */
  659. pdf_char_proc_t *pcp = (pdf_char_proc_t *)pres;
  660. pdf_font_resource_t *pdfont;
  661. gs_char ch = pcp->char_code;
  662. double *real_widths;
  663. byte *glyph_usage;
  664. int char_cache_size, width_cache_size;
  665. gs_glyph glyph0;
  666. bool checking_glyph_variation = false;
  667. int i;
  668. code = pdf_attached_font_resource(pdev, font, &pdfont, NULL, NULL, NULL, NULL);
  669. if (code < 0)
  670. return code;
  671. if (pdfont != (pdf_font_resource_t *)pdev->font3)
  672. return_error(gs_error_unregistered); /* Must not happen. */
  673. if (ch == GS_NO_CHAR)
  674. return_error(gs_error_unregistered); /* Must not happen. */
  675. if (ch >= 256)
  676. return_error(gs_error_unregistered); /* Must not happen. */
  677. code = pdf_exit_substream(pdev);
  678. if (code < 0)
  679. return code;
  680. if (pdfont->used[ch >> 3] & (0x80 >> (ch & 7))) {
  681. if (!(pdfont->u.simple.s.type3.cached[ch >> 3] & (0x80 >> (ch & 7)))) {
  682. checking_glyph_variation = true;
  683. code = pdf_find_same_charproc(pdev, pdfont, cgp, &pcp);
  684. if (code < 0)
  685. return code;
  686. if (code != 0) {
  687. code = pdf_cancel_resource(pdev, pres, resourceCharProc);
  688. if (code < 0)
  689. return code;
  690. pdf_forget_resource(pdev, pres, resourceCharProc);
  691. if (pcp->font != pdfont) {
  692. code = pdf_attach_font_resource(pdev, font, pcp->font);
  693. if (code < 0)
  694. return code;
  695. }
  696. pdev->charproc_just_accumulated = true;
  697. return 0;
  698. }
  699. if (pdf_is_charproc_defined(pdev, pdfont, ch)) {
  700. gs_font *base_font = font, *below;
  701. while ((below = base_font->base) != base_font &&
  702. base_font->procs.same_font(base_font, below, FONT_SAME_OUTLINES))
  703. base_font = below;
  704. code = pdf_make_font3_resource(pdev, base_font, &pdfont);
  705. if (code < 0)
  706. return code;
  707. code = pdf_attach_font_resource(pdev, font, pdfont);
  708. if (code < 0)
  709. return code;
  710. }
  711. }
  712. }
  713. pdf_reserve_object_id(pdev, pres, 0);
  714. code = pdf_attached_font_resource(pdev, font, &pdfont,
  715. &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
  716. if (code < 0)
  717. return code;
  718. if (ch >= char_cache_size || ch >= width_cache_size)
  719. return_error(gs_error_unregistered); /* Must not happen. */
  720. if (checking_glyph_variation)
  721. pdev->charproc_just_accumulated = true;
  722. glyph0 = font->procs.encode_char(font, ch, GLYPH_SPACE_NAME);
  723. pcp->char_next = pdfont->u.simple.s.type3.char_procs;
  724. pdfont->u.simple.s.type3.char_procs = pcp;
  725. pcp->font = pdfont;
  726. pdfont->Widths[ch] = pcp->real_width.x;
  727. real_widths[ch * 2 ] = pcp->real_width.x;
  728. real_widths[ch * 2 + 1] = pcp->real_width.y;
  729. glyph_usage[ch / 8] |= 0x80 >> (ch & 7);
  730. pdfont->used[ch >> 3] |= 0x80 >> (ch & 7);
  731. if (pdfont->u.simple.v != NULL && font->WMode) {
  732. pdfont->u.simple.v[ch].x = pcp->v.x;
  733. pdfont->u.simple.v[ch].y = pcp->v.x;
  734. }
  735. for (i = 0; i < 256; i++) {
  736. gs_glyph glyph = font->procs.encode_char(font, i,
  737. font->FontType == ft_user_defined ? GLYPH_SPACE_NOGEN
  738. : GLYPH_SPACE_NAME);
  739. if (glyph == glyph0) {
  740. real_widths[i * 2 ] = real_widths[ch * 2 ];
  741. real_widths[i * 2 + 1] = real_widths[ch * 2 + 1];
  742. glyph_usage[i / 8] |= 0x80 >> (i & 7);
  743. pdfont->used[i >> 3] |= 0x80 >> (i & 7);
  744. pdfont->u.simple.v[i] = pdfont->u.simple.v[ch];
  745. pdfont->Widths[i] = pdfont->Widths[ch];
  746. }
  747. }
  748. return 0;
  749. }
  750. /* Add procsets to substream Resources. */
  751. int
  752. pdf_add_procsets(cos_dict_t *pcd, pdf_procset_t procsets)
  753. {
  754. char str[5 + 7 + 7 + 7 + 5 + 2];
  755. cos_value_t v;
  756. strcpy(str, "[/PDF");
  757. if (procsets & ImageB)
  758. strcat(str, "/ImageB");
  759. if (procsets & ImageC)
  760. strcat(str, "/ImageC");
  761. if (procsets & ImageI)
  762. strcat(str, "/ImageI");
  763. if (procsets & Text)
  764. strcat(str, "/Text");
  765. strcat(str, "]");
  766. cos_string_value(&v, (byte *)str, strlen(str));
  767. return cos_dict_put_c_key(pcd, "/ProcSet", &v);
  768. }
  769. /* Add a resource to substream Resources. */
  770. int
  771. pdf_add_resource(gx_device_pdf *pdev, cos_dict_t *pcd, const char *key, pdf_resource_t *pres)
  772. {
  773. if (pcd != 0) {
  774. const cos_value_t *v = cos_dict_find(pcd, (const byte *)key, strlen(key));
  775. cos_dict_t *list;
  776. int code;
  777. char buf[10 + (sizeof(long) * 8 / 3 + 1)], buf1[sizeof(pres->rname) + 1];
  778. if (pdev->ForOPDFRead && !pres->global && pdev->accumulating_a_global_object) {
  779. pres->global = true;
  780. code = cos_dict_put_c_key_bool((cos_dict_t *)pres->object, "/.Global", true);
  781. if (code < 0)
  782. return code;
  783. }
  784. sprintf(buf, "%ld 0 R\n", pres->object->id);
  785. if (v != NULL) {
  786. if (v->value_type != COS_VALUE_OBJECT &&
  787. v->value_type != COS_VALUE_RESOURCE)
  788. return_error(gs_error_unregistered); /* Must not happen. */
  789. list = (cos_dict_t *)v->contents.object;
  790. if (list->cos_procs != &cos_dict_procs)
  791. return_error(gs_error_unregistered); /* Must not happen. */
  792. } else {
  793. list = cos_dict_alloc(pdev, "pdf_add_resource");
  794. if (list == NULL)
  795. return_error(gs_error_VMerror);
  796. code = cos_dict_put_c_key_object((cos_dict_t *)pcd, key, (cos_object_t *)list);
  797. if (code < 0)
  798. return code;
  799. }
  800. buf1[0] = '/';
  801. strcpy(buf1 + 1, pres->rname);
  802. return cos_dict_put_string(list, (const byte *)buf1, strlen(buf1),
  803. (const byte *)buf, strlen(buf));
  804. }
  805. return 0;
  806. }