~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to src/gdevpdtb.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2001-2006 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, 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.
12
 
*/
13
 
 
14
 
/* $Id: gdevpdtb.c 8622 2008-04-04 08:53:57Z ken $ */
15
 
/* BaseFont implementation for pdfwrite */
16
 
#include "memory_.h"
17
 
#include "ctype_.h"
18
 
#include "string_.h"
19
 
#include "gx.h"
20
 
#include "gserrors.h"
21
 
#include "gsutil.h"             /* for bytes_compare */
22
 
#include "gxfcid.h"
23
 
#include "gxfcopy.h"
24
 
#include "gxfont.h"             /* for gxfont42.h */
25
 
#include "gxfont42.h"
26
 
#include "gdevpsf.h"
27
 
#include "gdevpdfx.h"
28
 
#include "gdevpdfo.h"
29
 
#include "gdevpdtb.h"
30
 
#include "gdevpdfg.h"
31
 
#include "gdevpdtf.h"
32
 
#include "smd5.h"
33
 
 
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 500        /* 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
 
/*
52
 
 * Determine whether a font is a subset font by examining the name.
53
 
 */
54
 
bool
55
 
pdf_has_subset_prefix(const byte *str, uint size)
56
 
{
57
 
    int i;
58
 
 
59
 
    if (size < SUBSET_PREFIX_SIZE || str[SUBSET_PREFIX_SIZE - 1] != '+')
60
 
        return false;
61
 
    for (i = 0; i < SUBSET_PREFIX_SIZE - 1; ++i)
62
 
        if ((uint)(str[i] - 'A') >= 26)
63
 
            return false;
64
 
    return true;
65
 
}
66
 
 
67
 
static inline ulong
68
 
hash(ulong v, int index, ushort w)
69
 
{
70
 
    return v * 3141592653u + w;
71
 
}
72
 
 
73
 
/*
74
 
 * Add the XXXXXX+ prefix for a subset font.
75
 
 */
76
 
int
77
 
pdf_add_subset_prefix(const gx_device_pdf *pdev, gs_string *pstr, byte *used, int count)
78
 
{
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);
85
 
    ulong v = 0;
86
 
    int i;
87
 
 
88
 
    if (data == 0)
89
 
        return_error(gs_error_VMerror);
90
 
    
91
 
    /* Hash the 'used' array. */
92
 
    for (i = 0; i < len0; i += sizeof(ushort))
93
 
        v = hash(v, i, *(ushort *)(used + i));
94
 
    for (; i < len; i++)
95
 
        v = hash(v, i, used[i]);
96
 
 
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] = '+';
101
 
    pstr->data = data;
102
 
    pstr->size = size + SUBSET_PREFIX_SIZE;
103
 
    return 0;
104
 
}
105
 
 
106
 
/* Finish writing FontFile* data. */
107
 
static int
108
 
pdf_end_fontfile(gx_device_pdf *pdev, pdf_data_writer_t *pdw)
109
 
{
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);
113
 
}
114
 
 
115
 
/* ---------------- Public ---------------- */
116
 
 
117
 
/*
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.
122
 
 */
123
 
int
124
 
pdf_base_font_alloc(gx_device_pdf *pdev, pdf_base_font_t **ppbfont,
125
 
                    gs_font_base *font, const gs_matrix *orig_matrix, 
126
 
                    bool is_standard)
127
 
{
128
 
    gs_memory_t *mem = pdev->pdf_memory;
129
 
    gs_font *copied;
130
 
    gs_font *complete;
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 */
137
 
    int code;
138
 
 
139
 
    if (pbfont == 0)
140
 
        return_error(gs_error_VMerror);
141
 
    code = gs_copy_font((gs_font *)font, orig_matrix, mem, &copied);
142
 
    if (code < 0)
143
 
        goto fail;
144
 
    memset(pbfont, 0, sizeof(*pbfont));
145
 
    {
146
 
        /* 
147
 
         * Adobe Technical Note # 5012 "The Type 42 Font Format Specification" says :
148
 
         *
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.
154
 
         *
155
 
         * Besides that, we found that Adobe Acrobat Reader 4 and 5 ignore 
156
 
         * FontMatrix.ty .
157
 
         */
158
 
        copied->FontMatrix.tx = copied->FontMatrix.ty = 0;
159
 
    }
160
 
    switch (font->FontType) {
161
 
    case ft_encrypted:
162
 
    case ft_encrypted2:
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;
166
 
        break;
167
 
    case ft_TrueType:
168
 
        pbfont->num_glyphs = ((gs_font_type42 *)font)->data.trueNumGlyphs;
169
 
        pbfont->do_subset =
170
 
            (pbfont->num_glyphs <= MAX_NO_SUBSET_GLYPHS ?
171
 
             DO_SUBSET_UNKNOWN : DO_SUBSET_YES);
172
 
        break;
173
 
    case ft_CID_encrypted:
174
 
        pbfont->num_glyphs = ((gs_font_cid0 *)font)->cidata.common.CIDCount;
175
 
        goto cid;
176
 
    case ft_CID_TrueType:
177
 
        pbfont->num_glyphs = ((gs_font_cid2 *)font)->cidata.common.CIDCount;
178
 
    cid:
179
 
        pbfont->do_subset = DO_SUBSET_YES;
180
 
        pbfont->CIDSet =
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);
185
 
            goto fail;
186
 
        }
187
 
        memset(pbfont->CIDSet, 0, (pbfont->num_glyphs + 7) / 8);
188
 
        break;
189
 
    default:
190
 
        code = gs_note_error(gs_error_rangecheck);
191
 
        goto fail;
192
 
    }
193
 
    if (pbfont->do_subset != DO_SUBSET_YES) {
194
 
        /* The only possibly non-subsetted fonts are Type 1/2 and Type 42. */
195
 
        if (is_standard)
196
 
            complete = copied, code = 0;
197
 
        else {
198
 
            code = gs_copy_font((gs_font *)font, &font->FontMatrix, mem, &complete);
199
 
            if (code < 0)
200
 
                goto fail;
201
 
        }
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);
206
 
 
207
 
            memcpy(buf, copied->font_name.chars, l);
208
 
            buf[l] = 0;
209
 
            eprintf1("Can't embed the complete font %s due to font error.\n", buf);
210
 
            goto fail;
211
 
        }
212
 
        if (code < 0) {
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.
217
 
             */
218
 
            complete = copied;
219
 
        } else if (pbfont->num_glyphs < 0) { /* Type 1 */
220
 
            int index, count;
221
 
            gs_glyph glyph;
222
 
 
223
 
            for (index = 0, count = 0;
224
 
                 (font->procs.enumerate_glyph((gs_font *)font, &index,
225
 
                                              GLYPH_SPACE_NAME, &glyph),
226
 
                  index != 0);
227
 
                 )
228
 
                ++count;
229
 
            pbfont->num_glyphs = count;
230
 
        }
231
 
    } else
232
 
        complete = copied;
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;
243
 
        }
244
 
    } else {
245
 
        sprintf(fnbuf, ".F%lx", (ulong)copied);
246
 
        font_name.data = (byte *)fnbuf;
247
 
        font_name.size = strlen(fnbuf);
248
 
    }
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)
252
 
        goto fail;
253
 
    memcpy(pbfont->font_name.data, font_name.data, font_name.size);
254
 
    pbfont->font_name.size = font_name.size;
255
 
    *ppbfont = pbfont;
256
 
    return 0;
257
 
 fail:
258
 
    gs_free_object(mem, pbfont, "pdf_base_font_alloc");
259
 
    return code;
260
 
}
261
 
 
262
 
/*
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.
266
 
 */
267
 
gs_string *
268
 
pdf_base_font_name(pdf_base_font_t *pbfont)
269
 
{
270
 
    return &pbfont->font_name;
271
 
}
272
 
 
273
 
/*
274
 
 * Return the (copied, subset) font associated with a base font.
275
 
 * This procedure probably shouldn't exist....
276
 
 */
277
 
gs_font_base *
278
 
pdf_base_font_font(const pdf_base_font_t *pbfont, bool complete)
279
 
{
280
 
    return (complete ? pbfont->complete : pbfont->copied);
281
 
}
282
 
 
283
 
/*
284
 
 * Check for subset font.
285
 
 */
286
 
bool
287
 
pdf_base_font_is_subset(const pdf_base_font_t *pbfont)
288
 
{
289
 
    return pbfont->do_subset == DO_SUBSET_YES;
290
 
}
291
 
 
292
 
/*
293
 
 * Drop the copied complete font associated with a base font.
294
 
 */
295
 
void
296
 
pdf_base_font_drop_complete(pdf_base_font_t *pbfont)
297
 
{
298
 
    pbfont->complete = NULL;
299
 
}
300
 
 
301
 
/*
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
305
 
 * will be the same.)
306
 
 */
307
 
int
308
 
pdf_base_font_copy_glyph(pdf_base_font_t *pbfont, gs_glyph glyph,
309
 
                         gs_font_base *font)
310
 
{
311
 
    int code =
312
 
        gs_copy_glyph_options((gs_font *)font, glyph,
313
 
                              (gs_font *)pbfont->copied,
314
 
                              (pbfont->is_standard ? COPY_GLYPH_NO_NEW : 0));
315
 
 
316
 
    if (code < 0)
317
 
        return code;
318
 
    if (pbfont->CIDSet != 0 &&
319
 
        (uint)(glyph - GS_MIN_CID_GLYPH) < pbfont->num_glyphs
320
 
        ) {
321
 
        uint cid = glyph - GS_MIN_CID_GLYPH;
322
 
 
323
 
        pbfont->CIDSet[cid >> 3] |= 0x80 >> (cid & 7);
324
 
    }
325
 
    return 0;
326
 
}
327
 
 
328
 
/*
329
 
 * Determine whether a font should be subsetted.
330
 
 */
331
 
bool
332
 
pdf_do_subset_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont, gs_id rid)
333
 
{
334
 
    gs_font_base *copied = pbfont->copied;
335
 
 
336
 
    /*
337
 
     * If the decision has not been made already, determine whether or not
338
 
     * to subset the font.
339
 
     */
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;
343
 
 
344
 
        if (do_subset && max_pct < 100) {
345
 
            /* We want to subset iff used <= total * MaxSubsetPct / 100. */
346
 
            do_subset = false;
347
 
            if (max_pct > 0) {
348
 
                int max_subset_used = pbfont->num_glyphs * max_pct / 100;
349
 
                int used, index;
350
 
                gs_glyph ignore_glyph;
351
 
 
352
 
                do_subset = true;
353
 
                for (index = 0, used = 0;
354
 
                     (copied->procs.enumerate_glyph((gs_font *)copied,
355
 
                                                &index, GLYPH_SPACE_INDEX,
356
 
                                                &ignore_glyph), index != 0);
357
 
                     )
358
 
                    if (++used > max_subset_used) {
359
 
                        do_subset = false;
360
 
                        break;
361
 
                    }
362
 
            }
363
 
        }
364
 
        pbfont->do_subset = (do_subset ? DO_SUBSET_YES : DO_SUBSET_NO);
365
 
    }
366
 
    return (pbfont->do_subset == DO_SUBSET_YES);
367
 
}
368
 
 
369
 
/*
370
 
 * Write the FontFile entry for an embedded font, /FontFile<n> # # R.
371
 
 */
372
 
int
373
 
pdf_write_FontFile_entry(gx_device_pdf *pdev, pdf_base_font_t *pbfont)
374
 
{
375
 
    stream *s = pdev->strm;
376
 
    const char *FontFile_key;
377
 
 
378
 
    switch (pbfont->copied->FontType) {
379
 
    case ft_TrueType:
380
 
    case ft_CID_TrueType:
381
 
        FontFile_key = "/FontFile2";
382
 
        break;
383
 
    default:                    /* Type 1/2, CIDFontType 0 */
384
 
        if (pdev->ResourcesBeforeUsage)
385
 
            FontFile_key = "/FontFile";
386
 
        else
387
 
            FontFile_key = "/FontFile3";
388
 
    }
389
 
    stream_puts(s, FontFile_key);
390
 
    pprintld1(s, " %ld 0 R", pbfont->FontFile->id);
391
 
    return 0;
392
 
}
393
 
 
394
 
/*
395
 
 * Adjust font name for Acrobat Reader 3.
396
 
 */
397
 
static int
398
 
pdf_adjust_font_name(gx_device_pdf *pdev, long id, pdf_base_font_t *pbfont)
399
 
{
400
 
    /*
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.
406
 
     */
407
 
    int i;
408
 
    byte *chars = (byte *)pbfont->font_name.data; /* break 'const' */
409
 
    byte *data;
410
 
    uint size = pbfont->font_name.size;
411
 
    char suffix[sizeof(long) * 2 + 2];
412
 
    uint suffix_size;
413
 
 
414
 
#define SUFFIX_CHAR '~'
415
 
    /*
416
 
     * If the name looks as though it has one of our unique suffixes,
417
 
     * remove the suffix.
418
 
     */
419
 
    for (i = size;
420
 
         i > 0 && isxdigit(chars[i - 1]);
421
 
         --i)
422
 
        DO_NOTHING;
423
 
    if (i < size && i > 0 && chars[i - 1] == SUFFIX_CHAR) {
424
 
        do {
425
 
            --i;
426
 
        } while (i > 0 && chars[i - 1] == SUFFIX_CHAR);
427
 
        size = i + 1;
428
 
    }
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,
433
 
                                  size + suffix_size,
434
 
                                  "pdf_adjust_font_name");
435
 
    if (data == 0)
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;
440
 
#undef SUFFIX_CHAR
441
 
    return 0;
442
 
}
443
 
 
444
 
/*
445
 
 * Write an embedded font.
446
 
 */
447
 
int
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)
450
 
{
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};
457
 
    int code;
458
 
 
459
 
    if (pbfont->written)
460
 
        return 0;               /* already written */
461
 
    code = copied_order_font((gs_font *)out_font);
462
 
    if (code < 0)
463
 
        return code;
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);
469
 
    if (code < 0)
470
 
        return code;
471
 
    if (pdev->PDFA) {
472
 
        stream *s = s_MD5C_make_stream(pdev->pdf_memory, writer.binary.strm);
473
 
 
474
 
        if (s == NULL)
475
 
            return_error(gs_error_VMerror);
476
 
        writer.binary.strm = s;
477
 
    }
478
 
    if (pdev->CompatibilityLevel == 1.2 &&
479
 
        !do_subset && !pbfont->is_standard ) {
480
 
        /*
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.
487
 
         */
488
 
         int code = pdf_adjust_font_name(pdev, writer.pres->object->id, pbfont);
489
 
         if (code < 0)
490
 
            return code;
491
 
    }
492
 
    fnstr.data = pbfont->font_name.data;
493
 
    fnstr.size = pbfont->font_name.size;
494
 
    /* Now write the font (or subset). */
495
 
    switch (FontType) {
496
 
 
497
 
    case ft_composite:
498
 
        /* Nothing to embed -- the descendant fonts do it all. */
499
 
        code = 0;
500
 
        break;
501
 
 
502
 
    case ft_encrypted2:
503
 
        if (!pdev->HaveCFF) {
504
 
            /* Must convert to Type 1 charstrings. */
505
 
            return_error(gs_error_unregistered); /* Not implemented yet. */
506
 
        }
507
 
    case ft_encrypted:
508
 
        if (pdev->HavePDFWidths) {
509
 
            code = copied_drop_extension_glyphs((gs_font *)out_font);
510
 
            if (code < 0)
511
 
                return code;
512
 
        }
513
 
        if (!pdev->HaveCFF) {
514
 
            /* Write the type 1 font with no converting to CFF. */
515
 
            int lengths[3];
516
 
 
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) {
523
 
                if (code < 0)
524
 
                    return code;
525
 
                code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object, 
526
 
                            "/Length1", lengths[0]);
527
 
            }
528
 
            if (lengths[1] > 0) {
529
 
                if (code < 0)
530
 
                    return code;
531
 
                code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object, 
532
 
                            "/Length2", lengths[1]);
533
 
                if (code < 0)
534
 
                    return code;
535
 
                code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object, 
536
 
                            "/Length3", lengths[2]);
537
 
            }
538
 
        } else {
539
 
            /*
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.
544
 
             */
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");
547
 
            if (code < 0)
548
 
                return code;
549
 
            code = psf_write_type2_font(writer.binary.strm,
550
 
                                        (gs_font_type1 *)out_font,
551
 
                                        TYPE2_OPTIONS |
552
 
                            (pdev->CompatibilityLevel < 1.3 ? WRITE_TYPE2_AR3 : 0),
553
 
                                        NULL, 0, &fnstr, FontBBox);
554
 
        }
555
 
        goto finish;
556
 
 
557
 
    case ft_TrueType: {
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);
570
 
        stream poss;
571
 
 
572
 
        if (pdev->HavePDFWidths) {
573
 
            code = copied_drop_extension_glyphs((gs_font *)out_font);
574
 
            if (code < 0)
575
 
                return code;
576
 
        }
577
 
        s_init(&poss, pdev->memory);
578
 
        swrite_position_only(&poss);
579
 
        code = psf_write_truetype_font(&poss, pfont, options, NULL, 0, &fnstr);
580
 
        if (code < 0)
581
 
            return code;
582
 
        code = cos_dict_put_c_key_int((cos_dict_t *)writer.pres->object, "/Length1", stell(&poss));
583
 
        if (code < 0)
584
 
            return code;
585
 
        if (code < 0)
586
 
            return code;
587
 
        code = psf_write_truetype_font(writer.binary.strm, pfont,
588
 
                                       options, NULL, 0, &fnstr);
589
 
        goto finish;
590
 
    }
591
 
 
592
 
    case ft_CID_encrypted:
593
 
        code = cos_dict_put_string_copy((cos_dict_t *)writer.pres->object, "/Subtype", "/CIDFontType0C");
594
 
        if (code < 0)
595
 
            return code;
596
 
        code = psf_write_cid0_font(writer.binary.strm,
597
 
                                   (gs_font_cid0 *)out_font, TYPE2_OPTIONS,
598
 
                                   NULL, 0, &fnstr);
599
 
        goto finish;
600
 
 
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);
607
 
    finish:
608
 
        if (pdev->PDFA) {
609
 
            sflush(writer.binary.strm);
610
 
            s_MD5C_get_digest(writer.binary.strm, digest, sizeof(digest));
611
 
        }
612
 
        *ppcd = (cos_dict_t *)writer.pres->object;
613
 
        if (code < 0) {
614
 
            pdf_end_fontfile(pdev, &writer);
615
 
            return code;
616
 
        }
617
 
        code = pdf_end_fontfile(pdev, &writer);
618
 
        if (pdev->PDFA && code >= 0) {
619
 
            gs_id metadata_object_id;
620
 
 
621
 
            code = pdf_font_metadata(pdev, pbfont, digest, sizeof(digest), &metadata_object_id);
622
 
            if (metadata_object_id && code >= 0) {
623
 
                char buf[20];
624
 
 
625
 
                sprintf(buf, "%ld 0 R", metadata_object_id);
626
 
                code = cos_dict_put_string_copy(*ppcd, "/Metadata", buf);
627
 
            }
628
 
        }
629
 
        break;
630
 
 
631
 
    default:
632
 
        code = gs_note_error(gs_error_rangecheck);
633
 
    }
634
 
 
635
 
    pbfont->written = true;
636
 
    return code;
637
 
}
638
 
 
639
 
/*
640
 
 * Write the CharSet for a subsetted font, as a PDF string.
641
 
 */
642
 
int
643
 
pdf_write_CharSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont)
644
 
{
645
 
    stream *s = pdev->strm;
646
 
    gs_font_base *font = pbfont->copied;
647
 
    int index;
648
 
    gs_glyph glyph;
649
 
 
650
 
    stream_puts(s, "(");
651
 
    for (index = 0;
652
 
         (font->procs.enumerate_glyph((gs_font *)font, &index,
653
 
                                      GLYPH_SPACE_NAME, &glyph),
654
 
          index != 0);
655
 
         ) {
656
 
        gs_const_string gstr;
657
 
        int code = font->procs.glyph_name((gs_font *)font, glyph, &gstr);
658
 
 
659
 
        /* Don't include .notdef. */
660
 
        if (code >= 0 &&
661
 
            bytes_compare(gstr.data, gstr.size, (const byte *)".notdef", 7)
662
 
            )
663
 
            pdf_put_name(pdev, gstr.data, gstr.size);
664
 
    }
665
 
    stream_puts(s, ")");
666
 
    return 0;
667
 
}
668
 
 
669
 
/*
670
 
 * Write the CIDSet object for a subsetted CIDFont.
671
 
 */
672
 
int
673
 
pdf_write_CIDSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont,
674
 
                 long *pcidset_id)
675
 
{
676
 
    pdf_data_writer_t writer;
677
 
    int code;
678
 
 
679
 
    code = pdf_begin_data_stream(pdev, &writer,
680
 
                      DATA_STREAM_BINARY | 
681
 
                      (pdev->CompressFonts ? DATA_STREAM_COMPRESS : 0), 
682
 
                      gs_no_id);
683
 
    if (code < 0)
684
 
        return code;
685
 
    stream_write(writer.binary.strm, pbfont->CIDSet,
686
 
                 (pbfont->num_glyphs + 7) / 8);
687
 
    code = pdf_end_data(&writer);
688
 
    if (code < 0)
689
 
        return code;
690
 
    *pcidset_id = pdf_resource_id(writer.pres);
691
 
    return 0;
692
 
}
693
 
/*
694
 
 * Check whether a base font is standard.
695
 
 */
696
 
bool
697
 
pdf_is_standard_font(pdf_base_font_t *bfont)
698
 
{   return bfont->is_standard;
699
 
}
700
 
 
701
 
void
702
 
pdf_set_FontFile_object(pdf_base_font_t *bfont, cos_dict_t *pcd)
703
 
{
704
 
    bfont->FontFile = pcd;
705
 
}
706
 
const cos_dict_t *
707
 
pdf_get_FontFile_object(pdf_base_font_t *bfont)
708
 
{
709
 
    return bfont->FontFile;
710
 
}