Coverage Report

Created: 2026-07-24 07:44

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