1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gdevpdtb.c 8622 2008-04-04 08:53:57Z ken $ */
15
/* BaseFont implementation for pdfwrite */
21
#include "gsutil.h" /* for bytes_compare */
24
#include "gxfont.h" /* for gxfont42.h */
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.
41
#define MAX_NO_SUBSET_GLYPHS 500 /* arbitrary */
43
/* ---------------- Private ---------------- */
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);
49
#define SUBSET_PREFIX_SIZE 7 /* XXXXXX+ */
52
* Determine whether a font is a subset font by examining the name.
55
pdf_has_subset_prefix(const byte *str, uint size)
59
if (size < SUBSET_PREFIX_SIZE || str[SUBSET_PREFIX_SIZE - 1] != '+')
61
for (i = 0; i < SUBSET_PREFIX_SIZE - 1; ++i)
62
if ((uint)(str[i] - 'A') >= 26)
68
hash(ulong v, int index, ushort w)
70
return v * 3141592653u + w;
74
* Add the XXXXXX+ prefix for a subset font.
77
pdf_add_subset_prefix(const gx_device_pdf *pdev, gs_string *pstr, byte *used, int count)
79
uint size = pstr->size;
80
byte *data = gs_resize_string(pdev->pdf_memory, pstr->data, size,
81
size + SUBSET_PREFIX_SIZE,
82
"pdf_add_subset_prefix");
83
int len = (count + 7) / 8;
84
int len0 = len & ~(sizeof(ushort) - 1);
89
return_error(gs_error_VMerror);
91
/* Hash the 'used' array. */
92
for (i = 0; i < len0; i += sizeof(ushort))
93
v = hash(v, i, *(ushort *)(used + i));
95
v = hash(v, i, used[i]);
97
memmove(data + SUBSET_PREFIX_SIZE, data, size);
98
for (i = 0; i < SUBSET_PREFIX_SIZE - 1; ++i, v /= 26)
99
data[i] = 'A' + (v % 26);
100
data[SUBSET_PREFIX_SIZE - 1] = '+';
102
pstr->size = size + SUBSET_PREFIX_SIZE;
106
/* Finish writing FontFile* data. */
108
pdf_end_fontfile(gx_device_pdf *pdev, pdf_data_writer_t *pdw)
110
/* We would like to call pdf_end_data,
111
but we don't want to write the object to the output file now. */
112
return pdf_close_aside(pdw->pdev);
115
/* ---------------- Public ---------------- */
118
* Allocate and initialize a base font structure, making the required
119
* stable copy/ies of the gs_font. Note that this removes any XXXXXX+
120
* font name prefix from the copy. If is_standard is true, the copy is
121
* a complete one, and adding glyphs or Encoding entries is not allowed.
124
pdf_base_font_alloc(gx_device_pdf *pdev, pdf_base_font_t **ppbfont,
125
gs_font_base *font, const gs_matrix *orig_matrix,
128
gs_memory_t *mem = pdev->pdf_memory;
131
pdf_base_font_t *pbfont =
132
gs_alloc_struct(mem, pdf_base_font_t,
133
&st_pdf_base_font, "pdf_base_font_alloc");
134
const gs_font_name *pfname = &font->font_name;
135
gs_const_string font_name;
136
char fnbuf[3 + sizeof(long) / 3 + 1]; /* .F#######\0 */
140
return_error(gs_error_VMerror);
141
code = gs_copy_font((gs_font *)font, orig_matrix, mem, &copied);
144
memset(pbfont, 0, sizeof(*pbfont));
147
* Adobe Technical Note # 5012 "The Type 42 Font Format Specification" says :
149
* There is a known bug in the TrueType rasterizer included in versions of the
150
* PostScript interpreter previous to version 2013. The problem is that the
151
* translation components of the FontMatrix, as used as an argument to the
152
* definefont or makefont operators, are ignored. Translation of user space is
153
* not affected by this bug.
155
* Besides that, we found that Adobe Acrobat Reader 4 and 5 ignore
158
copied->FontMatrix.tx = copied->FontMatrix.ty = 0;
160
switch (font->FontType) {
163
pbfont->do_subset = (is_standard ? DO_SUBSET_NO : DO_SUBSET_UNKNOWN);
164
/* We will count the number of glyphs below. */
165
pbfont->num_glyphs = -1;
168
pbfont->num_glyphs = ((gs_font_type42 *)font)->data.trueNumGlyphs;
170
(pbfont->num_glyphs <= MAX_NO_SUBSET_GLYPHS ?
171
DO_SUBSET_UNKNOWN : DO_SUBSET_YES);
173
case ft_CID_encrypted:
174
pbfont->num_glyphs = ((gs_font_cid0 *)font)->cidata.common.CIDCount;
176
case ft_CID_TrueType:
177
pbfont->num_glyphs = ((gs_font_cid2 *)font)->cidata.common.CIDCount;
179
pbfont->do_subset = DO_SUBSET_YES;
181
gs_alloc_bytes(mem, (pbfont->num_glyphs + 7) / 8,
182
"pdf_base_font_alloc(CIDSet)");
183
if (pbfont->CIDSet == 0) {
184
code = gs_note_error(gs_error_VMerror);
187
memset(pbfont->CIDSet, 0, (pbfont->num_glyphs + 7) / 8);
190
code = gs_note_error(gs_error_rangecheck);
193
if (pbfont->do_subset != DO_SUBSET_YES) {
194
/* The only possibly non-subsetted fonts are Type 1/2 and Type 42. */
196
complete = copied, code = 0;
198
code = gs_copy_font((gs_font *)font, &font->FontMatrix, mem, &complete);
202
code = gs_copy_font_complete((gs_font *)font, complete);
203
if (code < 0 && pbfont->do_subset == DO_SUBSET_NO) {
204
char buf[gs_font_name_max + 1];
205
int l = min(copied->font_name.size, sizeof(buf) - 1);
207
memcpy(buf, copied->font_name.chars, l);
209
eprintf1("Can't embed the complete font %s due to font error.\n", buf);
213
/* A font error happened, but it may be caused by a glyph,
214
which is not used in the document. Continue with subsetting the font.
215
If the failed glyph will be used in the document,
216
another error will hgappen when the glyph is used.
219
} else if (pbfont->num_glyphs < 0) { /* Type 1 */
223
for (index = 0, count = 0;
224
(font->procs.enumerate_glyph((gs_font *)font, &index,
225
GLYPH_SPACE_NAME, &glyph),
229
pbfont->num_glyphs = count;
233
pbfont->copied = (gs_font_base *)copied;
234
pbfont->complete = (gs_font_base *)complete;
235
pbfont->is_standard = is_standard;
236
if (pfname->size > 0) {
237
font_name.data = pfname->chars;
238
font_name.size = pfname->size;
239
while (pdf_has_subset_prefix(font_name.data, font_name.size)) {
240
/* Strip off an existing subset prefix. */
241
font_name.data += SUBSET_PREFIX_SIZE;
242
font_name.size -= SUBSET_PREFIX_SIZE;
245
sprintf(fnbuf, ".F%lx", (ulong)copied);
246
font_name.data = (byte *)fnbuf;
247
font_name.size = strlen(fnbuf);
249
pbfont->font_name.data =
250
gs_alloc_string(mem, font_name.size, "pdf_base_font_alloc(font_name)");
251
if (pbfont->font_name.data == 0)
253
memcpy(pbfont->font_name.data, font_name.data, font_name.size);
254
pbfont->font_name.size = font_name.size;
258
gs_free_object(mem, pbfont, "pdf_base_font_alloc");
263
* Return a reference to the name of a base font. This name is guaranteed
264
* not to have a XXXXXX+ prefix. The client may change the name at will,
265
* but must not add a XXXXXX+ prefix.
268
pdf_base_font_name(pdf_base_font_t *pbfont)
270
return &pbfont->font_name;
274
* Return the (copied, subset) font associated with a base font.
275
* This procedure probably shouldn't exist....
278
pdf_base_font_font(const pdf_base_font_t *pbfont, bool complete)
280
return (complete ? pbfont->complete : pbfont->copied);
284
* Check for subset font.
287
pdf_base_font_is_subset(const pdf_base_font_t *pbfont)
289
return pbfont->do_subset == DO_SUBSET_YES;
293
* Drop the copied complete font associated with a base font.
296
pdf_base_font_drop_complete(pdf_base_font_t *pbfont)
298
pbfont->complete = NULL;
302
* Copy a glyph (presumably one that was just used) into a saved base
303
* font. Note that it is the client's responsibility to determine that
304
* the source font is compatible with the target font. (Normally they
308
pdf_base_font_copy_glyph(pdf_base_font_t *pbfont, gs_glyph glyph,
312
gs_copy_glyph_options((gs_font *)font, glyph,
313
(gs_font *)pbfont->copied,
314
(pbfont->is_standard ? COPY_GLYPH_NO_NEW : 0));
318
if (pbfont->CIDSet != 0 &&
319
(uint)(glyph - GS_MIN_CID_GLYPH) < pbfont->num_glyphs
321
uint cid = glyph - GS_MIN_CID_GLYPH;
323
pbfont->CIDSet[cid >> 3] |= 0x80 >> (cid & 7);
329
* Determine whether a font should be subsetted.
332
pdf_do_subset_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont, gs_id rid)
334
gs_font_base *copied = pbfont->copied;
337
* If the decision has not been made already, determine whether or not
338
* to subset the font.
340
if (pbfont->do_subset == DO_SUBSET_UNKNOWN) {
341
int max_pct = pdev->params.MaxSubsetPct;
342
bool do_subset = pdev->params.SubsetFonts && max_pct > 0;
344
if (do_subset && max_pct < 100) {
345
/* We want to subset iff used <= total * MaxSubsetPct / 100. */
348
int max_subset_used = pbfont->num_glyphs * max_pct / 100;
350
gs_glyph ignore_glyph;
353
for (index = 0, used = 0;
354
(copied->procs.enumerate_glyph((gs_font *)copied,
355
&index, GLYPH_SPACE_INDEX,
356
&ignore_glyph), index != 0);
358
if (++used > max_subset_used) {
364
pbfont->do_subset = (do_subset ? DO_SUBSET_YES : DO_SUBSET_NO);
366
return (pbfont->do_subset == DO_SUBSET_YES);
370
* Write the FontFile entry for an embedded font, /FontFile<n> # # R.
373
pdf_write_FontFile_entry(gx_device_pdf *pdev, pdf_base_font_t *pbfont)
375
stream *s = pdev->strm;
376
const char *FontFile_key;
378
switch (pbfont->copied->FontType) {
380
case ft_CID_TrueType:
381
FontFile_key = "/FontFile2";
383
default: /* Type 1/2, CIDFontType 0 */
384
if (pdev->ResourcesBeforeUsage)
385
FontFile_key = "/FontFile";
387
FontFile_key = "/FontFile3";
389
stream_puts(s, FontFile_key);
390
pprintld1(s, " %ld 0 R", pbfont->FontFile->id);
395
* Adjust font name for Acrobat Reader 3.
398
pdf_adjust_font_name(gx_device_pdf *pdev, long id, pdf_base_font_t *pbfont)
401
* In contradiction with previous version of pdfwrite,
402
* this always adds a suffix. We don't check the original name
403
* for uniquity bacause the layered architecture
404
* (see gdevpdtx.h) doesn't provide an easy access for
405
* related information.
408
byte *chars = (byte *)pbfont->font_name.data; /* break 'const' */
410
uint size = pbfont->font_name.size;
411
char suffix[sizeof(long) * 2 + 2];
414
#define SUFFIX_CHAR '~'
416
* If the name looks as though it has one of our unique suffixes,
420
i > 0 && isxdigit(chars[i - 1]);
423
if (i < size && i > 0 && chars[i - 1] == SUFFIX_CHAR) {
426
} while (i > 0 && chars[i - 1] == SUFFIX_CHAR);
429
/* Create a unique name. */
430
sprintf(suffix, "%c%lx", SUFFIX_CHAR, id);
431
suffix_size = strlen(suffix);
432
data = gs_resize_string(pdev->pdf_memory, chars, size,
434
"pdf_adjust_font_name");
436
return_error(gs_error_VMerror);
437
memcpy(data + size, (const byte *)suffix, suffix_size);
438
pbfont->font_name.data = data;
439
pbfont->font_name.size = size + suffix_size;
445
* Write an embedded font.
448
pdf_write_embedded_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont, font_type FontType,
449
gs_int_rect *FontBBox, gs_id rid, cos_dict_t **ppcd)
451
bool do_subset = pdf_do_subset_font(pdev, pbfont, rid);
452
gs_font_base *out_font =
453
(do_subset || pbfont->complete == NULL ? pbfont->copied : pbfont->complete);
454
gs_const_string fnstr;
455
pdf_data_writer_t writer;
456
byte digest[6] = {0,0,0,0,0,0};
460
return 0; /* already written */
461
code = copied_order_font((gs_font *)out_font);
464
code = pdf_begin_data_stream(pdev, &writer, DATA_STREAM_BINARY |
465
/* Don't set DATA_STREAM_ENCRYPT since we write to a temporary file.
466
See comment in pdf_begin_encrypt. */
467
(pdev->CompressFonts ?
468
DATA_STREAM_COMPRESS : 0), 0);
472
stream *s = s_MD5C_make_stream(pdev->pdf_memory, writer.binary.strm);
475
return_error(gs_error_VMerror);
476
writer.binary.strm = s;
478
if (pdev->CompatibilityLevel == 1.2 &&
479
!do_subset && !pbfont->is_standard ) {
481
* Due to a bug in Acrobat Reader 3, we need to generate
482
* unique font names, except base 14 fonts being not embedded.
483
* To recognize base 14 fonts here we used the knowledge
484
* that pbfont->is_standard is true for base 14 fonts only.
485
* Note that subsetted fonts already have an unique name
486
* due to subset prefix.
488
int code = pdf_adjust_font_name(pdev, writer.pres->object->id, pbfont);
492
fnstr.data = pbfont->font_name.data;
493
fnstr.size = pbfont->font_name.size;
494
/* Now write the font (or subset). */
498
/* Nothing to embed -- the descendant fonts do it all. */
503
if (!pdev->HaveCFF) {
504
/* Must convert to Type 1 charstrings. */
505
return_error(gs_error_unregistered); /* Not implemented yet. */
508
if (pdev->HavePDFWidths) {
509
code = copied_drop_extension_glyphs((gs_font *)out_font);
513
if (!pdev->HaveCFF) {
514
/* Write the type 1 font with no converting to CFF. */
517
code = psf_write_type1_font(writer.binary.strm,
518
(gs_font_type1 *)out_font,
519
WRITE_TYPE1_WITH_LENIV |
520
WRITE_TYPE1_EEXEC | WRITE_TYPE1_EEXEC_PAD,
521
NULL, 0, &fnstr, lengths);
522
if (lengths[0] > 0) {
525
code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object,
526
"/Length1", lengths[0]);
528
if (lengths[1] > 0) {
531
code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object,
532
"/Length2", lengths[1]);
535
code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object,
536
"/Length3", lengths[2]);
540
* Since we only support PDF 1.2 and later, always write Type 1
541
* fonts as Type1C (Type 2). Acrobat Reader apparently doesn't
542
* accept CFF fonts with Type 1 CharStrings, so we need to convert
543
* them. Also remove lenIV, so Type 2 fonts will compress better.
545
#define TYPE2_OPTIONS (WRITE_TYPE2_NO_LENIV | WRITE_TYPE2_CHARSTRINGS)
546
code = cos_dict_put_string_copy((cos_dict_t *)writer.pres->object, "/Subtype", "/Type1C");
549
code = psf_write_type2_font(writer.binary.strm,
550
(gs_font_type1 *)out_font,
552
(pdev->CompatibilityLevel < 1.3 ? WRITE_TYPE2_AR3 : 0),
553
NULL, 0, &fnstr, FontBBox);
558
gs_font_type42 *const pfont = (gs_font_type42 *)out_font;
559
#define TRUETYPE_OPTIONS (WRITE_TRUETYPE_NAME | WRITE_TRUETYPE_HVMTX)
560
/* Acrobat Reader 3 doesn't handle cmap format 6 correctly. */
561
const int options = TRUETYPE_OPTIONS |
562
(pdev->PDFA ? WRITE_TRUETYPE_UNICODE_CMAP : 0) |
563
(pdev->CompatibilityLevel <= 1.2 ?
564
WRITE_TRUETYPE_NO_TRIMMED_TABLE : 0) |
565
/* Generate a cmap only for incrementally downloaded fonts
566
and for subsetted fonts. */
567
(pfont->data.numGlyphs != pfont->data.trueNumGlyphs ||
568
pbfont->do_subset == DO_SUBSET_YES ?
569
WRITE_TRUETYPE_CMAP : 0);
572
if (pdev->HavePDFWidths) {
573
code = copied_drop_extension_glyphs((gs_font *)out_font);
577
s_init(&poss, pdev->memory);
578
swrite_position_only(&poss);
579
code = psf_write_truetype_font(&poss, pfont, options, NULL, 0, &fnstr);
582
code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object, "/Length1", stell(&poss));
587
code = psf_write_truetype_font(writer.binary.strm, pfont,
588
options, NULL, 0, &fnstr);
592
case ft_CID_encrypted:
593
code = cos_dict_put_string_copy((cos_dict_t *)writer.pres->object, "/Subtype", "/CIDFontType0C");
596
code = psf_write_cid0_font(writer.binary.strm,
597
(gs_font_cid0 *)out_font, TYPE2_OPTIONS,
601
case ft_CID_TrueType:
602
/* CIDFontType 2 fonts don't use cmap, name, OS/2, or post. */
603
#define CID2_OPTIONS WRITE_TRUETYPE_HVMTX
604
code = psf_write_cid2_font(writer.binary.strm,
605
(gs_font_cid2 *)out_font,
606
CID2_OPTIONS, NULL, 0, &fnstr);
609
sflush(writer.binary.strm);
610
s_MD5C_get_digest(writer.binary.strm, digest, sizeof(digest));
612
*ppcd = (cos_dict_t *)writer.pres->object;
614
pdf_end_fontfile(pdev, &writer);
617
code = pdf_end_fontfile(pdev, &writer);
618
if (pdev->PDFA && code >= 0) {
619
gs_id metadata_object_id;
621
code = pdf_font_metadata(pdev, pbfont, digest, sizeof(digest), &metadata_object_id);
622
if (metadata_object_id && code >= 0) {
625
sprintf(buf, "%ld 0 R", metadata_object_id);
626
code = cos_dict_put_string_copy(*ppcd, "/Metadata", buf);
632
code = gs_note_error(gs_error_rangecheck);
635
pbfont->written = true;
640
* Write the CharSet for a subsetted font, as a PDF string.
643
pdf_write_CharSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont)
645
stream *s = pdev->strm;
646
gs_font_base *font = pbfont->copied;
652
(font->procs.enumerate_glyph((gs_font *)font, &index,
653
GLYPH_SPACE_NAME, &glyph),
656
gs_const_string gstr;
657
int code = font->procs.glyph_name((gs_font *)font, glyph, &gstr);
659
/* Don't include .notdef. */
661
bytes_compare(gstr.data, gstr.size, (const byte *)".notdef", 7)
663
pdf_put_name(pdev, gstr.data, gstr.size);
670
* Write the CIDSet object for a subsetted CIDFont.
673
pdf_write_CIDSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont,
676
pdf_data_writer_t writer;
679
code = pdf_begin_data_stream(pdev, &writer,
681
(pdev->CompressFonts ? DATA_STREAM_COMPRESS : 0),
685
stream_write(writer.binary.strm, pbfont->CIDSet,
686
(pbfont->num_glyphs + 7) / 8);
687
code = pdf_end_data(&writer);
690
*pcidset_id = pdf_resource_id(writer.pres);
694
* Check whether a base font is standard.
697
pdf_is_standard_font(pdf_base_font_t *bfont)
698
{ return bfont->is_standard;
702
pdf_set_FontFile_object(pdf_base_font_t *bfont, cos_dict_t *pcd)
704
bfont->FontFile = pcd;
707
pdf_get_FontFile_object(pdf_base_font_t *bfont)
709
return bfont->FontFile;