Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_text.c
Line
Count
Source
1
/* Copyright (C) 2018-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
/* Text operations for the PDF interpreter */
17
18
#include "pdf_int.h"
19
#include "pdf_array.h"
20
#include "pdf_text.h"
21
#include "pdf_image.h"
22
#include "pdf_colour.h"
23
#include "pdf_stack.h"
24
#include "pdf_font.h"
25
#include "pdf_font_types.h"
26
#include "pdf_gstate.h"
27
#include "pdf_trans.h"
28
#include "pdf_optcontent.h"
29
30
#include "gsstate.h"
31
#include "gsmatrix.h"
32
#include "gdevbbox.h"
33
#include "gspaint.h"        /* For gs_fill() and friends */
34
#include "gscoord.h"        /* For gs_setmatrix() */
35
#include "gxdevsop.h"               /* For special ops */
36
37
static int pdfi_set_TL(pdf_context *ctx, double TL);
38
39
int pdfi_BT(pdf_context *ctx)
40
1.60M
{
41
1.60M
    int code;
42
1.60M
    gs_matrix m;
43
1.60M
    bool illegal_BT = false;
44
45
1.60M
    if (ctx->text.BlockDepth != 0) {
46
36.8k
        illegal_BT = true;
47
36.8k
        if (ctx->text.TextClip) {
48
96
            gx_device *dev = gs_currentdevice_inline(ctx->pgs);
49
50
96
            ctx->text.TextClip = false;
51
96
            (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1);
52
96
        }
53
36.8k
        code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
54
36.8k
        if (code < 0)
55
0
            return code;
56
36.8k
    }
57
58
1.60M
    gs_make_identity(&m);
59
1.60M
    code = gs_settextmatrix(ctx->pgs, &m);
60
1.60M
    if (code < 0)
61
0
        return code;
62
63
1.60M
    code = gs_settextlinematrix(ctx->pgs, &m);
64
1.60M
    if (code < 0)
65
0
        return code;
66
67
    /* We should not perform the clip (for text rendering modes involving a clip)
68
     * when preserving the text rendering mode.
69
     */
70
1.60M
    if (gs_currenttextrenderingmode(ctx->pgs) >= 4 && ctx->text.BlockDepth == 0 && !ctx->device_state.preserve_tr_mode) {
71
        /* Start a new path (so our clip doesn't include any
72
         * already extant path in the graphics state)
73
         */
74
138
        gs_newpath(ctx->pgs);
75
138
    }
76
77
1.60M
    ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
78
1.60M
    if (!ctx->pgs->current_point_valid)
79
1.27M
        code = gs_moveto(ctx->pgs, 0, 0);
80
81
1.60M
    ctx->text.BlockDepth++;
82
83
1.60M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
84
383k
        gs_begin_transparency_text_group(ctx->pgs);
85
86
1.60M
    return code;
87
1.60M
}
88
89
static int do_ET(pdf_context *ctx)
90
1.61M
{
91
1.61M
    int code = 0;
92
1.61M
    gx_clip_path *copy = NULL;
93
94
    /* If we have reached the end of a text block (or the outermost block
95
     * if we have illegally nested text blocks) and we are using a 'clip'
96
     * text rendering mode, then we need to apply the clip. We also need
97
     * to grestore back one level as we will have pushed a gsave either in
98
     * pdfi_BT or in pdfi_Tr. The extra gsave is so we can accumulate a
99
     * clipping path separately to any path already existing in the
100
     * graphics state.
101
     */
102
103
    /* See the note on text rendering modes with clip in pdfi_BT() above */
104
1.61M
    if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
105
901
        gs_point initial_point;
106
107
901
        if  (!ctx->device_state.preserve_tr_mode) {
108
857
            ctx->text.TextClip = false;
109
            /* Capture the current position */
110
857
            code = gs_currentpoint(ctx->pgs, &initial_point);
111
857
            if (code >= 0 || code == gs_error_nocurrentpoint) {
112
855
                gs_point adjust;
113
855
                bool nocurrentpoint = code >= 0 ? false : true;
114
115
855
                gs_currentfilladjust(ctx->pgs, &adjust);
116
855
                code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
117
855
                if (code < 0)
118
0
                    return code;
119
120
855
                code = gs_clip(ctx->pgs);
121
855
                if (code >= 0)
122
855
                    copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
123
124
855
                code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
125
855
                if (code < 0)
126
0
                    return code;
127
128
855
                if (copy != NULL)
129
855
                    (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
130
131
855
                if (nocurrentpoint == false)
132
855
                    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
133
855
            }
134
857
        }
135
901
    }
136
1.61M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
137
390k
        gs_end_transparency_text_group(ctx->pgs);
138
139
1.61M
    if (!ctx->text.initial_current_point_valid)
140
846k
        gs_newpath(ctx->pgs);
141
1.61M
    return code;
142
1.61M
}
143
144
int pdfi_ET(pdf_context *ctx)
145
1.57M
{
146
1.57M
    if (ctx->text.BlockDepth == 0) {
147
61.8k
        int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
148
61.8k
        return code;
149
61.8k
    }
150
151
1.51M
    ctx->text.BlockDepth--;
152
153
1.51M
    return do_ET(ctx);
154
1.57M
}
155
156
int pdfi_T_star(pdf_context *ctx)
157
145k
{
158
145k
    int code;
159
145k
    gs_matrix m, mat;
160
161
145k
    if (ctx->text.BlockDepth == 0) {
162
31.4k
        int code;
163
31.4k
        if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_TEXTOPNOBT, "pdfi_T_star", NULL)) < 0)
164
0
            return code;
165
31.4k
    }
166
167
145k
    gs_make_identity(&m);
168
145k
    m.ty += ctx->pgs->textleading;
169
170
145k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
171
145k
    if (code < 0)
172
0
        return code;
173
174
145k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
175
145k
    if (code < 0)
176
0
        return code;
177
178
145k
    code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
179
145k
    return code;
180
145k
}
181
182
static int pdfi_set_Tc(pdf_context *ctx, double Tc)
183
649k
{
184
649k
    return gs_settextspacing(ctx->pgs, Tc);
185
649k
}
186
187
int pdfi_Tc(pdf_context *ctx)
188
661k
{
189
661k
    int code;
190
661k
    double d;
191
192
661k
    code = pdfi_destack_real(ctx, &d);
193
661k
    if (code < 0)
194
12.4k
        return code;
195
196
649k
    return pdfi_set_Tc(ctx, d);
197
661k
}
198
199
int pdfi_Td(pdf_context *ctx)
200
1.15M
{
201
1.15M
    int code;
202
1.15M
    double Txy[2];
203
1.15M
    gs_matrix m, mat;
204
205
1.15M
    code = pdfi_destack_reals(ctx, Txy, 2);
206
1.15M
    if (code < 0)
207
19.7k
        return code;
208
209
1.13M
    gs_make_identity(&m);
210
211
1.13M
    m.tx = Txy[0];
212
1.13M
    m.ty = Txy[1];
213
214
1.13M
    if (ctx->text.BlockDepth == 0) {
215
12.5k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL);
216
217
12.5k
        gs_make_identity(&mat);
218
12.5k
        code = gs_settextmatrix(ctx->pgs, &mat);
219
12.5k
        if (code < 0)
220
0
            return code;
221
222
12.5k
        code = gs_settextlinematrix(ctx->pgs, &mat);
223
12.5k
        if (code < 0)
224
0
            return code;
225
12.5k
    }
226
227
1.13M
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
228
1.13M
    if (code < 0)
229
0
        return code;
230
231
1.13M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
232
1.13M
    if (code < 0)
233
0
        return code;
234
235
1.13M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
236
1.13M
}
237
238
int pdfi_TD(pdf_context *ctx)
239
362k
{
240
362k
    int code;
241
362k
    double Txy[2];
242
362k
    gs_matrix m, mat;
243
244
362k
    gs_make_identity(&m);
245
246
362k
    code = pdfi_destack_reals(ctx, Txy, 2);
247
362k
    if (code < 0)
248
2.60k
        return code;
249
250
360k
    m.tx = Txy[0];
251
360k
    m.ty = Txy[1];
252
253
360k
    if (ctx->text.BlockDepth == 0) {
254
1.32k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
255
256
1.32k
        gs_make_identity(&mat);
257
1.32k
        code = gs_settextmatrix(ctx->pgs, &mat);
258
1.32k
        if (code < 0)
259
0
            return code;
260
261
1.32k
        code = gs_settextlinematrix(ctx->pgs, &mat);
262
1.32k
        if (code < 0)
263
0
            return code;
264
1.32k
    }
265
266
360k
    code = pdfi_set_TL(ctx, m.ty * 1.0f);
267
360k
    if (code < 0)
268
0
        return code;
269
270
360k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
271
360k
    if (code < 0)
272
0
        return code;
273
274
360k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
275
360k
    if (code < 0)
276
0
        return code;
277
278
360k
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
279
360k
}
280
281
/* This routine sets up most of the text params structure. In particular it
282
 * creates and initialises the x and y widths arrays, and for type 3 fonts
283
 * it creates and populates the 'chars' member.
284
 * It also sets the delta_sace member and partially set sup the 'operation'
285
 * bitfield. It does not set any of the TEXT_DO_* fields because we intend
286
 * to use this routine to set up the 'common' parts of the structure and
287
 * then we will twiddle the 'TEXT_DO_*' fields as required for the type of
288
 * operation we are doing (fill, create path for stroke, create path for fill)
289
 */
290
static int pdfi_show_set_params(pdf_context *ctx, pdf_string *s, gs_text_params_t *text)
291
12.3M
{
292
12.3M
    pdf_font *current_font = NULL;
293
12.3M
    gs_matrix mat;
294
12.3M
    float *x_widths = NULL, *y_widths = NULL, width;
295
12.3M
    double Tw = 0, Tc = 0;
296
12.3M
    int i, code;
297
298
12.3M
    text->data.chars = NULL;
299
12.3M
    text->x_widths = NULL;
300
12.3M
    text->y_widths = NULL;
301
302
    /* NOTE: we don't scale the FontMatrix we leave it as the default
303
     * and do all our scaling with the textmatrix/ctm. This saves having
304
     * to create multiple instances of the same font, and simplifies
305
     * composite fonts significantly.
306
     */
307
12.3M
    current_font = pdfi_get_current_pdf_font(ctx);
308
309
12.3M
    if (current_font == NULL)
310
0
        return_error(gs_error_invalidfont);
311
312
    /* Division by PDFfontsize because these are in unscaled font units,
313
       and the font scale is now pickled into the text matrix, so we have to
314
       undo that.
315
     */
316
12.3M
    Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
317
318
12.3M
    if (current_font->pdfi_font_type == e_pdf_font_type1 ||
319
4.54M
        current_font->pdfi_font_type == e_pdf_font_cff ||
320
3.11M
        current_font->pdfi_font_type == e_pdf_font_type3 ||
321
3.06M
        current_font->pdfi_font_type == e_pdf_font_cff ||
322
3.06M
        current_font->pdfi_font_type == e_pdf_font_truetype ||
323
1.23M
        current_font->pdfi_font_type == e_pdf_font_microtype ||
324
1.23M
        current_font->pdfi_font_type == e_pdf_font_type0)
325
12.3M
    {
326
        /* For Type 0 fonts, we apply the DW/W/DW2/W2 values when we retrieve the metrics for
327
           setcachedevice - see pdfi_fapi_set_cache()
328
         */
329
12.3M
        if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
330
4.76M
            text->operation = TEXT_RETURN_WIDTH;
331
4.76M
            if (Tc != 0) {
332
961k
                text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
333
961k
                if (current_font->pfont && current_font->pfont->WMode == 0) {
334
961k
                    text->delta_all.x = Tc;
335
961k
                    text->delta_all.y = 0;
336
961k
                } else {
337
1
                    text->delta_all.y = Tc;
338
1
                    text->delta_all.x = 0;
339
1
                }
340
961k
            }
341
7.57M
        } else {
342
7.57M
            gs_point pt;
343
344
7.57M
            x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
345
7.57M
            y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
346
7.57M
            if (x_widths == NULL || y_widths == NULL) {
347
0
                code = gs_note_error(gs_error_VMerror);
348
0
                goto text_params_error;
349
0
            }
350
351
7.57M
            memset(x_widths, 0x00, s->length * sizeof(float));
352
7.57M
            memset(y_widths, 0x00, s->length * sizeof(float));
353
354
            /* To calculate the Width (which is defined in unscaled text units) as a value in user
355
             * space we need to transform it by the FontMatrix
356
             */
357
7.57M
            mat = current_font->pfont->FontMatrix;
358
359
32.1M
            for (i = 0;i < s->length; i++) {
360
                /* Get the width (in unscaled text units) */
361
24.5M
                if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
362
86.1k
                    width = current_font->MissingWidth;
363
24.4M
                else
364
24.4M
                    width = current_font->Widths[s->data[i] - current_font->FirstChar];
365
                /* And convert the width into an appropriate value for the current environment */
366
24.5M
                gs_distance_transform(width, 0, &mat, &pt);
367
24.5M
                x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
368
                /* Add any Tc value */
369
24.5M
                x_widths[i] += Tc;
370
24.5M
            }
371
7.57M
            text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
372
7.57M
            text->x_widths = x_widths;
373
7.57M
            text->y_widths = y_widths;
374
7.57M
            text->widths_size = s->length * 2;
375
7.57M
        }
376
377
12.3M
        Tw = gs_currentwordspacing(ctx->pgs);
378
12.3M
        if (Tw != 0) {
379
808k
            text->operation |= TEXT_ADD_TO_SPACE_WIDTH;
380
            /* Division by PDFfontsize because these are in unscaled font units,
381
               and the font scale is now pickled into the text matrix, so we have to
382
               undo that.
383
             */
384
808k
            if (current_font->pfont && current_font->pfont->WMode == 0) {
385
808k
                text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
386
808k
                text->delta_space.y = 0;
387
808k
            } else {
388
0
                text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
389
0
                text->delta_space.x = 0;
390
0
            }
391
808k
            text->space.s_char = 0x20;
392
808k
        }
393
394
12.3M
        if (current_font->pdfi_font_type == e_pdf_font_type3) {
395
43.7k
            text->operation |= TEXT_FROM_CHARS;
396
43.7k
            text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
397
43.7k
            if (!text->data.chars) {
398
0
                code = gs_note_error(gs_error_VMerror);
399
0
                goto text_params_error;
400
0
            }
401
402
215k
            for (i = 0; i < s->length; i++) {
403
171k
                ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
404
171k
            }
405
43.7k
        }
406
12.2M
        else {
407
12.2M
            text->operation |= TEXT_FROM_BYTES;
408
12.2M
            text->data.bytes = (const byte *)s->data;
409
12.2M
        }
410
12.3M
        text->size = s->length;
411
12.3M
    }
412
40
    else {
413
40
        code = gs_note_error(gs_error_invalidfont);
414
40
        goto text_params_error;
415
40
    }
416
417
12.3M
    return 0;
418
419
40
text_params_error:
420
40
    gs_free_object(ctx->memory, x_widths, "X widths array for text");
421
40
    text->x_widths = NULL;
422
40
    gs_free_object(ctx->memory, y_widths, "Y widths array for text");
423
40
    text->y_widths = NULL;
424
40
    gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars");
425
40
    text->data.chars = NULL;
426
40
    return code;
427
12.3M
}
428
429
/* These routines actually perform the fill/stroke/clip of the text.
430
 * We build up the compound cases by reusing the basic cases which
431
 * is why we reset the TEXT_DO_ fields after use.
432
 */
433
434
static int pdfi_show_simple(pdf_context *ctx, gs_text_params_t *text)
435
12.2M
{
436
12.2M
    int code = 0;
437
12.2M
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
438
439
12.2M
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
440
12.2M
    if (code >= 0) {
441
12.1M
        penum->single_byte_space = true;
442
12.1M
        saved_penum = ctx->text.current_enum;
443
12.1M
        ctx->text.current_enum = penum;
444
12.1M
        code = gs_text_process(penum);
445
12.1M
        gs_text_release(ctx->pgs, penum, "pdfi_Tj");
446
12.1M
        ctx->text.current_enum = saved_penum;
447
12.1M
    }
448
12.2M
    return code;
449
12.2M
}
450
451
/* Mode 0 - fill */
452
static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
453
9.70M
{
454
9.70M
    int code;
455
456
    /* just draw the text */
457
9.70M
    text->operation |= TEXT_DO_DRAW;
458
459
9.70M
    code = pdfi_show_simple(ctx, text);
460
461
9.70M
    text->operation &= ~TEXT_DO_DRAW;
462
9.70M
    return code;
463
9.70M
}
464
465
/* Mode 1 - stroke */
466
static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text)
467
1.20k
{
468
1.20k
    int code;
469
1.20k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
470
1.20k
    gs_point end_point, initial_point;
471
1.20k
    gx_device *dev = ctx->pgs->device;
472
1.20k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
473
474
1.20k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
475
476
    /* Capture the current position */
477
1.20k
    code = gs_currentpoint(ctx->pgs, &initial_point);
478
1.20k
    if (code < 0)
479
0
        return code;
480
481
    /* We don't want to disturb the current path, so do a gsave now
482
     * We will grestore back to this point after we have stroked the
483
     * text, which will leave any current path unchanged.
484
     */
485
1.20k
    code = pdfi_gsave(ctx);
486
1.20k
    if (code < 0)
487
0
        goto Tr1_error;
488
489
    /* Start a new path (so our stroke doesn't include any
490
     * already extant path in the graphics state)
491
     */
492
1.20k
    code = gs_newpath(ctx->pgs);
493
1.20k
    if (code < 0)
494
0
        goto Tr1_error;
495
1.20k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
496
1.20k
    if (code < 0)
497
0
        goto Tr1_error;
498
499
    /* Don't draw the text, create a path suitable for stroking */
500
1.20k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
501
502
    /* Run the text methods to create the path */
503
1.20k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
504
1.20k
    if (code < 0)
505
15
        goto Tr1_error;
506
507
1.18k
    penum->single_byte_space = true;
508
1.18k
    saved_penum = ctx->text.current_enum;
509
1.18k
    ctx->text.current_enum = penum;
510
1.18k
    code = gs_text_process(penum);
511
1.18k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
512
1.18k
    ctx->text.current_enum = saved_penum;
513
1.18k
    if (code < 0)
514
0
        goto Tr1_error;
515
516
    /* After a stroke operation there is no current point and we need
517
     * it to be set as if we had drawn the text. So capture it now
518
     * and we will append a 'move' to the current point when we have
519
     * finished the stroke.
520
     */
521
1.18k
    code = gs_currentpoint(ctx->pgs, &end_point);
522
1.18k
    if (code < 0)
523
0
        goto Tr1_error;
524
525
1.18k
    if (talphabits != galphabits)
526
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
527
528
    /* Change to the current stroking colour */
529
1.18k
    gs_swapcolors_quick(ctx->pgs);
530
    /* Finally, stroke the actual path */
531
1.18k
    code = gs_stroke(ctx->pgs);
532
    /* Switch back to the non-stroke colour */
533
1.18k
    gs_swapcolors_quick(ctx->pgs);
534
535
1.18k
    if (talphabits != galphabits)
536
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
537
538
1.20k
Tr1_error:
539
    /* And grestore back to where we started */
540
1.20k
    (void)pdfi_grestore(ctx);
541
    /* If everything went well, then move the current point to the
542
     * position we captured at the end of the path creation */
543
1.20k
    if (code >= 0)
544
1.18k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
545
546
1.20k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
547
1.20k
    return code;
548
1.18k
}
549
550
/* Mode 2 - fill then stroke */
551
static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text)
552
7.68k
{
553
7.68k
    int code, restart = 0;
554
7.68k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
555
7.68k
    gs_point end_point, initial_point;
556
7.68k
    gx_device *dev = ctx->pgs->device;
557
7.68k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
558
559
7.68k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
560
561
    /* Capture the current position */
562
7.68k
    code = gs_currentpoint(ctx->pgs, &initial_point);
563
7.68k
    if (code < 0)
564
0
        return code;
565
566
    /* We don't want to disturb the current path, so do a gsave now
567
     * We will grestore back to this point after we have stroked the
568
     * text, which will leave any current path unchanged.
569
     */
570
7.68k
    code = pdfi_gsave(ctx);
571
7.68k
    if (code < 0)
572
0
        goto Tr1_error;
573
574
    /* Start a new path (so our stroke doesn't include any
575
     * already extant path in the graphics state)
576
     */
577
7.68k
    code = gs_newpath(ctx->pgs);
578
7.68k
    if (code < 0)
579
0
        goto Tr1_error;
580
7.68k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
581
7.68k
    if (code < 0)
582
0
        goto Tr1_error;
583
584
    /* Don't draw the text, create a path suitable for stroking */
585
7.68k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
586
587
    /* Run the text methods to create the path */
588
7.68k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
589
7.68k
    if (code < 0)
590
0
        goto Tr1_error;
591
592
7.68k
    penum->single_byte_space = true;
593
7.68k
    saved_penum = ctx->text.current_enum;
594
7.68k
    ctx->text.current_enum = penum;
595
7.68k
    code = gs_text_process(penum);
596
7.68k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
597
7.68k
    ctx->text.current_enum = saved_penum;
598
7.68k
    if (code < 0)
599
47
        goto Tr1_error;
600
601
    /* After a stroke operation there is no current point and we need
602
     * it to be set as if we had drawn the text. So capture it now
603
     * and we will append a 'move' to the current point when we have
604
     * finished the stroke.
605
     */
606
7.63k
    code = gs_currentpoint(ctx->pgs, &end_point);
607
7.63k
    if (code < 0)
608
0
        goto Tr1_error;
609
7.63k
    if (talphabits != galphabits)
610
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
611
7.63k
    code = gs_fillstroke(ctx->pgs, &restart);
612
7.63k
    if (talphabits != galphabits)
613
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
614
615
7.68k
Tr1_error:
616
    /* And grestore back to where we started */
617
7.68k
    (void)pdfi_grestore(ctx);
618
    /* If everything went well, then move the current point to the
619
     * position we captured at the end of the path creation */
620
7.68k
    if (code >= 0)
621
7.63k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
622
623
7.68k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
624
7.68k
    return code;
625
7.63k
}
626
627
static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
628
115k
{
629
115k
    int code;
630
115k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
631
632
    /* Don't draw the text */
633
115k
    text->operation |= TEXT_DO_NONE | TEXT_RETURN_WIDTH | TEXT_RENDER_MODE_3;
634
635
    /* Run the text methods to create the path */
636
115k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
637
115k
    if (code < 0)
638
0
        return code;
639
640
115k
    penum->single_byte_space = true;
641
115k
    saved_penum = ctx->text.current_enum;
642
115k
    ctx->text.current_enum = penum;
643
115k
    code = gs_text_process(penum);
644
115k
    if (code >= 0)
645
115k
        code = gs_rmoveto(ctx->pgs, penum->returned.total_width.x, penum->returned.total_width.y);
646
115k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
647
115k
    ctx->text.current_enum = saved_penum;
648
649
115k
    return code;
650
115k
}
651
652
/* Prototype the basic 'clip' function, as the following routines will all use it */
653
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text);
654
655
static int pdfi_show_Tr_4(pdf_context *ctx, gs_text_params_t *text)
656
2.36k
{
657
2.36k
    int code;
658
2.36k
    gs_point initial_point;
659
660
    /* Capture the current position */
661
2.36k
    code = gs_currentpoint(ctx->pgs, &initial_point);
662
2.36k
    if (code < 0)
663
0
        return code;
664
665
2.36k
    ctx->text.TextClip = true;
666
    /* First fill the text */
667
2.36k
    code = pdfi_show_Tr_0(ctx, text);
668
2.36k
    if (code < 0)
669
0
        return code;
670
671
    /* Return the current point to the initial point */
672
2.36k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
673
2.36k
    if (code >= 0)
674
        /* And add the text to the acumulated path for clipping */
675
2.36k
        code = pdfi_show_Tr_7(ctx, text);
676
677
2.36k
    return code;
678
2.36k
}
679
680
static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
681
301
{
682
301
    int code;
683
301
    gs_point initial_point;
684
685
    /* Capture the current position */
686
301
    code = gs_currentpoint(ctx->pgs, &initial_point);
687
301
    if (code < 0)
688
0
        return code;
689
690
301
    ctx->text.TextClip = true;
691
    /* First stroke the text */
692
301
    code = pdfi_show_Tr_1(ctx, text);
693
301
    if (code < 0)
694
0
        return code;
695
696
    /* Return the current point to the initial point */
697
301
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
698
301
    if (code >= 0)
699
        /* And add the text to the acumulated path for clipping */
700
301
        code = pdfi_show_Tr_7(ctx, text);
701
702
301
    return code;
703
301
}
704
705
static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
706
333
{
707
333
    int code;
708
333
    gs_point initial_point;
709
710
    /* Capture the current position */
711
333
    code = gs_currentpoint(ctx->pgs, &initial_point);
712
333
    if (code < 0)
713
0
        return code;
714
715
333
    ctx->text.TextClip = true;
716
    /* First fill and stroke the text */
717
333
    code = pdfi_show_Tr_2(ctx, text);
718
333
    if (code < 0)
719
0
        return code;
720
721
    /* Return the current point to the initial point */
722
333
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
723
333
    if (code >= 0)
724
        /* And add the text to the acumulated path for clipping */
725
333
        code = pdfi_show_Tr_7(ctx, text);
726
727
333
    return code;
728
333
}
729
730
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
731
3.12k
{
732
3.12k
    int code;
733
3.12k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
734
735
3.12k
    ctx->text.TextClip = true;
736
    /* Don't draw the text, create a path suitable for filling */
737
3.12k
    text->operation |= TEXT_DO_TRUE_CHARPATH;
738
739
    /* Run the text methods to create the path */
740
3.12k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
741
3.12k
    if (code < 0)
742
0
        goto Tr7_error;
743
744
3.12k
    penum->single_byte_space = true;
745
3.12k
    saved_penum = ctx->text.current_enum;
746
3.12k
    ctx->text.current_enum = penum;
747
3.12k
    code = gs_text_process(penum);
748
3.12k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
749
3.12k
    ctx->text.current_enum = saved_penum;
750
751
3.12k
Tr7_error:
752
3.12k
    text->operation &= ~TEXT_DO_TRUE_CHARPATH;
753
3.12k
    return code;
754
3.12k
}
755
756
static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
757
2.50M
{
758
2.50M
    int Trmode = 0, code;
759
2.50M
    pdf_font *current_font = NULL;
760
2.50M
    gs_point initial_point;
761
762
2.50M
    current_font = pdfi_get_current_pdf_font(ctx);
763
2.50M
    if (current_font == NULL)
764
0
        return_error(gs_error_invalidfont);
765
766
    /* Capture the current position, in case we need it for clipping */
767
2.50M
    code = gs_currentpoint(ctx->pgs, &initial_point);
768
769
2.50M
    Trmode = gs_currenttextrenderingmode(ctx->pgs);
770
2.50M
    if (Trmode == 3) {
771
4.21k
        if (current_font->pdfi_font_type == e_pdf_font_type3)
772
0
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
773
4.21k
        else
774
4.21k
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
775
4.21k
    }
776
2.50M
    else
777
2.50M
        text->operation |= TEXT_RETURN_WIDTH | TEXT_DO_DRAW;
778
779
    /* If we're preserving the text rendering mode, then we don't run a separate
780
     * stroke operation, we do effectively run a fill. The fill setup loads the
781
     * fill device colour whch, for patterns, creates the pattern tile.
782
     * But, because we never load the stroke colour into a device colour, the
783
     * pattern tile never gets loaded, so we get an error trying to draw the
784
     * text.
785
     * We could load the stroke colour in gs_text_begin, but that's potentially
786
     * wasteful given that most of the time we won't be using the stroke colour
787
     * os I've chosen to load the stroke device colour explicitly here.
788
     * But, obviously, only when preserving a stroking text rendering mode.
789
     */
790
2.50M
    if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) {
791
232
        gs_swapcolors_quick(ctx->pgs);
792
793
232
        code = gx_set_dev_color(ctx->pgs);
794
232
        if (code != 0)
795
0
            return code;
796
797
232
        code = gs_gstate_color_load(ctx->pgs);
798
232
        if (code < 0)
799
0
            return code;
800
801
232
        gs_swapcolors_quick(ctx->pgs);
802
232
    }
803
804
    /* If we've switched to aemitting text with a 'clip' rendering mode then we
805
     * tell pdfwrite that here, so that it can emit a 'q', which it can later
806
     * (see pdfi_op_Q) restore to with a Q. It's the only way to get the clipping
807
     * right.
808
     */
809
2.50M
    if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) {
810
15
        gx_device *dev = gs_currentdevice_inline(ctx->pgs);
811
812
15
        ctx->text.TextClip = true;
813
15
        code = dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1);
814
15
        if (code < 0 && code != gs_error_undefined)
815
0
            return code;
816
15
  }
817
818
2.50M
    code = pdfi_show_simple(ctx, text);
819
2.50M
    return code;
820
2.50M
}
821
822
static int pdfi_show(pdf_context *ctx, pdf_string *s)
823
12.3M
{
824
12.3M
    int code = 0, SavedTextDepth = 0;
825
12.3M
    int code1 = 0;
826
12.3M
    gs_text_params_t text;
827
12.3M
    pdf_font *current_font = NULL;
828
12.3M
    int Trmode = 0;
829
12.3M
    int initial_gsave_level = ctx->pgs->level;
830
12.3M
    pdfi_trans_state_t state;
831
12.3M
    int outside_text_block = 0;
832
833
12.3M
    if (ctx->text.BlockDepth == 0) {
834
108k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
835
108k
        outside_text_block = 1;
836
108k
    }
837
838
12.3M
    if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
839
12.3M
     || hypot(ctx->pgs->ctm.yy, ctx->pgs->ctm.yx) == 0.0) {
840
        /* This can mean a font scaled to 0, which appears to be valid, or a degenerate
841
         * ctm/text matrix, which isn't. A degenrate matrix will have triggered a warning
842
         * before, so we just skip the operation here.
843
         */
844
8.88k
         return 0;
845
8.88k
    }
846
847
12.3M
    current_font = pdfi_get_current_pdf_font(ctx);
848
12.3M
    if (current_font == NULL)
849
11
        return_error(gs_error_invalidfont);
850
851
12.3M
    code = pdfi_show_set_params(ctx, s, &text);
852
12.3M
    if (code < 0)
853
40
        goto show_error;
854
855
    /* <sigh> Ugliness......
856
     * It seems that transparency can require that we set a transparency Group
857
     * during the course of a text operation, and that Group can, of course,
858
     * execute operations which are barred during text operations (because
859
     * the Group doesn't affect the text as such). So we need to not throw
860
     * errors if that happens. Do that by just setting the BlockDepth to 0.
861
     */
862
12.3M
    SavedTextDepth = ctx->text.BlockDepth;
863
12.3M
    ctx->text.BlockDepth = 0;
864
12.3M
    code = pdfi_trans_setup_text(ctx, &state, true);
865
12.3M
    ctx->text.BlockDepth = SavedTextDepth;
866
867
12.3M
    if (code >= 0) {
868
12.3M
        if (ctx->device_state.preserve_tr_mode) {
869
2.50M
            code = pdfi_show_Tr_preserve(ctx, &text);
870
9.82M
        } else {
871
9.82M
            Trmode = gs_currenttextrenderingmode(ctx->pgs);
872
873
            /* The spec says that we ignore text rendering modes other than 0 for
874
             * type 3 fonts, but Acrobat also honours mode 3 (do nothing)
875
             */
876
9.82M
            if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
877
286
                Trmode = 0;
878
879
9.82M
            switch(Trmode) {
880
9.70M
            case 0:
881
9.70M
                code = pdfi_show_Tr_0(ctx, &text);
882
9.70M
                break;
883
902
            case 1:
884
902
                code = pdfi_show_Tr_1(ctx, &text);
885
902
                break;
886
7.35k
            case 2:
887
7.35k
                code = pdfi_show_Tr_2(ctx, &text);
888
7.35k
                break;
889
115k
            case 3:
890
115k
                code = pdfi_show_Tr_3(ctx, &text);
891
115k
                break;
892
2.36k
            case 4:
893
2.36k
                code = pdfi_show_Tr_4(ctx, &text);
894
2.36k
                break;
895
301
            case 5:
896
301
                code = pdfi_show_Tr_5(ctx, &text);
897
301
                break;
898
333
            case 6:
899
333
                code = pdfi_show_Tr_6(ctx, &text);
900
333
                break;
901
128
            case 7:
902
128
                code = pdfi_show_Tr_7(ctx, &text);
903
128
                break;
904
0
            default:
905
0
                break;
906
9.82M
            }
907
9.82M
        }
908
12.3M
        code1 = pdfi_trans_teardown_text(ctx, &state);
909
12.3M
        if (code == 0)
910
12.2M
            code = code1;
911
12.3M
    }
912
913
    /* We shouldn't need to do this, but..... It turns out that if we have text rendering mode 3 set
914
     * then gs_text_begin will execute a gsave, push the nulldevice and alter the saved gsave level.
915
     * If we then get an error while processing the text we don't gs_restore enough times which
916
     * leaves the nulldevice as the current device, and eats all the following content!
917
     * To avoid that, we save the gsave depth at the start of this routine, and grestore enough
918
     * times to get back to the same level. I think this is actually a bug in the graphics library
919
     * but it doesn't get exposed in normal usage so we'll just work around it here.
920
     */
921
12.3M
    while(ctx->pgs->level > initial_gsave_level)
922
375
        gs_grestore(ctx->pgs);
923
924
    /* If we were outside a text block when we started this function, then we effectively started
925
     * one by calling the show mechanism. Among other things, this can have pushed a transparency
926
     * group. We need to ensure that this is properly closed, so do the guts of the 'ET' operator
927
     * here (without adjusting the blockDepth, or giving more warnings). See Bug 707753. */
928
12.3M
    if (outside_text_block) {
929
103k
        code1 = do_ET(ctx);
930
103k
        if (code == 0)
931
97.8k
            code = code1;
932
103k
    }
933
934
12.3M
show_error:
935
12.3M
    if ((void *)text.data.chars != (void *)s->data)
936
43.8k
        gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
937
938
12.3M
    gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
939
12.3M
    gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
940
12.3M
    return code;
941
12.3M
}
942
943
/* NOTE: the bounding box this generates has llx and lly at 0,0,
944
   so is arguably not a "real" bounding box
945
 */
946
int pdfi_string_bbox(pdf_context *ctx, pdf_string *s, gs_rect *bboxout, gs_point *advance_width, bool for_stroke)
947
19.4k
{
948
19.4k
    int code = 0;
949
19.4k
    gx_device_bbox *bbdev;
950
19.4k
    pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
951
19.4k
    gs_matrix tmpmat, Trm, Trm_ctm;
952
19.4k
    gs_point cppt, startpt;
953
954
19.4k
    if (current_font == NULL)
955
0
        return_error(gs_error_invalidfont);
956
957
958
19.4k
    for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
959
960
19.4k
    if (ctx->devbbox == NULL) {
961
182
        bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
962
182
        if (bbdev == NULL)
963
0
           return_error(gs_error_VMerror);
964
182
        gx_device_bbox_init(bbdev, NULL, ctx->memory);
965
182
        ctx->devbbox = (gx_device *)bbdev;
966
182
        rc_increment(ctx->devbbox);
967
182
    }
968
19.3k
    else {
969
19.3k
        bbdev = (gx_device_bbox *)ctx->devbbox;
970
19.3k
    }
971
19.4k
    gx_device_retain((gx_device *)bbdev, true);
972
19.4k
    gx_device_bbox_set_white_opaque(bbdev, true);
973
974
19.4k
    code = pdfi_gsave(ctx);
975
19.4k
    if (code < 0) {
976
0
        gx_device_retain((gx_device *)bbdev, false);
977
0
        return code;
978
0
    }
979
    /* The bbox device needs a fairly high resolution to get accurate results */
980
19.4k
    gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
981
982
19.4k
    code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
983
19.4k
    if (code < 0)
984
0
        goto out;
985
986
19.4k
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
987
19.4k
    Trm.xy = 0;
988
19.4k
    Trm.yx = 0;
989
19.4k
    Trm.yy = ctx->pgs->PDFfontsize;
990
19.4k
    Trm.tx = 0;
991
19.4k
    Trm.ty = ctx->pgs->textrise;
992
993
19.4k
    memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
994
    /* We want to avoid any translations unrelated to the extents of the text */
995
19.4k
    tmpmat.tx = tmpmat.ty = 0;
996
19.4k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm);
997
998
19.4k
    memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
999
    /* As above */
1000
19.4k
    tmpmat.tx = tmpmat.ty = 0;
1001
19.4k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
1002
19.4k
    gs_setmatrix(ctx->pgs, &Trm_ctm);
1003
1004
19.4k
    gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
1005
1006
19.4k
    code = pdfi_gs_setgray(ctx, 1.0);
1007
19.4k
    if (code < 0)
1008
0
        goto out;
1009
1010
    /* The bbox device (not surprisingly) clips to the device width/height
1011
       so we have to offset our initial point to cope with glyphs that include
1012
       negative coordinates. Most significantly, descender features - hence the
1013
       larger y offset.
1014
       The offsets are guesses.
1015
     */
1016
19.4k
    startpt.x = ctx->pgs->PDFfontsize;
1017
19.4k
    startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1018
19.4k
    code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1019
19.4k
    if (code < 0)
1020
0
        goto out;
1021
1022
    /* Pretend to have at least one BT or pdfi_show will generate a spurious warning */
1023
19.4k
    ctx->text.BlockDepth++;
1024
19.4k
    code = pdfi_show(ctx, s);
1025
    /* And an ET */
1026
19.4k
    ctx->text.BlockDepth--;
1027
19.4k
    if (code < 0)
1028
0
        goto out;
1029
1030
19.4k
    code = gx_device_bbox_bbox(bbdev, bboxout);
1031
19.4k
    if (code < 0)
1032
0
        goto out;
1033
1034
19.4k
    bboxout->q.x -= bboxout->p.x;
1035
19.4k
    bboxout->q.y -= bboxout->p.y;
1036
19.4k
    bboxout->p.x = bboxout->p.y = 0;
1037
1038
19.4k
    code = gs_currentpoint(ctx->pgs, &cppt);
1039
19.4k
    if (code >= 0) {
1040
19.4k
        code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1041
19.4k
        if (code < 0)
1042
0
            goto out;
1043
19.4k
        advance_width->x = ctx->pgs->current_point.x - startpt.x;
1044
19.4k
        advance_width->y = ctx->pgs->current_point.y - startpt.y;
1045
19.4k
        code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1046
19.4k
    }
1047
19.4k
out:
1048
19.4k
    pdfi_grestore(ctx);
1049
19.4k
    (void)gs_closedevice((gx_device *)bbdev);
1050
19.4k
    gx_device_retain((gx_device *)bbdev, false);
1051
1052
19.4k
    return code;
1053
19.4k
}
1054
1055
int pdfi_Tj(pdf_context *ctx)
1056
1.54M
{
1057
1.54M
    int code = 0, code1 = 0;
1058
1.54M
    bool retrying = false, saved_preserve = false;
1059
1.54M
    pdf_string *s = NULL;
1060
1.54M
    gs_matrix saved, Trm;
1061
1.54M
    gs_point initial_point, current_point, pt;
1062
1.54M
    double linewidth = ctx->pgs->line_params.half_width;
1063
1.54M
    int initial_point_valid;
1064
1065
1.54M
    if (pdfi_count_stack(ctx) < 1)
1066
29.4k
        return_error(gs_error_stackunderflow);
1067
1068
1.51M
    if (pdfi_oc_is_off(ctx))
1069
1.85k
        goto exit;
1070
1071
1.51M
    s = (pdf_string *)ctx->stack_top[-1];
1072
1.51M
    if (pdfi_type_of(s) != PDF_STRING) {
1073
45.2k
        pdfi_pop(ctx, 1);
1074
45.2k
        return_error(gs_error_typecheck);
1075
45.2k
    }
1076
1077
    /* We can't rely on the stack reference because an error during
1078
       the text operation (i.e. retrieving objects for glyph metrics
1079
       may cause the stack to be cleared.
1080
     */
1081
1.46M
    pdfi_countup(s);
1082
1.46M
    pdfi_pop(ctx, 1);
1083
1084
1.46M
retry:
1085
    /* Save the CTM for later restoration */
1086
1.46M
    saved = ctm_only(ctx->pgs);
1087
1.46M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1088
1089
1.46M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1090
1.46M
    Trm.xy = 0;
1091
1.46M
    Trm.yx = 0;
1092
1.46M
    Trm.yy = ctx->pgs->PDFfontsize;
1093
1.46M
    Trm.tx = 0;
1094
1.46M
    Trm.ty = ctx->pgs->textrise;
1095
1096
1.46M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1097
1.46M
    if (code < 0)
1098
0
        goto exit;
1099
1100
1.46M
    if (!ctx->device_state.preserve_tr_mode) {
1101
1.38M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1102
1.38M
        if (code < 0)
1103
50.3k
            goto exit;
1104
1.33M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1105
1.33M
    } else {
1106
        /* We have to adjust the stroke width for pdfwrite so that we take into
1107
         * account the CTM, but we do not spply the font scaling. Because of
1108
         * the disconnect between pdfwrite and the interpreter, we also have to
1109
         * remove the scaling due to the resolution.
1110
         */
1111
79.9k
        gs_matrix devmatrix, matrix;
1112
79.9k
        gx_device *device = gs_currentdevice(ctx->pgs);
1113
1114
79.9k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1115
79.9k
        devmatrix.xy = 0;
1116
79.9k
        devmatrix.yx = 0;
1117
79.9k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1118
79.9k
        devmatrix.tx = 0;
1119
79.9k
        devmatrix.ty = 0;
1120
1121
79.9k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1122
79.9k
        if (code < 0)
1123
0
            goto exit;
1124
1125
79.9k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1126
79.9k
        if (code < 0)
1127
0
            goto exit;
1128
79.9k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1129
79.9k
    }
1130
1131
1.41M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1132
1.41M
    if (code < 0)
1133
0
        goto exit;
1134
1135
1.41M
    code = gs_setmatrix(ctx->pgs, &Trm);
1136
1.41M
    if (code < 0)
1137
0
        goto exit;
1138
1139
1.41M
    code = gs_moveto(ctx->pgs, 0, 0);
1140
1.41M
    if (code < 0)
1141
0
        goto Tj_error;
1142
1143
1.41M
    code = pdfi_show(ctx, s);
1144
1.41M
    if (code < 0)
1145
38.1k
        goto Tj_error;
1146
1147
1.38M
    ctx->pgs->line_params.half_width = linewidth;
1148
    /* Update the Text matrix with the current point, for the next operation
1149
     */
1150
1.38M
    (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1151
1.38M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1152
1.38M
    Trm.xy = 0;
1153
1.38M
    Trm.yx = 0;
1154
1.38M
    Trm.yy = ctx->pgs->PDFfontsize;
1155
1.38M
    Trm.tx = 0;
1156
1.38M
    Trm.ty = 0;
1157
1.38M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1158
1.38M
    if (code < 0)
1159
0
        goto Tj_error;
1160
1161
1.38M
    code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1162
1.38M
    if (code < 0)
1163
0
        goto Tj_error;
1164
1.38M
    ctx->pgs->textmatrix.tx += pt.x;
1165
1.38M
    ctx->pgs->textmatrix.ty += pt.y;
1166
1167
1.41M
Tj_error:
1168
    /* Restore the CTM to the saved value */
1169
1.41M
    (void)gs_setmatrix(ctx->pgs, &saved);
1170
    /* And restore the currentpoint */
1171
1.41M
    if (initial_point_valid)
1172
1.35M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1173
62.2k
    else
1174
62.2k
        code1 = gs_newpath(ctx->pgs);
1175
    /* And the line width */
1176
1.41M
    ctx->pgs->line_params.half_width = linewidth;
1177
1.41M
    if (code == gs_error_fallback_failed && !retrying) {
1178
6
        retrying = true;
1179
6
        saved_preserve = ctx->device_state.preserve_tr_mode;
1180
6
        ctx->device_state.preserve_tr_mode = false;
1181
6
        goto retry;
1182
6
    }
1183
1184
1.41M
    if (code == 0 && code1 < 0)
1185
0
        code = code1;
1186
1.47M
exit:
1187
1.47M
    if (retrying)
1188
6
        ctx->device_state.preserve_tr_mode = saved_preserve;
1189
1.47M
    pdfi_countdown(s);
1190
1.47M
    return code;
1191
1.41M
}
1192
1193
int pdfi_TJ(pdf_context *ctx)
1194
1.68M
{
1195
1.68M
    int code = 0, code1 = 0, i;
1196
1.68M
    bool retrying = false, saved_preserve = false;
1197
1.68M
    pdf_array *a = NULL;
1198
1.68M
    pdf_obj *o = NULL;
1199
1.68M
    double dx = 0;
1200
1.68M
    gs_point pt;
1201
1.68M
    gs_matrix saved, Trm;
1202
1.68M
    gs_point initial_point, current_point;
1203
1.68M
    double linewidth = ctx->pgs->line_params.half_width;
1204
1.68M
    pdf_font *current_font = NULL;
1205
1.68M
    int initial_point_valid;
1206
1207
1.68M
    current_font = pdfi_get_current_pdf_font(ctx);
1208
1.68M
    if (current_font == NULL)
1209
5.14k
        return_error(gs_error_invalidfont);
1210
1211
1.68M
    if (ctx->text.BlockDepth == 0) {
1212
62.0k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1213
62.0k
    }
1214
1215
1.68M
    if (pdfi_count_stack(ctx) < 1)
1216
13.4k
        return_error(gs_error_stackunderflow);
1217
1218
1.66M
    if (pdfi_oc_is_off(ctx))
1219
456
        goto exit;
1220
1221
1.66M
    a = (pdf_array *)ctx->stack_top[-1];
1222
1.66M
    if (pdfi_type_of(a) != PDF_ARRAY) {
1223
42.8k
        pdfi_pop(ctx, 1);
1224
42.8k
        return gs_note_error(gs_error_typecheck);
1225
42.8k
    }
1226
1.62M
    pdfi_countup(a);
1227
1.62M
    pdfi_pop(ctx, 1);
1228
1229
1.62M
retry:
1230
    /* Save the CTM for later restoration */
1231
1.62M
    saved = ctm_only(ctx->pgs);
1232
1.62M
    ctx->text.initial_current_point_valid = (initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0));
1233
1234
    /* Calculate the text rendering matrix, see section 1.7 PDF Reference
1235
     * page 409, section 5.3.3 Text Space details.
1236
     */
1237
1.62M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1238
1.62M
    Trm.xy = 0;
1239
1.62M
    Trm.yx = 0;
1240
1.62M
    Trm.yy = ctx->pgs->PDFfontsize;
1241
1.62M
    Trm.tx = 0;
1242
1.62M
    Trm.ty = ctx->pgs->textrise;
1243
1244
1.62M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1245
1.62M
    if (code < 0)
1246
0
        goto exit;
1247
1248
1.62M
    if (!ctx->device_state.preserve_tr_mode) {
1249
1.44M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1250
1.44M
        if (code < 0)
1251
12.8k
            goto exit;
1252
1.42M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1253
1.42M
    } else {
1254
        /* We have to adjust the stroke width for pdfwrite so that we take into
1255
         * account the CTM, but we do not spply the font scaling. Because of
1256
         * the disconnect between pdfwrite and the interpreter, we also have to
1257
         * remove the scaling due to the resolution.
1258
         */
1259
184k
        gs_matrix devmatrix, matrix;
1260
184k
        gx_device *device = gs_currentdevice(ctx->pgs);
1261
1262
184k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1263
184k
        devmatrix.xy = 0;
1264
184k
        devmatrix.yx = 0;
1265
184k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1266
184k
        devmatrix.tx = 0;
1267
184k
        devmatrix.ty = 0;
1268
1269
184k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1270
184k
        if (code < 0)
1271
0
            goto exit;
1272
1273
184k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1274
184k
        if (code < 0)
1275
0
            goto exit;
1276
184k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1277
184k
    }
1278
1279
1.61M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1280
1.61M
    if (code < 0)
1281
0
        goto exit;
1282
1.61M
    code = gs_setmatrix(ctx->pgs, &Trm);
1283
1.61M
    if (code < 0)
1284
0
        goto TJ_error;
1285
1286
1.61M
    code = gs_moveto(ctx->pgs, 0, 0);
1287
1.61M
    if (code < 0)
1288
0
        goto TJ_error;
1289
1290
21.8M
    for (i = 0; i < pdfi_array_size(a); i++) {
1291
20.2M
        code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1292
20.2M
        if (code < 0)
1293
1
            goto TJ_error;
1294
1295
20.2M
        if (pdfi_type_of(o) == PDF_STRING)
1296
10.9M
            code = pdfi_show(ctx, (pdf_string *)o);
1297
9.36M
        else {
1298
9.36M
            code = pdfi_obj_to_real(ctx, o, &dx);
1299
9.36M
            if (code < 0)
1300
80
                goto TJ_error;
1301
9.36M
            dx /= -1000;
1302
9.36M
            if (current_font->pfont && current_font->pfont->WMode == 0)
1303
9.36M
                code = gs_rmoveto(ctx->pgs, dx, 0);
1304
5.01k
            else
1305
5.01k
                code = gs_rmoveto(ctx->pgs, 0, dx);
1306
9.36M
        }
1307
20.2M
        pdfi_countdown(o);
1308
20.2M
        o = NULL;
1309
20.2M
        if (code < 0)
1310
40.6k
            goto TJ_error;
1311
20.2M
    }
1312
1313
    /* We don't want to update the Text matrix if we aren't in a text block, there
1314
     * is no point because the matrix isn't persistent between text blocks.
1315
     */
1316
1.57M
    if (ctx->text.BlockDepth > 0) {
1317
        /* Update the Text matrix with the current point, for the next operation
1318
         */
1319
1.55M
        (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1320
1.55M
        Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1321
1.55M
        Trm.xy = 0;
1322
1.55M
        Trm.yx = 0;
1323
1.55M
        Trm.yy = ctx->pgs->PDFfontsize;
1324
1.55M
        Trm.tx = 0;
1325
1.55M
        Trm.ty = 0;
1326
1.55M
        code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1327
1.55M
        if (code < 0)
1328
0
            goto TJ_error;
1329
1330
1.55M
        code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1331
1.55M
        if (code < 0)
1332
0
            goto TJ_error;
1333
1.55M
        ctx->pgs->textmatrix.tx += pt.x;
1334
1.55M
        ctx->pgs->textmatrix.ty += pt.y;
1335
1.55M
    }
1336
1.61M
TJ_error:
1337
1.61M
    pdfi_countdown(o);
1338
    /* Restore the CTM to the saved value */
1339
1.61M
    (void)gs_setmatrix(ctx->pgs, &saved);
1340
    /* And restore the currentpoint */
1341
1.61M
    if (initial_point_valid)
1342
1.58M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1343
32.2k
    else
1344
32.2k
        code1 = gs_newpath(ctx->pgs);
1345
    /* And the line width */
1346
1.61M
    ctx->pgs->line_params.half_width = linewidth;
1347
1.61M
    if (code == gs_error_fallback_failed && !retrying) {
1348
0
        retrying = true;
1349
0
        saved_preserve = ctx->device_state.preserve_tr_mode;
1350
0
        ctx->device_state.preserve_tr_mode = false;
1351
0
        goto retry;
1352
0
    }
1353
1354
1.61M
    if (code == 0 && code1 < 0)
1355
0
        code = code1;
1356
1.62M
 exit:
1357
1.62M
    if (retrying)
1358
0
        ctx->device_state.preserve_tr_mode = saved_preserve;
1359
1360
1.62M
    pdfi_countdown(a);
1361
1.62M
    return code;
1362
1.61M
}
1363
1364
static int pdfi_set_TL(pdf_context *ctx, double TL)
1365
371k
{
1366
371k
    return gs_settextleading(ctx->pgs, TL);
1367
371k
}
1368
1369
int pdfi_TL(pdf_context *ctx)
1370
11.2k
{
1371
11.2k
    int code;
1372
11.2k
    double d;
1373
1374
11.2k
    code = pdfi_destack_real(ctx, &d);
1375
11.2k
    if (code < 0)
1376
42
        return code;
1377
1378
11.2k
    return pdfi_set_TL(ctx, -d);
1379
11.2k
}
1380
1381
int pdfi_Tm(pdf_context *ctx)
1382
1.59M
{
1383
1.59M
    int code;
1384
1.59M
    float m[6];
1385
1.59M
    gs_matrix mat;
1386
1387
1.59M
    code = pdfi_destack_floats(ctx, m, 6);
1388
1.59M
    if (code < 0)
1389
165k
        return code;
1390
1391
1.43M
    if (ctx->text.BlockDepth == 0) {
1392
58.2k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1393
1394
58.2k
        gs_make_identity(&mat);
1395
58.2k
        code = gs_settextmatrix(ctx->pgs, &mat);
1396
58.2k
        if (code < 0)
1397
0
            return code;
1398
1399
58.2k
        code = gs_settextlinematrix(ctx->pgs, &mat);
1400
58.2k
        if (code < 0)
1401
0
            return code;
1402
58.2k
    }
1403
1404
1.43M
    if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1405
16.9k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1406
1407
1.43M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1408
1.43M
    if (code < 0)
1409
0
        return code;
1410
1411
1.43M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1412
1.43M
}
1413
1414
int pdfi_Tr(pdf_context *ctx)
1415
522k
{
1416
522k
    int code;
1417
522k
    int64_t mode;
1418
1419
522k
    code = pdfi_destack_int(ctx, &mode);
1420
522k
    if (code < 0)
1421
7.07k
        return code;
1422
1423
515k
    if (mode < 0 || mode > 7)
1424
4.29k
        return_error(gs_error_rangecheck);
1425
1426
    /* Detect attempts to switch from a clipping mode to a non-clipping
1427
     * mode, this is defined as invalid in the spec. (We don't warn if we haven't yet
1428
     * drawn any text in the clipping mode).
1429
     */
1430
511k
    if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1431
70
        pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1432
1433
511k
    if (ctx->device_state.preserve_tr_mode) {
1434
5.12k
        gs_settextrenderingmode(ctx->pgs, mode);
1435
5.12k
    } else
1436
506k
    {
1437
506k
        gs_point initial_point;
1438
1439
506k
        if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1440
413
            gs_settextrenderingmode(ctx->pgs, mode);
1441
            /* Capture the current position */
1442
413
            code = gs_currentpoint(ctx->pgs, &initial_point);
1443
            /* Start a new path (so our clip doesn't include any
1444
             * already extant path in the graphics state)
1445
             */
1446
413
            gs_newpath(ctx->pgs);
1447
413
            if (code >= 0)
1448
367
                gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1449
413
            code = 0;
1450
505k
        } else if (gs_currenttextrenderingmode(ctx->pgs) >= 4 && mode < 4 && ctx->text.BlockDepth != 0) {
1451
            /* If we are switching from a clipping mode to a non-clipping
1452
             * mode then behave as if we had an implicit ET to flush the
1453
             * accumulated text to a clip, then set the text rendering mode
1454
             * to the non-clip mode, and perform an implicit BT.
1455
             */
1456
72
            code = pdfi_ET(ctx);
1457
72
            if (code < 0)
1458
2
                return code;
1459
70
            gs_settextrenderingmode(ctx->pgs, mode);
1460
70
            code = pdfi_BT(ctx);
1461
70
            if (code < 0)
1462
0
                return code;
1463
70
        }
1464
505k
        else
1465
505k
            gs_settextrenderingmode(ctx->pgs, mode);
1466
506k
    }
1467
511k
    return code;
1468
511k
}
1469
1470
static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1471
3.35k
{
1472
3.35k
    return gs_settextrise(ctx->pgs, Ts);
1473
3.35k
}
1474
1475
int pdfi_Ts(pdf_context *ctx)
1476
3.74k
{
1477
3.74k
    int code;
1478
3.74k
    double d;
1479
1480
3.74k
    code = pdfi_destack_real(ctx, &d);
1481
3.74k
    if (code < 0)
1482
397
        return code;
1483
1484
3.35k
    return pdfi_set_Ts(ctx, d);
1485
3.74k
}
1486
1487
static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1488
141k
{
1489
141k
    return gs_setwordspacing(ctx->pgs, Tw);
1490
141k
}
1491
1492
int pdfi_Tw(pdf_context *ctx)
1493
141k
{
1494
141k
    int code;
1495
141k
    double d;
1496
1497
141k
    code = pdfi_destack_real(ctx, &d);
1498
141k
    if (code < 0)
1499
801
        return code;
1500
1501
140k
    return pdfi_set_Tw(ctx, d);
1502
141k
}
1503
1504
int pdfi_Tz(pdf_context *ctx)
1505
93.0k
{
1506
93.0k
    int code;
1507
93.0k
    double d;
1508
1509
93.0k
    code = pdfi_destack_real(ctx, &d);
1510
93.0k
    if (code < 0)
1511
1.77k
        return code;
1512
1513
91.2k
    return gs_settexthscaling(ctx->pgs, d);
1514
93.0k
}
1515
1516
int pdfi_singlequote(pdf_context *ctx)
1517
17.2k
{
1518
17.2k
    int code;
1519
1520
17.2k
    if (ctx->text.BlockDepth == 0) {
1521
8.24k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1522
8.24k
    }
1523
1524
17.2k
    code = pdfi_T_star(ctx);
1525
17.2k
    if (code < 0)
1526
0
        return code;
1527
1528
17.2k
    return pdfi_Tj(ctx);
1529
17.2k
}
1530
1531
int pdfi_doublequote(pdf_context *ctx)
1532
5.62k
{
1533
5.62k
    int code;
1534
5.62k
    double Tw, Tc;
1535
1536
5.62k
    if (ctx->text.BlockDepth == 0) {
1537
5.35k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1538
5.35k
    }
1539
1540
5.62k
    if (pdfi_count_stack(ctx) < 3) {
1541
5.24k
        pdfi_clearstack(ctx);
1542
5.24k
        return_error(gs_error_stackunderflow);
1543
5.24k
    }
1544
1545
382
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1546
177
        pdfi_pop(ctx, 3);
1547
177
        return gs_note_error(gs_error_typecheck);
1548
177
    }
1549
1550
205
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1551
205
    if (code < 0)
1552
2
        goto error;
1553
203
    code = pdfi_set_Tc(ctx, Tc);
1554
203
    if (code < 0)
1555
0
        goto error;
1556
1557
203
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1558
203
    if (code < 0)
1559
11
        goto error;
1560
192
    code = pdfi_set_Tw(ctx, Tw);
1561
192
    if (code < 0)
1562
0
        goto error;
1563
1564
192
    code = pdfi_T_star(ctx);
1565
192
    if (code < 0)
1566
0
        goto error;
1567
1568
192
    code = pdfi_Tj(ctx);
1569
    /* Tj pops one off the stack for us, leaving us 2 to go. */
1570
192
    pdfi_pop(ctx, 2);
1571
192
    return code;
1572
1573
13
error:
1574
13
    pdfi_pop(ctx, 3);
1575
13
    return code;
1576
192
}