Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/devices/vector/gdevpdtt.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 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.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Text processing for pdfwrite. */
18
#include "math_.h"
19
#include "string_.h"
20
#include <stdlib.h> /* abs() */
21
#include "gx.h"
22
#include "gserrors.h"
23
#include "gsutil.h"                /* for bytes_compare */
24
#include "gscencs.h"
25
#include "gscedata.h"
26
#include "gsmatrix.h"
27
#include "gzstate.h"
28
#include "gxfcache.h"                /* for orig_fonts list */
29
#include "gxfont.h"
30
#include "gxfont0.h"
31
#include "gxfcid.h"
32
#include "gxfcopy.h"
33
#include "gxfcmap.h"
34
#include "gxpath.h"                /* for getting current point */
35
#include "gxchar.h"
36
#include "gxstate.h"
37
#include "gdevpdfx.h"
38
#include "gdevpdfg.h"
39
#include "gdevpdfo.h"
40
#include "gdevpdtx.h"
41
#include "gdevpdtd.h"
42
#include "gdevpdtf.h"
43
#include "gdevpdts.h"
44
#include "gdevpdtt.h"
45
#include "gdevpdti.h"
46
#include "gxhldevc.h"
47
#include "gzpath.h"
48
49
/* ================ Text enumerator ================ */
50
51
/* GC descriptor */
52
private_st_pdf_text_enum();
53
54
/* Define the auxiliary procedures for text processing. */
55
static int
56
pdf_text_resync(gs_text_enum_t *pte, const gs_text_enum_t *pfrom)
57
0
{
58
0
    pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
59
60
0
    if ((pte->text.operation ^ pfrom->text.operation) & ~TEXT_FROM_ANY)
61
0
        return_error(gs_error_rangecheck);
62
0
    if (penum->pte_default) {
63
0
        int code = gs_text_resync(penum->pte_default, pfrom);
64
65
0
        if (code < 0)
66
0
            return code;
67
0
    }
68
0
    pte->text = pfrom->text;
69
0
    gs_text_enum_copy_dynamic(pte, pfrom, false);
70
0
    return 0;
71
0
}
72
static bool
73
pdf_text_is_width_only(const gs_text_enum_t *pte)
74
113k
{
75
113k
    const pdf_text_enum_t *const penum = (const pdf_text_enum_t *)pte;
76
77
113k
    if (penum->pte_default)
78
113k
        return gs_text_is_width_only(penum->pte_default);
79
0
    return false;
80
113k
}
81
static int
82
pdf_text_current_width(const gs_text_enum_t *pte, gs_point *pwidth)
83
0
{
84
0
    const pdf_text_enum_t *const penum = (const pdf_text_enum_t *)pte;
85
86
0
    if (penum->pte_default)
87
0
        return gs_text_current_width(penum->pte_default, pwidth);
88
0
    return_error(gs_error_rangecheck); /* can't happen */
89
0
}
90
91
static void
92
pdf_show_text_release(gs_text_enum_t *pte, client_name_t cname)
93
26.4k
{
94
26.4k
     gs_show_enum *const penum = (gs_show_enum *)pte;
95
26.4k
     gs_text_enum_procs_t *procs = (gs_text_enum_procs_t *)penum->procs;
96
97
26.4k
     penum->cc = 0;
98
26.4k
     if (penum->dev_cache2) {
99
0
         gx_device_retain((gx_device *)penum->dev_cache2, false);
100
0
         penum->dev_cache2 = 0;
101
0
     }
102
26.4k
     if (penum->dev_cache) {
103
0
         gx_device_retain((gx_device *)penum->dev_cache, false);
104
0
         penum->dev_cache = 0;
105
0
     }
106
26.4k
     if (penum->dev_null) {
107
34
         gx_device_retain((gx_device *)penum->dev_null, false);
108
34
         penum->dev_null = 0;
109
34
     }
110
26.4k
     gx_default_text_release(pte, cname);
111
26.4k
     gs_free_object(penum->memory->non_gc_memory, procs, "pdf_show_text_release");
112
26.4k
}
113
114
static int
115
pdf_text_set_cache(gs_text_enum_t *pte, const double *pw,
116
                   gs_text_cache_control_t control)
117
123k
{
118
123k
    pdf_text_enum_t *penum;
119
123k
    gs_text_enum_t *pgste;
120
123k
    gx_device_pdf *pdev = (gx_device_pdf *)pte->dev;
121
123k
    gs_matrix m;
122
123
123k
    if (pdev->pte != NULL)
124
10.3k
        penum = (pdf_text_enum_t *)pdev->pte;
125
113k
    else {
126
113k
        if (gs_object_type(pte->memory, pte) == &st_pdf_text_enum)
127
113k
            penum = (pdf_text_enum_t *)pte;
128
0
        else
129
0
            return_error(gs_error_typecheck);
130
113k
    }
131
132
123k
    if (pdev->type3charpath || (penum->current_font->FontType == ft_PDF_user_defined && pdev->OCRStage == OCR_Rendering))
133
10.5k
        return gs_text_set_cache(penum->pte_default, pw, control);
134
135
113k
    switch (control) {
136
102k
    case TEXT_SET_CHAR_WIDTH:
137
113k
    case TEXT_SET_CACHE_DEVICE:
138
        /* See comments in pdf_text_process. We are using a 100x100 matrix
139
         * NOT the identity, but we want the cache device values to be in
140
         * font co-ordinate space, so we need to undo that scale here.
141
         */
142
113k
        if (pdev->PS_accumulator || pdev->Scaled_accumulator){
143
113k
            gs_matrix_scale(&ctm_only(pte->pgs), .01, .01, &m);
144
113k
            gs_distance_transform(pw[0], pw[1], &m, &pdev->char_width);
145
113k
        } else {
146
0
            pdev->char_width.x = pw[0];
147
0
            pdev->char_width.y = pw[1];
148
0
        }
149
113k
        break;
150
0
    case TEXT_SET_CACHE_DEVICE2:
151
        /*
152
         * pdev->char_width is used with synthesized Type 3 fonts only.
153
         * Since they are simple fonts, we only need the horisontal
154
         * width for Widths array. Therefore we don't check
155
         * gs_rootfont(pgs)->WMode and don't use pw[6:7].
156
         */
157
        /* See comments in pdf_text_process. We are using a 100x100 matrix
158
         * NOT the identity, but we want the cache device values to be in
159
         * font co-ordinate space, so we need to undo that scale here.
160
         */
161
0
        if (pdev->PS_accumulator || pdev->Scaled_accumulator){
162
0
            gs_matrix_scale(&ctm_only(pte->pgs), .01, .01, &m);
163
0
            gs_distance_transform(pw[0], pw[1], &m, &pdev->char_width);
164
0
        } else {
165
0
            pdev->char_width.x = pw[0];
166
0
            pdev->char_width.y = pw[1];
167
0
        }
168
0
        if (penum->cdevproc_callout) {
169
0
            memcpy(penum->cdevproc_result, pw, sizeof(penum->cdevproc_result));
170
0
            return 0;
171
0
        }
172
0
        break;
173
0
    default:
174
0
        return_error(gs_error_rangecheck);
175
113k
    }
176
113k
    if (!pdev->PS_accumulator)
177
10.3k
        pgste = (gs_text_enum_t *)penum;
178
103k
    else
179
103k
        pgste = penum->pte_default;
180
181
113k
    if ((penum->current_font->FontType == ft_user_defined ||
182
113k
        penum->current_font->FontType == ft_PDF_user_defined ||
183
113k
        penum->current_font->FontType == ft_PCL_user_defined ||
184
113k
        penum->current_font->FontType == ft_MicroType ||
185
113k
        penum->current_font->FontType == ft_GL2_stick_user_defined ||
186
113k
        penum->current_font->FontType == ft_GL2_531) &&
187
113k
            penum->outer_CID == GS_NO_GLYPH &&
188
113k
            !(pgste->text.operation & TEXT_DO_CHARWIDTH)) {
189
113k
        int code;
190
113k
        gs_glyph glyph;
191
192
113k
        glyph = pte->returned.current_glyph;
193
113k
        if ((glyph != GS_NO_GLYPH && penum->output_char_code != GS_NO_CHAR) || !pdev->PS_accumulator) {
194
113k
            gs_show_enum *penum_s;
195
113k
            gs_fixed_rect clip_box;
196
113k
            double pw1[10];
197
113k
            int narg = (control == TEXT_SET_CHAR_WIDTH ? 2 :
198
113k
                        control == TEXT_SET_CACHE_DEVICE ? 6 : 10), i;
199
200
113k
            penum_s = (gs_show_enum *)pgste;
201
            /* BuildChar could change the scale before calling setcachedevice (Bug 687290).
202
               We must scale the setcachedevice arguments because we assumed
203
               identity scale before entering the charproc.
204
               For now we only handle scaling matrices.
205
            */
206
247k
            for (i = 0; i < narg; i += 2) {
207
134k
                gs_point p;
208
209
134k
                gs_point_transform(pw[i], pw[i + 1], &ctm_only(penum_s->pgs), &p);
210
134k
                pw1[i] = p.x;
211
134k
                pw1[i + 1] = p.y;
212
134k
            }
213
113k
            if (control != TEXT_SET_CHAR_WIDTH) {
214
10.4k
                clip_box.p.x = float2fixed(pw1[2]);
215
10.4k
                clip_box.p.y = float2fixed(pw1[3]);
216
10.4k
                clip_box.q.x = float2fixed(pw1[4]);
217
10.4k
                clip_box.q.y = float2fixed(pw1[5]);
218
102k
            } else {
219
                /*
220
                 * We have no character bbox, but we need one to install the clipping
221
                 * to the graphic state of the PS interpreter. Since some fonts don't
222
                 * provide a proper FontBBox (Bug 687239 supplies a zero one),
223
                 * we set an "infinite" clipping here.
224
                 * We also detected that min_int, max_int don't work here with
225
                 * comparefiles/Bug687044.ps, therefore we divide them by 2.
226
                 */
227
102k
                clip_box.p.x = clip_box.p.y = min_int / 2;
228
102k
                clip_box.q.x = clip_box.q.y = max_int / 2;
229
102k
            }
230
113k
            code = gx_clip_to_rectangle(penum_s->pgs, &clip_box);
231
113k
            if (code < 0)
232
0
                return code;
233
234
            /* See comments in pdf_text_process. We are using a 100x100 matrix
235
             * NOT the identity, but we want the cache device values to be in
236
             * font co-ordinate space, so we need to undo that scale here. We
237
             * can't do it above, where we take any scaling from the BuildChar
238
             * into account, because that would get the clip path wrong, that
239
             * needs to be in the 100x100 space so that it doesn't clip
240
             * out marking operations.
241
             */
242
113k
            if (pdev->PS_accumulator || pdev->Scaled_accumulator)
243
113k
                gs_matrix_scale(&ctm_only(penum_s->pgs), .01, .01, &m);
244
0
            else
245
0
                m = ctm_only(penum_s->pgs);
246
247k
            for (i = 0; i < narg; i += 2) {
247
134k
                gs_point p;
248
249
134k
                gs_point_transform(pw[i], pw[i + 1], &m, &p);
250
134k
                pw1[i] = p.x;
251
134k
                pw1[i + 1] = p.y;
252
134k
            }
253
113k
            if (!pdev->PS_accumulator && !pdev->Scaled_accumulator)
254
0
                code = pdf_set_charproc_attrs(pdev, pte->current_font,
255
0
                        pw1, narg, control, penum->returned.current_char, false);
256
113k
            else
257
113k
                code = pdf_set_charproc_attrs(pdev, pte->current_font,
258
113k
                        pw1, narg, control, penum->output_char_code, true);
259
113k
            if (code < 0)
260
0
                return code;
261
            /* Prevent writing the clipping path to charproc.
262
               See the comment above and bugs 687678, 688327.
263
               Note that the clipping in the graphic state will be used while
264
               fallbacks to default implementations of graphic objects.
265
               Hopely such fallbacks are rare. */
266
113k
            pdev->clip_path_id = gx_get_clip_path_id(penum_s->pgs);
267
113k
            return code;
268
113k
        } else {
269
0
            gs_matrix m;
270
0
            pdf_resource_t *pres = pdev->accumulating_substream_resource;
271
272
            /* pdf_text_process started a charproc stream accumulation,
273
               but now we re-decided to go with the default implementation.
274
               Cancel the stream now.
275
             */
276
0
            if (pres == NULL)
277
0
                return_error(gs_error_unregistered);
278
0
            code = pdf_exit_substream(pdev);
279
0
            if (code < 0)
280
0
                return code;
281
0
            code = pdf_cancel_resource(pdev, pres, resourceCharProc);
282
0
            if (code < 0)
283
0
                return code;
284
0
            pdf_forget_resource(pdev, pres, resourceCharProc);
285
            /* pdf_text_process had set an identity CTM for the
286
               charproc stream accumulation, but now we re-decided
287
               to go with the default implementation.
288
               Need to restore the correct CTM and add
289
               changes, which the charproc possibly did. */
290
            /* See comments in pdf_text_process. We are using a 100x100 matrix
291
             * NOT the identity,  so we need to undo that scale here.
292
             */
293
0
            gs_matrix_scale(&ctm_only(penum->pgs), .01, .01, (gs_matrix *)&ctm_only(penum->pgs));
294
            /* We also scaled the page height and width. Because we
295
             * don't go through the accumulator 'close' in pdf_text_process
296
             * we must also undo that scale.
297
             */
298
0
            pdev->width /= 100;
299
0
            pdev->height /= 100;
300
301
0
            gs_matrix_multiply((gs_matrix *)&pdev->charproc_ctm, (gs_matrix *)&penum->pgs->ctm, &m);
302
0
            gs_matrix_fixed_from_matrix(&penum->pgs->ctm, &m);
303
0
            penum->charproc_accum = false;
304
0
            pdev->accumulating_charproc = false;
305
0
        }
306
113k
    }
307
0
    if (pdev->PS_accumulator && penum->pte_default) {
308
0
        if (penum->pte_default->text.operation & TEXT_DO_CHARWIDTH /* See process_cmap_text.*/)
309
0
            return gs_text_set_cache(penum->pte_default, pw, TEXT_SET_CHAR_WIDTH);
310
0
        else
311
0
            return gs_text_set_cache(penum->pte_default, pw, control);
312
0
    }
313
0
    return_error(gs_error_unregistered); /* can't happen */
314
0
}
315
316
static int
317
pdf_text_retry(gs_text_enum_t *pte)
318
0
{
319
0
    pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
320
321
0
    if (penum->pte_default)
322
0
        return gs_text_retry(penum->pte_default);
323
0
    return_error(gs_error_rangecheck); /* can't happen */
324
0
}
325
static void
326
pdf_text_release(gs_text_enum_t *pte, client_name_t cname)
327
6.40M
{
328
6.40M
    pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
329
6.40M
    gx_device_pdf *pdev = (gx_device_pdf *)penum->dev;
330
6.40M
    ocr_glyph_t *next;
331
332
    /* Track the dominant text rotation. Ignore text outside the page (text_clipped) and
333
     * any text that isn't drawn, unless it is drawn in text rendering mode 3 (invisible)
334
     */
335
6.40M
    if (!penum->text_clipped && (penum->text.operation & TEXT_DO_DRAW || penum->pgs->text_rendering_mode == 3))
336
6.32M
    {
337
6.32M
        gs_matrix tmat, fmat;
338
6.32M
        gs_point p;
339
6.32M
        int i;
340
6.32M
        gs_font *font = (gs_font *)penum->current_font;
341
6.32M
        gs_text_params_t *const text = &pte->text;
342
343
        /* If we have a CIDFont or type 0 font, then the current font will be the actual
344
         * marking font from the FDepVector array of dictionaries. We need to multiply
345
         * that font's FontMatrix by the FontMatrix of the original type 0 font.
346
         */
347
6.32M
        if (font != penum->orig_font) {
348
362k
            gs_matrix_multiply(&font->FontMatrix, &penum->orig_font->FontMatrix, &fmat);
349
362k
            gs_matrix_multiply(&fmat, &ctm_only(penum->pgs), &tmat);
350
5.96M
        } else {
351
5.96M
            gs_matrix_multiply(&font->FontMatrix, &ctm_only(penum->pgs), &tmat);
352
5.96M
        }
353
354
6.32M
        gs_distance_transform(1, 0, &tmat, &p);
355
6.32M
        if (p.x > fabs(p.y))
356
6.29M
            i = 0;
357
31.8k
        else if (p.x < -fabs(p.y))
358
2.14k
            i = 2;
359
29.7k
        else if (p.y > fabs(p.x))
360
22.2k
            i = 1;
361
7.53k
        else if (p.y < -fabs(p.x))
362
1.49k
            i = 3;
363
6.03k
        else
364
6.03k
            i = 4;
365
6.32M
        pdf_current_page(pdev)->text_rotation.counts[i] += text->size;
366
6.32M
    }
367
368
6.40M
    if (penum->pte_default) {
369
1.27k
        gs_text_release(NULL, penum->pte_default, cname);
370
1.27k
        penum->pte_default = 0;
371
1.27k
    }
372
6.40M
    pdf_text_release_cgp(penum);
373
374
6.40M
    while (pdev->ocr_glyphs != NULL)
375
0
    {
376
0
        next = pdev->ocr_glyphs->next;
377
378
0
        gs_free_object(pdev->memory, pdev->ocr_glyphs->data, "free bitmap");
379
0
        gs_free_object(pdev->memory, pdev->ocr_glyphs, "free bitmap");
380
0
        pdev->ocr_glyphs = next;
381
0
    }
382
6.40M
    if (pdev->OCRUnicode != NULL)
383
0
        gs_free_object(pdev->memory, pdev->OCRUnicode, "free returned unicodes");
384
6.40M
    pdev->OCRUnicode = NULL;
385
386
6.40M
    gx_default_text_release(pte, cname);
387
6.40M
    pdev->OCRStage = 0;
388
6.40M
}
389
void
390
pdf_text_release_cgp(pdf_text_enum_t *penum)
391
6.51M
{
392
6.51M
    if (penum->cgp) {
393
6.03M
        gs_free_object(penum->memory, penum->cgp, "pdf_text_release");
394
6.03M
        penum->cgp = 0;
395
6.03M
    }
396
6.51M
}
397
398
/* Begin processing text. */
399
static text_enum_proc_process(pdf_text_process);
400
static const gs_text_enum_procs_t pdf_text_procs = {
401
    pdf_text_resync, pdf_text_process,
402
    pdf_text_is_width_only, pdf_text_current_width,
403
    pdf_text_set_cache, pdf_text_retry,
404
    pdf_text_release
405
};
406
407
/* Ideally we would set the stroke and fill colours in pdf_prepare_text_drawing
408
 * but this is called from pdf_process_text, not pdf_begin_text. The problem is
409
 * that if we get a pattern colour we need to exit to the interpreter and run
410
 * the PaintProc, we do this by retunring e_ReampColor. But the 'process' routines
411
 * aren't set up to accept this, and just throw an error. Trying to rework the
412
 * interpreter routines began turning into a gigantic task, so I chose instead
413
 * to 'set' the colours here, which is called from text_begin, where the code
414
 * allows us to return_error(gs_error_RemapColor. Attempting to write the colour to the PDF file
415
 * in this routine as well caused trouble keeping the graphics states synchronised,
416
 * so this functionality was left in pdf_prepare_text_drawing.
417
 */
418
static int
419
pdf_prepare_text_color(gx_device_pdf *const pdev, gs_gstate *pgs, const gs_text_params_t *text, gs_font *font)
420
2.70M
{
421
2.70M
    int code=0;
422
2.70M
    if (text->operation & TEXT_DO_DRAW) {
423
2.69M
        if (!pdev->ForOPDFRead) {
424
2.69M
            if (pgs->text_rendering_mode != 3 && pgs->text_rendering_mode != 7) {
425
2.69M
                if (font->PaintType == 2) {
426
                    /* Bit awkward, if the PaintType is 2 then we want to set the
427
                     * current ie 'fill' colour, but as a stroke colour because we
428
                     * will later change the text rendering mode to 1 (stroke).
429
                     */
430
0
                    code = gx_set_dev_color(pgs);
431
0
                    if (code != 0)
432
0
                        return code;
433
0
                    code = pdf_set_drawing_color(pdev, pgs, pgs->color[0].dev_color, &pdev->saved_stroke_color,
434
0
                                 &pdev->stroke_used_process_color,
435
0
                                 &psdf_set_stroke_color_commands);
436
0
                    if (code < 0)
437
0
                        return code;
438
2.69M
                } else {
439
2.69M
                    if ((pgs->text_rendering_mode == 0 || pgs->text_rendering_mode == 2 ||
440
2.69M
                        pgs->text_rendering_mode == 4 || pgs->text_rendering_mode == 6) &&
441
2.69M
                        !pdev->remap_stroke_color) {
442
2.69M
                        code = gx_set_dev_color(pgs);
443
2.69M
                        if (code != 0)
444
0
                            return code;
445
2.69M
                    }
446
2.69M
                    if (pgs->text_rendering_mode == 1 || pgs->text_rendering_mode == 2 ||
447
2.69M
                        pgs->text_rendering_mode == 5 || pgs->text_rendering_mode == 6) {
448
810
                        if (!pdev->remap_fill_color) {
449
810
                            if (pdev->remap_stroke_color) {
450
0
                                pdev->remap_stroke_color = false;
451
810
                            } else {
452
810
                                gs_swapcolors_quick(pgs);
453
810
                                code = gx_set_dev_color(pgs);
454
810
                                if (code == gs_error_Remap_Color)
455
0
                                    pdev->remap_stroke_color = true;
456
810
                                if (code != 0)
457
0
                                    return code;
458
810
                            }
459
810
                        } else
460
0
                            pdev->remap_fill_color = false;
461
810
                        gs_swapcolors_quick(pgs);
462
810
                        code = gx_set_dev_color(pgs);
463
810
                        if (code == gs_error_Remap_Color)
464
0
                            pdev->remap_fill_color = true;
465
810
                        if (code != 0)
466
0
                            return code;
467
810
                    }
468
2.69M
                }
469
2.69M
            }
470
2.69M
        }
471
2.69M
    }
472
2.70M
    return code;
473
2.70M
}
474
475
static int
476
pdf_prepare_text_drawing(gx_device_pdf *const pdev, gs_text_enum_t *pte)
477
6.40M
{
478
6.40M
    gs_gstate * pgs = pte->pgs;
479
6.40M
    const gx_clip_path * pcpath = pte->pcpath;
480
6.40M
    const gs_text_params_t *text = &pte->text;
481
6.40M
    bool new_clip = false; /* Quiet compiler. */
482
6.40M
    int code;
483
6.40M
    gs_font *font = pte->current_font;
484
485
6.40M
    if (!(text->operation & TEXT_DO_NONE) || pgs->text_rendering_mode == 3) {
486
6.40M
        code = pdf_check_soft_mask(pdev, pte->pgs);
487
6.40M
        if (code < 0)
488
0
            return code;
489
6.40M
        new_clip = pdf_must_put_clip_path(pdev, pcpath);
490
6.40M
        if (new_clip)
491
53.8k
            code = pdf_unclip(pdev);
492
6.34M
        else if (pdev->context == PDF_IN_NONE)
493
13.3k
            code = pdf_open_page(pdev, PDF_IN_STREAM);
494
6.33M
        else
495
6.33M
            code = 0;
496
6.40M
        if (code < 0)
497
0
            return code;
498
6.40M
        code = pdf_prepare_fill(pdev, pgs, true);
499
6.40M
        if (code < 0)
500
4.83k
            return code;
501
6.40M
    }
502
6.39M
    if (text->operation & TEXT_DO_DRAW) {
503
        /*
504
         * Set the clipping path and drawing color.  We set both the fill
505
         * and stroke color, because we don't know whether the fonts will be
506
         * filled or stroked, and we can't set a color while we are in text
507
         * mode.  (This is a consequence of the implementation, not a
508
         * limitation of PDF.)
509
         */
510
511
6.36M
        if (new_clip) {
512
16.0k
            code = pdf_put_clip_path(pdev, pcpath);
513
16.0k
            if (code < 0)
514
0
                return code;
515
516
            /* If we have text in a clipping mode we need to update the saved 'bottom'
517
             * of the saved gstate stack. This is the point we will use for potentially
518
             * writing out any additional clip path, or clips used by images. We *don't*
519
             * update the 'saved bottom' (saved_vgstack_depth_for_textclip) because that's
520
             * what we restore back to after we grestore back to a time when the text
521
             * rendering mode didn't involve clip.
522
             */
523
16.0k
            if (pdev->clipped_text_pending)
524
1
                pdev->vgstack_bottom = pdev->vgstack_depth;
525
16.0k
        }
526
527
        /* For ps2write output, and for any 'type 3' font we need to write both the stroke and fill colours
528
         * because we don't know whether the font will use stroke or fill, or both, and expect the
529
         * current colour to work for both operations.
530
         */
531
6.36M
        if (!pdev->ForOPDFRead && font->FontType != ft_user_defined && font->FontType != ft_CID_user_defined
532
6.36M
            && font->FontType != ft_PCL_user_defined && font->FontType != ft_GL2_531 && font->FontType != ft_PDF_user_defined
533
6.36M
            && font->FontType != ft_GL2_stick_user_defined) {
534
2.67M
            if (pgs->text_rendering_mode != 3 && pgs->text_rendering_mode != 7) {
535
2.67M
                if (font->PaintType == 2) {
536
                    /* Bit awkward, if the PaintType is 2 (or its the PCL stick font, which is stroked)
537
                     * then we want to set the current ie 'fill' colour, but as a stroke colour because we
538
                     * will later change the text rendering mode to 1 (stroke).
539
                     */
540
0
                    code = gx_set_dev_color(pgs);
541
0
                    if (code != 0)
542
0
                        return code;
543
0
                    code = pdf_set_drawing_color(pdev, pgs, pgs->color[0].dev_color, &pdev->saved_stroke_color,
544
0
                                 &pdev->stroke_used_process_color,
545
0
                                 &psdf_set_stroke_color_commands);
546
0
                    if (code < 0)
547
0
                        return code;
548
2.67M
                } else {
549
2.67M
                    if (pgs->text_rendering_mode == 0 || pgs->text_rendering_mode == 2 ||
550
2.67M
                        pgs->text_rendering_mode == 4 || pgs->text_rendering_mode == 6) {
551
2.67M
                        code = gx_set_dev_color(pgs);
552
2.67M
                        if (code != 0)
553
0
                            return code;
554
2.67M
                        code = pdf_set_drawing_color(pdev, pgs, pgs->color[0].dev_color, &pdev->saved_fill_color,
555
2.67M
                                     &pdev->fill_used_process_color,
556
2.67M
                                     &psdf_set_fill_color_commands);
557
2.67M
                        if (code < 0)
558
1.16k
                            return code;
559
2.67M
                    }
560
2.66M
                    if (pgs->text_rendering_mode == 1 || pgs->text_rendering_mode == 2 ||
561
2.66M
                        pgs->text_rendering_mode == 5 || pgs->text_rendering_mode == 6) {
562
769
                        gs_swapcolors_quick(pgs);
563
769
                        code = gx_set_dev_color(pgs);
564
769
                        if (code != 0)
565
0
                            return code;
566
769
                        code = pdf_set_drawing_color(pdev, pgs, pgs->color[0].dev_color, &pdev->saved_stroke_color,
567
769
                                     &pdev->stroke_used_process_color,
568
769
                                     &psdf_set_stroke_color_commands);
569
769
                        if (code < 0)
570
0
                            return code;
571
572
769
                        gs_swapcolors_quick(pgs);
573
769
                    }
574
2.66M
                }
575
2.67M
            }
576
3.68M
        } else {
577
3.68M
            code = gx_set_dev_color(pgs);
578
3.68M
            if (code != 0)
579
0
                return code;
580
581
3.68M
            if ((code =
582
3.68M
                 pdf_set_drawing_color(pdev, pgs, pgs->color[0].dev_color, &pdev->saved_stroke_color,
583
3.68M
                                       &pdev->stroke_used_process_color,
584
3.68M
                                       &psdf_set_stroke_color_commands)) < 0 ||
585
3.68M
                (code =
586
3.68M
                 pdf_set_drawing_color(pdev, pgs, pgs->color[0].dev_color, &pdev->saved_fill_color,
587
3.68M
                                       &pdev->fill_used_process_color,
588
3.68M
                                       &psdf_set_fill_color_commands)) < 0
589
3.68M
                )
590
404
                return code;
591
3.68M
        }
592
6.36M
    }
593
6.39M
    return 0;
594
6.39M
}
595
596
static bool
597
outline_list_includes(const gs_param_string_array *psa, const byte *chars,
598
                    uint size)
599
6.40M
{
600
6.40M
    uint i;
601
602
6.40M
    for (i = 0; i < psa->size; ++i)
603
0
        if (!bytes_compare(psa->data[i].data, psa->data[i].size, chars, size))
604
0
            return true;
605
6.40M
    return false;
606
6.40M
}
607
608
int
609
gdev_pdf_text_begin(gx_device * dev, gs_gstate * pgs,
610
                    const gs_text_params_t *text, gs_font * font,
611
                    const gx_clip_path * pcpath,
612
                    gs_text_enum_t ** ppte)
613
6.40M
{
614
6.40M
    gx_device_pdf *const pdev = (gx_device_pdf *)dev;
615
6.40M
    gx_path *path0 = pgs->path;
616
6.40M
    gx_path *path = ((text->operation & TEXT_DO_NONE) &&
617
6.40M
                    !(text->operation & TEXT_RETURN_WIDTH) ? NULL : path0);
618
6.40M
    pdf_text_enum_t *penum;
619
6.40M
    int code, user_defined = 0;
620
6.40M
    gs_memory_t * mem = pgs->memory;
621
6.40M
    byte *chars_ptr = font->font_name.chars;
622
6.40M
    uint size = font->font_name.size;
623
624
10.2M
    while (pdf_has_subset_prefix(chars_ptr, size)) {
625
        /* Strip off an existing subset prefix. */
626
3.86M
        chars_ptr += SUBSET_PREFIX_SIZE;
627
3.86M
        size -= SUBSET_PREFIX_SIZE;
628
3.86M
    }
629
630
    /* should we "flatten" the font to "normal" marking operations */
631
6.40M
    if (outline_list_includes(&pdev->params.AlwaysOutline, (const byte *)chars_ptr, size) ||
632
6.40M
        (pdev->FlattenFonts && !outline_list_includes(&pdev->params.NeverOutline, (const byte *)chars_ptr, size))
633
6.40M
        ) {
634
0
        font->dir->ccache.upper = 0;
635
0
        return gx_default_text_begin(dev, pgs, text, font,
636
0
                                     pcpath, ppte);
637
0
    }
638
639
6.40M
    pdev->last_charpath_op = 0;
640
6.40M
    if ((text->operation & TEXT_DO_ANY_CHARPATH) && !path0->first_subpath) {
641
2.18k
        if (pdf_compare_text_state_for_charpath(pdev->text->text_state, pdev, pgs, font, text))
642
18
            pdev->last_charpath_op = text->operation & TEXT_DO_ANY_CHARPATH;
643
2.18k
    }
644
645
6.40M
    if(font->FontType == ft_user_defined ||
646
6.40M
       font->FontType == ft_PDF_user_defined ||
647
6.40M
       font->FontType == ft_PCL_user_defined ||
648
6.40M
       font->FontType == ft_MicroType ||
649
6.40M
       font->FontType == ft_GL2_stick_user_defined ||
650
6.40M
       font->FontType == ft_GL2_531)
651
273k
        user_defined = 1;
652
653
    /* We need to know whether any of the glyphs in a string using a composite font
654
     * use a descendant font which is a type 3 (user-defined) so that we can properly
655
     * skip the caching below.
656
     */
657
6.40M
    if(font->FontType == ft_composite && ((gs_font_type0 *)font)->data.FMapType != fmap_CMap) {
658
0
        int font_code;
659
0
        gs_char chr;
660
0
        gs_glyph glyph;
661
662
0
        rc_alloc_struct_1(penum, pdf_text_enum_t, &st_pdf_text_enum, mem,
663
0
                      return_error(gs_error_VMerror), "gdev_pdf_text_begin");
664
0
        penum->rc.free = rc_free_text_enum;
665
0
        penum->pte_default = 0;
666
0
        penum->charproc_accum = false;
667
0
        pdev->accumulating_charproc = false;
668
0
        penum->cdevproc_callout = false;
669
0
        penum->returned.total_width.x = penum->returned.total_width.y = 0;
670
0
        penum->cgp = NULL;
671
0
        penum->output_char_code = GS_NO_CHAR;
672
0
        code = gs_text_enum_init((gs_text_enum_t *)penum, &pdf_text_procs,
673
0
                             dev, pgs, text, font, pcpath, mem);
674
0
        if (code < 0) {
675
0
            gs_free_object(mem, penum, "gdev_pdf_text_begin");
676
0
            return code;
677
0
        }
678
0
        do {
679
0
            font_code = penum->orig_font->procs.next_char_glyph
680
0
                ((gs_text_enum_t *)penum, &chr, &glyph);
681
0
            if (font_code == 1){
682
0
                if (penum->fstack.items[penum->fstack.depth].font->FontType == 3) {
683
0
                    user_defined = 1;
684
0
                    break;
685
0
                }
686
0
            }
687
0
        } while(font_code != 2 && font_code >= 0);
688
0
        if (!user_defined) {
689
0
            if (penum->fstack.items[penum->fstack.depth].font->FontType == 3)
690
0
                user_defined = 1;
691
0
        }
692
0
        gs_text_release(NULL, (gs_text_enum_t *)penum, "pdf_text_process");
693
0
    }
694
695
6.40M
    if (!user_defined || !(text->operation & TEXT_DO_ANY_CHARPATH)) {
696
6.40M
        if (user_defined &&
697
6.40M
            (text->operation & TEXT_DO_NONE) && (text->operation & TEXT_RETURN_WIDTH)
698
6.40M
            && pgs->text_rendering_mode != 3) {
699
            /* This is stringwidth, see gx_default_text_begin.
700
             * We need to prevent writing characters to PS cache,
701
             * otherwise the font converts to bitmaps.
702
             * So pass through even with stringwidth.
703
             */
704
48
            code = gx_hld_stringwidth_begin(pgs, &path);
705
48
            if (code < 0)
706
0
                return code;
707
6.40M
        } else if ((!(text->operation & TEXT_DO_DRAW) && pgs->text_rendering_mode != 3)
708
6.40M
                    || path == 0 || !path_position_valid(path)
709
6.40M
                    || pdev->type3charpath)
710
2.85k
            return gx_default_text_begin(dev, pgs, text, font,
711
2.85k
                                         pcpath, ppte);
712
6.40M
        else if (text->operation & TEXT_DO_ANY_CHARPATH)
713
0
            return gx_default_text_begin(dev, pgs, text, font,
714
0
                                         pcpath, ppte);
715
6.40M
    }
716
717
6.40M
    if (!pdev->ForOPDFRead) {
718
2.70M
    code = pdf_prepare_text_color(pdev, pgs, text, font);
719
2.70M
    if (code != 0)
720
0
        return code;
721
2.70M
    }
722
723
    /* Allocate and initialize the enumerator. */
724
725
6.40M
    rc_alloc_struct_1(penum, pdf_text_enum_t, &st_pdf_text_enum, mem,
726
6.40M
                      return_error(gs_error_VMerror), "gdev_pdf_text_begin");
727
6.40M
    penum->rc.free = rc_free_text_enum;
728
6.40M
    penum->pte_default = 0;
729
6.40M
    penum->charproc_accum = false;
730
6.40M
    pdev->accumulating_charproc = false;
731
6.40M
    penum->cdevproc_callout = false;
732
6.40M
    penum->text_clipped = false;
733
6.40M
    penum->returned.total_width.x = penum->returned.total_width.y = 0;
734
6.40M
    penum->cgp = NULL;
735
6.40M
    penum->returned.current_glyph = GS_NO_GLYPH;
736
6.40M
    penum->output_char_code = GS_NO_CHAR;
737
6.40M
    code = gs_text_enum_init((gs_text_enum_t *)penum, &pdf_text_procs,
738
6.40M
                             dev, pgs, text, font, pcpath, mem);
739
6.40M
    penum->k_text_release = 1; /* early release of black_textvec_state */
740
741
6.40M
    if (code < 0) {
742
0
        gs_free_object(mem, penum, "gdev_pdf_text_begin");
743
0
        return code;
744
0
    }
745
6.40M
    if (pdev->font3 != 0) {
746
        /* A text operation happens while accumulating a charproc.
747
           This is a case when source document uses a Type 3 font,
748
           which's charproc uses another font.
749
           Since the text operation is handled by the device,
750
           the font isn't converting to a raster (i.e. to a bitmap font).
751
           Disable the grid fitting for the convertion to get a proper outlines,
752
           because the viewer resolution is not known during the accumulation.
753
           Note we set identity CTM in pdf_text_set_cache for the accumilation,
754
           and therefore the font may look too small while the source charproc
755
           interpretation. The document tpc2.ps of the bug 687087 is an example.
756
        */
757
2.12k
        penum->device_disabled_grid_fitting = true;
758
2.12k
    }
759
760
6.40M
    *ppte = (gs_text_enum_t *)penum;
761
762
6.40M
    return 0;
763
6.40M
}
764
765
/* ================ Font cache element ================ */
766
767
/* GC descriptor */
768
private_st_pdf_font_cache_elem();
769
770
/*
771
 * Compute id for a font cache element.
772
 */
773
static int64_t
774
pdf_font_cache_elem_id(gs_font *font)
775
39.5M
{
776
39.5M
    return font->id;
777
39.5M
}
778
779
pdf_font_cache_elem_t **
780
pdf_locate_font_cache_elem(gx_device_pdf *pdev, gs_font *font)
781
39.3M
{
782
39.3M
    pdf_font_cache_elem_t **e = &pdev->font_cache;
783
39.3M
    pdf_font_cache_elem_t *prev = NULL;
784
39.3M
    int64_t id = pdf_font_cache_elem_id(font);
785
786
111M
    for (; *e != 0; e = &(*e)->next) {
787
110M
        if ((*e)->font_id == id) {
788
37.4M
            if (prev != NULL) {
789
147k
                pdf_font_cache_elem_t *curr = *e;
790
791
                /* move the curr font to head of list (Most Recently Used) */
792
147k
                prev->next = curr->next;
793
147k
                curr->next = pdev->font_cache;
794
147k
                pdev->font_cache = curr;
795
147k
            }
796
37.4M
            return &(pdev->font_cache);
797
37.4M
        }
798
72.6M
        prev = *e;
799
72.6M
    }
800
1.83M
    return 0;
801
39.3M
}
802
803
static void
804
pdf_remove_font_cache_elem(gx_device_pdf *pdev, pdf_font_cache_elem_t *e0)
805
186k
{
806
186k
    pdf_font_cache_elem_t **e = &pdev->font_cache;
807
808
186k
    for (; *e != 0; e = &(*e)->next)
809
186k
        if (*e == e0) {
810
186k
            *e = e0->next;
811
186k
            gs_free_object(pdev->pdf_memory, e0->glyph_usage,
812
186k
                                "pdf_remove_font_cache_elem");
813
186k
            gs_free_object(pdev->pdf_memory, e0->real_widths,
814
186k
                                "pdf_remove_font_cache_elem");
815
            /* Clean pointers, because gs_free_object below may work idle
816
               when this function is called by a garbager notification.
817
               Leaving unclean pointers may cause
818
               a further heap validation to fail
819
               since the unfreed structure points to freed areas.
820
               For instance, e0->next may point to an element,
821
               which is really freed with a subsequent 'restore'.
822
               Bug 688837. */
823
186k
            e0->next = 0;
824
186k
            e0->glyph_usage = 0;
825
186k
            e0->real_widths = 0;
826
186k
            gs_free_object(pdev->pdf_memory, e0,
827
186k
                                "pdf_remove_font_cache_elem");
828
186k
            return;
829
186k
        }
830
186k
}
831
832
static void
833
font_cache_elem_array_sizes(gx_device_pdf *pdev, gs_font *font,
834
                            int *num_widths, int *num_chars)
835
378k
{
836
378k
    switch (font->FontType) {
837
0
    case ft_composite:
838
0
        *num_widths = 0; /* Unused for Type 0 */
839
0
        *num_chars = 65536; /* No chance to determine, use max. */
840
0
        break;
841
360k
    case ft_encrypted:
842
362k
    case ft_encrypted2:
843
363k
    case ft_user_defined:
844
365k
    case ft_PDF_user_defined:
845
365k
    case ft_GL2_stick_user_defined:
846
365k
    case ft_PCL_user_defined:
847
365k
    case ft_MicroType:
848
365k
    case ft_GL2_531:
849
365k
    case ft_disk_based:
850
365k
    case ft_Chameleon:
851
375k
    case ft_TrueType:
852
375k
        *num_widths = *num_chars = 256; /* Assuming access to glyph_usage by character codes */
853
375k
        break;
854
398
    case ft_CID_encrypted:
855
398
        *num_widths = *num_chars = ((gs_font_cid0 *)font)->cidata.common.MaxCID + 1;
856
398
        break;
857
2.18k
    case ft_CID_TrueType:
858
2.18k
        *num_widths = *num_chars = ((gs_font_cid2 *)font)->cidata.common.CIDCount;
859
2.18k
        break;
860
0
    default:
861
0
        *num_widths = *num_chars = 65536; /* No chance to determine, use max. */
862
378k
    }
863
378k
}
864
865
static int
866
alloc_font_cache_elem_arrays(gx_device_pdf *pdev, pdf_font_cache_elem_t *e,
867
                             gs_font *font)
868
186k
{
869
186k
    int num_widths, num_chars, len;
870
871
186k
    font_cache_elem_array_sizes(pdev, font, &num_widths, &num_chars);
872
186k
    len = (num_chars + 7) / 8;
873
186k
    if (e->glyph_usage != NULL)
874
0
        gs_free_object(pdev->pdf_memory, e->glyph_usage,
875
186k
                            "pdf_attach_font_resource, reallocating");
876
186k
    if (e->real_widths != NULL)
877
0
        gs_free_object(pdev->pdf_memory, e->real_widths,
878
186k
                            "alloc_font_cache_elem_arrays, reallocating");
879
880
186k
    e->glyph_usage = gs_alloc_bytes(pdev->pdf_memory,
881
186k
                        len, "alloc_font_cache_elem_arrays");
882
883
186k
    e->real_widths = (num_widths > 0 ? (double *)gs_alloc_bytes(pdev->pdf_memory,
884
186k
                        num_widths * sizeof(*e->real_widths) *
885
186k
                            ((font->FontType == ft_user_defined ||
886
186k
                            font->FontType == ft_PDF_user_defined ||
887
186k
                            font->FontType == ft_GL2_stick_user_defined ||
888
186k
                            font->FontType == ft_PCL_user_defined ||
889
186k
                            font->FontType == ft_MicroType ||
890
186k
                            font->FontType == ft_GL2_531) ? 2 : 1),
891
186k
                        "alloc_font_cache_elem_arrays") : NULL);
892
186k
    if (e->glyph_usage == NULL || (num_widths !=0 && e->real_widths == NULL)) {
893
0
        gs_free_object(pdev->pdf_memory, e->glyph_usage,
894
0
                            "pdf_attach_font_resource");
895
0
        gs_free_object(pdev->pdf_memory, e->real_widths,
896
0
                            "alloc_font_cache_elem_arrays");
897
        /* Avoid risk of double freeing above if we come around again */
898
0
        e->glyph_usage = NULL;
899
0
        e->real_widths = NULL;
900
0
        return_error(gs_error_VMerror);
901
0
    }
902
186k
    e->num_chars = num_chars;
903
186k
    e->num_widths = num_widths;
904
186k
    memset(e->glyph_usage, 0, len);
905
186k
    if (e->real_widths != NULL)
906
186k
        memset(e->real_widths, 0, num_widths * sizeof(*e->real_widths));
907
186k
    return 0;
908
186k
}
909
910
int
911
pdf_free_font_cache(gx_device_pdf *pdev)
912
41.8k
{
913
41.8k
    pdf_font_cache_elem_t *e = pdev->font_cache, *next;
914
915
    /* fixed! fixme : release elements.
916
     * We no longer track font_cache elements in the original font, which is where
917
     * the memory used to be released. So now we must release it ourselves.
918
     */
919
920
228k
    while (e != NULL) {
921
186k
        next = e->next;
922
186k
        pdf_remove_font_cache_elem(pdev, e);
923
186k
        e = next;
924
186k
    }
925
41.8k
    pdev->font_cache = NULL;
926
41.8k
    return 0;
927
41.8k
}
928
929
/*
930
 * Retrive font resource attached to a font,
931
 * allocating glyph_usage and real_widths on request.
932
 */
933
int
934
pdf_attached_font_resource(gx_device_pdf *pdev, gs_font *font,
935
                            pdf_font_resource_t **pdfont, byte **glyph_usage,
936
                            double **real_widths, int *num_chars, int *num_widths)
937
39.1M
{
938
39.1M
    pdf_font_cache_elem_t **e = pdf_locate_font_cache_elem(pdev, font);
939
940
39.1M
    if (e != NULL && (((*e)->glyph_usage == NULL && glyph_usage !=NULL) ||
941
37.4M
                      ((*e)->real_widths == NULL && real_widths !=NULL))) {
942
186k
        int code = alloc_font_cache_elem_arrays(pdev, *e, font);
943
944
186k
        if (code < 0)
945
0
            return code;
946
186k
    }
947
39.1M
    *pdfont = (e == NULL ? NULL : (*e)->pdfont);
948
39.1M
    if (glyph_usage != NULL)
949
35.2M
        *glyph_usage = (e == NULL ? NULL : (*e)->glyph_usage);
950
39.1M
    if (real_widths != NULL)
951
35.2M
        *real_widths = (e == NULL ? NULL : (*e)->real_widths);
952
39.1M
    if (num_chars != NULL)
953
35.2M
        *num_chars = (e == NULL ? 0 : (*e)->num_chars);
954
39.1M
    if (num_widths != NULL)
955
35.2M
        *num_widths = (e == NULL ? 0 : (*e)->num_widths);
956
39.1M
    return 0;
957
39.1M
}
958
959
/*
960
 * Attach font resource to a font.
961
 */
962
int
963
pdf_attach_font_resource(gx_device_pdf *pdev, gs_font *font,
964
                         pdf_font_resource_t *pdfont)
965
191k
{
966
191k
    int num_chars, num_widths, len;
967
191k
    pdf_font_cache_elem_t *e, **pe = pdf_locate_font_cache_elem(pdev, font);
968
969
    /* Allow the HPGL2 stick font to be attached to a type 3 font, it *is* a
970
     * type 3 font, its just identified differently so that we can choose not
971
     * to capture it elsewhere. Same for PCL bitmap fonts.
972
     */
973
191k
    if (pdfont->FontType != font->FontType &&
974
191k
        (pdfont->FontType != ft_user_defined ||
975
858
        (font->FontType != ft_PCL_user_defined &&
976
858
        font->FontType != ft_PDF_user_defined &&
977
858
        font->FontType != ft_MicroType &&
978
858
        font->FontType != ft_GL2_stick_user_defined &&
979
858
        font->FontType != ft_GL2_531)))
980
0
        return_error(gs_error_unregistered); /* Must not happen. */
981
191k
    font_cache_elem_array_sizes(pdev, font, &num_widths, &num_chars);
982
191k
    len = (num_chars + 7) / 8;
983
191k
    if (pe != NULL) {
984
4.81k
        e = *pe;
985
4.81k
        if (e->pdfont == pdfont)
986
1.54k
            return 0;
987
3.27k
        e->pdfont = pdfont;
988
        /* Reset glyph cache because e->pdfont had changed. */
989
3.27k
        memset(e->glyph_usage, 0, len);
990
3.27k
        memset(e->real_widths, 0, num_widths * sizeof(*e->real_widths));
991
186k
    } else {
992
186k
        e = (pdf_font_cache_elem_t *)gs_alloc_struct(pdev->pdf_memory,
993
186k
                pdf_font_cache_elem_t, &st_pdf_font_cache_elem,
994
186k
                            "pdf_attach_font_resource");
995
186k
        if (e == NULL)
996
0
            return_error(gs_error_VMerror);
997
186k
        e->pdfont = pdfont;
998
186k
        e->font_id = pdf_font_cache_elem_id(font);
999
186k
        e->num_chars = 0;
1000
186k
        e->glyph_usage = NULL;
1001
186k
        e->real_widths = NULL;
1002
186k
        e->next = pdev->font_cache;
1003
186k
        pdev->font_cache = e;
1004
186k
        return 0;
1005
186k
    }
1006
3.27k
    return 0;
1007
191k
}
1008
1009
/* ================ Process text ================ */
1010
1011
/* ---------------- Internal utilities ---------------- */
1012
1013
/*
1014
 * Compute and return the orig_matrix of a font.
1015
 */
1016
int
1017
pdf_font_orig_matrix(const gs_font *font, gs_matrix *pmat)
1018
4.99M
{
1019
4.99M
    switch (font->FontType) {
1020
0
    case ft_composite:                /* subfonts have their own FontMatrix */
1021
223k
    case ft_TrueType:
1022
1.41M
    case ft_CID_TrueType:
1023
        /* The TrueType FontMatrix is 1 unit per em, which is what we want. */
1024
1.41M
        gs_make_identity(pmat);
1025
1.41M
        return 0;
1026
3.47M
    case ft_encrypted:
1027
3.54M
    case ft_encrypted2:
1028
3.57M
    case ft_CID_encrypted:
1029
3.57M
    case ft_user_defined:
1030
3.57M
    case ft_PCL_user_defined:
1031
3.57M
    case ft_PDF_user_defined:
1032
3.57M
    case ft_MicroType:
1033
3.57M
    case ft_GL2_stick_user_defined:
1034
3.57M
    case ft_GL2_531:
1035
        /*
1036
         * Type 1 fonts are supposed to use a standard FontMatrix of
1037
         * [0.001 0 0 0.001 0 0], with a 1000-unit cell.  However,
1038
         * Windows NT 4.0 creates Type 1 fonts, apparently derived from
1039
         * TrueType fonts, that use a 2048-unit cell and corresponding
1040
         * FontMatrix.  Also, some PS programs perform font scaling by
1041
         * replacing FontMatrix like this :
1042
         *
1043
         *   /f12 /Times-Roman findfont
1044
         *   copyfont          % (remove FID)
1045
         *   dup /FontMatrix [0.012 0 0 0.012 0 0] put
1046
         *   definefont
1047
         *   /f12 1 selectfont
1048
         *
1049
         * Such fonts are their own "base font", but the orig_matrix
1050
         * must still be set to 0.001, not 0.012 .
1051
         *
1052
         * The old code used a heuristic to detect and correct for this here.
1053
         * Unfortunately it doesn't work properly when it meets a font
1054
         * with FontMatrix like this :
1055
         *
1056
         *   /FontMatrix [1 2288 div 0 0 1 2288 div 0 0 ] def
1057
         *
1058
         * (the bug 686970). Also comparefiles\455690.pdf appears to
1059
         * have similar problem. Therefore we added a support to lib/gs_fonts.ps,
1060
         * src/zbfont.c, src/gsfont.c that provides an acces to the original
1061
         * font via a special key OrigFont added to the font dictionary while definefont.
1062
         * Now we work through this access with PS interpreter,
1063
         * but keep the old heuristic for other clients.
1064
         */
1065
3.57M
        {
1066
3.57M
            const gs_font *base_font = font;
1067
1068
3.63M
            while (base_font->base != base_font)
1069
59.4k
                base_font = base_font->base;
1070
3.57M
            if (font->FontType == ft_user_defined ||
1071
3.57M
                font->FontType == ft_PDF_user_defined ||
1072
3.57M
                font->FontType == ft_PCL_user_defined ||
1073
3.57M
                font->FontType == ft_MicroType ||
1074
3.57M
                font->FontType == ft_GL2_stick_user_defined ||
1075
3.57M
                font->FontType == ft_GL2_531)
1076
0
                *pmat = base_font->FontMatrix;
1077
3.57M
            else if (base_font->orig_FontMatrix.xx != 0 || base_font->orig_FontMatrix.xy != 0 ||
1078
3.57M
                base_font->orig_FontMatrix.yx != 0 || base_font->orig_FontMatrix.yy != 0)
1079
3.57M
                *pmat = base_font->orig_FontMatrix;
1080
0
            else {
1081
                /*  Must not happen with PS interpreter.
1082
                    Provide a hewuristic for other clients.
1083
                */
1084
0
                if (base_font->FontMatrix.xx == 1.0/2048 &&
1085
0
                    base_font->FontMatrix.xy == 0 &&
1086
0
                    base_font->FontMatrix.yx == 0 &&
1087
0
                    any_abs(base_font->FontMatrix.yy) == 1.0/2048
1088
0
                    )
1089
0
                    *pmat = base_font->FontMatrix;
1090
0
                else
1091
0
                    gs_make_scaling(0.001, 0.001, pmat);
1092
0
            }
1093
3.57M
        }
1094
3.57M
        return 0;
1095
0
    default:
1096
0
        return_error(gs_error_rangecheck);
1097
4.99M
    }
1098
4.99M
}
1099
1100
/*
1101
 * Special version of pdf_font_orig_matrix(), that cares FDArray font's FontMatrix too.
1102
 * Called only by pdf_glyph_width().
1103
 * 'cid' is only consulted if 'font' is a CIDFontType 0 CID font.
1104
 */
1105
static int
1106
glyph_orig_matrix(const gs_font *font, gs_glyph cid, gs_matrix *pmat)
1107
4.87M
{
1108
4.87M
    int code = pdf_font_orig_matrix(font, pmat);
1109
4.87M
    if (code >= 0) {
1110
4.87M
        if (font->FontType == ft_CID_encrypted) {
1111
23.1k
            int fidx;
1112
1113
23.1k
            if (cid < GS_MIN_CID_GLYPH)
1114
0
                cid = GS_MIN_CID_GLYPH;
1115
23.1k
            code = ((gs_font_cid0 *)font)->cidata.glyph_data((gs_font_base *)font,
1116
23.1k
                                cid, NULL, &fidx);
1117
23.1k
            if (code < 0) {
1118
0
                code = ((gs_font_cid0 *)font)->cidata.glyph_data((gs_font_base *)font,
1119
0
                                (gs_glyph)GS_MIN_CID_GLYPH, NULL, &fidx);
1120
0
            }
1121
23.1k
            if (code >= 0) {
1122
23.1k
                gs_matrix_multiply(&(gs_cid0_indexed_font(font, fidx)->FontMatrix),
1123
23.1k
                                pmat, pmat);
1124
23.1k
            }
1125
23.1k
        }
1126
4.87M
    }
1127
4.87M
    return code;
1128
4.87M
}
1129
1130
/*
1131
 * Check the Encoding compatibility
1132
 */
1133
bool
1134
pdf_check_encoding_compatibility(const pdf_font_resource_t *pdfont,
1135
            const pdf_char_glyph_pair_t *pairs, int num_chars)
1136
6.59M
{
1137
6.59M
    int i;
1138
1139
36.5M
    for (i = 0; i < num_chars; ++i) {
1140
30.0M
        gs_char ch = pairs[i].chr;
1141
30.0M
        pdf_encoding_element_t *pet = &pdfont->u.simple.Encoding[ch];
1142
1143
30.0M
        if (pairs[i].glyph == pet->glyph)
1144
23.9M
            continue;
1145
6.07M
        if (pet->glyph != GS_NO_GLYPH) /* encoding conflict */
1146
3.42k
            return false;
1147
6.07M
    }
1148
6.59M
    return true;
1149
6.59M
}
1150
1151
/*
1152
 * Check whether the Encoding has listed glyphs.
1153
 */
1154
static bool
1155
pdf_check_encoding_has_glyphs(const pdf_font_resource_t *pdfont,
1156
            const pdf_char_glyph_pair_t *pairs, int num_chars)
1157
0
{
1158
    /* This function is pretty slow, but we can't find a better algorithm.
1159
       It works for the case of glyphshow with no proper encoding,
1160
       which we believe comes from poorly designed documents.
1161
     */
1162
0
    int i, ch;
1163
1164
0
    for (i = 0; i < num_chars; ++i) {
1165
0
        for (ch = 0; ch < 256; ch++) {
1166
0
            pdf_encoding_element_t *pet = &pdfont->u.simple.Encoding[ch];
1167
1168
0
            if (pairs[i].glyph == pet->glyph)
1169
0
                return true;
1170
0
        }
1171
0
    }
1172
0
    return false;
1173
0
}
1174
1175
/*
1176
 * Check font resource for encoding compatibility.
1177
 */
1178
static bool
1179
pdf_is_compatible_encoding(gx_device_pdf *pdev, pdf_font_resource_t *pdfont,
1180
                           gs_font *font, const pdf_char_glyph_pair_t *pairs, int num_chars)
1181
6.59M
{
1182
    /*
1183
     * This crude version of the code ignores
1184
     * the possibility of re-encoding characters.
1185
     */
1186
6.59M
    switch (pdfont->FontType) {
1187
0
    case ft_composite:
1188
0
        {   /*
1189
             * We assume that source document don't redefine CMap
1190
             * resources and that incremental CMaps do not exist.
1191
             * Therefore we don't maintain stable CMap copies,
1192
             * but just compare CMap names for equality.
1193
             * A better implementation should compare the chars->glyphs
1194
             * translation against the stable copy of CMap,
1195
             * which to be handled with PDF CMap resource.
1196
             */
1197
0
            gs_font_type0 *pfont = (gs_font_type0 *)font;
1198
1199
0
            if (pfont->data.FMapType == fmap_CMap) {
1200
0
                const gs_cmap_t *pcmap = pfont->data.CMap;
1201
0
                const gs_const_string *s1 = &pcmap->CMapName;
1202
1203
0
                return (pdfont->u.type0.CMapName_size == s1->size &&
1204
0
                        !memcmp(pdfont->u.type0.CMapName_data, s1->data, pdfont->u.type0.CMapName_size));
1205
0
            }
1206
0
        }
1207
0
        return false;
1208
400k
    case ft_user_defined:
1209
400k
    case ft_PDF_user_defined:
1210
400k
    case ft_PCL_user_defined:
1211
400k
    case ft_MicroType:
1212
400k
    case ft_GL2_stick_user_defined:
1213
400k
    case ft_GL2_531:
1214
400k
        if (pdfont->u.simple.Encoding == NULL)
1215
0
            return false; /* Not sure. Happens with 020-01.ps . */
1216
        /* fall through */
1217
5.31M
    case ft_encrypted:
1218
5.63M
    case ft_encrypted2:
1219
6.59M
    case ft_TrueType:
1220
6.59M
        return pdf_check_encoding_compatibility(pdfont, pairs, num_chars);
1221
0
    case ft_CID_encrypted:
1222
0
    case ft_CID_TrueType:
1223
0
        {
1224
0
            gs_font *font1 = (gs_font *)pdf_font_resource_font(pdfont, false);
1225
1226
0
            return gs_is_CIDSystemInfo_compatible(
1227
0
                                gs_font_cid_system_info(font),
1228
0
                                gs_font_cid_system_info(font1));
1229
0
        }
1230
0
    default:
1231
0
        return false;
1232
6.59M
    }
1233
6.59M
}
1234
1235
/*
1236
 * Check whethet the font resource has glyphs.
1237
 */
1238
static bool
1239
pdf_font_has_glyphs(gx_device_pdf *pdev, pdf_font_resource_t *pdfont,
1240
                           gs_font *font, const pdf_char_glyph_pair_t *pairs, int num_chars)
1241
0
{
1242
    /*
1243
     * This crude version of the code ignores
1244
     * the possibility of re-encoding characters.
1245
     */
1246
0
    switch (pdfont->FontType) {
1247
0
    case ft_composite:
1248
0
    case ft_user_defined:
1249
0
    case ft_PDF_user_defined:
1250
0
    case ft_PCL_user_defined:
1251
0
    case ft_MicroType:
1252
0
    case ft_GL2_stick_user_defined:
1253
0
    case ft_GL2_531:
1254
        /* Unused case. */
1255
0
        return false;
1256
0
    case ft_encrypted:
1257
0
    case ft_encrypted2:
1258
0
    case ft_TrueType:
1259
0
        return pdf_check_encoding_has_glyphs(pdfont, pairs, num_chars);
1260
0
    case ft_CID_encrypted:
1261
0
    case ft_CID_TrueType:
1262
        /* Unused case. */
1263
0
        return false;
1264
0
    default:
1265
0
        return false;
1266
0
    }
1267
0
}
1268
1269
/*
1270
 * Find a font resource compatible with a given font.
1271
 */
1272
static int
1273
pdf_find_font_resource(gx_device_pdf *pdev, gs_font *font,
1274
                       pdf_resource_type_t type,
1275
                       pdf_font_resource_t **ppdfont,
1276
                       pdf_char_glyph_pairs_t *cgp,
1277
                       bool compatible_encoding)
1278
952k
{
1279
952k
    pdf_resource_t **pchain = pdev->resources[type].chains;
1280
952k
    pdf_resource_t *pres;
1281
952k
    int i;
1282
1283
15.1M
    for (i = 0; i < NUM_RESOURCE_CHAINS; i++) {
1284
19.0M
        for (pres = pchain[i]; pres != 0; pres = pres->next) {
1285
4.92M
            pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pres;
1286
4.92M
            const gs_font_base *cfont;
1287
4.92M
            gs_font *ofont = font;
1288
4.92M
            int code;
1289
1290
4.92M
            cfont = (gs_font_base *)font;
1291
4.92M
            if (uid_is_XUID(&cfont->UID)){
1292
4.19M
                int size = uid_XUID_size(&cfont->UID);
1293
4.19M
                long *xvalues = uid_XUID_values(&cfont->UID);
1294
4.19M
                if (xvalues && size >= 2 && xvalues[0] == 1000000) {
1295
4.19M
                    if (xvalues[size - 1] != pdfont->XUID)
1296
3.68M
                        continue;
1297
4.19M
                }
1298
4.19M
            }
1299
1300
1.23M
            if (font->FontType != pdfont->FontType)
1301
93.7k
                continue;
1302
1.14M
            if (pdfont->FontType == ft_composite) {
1303
0
                gs_font_type0 *font0 = (gs_font_type0 *)font;
1304
1305
0
                ofont = font0->data.FDepVector[0]; /* See pdf_make_font_resource. */
1306
0
                cfont = pdf_font_resource_font(pdfont->u.type0.DescendantFont, false);
1307
0
                if (font0->data.CMap->WMode != pdfont->u.type0.WMode)
1308
0
                    continue;
1309
0
            } else
1310
1.14M
                cfont = pdf_font_resource_font(pdfont, false);
1311
1.14M
            if (!pdf_is_CID_font(ofont) &&
1312
1.14M
                (compatible_encoding
1313
1.13M
                    ? !pdf_is_compatible_encoding(pdev, pdfont, font, cgp->s, cgp->num_all_chars)
1314
1.13M
                    : !pdf_font_has_glyphs(pdev, pdfont, font, cgp->s, cgp->num_all_chars)))
1315
3.35k
                continue;
1316
1.14M
            if (cfont == 0)
1317
34
                continue;
1318
1.14M
            code = gs_copied_can_copy_glyphs((const gs_font *)cfont, ofont,
1319
1.14M
                            &cgp->s[cgp->unused_offset].glyph, cgp->num_unused_chars,
1320
1.14M
                            sizeof(pdf_char_glyph_pair_t), true);
1321
1.14M
            if (code == gs_error_unregistered) /* Debug purpose only. */
1322
0
                return code;
1323
1.14M
            if(code > 0) {
1324
151k
                *ppdfont = pdfont;
1325
151k
                return 1;
1326
151k
            }
1327
1.14M
        }
1328
14.3M
    }
1329
801k
    return 0;
1330
952k
}
1331
1332
/*
1333
 * Find a type0 font resource for a gived descendent name and CMap name.
1334
 */
1335
static int
1336
pdf_find_type0_font_resource(gx_device_pdf *pdev, const pdf_font_resource_t *pdsubf,
1337
            const gs_const_string *CMapName, uint font_index, pdf_font_resource_t **ppdfont)
1338
4
{
1339
4
    pdf_resource_t **pchain = pdev->resources[resourceFont].chains;
1340
4
    pdf_resource_t *pres;
1341
4
    int i;
1342
1343
68
    for (i = 0; i < NUM_RESOURCE_CHAINS; i++) {
1344
68
        for (pres = pchain[i]; pres != 0; pres = pres->next) {
1345
4
            pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pres;
1346
1347
4
            if (pdfont->FontType != ft_composite)
1348
0
                continue;
1349
4
            if (pdfont->u.type0.DescendantFont != pdsubf)
1350
0
                continue;
1351
4
            if (pdfont->u.type0.font_index != font_index)
1352
0
                continue;
1353
1354
            /* Check to see if the PDF font name is of the form FontName-CMapName
1355
             * If it is, check the name and cmap against the BaseFont Name and CMap
1356
             * I'm not certain this is *ever* true.
1357
             */
1358
4
            if (pdfont->BaseFont.size == pdsubf->BaseFont.size + CMapName->size + 1) {
1359
0
                if (memcmp(pdfont->BaseFont.data + pdsubf->BaseFont.size + 1,
1360
0
                            CMapName->data, CMapName->size))
1361
0
                    continue;
1362
4
            } else {
1363
                /* Otherwise, check the PDF font name against the subfont name, and the
1364
                 * CMap used with the PDF font against the requested CMap. If either differs
1365
                 * then this PDF font is not usable.
1366
                 */
1367
4
                if (pdfont->BaseFont.size != pdsubf->BaseFont.size)
1368
0
                    continue;
1369
4
                if (pdfont->u.type0.CMapName_size != CMapName->size)
1370
0
                    continue;
1371
4
                if (memcmp(pdfont->u.type0.CMapName_data, CMapName->data, CMapName->size))
1372
4
                    continue;
1373
4
            }
1374
1375
0
            *ppdfont = pdfont;
1376
0
            return 1;
1377
4
        }
1378
64
    }
1379
4
    return 0;
1380
4
}
1381
1382
static int pdf_make_font_resource(gx_device_pdf *pdev, gs_font *font,
1383
                       pdf_font_resource_t **ppdfont,
1384
                       pdf_char_glyph_pairs_t *cgp);
1385
1386
/*
1387
 * Create or find a CID font resource object for a glyph set.
1388
 */
1389
int
1390
pdf_obtain_cidfont_resource(gx_device_pdf *pdev, gs_font *subfont,
1391
                            pdf_font_resource_t **ppdsubf,
1392
                            pdf_char_glyph_pairs_t *cgp)
1393
528k
{
1394
528k
    int code = 0;
1395
1396
528k
    code = pdf_attached_font_resource(pdev, subfont, ppdsubf, NULL, NULL, NULL, NULL);
1397
528k
    if (code < 0)
1398
0
        return code;
1399
528k
    if (*ppdsubf != NULL) {
1400
278k
        const gs_font_base *cfont = pdf_font_resource_font(*ppdsubf, false);
1401
1402
278k
        code = gs_copied_can_copy_glyphs((const gs_font *)cfont, subfont,
1403
278k
                        &cgp->s[cgp->unused_offset].glyph, cgp->num_unused_chars,
1404
278k
                        sizeof(pdf_char_glyph_pair_t), true);
1405
278k
        if (code > 0)
1406
277k
            return 0;
1407
208
        if (code < 0)
1408
51
            return code;
1409
157
        *ppdsubf = NULL;
1410
157
    }
1411
250k
    code = pdf_find_font_resource(pdev, subfont,
1412
250k
                                  resourceCIDFont, ppdsubf, cgp, true);
1413
250k
    if (code < 0)
1414
0
        return code;
1415
250k
    if (*ppdsubf == NULL) {
1416
250k
        code = pdf_make_font_resource(pdev, subfont, ppdsubf, cgp);
1417
250k
        if (code < 0)
1418
248k
            return code;
1419
250k
    }
1420
1.36k
    return pdf_attach_font_resource(pdev, subfont, *ppdsubf);
1421
250k
}
1422
1423
/*
1424
 * Refine index of BaseEncoding.
1425
 */
1426
static int
1427
pdf_refine_encoding_index(const gx_device_pdf *pdev, int index, bool is_standard)
1428
34.6k
{
1429
34.6k
    if (pdev->ForOPDFRead) {
1430
        /*
1431
        * Allow Postscript encodings only.
1432
        * No, also allow MacRoman because if we have a TT font, and subset it, then opdfread
1433
        * will prefer thte MacRoman CMAP from the subset TT font.
1434
        */
1435
18.6k
        switch (index) {
1436
1437
2.80k
            case ENCODING_INDEX_STANDARD: return index;
1438
4.17k
            case ENCODING_INDEX_MACROMAN: return index;
1439
217
            case ENCODING_INDEX_ISOLATIN1: return index;
1440
11.4k
            default:
1441
11.4k
                return ENCODING_INDEX_STANDARD;
1442
18.6k
        }
1443
18.6k
    }
1444
    /*
1445
     * Per the PDF 1.3 documentation, there are only 3 BaseEncoding
1446
     * values allowed for non-embedded fonts.  Pick one here.
1447
     */
1448
15.9k
    switch (index) {
1449
3.35k
    case ENCODING_INDEX_WINANSI:
1450
4.05k
    case ENCODING_INDEX_MACROMAN:
1451
4.05k
    case ENCODING_INDEX_MACEXPERT:
1452
4.05k
        return index;
1453
2.48k
    case ENCODING_INDEX_STANDARD:
1454
2.48k
        if (is_standard)
1455
870
            return index;
1456
        /* Falls through. */
1457
11.0k
    default:
1458
11.0k
        return ENCODING_INDEX_WINANSI;
1459
15.9k
    }
1460
15.9k
}
1461
1462
/*
1463
 * Create a font resource object for a gs_font of Type 3.
1464
 */
1465
int
1466
pdf_make_font3_resource(gx_device_pdf *pdev, gs_font *font,
1467
                       pdf_font_resource_t **ppdfont)
1468
1.21k
{
1469
1.21k
    const gs_font_base *bfont = (const gs_font_base *)font;
1470
1.21k
    pdf_font_resource_t *pdfont;
1471
1.21k
    byte *cached;
1472
1.21k
    int code;
1473
1474
1.21k
    cached = gs_alloc_bytes(pdev->pdf_memory, 256/8, "pdf_make_font3_resource");
1475
1.21k
    if (cached == NULL)
1476
0
        return_error(gs_error_VMerror);
1477
1.21k
    code = font_resource_encoded_alloc(pdev, &pdfont, bfont->id,
1478
1.21k
                    ft_user_defined, pdf_write_contents_bitmap);
1479
1.21k
    if (code < 0) {
1480
0
        gs_free_object(pdev->pdf_memory, cached, "pdf_make_font3_resource");
1481
0
        return code;
1482
0
    }
1483
1.21k
    memset(cached, 0, 256 / 8);
1484
1.21k
    pdfont->mark_glyph = font->dir->ccache.mark_glyph; /* For pdf_font_resource_enum_ptrs. */
1485
1.21k
    pdfont->u.simple.s.type3.bitmap_font = false;
1486
1.21k
    pdfont->u.simple.BaseEncoding = pdf_refine_encoding_index(pdev,
1487
1.21k
                        bfont->nearest_encoding_index, true);
1488
1.21k
    pdfont->u.simple.s.type3.char_procs = NULL;
1489
1.21k
    pdfont->u.simple.s.type3.cached = cached;
1490
1.21k
    if ((pdfont->FontType == ft_user_defined  || pdfont->FontType == ft_PDF_user_defined) && bfont->FontBBox.p.x == 0.0 &&
1491
1.21k
        bfont->FontBBox.p.y == 0.00 && bfont->FontBBox.q.x == 0.00 &&
1492
1.21k
        bfont->FontBBox.q.y == 0.0) {
1493
        /* I think this can only happen with a type 3 (bitmap) font from PCL.
1494
         * If we leave the BBox as 0 then we end up putting a 0 0 1000 1000 in
1495
         * the PDF. This causes Acrobat to be unable to search or highlight
1496
         * the search results. I think that we will always get a consistent
1497
         * type of font from the PCL interpreter, and if so the correct BBox
1498
         * sould always is 0 0 1 -1.
1499
         */
1500
415
        pdfont->u.simple.s.type3.FontBBox.p.x = 0;
1501
415
        pdfont->u.simple.s.type3.FontBBox.p.y = 0;
1502
415
        pdfont->u.simple.s.type3.FontBBox.q.x = 1;
1503
415
        pdfont->u.simple.s.type3.FontBBox.q.y = -1;
1504
800
    } else {
1505
800
        pdfont->u.simple.s.type3.FontBBox.p.x = bfont->FontBBox.p.x;
1506
800
        pdfont->u.simple.s.type3.FontBBox.p.y = bfont->FontBBox.p.y;
1507
800
        pdfont->u.simple.s.type3.FontBBox.q.x = bfont->FontBBox.q.x;
1508
800
        pdfont->u.simple.s.type3.FontBBox.q.y = bfont->FontBBox.q.y;
1509
800
    }
1510
1.21k
    pdfont->u.simple.s.type3.FontMatrix = bfont->FontMatrix;
1511
1.21k
    pdfont->u.simple.s.type3.Resources = cos_dict_alloc(pdev, "pdf_make_font3_resource");
1512
1.21k
    if (pdfont->u.simple.s.type3.Resources == NULL)
1513
0
        return_error(gs_error_VMerror);
1514
    /* Adobe viewers have a precision problem with small font matrices : */
1515
    /* Don't perform this test if all entries are 0, leads to infinite loop! */
1516
1.21k
    if (pdfont->u.simple.s.type3.FontMatrix.xx != 0.0 ||
1517
1.21k
        pdfont->u.simple.s.type3.FontMatrix.xy != 0.0 ||
1518
1.21k
        pdfont->u.simple.s.type3.FontMatrix.yx != 0.0 ||
1519
1.21k
        pdfont->u.simple.s.type3.FontMatrix.yy != 0.0) {
1520
1.21k
        while (any_abs(pdfont->u.simple.s.type3.FontMatrix.xx) < 0.001 &&
1521
1.21k
               any_abs(pdfont->u.simple.s.type3.FontMatrix.xy) < 0.001 &&
1522
1.21k
               any_abs(pdfont->u.simple.s.type3.FontMatrix.yx) < 0.001 &&
1523
1.21k
               any_abs(pdfont->u.simple.s.type3.FontMatrix.yy) < 0.001) {
1524
0
            pdfont->u.simple.s.type3.FontMatrix.xx *= 10;
1525
0
            pdfont->u.simple.s.type3.FontMatrix.xy *= 10;
1526
0
            pdfont->u.simple.s.type3.FontMatrix.yx *= 10;
1527
0
            pdfont->u.simple.s.type3.FontMatrix.yy *= 10;
1528
0
        }
1529
1.21k
    }
1530
1.21k
    *ppdfont = pdfont;
1531
1.21k
    return 0;
1532
1.21k
}
1533
1534
/*
1535
 * Create a font resource object for a gs_font.  Return 1 iff the
1536
 * font was newly created (it's a roudiment, keeping reverse compatibility).
1537
 * This procedure is only intended to be called
1538
 * from a few places in the text code.
1539
 */
1540
static int
1541
pdf_make_font_resource(gx_device_pdf *pdev, gs_font *font,
1542
                       pdf_font_resource_t **ppdfont,
1543
                       pdf_char_glyph_pairs_t *cgp)
1544
801k
{
1545
801k
    int index = -1;
1546
801k
    font_type orig_type = ft_undefined;
1547
801k
    int BaseEncoding = ENCODING_INDEX_UNKNOWN;
1548
801k
    pdf_font_embed_t embed;
1549
801k
    pdf_font_descriptor_t *pfd = 0;
1550
801k
    int (*font_alloc)(gx_device_pdf *, pdf_font_resource_t **,
1551
801k
                      gs_id, pdf_font_descriptor_t *);
1552
801k
    gs_font *base_font = font; /* A roudiment from old code. Keep it for a while. */
1553
801k
    pdf_font_resource_t *pdfont;
1554
801k
    pdf_standard_font_t *const psfa =
1555
801k
        pdev->text->outline_fonts->standard_fonts;
1556
801k
    int code = 0;
1557
801k
    long XUID = 0;
1558
801k
    gs_font_base *bfont = (gs_font_base *)font;
1559
1560
801k
    if (pdev->version < psdf_version_level2_with_TT) {
1561
0
        switch(font->FontType) {
1562
0
            case ft_TrueType:
1563
0
            case ft_CID_TrueType:
1564
0
                return_error(gs_error_undefined);
1565
0
            default:
1566
0
                break;
1567
0
        }
1568
0
    }
1569
801k
    if (pdev->ForOPDFRead && !pdev->HaveCIDSystem) {
1570
767k
        switch(font->FontType) {
1571
5.57k
            case ft_CID_encrypted:
1572
247k
            case ft_CID_TrueType:
1573
247k
                return_error(gs_error_undefined);
1574
519k
            default:
1575
519k
                break;
1576
767k
        }
1577
767k
    }
1578
553k
    if (!pdev->HaveCFF) {
1579
519k
        if (font->FontType == ft_encrypted2)
1580
488k
            return_error(gs_error_undefined);
1581
519k
    }
1582
64.7k
    embed = pdf_font_embed_status(pdev, base_font, &index, cgp->s, cgp->num_all_chars, &orig_type);
1583
64.7k
    if (pdev->CompatibilityLevel < 1.3)
1584
30.5k
        if (embed != FONT_EMBED_NO && font->FontType == ft_CID_TrueType)
1585
0
            return_error(gs_error_rangecheck);
1586
64.7k
    if (embed == FONT_EMBED_STANDARD && pdev->CompatibilityLevel < 2.0) {
1587
3.91k
        pdf_standard_font_t *psf = &psfa[index];
1588
1589
3.91k
        if (psf->pdfont == NULL ||
1590
3.91k
                !pdf_is_compatible_encoding(pdev, psf->pdfont, font,
1591
3.91k
                        cgp->s, cgp->num_all_chars)) {
1592
3.91k
            code = pdf_font_std_alloc(pdev, ppdfont, (psf->pdfont == NULL), base_font->id,
1593
3.91k
                                      (gs_font_base *)base_font, index);
1594
3.91k
            if (code < 0)
1595
0
                return code;
1596
3.91k
            if (psf->pdfont == NULL)
1597
0
                psf->pdfont = *ppdfont;
1598
3.91k
            (*ppdfont)->u.simple.BaseEncoding = pdf_refine_encoding_index(pdev,
1599
3.91k
                ((const gs_font_base *)base_font)->nearest_encoding_index, true);
1600
3.91k
            code = 1;
1601
3.91k
        } else
1602
0
            *ppdfont = psf->pdfont;
1603
3.91k
        return code;
1604
3.91k
    }
1605
1606
60.8k
    if (uid_is_XUID(&bfont->UID)){
1607
37.1k
        int size = uid_XUID_size(&bfont->UID);
1608
37.1k
        long *xvalues = uid_XUID_values(&bfont->UID);
1609
37.1k
        if (xvalues && size >= 2 && xvalues[0] == 1000000) {
1610
37.1k
            XUID = xvalues[size - 1];
1611
37.1k
        }
1612
37.1k
    }
1613
1614
60.8k
    switch (font->FontType) {
1615
193
    case ft_CID_encrypted:
1616
2.33k
    case ft_CID_TrueType:
1617
2.33k
        font_alloc = pdf_font_cidfont_alloc;
1618
2.33k
        break;
1619
24.6k
    case ft_encrypted:
1620
25.5k
    case ft_encrypted2:
1621
57.3k
    case ft_TrueType:
1622
57.3k
        font_alloc = pdf_font_simple_alloc;
1623
57.3k
        break;
1624
356
    case ft_user_defined:
1625
1.18k
    case ft_PDF_user_defined:
1626
1.18k
    case ft_PCL_user_defined:
1627
1.18k
    case ft_MicroType:
1628
1.18k
    case ft_GL2_stick_user_defined:
1629
1.18k
    case ft_GL2_531:
1630
1.18k
        code = pdf_make_font3_resource(pdev, font, ppdfont);
1631
1.18k
        if (code < 0)
1632
0
            return code;
1633
1.18k
        (*ppdfont)->XUID = XUID;
1634
1.18k
        return 1;
1635
0
    default:
1636
0
        return_error(gs_error_invalidfont);
1637
60.8k
    }
1638
1639
    /* Create an appropriate font resource and descriptor. */
1640
59.6k
    if (embed == FONT_EMBED_YES) {
1641
        /*
1642
         * HACK: Acrobat Reader 3 has a bug that makes cmap formats 4
1643
         * and 6 not work in embedded TrueType fonts.  Consequently, it
1644
         * can only handle embedded TrueType fonts if all the glyphs
1645
         * referenced by the Encoding have numbers 0-255.  Check for
1646
         * this now.
1647
         */
1648
59.5k
        if (font->FontType == ft_TrueType &&
1649
59.5k
            pdev->CompatibilityLevel <= 1.2 && !pdev->ForOPDFRead
1650
59.5k
            ) {
1651
0
            int i;
1652
1653
0
            for (i = 0; i <= 0xff; ++i) {
1654
0
                gs_glyph glyph =
1655
0
                    font->procs.encode_char(font, (gs_char)i,
1656
0
                                            GLYPH_SPACE_INDEX);
1657
1658
0
                if (glyph == GS_NO_GLYPH ||
1659
0
                    (glyph >= GS_MIN_GLYPH_INDEX &&
1660
0
                     glyph <= GS_MIN_GLYPH_INDEX + 0xff)
1661
0
                    )
1662
0
                    continue;
1663
                /* Can't embed, punt. */
1664
0
                return_error(gs_error_rangecheck);
1665
0
            }
1666
0
        }
1667
59.5k
    }
1668
59.6k
    if ((code = pdf_font_descriptor_alloc(pdev, &pfd,
1669
59.6k
                                          (gs_font_base *)base_font,
1670
59.6k
                                          embed == FONT_EMBED_YES)) < 0 ||
1671
59.6k
        (code = font_alloc(pdev, &pdfont, base_font->id, pfd)) < 0
1672
59.6k
        )
1673
26.3k
        return code;
1674
1675
33.3k
    pdfont->XUID = XUID;
1676
33.3k
    pdf_do_subset_font(pdev, pfd->base_font, -1);
1677
33.3k
    if (!embed)
1678
0
        pfd->base_font->do_subset = false;
1679
1680
    /* If we have a TrueType font to embed, and we're producing an (E)PS output
1681
     * file, and we are subsetting the font, then the font we produce will *NOT*
1682
     * have the CMAP from the original TrueType font, we will generate a Windows 3,1
1683
     * and a MacRoman 1,0 CMAP, and the opdfread.ps code will prefer to use the
1684
     * MacRoman one. So, in this case, use MacRoman otherwise we will end up
1685
     * with erroneous encodings. Of course, when we are not subssetting the font
1686
     * we do want ot use the original fonts encoding.
1687
     */
1688
33.3k
    if ((font->FontType == ft_TrueType && pdev->ForOPDFRead)) {
1689
4.00k
        if (pfd->base_font->do_subset == DO_SUBSET_YES)
1690
4.00k
            BaseEncoding = pdf_refine_encoding_index(pdev,
1691
4.00k
                ENCODING_INDEX_MACROMAN, false);
1692
0
        else
1693
0
            BaseEncoding = pdf_refine_encoding_index(pdev,
1694
0
                ((const gs_font_base *)base_font)->nearest_encoding_index, false);
1695
4.00k
    }
1696
33.3k
    if (font->FontType == ft_encrypted || font->FontType == ft_encrypted2
1697
33.3k
        || (font->FontType == ft_TrueType && (((((const gs_font_base *)base_font)->nearest_encoding_index != ENCODING_INDEX_UNKNOWN && pfd->base_font->do_subset == DO_SUBSET_NO)) || embed != FONT_EMBED_YES))) {
1698
        /* Yet more crazy heuristics. If we embed a TrueType font and don't subset it, then
1699
         * we preserve the CMAP subtable(s) rather than generatng new ones. The problem is
1700
         * that if we ake the font symbolic, Acrobat uses the 1,0 CMAP, whereas if we don't
1701
         * It uses the 3,1 CMAP (and the Encoding). If these two are not compatible, then
1702
         * the result will be different. So, if we are not subsetting the font, and the original
1703
         * font wasn't symbolic (it has an Encoding) then we will create a new Encoding, and
1704
         * in pdf_write_font_descriptor we wil take back off the symbolic flag.
1705
         */
1706
        /* Removed the addition of Encodings to TrueType fonts as we always write
1707
         * these with the Symbolic flag set. The comment below explains why we
1708
         * previously wrote these, but as the comment notes this was incorrect.
1709
         * Removed as it is causing preflight problems, and is specifically
1710
         * disallowed with PDF/A output.
1711
         */
1712
        /*
1713
         * We write True Types with Symbolic flag set.
1714
         * PDF spec says that "symbolic font should not specify Encoding entry"
1715
         * (see section 5.5, the article "Encodings for True Type fonts", paragraph 3).
1716
         * However Acrobat Reader 4,5,6 fail when TT font with no Encoding
1717
         * appears in a document together with a CID font with a non-standard CMap
1718
         * (AR 4 and 5 claim "The encoding (CMap) specified by a font is corrupted."
1719
         * (we read it as "The encoding or CMap specified by a font is corrupted.",
1720
         * and apply the 1st alternative)). We believe that AR is buggy,
1721
         * and therefore we write an Encoding with non-CID True Type fonts.
1722
         * Hopely other viewers can ignore Encoding in such case. Actually in this case
1723
         * an Encoding doesn't add an useful information.
1724
         */
1725
25.5k
        BaseEncoding = pdf_refine_encoding_index(pdev,
1726
25.5k
            ((const gs_font_base *)base_font)->nearest_encoding_index, false);
1727
25.5k
    }
1728
33.3k
    if (!pdf_is_CID_font(font)) {
1729
31.9k
        pdfont->u.simple.BaseEncoding = BaseEncoding;
1730
31.9k
        pdfont->mark_glyph = font->dir->ccache.mark_glyph;
1731
31.9k
    }
1732
1733
33.3k
    if (pdev->PDFA != 0 && font->FontType == ft_TrueType) {
1734
        /* The Adobe preflight tool for PDF/A
1735
           checks whether Width or W include elements
1736
           for all characters in the True Type font.
1737
           Due to that we need to provide a width
1738
           for .notdef glyph.
1739
           (It's a part of the bug 688790).
1740
         */
1741
0
        gs_font_base *cfont = pdf_font_descriptor_font(pfd, false/*any*/);
1742
0
        gs_glyph notdef_glyph = copied_get_notdef((const gs_font *)cfont);
1743
0
        pdf_glyph_widths_t widths;
1744
0
        double cdevproc_result[10] = {0,0,0,0,0, 0,0,0,0,0};
1745
0
        double *w, *v, *w0;
1746
1747
0
        if (notdef_glyph != GS_NO_GLYPH) {
1748
0
            code = pdf_obtain_cidfont_widths_arrays(pdev, pdfont, font->WMode, &w, &w0, &v);
1749
0
            if (code < 0)
1750
0
                return code;
1751
0
            widths.Width.w = 0;
1752
0
            code = pdf_glyph_widths(pdfont, font->WMode, notdef_glyph,
1753
0
                 font, &widths, cdevproc_result);
1754
0
            if (code < 0)
1755
0
                return code;
1756
0
            w[0] = widths.Width.w;
1757
0
            pdfont->used[0] |= 0x80;
1758
0
        }
1759
0
    }
1760
33.3k
    if (embed == FONT_EMBED_NO && orig_type != ft_undefined) {
1761
0
        pfd->FontType = orig_type;
1762
0
    }
1763
33.3k
    *ppdfont = pdfont;
1764
33.3k
    return 1;
1765
33.3k
}
1766
1767
/*
1768
 * Check for simple font.
1769
 */
1770
bool
1771
pdf_is_simple_font(gs_font *font)
1772
11.6M
{
1773
11.6M
    return (font->FontType == ft_encrypted ||
1774
11.6M
            font->FontType == ft_encrypted2 ||
1775
11.6M
            font->FontType == ft_TrueType ||
1776
11.6M
            font->FontType == ft_user_defined ||
1777
11.6M
            font->FontType == ft_PDF_user_defined ||
1778
11.6M
            font->FontType == ft_PCL_user_defined ||
1779
11.6M
            font->FontType == ft_MicroType ||
1780
11.6M
            font->FontType == ft_GL2_stick_user_defined ||
1781
11.6M
            font->FontType == ft_GL2_531);
1782
11.6M
}
1783
1784
/*
1785
 * Check for CID font.
1786
 */
1787
bool
1788
pdf_is_CID_font(gs_font *font)
1789
2.17M
{
1790
2.17M
    return (font->FontType == ft_CID_encrypted ||
1791
2.17M
            font->FontType == ft_CID_TrueType);
1792
2.17M
}
1793
1794
/*
1795
 * Enumerate glyphs for a text.
1796
 */
1797
static int
1798
pdf_next_char_glyph(gs_text_enum_t *penum, const gs_string *pstr,
1799
               /* const */ gs_font *font, bool font_is_simple,
1800
               gs_char *char_code, gs_char *cid, gs_glyph *glyph)
1801
153M
{
1802
153M
    int code = font->procs.next_char_glyph(penum, char_code, glyph);
1803
1804
153M
    if (code == 2)                /* end of string */
1805
11.6M
        return code;
1806
141M
    if (code < 0)
1807
0
        return code;
1808
141M
    if (font_is_simple) {
1809
141M
        *cid = *char_code;
1810
141M
        *glyph = font->procs.encode_char(font, *char_code, GLYPH_SPACE_NAME);
1811
141M
        if (*glyph == GS_NO_GLYPH)
1812
2.53k
            return 3;
1813
141M
    } else {
1814
0
        if (*glyph < GS_MIN_CID_GLYPH)
1815
0
            return 3; /* Not sure why, copied from scan_cmap_text. */
1816
0
        *cid = *glyph - GS_MIN_CID_GLYPH; /* CID */
1817
0
    }
1818
141M
    return 0;
1819
141M
}
1820
1821
static void
1822
store_glyphs(pdf_char_glyph_pairs_t *cgp,
1823
             byte *glyph_usage, int char_cache_size,
1824
             gs_char char_code, gs_char cid, gs_glyph glyph)
1825
24.8M
{
1826
24.8M
    int j;
1827
1828
145M
    for (j = 0; j < cgp->num_all_chars; j++)
1829
126M
        if (cgp->s[j].chr == cid)
1830
6.18M
            break;
1831
24.8M
    if (j < cgp->num_all_chars)
1832
6.18M
        return;
1833
18.6M
    cgp->s[cgp->num_all_chars].glyph = glyph;
1834
18.6M
    cgp->s[cgp->num_all_chars].chr = char_code;
1835
18.6M
    cgp->num_all_chars++;
1836
18.6M
    if (glyph_usage == 0 || !(glyph_usage[cid / 8] & (0x80 >> (cid & 7)))) {
1837
3.28M
        cgp->s[cgp->unused_offset + cgp->num_unused_chars].glyph = glyph;
1838
3.28M
        cgp->s[cgp->unused_offset + cgp->num_unused_chars].chr = char_code;
1839
3.28M
        cgp->num_unused_chars++;
1840
3.28M
    }
1841
    /* We are disliked that gs_copied_can_copy_glyphs can get redundant
1842
     * glyphs, if Encoding specifies several codes for same glyph.
1843
     * But we need the positional correspondence
1844
     * of glyphs to codes for pdf_is_compatible_encoding.
1845
     * Redundant glyphs isn't a big payment for it
1846
     * because they happen seldom.
1847
     */
1848
18.6M
}
1849
1850
static gs_char
1851
pdf_new_char_code_in_pdfont(pdf_char_glyph_pairs_t *cgp, gs_glyph glyph, int *last_reserved_char)
1852
0
{   /* Returns 256 if encoding overflows. */
1853
0
    int j, ch;
1854
1855
0
    for (j = 0; j < cgp->num_all_chars; j++)
1856
0
        if (cgp->s[j].glyph == glyph)
1857
0
            break;
1858
0
    if (j < cgp->num_all_chars)
1859
0
        return cgp->s[j].chr;
1860
0
    ch = ++*last_reserved_char;
1861
0
    cgp->s[cgp->num_all_chars].glyph = glyph;
1862
0
    cgp->s[cgp->num_all_chars].chr = ch;
1863
0
    cgp->num_all_chars++;
1864
0
    cgp->s[cgp->unused_offset + cgp->num_unused_chars].glyph = glyph;
1865
0
    cgp->s[cgp->unused_offset + cgp->num_unused_chars].chr = ch;
1866
0
    cgp->num_unused_chars++;
1867
0
    return ch;
1868
0
}
1869
1870
static gs_char
1871
pdf_reserve_char_code_in_pdfont(pdf_font_resource_t *pdfont, pdf_char_glyph_pairs_t *cgp, gs_glyph glyph,
1872
                                int *last_reserved_char)
1873
0
{   /* Returns 256 if encoding overflows. */
1874
    /* This function is pretty slow, but we can't find a better algorithm.
1875
       It works for the case of glyphshow with no proper encoding,
1876
       which we believe comes from poorly designed documents.
1877
     */
1878
0
    int j, ch;
1879
1880
0
    for (j = 0; j < cgp->num_all_chars; j++)
1881
0
        if (cgp->s[j].glyph == glyph)
1882
0
            break;
1883
0
    if (j < cgp->num_all_chars)
1884
0
        return cgp->s[j].chr;
1885
1886
0
    for (ch = 0; ch < 256; ch++) {
1887
0
        pdf_encoding_element_t *pet = &pdfont->u.simple.Encoding[ch];
1888
1889
0
        if (glyph == pet->glyph)
1890
0
            return ch;
1891
0
    }
1892
    /* If the font has a known encoding, prefer .notdef codes. */
1893
0
    if (pdfont->u.simple.preferred_encoding_index != -1) {
1894
0
        const ushort *enc = gs_c_known_encodings[pdfont->u.simple.preferred_encoding_index];
1895
1896
0
        for (ch = *last_reserved_char + 1; ch < 256; ch++) {
1897
0
            pdf_encoding_element_t *pet = &pdfont->u.simple.Encoding[ch];
1898
1899
0
            if (pet->glyph == GS_NO_GLYPH && enc[ch] == pdfont->u.simple.standard_glyph_code_for_notdef) {
1900
0
                *last_reserved_char = ch;
1901
0
                break;
1902
0
            }
1903
0
        }
1904
0
    }
1905
    /* Otherwise use any code unused in the font. */
1906
0
    if (ch > 255) {
1907
0
        ch = *last_reserved_char + 1;
1908
0
        for (; ch < 255; ch++) {
1909
0
            pdf_encoding_element_t *pet = &pdfont->u.simple.Encoding[ch];
1910
1911
0
            if (pet->glyph == GS_NO_GLYPH)
1912
0
                break;
1913
0
        }
1914
0
        *last_reserved_char = ch;
1915
0
    }
1916
0
    cgp->s[cgp->num_all_chars].glyph = glyph;
1917
0
    cgp->s[cgp->num_all_chars].chr = ch;
1918
0
    cgp->num_all_chars++;
1919
0
    cgp->s[cgp->unused_offset + cgp->num_unused_chars].glyph = glyph;
1920
0
    cgp->s[cgp->unused_offset + cgp->num_unused_chars].chr = ch;
1921
0
    cgp->num_unused_chars++;
1922
0
    return ch;
1923
0
}
1924
1925
/* Allocate storage for the glyph set of the text. */
1926
static int
1927
pdf_alloc_text_glyphs_table(gx_device_pdf *pdev, pdf_text_enum_t *penum, const gs_string *pstr)
1928
6.03M
{
1929
6.03M
    const int go = (pstr != NULL ? pstr->size : penum->text.size);
1930
6.03M
    const int struct_size = sizeof(pdf_char_glyph_pairs_t) +
1931
6.03M
                            sizeof(pdf_char_glyph_pair_t) * (2 * go - 1);
1932
6.03M
    pdf_char_glyph_pairs_t *cgp = (pdf_char_glyph_pairs_t *)gs_alloc_bytes(penum->memory,
1933
6.03M
                struct_size, "pdf_alloc_text_glyphs_table");
1934
6.03M
    if (cgp == NULL)
1935
0
        return_error(gs_error_VMerror);
1936
6.03M
    penum->cgp = cgp;
1937
6.03M
    cgp->unused_offset = go;
1938
6.03M
    cgp->num_all_chars = 0;
1939
6.03M
    cgp->num_unused_chars = 0;
1940
6.03M
    return 0;
1941
6.03M
}
1942
1943
/* Build the glyph set of the text. */
1944
static int
1945
pdf_make_text_glyphs_table(pdf_text_enum_t *penum, const gs_string *pstr,
1946
                byte *glyph_usage, int char_cache_size)
1947
6.03M
{
1948
6.03M
    gs_text_enum_t scan = *(gs_text_enum_t *)penum;
1949
6.03M
    gs_font *font = (gs_font *)penum->current_font;
1950
6.03M
    bool font_is_simple = pdf_is_simple_font(font);
1951
6.03M
    pdf_char_glyph_pairs_t *cgp = penum->cgp;
1952
6.03M
    gs_char char_code, cid;
1953
6.03M
    gs_glyph glyph;
1954
6.03M
    int code;
1955
1956
6.03M
    cgp->num_unused_chars = 0;
1957
6.03M
    cgp->num_all_chars = 0;
1958
6.03M
    if (pstr != NULL) {
1959
6.03M
        scan.text.data.bytes = pstr->data;
1960
6.03M
        scan.text.size = pstr->size;
1961
6.03M
        scan.index = 0;
1962
        /* if TEXT_FROM_CHARS the data was converted to bytes earlier */
1963
6.03M
        if ( scan.text.operation & TEXT_FROM_CHARS )
1964
27.8k
            scan.text.operation = ((scan.text.operation & ~TEXT_FROM_CHARS) | TEXT_FROM_STRING);
1965
6.03M
    }
1966
30.8M
    for (;;) {
1967
30.8M
        code = pdf_next_char_glyph(&scan, pstr, font, font_is_simple,
1968
30.8M
                                   &char_code, &cid, &glyph);
1969
30.8M
        if (code == 2)                /* end of string */
1970
6.03M
            break;
1971
24.8M
        if (code == 3)                /* no glyph */
1972
917
            continue;
1973
24.8M
        if (code < 0)
1974
0
            return code;
1975
24.8M
        if (cgp->num_all_chars > cgp->unused_offset)
1976
0
            return_error(gs_error_unregistered); /* Must not happen. */
1977
24.8M
        if (glyph_usage != 0 && cid > char_cache_size)
1978
0
            continue;
1979
24.8M
        store_glyphs(cgp, glyph_usage, char_cache_size,
1980
24.8M
                     char_code, cid, glyph);
1981
24.8M
    }
1982
6.03M
    return 0;
1983
6.03M
}
1984
1985
/* Build the glyph set of the glyphshow text, and re_encode the text. */
1986
static int
1987
pdf_make_text_glyphs_table_unencoded(gx_device_pdf *pdev, pdf_char_glyph_pairs_t *cgp,
1988
                gs_font *font, const gs_string *pstr, const gs_glyph *gdata,
1989
                int *ps_encoding_index)
1990
0
{
1991
0
    int i, j, code;
1992
0
    gs_char ch;
1993
0
    gs_const_string gname;
1994
0
    gs_glyph *gid = (gs_glyph *)pstr->data; /* pdf_text_process allocs enough space. */
1995
0
    gs_font_base *bfont;
1996
0
    bool unknown = false;
1997
0
    pdf_font_resource_t *pdfont;
1998
0
    int ei, start_ei = -1;
1999
2000
0
    code = pdf_attached_font_resource(pdev, font, &pdfont, NULL, NULL, NULL, NULL);
2001
0
    if (code < 0)
2002
0
        return code;
2003
    /* Translate glyph name indices into gscencs.c indices. */
2004
0
    for (i = 0; i < pstr->size; i++) {
2005
0
        int code = font->procs.glyph_name(font, gdata[i], &gname);
2006
2007
0
        if (code < 0)
2008
0
            return code;
2009
0
        gid[i] = gs_c_name_glyph(gname.data, gname.size);
2010
0
        if (gid[i] == GS_NO_GLYPH) {
2011
            /* Use global glyph name. */
2012
            /* Assuming this can't fail in a middle of a text,
2013
               because TEXT_FROM_GLYPHS never works for Postscript. */
2014
0
            gid[i] = gdata[i];
2015
0
            unknown = true;
2016
0
        }
2017
0
    }
2018
0
do_unknown:
2019
0
    if (unknown) {
2020
0
        int last_reserved_char = -1;
2021
         /* Using global glyph names. */
2022
2023
        /* Try to find an existing font resource, which has necessary glyphs.
2024
           Doing so to prevent creating multiple font copies.
2025
         */
2026
0
        cgp->num_unused_chars = 0;
2027
0
        cgp->num_all_chars = 0;
2028
0
        for (i = 0; i < pstr->size; i++) {
2029
            /* Temporary stub gid instead cid and char_code : */
2030
0
            store_glyphs(cgp, NULL, 0, gdata[i], gdata[i], gdata[i]);
2031
0
        }
2032
0
        code = pdf_find_font_resource(pdev, font, resourceFont, &pdfont, cgp, false);
2033
0
        if (code < 0)
2034
0
            return code;
2035
0
        if (code) {
2036
            /* Found one - make it be current. */
2037
0
            code = pdf_attach_font_resource(pdev, font, pdfont);
2038
0
            if (code < 0)
2039
0
                return code;
2040
0
        }
2041
        /* Try to add glyphs to the current font resource. . */
2042
0
        cgp->num_unused_chars = 0;
2043
0
        cgp->num_all_chars = 0;
2044
2045
0
        if(pdfont != NULL)
2046
0
                last_reserved_char = pdfont->u.simple.last_reserved_char;
2047
2048
0
        for (i = 0; i < pstr->size; i++) {
2049
2050
0
            if (pdfont == NULL)
2051
0
                ch = 256; /* Force new encoding. */
2052
0
            else
2053
0
                ch = pdf_reserve_char_code_in_pdfont(pdfont, cgp, gdata[i], &last_reserved_char);
2054
0
            if (ch > 255) {
2055
0
                if(pdfont != NULL)
2056
0
                        last_reserved_char = pdfont->u.simple.last_reserved_char;
2057
                /* Start a new font/encoding. */
2058
0
                last_reserved_char = -1;
2059
2060
0
                cgp->num_unused_chars = 0;
2061
0
                cgp->num_all_chars = 0;
2062
0
                for (i = 0; i < pstr->size; i++) {
2063
0
                    ch = pdf_new_char_code_in_pdfont(cgp, gdata[i], &last_reserved_char);
2064
0
                    if (ch > 255) {
2065
                        /* More than 255 unknown characters in a text.
2066
                           It must not happen because TEXT_FROM_GLYPHS
2067
                           never works for Postscript. */
2068
0
                        return_error(gs_error_unregistered);
2069
0
                    }
2070
0
                }
2071
0
            }
2072
0
        }
2073
0
        if (pdfont != NULL)
2074
0
            pdfont->u.simple.last_reserved_char = last_reserved_char;
2075
        /* Change glyphs to char codes in the text : */
2076
0
        for (i = 0; i < pstr->size; i++) {
2077
            /* Picked up by Coverity, if pdfont is NULL then the call might dereference it */
2078
0
            if (pdfont != NULL) {
2079
                /* A trick : pdf_reserve_char_code_in_pdfont here simply encodes with cgp. */
2080
0
                ch = pdf_reserve_char_code_in_pdfont(pdfont, cgp, gdata[i], &pdfont->u.simple.last_reserved_char);
2081
0
                pstr->data[i] = ch;
2082
0
            } else {
2083
                /* So if pdffont is NULL, do the 'trick' code mentioned above.
2084
                 * If that fails (I believe it shouod not), then return an error.
2085
                 */
2086
0
                int j;
2087
2088
0
                for (j = 0; j < cgp->num_all_chars; j++)
2089
0
                    if (cgp->s[j].glyph == gdata[i])
2090
0
                        break;
2091
0
                if (j < cgp->num_all_chars)
2092
0
                    pstr->data[i] = cgp->s[j].chr;
2093
0
                else
2094
0
                    return_error(gs_error_unregistered);
2095
0
            }
2096
0
        }
2097
0
        return 0;
2098
0
    }
2099
    /* Now we know it's a base font, bcause it has glyph names. */
2100
0
    bfont = (gs_font_base *)font;
2101
0
    if (start_ei < 0)
2102
0
        start_ei = bfont->nearest_encoding_index;
2103
0
    if (start_ei < 0)
2104
0
        start_ei = 0;
2105
    /* Find an acceptable encodng, starting from start_ei.
2106
       We need a conservative search to minimize the probability
2107
       of encoding conflicts.
2108
     */
2109
0
    for (j = 0, ei = start_ei; gs_c_known_encodings[j]; j++, ei++) {
2110
0
        if (!gs_c_known_encodings[ei])
2111
0
            ei = 0;
2112
        /* Restrict with PDF encodings, because others give frequent conflicts. */
2113
0
        if (ei > 5) /* Hack : gscedata.c must provide a constant. */
2114
0
            continue;
2115
0
        cgp->num_unused_chars = 0;
2116
0
        cgp->num_all_chars = 0;
2117
0
        for (i = 0; i < pstr->size; i++) {
2118
0
            ch = gs_c_decode(gid[i], ei);
2119
0
            if (ch == GS_NO_CHAR)
2120
0
                break;
2121
0
            if (ch > 255)
2122
0
                break; /* MacGlyphEncoding defines extra glyphs. */
2123
            /* pstr->data[i] = (byte)ch; Can't do because pstr->data and gid
2124
               are same pointer. Will do in a separate pass below. */
2125
0
            store_glyphs(cgp, NULL, 0, ch, ch, gdata[i]);
2126
0
        }
2127
0
        *ps_encoding_index = ei;
2128
0
        if (i == pstr->size) {
2129
            /* Change glyphs to char codes in the text : */
2130
0
            for (i = 0; i < pstr->size; i++)
2131
0
                pstr->data[i] = (byte)gs_c_decode(gid[i], ei);
2132
0
            return 0;
2133
0
        }
2134
0
    }
2135
0
    unknown = true;
2136
0
    goto do_unknown;
2137
0
}
2138
2139
/* Get/make font resource for the font with a known encoding. */
2140
static int
2141
pdf_obtain_font_resource_encoded(gx_device_pdf *pdev, gs_font *font,
2142
        pdf_font_resource_t **ppdfont, pdf_char_glyph_pairs_t *cgp)
2143
6.16M
{
2144
6.16M
    int code;
2145
6.16M
    pdf_font_resource_t *pdfont_not_allowed = NULL;
2146
2147
6.16M
    if (*ppdfont != 0) {
2148
5.46M
        gs_font_base *cfont = pdf_font_resource_font(*ppdfont, false);
2149
2150
5.46M
        if (font->FontType != ft_user_defined &&
2151
5.46M
            font->FontType != ft_PDF_user_defined &&
2152
5.46M
            font->FontType != ft_PCL_user_defined &&
2153
5.46M
            font->FontType != ft_MicroType &&
2154
5.46M
            font->FontType != ft_GL2_stick_user_defined &&
2155
5.46M
            font->FontType != ft_GL2_531) {
2156
5.05M
            code = gs_copied_can_copy_glyphs((gs_font *)cfont, font,
2157
5.05M
                        &cgp->s[cgp->unused_offset].glyph, cgp->num_unused_chars,
2158
5.05M
                        sizeof(pdf_char_glyph_pair_t), true);
2159
5.05M
            if (code < 0)
2160
2
                code = 1;
2161
5.05M
        } else
2162
400k
            code = 1;
2163
5.46M
        if (code == 0) {
2164
3.06k
            pdfont_not_allowed = *ppdfont;
2165
3.06k
            *ppdfont = 0;
2166
5.45M
        } else if(!pdf_is_compatible_encoding(pdev, *ppdfont, font,
2167
5.45M
                        cgp->s, cgp->num_all_chars)) {
2168
29
            pdfont_not_allowed = *ppdfont;
2169
29
            *ppdfont = 0;
2170
29
        }
2171
5.46M
    }
2172
6.16M
    if (*ppdfont == 0) {
2173
702k
        gs_font *base_font = font;
2174
702k
        gs_font *below;
2175
702k
        bool same_encoding = true;
2176
2177
        /*
2178
         * Find the "lowest" base font that has the same outlines.
2179
         * We use its FontName for font resource.
2180
         */
2181
704k
        while ((below = base_font->base) != base_font &&
2182
704k
               base_font->procs.same_font(base_font, below, FONT_SAME_OUTLINES))
2183
1.54k
            base_font = below;
2184
702k
        if (base_font != font)
2185
1.54k
            same_encoding = ((base_font->procs.same_font(base_font, font,
2186
1.54k
                              FONT_SAME_ENCODING) & FONT_SAME_ENCODING) != 0);
2187
        /* Find or make font resource. */
2188
702k
        code = pdf_attached_font_resource(pdev, base_font, ppdfont, NULL, NULL, NULL, NULL);
2189
702k
        if (code < 0)
2190
0
            return code;
2191
702k
        if (base_font != font) {
2192
1.54k
            if (pdfont_not_allowed == *ppdfont)
2193
1.54k
                *ppdfont = NULL;
2194
1.54k
        }
2195
702k
        if(*ppdfont != NULL && !pdf_is_compatible_encoding(pdev, *ppdfont,
2196
3.09k
                                    base_font, cgp->s, cgp->num_all_chars))
2197
29
                *ppdfont = NULL;
2198
702k
        if (*ppdfont == NULL || *ppdfont == pdfont_not_allowed) {
2199
702k
            pdf_resource_type_t type =
2200
702k
                (pdf_is_CID_font(base_font) ? resourceCIDFont
2201
702k
                                            : resourceFont);
2202
702k
            *ppdfont = NULL;
2203
702k
            code = pdf_find_font_resource(pdev, base_font, type, ppdfont, cgp, true);
2204
702k
            if (code < 0)
2205
0
                return code;
2206
702k
            if (*ppdfont == NULL) {
2207
551k
                code = pdf_make_font_resource(pdev, base_font, ppdfont, cgp);
2208
551k
                if (code < 0)
2209
514k
                    return code;
2210
551k
            }
2211
188k
            if (base_font != font && same_encoding) {
2212
1.54k
                code = pdf_attach_font_resource(pdev, base_font, *ppdfont);
2213
1.54k
                if (code < 0)
2214
0
                    return code;
2215
1.54k
            }
2216
188k
        }
2217
188k
        code = pdf_attach_font_resource(pdev, font, *ppdfont);
2218
188k
        if (code < 0)
2219
0
            return code;
2220
188k
    }
2221
5.64M
    return 0;
2222
6.16M
}
2223
2224
/* Mark glyphs used in the text with the font resource. */
2225
static int
2226
pdf_mark_text_glyphs(const gs_text_enum_t *penum, const gs_string *pstr,
2227
            byte *glyph_usage, int char_cache_size)
2228
5.64M
{
2229
5.64M
    gs_text_enum_t scan = *penum;
2230
5.64M
    gs_font *font = (gs_font *)penum->current_font;
2231
5.64M
    bool font_is_simple = pdf_is_simple_font(font);
2232
5.64M
    gs_char char_code, cid;
2233
5.64M
    gs_glyph glyph;
2234
2235
5.64M
    if (glyph_usage == NULL)
2236
0
        return 0;
2237
2238
5.64M
    if (pstr != NULL) {
2239
5.64M
        scan.text.data.bytes = pstr->data;
2240
5.64M
        scan.text.size = pstr->size;
2241
5.64M
        scan.index = 0;
2242
        /* if TEXT_FROM_CHARS the data was converted to bytes earlier */
2243
5.64M
        if ( scan.text.operation & TEXT_FROM_CHARS )
2244
53.5k
            scan.text.operation =
2245
53.5k
                ((scan.text.operation & ~TEXT_FROM_CHARS) | TEXT_FROM_STRING);
2246
5.64M
    }
2247
122M
    for (;;) {
2248
122M
        int code = pdf_next_char_glyph(&scan, pstr, font, font_is_simple,
2249
122M
                                       &char_code, &cid, &glyph);
2250
2251
122M
        if (code == 2)                /* end of string */
2252
5.64M
            break;
2253
116M
        if (code == 3)                /* no glyph */
2254
1.61k
            continue;
2255
116M
        if (code < 0)
2256
0
            return code;
2257
116M
        if (cid >= char_cache_size)
2258
0
            continue;
2259
116M
        glyph_usage[cid / 8] |= 0x80 >> (cid & 7);
2260
116M
    }
2261
5.64M
    return 0;
2262
5.64M
}
2263
2264
/* Mark glyphs used in the glyphshow text with the font resource. */
2265
static int
2266
pdf_mark_text_glyphs_unencoded(const gs_text_enum_t *penum, const gs_string *pstr,
2267
            byte *glyph_usage, int char_cache_size)
2268
0
{
2269
0
    int i;
2270
2271
0
    for(i = 0; i < pstr->size; i++) {
2272
0
        byte ch = pstr->data[i];
2273
2274
0
        if (ch >= char_cache_size)
2275
0
            return_error(gs_error_rangecheck);
2276
0
        glyph_usage[ch / 8] |= 0x80 >> (ch & 7);
2277
0
    }
2278
0
    return 0;
2279
0
}
2280
2281
/*
2282
 * Create or find a font resource object for a text.
2283
 */
2284
int
2285
pdf_obtain_font_resource(pdf_text_enum_t *penum,
2286
            const gs_string *pstr, pdf_font_resource_t **ppdfont)
2287
6.16M
{
2288
6.16M
    gx_device_pdf *pdev = (gx_device_pdf *)penum->dev;
2289
6.16M
    gs_font *font = (gs_font *)penum->current_font;
2290
6.16M
    byte *glyph_usage = 0;
2291
6.16M
    double *real_widths;
2292
6.16M
    int char_cache_size, width_cache_size;
2293
6.16M
    int code;
2294
2295
6.16M
    if (font->FontType == ft_composite) {
2296
        /* Must not happen, because we always split composite fonts into descendents. */
2297
0
        return_error(gs_error_unregistered);
2298
0
    }
2299
6.16M
    code = pdf_attached_font_resource(pdev, font, ppdfont,
2300
6.16M
                               &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
2301
    /* *ppdfont is NULL if no resource attached. */
2302
6.16M
    if (code < 0)
2303
0
        return code;
2304
6.16M
    if (penum->cgp == NULL) {
2305
6.03M
        code = pdf_alloc_text_glyphs_table(pdev, penum, pstr);
2306
6.03M
        if (code < 0)
2307
0
            return code;
2308
6.03M
        code = pdf_make_text_glyphs_table(penum, pstr,
2309
6.03M
                            glyph_usage, char_cache_size);
2310
6.03M
        if (code < 0)
2311
0
            return code;
2312
6.03M
    }
2313
6.16M
    code = pdf_obtain_font_resource_encoded(pdev, font, ppdfont, penum->cgp);
2314
6.16M
    if (code < 0)
2315
514k
        return code;
2316
5.64M
    code = pdf_attached_font_resource(pdev, font, ppdfont,
2317
5.64M
                               &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
2318
5.64M
    if (code < 0)
2319
0
        return code;
2320
5.64M
    return pdf_mark_text_glyphs((const gs_text_enum_t *)penum, pstr, glyph_usage, char_cache_size);
2321
5.64M
}
2322
2323
/*
2324
 * Create or find a font resource object for a glyphshow text.
2325
 */
2326
int
2327
pdf_obtain_font_resource_unencoded(pdf_text_enum_t *penum,
2328
            const gs_string *pstr, pdf_font_resource_t **ppdfont, const gs_glyph *gdata)
2329
0
{
2330
0
    gx_device_pdf *pdev = (gx_device_pdf *)penum->dev;
2331
0
    gs_font *font = (gs_font *)penum->current_font;
2332
0
    byte *glyph_usage = 0;
2333
0
    double *real_widths = 0;
2334
0
    int char_cache_size = 0, width_cache_size = 0;
2335
0
    int code, ps_encoding_index = -1;
2336
2337
0
    if (font->FontType == ft_composite) {
2338
        /* Must not happen, because we always split composite fonts into descendents. */
2339
0
        return_error(gs_error_unregistered);
2340
0
    }
2341
0
    code = pdf_attached_font_resource(pdev, font, ppdfont,
2342
0
                               &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
2343
0
    if (code < 0)
2344
0
        return code;
2345
    /* *ppdfont is NULL if no resource attached. */
2346
0
    if (*ppdfont != NULL)
2347
0
        ps_encoding_index = (*ppdfont)->u.simple.preferred_encoding_index;
2348
0
    if (penum->cgp == NULL) {
2349
0
        code = pdf_alloc_text_glyphs_table(pdev, penum, pstr);
2350
0
        if (code < 0)
2351
0
            return code;
2352
0
        code = pdf_make_text_glyphs_table_unencoded(pdev, penum->cgp, font, pstr, gdata, &ps_encoding_index);
2353
0
        if (code < 0)
2354
0
            return code;
2355
0
    }
2356
0
    code = pdf_obtain_font_resource_encoded(pdev, font, ppdfont, penum->cgp);
2357
0
    if (code < 0)
2358
0
        return code;
2359
0
    code = pdf_attached_font_resource(pdev, font, ppdfont,
2360
0
                               &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
2361
0
    if (code < 0)
2362
0
        return code;
2363
0
    (*ppdfont)->u.simple.preferred_encoding_index = ps_encoding_index;
2364
0
    return pdf_mark_text_glyphs_unencoded((const gs_text_enum_t *)penum,
2365
0
                    pstr, glyph_usage, char_cache_size);
2366
0
}
2367
2368
static inline bool
2369
strings_equal(const gs_const_string *s1, const gs_const_string *s2)
2370
392k
{
2371
392k
    return s1->size == s2->size &&
2372
392k
            !memcmp(s1->data, s2->data, s1->size);
2373
392k
}
2374
2375
/*
2376
 * Create or find a parent Type 0 font resource object for a CID font resource.
2377
 */
2378
int
2379
pdf_obtain_parent_type0_font_resource(gx_device_pdf *pdev, pdf_font_resource_t *pdsubf,
2380
                uint font_index, const gs_const_string *CMapName, pdf_font_resource_t **pdfont)
2381
394k
{
2382
394k
    gs_const_string s1;
2383
2384
394k
    if (pdsubf->u.cidfont.parent != 0) {
2385
392k
        s1.data = pdsubf->u.cidfont.parent->u.type0.CMapName_data;
2386
392k
        s1.size = pdsubf->u.cidfont.parent->u.type0.CMapName_size;
2387
392k
    }
2388
2389
394k
    if (pdsubf->u.cidfont.parent != 0 &&
2390
394k
            font_index == pdsubf->u.cidfont.parent->u.type0.font_index &&
2391
394k
            strings_equal(CMapName, &s1))
2392
392k
        *pdfont = pdsubf->u.cidfont.parent;
2393
1.35k
    else {
2394
        /*
2395
         * PDF spec 1.4 section 5.6 "Composite Fonts" says :
2396
         *
2397
         * PDF 1.2 introduces a general architecture for composite fonts that theoretically
2398
         * allows a Type 0 font to have multiple descendants,which might themselves be
2399
         * Type 0 fonts.However,in versions up to and including PDF 1.4,only a single
2400
         * descendant is allowed,which must be a CIDFont (not a font).This restriction
2401
         * may be relaxed in a future PDF version.
2402
         */
2403
2404
1.35k
        if (pdsubf->u.cidfont.parent == NULL ||
2405
1.35k
                pdf_find_type0_font_resource(pdev, pdsubf, CMapName, font_index, pdfont) <= 0) {
2406
1.35k
            int code = pdf_font_type0_alloc(pdev, pdfont, gs_no_id, pdsubf, CMapName);
2407
2408
1.35k
            if (code < 0)
2409
0
                return code;
2410
1.35k
            (*pdfont)->u.type0.font_index = font_index;
2411
1.35k
        }
2412
1.35k
        pdsubf->u.cidfont.parent = *pdfont;
2413
1.35k
    }
2414
394k
    return 0;
2415
394k
}
2416
2417
gs_char
2418
pdf_find_glyph(pdf_font_resource_t *pdfont, gs_glyph glyph)
2419
0
{
2420
0
    if (pdfont->FontType != ft_user_defined &&
2421
0
        pdfont->FontType != ft_PDF_user_defined &&
2422
0
        pdfont->FontType != ft_PCL_user_defined &&
2423
0
        pdfont->FontType != ft_MicroType &&
2424
0
        pdfont->FontType != ft_GL2_stick_user_defined &&
2425
0
        pdfont->FontType != ft_GL2_531)
2426
0
        return GS_NO_CHAR;
2427
0
    else {
2428
0
        pdf_encoding_element_t *pet = pdfont->u.simple.Encoding;
2429
0
        int i, i0 = -1;
2430
2431
0
        if (pdfont->u.simple.FirstChar > pdfont->u.simple.LastChar)
2432
0
            return (gs_char)0;
2433
0
        for (i = pdfont->u.simple.FirstChar; i <= pdfont->u.simple.LastChar; i++, pet++) {
2434
0
            if (pet->glyph == glyph)
2435
0
                return (gs_char)i;
2436
0
            if (i0 == -1 && pet->glyph == GS_NO_GLYPH)
2437
0
                i0 = i;
2438
0
        }
2439
0
        if (i0 != -1)
2440
0
            return (gs_char)i0;
2441
0
        if (i < 256)
2442
0
            return (gs_char)i;
2443
0
        return GS_NO_CHAR;
2444
0
    }
2445
0
}
2446
2447
/*
2448
 * Compute the cached values in the text processing state from the text
2449
 * parameters, current_font, and pgs->ctm.  Return either an error code (<
2450
 * 0) or a mask of operation attributes that the caller must emulate.
2451
 * Currently the only such attributes are TEXT_ADD_TO_ALL_WIDTHS and
2452
 * TEXT_ADD_TO_SPACE_WIDTH.  Note that this procedure fills in all the
2453
 * values in ppts->values, not just the ones that need to be set now.
2454
 */
2455
static int
2456
transform_delta_inverse(const gs_point *pdelta, const gs_matrix *pmat,
2457
                        gs_point *ppt)
2458
561k
{
2459
561k
    int code = gs_distance_transform_inverse(pdelta->x, pdelta->y, pmat, ppt);
2460
561k
    gs_point delta;
2461
2462
561k
    if (code < 0)
2463
0
        return code;
2464
561k
    if (ppt->y == 0)
2465
560k
        return 0;
2466
    /* Check for numerical fuzz. */
2467
786
    code = gs_distance_transform(ppt->x, 0.0, pmat, &delta);
2468
786
    if (code < 0)
2469
0
        return 0;                /* punt */
2470
786
    if (fabs(delta.x - pdelta->x) < 0.01 && fabs(delta.y - pdelta->y) < 0.01) {
2471
        /* Close enough to y == 0: device space error < 0.01 pixel. */
2472
4
        ppt->y = 0;
2473
4
    }
2474
786
    return 0;
2475
786
}
2476
2477
float pdf_calculate_text_size(gs_gstate *pgs, pdf_font_resource_t *pdfont,
2478
                              const gs_matrix *pfmat, gs_matrix *smat, gs_matrix *tmat,
2479
                              gs_font *font, gx_device_pdf *pdev)
2480
5.75M
{
2481
5.75M
    gs_matrix orig_matrix;
2482
5.75M
    double
2483
5.75M
        sx = pdev->HWResolution[0] / 72.0,
2484
5.75M
        sy = pdev->HWResolution[1] / 72.0;
2485
5.75M
    float size;
2486
2487
    /* Get the original matrix of the base font. */
2488
2489
5.75M
    {
2490
5.75M
        gs_font_base *cfont = pdf_font_resource_font(pdfont, false);
2491
2492
5.75M
        if (pdfont->FontType == ft_user_defined ||
2493
5.75M
            pdfont->FontType == ft_PDF_user_defined ||
2494
5.75M
            pdfont->FontType == ft_PCL_user_defined ||
2495
5.75M
            pdfont->FontType == ft_MicroType ||
2496
5.75M
            pdfont->FontType == ft_GL2_stick_user_defined ||
2497
5.75M
            pdfont->FontType == ft_GL2_531)
2498
401k
            orig_matrix = pdfont->u.simple.s.type3.FontMatrix;
2499
5.35M
        else if (cfont != 0) {
2500
            /*
2501
             * The text matrix to be computed relatively to the
2502
             * embedded font matrix.
2503
             */
2504
5.24M
            orig_matrix = cfont->FontMatrix;
2505
5.24M
        } else {
2506
            /*
2507
             * We don't embed the font.
2508
             * The text matrix to be computed relatively to
2509
             * standard font matrix.
2510
             */
2511
114k
            pdf_font_orig_matrix(font, &orig_matrix);
2512
114k
        }
2513
5.75M
    }
2514
2515
    /* Compute the scaling matrix and combined matrix. */
2516
2517
5.75M
    if (gs_matrix_invert(&orig_matrix, smat) < 0) {
2518
21.8k
        gs_make_identity(smat);
2519
21.8k
        gs_make_identity(tmat);
2520
21.8k
        return 1; /* Arbitrary */
2521
21.8k
    }
2522
5.73M
    gs_matrix_multiply(smat, pfmat, smat);
2523
5.73M
    *tmat = ctm_only(pgs);
2524
5.73M
    tmat->tx = tmat->ty = 0;
2525
5.73M
    gs_matrix_multiply(smat, tmat, tmat);
2526
2527
    /* Try to find a reasonable size value.  This isn't necessary, */
2528
    /* but it's worth a little effort. */
2529
2530
5.73M
    size = hypot(tmat->yx, tmat->yy) / sy;
2531
5.73M
    if (size < 0.01)
2532
1.77k
        size = hypot(tmat->xx, tmat->xy) / sx;
2533
5.73M
    if (size < 0.01)
2534
1.33k
        size = 1;
2535
2536
5.73M
    return(size);
2537
5.75M
}
2538
2539
int
2540
pdf_update_text_state(pdf_text_process_state_t *ppts,
2541
                      const pdf_text_enum_t *penum,
2542
                      pdf_font_resource_t *pdfont, const gs_matrix *pfmat)
2543
5.75M
{
2544
5.75M
    gx_device_pdf *const pdev = (gx_device_pdf *)penum->dev;
2545
5.75M
    gs_font *font = penum->current_font;
2546
5.75M
    gs_fixed_point cpt;
2547
5.75M
    gs_matrix smat, tmat;
2548
5.75M
    float size;
2549
5.75M
    float c_s = 0, w_s = 0;
2550
5.75M
    int mask = 0;
2551
5.75M
    int code = gx_path_current_point(gs_text_enum_path(penum), &cpt);
2552
2553
5.75M
    if (code < 0)
2554
0
        return code;
2555
2556
5.75M
    size = pdf_calculate_text_size(penum->pgs, pdfont, pfmat, &smat, &tmat, penum->current_font, pdev);
2557
    /* Check for spacing parameters we can handle, and transform them. */
2558
2559
5.75M
    if (penum->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
2560
326k
        if (penum->current_font->WMode == 0) {
2561
326k
            gs_point pt;
2562
2563
326k
            code = transform_delta_inverse(&penum->text.delta_all, &smat, &pt);
2564
326k
            if (code >= 0 && pt.y == 0)
2565
325k
                c_s = pt.x * size;
2566
782
            else
2567
782
                mask |= TEXT_ADD_TO_ALL_WIDTHS;
2568
326k
        }
2569
0
        else
2570
0
            mask |= TEXT_ADD_TO_ALL_WIDTHS;
2571
326k
    }
2572
2573
5.75M
    if (penum->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
2574
234k
        gs_point pt;
2575
2576
234k
        code = transform_delta_inverse(&penum->text.delta_space, &smat, &pt);
2577
234k
        if (code >= 0 && pt.y == 0 && penum->text.space.s_char == 32)
2578
234k
            w_s = pt.x * size;
2579
0
        else
2580
0
            mask |= TEXT_ADD_TO_SPACE_WIDTH;
2581
234k
    }
2582
    /* Store the updated values. */
2583
2584
5.75M
    tmat.xx /= size;
2585
5.75M
    tmat.xy /= size;
2586
5.75M
    tmat.yx /= size;
2587
5.75M
    tmat.yy /= size;
2588
5.75M
    tmat.tx += fixed2float(cpt.x);
2589
5.75M
    tmat.ty += fixed2float(cpt.y);
2590
2591
5.75M
    ppts->values.character_spacing = c_s;
2592
5.75M
    ppts->values.pdfont = pdfont;
2593
5.75M
    ppts->values.size = size;
2594
5.75M
    ppts->values.matrix = tmat;
2595
5.75M
    ppts->values.render_mode = penum->pgs->text_rendering_mode;
2596
5.75M
    ppts->values.word_spacing = w_s;
2597
5.75M
    ppts->font = font;
2598
2599
5.75M
    if (font->PaintType == 2 && penum->pgs->text_rendering_mode == 0)
2600
0
    {
2601
0
        gs_gstate *pgs = penum->pgs;
2602
0
        gs_font *font = penum->current_font;
2603
0
        double scaled_width = font->StrokeWidth != 0 ? font->StrokeWidth : 0.001;
2604
0
        double saved_width = pgs->line_params.half_width;
2605
        /*
2606
         * See stream_to_text in gdevpdfu.c re the computation of
2607
         * the scaling value.
2608
         */
2609
0
        double scale = 72.0 / pdev->HWResolution[1];
2610
2611
0
        if (font->FontMatrix.yy != 0)
2612
0
            scaled_width *= fabs(font->orig_FontMatrix.yy) * size * scale;
2613
0
        else
2614
0
            scaled_width *= fabs(font->orig_FontMatrix.xy) * size * scale;
2615
2616
0
        if (tmat.yy != 0)
2617
0
            scaled_width *= tmat.yy;
2618
0
        else
2619
0
            scaled_width *= tmat.xy;
2620
2621
0
        ppts->values.render_mode = 1;
2622
2623
        /* Sort out any pending glyphs */
2624
0
        code = pdf_set_PaintType0_params(pdev, pgs, size, scaled_width, &ppts->values);
2625
0
        if (code < 0)
2626
0
            return code;
2627
2628
0
        pgs->line_params.half_width = scaled_width / 2;
2629
0
        code = pdf_set_text_process_state(pdev, (const gs_text_enum_t *)penum,
2630
0
                                      ppts);
2631
0
        if (code < 0)
2632
0
            return code;
2633
2634
0
        pgs->line_params.half_width = saved_width;
2635
5.75M
    } else {
2636
5.75M
        code = pdf_set_text_process_state(pdev, (const gs_text_enum_t *)penum,
2637
5.75M
                                      ppts);
2638
5.75M
    }
2639
5.75M
    return (code < 0 ? code : mask);
2640
5.75M
}
2641
2642
/*
2643
 * Set up commands to make the output state match the processing state.
2644
 * General graphics state commands are written now; text state commands
2645
 * are written later.
2646
 */
2647
int
2648
pdf_set_text_process_state(gx_device_pdf *pdev,
2649
                           const gs_text_enum_t *pte,        /* for pdcolor, pgs */
2650
                           pdf_text_process_state_t *ppts)
2651
5.75M
{
2652
    /*
2653
     * Setting the stroke parameters may exit text mode, causing the
2654
     * settings of the text parameters to be lost.  Therefore, we set the
2655
     * stroke parameters first.
2656
     */
2657
5.75M
    if (pdf_render_mode_uses_stroke(pdev, &ppts->values)) {
2658
        /* Write all the parameters for stroking. */
2659
916
        gs_gstate *pgs = pte->pgs;
2660
916
        float save_width = pgs->line_params.half_width;
2661
916
        int code;
2662
2663
916
        if (pdev->context == PDF_IN_STRING) {
2664
661
            code = sync_text_state(pdev);
2665
661
            if (code < 0)
2666
0
                return code;
2667
661
        }
2668
2669
916
        code = pdf_open_contents(pdev, PDF_IN_STREAM);
2670
916
        if (code < 0)
2671
0
            return code;
2672
2673
916
        code = pdf_prepare_stroke(pdev, pgs, true);
2674
916
        if (code >= 0) {
2675
916
            code = gdev_vector_prepare_stroke((gx_device_vector *)pdev,
2676
916
                                              pgs, NULL, NULL, 1);
2677
916
            if (code < 0)
2678
0
                return code;
2679
916
        }
2680
2681
916
        code = pdf_open_contents(pdev, PDF_IN_STRING);
2682
916
        if (code < 0)
2683
0
            return code;
2684
2685
916
        pgs->line_params.half_width = save_width;
2686
916
    }
2687
2688
    /* Now set all the other parameters. */
2689
2690
5.75M
    return pdf_set_text_state_values(pdev, &ppts->values);
2691
5.75M
}
2692
2693
static int
2694
store_glyph_width(pdf_glyph_width_t *pwidth, int wmode, const gs_matrix *scale,
2695
                  const gs_glyph_info_t *pinfo)
2696
4.82M
{
2697
4.82M
    double w, v;
2698
2699
4.82M
    gs_distance_transform(pinfo->width[wmode].x, pinfo->width[wmode].y, scale, &pwidth->xy);
2700
4.82M
    if (wmode)
2701
2.75k
        w = pwidth->xy.y, v = pwidth->xy.x;
2702
4.82M
    else
2703
4.82M
        w = pwidth->xy.x, v = pwidth->xy.y;
2704
4.82M
    pwidth->w = w;
2705
4.82M
    if (v != 0)
2706
78
        return 1;
2707
4.82M
    gs_distance_transform(pinfo->v.x, pinfo->v.y, scale, &pwidth->v);
2708
4.82M
    return 0;
2709
4.82M
}
2710
2711
static int
2712
get_missing_width(gs_font_base *cfont, int wmode, const gs_matrix *scale_c,
2713
                    pdf_glyph_widths_t *pwidths)
2714
14.2k
{
2715
14.2k
    gs_font_info_t finfo;
2716
14.2k
    int code;
2717
2718
14.2k
    code = cfont->procs.font_info((gs_font *)cfont, NULL,
2719
14.2k
                                  FONT_INFO_MISSING_WIDTH, &finfo);
2720
14.2k
    if (code < 0)
2721
0
        return code;
2722
14.2k
    if (wmode) {
2723
1.44k
        gs_distance_transform(0.0, -finfo.MissingWidth, scale_c, &pwidths->real_width.xy);
2724
1.44k
        pwidths->Width.xy.x = 0;
2725
1.44k
        pwidths->Width.xy.y = pwidths->real_width.xy.y;
2726
1.44k
        pwidths->Width.w = pwidths->real_width.w =
2727
1.44k
                pwidths->Width.xy.y;
2728
1.44k
        pwidths->Width.v.x = - pwidths->Width.xy.y / 2;
2729
1.44k
        pwidths->Width.v.y = - pwidths->Width.xy.y;
2730
12.8k
    } else {
2731
12.8k
        gs_distance_transform(finfo.MissingWidth, 0.0, scale_c, &pwidths->real_width.xy);
2732
12.8k
        pwidths->Width.xy.x = pwidths->real_width.xy.x;
2733
12.8k
        pwidths->Width.xy.y = 0;
2734
12.8k
        pwidths->Width.w = pwidths->real_width.w =
2735
12.8k
                pwidths->Width.xy.x;
2736
12.8k
        pwidths->Width.v.x = pwidths->Width.v.y = 0;
2737
12.8k
    }
2738
    /*
2739
     * Don't mark the width as known, just in case this is an
2740
     * incrementally defined font.
2741
     */
2742
14.2k
    return 1;
2743
14.2k
}
2744
2745
/*
2746
 * Get the widths (unmodified from the copied font,
2747
 * and possibly modified from the original font) of a given glyph.
2748
 * Return 1 if the width was defaulted to MissingWidth.
2749
 * Return TEXT_PROCESS_CDEVPROC if a CDevProc callout is needed.
2750
 * cdevproc_result != NULL if we restart after a CDevProc callout.
2751
 */
2752
int
2753
pdf_glyph_widths(pdf_font_resource_t *pdfont, int wmode, gs_glyph glyph,
2754
                 gs_font *orig_font, pdf_glyph_widths_t *pwidths,
2755
                 const double cdevproc_result[10])
2756
2.43M
{
2757
2.43M
    gs_font_base *cfont = pdf_font_resource_font(pdfont, false);
2758
2.43M
    gs_font *ofont = orig_font;
2759
2.43M
    gs_glyph_info_t info;
2760
2.43M
    gs_matrix scale_c, scale_o;
2761
2.43M
    int code, rcode = 0;
2762
2.43M
    gs_point v;
2763
2.43M
    int allow_cdevproc_callout = (orig_font->FontType == ft_CID_TrueType
2764
2.43M
                || orig_font->FontType == ft_CID_encrypted
2765
2.43M
                ? GLYPH_INFO_CDEVPROC : 0); /* fixme : allow more font types. */
2766
2767
2.43M
    if (ofont->FontType == ft_composite)
2768
0
        return_error(gs_error_unregistered); /* Must not happen. */
2769
2.43M
    code = glyph_orig_matrix((const gs_font *)cfont, glyph, &scale_c);
2770
2.43M
    if (code < 0)
2771
0
        return code;
2772
2.43M
    code = glyph_orig_matrix(ofont, glyph, &scale_o);
2773
2.43M
    if (code < 0)
2774
0
        return code;
2775
2.43M
    gs_matrix_scale(&scale_c, 1000.0, 1000.0, &scale_c);
2776
2.43M
    gs_matrix_scale(&scale_o, 1000.0, 1000.0, &scale_o);
2777
2.43M
    pwidths->Width.v.x = pwidths->Width.v.y = 0;
2778
2.43M
    pwidths->real_width.v.x = pwidths->real_width.v.y = 0;
2779
2.43M
    pwidths->real_width.w = pwidths->real_width.xy.x = pwidths->real_width.xy.y = 0;
2780
2.43M
    pwidths->replaced_v = false;
2781
2.43M
    pwidths->ignore_wmode = false;
2782
2.43M
    if (glyph == GS_NO_GLYPH)
2783
221
        return get_missing_width(cfont, wmode, &scale_c, pwidths);
2784
2.43M
    code = cfont->procs.glyph_info((gs_font *)cfont, glyph, NULL,
2785
2.43M
                                    GLYPH_INFO_WIDTH0 |
2786
2.43M
                                    (GLYPH_INFO_WIDTH0 << wmode) |
2787
2.43M
                                    GLYPH_INFO_OUTLINE_WIDTHS |
2788
2.43M
                                    (GLYPH_INFO_VVECTOR0 << wmode),
2789
2.43M
                                    &info);
2790
    /* For CID fonts the PDF spec requires the x-component of v-vector
2791
       to be equal to half glyph width, and AR5 takes it from W, DW.
2792
       So make a compatibe data here.
2793
     */
2794
2.43M
    if ((code == gs_error_undefined || !(info.members & (GLYPH_INFO_WIDTH0 << wmode)))) {
2795
        /* If we got an undefined error, and its a type 1/CFF font, try to
2796
         * find the /.notdef glyph and use its width instead (as this is the
2797
         * glyph which will be rendered). We don't do this for other font types
2798
         * as it seems Acrobat/Distiller may not do so either.
2799
         */
2800
24.5k
        if (code == gs_error_undefined && (ofont->FontType == ft_encrypted || ofont->FontType == ft_encrypted2)) {
2801
11.9k
            int index;
2802
11.9k
            gs_glyph notdef_glyph;
2803
2804
11.9k
            v.x = v.y = 0;
2805
2806
11.9k
            for (index = 0;
2807
1.36M
                (ofont->procs.enumerate_glyph((gs_font *)ofont, &index,
2808
1.36M
                (GLYPH_SPACE_NAME), &notdef_glyph)) >= 0 &&
2809
1.36M
                index != 0;) {
2810
1.36M
                    if (gs_font_glyph_is_notdef((gs_font_base *)ofont, notdef_glyph)) {
2811
11.9k
                        code = ofont->procs.glyph_info((gs_font *)ofont, notdef_glyph, NULL,
2812
11.9k
                                            GLYPH_INFO_WIDTH0 << wmode,
2813
11.9k
                                            &info);
2814
2815
11.9k
                    if (code < 0)
2816
0
                        return code;
2817
11.9k
                    code = store_glyph_width(&pwidths->Width, wmode, &scale_c, &info);
2818
11.9k
                    if (code < 0)
2819
0
                        return code;
2820
11.9k
                    rcode |= code;
2821
11.9k
                    if (info.members  & (GLYPH_INFO_VVECTOR0 << wmode))
2822
0
                        gs_distance_transform(info.v.x, info.v.y, &scale_c, &v);
2823
11.9k
                    else
2824
11.9k
                        v.x = v.y = 0;
2825
11.9k
                    break;
2826
11.9k
                }
2827
1.36M
            }
2828
12.5k
        } else {
2829
12.5k
        code = get_missing_width(cfont, wmode, &scale_c, pwidths);
2830
12.5k
            if (code < 0)
2831
0
                v.y = 0;
2832
12.5k
            else
2833
12.5k
                v.y = pwidths->Width.v.y;
2834
12.5k
            if (wmode) {
2835
1.44k
                pdf_glyph_widths_t widths1;
2836
2837
1.44k
                if (get_missing_width(cfont, 0, &scale_c, &widths1) < 0)
2838
0
                    v.x = 0;
2839
1.44k
                else
2840
1.44k
                    v.x = widths1.Width.w / 2;
2841
1.44k
            } else
2842
11.1k
                v.x = pwidths->Width.v.x;
2843
12.5k
        }
2844
2.41M
    } else if (code < 0)
2845
0
        return code;
2846
2.41M
    else {
2847
2.41M
        code = store_glyph_width(&pwidths->Width, wmode, &scale_c, &info);
2848
2.41M
        if (code < 0)
2849
0
            return code;
2850
2.41M
        rcode |= code;
2851
2.41M
        if (info.members  & (GLYPH_INFO_VVECTOR0 << wmode))
2852
2.41M
            gs_distance_transform(info.v.x, info.v.y, &scale_c, &v);
2853
1.29k
        else
2854
1.29k
            v.x = v.y = 0;
2855
2.41M
        if (wmode && pdf_is_CID_font(ofont)) {
2856
1.29k
            if (info.members & (GLYPH_INFO_WIDTH0 << wmode)) {
2857
1.29k
                gs_point xy;
2858
2859
1.29k
                gs_distance_transform(info.width[0].x, info.width[0].y, &scale_c, &xy);
2860
1.29k
                v.x = xy.x / 2;
2861
1.29k
            } else {
2862
0
                pdf_glyph_widths_t widths1;
2863
2864
0
                if (get_missing_width(cfont, 0, &scale_c, &widths1) < 0)
2865
0
                    v.x = 0;
2866
0
                else
2867
0
                    v.x = widths1.Width.w / 2;
2868
0
            }
2869
1.29k
        }
2870
2.41M
    }
2871
2.43M
    pwidths->Width.v = v;
2872
2.43M
    if (code > 0 && !pdf_is_CID_font(ofont))
2873
4.33k
        pwidths->Width.xy.x = pwidths->Width.xy.y = pwidths->Width.w = 0;
2874
2.43M
    if (cdevproc_result == NULL) {
2875
2.43M
        info.members = 0;
2876
2.43M
        code = ofont->procs.glyph_info(ofont, glyph, NULL,
2877
2.43M
                                            (GLYPH_INFO_WIDTH0 << wmode) |
2878
2.43M
                                            (GLYPH_INFO_VVECTOR0 << wmode) |
2879
2.43M
                                            allow_cdevproc_callout,
2880
2.43M
                                            &info);
2881
        /* fixme : Move this call before cfont->procs.glyph_info. */
2882
2.43M
        if (info.members & GLYPH_INFO_CDEVPROC) {
2883
0
            if (allow_cdevproc_callout)
2884
0
                return TEXT_PROCESS_CDEVPROC;
2885
0
        else
2886
0
            return_error(gs_error_rangecheck);
2887
0
        }
2888
2.43M
    } else {
2889
0
        info.width[0].x = cdevproc_result[0];
2890
0
        info.width[0].y = cdevproc_result[1];
2891
0
        info.width[1].x = cdevproc_result[6];
2892
0
        info.width[1].y = cdevproc_result[7];
2893
0
        info.v.x = (wmode ? cdevproc_result[8] : 0);
2894
0
        info.v.y = (wmode ? cdevproc_result[9] : 0);
2895
0
        info.members = (GLYPH_INFO_WIDTH0 << wmode) |
2896
0
                       (wmode ? GLYPH_INFO_VVECTOR1 : 0);
2897
0
        code = 0;
2898
0
    }
2899
2.43M
    if (code == gs_error_undefined || !(info.members & (GLYPH_INFO_WIDTH0 << wmode)))
2900
35.7k
        pwidths->real_width = pwidths->Width;
2901
2.40M
    else if (code < 0)
2902
0
        return code;
2903
2.40M
    else {
2904
2.40M
        if ((info.members & (GLYPH_INFO_VVECTOR0 | GLYPH_INFO_VVECTOR1)) != 0) {
2905
1.82M
            pwidths->replaced_v = true;
2906
1.82M
            if ((info.members & GLYPH_INFO_VVECTOR1) == 0 && wmode == 1)
2907
0
                pwidths->ignore_wmode = true;
2908
1.82M
        }
2909
582k
        else
2910
582k
            info.v.x = info.v.y = 0;
2911
2.40M
        code = store_glyph_width(&pwidths->real_width, wmode, &scale_o, &info);
2912
2.40M
        if (code < 0)
2913
0
            return code;
2914
2.40M
        rcode |= code;
2915
2.40M
        gs_distance_transform(info.v.x, info.v.y, &scale_o, &pwidths->real_width.v);
2916
2.40M
    }
2917
2.43M
    return rcode;
2918
2.43M
}
2919
2920
static int
2921
pdf_choose_output_char_code(gx_device_pdf *pdev, pdf_text_enum_t *penum, gs_char *pch)
2922
155k
{
2923
155k
    gs_char ch;
2924
155k
    gs_font *font = penum->current_font;
2925
2926
155k
    if (penum->text.operation & TEXT_FROM_SINGLE_GLYPH) {
2927
0
        byte buf[1];
2928
0
        int char_code_length;
2929
0
        gs_glyph glyph = penum->text.data.d_glyph;
2930
0
        int code = pdf_encode_glyph((gs_font_base *)font, glyph,
2931
0
                    buf, sizeof(buf), &char_code_length);
2932
2933
0
        if (code < 0) {
2934
            /* Must not happen, becuse pdf_encode_glyph was passed in process_plain_text.*/
2935
0
            ch = GS_NO_CHAR;
2936
0
        } else if (char_code_length != 1) {
2937
            /* Must not happen with type 3 fonts.*/
2938
0
            ch = GS_NO_CHAR;
2939
0
        } else
2940
0
            ch = buf[0];
2941
155k
    } else if (penum->orig_font->FontType == ft_composite) {
2942
0
        gs_font_type0 *font0 = (gs_font_type0 *)penum->orig_font;
2943
0
        gs_glyph glyph = penum->returned.current_glyph;
2944
2945
0
        if (font0->data.FMapType == fmap_CMap) {
2946
0
            pdf_font_resource_t *pdfont;
2947
0
            int code = pdf_attached_font_resource(pdev, font, &pdfont, NULL, NULL, NULL, NULL);
2948
2949
0
            if (code < 0)
2950
0
                return code;
2951
0
            ch = pdf_find_glyph(pdfont, glyph);
2952
0
        } else
2953
0
            ch = penum->returned.current_char;
2954
155k
    } else {
2955
155k
        ch = penum->returned.current_char;
2956
        /* Keep for records : glyph = font->procs.encode_char(font, ch, GLYPH_SPACE_NAME); */
2957
        /*
2958
         * If glyph == GS_NO_GLYPH, we should replace it with
2959
         * a notdef glyph, but we don't know how to do with Type 3 fonts.
2960
         */
2961
155k
    }
2962
155k
    *pch = ch;
2963
155k
    return 0;
2964
155k
}
2965
2966
static int
2967
pdf_choose_output_glyph_name(gx_device_pdf *pdev, pdf_text_enum_t *penum, gs_const_string *gnstr, gs_glyph glyph, bool *cleanup)
2968
129k
{
2969
129k
    if (penum->orig_font->FontType == ft_composite || penum->orig_font->procs.glyph_name(penum->orig_font, glyph, gnstr) < 0
2970
129k
        || (penum->orig_font->FontType > 42 && gnstr->size == 7 && strcmp((const char *)gnstr->data, ".notdef")== 0)) {
2971
        /* If we're capturing a PCL bitmap, and the glyph comes back with a name of '/.notdef' then
2972
         * generate a name instead. There's nothing wrong technically with using /.notdef, but Acrobat does
2973
         * 'special stuff' with that name, and messes up the display. See bug #699102.
2974
         */
2975
        /* couldn't find a glyph name, so make one up! This can happen if we are handling PCL and the glyph
2976
         * (character code) is less than 29, the PCL glyph names start with /.notdef at 29. We also need to
2977
         * do this for composite fonts.
2978
         */
2979
0
        char buf[6];
2980
0
        byte *p;
2981
2982
0
        gnstr->size = 5;
2983
0
        p = (byte *)gs_alloc_string(pdev->pdf_memory, gnstr->size, "pdf_text_set_cache");
2984
0
        if (p == NULL)
2985
0
            return_error(gs_error_VMerror);
2986
0
        gs_snprintf(buf, sizeof(buf), "g%04x", (unsigned int)(glyph & 0xFFFF));
2987
0
        memcpy(p, buf, 5);
2988
0
        gnstr->data = p;
2989
0
        *cleanup = true;
2990
0
    }
2991
129k
    return 0;
2992
129k
}
2993
2994
/* ---------------- Main entry ---------------- */
2995
2996
/*
2997
 * Fall back to the default text processing code when needed.
2998
 */
2999
int
3000
pdf_default_text_begin(gs_text_enum_t *pte, const gs_text_params_t *text,
3001
                       gs_text_enum_t **ppte)
3002
889k
{
3003
889k
    gs_text_params_t text1 = *text;
3004
3005
889k
    if(pte->current_font->FontType == 3 && (text1.operation & TEXT_DO_NONE)) {
3006
        /* We need a real drawing to accumulate charproc. */
3007
33.7k
        text1.operation &= ~TEXT_DO_NONE;
3008
33.7k
        text1.operation |= TEXT_DO_DRAW;
3009
33.7k
    }
3010
889k
    return gx_default_text_begin(pte->dev, pte->pgs, &text1, pte->current_font,
3011
889k
                                 pte->pcpath, ppte);
3012
889k
}
3013
3014
static int install_PS_charproc_accumulator(gx_device_pdf *pdev, gs_text_enum_t *pte,
3015
                             gs_text_enum_t *pte_default, pdf_text_enum_t *const penum)
3016
103k
{
3017
103k
    int code;
3018
103k
    const gx_device_color * pdcolor = gs_currentdevicecolor_inline(pte->pgs);
3019
3020
103k
    penum->returned.current_char = pte_default->returned.current_char;
3021
103k
    penum->returned.current_glyph = pte_default->returned.current_glyph;
3022
103k
    pdev->charproc_ctm = penum->pgs->ctm;
3023
103k
    if ((penum->current_font->FontType == ft_user_defined || penum->current_font->FontType == ft_PDF_user_defined)&&
3024
103k
        penum->outer_CID == GS_NO_GLYPH &&
3025
103k
            !(penum->pte_default->text.operation & TEXT_DO_CHARWIDTH)) {
3026
        /* The condition above must be consistent with one in pdf_text_set_cache,
3027
           which decides to apply pdf_set_charproc_attrs. */
3028
103k
        gs_matrix m;
3029
103k
        pdf_font_resource_t *pdfont;
3030
3031
103k
        code = pdf_start_charproc_accum(pdev);
3032
103k
        if (code < 0)
3033
0
            return code;
3034
3035
        /* We need to give FreeType some room for accuracy when
3036
         * retrieving the outline. We will use a scale factor of 100
3037
         * (see below). Because this appears to the regular path
3038
         * handling code as if it were at the page level, the co-ords
3039
         * would be clipped frequently, so we temporarily hack the
3040
         * width and height of the device here to ensure it doesn't.
3041
         * Note that this requires some careful manipulation of the
3042
         * CTM in various places, which want numbers in the font
3043
         * co-ordinate space, not the scaled user space.
3044
         */
3045
103k
        pdev->width *= 100;
3046
103k
        pdev->height *= 100;
3047
3048
103k
        pdf_viewer_state_from_gs_gstate(pdev, pte->pgs, pdcolor);
3049
        /* Set line params to unallowed values so that
3050
           they'll synchronize with writing them out on the first use.
3051
           Doing so because PDF viewer inherits them from the
3052
           contents stream when executing the charproc,
3053
           but at this moment we don't know in what contexts
3054
           it will be used. */
3055
103k
        pdev->state.line_params.half_width = -1;
3056
103k
        pdev->state.line_params.start_cap = gs_cap_unknown;
3057
103k
        pdev->state.line_params.end_cap = gs_cap_unknown;
3058
103k
        pdev->state.line_params.dash_cap = gs_cap_unknown;
3059
103k
        pdev->state.line_params.join = gs_join_unknown;
3060
103k
        pdev->state.line_params.miter_limit = -1;
3061
103k
        pdev->state.line_params.dash.pattern_size = -1;
3062
        /* Must set an identity CTM for the charproc accumulation.
3063
           The function show_proceed (called from gs_text_process above)
3064
           executed gsave, so we are safe to change CTM now.
3065
           Note that BuildChar may change CTM before calling setcachedevice. */
3066
103k
        gs_make_identity(&m);
3067
        /* See comment above, we actually want to use a scale factor
3068
         * of 100 in order to give FreeType some room in the fixed
3069
         * precision calculations when retrieing the outline. So in
3070
         * fact we don't use the identity CTM, but a 100x100 matrix
3071
         * Originally tried 1000, but that was too likely to cause
3072
         * clipping or arithmetic overflow.
3073
         */
3074
103k
        gs_matrix_scale(&m, 100, 100, &m);
3075
103k
        gs_matrix_fixed_from_matrix(&penum->pgs->ctm, &m);
3076
3077
        /* Choose a character code to use with the charproc. */
3078
103k
        code = pdf_choose_output_char_code(pdev, penum, &penum->output_char_code);
3079
103k
        if (code < 0)
3080
0
            return code;
3081
103k
        code = pdf_attached_font_resource(pdev, penum->current_font, &pdfont, NULL, NULL, NULL, NULL);
3082
103k
        if (code < 0)
3083
0
            return code;
3084
103k
        pdev->font3 = (pdf_resource_t *)pdfont;
3085
103k
        pdev->substream_Resources = pdfont->u.simple.s.type3.Resources;
3086
103k
        penum->charproc_accum = true;
3087
103k
        pdev->accumulating_charproc = true;
3088
103k
        pdev->charproc_BBox.p.x = 10000;
3089
103k
        pdev->charproc_BBox.p.y = 10000;
3090
103k
        pdev->charproc_BBox.q.x = 0;
3091
103k
        pdev->charproc_BBox.q.y = 0;
3092
103k
        return TEXT_PROCESS_RENDER;
3093
103k
    }
3094
0
    return 0;
3095
103k
}
3096
3097
static int install_charproc_accumulator(gx_device_pdf *pdev, gs_text_enum_t *pte,
3098
                             gs_text_enum_t *pte_default, pdf_text_enum_t *const penum)
3099
26.4k
{
3100
26.4k
    int code;
3101
26.4k
    const gx_device_color * pdcolor = gs_currentdevicecolor_inline(pte->pgs);
3102
3103
26.4k
    pdev->charproc_ctm = penum->pgs->ctm;
3104
26.4k
    if ((penum->current_font->FontType == ft_user_defined ||
3105
26.4k
        penum->current_font->FontType == ft_PDF_user_defined ||
3106
26.4k
        penum->current_font->FontType == ft_PCL_user_defined ||
3107
26.4k
        penum->current_font->FontType == ft_MicroType ||
3108
26.4k
        penum->current_font->FontType == ft_GL2_stick_user_defined ||
3109
26.4k
        penum->current_font->FontType == ft_GL2_531) &&
3110
26.4k
            penum->outer_CID == GS_NO_GLYPH &&
3111
26.4k
            !(penum->pte_default->text.operation & TEXT_DO_CHARWIDTH)) {
3112
        /* The condition above must be consistent with one in pdf_text_set_cache,
3113
           which decides to apply pdf_set_charproc_attrs. */
3114
26.4k
        gs_matrix m;
3115
26.4k
        pdf_font_resource_t *pdfont;
3116
3117
26.4k
        pdev->PS_accumulator = false;
3118
26.4k
        code = pdf_start_charproc_accum(pdev);
3119
26.4k
        if (code < 0)
3120
0
            return code;
3121
3122
26.4k
        pdf_viewer_state_from_gs_gstate(pdev, pte->pgs, pdcolor);
3123
        /* Set line params to unallowed values so that
3124
           they'll synchronize with writing them out on the first use.
3125
           Doing so because PDF viewer inherits them from the
3126
           contents stream when executing the charproc,
3127
           but at this moment we don't know in what contexts
3128
           it will be used. */
3129
26.4k
        pdev->state.line_params.half_width = -1;
3130
26.4k
        pdev->state.line_params.start_cap = gs_cap_unknown;
3131
26.4k
        pdev->state.line_params.end_cap = gs_cap_unknown;
3132
26.4k
        pdev->state.line_params.dash_cap = gs_cap_unknown;
3133
26.4k
        pdev->state.line_params.join = gs_join_unknown;
3134
26.4k
        pdev->state.line_params.miter_limit = -1;
3135
26.4k
        pdev->state.line_params.dash.pattern_size = -1;
3136
        /* Must set an identity CTM for the charproc accumulation.
3137
           The function show_proceed (called from gs_text_process above)
3138
           executed gsave, so we are safe to change CTM now.
3139
           Note that BuildChar may change CTM before calling setcachedevice. */
3140
26.4k
        gs_make_identity(&m);
3141
26.4k
        if (penum->current_font->FontType == ft_PDF_user_defined) {
3142
26.4k
            if (!pdev->Scaled_accumulator) {
3143
25.8k
                if (pdev->width > max_int / 100 || pdev->height > max_int / 100)
3144
0
                    return_error(gs_error_rangecheck);
3145
25.8k
                pdev->width *= 100;
3146
25.8k
                pdev->height *= 100;
3147
25.8k
                gs_matrix_scale(&m, 100, 100, &m);
3148
25.8k
                gs_matrix_fixed_from_matrix(&penum->pgs->ctm, &m);
3149
25.8k
            }
3150
26.4k
            pdev->Scaled_accumulator++;
3151
26.4k
        } else
3152
0
            gs_matrix_fixed_from_matrix(&penum->pgs->ctm, &m);
3153
3154
        /* Choose a character code to use with the charproc. */
3155
26.4k
        code = pdf_choose_output_char_code(pdev, penum, &penum->output_char_code);
3156
26.4k
        if (code < 0) {
3157
0
            (void)pdf_exit_substream(pdev);
3158
0
            return code;
3159
0
        }
3160
26.4k
        code = pdf_attached_font_resource(pdev, penum->current_font, &pdfont, NULL, NULL, NULL, NULL);
3161
26.4k
        if (code < 0) {
3162
0
            (void)pdf_exit_substream(pdev);
3163
0
            return code;
3164
0
        }
3165
26.4k
        pdev->font3 = (pdf_resource_t *)pdfont;
3166
26.4k
        if (pdfont == 0L) {
3167
0
            (void)pdf_exit_substream(pdev);
3168
0
            return gs_note_error(gs_error_invalidfont);
3169
0
        }
3170
3171
26.4k
        pdev->substream_Resources = pdfont->u.simple.s.type3.Resources;
3172
26.4k
        penum->charproc_accum = true;
3173
26.4k
        pdev->accumulating_charproc = true;
3174
26.4k
        return TEXT_PROCESS_RENDER;
3175
26.4k
    }
3176
0
    return 0;
3177
26.4k
}
3178
3179
static int complete_charproc(gx_device_pdf *pdev, gs_text_enum_t *pte,
3180
                             gs_text_enum_t *pte_default, pdf_text_enum_t *const penum,
3181
                             bool was_PS_type3)
3182
129k
{
3183
129k
    gs_const_string gnstr;
3184
129k
    int code;
3185
129k
    bool cleanup = false;
3186
3187
129k
    if (pte_default->returned.current_glyph == GS_NO_GLYPH)
3188
0
      return_error(gs_error_undefined);
3189
129k
    code = pdf_choose_output_glyph_name(pdev, penum, &gnstr, pte_default->returned.current_glyph, &cleanup);
3190
129k
    if (code < 0) {
3191
0
        return code;
3192
0
    }
3193
3194
129k
    if ((penum->current_font->FontType == ft_user_defined ||
3195
129k
       penum->current_font->FontType == ft_PDF_user_defined ||
3196
129k
       penum->current_font->FontType == ft_PCL_user_defined ||
3197
129k
       penum->current_font->FontType == ft_MicroType ||
3198
129k
       penum->current_font->FontType == ft_GL2_stick_user_defined ||
3199
129k
       penum->current_font->FontType == ft_GL2_531) &&
3200
129k
       stell(pdev->strm) == 0)
3201
15.9k
    {
3202
15.9k
        char glyph[256], FontName[gs_font_name_max + 1], KeyName[256];
3203
15.9k
        int len;
3204
3205
15.9k
        len = min(gs_font_name_max, gnstr.size);
3206
15.9k
        memcpy(glyph, gnstr.data, len);
3207
15.9k
        glyph[len] = 0x00;
3208
15.9k
        len = min(gs_font_name_max, penum->current_font->font_name.size);
3209
15.9k
        memcpy(FontName, penum->current_font->font_name.chars, len);
3210
15.9k
        FontName[len] = 0x00;
3211
15.9k
        len = min(gs_font_name_max, penum->current_font->key_name.size);
3212
15.9k
        memcpy(KeyName, penum->current_font->key_name.chars, len);
3213
15.9k
        KeyName[len] = 0x00;
3214
3215
15.9k
        emprintf4(pdev->memory,
3216
15.9k
            "ERROR: Page %d used undefined glyph '%s' from type 3 font '%s', key '%s'\n",
3217
15.9k
            pdev->next_page, glyph, FontName, KeyName);
3218
15.9k
            stream_puts(pdev->strm, "0 0 0 0 0 0 d1\n");
3219
15.9k
    }
3220
3221
129k
    if (was_PS_type3 || pdev->Scaled_accumulator) {
3222
129k
        if (pdev->Scaled_accumulator)
3223
26.4k
            pdev->Scaled_accumulator--;
3224
129k
        if (was_PS_type3 || pdev->Scaled_accumulator == 0) {
3225
            /* See below, we scaled the device height and width to prevent
3226
             * clipping of the CharProc operations, now we need to undo that.
3227
             */
3228
128k
            pdev->width /= 100;
3229
128k
            pdev->height /= 100;
3230
128k
        }
3231
129k
    }
3232
129k
    code = pdf_end_charproc_accum(pdev, penum->current_font, penum->cgp,
3233
129k
                pte_default->returned.current_glyph, penum->output_char_code, &gnstr);
3234
129k
    if (code < 0)
3235
16
        goto exit;
3236
129k
    pdev->accumulating_charproc = false;
3237
129k
    penum->charproc_accum = false;
3238
129k
    code = gx_default_text_restore_state(pte_default);
3239
129k
    if (code < 0)
3240
0
        goto exit;
3241
129k
    gs_text_release(NULL, pte_default, "pdf_text_process");
3242
129k
    penum->pte_default = 0;
3243
3244
129k
exit:
3245
129k
    if (cleanup)
3246
0
        gs_free_string(pdev->pdf_memory, (byte *)gnstr.data, gnstr.size, "pdf_text_set_cache free working name");
3247
129k
    return code;
3248
129k
}
3249
3250
/* Nasty hackery. The PCL 'stick font' is drawn by constructing a path, and then stroking it.
3251
 * The stroke width is calculated under the influence of the device default matrix, which
3252
 * ensures it is the same, no matter what size the font is drawn at. Of course we are
3253
 * capturing the glyph under an identity matrix, so using the device matrix blows it up.
3254
 * What we do is replace the get_initial_matrix method for the course of the charproc
3255
 * accumulation *only*, and return a more sensible 'device' matrix.
3256
 * We know that the Charproc CTM is a combination of the font point size, and the default device
3257
 * matrix, so we just take off the default matrix, and invert the font scaling.
3258
 * This will scale the linewidth nicely for us. It does mean that if we use the
3259
 * same font at a different size, then the width will not be 'correct', but I
3260
 * think this is good enough.
3261
 */
3262
static void pdf_type3_get_initial_matrix(gx_device * dev, gs_matrix * pmat)
3263
0
{
3264
0
    gx_device_pdf *pdev = (gx_device_pdf *)dev;
3265
3266
0
    pmat->xx = pdev->charproc_ctm.xx;
3267
0
    pmat->xy = pdev->charproc_ctm.xy;
3268
0
    pmat->yx = pdev->charproc_ctm.yx;
3269
0
    pmat->yy = pdev->charproc_ctm.yy;
3270
0
    pmat->tx = 0;
3271
0
    pmat->ty = 0;
3272
    /* FIXME: Handle error here? Or is the charproc_ctm guaranteed to be invertible? */
3273
0
    (void)gs_matrix_invert(pmat, pmat);
3274
0
    gs_matrix_scale(pmat, pdev->HWResolution[0] / 72.0, pdev->HWResolution[0] / 72.0, pmat);
3275
0
}
3276
3277
static bool pdf_query_purge_cached_char(const gs_memory_t *mem, cached_char *cc, void *data)
3278
0
{
3279
0
    cached_char *to_purge = (cached_char *)data;
3280
3281
0
    return cc->code == to_purge->code && cc_pair(cc) == cc_pair(to_purge) &&
3282
0
           cc->subpix_origin.x == to_purge->subpix_origin.x &&
3283
0
           cc->subpix_origin.y == to_purge->subpix_origin.y &&
3284
0
           cc->wmode == to_purge->wmode && cc_depth(cc) == cc_depth(to_purge);
3285
0
}
3286
3287
static int ProcessTextForOCR(gs_text_enum_t *pte)
3288
0
{
3289
0
    pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
3290
0
    gx_device_pdf *pdev = (gx_device_pdf *)penum->dev;
3291
0
    gs_text_enum_t *pte_default;
3292
0
    int code;
3293
3294
0
    if (pdev->OCRStage == OCR_UnInit) {
3295
0
        gs_gsave(pte->pgs);
3296
0
        pdev->OCRSaved = (gs_text_enum_t*)gs_alloc_bytes(pdev->memory,sizeof(gs_text_enum_t),"saved enumerator for OCR");
3297
0
        if(pdev->OCRSaved == NULL)
3298
0
            return_error(gs_error_VMerror);
3299
0
        *(pdev->OCRSaved) = *pte;
3300
0
        gs_text_enum_copy_dynamic(pdev->OCRSaved,pte,true);
3301
3302
0
        code = pdf_default_text_begin(pte, &pte->text, &pte_default);
3303
0
        if (code < 0)
3304
0
            return code;
3305
0
        penum->pte_default = pte_default;
3306
0
        gs_text_enum_copy_dynamic(pte_default, pte, false);
3307
0
        pdev->OCRStage = OCR_Rendering;
3308
0
    }
3309
3310
0
    if (pdev->OCRStage == OCR_Rendering) {
3311
0
        penum->pte_default->can_cache = 0;
3312
0
        pdev->OCR_enum = (gs_text_enum_t *)penum;
3313
0
        code = gs_text_process(penum->pte_default);
3314
0
        gs_text_enum_copy_dynamic(pte, penum->pte_default, true);
3315
0
        if (code == TEXT_PROCESS_RENDER) {
3316
0
            pdev->OCR_char_code = penum->pte_default->returned.current_char;
3317
0
            pdev->OCR_glyph = penum->pte_default->returned.current_glyph;
3318
0
            return code;
3319
0
        }
3320
0
        if (code != 0) {
3321
0
            gs_free_object(pdev->memory, pdev->OCRSaved,"saved enumerator for OCR");
3322
0
            pdev->OCRSaved = NULL;
3323
0
            gs_grestore(pte->pgs);
3324
0
            gs_text_release(pte->pgs, penum->pte_default, "pdf_text_process");
3325
0
            penum->pte_default = NULL;
3326
0
            return code;
3327
0
        }
3328
0
        pdev->OCR_enum = NULL;
3329
0
        gs_grestore(pte->pgs);
3330
0
        *pte = *(pdev->OCRSaved);
3331
0
        gs_text_enum_copy_dynamic(pte, pdev->OCRSaved, true);
3332
0
        gs_free_object(pdev->memory, pdev->OCRSaved,"saved enumerator for OCR");
3333
0
        pdev->OCRSaved = NULL;
3334
0
        gs_text_release(pte->pgs, penum->pte_default, "pdf_text_process");
3335
0
        penum->pte_default = NULL;
3336
0
        pdev->OCRStage = OCR_Rendered;
3337
0
    }
3338
0
    return 0;
3339
0
}
3340
3341
/*
3342
 * Continue processing text.  This is the 'process' procedure in the text
3343
 * enumerator.  Per the check in pdf_text_begin, we know the operation is
3344
 * not a charpath, but it could be anything else.
3345
 */
3346
int
3347
pdf_text_process(gs_text_enum_t *pte)
3348
6.51M
{
3349
6.51M
    pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
3350
6.51M
    uint operation = pte->text.operation;
3351
6.51M
    uint size = 0;
3352
6.51M
    gs_text_enum_t *pte_default;
3353
6.51M
    PROCESS_TEXT_PROC((*process));
3354
6.51M
    int code = 0, early_accumulator = 0;
3355
6.51M
    gx_device_pdf *pdev = (gx_device_pdf *)penum->dev;
3356
6.51M
    uint captured_pte_index = 0xFFFFFFFF;
3357
6.51M
#define BUF_SIZE 100                /* arbitrary > 0 */
3358
    /* Use a union to ensure alignment. */
3359
6.51M
    union bu_ {
3360
6.51M
        byte bytes[BUF_SIZE];
3361
6.51M
        gs_char chars[BUF_SIZE / sizeof(gs_char)];
3362
6.51M
        gs_glyph glyphs[BUF_SIZE / sizeof(gs_glyph)];
3363
6.51M
    } buf;
3364
3365
6.51M
     if (!penum->pte_default) {
3366
        /* Don't need to sync before exiting charproc. */
3367
6.40M
        code = pdf_prepare_text_drawing(pdev, pte);
3368
6.40M
        if (code == gs_error_rangecheck) {
3369
            /* Fallback to the default implermentation for handling
3370
               a transparency with CompatibilityLevel<=1.3 . */
3371
6.40k
            goto default_impl;
3372
6.40k
        }
3373
6.39M
        if (code < 0)
3374
0
            return code;
3375
6.39M
    }
3376
6.50M
    if (!penum->pte_default) {
3377
6.39M
        pdev->charproc_just_accumulated = false;
3378
6.39M
        if (penum->cdevproc_callout) {
3379
            /* Restore after TEXT_PROCESS_CDEVPROC in scan_cmap_text. */
3380
0
            penum->current_font = penum->orig_font;
3381
0
        }
3382
6.39M
    }
3383
6.50M
    if ((penum->current_font->FontType == ft_user_defined ||
3384
6.50M
        penum->current_font->FontType == ft_PCL_user_defined ||
3385
6.50M
        penum->current_font->FontType == ft_PDF_user_defined ||
3386
6.50M
        penum->current_font->FontType == ft_MicroType ||
3387
6.50M
        penum->current_font->FontType == ft_GL2_stick_user_defined ||
3388
6.50M
        penum->current_font->FontType == ft_GL2_531) &&
3389
6.50M
        (penum->text.operation & TEXT_DO_ANY_CHARPATH)
3390
6.50M
        && !pdev->type3charpath) {
3391
408
        pdev->type3charpath = true;
3392
408
        if (!penum->charproc_accum)
3393
408
            goto default_impl;
3394
408
    }
3395
3396
6.50M
    if (pdev->UseOCR != UseOCRNever) {
3397
0
        code = ProcessTextForOCR(pte);
3398
0
        if (code != 0)
3399
0
            return code;
3400
0
    }
3401
3402
6.50M
    operation = pte->text.operation;
3403
6.50M
    size = pte->text.size - pte->index;
3404
3405
6.50M
    code = -1;                /* to force default implementation */
3406
3407
    /*
3408
     * If we fell back to the default implementation, continue using it.
3409
     */
3410
7.52M
 top:
3411
7.52M
    pte_default = penum->pte_default;
3412
    /* pte_default is a signal that we can't handle the glyph in the current
3413
     * font 'natively'. This means either a type 3 font (for PostScript we
3414
     * need to return to the PS interpreter to run the BuildChar), or a font
3415
     * type we can't or don't support in PDF. In the latter case we have the
3416
     * BuildChar render the glyph and store it in the cache, spot the usage
3417
     * in the pdfwrite image routine and we store the resluting bitmap in a
3418
     * Type 3 font. NB if the glyph is uncached then it just ends up in the
3419
     * output as a bitmap image.
3420
     */
3421
7.52M
    if (pte_default) {
3422
1.00M
        gs_char *cdata;
3423
3424
1.00M
        cdata = (gs_char *)pte->text.data.chars;
3425
1.00M
        if (penum->charproc_accum) {
3426
            /* We've been accumulating a Type 3 CharProc, so now we need to
3427
             * finish off and store it in the font.
3428
             */
3429
103k
            code = complete_charproc(pdev, pte, pte_default, penum, true);
3430
103k
            if (code < 0)
3431
0
                return code;
3432
            /* Record the current index of the text, this is the index for which we captured
3433
             * this charproc. We use this below to determine whether an error return is an
3434
             * error after we've already captured a CharProc. If it is, then we must not
3435
             * attempt to capture it again, this wil lead to an infinite loop.
3436
             */
3437
103k
            captured_pte_index = pte->index;
3438
103k
            if (!pdev->type3charpath)
3439
103k
                goto top;
3440
0
            else
3441
0
                goto default_impl;
3442
103k
        }
3443
3444
899k
        if ((penum->current_font->FontType == ft_GL2_stick_user_defined ||
3445
899k
            penum->current_font->FontType == ft_GL2_531 || penum->current_font->FontType == ft_MicroType)
3446
899k
            && operation & TEXT_FROM_CHARS && !pdev->type3charpath) {
3447
            /* Check for anamorphic sacling, we msut not cache stick font
3448
             * in this case, as the stroke width will vary with the scaling.
3449
             */
3450
3451
            /* To test the scaling we'll map two unit vectors, calculate the
3452
             * length of the transformed vectors and see if they are the same
3453
             * x' = ax + cy + Tx
3454
             * y' = bx + dy + Ty
3455
             * We set Tx and Ty to zero (we only care about scale not translation)
3456
             * We then test 0,0->0,1 and 0,0 -> 1,0. Because one term is always 0
3457
             * this means we can simplify the math.
3458
             */
3459
            /* Vector 0,0 -> 0,1 x is always 0, Tx and Ty are 0 =>
3460
             * x' = cy = c
3461
             * y' = dy = d
3462
             */
3463
            /* Vector 0,0 -> 1,0 y is always 0, Tx and Ty are 0 =>
3464
             * x' = ax = a
3465
             * y' = bx = b
3466
             */
3467
            /* Length of hypotenuse squared = sum of squares We don't care
3468
             * about the length's actual magnitude, so we can compare the
3469
             * squared length
3470
             */
3471
0
            float x0y1, x1y0;
3472
3473
0
            x0y1 = (penum->pgs->ctm.yx * penum->pgs->ctm.yx) +
3474
0
                (penum->pgs->ctm.yy * penum->pgs->ctm.yy);
3475
0
            x1y0 = (penum->pgs->ctm.xx * penum->pgs->ctm.xx) +
3476
0
                (penum->pgs->ctm.xy * penum->pgs->ctm.xy);
3477
3478
0
            if (x0y1 == x1y0)
3479
0
                early_accumulator = 1;
3480
0
        }
3481
899k
        if (penum->current_font->FontType == ft_PDF_user_defined) {
3482
26.4k
            early_accumulator = 1;
3483
26.4k
        }
3484
899k
        if (early_accumulator) {
3485
26.4k
            if (pte->text.operation & TEXT_FROM_BYTES || pte->text.operation & TEXT_FROM_STRING || (pte->text.operation & TEXT_FROM_CHARS && cdata[pte->index] <= 255)) {
3486
                /* The enumerator is created in pdf_default_text_begin and *is*
3487
                 * a show enumerator, so this is safe. We need to update the
3488
                 * graphics state pointer below which is why we need this.
3489
                 */
3490
26.4k
                gs_show_enum psenum = *(gs_show_enum *)pte_default;
3491
26.4k
                gs_gstate *pgs = (gs_gstate *)penum->pgs;
3492
26.4k
                gs_text_enum_procs_t *special_procs;
3493
26.4k
                void (*save_proc)(gx_device *, gs_matrix *) = pdev->procs.get_initial_matrix;
3494
26.4k
                gs_matrix m, savem;
3495
3496
26.4k
                special_procs = (gs_text_enum_procs_t *)gs_alloc_bytes(pte_default->memory->non_gc_memory, sizeof(gs_text_enum_procs_t), "pdf_text_process");
3497
26.4k
                if (special_procs == NULL)
3498
0
                    return_error(gs_error_VMerror);
3499
3500
26.4k
                *special_procs = *pte_default->procs;
3501
26.4k
                special_procs->set_cache = pdf_text_set_cache;
3502
26.4k
                special_procs->release = pdf_show_text_release;
3503
26.4k
                pte_default->procs = special_procs;
3504
3505
26.4k
                {
3506
                    /* We should not come here if we already have a cached character (except for the special case
3507
                     * of redefined characters in PCL downloaded fonts). If we do, it means that we are doing
3508
                     * 'page bursting', ie each page is going to a separate file, and the cache has an entry
3509
                     * from previous pages, but the PDF font resource doesn't. So empty the cached character
3510
                     * from the cache, to ensure the character description gets executed.
3511
                     */
3512
26.4k
                    gs_font *rfont = (pte->fstack.depth < 0 ? pte->current_font : pte->fstack.items[0].font);
3513
26.4k
                    gs_font *pfont = (pte->fstack.depth < 0 ? pte->current_font :
3514
26.4k
                        pte->fstack.items[pte->fstack.depth].font);
3515
26.4k
                    int wmode = rfont->WMode;
3516
26.4k
                    gs_log2_scale_point log2_scale = {0,0};
3517
26.4k
                    gs_fixed_point subpix_origin = {0,0};
3518
26.4k
                    cached_fm_pair *pair;
3519
26.4k
                    cached_char *cc;
3520
26.4k
                    gs_glyph glyph;
3521
3522
26.4k
                    code = gx_lookup_fm_pair(pfont, &char_tm_only(pte->pgs), &log2_scale, false, &pair);
3523
26.4k
                    if (code < 0)
3524
0
                        return code;
3525
26.4k
                    glyph = (*pte_default->encode_char)(pfont, pte_default->text.data.chars[pte_default->index],
3526
26.4k
                                                      GLYPH_SPACE_NAME);
3527
26.4k
                    cc = gx_lookup_cached_char(pfont, pair, glyph, wmode, 1, &subpix_origin);
3528
26.4k
                    if (cc != 0) {
3529
0
                        gx_purge_selected_cached_chars(pfont->dir, pdf_query_purge_cached_char, (void *)cc);
3530
0
                    }
3531
26.4k
                }
3532
                /* charproc completion will restore a gstate */
3533
0
                gs_gsave(pgs);
3534
                /* Assigning the gs_gstate pointer to the graphics state pointer
3535
                 * makes sure that when we make the CTM into the identity it
3536
                 * affects both.
3537
                 */
3538
26.4k
                psenum.pgs = (gs_gstate *)psenum.pgs;
3539
                /* Save the current FontMatrix */
3540
26.4k
                savem = pgs->font->FontMatrix;
3541
                /* Make the FontMatrix the identity otherwise we will apply
3542
                 * it twice, once in the glyph description and again in the
3543
                 * text matrix.
3544
                 */
3545
26.4k
                gs_make_identity(&m);
3546
26.4k
                pgs->font->FontMatrix = m;
3547
3548
26.4k
                pgs->char_tm_valid = 0;
3549
26.4k
                pgs->char_tm.txy_fixed_valid = 0;
3550
26.4k
                pgs->current_point.x = pgs->current_point.y = 0;
3551
26.4k
                pgs->char_tm.txy_fixed_valid = 0;
3552
3553
26.4k
                if (pte->text.operation & TEXT_FROM_CHARS)
3554
26.4k
                    pte_default->returned.current_char = penum->returned.current_char =
3555
26.4k
                        pte->text.data.chars[pte->index];
3556
0
                else
3557
0
                    pte_default->returned.current_char = penum->returned.current_char =
3558
0
                        pte->text.data.bytes[pte->index];
3559
3560
26.4k
                code = install_charproc_accumulator(pdev, pte, pte_default, penum);
3561
26.4k
                if (code < 0) {
3562
0
                    gs_grestore(pgs);
3563
0
                    pgs->font->FontMatrix = savem;
3564
0
                    return code;
3565
0
                }
3566
3567
                /* We can't capture more than one glyph at a time, so amek this one
3568
                 * otherwise the gs_text_process would try to render all the glyphs
3569
                 * in the string.
3570
                 */
3571
26.4k
                if (pte->text.size != 1)
3572
23.8k
                    pte_default->text.size = pte->index + 1;
3573
                /* We need a special 'initial matrix' method for stick fonts,
3574
                 * See pdf_type3_get_initial_matrix above.
3575
                 */
3576
26.4k
                if (penum->current_font->FontType != ft_PDF_user_defined)
3577
0
                    pdev->procs.get_initial_matrix = pdf_type3_get_initial_matrix;
3578
3579
26.4k
                pdev->pte = (gs_text_enum_t *)penum; /* CAUTION: See comment in gdevpdfx.h . */
3580
                /* In case of error, text_process will restore back to the enumerator 'level'
3581
                 * we must make certain we do not restore back too far!
3582
                 */
3583
26.4k
                pte_default->level = penum->pgs->level;
3584
26.4k
                code = gs_text_process(pte_default);
3585
26.4k
                if (code < 0) {
3586
698
                    (void)complete_charproc(pdev, pte, pte_default, penum, false);
3587
698
                    return code;
3588
698
                }
3589
25.7k
                pdev->pte = NULL;         /* CAUTION: See comment in gdevpdfx.h . */
3590
25.7k
                pdev->charproc_just_accumulated = false;
3591
3592
                /* Restore the saved FontMatrix, so that it gets applied
3593
                 * as part of the text matrix
3594
                 */
3595
25.7k
                pgs->font->FontMatrix = savem;
3596
                /* Restore the default 'initial matrix' mathod. */
3597
25.7k
                pdev->procs.get_initial_matrix = save_proc;
3598
25.7k
                penum->returned.current_char = pte_default->returned.current_char;
3599
25.7k
                penum->returned.current_glyph = pte_default->returned.current_glyph;
3600
25.7k
                code = pdf_choose_output_char_code(pdev, penum, &penum->output_char_code);
3601
25.7k
                if (code < 0) {
3602
0
                    (void)complete_charproc(pdev, pte, pte_default, penum, false);
3603
0
                    gs_grestore(pgs);
3604
0
                    return code;
3605
0
                }
3606
3607
                /* complete_charproc will release the text enumerator 'pte_default', we assume
3608
                 * that we are holding the only reference to it. Note that complete_charproc
3609
                 * also sets penum->pte_default to 0, so that we don't re-entre this  section of
3610
                 * code but process the text normally, now that we have captured the glyph description.
3611
                 */
3612
25.7k
                code = complete_charproc(pdev, pte, pte_default, penum, false);
3613
25.7k
                if (penum->current_font->FontType == ft_PCL_user_defined)
3614
0
                {
3615
                    /* PCL bitmap fonts can have glyphs 'reused'. We can't handle
3616
                     * handle this in a single PDF font, so we need to know when
3617
                     * it has happened and fall back to the 'default' implementation.
3618
                     * In order to do this, we create a cached character representation
3619
                     * and add it to the cache. If the glyph is reused then the
3620
                     * act of redefining it will remove this entry from the cache.
3621
                     * We check if the glyph is in the cache in process_text_return_width
3622
                     * and come back into this code. In the test above for PCL bitmap
3623
                     * fonts we also check to see if the PDF font already has a
3624
                     *'used' entry. If it does we know that its a redefinition, and
3625
                     * we don't try to capture the glyph.
3626
                     */
3627
0
                    cached_char *cc;
3628
0
                    cached_fm_pair *pair;
3629
0
                    gs_log2_scale_point log2_scale = {0,0};
3630
0
                    gs_font *rfont = (pte->fstack.depth < 0 ? pte->current_font : pte->fstack.items[0].font);
3631
0
                    int wmode = rfont->WMode;
3632
0
                    pdf_font_resource_t *pdfont;
3633
3634
                    /* This code copied from show_cache_setup */
3635
0
                    gs_memory_t *mem = penum->memory;
3636
0
                    gx_device_memory *dev =
3637
0
                        gs_alloc_struct_immovable(mem, gx_device_memory, &st_device_memory,
3638
0
                        "show_cache_setup(dev_cache)");
3639
3640
0
                    if (dev == 0) {
3641
0
                        gs_free_object(mem, dev, "show_cache_setup(dev_cache)");
3642
0
                        return_error(gs_error_VMerror);
3643
0
                    }
3644
0
                    gs_make_mem_mono_device(dev, mem, penum->dev);
3645
0
                    dev->HWResolution[0] = pgs->device->HWResolution[0];
3646
0
                    dev->HWResolution[1] = pgs->device->HWResolution[1];
3647
                    /* Retain these devices. */
3648
0
                    gx_device_retain((gx_device *)dev, true);
3649
                    /* end of code copied from show_cache_setup */
3650
3651
                    /* This copied from set_cache */
3652
0
                    code = gx_alloc_char_bits(pte->current_font->dir, dev,
3653
0
                                0, 0, &log2_scale, 1, &cc);
3654
0
                    if (code < 0)
3655
0
                        return code;
3656
3657
                    /* The following code is copied from show_update, case sws_cache */
3658
0
                    code = gx_lookup_fm_pair(pte->current_font, &ctm_only(pte->pgs),
3659
0
                            &log2_scale, false, &pair);
3660
0
                    if (code < 0)
3661
0
                        return code;
3662
3663
                    /* These members of the cached character are required
3664
                     * in order to find the cache entry later
3665
                     */
3666
0
                    cc->code = penum->returned.current_char;
3667
0
                    cc->wmode = wmode;
3668
0
                    cc->subpix_origin.x = cc->subpix_origin.y = 0;
3669
0
                    log2_scale.x = log2_scale.y = 0;
3670
0
                    code = gx_add_cached_char(pte->current_font->dir, dev,
3671
0
                               cc, pair, &log2_scale);
3672
0
                    if (code < 0)
3673
0
                        return code;
3674
                    /* End of code from show_update */
3675
3676
                    /* Finally, dispose of the device, which we don't actually need */
3677
0
                    gx_device_retain((gx_device *)dev, false);
3678
3679
                    /* Its possible for a bitmapped PCL font to not execute setcachedevice (invalid empty glyph)
3680
                     * which means that the pdfwrite override doesn't see it, and we don't note that the
3681
                     * glyph is 'cached' in the PDF font resource, but the 'used' flag does get set.
3682
                     * This causes our detection some problems, so mark it 'cached' here to make sure.
3683
                     */
3684
0
                    code = pdf_attached_font_resource(pdev, (gs_font *)penum->current_font, &pdfont, NULL, NULL, NULL, NULL);
3685
0
                    if (code < 0)
3686
0
                        return code;
3687
3688
0
                    pdfont->u.simple.s.type3.cached[cdata[pte->index] >> 3] |= 0x80 >> (cdata[pte->index] & 7);
3689
0
                }
3690
25.7k
                size = pte->text.size - pte->index;
3691
25.7k
                if (code < 0)
3692
16
                    return code;
3693
25.7k
                if (!pdev->type3charpath)
3694
25.7k
                    goto top;
3695
0
                else
3696
0
                    goto default_impl;
3697
25.7k
            }
3698
26.4k
        }
3699
3700
        /* Now we run the default text procedure to see what happens.
3701
         * PCL bitmap fonts and the HP/GL-2 stick font are handled above.
3702
         * If its a PostScript type 3 font (and some other kinds)
3703
         * then it will return TEXT_PROCESS_RENDER and we need to exit
3704
         * to the interpreter to run the glyph description. Otherwise we just
3705
         * run the BuildChar, and later capture the cached bitmap for incorporation
3706
         * into a type 3 font. Note that sometimes we will handle PCL or HP/GL-2
3707
         * fonts by not capturing them above, and they will come through here.
3708
         * The stick font will not be captured as a font at all, just vectors
3709
         * stored into the content stream (for osme kinds of scale/shear),
3710
         * bitmap fonts will be captured in the pdfwrite image routine. These
3711
         * are added to a type 3 font in the same way as glyphs which need to
3712
         * be rendered.
3713
         */
3714
873k
        {
3715
873k
            gs_show_enum *penum = (gs_show_enum *)pte_default;
3716
873k
            int save_can_cache = penum->can_cache;
3717
873k
            const gs_color_space *pcs;
3718
873k
            const gs_client_color *pcc;
3719
873k
            gs_gstate * pgs = pte->pgs;
3720
3721
            /* If we are using a high level pattern, we must not use the cached character, as the
3722
             * cache hasn't seen the pattern. So set can_cache to < 0, which prevents us consulting
3723
             * the cache, or storing the result in it.
3724
             * We actually don't want to use the cache for any Type 3 font where we're trying to
3725
             * capture the charproc - if we want to have a chance of capturing the charproc, we
3726
             * need it to execute, and not use a cache entry.
3727
             */
3728
873k
            if (gx_hld_get_color_space_and_ccolor(pgs, (const gx_drawing_color *)pgs->color[0].dev_color, &pcs, &pcc) == pattern_color_space)
3729
121
                penum->can_cache = -1;
3730
873k
            pdev->pte = pte_default; /* CAUTION: See comment in gdevpdfx.h . */
3731
873k
            code = gs_text_process(pte_default);
3732
873k
            if (code < 0)
3733
1.23k
                return code;
3734
3735
872k
            penum->can_cache = save_can_cache;
3736
3737
872k
            pdev->pte = NULL;         /* CAUTION: See comment in gdevpdfx.h . */
3738
872k
            pdev->charproc_just_accumulated = false;
3739
872k
        }
3740
3741
        /* If the BuildChar returned TEXT_PROCESS_RENDER then we want to try and
3742
         * capture this as a type 3 font, so start the accumulator now. Note
3743
         * that the setcachedevice handling can still abort this later. In which
3744
         * case we get the rendered bitmap, just as for non type 3 fonts.
3745
         */
3746
872k
        if (code == TEXT_PROCESS_RENDER && !pdev->type3charpath) {
3747
103k
            int code1;
3748
3749
103k
            code1 = install_PS_charproc_accumulator(pdev, pte, pte_default, penum);
3750
103k
            if (code1 != 0)
3751
103k
                return code1;
3752
103k
        }
3753
3754
769k
        gs_text_enum_copy_dynamic(pte, pte_default, true);
3755
3756
769k
        if (code)
3757
10.5k
            return code;
3758
758k
        gs_text_release(NULL, pte_default, "pdf_text_process");
3759
758k
        penum->pte_default = 0;
3760
758k
        if (pdev->type3charpath)
3761
408
            pdev->type3charpath = false;
3762
758k
        return 0;
3763
769k
    }
3764
6.52M
    {
3765
6.52M
        gs_font *font = pte->orig_font; /* Not sure. Changed for CDevProc callout. Was pte->current_font */
3766
3767
6.52M
        switch (font->FontType) {
3768
0
        case ft_CID_encrypted:
3769
0
        case ft_CID_TrueType:
3770
0
            process = process_cid_text;
3771
0
            break;
3772
4.45M
        case ft_encrypted:
3773
5.26M
        case ft_encrypted2:
3774
5.75M
        case ft_TrueType:
3775
6.10M
        case ft_user_defined:
3776
6.16M
        case ft_PDF_user_defined:
3777
6.16M
        case ft_PCL_user_defined:
3778
6.16M
        case ft_MicroType:
3779
6.16M
        case ft_GL2_stick_user_defined:
3780
6.16M
        case ft_GL2_531:
3781
            /* The data may be either glyphs or characters. */
3782
6.16M
            process = process_plain_text;
3783
6.16M
            break;
3784
364k
        case ft_composite:
3785
364k
            process =
3786
364k
                (((gs_font_type0 *)font)->data.FMapType == fmap_CMap ?
3787
364k
                 process_cmap_text :
3788
364k
                 process_composite_text);
3789
364k
            break;
3790
0
        default:
3791
0
            goto skip;
3792
6.52M
        }
3793
6.52M
    }
3794
3795
    /*
3796
     * We want to process the entire string in a single call, but we may
3797
     * need to modify it.  Copy it to a buffer.  Note that it may consist
3798
     * of bytes, gs_chars, or gs_glyphs.
3799
     */
3800
3801
6.52M
    if (operation & (TEXT_FROM_STRING | TEXT_FROM_BYTES)) {
3802
6.47M
        if (size < sizeof(gs_glyph))
3803
5.72M
            size = sizeof(gs_glyph); /* for process_cid_text */
3804
6.47M
    } else if (operation & TEXT_FROM_CHARS)
3805
53.5k
        size *= sizeof(gs_char);
3806
0
    else if (operation & TEXT_FROM_SINGLE_CHAR)
3807
0
        size = sizeof(gs_char);
3808
0
    else if (operation & TEXT_FROM_GLYPHS)
3809
0
        size *= sizeof(gs_glyph);
3810
0
    else if (operation & TEXT_FROM_SINGLE_GLYPH)
3811
0
        size = sizeof(gs_glyph);
3812
0
    else
3813
0
        goto skip;
3814
3815
    /* Now that we are using the working buffer for text in composite fonts, we must make sure
3816
     * it is large enough. Each input character code may be promoted to a gs_glyph, in scan_cmap_text
3817
     * when processing a Type 0 font with a type 1 descendant font. This routine uses
3818
     * pdf_make_text_glyphs_table_unencoded (called from pdf_obtain_font_resource_unencoded) which
3819
     * also may require an identically sized buffer, so we need:
3820
     * num__input_characters * sizeof(gs_glyph) * 2.
3821
     */
3822
6.52M
    if (pte->orig_font->FontType == ft_composite) {
3823
364k
        if (size < (pte->text.size - pte->index) * sizeof(gs_glyph) * 2)
3824
364k
            size = (pte->text.size - pte->index) * sizeof(gs_glyph) * 2;
3825
364k
    }
3826
3827
6.52M
    if (size <= sizeof(buf)) {
3828
6.42M
        code = process(pte, buf.bytes, size);
3829
6.42M
    } else {
3830
100k
        byte *dbuf = gs_alloc_bytes(pte->memory, size, "pdf_text_process");
3831
3832
100k
        if (dbuf == 0)
3833
0
            return_error(gs_error_VMerror);
3834
100k
        code = process(pte, dbuf, size);
3835
100k
        gs_free_object(pte->memory, dbuf, "pdf_text_process");
3836
100k
    }
3837
6.52M
 skip:
3838
6.52M
    if (code < 0 ||
3839
6.52M
            ((pte->current_font->FontType == ft_user_defined ||
3840
5.73M
            pte->current_font->FontType == ft_PCL_user_defined ||
3841
5.73M
            pte->current_font->FontType == ft_PDF_user_defined ||
3842
5.73M
            pte->current_font->FontType == ft_MicroType ||
3843
5.73M
            pte->current_font->FontType == ft_GL2_stick_user_defined ||
3844
5.73M
            pte->current_font->FontType == ft_GL2_531 ||
3845
5.73M
            pte->current_font->FontType == ft_TrueType) &&
3846
5.73M
             code != TEXT_PROCESS_INTERVENE &&
3847
5.73M
            penum->index < penum->text.size)) {
3848
903k
        if (code == gs_error_unregistered) /* Debug purpose only. */
3849
12.6k
            return code;
3850
891k
        if (code == gs_error_VMerror)
3851
0
            return code;
3852
891k
        if (code == gs_error_invalidfont) /* Bug 688370. */
3853
8.63k
            return code;
3854
889k
 default_impl:
3855
        /* If we have captured a CharProc, and the current index into the
3856
         * text is unchanged, then the capturing a CharProc for this text still
3857
         * resulted in a text error. So don't try to fix it by capturing the
3858
         * CharProc again, this causes an endless loop.
3859
         */
3860
889k
        if (captured_pte_index == pte->index)
3861
1
            return code;
3862
        /* Fall back to the default implementation. */
3863
889k
        code = pdf_default_text_begin(pte, &pte->text, &pte_default);
3864
889k
        if (code < 0)
3865
46
            return code;
3866
889k
        penum->pte_default = pte_default;
3867
889k
        gs_text_enum_copy_dynamic(pte_default, pte, false);
3868
889k
    }
3869
    /* The 'process' procedure might also have set pte_default itself. */
3870
6.51M
    if (penum->pte_default && !code)
3871
889k
        goto top;
3872
5.62M
    return code;
3873
    /*
3874
     * This function uses an unobvious algorithm while handling type 3 fonts.
3875
     * It runs 'process' to copy text until a glyph, which was not copied to
3876
     * output font. Then it installs pte_default and falls back to default
3877
     * implementation with PS interpreter callout. The callout executes
3878
     * BuildChar/BuildGlyph with setcachedevice. The latter calls
3879
     * pdf_set_charproc_attrs, which sets up an accumulator
3880
     * of graphic objects to a pdf_begin_resource stream.
3881
     * When the callout completes, pdf_text_process calls pdf_end_charproc_accum
3882
     * and later resumes the normal (non-default) text enumeration, repeating the
3883
     * the "callouted" glyph AT SECOND TIME. We can't do without the second pass
3884
     * becauase in the first pass the glyph widths is unknown.
3885
     */
3886
     /*
3887
      * Another unobvious thing is a CDevProc callout.
3888
      * If 'process' returns with TEXT_PROCESS_CDEVPROC,
3889
      * an interpreter callout will happen, and the function will be called again
3890
      * with pte->cdevproc_result_valid = true. Then it restatrs with taking
3891
      * glyph metrics from pte->cdevproc_result instead obtaining them with
3892
      * font->procs.glyph_info .
3893
      */
3894
6.51M
}