~ubuntu-branches/ubuntu/vivid/ghostscript/vivid-security

« back to all changes in this revision

Viewing changes to devices/vector/gdevpdtb.c

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2013-08-09 20:01:36 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20130809200136-amb6zrr7hnjb5jq9
Tags: 9.08~rc1~dfsg-0ubuntu1
* New upstream release
   - Ghostscript 9.08rc1.
   - We are using the system's liblcms2 and libopenjpeg now.
* debian/patches/020130401-852e545-pxl-xl-driver-produced-drawing-commands-without-setting-color-space.patch:
  Removed patch backported from upstream.
* debian/patches/ojdk-8007925+8007926.patch,
  debian/patches/ojdk-8007927.patch,
  debian/patches/ojdk-8007929.patch,
  debian/patches/ojdk-8009654.patch: Removed patches on build in liblcms2, we
  use the system's liblcms2 now.
* debian/patches/2001_docdir_fix_for_debian.patch: Manually updated to new
  upstream source code.
* debian/patches/2003_support_multiarch.patch: Refreshed with quilt.
* debian/control: Added build dependencies on liblcms2-dev and
  libopenjpeg-dev.
* debian/rules: Check for removed lcms2/ and openjpeg/ subdirectories in
  the repackaging check again, also set build options for shared liblcms2
  and libopenjpeg libraries.
* debian/rules: Makefile.in and configure.ac are in the root directory of
  the source now and do not need to get linked from base/. Also there is no
  gstoraster and gstopxl CUPS filter in the package any more and no
  "install-cups" make target any more.
* debian/control, debian/rules, debian/ghostscript-cups.install,
  debian/ghostscript-cups.ppd-updater: Removed the ghostscript-cups binary
  package. The files are now provided by cups-filters.
* debian/symbols.common: Updated for new upstream source. Applied patch
  which dpkg-gensymbols generated for debian/libgs9.symbols to this file.

Show diffs side-by-side

added added

removed removed

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