Coverage Report

Created: 2026-09-14 07:34

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.66M
{
41
1.66M
    int code;
42
1.66M
    gs_matrix m;
43
1.66M
    bool illegal_BT = false;
44
45
1.66M
    if (ctx->text.BlockDepth != 0) {
46
36.0k
        illegal_BT = true;
47
36.0k
        if (ctx->text.TextClip) {
48
78
            gx_device *dev = gs_currentdevice_inline(ctx->pgs);
49
50
78
            ctx->text.TextClip = false;
51
78
            (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1);
52
78
        }
53
36.0k
        code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
54
36.0k
        if (code < 0)
55
0
            return code;
56
36.0k
    }
57
58
1.66M
    gs_make_identity(&m);
59
1.66M
    code = gs_settextmatrix(ctx->pgs, &m);
60
1.66M
    if (code < 0)
61
0
        return code;
62
63
1.66M
    code = gs_settextlinematrix(ctx->pgs, &m);
64
1.66M
    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.66M
    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
126
        gs_newpath(ctx->pgs);
75
126
    }
76
77
1.66M
    ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
78
1.66M
    if (!ctx->pgs->current_point_valid)
79
1.34M
        code = gs_moveto(ctx->pgs, 0, 0);
80
81
1.66M
    ctx->text.BlockDepth++;
82
83
1.66M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
84
406k
        gs_begin_transparency_text_group(ctx->pgs);
85
86
1.66M
    return code;
87
1.66M
}
88
89
static int do_ET(pdf_context *ctx)
90
1.68M
{
91
1.68M
    int code = 0;
92
1.68M
    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.68M
    if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
105
846
        gs_point initial_point;
106
107
846
        if  (!ctx->device_state.preserve_tr_mode) {
108
802
            ctx->text.TextClip = false;
109
            /* Capture the current position */
110
802
            code = gs_currentpoint(ctx->pgs, &initial_point);
111
802
            if (code >= 0 || code == gs_error_nocurrentpoint) {
112
800
                gs_point adjust;
113
800
                bool nocurrentpoint = code >= 0 ? false : true;
114
115
800
                gs_currentfilladjust(ctx->pgs, &adjust);
116
800
                code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
117
800
                if (code < 0)
118
0
                    return code;
119
120
800
                code = gs_clip(ctx->pgs);
121
800
                if (code >= 0)
122
800
                    copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
123
124
800
                code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
125
800
                if (code < 0)
126
0
                    return code;
127
128
800
                if (copy != NULL)
129
800
                    (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
130
131
800
                if (nocurrentpoint == false)
132
800
                    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
133
800
            }
134
802
        }
135
846
    }
136
1.68M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
137
413k
        gs_end_transparency_text_group(ctx->pgs);
138
139
1.68M
    if (!ctx->text.initial_current_point_valid)
140
885k
        gs_newpath(ctx->pgs);
141
1.68M
    return code;
142
1.68M
}
143
144
int pdfi_ET(pdf_context *ctx)
145
1.64M
{
146
1.64M
    if (ctx->text.BlockDepth == 0) {
147
61.4k
        int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
148
61.4k
        return code;
149
61.4k
    }
150
151
1.57M
    ctx->text.BlockDepth--;
152
153
1.57M
    return do_ET(ctx);
154
1.64M
}
155
156
int pdfi_T_star(pdf_context *ctx)
157
142k
{
158
142k
    int code;
159
142k
    gs_matrix m, mat;
160
161
142k
    if (ctx->text.BlockDepth == 0) {
162
30.6k
        int code;
163
30.6k
        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
30.6k
    }
166
167
142k
    gs_make_identity(&m);
168
142k
    m.ty += ctx->pgs->textleading;
169
170
142k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
171
142k
    if (code < 0)
172
0
        return code;
173
174
142k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
175
142k
    if (code < 0)
176
0
        return code;
177
178
142k
    code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
179
142k
    return code;
180
142k
}
181
182
static int pdfi_set_Tc(pdf_context *ctx, double Tc)
183
653k
{
184
653k
    return gs_settextspacing(ctx->pgs, Tc);
185
653k
}
186
187
int pdfi_Tc(pdf_context *ctx)
188
666k
{
189
666k
    int code;
190
666k
    double d;
191
192
666k
    code = pdfi_destack_real(ctx, &d);
193
666k
    if (code < 0)
194
12.4k
        return code;
195
196
653k
    return pdfi_set_Tc(ctx, d);
197
666k
}
198
199
int pdfi_Td(pdf_context *ctx)
200
1.23M
{
201
1.23M
    int code;
202
1.23M
    double Txy[2];
203
1.23M
    gs_matrix m, mat;
204
205
1.23M
    code = pdfi_destack_reals(ctx, Txy, 2);
206
1.23M
    if (code < 0)
207
20.1k
        return code;
208
209
1.21M
    gs_make_identity(&m);
210
211
1.21M
    m.tx = Txy[0];
212
1.21M
    m.ty = Txy[1];
213
214
1.21M
    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.21M
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
228
1.21M
    if (code < 0)
229
0
        return code;
230
231
1.21M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
232
1.21M
    if (code < 0)
233
0
        return code;
234
235
1.21M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
236
1.21M
}
237
238
int pdfi_TD(pdf_context *ctx)
239
363k
{
240
363k
    int code;
241
363k
    double Txy[2];
242
363k
    gs_matrix m, mat;
243
244
363k
    gs_make_identity(&m);
245
246
363k
    code = pdfi_destack_reals(ctx, Txy, 2);
247
363k
    if (code < 0)
248
2.50k
        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.24k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
255
256
1.24k
        gs_make_identity(&mat);
257
1.24k
        code = gs_settextmatrix(ctx->pgs, &mat);
258
1.24k
        if (code < 0)
259
0
            return code;
260
261
1.24k
        code = gs_settextlinematrix(ctx->pgs, &mat);
262
1.24k
        if (code < 0)
263
0
            return code;
264
1.24k
    }
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
13.1M
{
292
13.1M
    pdf_font *current_font = NULL;
293
13.1M
    gs_matrix mat;
294
13.1M
    float *x_widths = NULL, *y_widths = NULL, width;
295
13.1M
    double Tw = 0, Tc = 0;
296
13.1M
    int i, code;
297
298
13.1M
    text->data.chars = NULL;
299
13.1M
    text->x_widths = NULL;
300
13.1M
    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
13.1M
    current_font = pdfi_get_current_pdf_font(ctx);
308
309
13.1M
    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
13.1M
    Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
317
318
13.1M
    if (current_font->pdfi_font_type == e_pdf_font_type1 ||
319
4.93M
        current_font->pdfi_font_type == e_pdf_font_cff ||
320
3.49M
        current_font->pdfi_font_type == e_pdf_font_type3 ||
321
3.44M
        current_font->pdfi_font_type == e_pdf_font_cff ||
322
3.44M
        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
13.1M
    {
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
13.1M
        if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
330
4.90M
            text->operation = TEXT_RETURN_WIDTH;
331
4.90M
            if (Tc != 0) {
332
1.00M
                text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
333
1.00M
                if (current_font->pfont && current_font->pfont->WMode == 0) {
334
1.00M
                    text->delta_all.x = Tc;
335
1.00M
                    text->delta_all.y = 0;
336
1.00M
                } else {
337
3
                    text->delta_all.y = Tc;
338
3
                    text->delta_all.x = 0;
339
3
                }
340
1.00M
            }
341
8.22M
        } else {
342
8.22M
            gs_point pt;
343
344
8.22M
            x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
345
8.22M
            y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
346
8.22M
            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
8.22M
            memset(x_widths, 0x00, s->length * sizeof(float));
352
8.22M
            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
8.22M
            mat = current_font->pfont->FontMatrix;
358
359
34.2M
            for (i = 0;i < s->length; i++) {
360
                /* Get the width (in unscaled text units) */
361
26.0M
                if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
362
109k
                    width = current_font->MissingWidth;
363
25.9M
                else
364
25.9M
                    width = current_font->Widths[s->data[i] - current_font->FirstChar];
365
                /* And convert the width into an appropriate value for the current environment */
366
26.0M
                gs_distance_transform(width, 0, &mat, &pt);
367
26.0M
                x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
368
                /* Add any Tc value */
369
26.0M
                x_widths[i] += Tc;
370
26.0M
            }
371
8.22M
            text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
372
8.22M
            text->x_widths = x_widths;
373
8.22M
            text->y_widths = y_widths;
374
8.22M
            text->widths_size = s->length * 2;
375
8.22M
        }
376
377
13.1M
        Tw = gs_currentwordspacing(ctx->pgs);
378
13.1M
        if (Tw != 0) {
379
799k
            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
799k
            if (current_font->pfont && current_font->pfont->WMode == 0) {
385
799k
                text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
386
799k
                text->delta_space.y = 0;
387
799k
            } else {
388
0
                text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
389
0
                text->delta_space.x = 0;
390
0
            }
391
799k
            text->space.s_char = 0x20;
392
799k
        }
393
394
13.1M
        if (current_font->pdfi_font_type == e_pdf_font_type3) {
395
45.6k
            text->operation |= TEXT_FROM_CHARS;
396
45.6k
            text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
397
45.6k
            if (!text->data.chars) {
398
0
                code = gs_note_error(gs_error_VMerror);
399
0
                goto text_params_error;
400
0
            }
401
402
222k
            for (i = 0; i < s->length; i++) {
403
177k
                ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
404
177k
            }
405
45.6k
        }
406
13.0M
        else {
407
13.0M
            text->operation |= TEXT_FROM_BYTES;
408
13.0M
            text->data.bytes = (const byte *)s->data;
409
13.0M
        }
410
13.1M
        text->size = s->length;
411
13.1M
    }
412
52
    else {
413
52
        code = gs_note_error(gs_error_invalidfont);
414
52
        goto text_params_error;
415
52
    }
416
417
13.1M
    return 0;
418
419
52
text_params_error:
420
52
    gs_free_object(ctx->memory, x_widths, "X widths array for text");
421
52
    text->x_widths = NULL;
422
52
    gs_free_object(ctx->memory, y_widths, "Y widths array for text");
423
52
    text->y_widths = NULL;
424
52
    gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars");
425
52
    text->data.chars = NULL;
426
52
    return code;
427
13.1M
}
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.9M
{
436
12.9M
    int code = 0;
437
12.9M
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
438
439
12.9M
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
440
12.9M
    if (code >= 0) {
441
12.9M
        penum->single_byte_space = true;
442
12.9M
        saved_penum = ctx->text.current_enum;
443
12.9M
        ctx->text.current_enum = penum;
444
12.9M
        code = gs_text_process(penum);
445
12.9M
        gs_text_release(ctx->pgs, penum, "pdfi_Tj");
446
12.9M
        ctx->text.current_enum = saved_penum;
447
12.9M
    }
448
12.9M
    return code;
449
12.9M
}
450
451
/* Mode 0 - fill */
452
static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
453
10.2M
{
454
10.2M
    int code;
455
456
    /* just draw the text */
457
10.2M
    text->operation |= TEXT_DO_DRAW;
458
459
10.2M
    code = pdfi_show_simple(ctx, text);
460
461
10.2M
    text->operation &= ~TEXT_DO_DRAW;
462
10.2M
    return code;
463
10.2M
}
464
465
/* Mode 1 - stroke */
466
static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text)
467
1.42k
{
468
1.42k
    int code;
469
1.42k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
470
1.42k
    gs_point end_point, initial_point;
471
1.42k
    gx_device *dev = ctx->pgs->device;
472
1.42k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
473
474
1.42k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
475
476
    /* Capture the current position */
477
1.42k
    code = gs_currentpoint(ctx->pgs, &initial_point);
478
1.42k
    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.42k
    code = pdfi_gsave(ctx);
486
1.42k
    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.42k
    code = gs_newpath(ctx->pgs);
493
1.42k
    if (code < 0)
494
0
        goto Tr1_error;
495
1.42k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
496
1.42k
    if (code < 0)
497
0
        goto Tr1_error;
498
499
    /* Don't draw the text, create a path suitable for stroking */
500
1.42k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
501
502
    /* Run the text methods to create the path */
503
1.42k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
504
1.42k
    if (code < 0)
505
30
        goto Tr1_error;
506
507
1.39k
    penum->single_byte_space = true;
508
1.39k
    saved_penum = ctx->text.current_enum;
509
1.39k
    ctx->text.current_enum = penum;
510
1.39k
    code = gs_text_process(penum);
511
1.39k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
512
1.39k
    ctx->text.current_enum = saved_penum;
513
1.39k
    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.39k
    code = gs_currentpoint(ctx->pgs, &end_point);
522
1.39k
    if (code < 0)
523
0
        goto Tr1_error;
524
525
1.39k
    if (talphabits != galphabits)
526
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
527
528
    /* Change to the current stroking colour */
529
1.39k
    gs_swapcolors_quick(ctx->pgs);
530
    /* Finally, stroke the actual path */
531
1.39k
    code = gs_stroke(ctx->pgs);
532
    /* Switch back to the non-stroke colour */
533
1.39k
    gs_swapcolors_quick(ctx->pgs);
534
535
1.39k
    if (talphabits != galphabits)
536
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
537
538
1.42k
Tr1_error:
539
    /* And grestore back to where we started */
540
1.42k
    (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.42k
    if (code >= 0)
544
1.39k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
545
546
1.42k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
547
1.42k
    return code;
548
1.39k
}
549
550
/* Mode 2 - fill then stroke */
551
static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text)
552
8.01k
{
553
8.01k
    int code, restart = 0;
554
8.01k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
555
8.01k
    gs_point end_point, initial_point;
556
8.01k
    gx_device *dev = ctx->pgs->device;
557
8.01k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
558
559
8.01k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
560
561
    /* Capture the current position */
562
8.01k
    code = gs_currentpoint(ctx->pgs, &initial_point);
563
8.01k
    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
8.01k
    code = pdfi_gsave(ctx);
571
8.01k
    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
8.01k
    code = gs_newpath(ctx->pgs);
578
8.01k
    if (code < 0)
579
0
        goto Tr1_error;
580
8.01k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
581
8.01k
    if (code < 0)
582
0
        goto Tr1_error;
583
584
    /* Don't draw the text, create a path suitable for stroking */
585
8.01k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
586
587
    /* Run the text methods to create the path */
588
8.01k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
589
8.01k
    if (code < 0)
590
0
        goto Tr1_error;
591
592
8.01k
    penum->single_byte_space = true;
593
8.01k
    saved_penum = ctx->text.current_enum;
594
8.01k
    ctx->text.current_enum = penum;
595
8.01k
    code = gs_text_process(penum);
596
8.01k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
597
8.01k
    ctx->text.current_enum = saved_penum;
598
8.01k
    if (code < 0)
599
46
        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.97k
    code = gs_currentpoint(ctx->pgs, &end_point);
607
7.97k
    if (code < 0)
608
0
        goto Tr1_error;
609
7.97k
    if (talphabits != galphabits)
610
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
611
7.97k
    code = gs_fillstroke(ctx->pgs, &restart);
612
7.97k
    if (talphabits != galphabits)
613
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
614
615
8.01k
Tr1_error:
616
    /* And grestore back to where we started */
617
8.01k
    (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
8.01k
    if (code >= 0)
621
7.97k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
622
623
8.01k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
624
8.01k
    return code;
625
7.97k
}
626
627
static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
628
121k
{
629
121k
    int code;
630
121k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
631
632
    /* Don't draw the text */
633
121k
    text->operation |= TEXT_DO_NONE | TEXT_RETURN_WIDTH | TEXT_RENDER_MODE_3;
634
635
    /* Run the text methods to create the path */
636
121k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
637
121k
    if (code < 0)
638
0
        return code;
639
640
121k
    penum->single_byte_space = true;
641
121k
    saved_penum = ctx->text.current_enum;
642
121k
    ctx->text.current_enum = penum;
643
121k
    code = gs_text_process(penum);
644
121k
    if (code >= 0)
645
120k
        code = gs_rmoveto(ctx->pgs, penum->returned.total_width.x, penum->returned.total_width.y);
646
121k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
647
121k
    ctx->text.current_enum = saved_penum;
648
649
121k
    return code;
650
121k
}
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.26k
{
657
2.26k
    int code;
658
2.26k
    gs_point initial_point;
659
660
    /* Capture the current position */
661
2.26k
    code = gs_currentpoint(ctx->pgs, &initial_point);
662
2.26k
    if (code < 0)
663
0
        return code;
664
665
2.26k
    ctx->text.TextClip = true;
666
    /* First fill the text */
667
2.26k
    code = pdfi_show_Tr_0(ctx, text);
668
2.26k
    if (code < 0)
669
0
        return code;
670
671
    /* Return the current point to the initial point */
672
2.26k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
673
2.26k
    if (code >= 0)
674
        /* And add the text to the acumulated path for clipping */
675
2.26k
        code = pdfi_show_Tr_7(ctx, text);
676
677
2.26k
    return code;
678
2.26k
}
679
680
static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
681
276
{
682
276
    int code;
683
276
    gs_point initial_point;
684
685
    /* Capture the current position */
686
276
    code = gs_currentpoint(ctx->pgs, &initial_point);
687
276
    if (code < 0)
688
0
        return code;
689
690
276
    ctx->text.TextClip = true;
691
    /* First stroke the text */
692
276
    code = pdfi_show_Tr_1(ctx, text);
693
276
    if (code < 0)
694
0
        return code;
695
696
    /* Return the current point to the initial point */
697
276
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
698
276
    if (code >= 0)
699
        /* And add the text to the acumulated path for clipping */
700
276
        code = pdfi_show_Tr_7(ctx, text);
701
702
276
    return code;
703
276
}
704
705
static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
706
277
{
707
277
    int code;
708
277
    gs_point initial_point;
709
710
    /* Capture the current position */
711
277
    code = gs_currentpoint(ctx->pgs, &initial_point);
712
277
    if (code < 0)
713
0
        return code;
714
715
277
    ctx->text.TextClip = true;
716
    /* First fill and stroke the text */
717
277
    code = pdfi_show_Tr_2(ctx, text);
718
277
    if (code < 0)
719
0
        return code;
720
721
    /* Return the current point to the initial point */
722
277
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
723
277
    if (code >= 0)
724
        /* And add the text to the acumulated path for clipping */
725
277
        code = pdfi_show_Tr_7(ctx, text);
726
727
277
    return code;
728
277
}
729
730
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
731
2.93k
{
732
2.93k
    int code;
733
2.93k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
734
735
2.93k
    ctx->text.TextClip = true;
736
    /* Don't draw the text, create a path suitable for filling */
737
2.93k
    text->operation |= TEXT_DO_TRUE_CHARPATH;
738
739
    /* Run the text methods to create the path */
740
2.93k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
741
2.93k
    if (code < 0)
742
0
        goto Tr7_error;
743
744
2.93k
    penum->single_byte_space = true;
745
2.93k
    saved_penum = ctx->text.current_enum;
746
2.93k
    ctx->text.current_enum = penum;
747
2.93k
    code = gs_text_process(penum);
748
2.93k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
749
2.93k
    ctx->text.current_enum = saved_penum;
750
751
2.93k
Tr7_error:
752
2.93k
    text->operation &= ~TEXT_DO_TRUE_CHARPATH;
753
2.93k
    return code;
754
2.93k
}
755
756
static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
757
2.76M
{
758
2.76M
    int Trmode = 0, code;
759
2.76M
    pdf_font *current_font = NULL;
760
2.76M
    gs_point initial_point;
761
762
2.76M
    current_font = pdfi_get_current_pdf_font(ctx);
763
2.76M
    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.76M
    code = gs_currentpoint(ctx->pgs, &initial_point);
768
769
2.76M
    Trmode = gs_currenttextrenderingmode(ctx->pgs);
770
2.76M
    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.75M
    else
777
2.75M
        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.76M
    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.76M
    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.76M
    code = pdfi_show_simple(ctx, text);
819
2.76M
    return code;
820
2.76M
}
821
822
static int pdfi_show(pdf_context *ctx, pdf_string *s)
823
13.1M
{
824
13.1M
    int code = 0, SavedTextDepth = 0;
825
13.1M
    int code1 = 0;
826
13.1M
    gs_text_params_t text;
827
13.1M
    pdf_font *current_font = NULL;
828
13.1M
    int Trmode = 0;
829
13.1M
    int initial_gsave_level = ctx->pgs->level;
830
13.1M
    pdfi_trans_state_t state;
831
13.1M
    int outside_text_block = 0;
832
833
13.1M
    if (ctx->text.BlockDepth == 0) {
834
105k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
835
105k
        outside_text_block = 1;
836
105k
    }
837
838
13.1M
    if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
839
13.1M
     || 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.90k
         return 0;
845
8.90k
    }
846
847
13.1M
    current_font = pdfi_get_current_pdf_font(ctx);
848
13.1M
    if (current_font == NULL)
849
12
        return_error(gs_error_invalidfont);
850
851
13.1M
    code = pdfi_show_set_params(ctx, s, &text);
852
13.1M
    if (code < 0)
853
52
        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
13.1M
    SavedTextDepth = ctx->text.BlockDepth;
863
13.1M
    ctx->text.BlockDepth = 0;
864
13.1M
    code = pdfi_trans_setup_text(ctx, &state, true);
865
13.1M
    ctx->text.BlockDepth = SavedTextDepth;
866
867
13.1M
    if (code >= 0) {
868
13.1M
        if (ctx->device_state.preserve_tr_mode) {
869
2.76M
        code = pdfi_show_Tr_preserve(ctx, &text);
870
10.3M
        } else {
871
10.3M
            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
10.3M
            if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
877
243
                Trmode = 0;
878
879
10.3M
            switch(Trmode) {
880
10.2M
            case 0:
881
10.2M
                code = pdfi_show_Tr_0(ctx, &text);
882
10.2M
                break;
883
1.14k
            case 1:
884
1.14k
                code = pdfi_show_Tr_1(ctx, &text);
885
1.14k
                break;
886
7.74k
            case 2:
887
7.74k
                code = pdfi_show_Tr_2(ctx, &text);
888
7.74k
                break;
889
121k
            case 3:
890
121k
                code = pdfi_show_Tr_3(ctx, &text);
891
121k
                break;
892
2.26k
            case 4:
893
2.26k
                code = pdfi_show_Tr_4(ctx, &text);
894
2.26k
                break;
895
276
            case 5:
896
276
                code = pdfi_show_Tr_5(ctx, &text);
897
276
                break;
898
277
            case 6:
899
277
                code = pdfi_show_Tr_6(ctx, &text);
900
277
                break;
901
118
            case 7:
902
118
                code = pdfi_show_Tr_7(ctx, &text);
903
118
                break;
904
0
            default:
905
0
                break;
906
10.3M
            }
907
10.3M
        }
908
13.1M
        code1 = pdfi_trans_teardown_text(ctx, &state);
909
13.1M
        if (code == 0)
910
13.0M
            code = code1;
911
13.1M
    }
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
13.1M
    while(ctx->pgs->level > initial_gsave_level)
922
394
        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
13.1M
    if (outside_text_block) {
929
101k
        code1 = do_ET(ctx);
930
101k
        if (code == 0)
931
96.8k
            code = code1;
932
101k
    }
933
934
13.1M
show_error:
935
13.1M
    if ((void *)text.data.chars != (void *)s->data)
936
45.7k
        gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
937
938
13.1M
    gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
939
13.1M
    gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
940
13.1M
    return code;
941
13.1M
}
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
18.0k
{
948
18.0k
    int code = 0;
949
18.0k
    gx_device_bbox *bbdev;
950
18.0k
    pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
951
18.0k
    gs_matrix tmpmat, Trm, Trm_ctm;
952
18.0k
    gs_point cppt, startpt;
953
954
18.0k
    if (current_font == NULL)
955
0
        return_error(gs_error_invalidfont);
956
957
958
18.0k
    for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
959
960
18.0k
    if (ctx->devbbox == NULL) {
961
189
        bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
962
189
        if (bbdev == NULL)
963
0
           return_error(gs_error_VMerror);
964
189
        gx_device_bbox_init(bbdev, NULL, ctx->memory);
965
189
        ctx->devbbox = (gx_device *)bbdev;
966
189
        rc_increment(ctx->devbbox);
967
189
    }
968
17.8k
    else {
969
17.8k
        bbdev = (gx_device_bbox *)ctx->devbbox;
970
17.8k
    }
971
18.0k
    gx_device_retain((gx_device *)bbdev, true);
972
18.0k
    gx_device_bbox_set_white_opaque(bbdev, true);
973
974
18.0k
    code = pdfi_gsave(ctx);
975
18.0k
    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
18.0k
    gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
981
982
18.0k
    code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
983
18.0k
    if (code < 0)
984
0
        goto out;
985
986
18.0k
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
987
18.0k
    Trm.xy = 0;
988
18.0k
    Trm.yx = 0;
989
18.0k
    Trm.yy = ctx->pgs->PDFfontsize;
990
18.0k
    Trm.tx = 0;
991
18.0k
    Trm.ty = ctx->pgs->textrise;
992
993
18.0k
    memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
994
    /* We want to avoid any translations unrelated to the extents of the text */
995
18.0k
    tmpmat.tx = tmpmat.ty = 0;
996
18.0k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm);
997
998
18.0k
    memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
999
    /* As above */
1000
18.0k
    tmpmat.tx = tmpmat.ty = 0;
1001
18.0k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
1002
18.0k
    gs_setmatrix(ctx->pgs, &Trm_ctm);
1003
1004
18.0k
    gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
1005
1006
18.0k
    code = pdfi_gs_setgray(ctx, 1.0);
1007
18.0k
    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
18.0k
    startpt.x = ctx->pgs->PDFfontsize;
1017
18.0k
    startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1018
18.0k
    code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1019
18.0k
    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
18.0k
    ctx->text.BlockDepth++;
1024
18.0k
    code = pdfi_show(ctx, s);
1025
    /* And an ET */
1026
18.0k
    ctx->text.BlockDepth--;
1027
18.0k
    if (code < 0)
1028
0
        goto out;
1029
1030
18.0k
    code = gx_device_bbox_bbox(bbdev, bboxout);
1031
18.0k
    if (code < 0)
1032
0
        goto out;
1033
1034
18.0k
    bboxout->q.x -= bboxout->p.x;
1035
18.0k
    bboxout->q.y -= bboxout->p.y;
1036
18.0k
    bboxout->p.x = bboxout->p.y = 0;
1037
1038
18.0k
    code = gs_currentpoint(ctx->pgs, &cppt);
1039
18.0k
    if (code >= 0) {
1040
18.0k
        code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1041
18.0k
        if (code < 0)
1042
0
            goto out;
1043
18.0k
        advance_width->x = ctx->pgs->current_point.x - startpt.x;
1044
18.0k
        advance_width->y = ctx->pgs->current_point.y - startpt.y;
1045
18.0k
        code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1046
18.0k
    }
1047
18.0k
out:
1048
18.0k
    pdfi_grestore(ctx);
1049
18.0k
    (void)gs_closedevice((gx_device *)bbdev);
1050
18.0k
    gx_device_retain((gx_device *)bbdev, false);
1051
1052
18.0k
    return code;
1053
18.0k
}
1054
1055
int pdfi_Tj(pdf_context *ctx)
1056
1.58M
{
1057
1.58M
    int code = 0;
1058
1.58M
    pdf_string *s = NULL;
1059
1.58M
    gs_matrix saved, Trm;
1060
1.58M
    gs_point initial_point, current_point, pt;
1061
1.58M
    double linewidth = ctx->pgs->line_params.half_width;
1062
1.58M
    int initial_point_valid;
1063
1064
1.58M
    if (pdfi_count_stack(ctx) < 1)
1065
29.0k
        return_error(gs_error_stackunderflow);
1066
1067
1.55M
    if (pdfi_oc_is_off(ctx))
1068
1.86k
        goto exit;
1069
1070
1.55M
    s = (pdf_string *)ctx->stack_top[-1];
1071
1.55M
    if (pdfi_type_of(s) != PDF_STRING) {
1072
46.1k
        pdfi_pop(ctx, 1);
1073
46.1k
        return_error(gs_error_typecheck);
1074
46.1k
    }
1075
1076
    /* We can't rely on the stack reference because an error during
1077
       the text operation (i.e. retrieving objects for glyph metrics
1078
       may cause the stack to be cleared.
1079
     */
1080
1.50M
    pdfi_countup(s);
1081
1.50M
    pdfi_pop(ctx, 1);
1082
1083
    /* Save the CTM for later restoration */
1084
1.50M
    saved = ctm_only(ctx->pgs);
1085
1.50M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1086
1087
1.50M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1088
1.50M
    Trm.xy = 0;
1089
1.50M
    Trm.yx = 0;
1090
1.50M
    Trm.yy = ctx->pgs->PDFfontsize;
1091
1.50M
    Trm.tx = 0;
1092
1.50M
    Trm.ty = ctx->pgs->textrise;
1093
1094
1.50M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1095
1.50M
    if (code < 0)
1096
0
        goto exit;
1097
1098
1.50M
    if (!ctx->device_state.preserve_tr_mode) {
1099
1.43M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1100
1.43M
        if (code < 0)
1101
52.0k
            goto exit;
1102
1.38M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1103
1.38M
    } else {
1104
        /* We have to adjust the stroke width for pdfwrite so that we take into
1105
         * account the CTM, but we do not spply the font scaling. Because of
1106
         * the disconnect between pdfwrite and the interpreter, we also have to
1107
         * remove the scaling due to the resolution.
1108
         */
1109
75.4k
        gs_matrix devmatrix, matrix;
1110
75.4k
        gx_device *device = gs_currentdevice(ctx->pgs);
1111
1112
75.4k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1113
75.4k
        devmatrix.xy = 0;
1114
75.4k
        devmatrix.yx = 0;
1115
75.4k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1116
75.4k
        devmatrix.tx = 0;
1117
75.4k
        devmatrix.ty = 0;
1118
1119
75.4k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1120
75.4k
        if (code < 0)
1121
0
            goto exit;
1122
1123
75.4k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1124
75.4k
        if (code < 0)
1125
0
            goto exit;
1126
75.4k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1127
75.4k
    }
1128
1129
1.45M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1130
1.45M
    if (code < 0)
1131
0
        goto exit;
1132
1133
1.45M
    code = gs_setmatrix(ctx->pgs, &Trm);
1134
1.45M
    if (code < 0)
1135
0
        goto exit;
1136
1137
1.45M
    code = gs_moveto(ctx->pgs, 0, 0);
1138
1.45M
    if (code < 0)
1139
0
        goto Tj_error;
1140
1141
1.45M
    code = pdfi_show(ctx, s);
1142
1.45M
    if (code < 0)
1143
38.5k
        goto Tj_error;
1144
1145
1.41M
    ctx->pgs->line_params.half_width = linewidth;
1146
    /* Update the Text matrix with the current point, for the next operation
1147
     */
1148
1.41M
    (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1149
1.41M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1150
1.41M
    Trm.xy = 0;
1151
1.41M
    Trm.yx = 0;
1152
1.41M
    Trm.yy = ctx->pgs->PDFfontsize;
1153
1.41M
    Trm.tx = 0;
1154
1.41M
    Trm.ty = 0;
1155
1.41M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1156
1.41M
    if (code < 0)
1157
0
        goto Tj_error;
1158
1159
1.41M
    code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1160
1.41M
    if (code < 0)
1161
0
        goto Tj_error;
1162
1.41M
    ctx->pgs->textmatrix.tx += pt.x;
1163
1.41M
    ctx->pgs->textmatrix.ty += pt.y;
1164
1165
1.45M
Tj_error:
1166
    /* Restore the CTM to the saved value */
1167
1.45M
    (void)gs_setmatrix(ctx->pgs, &saved);
1168
    /* And restore the currentpoint */
1169
1.45M
    if (initial_point_valid)
1170
1.39M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1171
63.1k
    else
1172
63.1k
        code = gs_newpath(ctx->pgs);
1173
    /* And the line width */
1174
1.45M
    ctx->pgs->line_params.half_width = linewidth;
1175
1176
1.51M
 exit:
1177
1.51M
    pdfi_countdown(s);
1178
1.51M
    return code;
1179
1.45M
}
1180
1181
int pdfi_TJ(pdf_context *ctx)
1182
1.73M
{
1183
1.73M
    int code = 0, i;
1184
1.73M
    pdf_array *a = NULL;
1185
1.73M
    pdf_obj *o = NULL;
1186
1.73M
    double dx = 0;
1187
1.73M
    gs_point pt;
1188
1.73M
    gs_matrix saved, Trm;
1189
1.73M
    gs_point initial_point, current_point;
1190
1.73M
    double linewidth = ctx->pgs->line_params.half_width;
1191
1.73M
    pdf_font *current_font = NULL;
1192
1.73M
    int initial_point_valid;
1193
1194
1.73M
    current_font = pdfi_get_current_pdf_font(ctx);
1195
1.73M
    if (current_font == NULL)
1196
5.10k
        return_error(gs_error_invalidfont);
1197
1198
1.72M
    if (ctx->text.BlockDepth == 0) {
1199
59.5k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1200
59.5k
    }
1201
1202
1.72M
    if (pdfi_count_stack(ctx) < 1)
1203
13.2k
        return_error(gs_error_stackunderflow);
1204
1205
1.71M
    if (pdfi_oc_is_off(ctx))
1206
522
        goto exit;
1207
1208
1.71M
    a = (pdf_array *)ctx->stack_top[-1];
1209
1.71M
    if (pdfi_type_of(a) != PDF_ARRAY) {
1210
42.2k
        pdfi_pop(ctx, 1);
1211
42.2k
        return gs_note_error(gs_error_typecheck);
1212
42.2k
    }
1213
1.66M
    pdfi_countup(a);
1214
1.66M
    pdfi_pop(ctx, 1);
1215
1216
    /* Save the CTM for later restoration */
1217
1.66M
    saved = ctm_only(ctx->pgs);
1218
1.66M
    ctx->text.initial_current_point_valid = (initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0));
1219
1220
    /* Calculate the text rendering matrix, see section 1.7 PDF Reference
1221
     * page 409, section 5.3.3 Text Space details.
1222
     */
1223
1.66M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1224
1.66M
    Trm.xy = 0;
1225
1.66M
    Trm.yx = 0;
1226
1.66M
    Trm.yy = ctx->pgs->PDFfontsize;
1227
1.66M
    Trm.tx = 0;
1228
1.66M
    Trm.ty = ctx->pgs->textrise;
1229
1230
1.66M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1231
1.66M
    if (code < 0)
1232
0
        goto exit;
1233
1234
1.66M
    if (!ctx->device_state.preserve_tr_mode) {
1235
1.48M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1236
1.48M
        if (code < 0)
1237
10.9k
            goto exit;
1238
1.47M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1239
1.47M
    } else {
1240
        /* We have to adjust the stroke width for pdfwrite so that we take into
1241
         * account the CTM, but we do not spply the font scaling. Because of
1242
         * the disconnect between pdfwrite and the interpreter, we also have to
1243
         * remove the scaling due to the resolution.
1244
         */
1245
186k
        gs_matrix devmatrix, matrix;
1246
186k
        gx_device *device = gs_currentdevice(ctx->pgs);
1247
1248
186k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1249
186k
        devmatrix.xy = 0;
1250
186k
        devmatrix.yx = 0;
1251
186k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1252
186k
        devmatrix.tx = 0;
1253
186k
        devmatrix.ty = 0;
1254
1255
186k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1256
186k
        if (code < 0)
1257
0
            goto exit;
1258
1259
186k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1260
186k
        if (code < 0)
1261
0
            goto exit;
1262
186k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1263
186k
    }
1264
1265
1.65M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1266
1.65M
    if (code < 0)
1267
0
        goto exit;
1268
1.65M
    code = gs_setmatrix(ctx->pgs, &Trm);
1269
1.65M
    if (code < 0)
1270
0
        goto TJ_error;
1271
1272
1.65M
    code = gs_moveto(ctx->pgs, 0, 0);
1273
1.65M
    if (code < 0)
1274
0
        goto TJ_error;
1275
1276
23.3M
    for (i = 0; i < pdfi_array_size(a); i++) {
1277
21.7M
        code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1278
21.7M
        if (code < 0)
1279
1
            goto TJ_error;
1280
1281
21.7M
        if (pdfi_type_of(o) == PDF_STRING)
1282
11.6M
            code = pdfi_show(ctx, (pdf_string *)o);
1283
10.0M
        else {
1284
10.0M
            code = pdfi_obj_to_real(ctx, o, &dx);
1285
10.0M
            if (code < 0)
1286
76
                goto TJ_error;
1287
10.0M
            dx /= -1000;
1288
10.0M
            if (current_font->pfont && current_font->pfont->WMode == 0)
1289
10.0M
                code = gs_rmoveto(ctx->pgs, dx, 0);
1290
5.15k
            else
1291
5.15k
                code = gs_rmoveto(ctx->pgs, 0, dx);
1292
10.0M
        }
1293
21.7M
        pdfi_countdown(o);
1294
21.7M
        o = NULL;
1295
21.7M
        if (code < 0)
1296
39.6k
            goto TJ_error;
1297
21.7M
    }
1298
1299
    /* We don't want to update the Text matrix if we aren't in a text block, there
1300
     * is no point because the matrix isn't persistent between text blocks.
1301
     */
1302
1.61M
    if (ctx->text.BlockDepth > 0) {
1303
        /* Update the Text matrix with the current point, for the next operation
1304
         */
1305
1.60M
        (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1306
1.60M
        Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1307
1.60M
        Trm.xy = 0;
1308
1.60M
        Trm.yx = 0;
1309
1.60M
        Trm.yy = ctx->pgs->PDFfontsize;
1310
1.60M
        Trm.tx = 0;
1311
1.60M
        Trm.ty = 0;
1312
1.60M
        code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1313
1.60M
        if (code < 0)
1314
0
            goto TJ_error;
1315
1316
1.60M
        code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1317
1.60M
        if (code < 0)
1318
0
            goto TJ_error;
1319
1.60M
        ctx->pgs->textmatrix.tx += pt.x;
1320
1.60M
        ctx->pgs->textmatrix.ty += pt.y;
1321
1.60M
    }
1322
1.65M
TJ_error:
1323
1.65M
    pdfi_countdown(o);
1324
    /* Restore the CTM to the saved value */
1325
1.65M
    (void)gs_setmatrix(ctx->pgs, &saved);
1326
    /* And restore the currentpoint */
1327
1.65M
    if (initial_point_valid)
1328
1.62M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1329
30.5k
    else
1330
30.5k
        code = gs_newpath(ctx->pgs);
1331
    /* And the line width */
1332
1.65M
    ctx->pgs->line_params.half_width = linewidth;
1333
1334
1.66M
 exit:
1335
1.66M
    pdfi_countdown(a);
1336
1.66M
    return code;
1337
1.65M
}
1338
1339
static int pdfi_set_TL(pdf_context *ctx, double TL)
1340
372k
{
1341
372k
    return gs_settextleading(ctx->pgs, TL);
1342
372k
}
1343
1344
int pdfi_TL(pdf_context *ctx)
1345
11.2k
{
1346
11.2k
    int code;
1347
11.2k
    double d;
1348
1349
11.2k
    code = pdfi_destack_real(ctx, &d);
1350
11.2k
    if (code < 0)
1351
41
        return code;
1352
1353
11.2k
    return pdfi_set_TL(ctx, -d);
1354
11.2k
}
1355
1356
int pdfi_Tm(pdf_context *ctx)
1357
1.60M
{
1358
1.60M
    int code;
1359
1.60M
    float m[6];
1360
1.60M
    gs_matrix mat;
1361
1362
1.60M
    code = pdfi_destack_floats(ctx, m, 6);
1363
1.60M
    if (code < 0)
1364
165k
        return code;
1365
1366
1.43M
    if (ctx->text.BlockDepth == 0) {
1367
57.5k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1368
1369
57.5k
        gs_make_identity(&mat);
1370
57.5k
        code = gs_settextmatrix(ctx->pgs, &mat);
1371
57.5k
        if (code < 0)
1372
0
            return code;
1373
1374
57.5k
        code = gs_settextlinematrix(ctx->pgs, &mat);
1375
57.5k
        if (code < 0)
1376
0
            return code;
1377
57.5k
    }
1378
1379
1.43M
    if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1380
15.3k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1381
1382
1.43M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1383
1.43M
    if (code < 0)
1384
0
        return code;
1385
1386
1.43M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1387
1.43M
}
1388
1389
int pdfi_Tr(pdf_context *ctx)
1390
560k
{
1391
560k
    int code;
1392
560k
    int64_t mode;
1393
1394
560k
    code = pdfi_destack_int(ctx, &mode);
1395
560k
    if (code < 0)
1396
7.89k
        return code;
1397
1398
552k
    if (mode < 0 || mode > 7)
1399
5.02k
        return_error(gs_error_rangecheck);
1400
1401
    /* Detect attempts to switch from a clipping mode to a non-clipping
1402
     * mode, this is defined as invalid in the spec. (We don't warn if we haven't yet
1403
     * drawn any text in the clipping mode).
1404
     */
1405
547k
    if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1406
65
        pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1407
1408
547k
    if (ctx->device_state.preserve_tr_mode) {
1409
5.10k
        gs_settextrenderingmode(ctx->pgs, mode);
1410
5.10k
    } else
1411
542k
    {
1412
542k
        gs_point initial_point;
1413
1414
542k
        if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1415
394
            gs_settextrenderingmode(ctx->pgs, mode);
1416
            /* Capture the current position */
1417
394
            code = gs_currentpoint(ctx->pgs, &initial_point);
1418
            /* Start a new path (so our clip doesn't include any
1419
             * already extant path in the graphics state)
1420
             */
1421
394
            gs_newpath(ctx->pgs);
1422
394
            if (code >= 0)
1423
348
                gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1424
394
            code = 0;
1425
542k
        } else if (gs_currenttextrenderingmode(ctx->pgs) >= 4 && mode < 4 && ctx->text.BlockDepth != 0) {
1426
            /* If we are switching from a clipping mode to a non-clipping
1427
             * mode then behave as if we had an implicit ET to flush the
1428
             * accumulated text to a clip, then set the text rendering mode
1429
             * to the non-clip mode, and perform an implicit BT.
1430
             */
1431
67
            code = pdfi_ET(ctx);
1432
67
            if (code < 0)
1433
2
                return code;
1434
65
            gs_settextrenderingmode(ctx->pgs, mode);
1435
65
            code = pdfi_BT(ctx);
1436
65
            if (code < 0)
1437
0
                return code;
1438
65
        }
1439
542k
        else
1440
542k
            gs_settextrenderingmode(ctx->pgs, mode);
1441
542k
    }
1442
547k
    return code;
1443
547k
}
1444
1445
static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1446
3.59k
{
1447
3.59k
    return gs_settextrise(ctx->pgs, Ts);
1448
3.59k
}
1449
1450
int pdfi_Ts(pdf_context *ctx)
1451
3.99k
{
1452
3.99k
    int code;
1453
3.99k
    double d;
1454
1455
3.99k
    code = pdfi_destack_real(ctx, &d);
1456
3.99k
    if (code < 0)
1457
402
        return code;
1458
1459
3.59k
    return pdfi_set_Ts(ctx, d);
1460
3.99k
}
1461
1462
static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1463
139k
{
1464
139k
    return gs_setwordspacing(ctx->pgs, Tw);
1465
139k
}
1466
1467
int pdfi_Tw(pdf_context *ctx)
1468
140k
{
1469
140k
    int code;
1470
140k
    double d;
1471
1472
140k
    code = pdfi_destack_real(ctx, &d);
1473
140k
    if (code < 0)
1474
705
        return code;
1475
1476
139k
    return pdfi_set_Tw(ctx, d);
1477
140k
}
1478
1479
int pdfi_Tz(pdf_context *ctx)
1480
94.2k
{
1481
94.2k
    int code;
1482
94.2k
    double d;
1483
1484
94.2k
    code = pdfi_destack_real(ctx, &d);
1485
94.2k
    if (code < 0)
1486
1.72k
        return code;
1487
1488
92.4k
    return gs_settexthscaling(ctx->pgs, d);
1489
94.2k
}
1490
1491
int pdfi_singlequote(pdf_context *ctx)
1492
16.6k
{
1493
16.6k
    int code;
1494
1495
16.6k
    if (ctx->text.BlockDepth == 0) {
1496
7.90k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1497
7.90k
    }
1498
1499
16.6k
    code = pdfi_T_star(ctx);
1500
16.6k
    if (code < 0)
1501
0
        return code;
1502
1503
16.6k
    return pdfi_Tj(ctx);
1504
16.6k
}
1505
1506
int pdfi_doublequote(pdf_context *ctx)
1507
5.64k
{
1508
5.64k
    int code;
1509
5.64k
    double Tw, Tc;
1510
1511
5.64k
    if (ctx->text.BlockDepth == 0) {
1512
5.37k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1513
5.37k
    }
1514
1515
5.64k
    if (pdfi_count_stack(ctx) < 3) {
1516
5.26k
        pdfi_clearstack(ctx);
1517
5.26k
        return_error(gs_error_stackunderflow);
1518
5.26k
    }
1519
1520
383
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1521
179
        pdfi_pop(ctx, 3);
1522
179
        return gs_note_error(gs_error_typecheck);
1523
179
    }
1524
1525
204
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1526
204
    if (code < 0)
1527
2
        goto error;
1528
202
    code = pdfi_set_Tc(ctx, Tc);
1529
202
    if (code < 0)
1530
0
        goto error;
1531
1532
202
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1533
202
    if (code < 0)
1534
9
        goto error;
1535
193
    code = pdfi_set_Tw(ctx, Tw);
1536
193
    if (code < 0)
1537
0
        goto error;
1538
1539
193
    code = pdfi_T_star(ctx);
1540
193
    if (code < 0)
1541
0
        goto error;
1542
1543
193
    code = pdfi_Tj(ctx);
1544
    /* Tj pops one off the stack for us, leaving us 2 to go. */
1545
193
    pdfi_pop(ctx, 2);
1546
193
    return code;
1547
1548
11
error:
1549
11
    pdfi_pop(ctx, 3);
1550
11
    return code;
1551
193
}