1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
13 CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15
16
17 /* BaseFont implementation for pdfwrite */
18 #include "memory_.h"
19 #include "ctype_.h"
20 #include "gx.h"
21 #include "gserrors.h"
22 #include "gsutil.h" /* for bytes_compare */
23 #include "gxfcid.h"
24 #include "gxfcopy.h"
25 #include "gxfont.h" /* for gxfont42.h */
26 #include "gxfont42.h"
27 #include "gdevpsf.h"
28 #include "gdevpdfx.h"
29 #include "gdevpdfo.h"
30 #include "gdevpdtb.h"
31 #include "gdevpdtf.h"
32 #include "smd5.h"
33 #include "gxfcache.h" /* for gs_purge_font_from_char_caches_completely */
34 /*
35 * Adobe's Distiller Parameters documentation for Acrobat Distiller 5
36 * says that all fonts other than Type 1 are always subsetted; the
37 * documentation for Distiller 4 says that fonts other than Type 1 and
38 * TrueType are subsetted. We do the latter, except that we always
39 * subset large TrueType fonts.
40 */
41 #define MAX_NO_SUBSET_GLYPHS 4096 /* arbitrary */
42
43 /* ---------------- Private ---------------- */
44
45 private_st_pdf_base_font();
46 gs_private_st_basic(st_pdf_base_font, pdf_base_font_t, "pdf_base_font_t",\
47 pdf_base_font_ptrs, pdf_base_font_data);
48
49 #define SUBSET_PREFIX_SIZE 7 /* XXXXXX+ */
50
51 typedef struct pdf_base14_font_info_s {
52 const char *urwname;
53 const char *stdname;
54 } pdf_base14_font_info_t;
55
56 static const pdf_base14_font_info_t base14_font_info[] = {
57 /* Standard mapping of URW fonts */
58 {"NimbusMonL-Regu", "Courier" },
59 {"NimbusMonL-Bold", "Courier-Bold" },
60 {"NimbusMonL-ReguObli", "Courier-Oblique" },
61 {"NimbusMonL-BoldObli", "Courier-BoldOblique" },
62 {"NimbusSanL-Regu", "Helvetica" },
63 {"NimbusSanL-Bold", "Helvetica-Bold" },
64 {"NimbusSanL-ReguItal", "Helvetica-Oblique" },
65 {"NimbusSanL-BoldItal", "Helvetica-BoldOblique"},
66 {"StandardSymL", "Symbol" },
67 {"NimbusRomNo9L-Regu", "Times-Roman" },
68 {"NimbusRomNo9L-Medi", "Times-Bold" },
69 {"NimbusRomNo9L-ReguItal","Times-Italic" },
70 {"NimbusRomNo9L-MediItal","Times-BoldItalic" },
71 {"Dingbats", "ZapfDingbats" },
72 /* A few other mappings of URW fonts */
73 {"NimbusMono-Reg", "Courier" },
74 {"NimbusMono-Bol", "Courier-Bold" },
75 {"NimbusMono-Ita", "Courier-Oblique" },
76 {"NimbusMono-BolIta", "Courier-BoldOblique" },
77 {"NimbusSan-Reg", "Helvetica" },
78 {"NimbusSan-Bol", "Helvetica-Bold" },
79 {"NimbusSan-Ita", "Helvetica-Oblique" },
80 {"NimbusSan-BolIta", "Helvetica-BoldOblique"},
81 {"A030-Reg", "Helvetica" },
82 {"A030-Bol", "Helvetica-Bold" },
83 {"A030-Ita", "Helvetica-Oblique" },
84 {"A030-BolIta", "Helvetica-BoldOblique"},
85 {"NimbusSanNo2-Reg", "Helvetica" },
86 {"NimbusSanNo2-Bol", "Helvetica-Bold" },
87 {"NimbusSanNo2-Ita", "Helvetica-Oblique" },
88 {"NimbusSanNo2-BolIta", "Helvetica-BoldOblique"},
89 {"NimbusRomanNo4-Lig", "Times-Roman" },
90 {"NimbusRomanNo4-Bol", "Times-Bold" },
91 {"NimbusRomanNo4-LigIta", "Times-Italic" },
92 {"NimbusRomanNo4-BolIta", "Times-BoldItalic" },
93 {"NimbusRomanNo9-Reg", "Times-Roman" },
94 {"NimbusRomanNo9-Med", "Times-Bold" },
95 {"NimbusRomanNo9-Ita", "Times-Italic" },
96 {"NimbusRomanNo9-MedIta", "Times-BoldItalic" },
97 {"NimbusRom-Reg", "Times-Roman" },
98 {"NimbusRom-Med", "Times-Bold" },
99 {"NimbusRom-Ita", "Times-Italic" },
100 {"NimbusRom-MedIta", "Times-BoldItalic" },
101 {"NimbusRomNo9-Reg", "Times-Roman" },
102 {"NimbusRomNo9-Bol", "Times-Bold" },
103 {"NimbusRomNo9-Ita", "Times-Italic" },
104 {"NimbusRomNo9-MedIta", "Times-BoldItalic" },
105 {0}
106 };
107
108 /* Given a pointer to a font name, return a pointer to an
109 * equivalent base 14 font name, of NULL if there is no
110 * equivalent.
111 */
pdf_find_base14_name(const byte * str,uint size)112 const char *pdf_find_base14_name(const byte *str, uint size)
113 {
114 const pdf_base14_font_info_t *ppsf;
115
116 for (ppsf = base14_font_info; ppsf->urwname; ++ppsf) {
117 if (strlen(ppsf->urwname) == size) {
118 if (!memcmp(ppsf->urwname, (const char *)str, size))
119 return ppsf->stdname;
120 }
121 }
122 return NULL;
123 }
124
125
126 /*
127 * Determine whether a font is a subset font by examining the name.
128 */
129 bool
pdf_has_subset_prefix(const byte * str,uint size)130 pdf_has_subset_prefix(const byte *str, uint size)
131 {
132 int i;
133
134 if (size < SUBSET_PREFIX_SIZE || str[SUBSET_PREFIX_SIZE - 1] != '+')
135 return false;
136 for (i = 0; i < SUBSET_PREFIX_SIZE - 1; ++i)
137 if ((uint)(str[i] - 'A') >= 26)
138 return false;
139 return true;
140 }
141
142 static inline ulong
hash(ulong v,int index,ushort w)143 hash(ulong v, int index, ushort w)
144 {
145 return v * 3141592653u + w;
146 }
147
148 /*
149 * Add the XXXXXX+ prefix for a subset font.
150 */
151 int
pdf_add_subset_prefix(const gx_device_pdf * pdev,gs_string * pstr,byte * used,int count,char * md5_hash)152 pdf_add_subset_prefix(const gx_device_pdf *pdev, gs_string *pstr, byte *used, int count, char *md5_hash)
153 {
154 uint size = pstr->size;
155 byte *data = gs_resize_string(pdev->pdf_memory, pstr->data, size,
156 size + SUBSET_PREFIX_SIZE,
157 "pdf_add_subset_prefix");
158 int len = (count + 7) / 8;
159 int len0 = len & ~(sizeof(ushort) - 1);
160 ulong v = 0;
161 int i;
162
163 if (data == 0)
164 return_error(gs_error_VMerror);
165
166 if (md5_hash) {
167 for (i = 0; i < 8; i += sizeof(ushort)) {
168 v = hash(v, i, (*(md5_hash + i) | *(md5_hash + i + 1) << 8));
169 }
170 }
171
172 /* Hash the 'used' array. */
173 for (i = 0; i < len0; i += sizeof(ushort))
174 v = hash(v, i, *(ushort *)(used + i));
175 for (; i < len; i++)
176 v = hash(v, i, used[i]);
177
178 memmove(data + SUBSET_PREFIX_SIZE, data, size);
179 for (i = 0; i < SUBSET_PREFIX_SIZE - 1; ++i, v /= 26)
180 data[i] = 'A' + (v % 26);
181 data[SUBSET_PREFIX_SIZE - 1] = '+';
182 pstr->data = data;
183 pstr->size = size + SUBSET_PREFIX_SIZE;
184 return 0;
185 }
186
187 /* Finish writing FontFile* data. */
188 static int
pdf_end_fontfile(gx_device_pdf * pdev,pdf_data_writer_t * pdw)189 pdf_end_fontfile(gx_device_pdf *pdev, pdf_data_writer_t *pdw)
190 {
191 /* We would like to call pdf_end_data,
192 but we don't want to write the object to the output file now. */
193 return pdf_close_aside(pdw->pdev);
194 }
195
copied_font_notify(void * proc_data,void * event_data)196 static int copied_font_notify(void *proc_data, void *event_data)
197 {
198 return gs_purge_font_from_char_caches_completely((gs_font *)proc_data);
199 }
200
201 /* ---------------- Public ---------------- */
202 /*
203 * Allocate and initialize a base font structure, making the required
204 * stable copy/ies of the gs_font. Note that this removes any XXXXXX+
205 * font name prefix from the copy. If is_standard is true, the copy is
206 * a complete one, and adding glyphs or Encoding entries is not allowed.
207 */
208 int
pdf_base_font_alloc(gx_device_pdf * pdev,pdf_base_font_t ** ppbfont,gs_font_base * font,const gs_matrix * orig_matrix,bool is_standard)209 pdf_base_font_alloc(gx_device_pdf *pdev, pdf_base_font_t **ppbfont,
210 gs_font_base *font, const gs_matrix *orig_matrix,
211 bool is_standard)
212 {
213 gs_memory_t *mem = pdev->pdf_memory;
214 gs_font *copied;
215 gs_font *complete;
216 pdf_base_font_t *pbfont =
217 gs_alloc_struct(mem, pdf_base_font_t,
218 &st_pdf_base_font, "pdf_base_font_alloc");
219 const gs_font_name *pfname = &font->font_name;
220 gs_const_string font_name;
221 char fnbuf[2*sizeof(long) + 3]; /* .F########\0 */
222 int code, reserve_glyphs = -1;
223
224 if (pbfont == 0)
225 return_error(gs_error_VMerror);
226 memset(pbfont, 0, sizeof(*pbfont));
227 switch (font->FontType) {
228 case ft_encrypted:
229 case ft_encrypted2:
230 {
231 int index, count;
232 gs_glyph glyph;
233
234 for (index = 0, count = 0;
235 (font->procs.enumerate_glyph((gs_font *)font, &index,
236 GLYPH_SPACE_NAME, &glyph),
237 index != 0);
238 )
239 ++count;
240 pbfont->num_glyphs = count;
241 pbfont->do_subset = (is_standard ? DO_SUBSET_NO : DO_SUBSET_UNKNOWN);
242 }
243 /* If we find an excessively large type 1 font we won't be able to emit
244 * a complete copy. Instead we will emit multiple subsets. Detect that here
245 * and only reserve enough space in the font copy for the maximum subset
246 * glyphs, 257.
247 * This also prevents us making a 'complete' copy of the font below. NB the
248 * value 2048 is merely a guess, intended to prevent copying very large fonts.
249 */
250 if(pbfont->num_glyphs > 2048 && !is_standard) {
251 reserve_glyphs = 257;
252 if(pbfont->do_subset != DO_SUBSET_NO){
253 char buf[gs_font_name_max + 1];
254 int l = min(font->font_name.size, sizeof(buf) - 1);
255
256 memcpy(buf, font->font_name.chars, l);
257 buf[l] = 0;
258 emprintf1(pdev->memory,
259 "Can't embed the complete font %s as it is too large, embedding a subset.\n",
260 buf);
261 }
262 }
263 break;
264 case ft_TrueType:
265 pbfont->num_glyphs = ((gs_font_type42 *)font)->data.trueNumGlyphs;
266 pbfont->do_subset =
267 (pbfont->num_glyphs <= MAX_NO_SUBSET_GLYPHS ?
268 DO_SUBSET_UNKNOWN : DO_SUBSET_YES);
269 break;
270 case ft_CID_encrypted:
271 pbfont->num_glyphs = ((gs_font_cid0 *)font)->cidata.common.CIDCount;
272 goto cid;
273 case ft_CID_TrueType:
274 pbfont->num_glyphs = ((gs_font_cid2 *)font)->cidata.common.CIDCount;
275 cid:
276 pbfont->do_subset = DO_SUBSET_YES;
277 pbfont->CIDSet =
278 gs_alloc_bytes(mem, (pbfont->num_glyphs + 7) / 8,
279 "pdf_base_font_alloc(CIDSet)");
280 if (pbfont->CIDSet == 0) {
281 code = gs_note_error(gs_error_VMerror);
282 goto fail;
283 }
284 pbfont->CIDSetLength = (pbfont->num_glyphs + 7) / 8;
285 memset(pbfont->CIDSet, 0, (pbfont->num_glyphs + 7) / 8);
286 break;
287 default:
288 code = gs_note_error(gs_error_rangecheck);
289 goto fail;
290 }
291
292 code = gs_copy_font((gs_font *)font, orig_matrix, mem, &copied, reserve_glyphs);
293 if (code < 0)
294 goto fail;
295 gs_notify_register(&copied->notify_list, copied_font_notify, copied);
296 {
297 /*
298 * Adobe Technical Note # 5012 "The Type 42 Font Format Specification" says :
299 *
300 * There is a known bug in the TrueType rasterizer included in versions of the
301 * PostScript interpreter previous to version 2013. The problem is that the
302 * translation components of the FontMatrix, as used as an argument to the
303 * definefont or makefont operators, are ignored. Translation of user space is
304 * not affected by this bug.
305 *
306 * Besides that, we found that Adobe Acrobat Reader 4 and 5 ignore
307 * FontMatrix.ty .
308 */
309 copied->FontMatrix.tx = copied->FontMatrix.ty = 0;
310 }
311
312 if (pbfont->do_subset != DO_SUBSET_YES && reserve_glyphs == -1) {
313 /* The only possibly non-subsetted fonts are Type 1/2 and Type 42. */
314 if (is_standard)
315 complete = copied;
316 else {
317 code = gs_copy_font((gs_font *)font, &font->FontMatrix, mem, &complete, -1);
318 if (code < 0)
319 goto fail;
320 }
321 code = gs_copy_font_complete((gs_font *)font, complete);
322 if (code < 0 && pbfont->do_subset == DO_SUBSET_NO) {
323 char buf[gs_font_name_max + 1];
324 int l = min(copied->font_name.size, sizeof(buf) - 1);
325
326 memcpy(buf, copied->font_name.chars, l);
327 buf[l] = 0;
328 emprintf1(pdev->memory,
329 "Can't embed the complete font %s due to font error.\n",
330 buf);
331 goto fail;
332 }
333 if (code < 0) {
334 /* A font error happened, but it may be caused by a glyph,
335 which is not used in the document. Continue with subsetting the font.
336 If the failed glyph will be used in the document,
337 another error will hgappen when the glyph is used.
338 */
339 gs_free_copied_font(complete);
340 complete = copied;
341 }
342 } else
343 complete = copied;
344 pbfont->copied = (gs_font_base *)copied;
345 pbfont->complete = (gs_font_base *)complete;
346
347 /* replace the font cache of the copied fonts with our own font cache
348 * this is required for PCL, see 'pdf_free_pdf_font_cache' in gdevpdf.c
349 * for further details.
350 */
351 pbfont->copied->dir = pbfont->complete->dir = pdev->pdf_font_dir;
352 pbfont->is_standard = is_standard;
353 if (pfname->size > 0) {
354 font_name.data = pfname->chars;
355 font_name.size = pfname->size;
356 while (pdf_has_subset_prefix(font_name.data, font_name.size)) {
357 /* Strip off an existing subset prefix. */
358 font_name.data += SUBSET_PREFIX_SIZE;
359 font_name.size -= SUBSET_PREFIX_SIZE;
360 }
361 } else {
362 gs_sprintf(fnbuf, ".F%lx", (ulong)copied);
363 font_name.data = (byte *)fnbuf;
364 font_name.size = strlen(fnbuf);
365 }
366 pbfont->font_name.data =
367 gs_alloc_string(mem, font_name.size, "pdf_base_font_alloc(font_name)");
368 if (pbfont->font_name.data == 0)
369 goto fail;
370 memcpy(pbfont->font_name.data, font_name.data, font_name.size);
371 pbfont->font_name.size = font_name.size;
372 *ppbfont = pbfont;
373 return 0;
374 fail:
375 gs_free_object(mem, pbfont, "pdf_base_font_alloc");
376 return code;
377 }
378
379 /*
380 * Return a reference to the name of a base font. This name is guaranteed
381 * not to have a XXXXXX+ prefix. The client may change the name at will,
382 * but must not add a XXXXXX+ prefix.
383 */
384 gs_string *
pdf_base_font_name(pdf_base_font_t * pbfont)385 pdf_base_font_name(pdf_base_font_t *pbfont)
386 {
387 return &pbfont->font_name;
388 }
389
390 /*
391 * Return the (copied, subset) font associated with a base font.
392 * This procedure probably shouldn't exist....
393 */
394 gs_font_base *
pdf_base_font_font(const pdf_base_font_t * pbfont,bool complete)395 pdf_base_font_font(const pdf_base_font_t *pbfont, bool complete)
396 {
397 return (complete ? pbfont->complete : pbfont->copied);
398 }
399
400 /*
401 * Check for subset font.
402 */
403 bool
pdf_base_font_is_subset(const pdf_base_font_t * pbfont)404 pdf_base_font_is_subset(const pdf_base_font_t *pbfont)
405 {
406 return pbfont->do_subset == DO_SUBSET_YES;
407 }
408
409 /*
410 * Drop the copied complete font associated with a base font.
411 */
412 void
pdf_base_font_drop_complete(pdf_base_font_t * pbfont)413 pdf_base_font_drop_complete(pdf_base_font_t *pbfont)
414 {
415 pbfont->complete = NULL;
416 }
417
418 /*
419 * Copy a glyph (presumably one that was just used) into a saved base
420 * font. Note that it is the client's responsibility to determine that
421 * the source font is compatible with the target font. (Normally they
422 * will be the same.)
423 */
424 int
pdf_base_font_copy_glyph(pdf_base_font_t * pbfont,gs_glyph glyph,gs_font_base * font)425 pdf_base_font_copy_glyph(pdf_base_font_t *pbfont, gs_glyph glyph,
426 gs_font_base *font)
427 {
428 int code;
429
430 /* If we're a TrueType CIDFont, check teh GSUB table for replacement glyphs.
431 * Bug #691574
432 */
433 if (font->FontType == ft_CID_TrueType) {
434 code =
435 gs_copy_glyph_options((gs_font *)font, glyph,
436 (gs_font *)pbfont->copied,
437 (pbfont->is_standard ? COPY_GLYPH_NO_NEW : COPY_GLYPH_USE_GSUB));
438 } else {
439 code =
440 gs_copy_glyph_options((gs_font *)font, glyph,
441 (gs_font *)pbfont->copied,
442 (pbfont->is_standard ? COPY_GLYPH_NO_NEW : 0));
443 }
444 if (code < 0)
445 return code;
446 if (pbfont->CIDSet != 0 &&
447 (uint)(glyph - GS_MIN_CID_GLYPH) < pbfont->num_glyphs
448 ) {
449 uint cid = glyph - GS_MIN_CID_GLYPH;
450
451 pbfont->CIDSet[cid >> 3] |= 0x80 >> (cid & 7);
452 }
453 return 0;
454 }
455
456 /*
457 * Determine whether a font should be subsetted.
458 */
459 bool
pdf_do_subset_font(gx_device_pdf * pdev,pdf_base_font_t * pbfont,gs_id rid)460 pdf_do_subset_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont, gs_id rid)
461 {
462 gs_font_base *copied = pbfont->copied;
463
464 /*
465 * If the decision has not been made already, determine whether or not
466 * to subset the font.
467 */
468 if (pbfont->do_subset == DO_SUBSET_UNKNOWN) {
469 int max_pct = pdev->params.MaxSubsetPct;
470 bool do_subset = pdev->params.SubsetFonts && max_pct > 0;
471
472 if (do_subset && max_pct < 100) {
473 /* We want to subset iff used <= total * MaxSubsetPct / 100. */
474 do_subset = false;
475 if (max_pct > 0) {
476 int max_subset_used = pbfont->num_glyphs * max_pct / 100;
477 int used, index;
478 gs_glyph ignore_glyph;
479
480 do_subset = true;
481 for (index = 0, used = 0;
482 (copied->procs.enumerate_glyph((gs_font *)copied,
483 &index, GLYPH_SPACE_INDEX,
484 &ignore_glyph), index != 0);
485 )
486 if (++used > max_subset_used) {
487 do_subset = false;
488 break;
489 }
490 }
491 }
492 pbfont->do_subset = (do_subset ? DO_SUBSET_YES : DO_SUBSET_NO);
493 }
494 return (pbfont->do_subset == DO_SUBSET_YES);
495 }
496
497 /*
498 * Write the FontFile entry for an embedded font, /FontFile<n> # # R.
499 */
500 int
pdf_write_FontFile_entry(gx_device_pdf * pdev,pdf_base_font_t * pbfont)501 pdf_write_FontFile_entry(gx_device_pdf *pdev, pdf_base_font_t *pbfont)
502 {
503 stream *s = pdev->strm;
504 const char *FontFile_key;
505
506 switch (pbfont->copied->FontType) {
507 case ft_TrueType:
508 case ft_CID_TrueType:
509 FontFile_key = "/FontFile2";
510 break;
511 default: /* Type 1/2, CIDFontType 0 */
512 if (!pdev->HaveCFF)
513 FontFile_key = "/FontFile";
514 else
515 FontFile_key = "/FontFile3";
516 }
517 stream_puts(s, FontFile_key);
518 pprintld1(s, " %ld 0 R", pbfont->FontFile->id);
519 return 0;
520 }
521
522 /*
523 * Adjust font name for Acrobat Reader 3.
524 */
525 static int
pdf_adjust_font_name(gx_device_pdf * pdev,long id,pdf_base_font_t * pbfont)526 pdf_adjust_font_name(gx_device_pdf *pdev, long id, pdf_base_font_t *pbfont)
527 {
528 /*
529 * In contradiction with previous version of pdfwrite,
530 * this always adds a suffix. We don't check the original name
531 * for uniquity bacause the layered architecture
532 * (see gdevpdtx.h) doesn't provide an easy access for
533 * related information.
534 */
535 int i;
536 byte *chars = (byte *)pbfont->font_name.data; /* break 'const' */
537 byte *data;
538 uint size = pbfont->font_name.size;
539 char suffix[sizeof(long) * 2 + 2];
540 uint suffix_size;
541
542 #define SUFFIX_CHAR '~'
543 /*
544 * If the name looks as though it has one of our unique suffixes,
545 * remove the suffix.
546 */
547 for (i = size;
548 i > 0 && isxdigit(chars[i - 1]);
549 --i)
550 DO_NOTHING;
551 if (i < size && i > 0 && chars[i - 1] == SUFFIX_CHAR) {
552 do {
553 --i;
554 } while (i > 0 && chars[i - 1] == SUFFIX_CHAR);
555 size = i + 1;
556 }
557 /* Create a unique name. */
558 gs_sprintf(suffix, "%c%lx", SUFFIX_CHAR, id);
559 suffix_size = strlen(suffix);
560 data = gs_resize_string(pdev->pdf_memory, chars, size,
561 size + suffix_size,
562 "pdf_adjust_font_name");
563 if (data == 0)
564 return_error(gs_error_VMerror);
565 memcpy(data + size, (const byte *)suffix, suffix_size);
566 pbfont->font_name.data = data;
567 pbfont->font_name.size = size + suffix_size;
568 #undef SUFFIX_CHAR
569 return 0;
570 }
571
572 /*
573 * Write an embedded font.
574 */
575 int
pdf_write_embedded_font(gx_device_pdf * pdev,pdf_base_font_t * pbfont,font_type FontType,gs_int_rect * FontBBox,gs_id rid,cos_dict_t ** ppcd)576 pdf_write_embedded_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont, font_type FontType,
577 gs_int_rect *FontBBox, gs_id rid, cos_dict_t **ppcd)
578 {
579 bool do_subset = pdf_do_subset_font(pdev, pbfont, rid);
580 gs_font_base *out_font =
581 (do_subset || pbfont->complete == NULL ? pbfont->copied : pbfont->complete);
582 gs_const_string fnstr;
583 pdf_data_writer_t writer;
584 byte digest[6] = {0,0,0,0,0,0};
585 int code = 0;
586 int options=0;
587
588 if (pbfont->written)
589 return 0; /* already written */
590 code = copied_order_font((gs_font *)out_font);
591 if (code < 0)
592 return code;
593 /* Since we now always ASCIIHex encode the eexec encrypted portion of a
594 * Type 1 font, such a font cannot contain any binary data, if its not being
595 * compressed then there is no reason to ASCII encode it (which will happen
596 * we set DATA_STREAM_BINARY and the device does not permit binary output)
597 * NB if HaveCFF is true then we convert type 1 to CFF which is a binary
598 * format, so we still need to set DATA_STREAM_BINARY.
599 */
600 if (pdev->CompressFonts)
601 options = DATA_STREAM_BINARY | DATA_STREAM_COMPRESS;
602 else
603 if (FontType != ft_encrypted || pdev->HaveCFF)
604 options = DATA_STREAM_BINARY;
605 /* Don't set DATA_STREAM_ENCRYPT since we write to a temporary file.
606 * See comment in pdf_begin_encrypt.
607 */
608 code = pdf_begin_data_stream(pdev, &writer, options, 0);
609 if (code < 0)
610 return code;
611 if (pdev->PDFA != 0) {
612 stream *s = s_MD5C_make_stream(pdev->pdf_memory, writer.binary.strm);
613
614 if (s == NULL)
615 return_error(gs_error_VMerror);
616 writer.binary.strm = s;
617 }
618 if (pdev->CompatibilityLevel == 1.2 &&
619 !do_subset && !pbfont->is_standard ) {
620 /*
621 * Due to a bug in Acrobat Reader 3, we need to generate
622 * unique font names, except base 14 fonts being not embedded.
623 * To recognize base 14 fonts here we used the knowledge
624 * that pbfont->is_standard is true for base 14 fonts only.
625 * Note that subsetted fonts already have an unique name
626 * due to subset prefix.
627 */
628 int code = pdf_adjust_font_name(pdev, writer.pres->object->id, pbfont);
629 if (code < 0)
630 return code;
631 }
632 fnstr.data = pbfont->font_name.data;
633 fnstr.size = pbfont->font_name.size;
634 /* Now write the font (or subset). */
635 switch (FontType) {
636
637 case ft_composite:
638 /* Nothing to embed -- the descendant fonts do it all. */
639 code = 0;
640 break;
641
642 case ft_encrypted2:
643 if (!pdev->HaveCFF) {
644 /* Must convert to Type 1 charstrings. */
645 return_error(gs_error_unregistered); /* Not implemented yet. */
646 }
647 case ft_encrypted:
648 if (pdev->HavePDFWidths) {
649 code = copied_drop_extension_glyphs((gs_font *)out_font);
650 if (code < 0)
651 return code;
652 }
653 if (!pdev->HaveCFF) {
654 /* Write the type 1 font with no converting to CFF. */
655 int lengths[3];
656
657 code = psf_write_type1_font(writer.binary.strm,
658 (gs_font_type1 *)out_font,
659 WRITE_TYPE1_WITH_LENIV | WRITE_TYPE1_EEXEC |
660 WRITE_TYPE1_EEXEC_PAD | WRITE_TYPE1_ASCIIHEX,
661 NULL, 0, &fnstr, lengths);
662 if (lengths[0] > 0) {
663 if (code < 0)
664 goto finish;
665 code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object,
666 "/Length1", lengths[0]);
667 }
668 if (lengths[1] > 0) {
669 if (code < 0)
670 goto finish;
671 code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object,
672 "/Length2", lengths[1]);
673 if (code < 0)
674 return code;
675 code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object,
676 "/Length3", lengths[2]);
677 }
678 } else {
679 /*
680 * Since we only support PDF 1.2 and later, always write Type 1
681 * fonts as Type1C (Type 2). Acrobat Reader apparently doesn't
682 * accept CFF fonts with Type 1 CharStrings, so we need to convert
683 * them. Also remove lenIV, so Type 2 fonts will compress better.
684 */
685 #define TYPE2_OPTIONS (WRITE_TYPE2_NO_LENIV | WRITE_TYPE2_CHARSTRINGS)
686 code = cos_dict_put_string_copy((cos_dict_t *)writer.pres->object, "/Subtype", "/Type1C");
687 if (code < 0)
688 return code;
689 code = psf_write_type2_font(writer.binary.strm,
690 (gs_font_type1 *)out_font,
691 TYPE2_OPTIONS |
692 (pdev->CompatibilityLevel < 1.3 ? WRITE_TYPE2_AR3 : 0),
693 NULL, 0, &fnstr, FontBBox);
694 }
695 goto finish;
696
697 case ft_TrueType: {
698 gs_font_type42 *const pfont = (gs_font_type42 *)out_font;
699 #define TRUETYPE_OPTIONS (WRITE_TRUETYPE_NAME | WRITE_TRUETYPE_HVMTX)
700 /* Acrobat Reader 3 doesn't handle cmap format 6 correctly. */
701 const int options = TRUETYPE_OPTIONS |
702 (pdev->PDFA != 0 ? WRITE_TRUETYPE_UNICODE_CMAP : 0) |
703 (pdev->CompatibilityLevel <= 1.2 ?
704 WRITE_TRUETYPE_NO_TRIMMED_TABLE : 0) |
705 /* Generate a cmap only for incrementally downloaded fonts
706 and for subsetted fonts. */
707 (pfont->data.numGlyphs != pfont->data.trueNumGlyphs ||
708 pbfont->do_subset == DO_SUBSET_YES ?
709 WRITE_TRUETYPE_CMAP : 0);
710 stream poss;
711
712 if (pdev->HavePDFWidths) {
713 code = copied_drop_extension_glyphs((gs_font *)out_font);
714 if (code < 0)
715 return code;
716 }
717 s_init(&poss, pdev->memory);
718 swrite_position_only(&poss);
719 code = psf_write_truetype_font(&poss, pfont, options, NULL, 0, &fnstr);
720 if (code < 0)
721 return code;
722 code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object, "/Length1", stell(&poss));
723 if (code < 0)
724 return code;
725 code = psf_write_truetype_font(writer.binary.strm, pfont,
726 options, NULL, 0, &fnstr);
727 goto finish;
728 }
729
730 case ft_CID_encrypted:
731 code = cos_dict_put_string_copy((cos_dict_t *)writer.pres->object, "/Subtype", "/CIDFontType0C");
732 if (code < 0)
733 return code;
734 code = psf_write_cid0_font(writer.binary.strm,
735 (gs_font_cid0 *)out_font, TYPE2_OPTIONS,
736 NULL, 0, &fnstr);
737 goto finish;
738
739 case ft_CID_TrueType:
740 /* CIDFontType 2 fonts don't use cmap, name, OS/2, or post. */
741 #define CID2_OPTIONS WRITE_TRUETYPE_HVMTX
742 code = psf_write_cid2_font(writer.binary.strm,
743 (gs_font_cid2 *)out_font,
744 CID2_OPTIONS, NULL, 0, &fnstr);
745 finish:
746 if (pdev->PDFA != 0) {
747 sflush(writer.binary.strm);
748 s_MD5C_get_digest(writer.binary.strm, digest, sizeof(digest));
749 }
750 *ppcd = (cos_dict_t *)writer.pres->object;
751 if (code < 0) {
752 pdf_end_fontfile(pdev, &writer);
753 pdf_obj_mark_unused(pdev, writer.pres->object->id);
754 return code;
755 }
756 code = pdf_end_fontfile(pdev, &writer);
757 break;
758
759 default:
760 code = gs_note_error(gs_error_rangecheck);
761 }
762
763 pbfont->written = true;
764 return code;
765 }
766
767 /*
768 * Write the CharSet for a subsetted font, as a PDF string.
769 */
770 int
pdf_write_CharSet(gx_device_pdf * pdev,pdf_base_font_t * pbfont)771 pdf_write_CharSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont)
772 {
773 stream *s = pdev->strm;
774 gs_font_base *font = pbfont->copied;
775 int index;
776 gs_glyph glyph;
777
778 stream_puts(s, "(");
779 for (index = 0;
780 (font->procs.enumerate_glyph((gs_font *)font, &index,
781 GLYPH_SPACE_NAME, &glyph),
782 index != 0);
783 ) {
784 gs_const_string gstr;
785 int code = font->procs.glyph_name((gs_font *)font, glyph, &gstr);
786
787 /* Don't include .notdef. */
788 if (code >= 0 &&
789 bytes_compare(gstr.data, gstr.size, (const byte *)".notdef", 7)
790 )
791 pdf_put_name(pdev, gstr.data, gstr.size);
792 }
793 stream_puts(s, ")");
794 return 0;
795 }
796
797 /*
798 * Write the CIDSet object for a subsetted CIDFont.
799 */
800 int
pdf_write_CIDSet(gx_device_pdf * pdev,pdf_base_font_t * pbfont,long * pcidset_id)801 pdf_write_CIDSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont,
802 long *pcidset_id)
803 {
804 pdf_data_writer_t writer;
805 int code;
806
807 code = pdf_begin_data_stream(pdev, &writer,
808 DATA_STREAM_BINARY |
809 (pdev->CompressFonts ? DATA_STREAM_COMPRESS : 0),
810 gs_no_id);
811 if (code < 0)
812 return code;
813 stream_write(writer.binary.strm, pbfont->CIDSet,
814 pbfont->CIDSetLength);
815 code = pdf_end_data(&writer);
816 if (code < 0)
817 return code;
818 *pcidset_id = pdf_resource_id(writer.pres);
819 return 0;
820 }
821 /*
822 * Check whether a base font is standard.
823 */
824 bool
pdf_is_standard_font(pdf_base_font_t * bfont)825 pdf_is_standard_font(pdf_base_font_t *bfont)
826 { return bfont->is_standard;
827 }
828
829 void
pdf_set_FontFile_object(pdf_base_font_t * bfont,cos_dict_t * pcd)830 pdf_set_FontFile_object(pdf_base_font_t *bfont, cos_dict_t *pcd)
831 {
832 bfont->FontFile = pcd;
833 }
834 const cos_dict_t *
pdf_get_FontFile_object(pdf_base_font_t * bfont)835 pdf_get_FontFile_object(pdf_base_font_t *bfont)
836 {
837 return bfont->FontFile;
838 }
839