Coverage Report

Created: 2026-07-24 07:44

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.78M
{
41
1.78M
    int code;
42
1.78M
    gs_matrix m;
43
1.78M
    bool illegal_BT = false;
44
45
1.78M
    if (ctx->text.BlockDepth != 0) {
46
38.8k
        illegal_BT = true;
47
38.8k
        if (ctx->text.TextClip) {
48
91
            gx_device *dev = gs_currentdevice_inline(ctx->pgs);
49
50
91
            ctx->text.TextClip = false;
51
91
            (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1);
52
91
        }
53
38.8k
        code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
54
38.8k
        if (code < 0)
55
0
            return code;
56
38.8k
    }
57
58
1.78M
    gs_make_identity(&m);
59
1.78M
    code = gs_settextmatrix(ctx->pgs, &m);
60
1.78M
    if (code < 0)
61
0
        return code;
62
63
1.78M
    code = gs_settextlinematrix(ctx->pgs, &m);
64
1.78M
    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.78M
    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
135
        gs_newpath(ctx->pgs);
75
135
    }
76
77
1.78M
    ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
78
1.78M
    if (!ctx->pgs->current_point_valid)
79
1.42M
        code = gs_moveto(ctx->pgs, 0, 0);
80
81
1.78M
    ctx->text.BlockDepth++;
82
83
1.78M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
84
429k
        gs_begin_transparency_text_group(ctx->pgs);
85
86
1.78M
    return code;
87
1.78M
}
88
89
static int do_ET(pdf_context *ctx)
90
1.79M
{
91
1.79M
    int code = 0;
92
1.79M
    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.79M
    if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
105
874
        gs_point initial_point;
106
107
874
        if  (!ctx->device_state.preserve_tr_mode) {
108
813
            ctx->text.TextClip = false;
109
            /* Capture the current position */
110
813
            code = gs_currentpoint(ctx->pgs, &initial_point);
111
813
            if (code >= 0 || code == gs_error_nocurrentpoint) {
112
811
                gs_point adjust;
113
811
                bool nocurrentpoint = code >= 0 ? false : true;
114
115
811
                gs_currentfilladjust(ctx->pgs, &adjust);
116
811
                code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
117
811
                if (code < 0)
118
0
                    return code;
119
120
811
                code = gs_clip(ctx->pgs);
121
811
                if (code >= 0)
122
811
                    copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
123
124
811
                code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
125
811
                if (code < 0)
126
0
                    return code;
127
128
811
                if (copy != NULL)
129
811
                    (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
130
131
811
                if (nocurrentpoint == false)
132
811
                    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
133
811
            }
134
813
        }
135
874
    }
136
1.79M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
137
435k
        gs_end_transparency_text_group(ctx->pgs);
138
139
1.79M
    if (!ctx->text.initial_current_point_valid)
140
931k
        gs_newpath(ctx->pgs);
141
1.79M
    return code;
142
1.79M
}
143
144
int pdfi_ET(pdf_context *ctx)
145
1.75M
{
146
1.75M
    if (ctx->text.BlockDepth == 0) {
147
66.8k
        int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
148
66.8k
        return code;
149
66.8k
    }
150
151
1.68M
    ctx->text.BlockDepth--;
152
153
1.68M
    return do_ET(ctx);
154
1.75M
}
155
156
int pdfi_T_star(pdf_context *ctx)
157
156k
{
158
156k
    int code;
159
156k
    gs_matrix m, mat;
160
161
156k
    if (ctx->text.BlockDepth == 0) {
162
32.4k
        int code;
163
32.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
32.4k
    }
166
167
156k
    gs_make_identity(&m);
168
156k
    m.ty += ctx->pgs->textleading;
169
170
156k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
171
156k
    if (code < 0)
172
0
        return code;
173
174
156k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
175
156k
    if (code < 0)
176
0
        return code;
177
178
156k
    code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
179
156k
    return code;
180
156k
}
181
182
static int pdfi_set_Tc(pdf_context *ctx, double Tc)
183
703k
{
184
703k
    return gs_settextspacing(ctx->pgs, Tc);
185
703k
}
186
187
int pdfi_Tc(pdf_context *ctx)
188
715k
{
189
715k
    int code;
190
715k
    double d;
191
192
715k
    code = pdfi_destack_real(ctx, &d);
193
715k
    if (code < 0)
194
12.6k
        return code;
195
196
703k
    return pdfi_set_Tc(ctx, d);
197
715k
}
198
199
int pdfi_Td(pdf_context *ctx)
200
1.28M
{
201
1.28M
    int code;
202
1.28M
    double Txy[2];
203
1.28M
    gs_matrix m, mat;
204
205
1.28M
    code = pdfi_destack_reals(ctx, Txy, 2);
206
1.28M
    if (code < 0)
207
21.3k
        return code;
208
209
1.26M
    gs_make_identity(&m);
210
211
1.26M
    m.tx = Txy[0];
212
1.26M
    m.ty = Txy[1];
213
214
1.26M
    if (ctx->text.BlockDepth == 0) {
215
13.6k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL);
216
217
13.6k
        gs_make_identity(&mat);
218
13.6k
        code = gs_settextmatrix(ctx->pgs, &mat);
219
13.6k
        if (code < 0)
220
0
            return code;
221
222
13.6k
        code = gs_settextlinematrix(ctx->pgs, &mat);
223
13.6k
        if (code < 0)
224
0
            return code;
225
13.6k
    }
226
227
1.26M
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
228
1.26M
    if (code < 0)
229
0
        return code;
230
231
1.26M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
232
1.26M
    if (code < 0)
233
0
        return code;
234
235
1.26M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
236
1.26M
}
237
238
int pdfi_TD(pdf_context *ctx)
239
394k
{
240
394k
    int code;
241
394k
    double Txy[2];
242
394k
    gs_matrix m, mat;
243
244
394k
    gs_make_identity(&m);
245
246
394k
    code = pdfi_destack_reals(ctx, Txy, 2);
247
394k
    if (code < 0)
248
2.69k
        return code;
249
250
391k
    m.tx = Txy[0];
251
391k
    m.ty = Txy[1];
252
253
391k
    if (ctx->text.BlockDepth == 0) {
254
1.37k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
255
256
1.37k
        gs_make_identity(&mat);
257
1.37k
        code = gs_settextmatrix(ctx->pgs, &mat);
258
1.37k
        if (code < 0)
259
0
            return code;
260
261
1.37k
        code = gs_settextlinematrix(ctx->pgs, &mat);
262
1.37k
        if (code < 0)
263
0
            return code;
264
1.37k
    }
265
266
391k
    code = pdfi_set_TL(ctx, m.ty * 1.0f);
267
391k
    if (code < 0)
268
0
        return code;
269
270
391k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
271
391k
    if (code < 0)
272
0
        return code;
273
274
391k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
275
391k
    if (code < 0)
276
0
        return code;
277
278
391k
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
279
391k
}
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.5M
{
292
13.5M
    pdf_font *current_font = NULL;
293
13.5M
    gs_matrix mat;
294
13.5M
    float *x_widths = NULL, *y_widths = NULL, width;
295
13.5M
    double Tw = 0, Tc = 0;
296
13.5M
    int i, code;
297
298
13.5M
    text->data.chars = NULL;
299
13.5M
    text->x_widths = NULL;
300
13.5M
    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.5M
    current_font = pdfi_get_current_pdf_font(ctx);
308
309
13.5M
    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.5M
    Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
317
318
13.5M
    if (current_font->pdfi_font_type == e_pdf_font_type1 ||
319
5.06M
        current_font->pdfi_font_type == e_pdf_font_cff ||
320
3.58M
        current_font->pdfi_font_type == e_pdf_font_type3 ||
321
3.54M
        current_font->pdfi_font_type == e_pdf_font_cff ||
322
3.54M
        current_font->pdfi_font_type == e_pdf_font_truetype ||
323
1.31M
        current_font->pdfi_font_type == e_pdf_font_microtype ||
324
1.31M
        current_font->pdfi_font_type == e_pdf_font_type0)
325
13.5M
    {
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.5M
        if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
330
5.12M
            text->operation = TEXT_RETURN_WIDTH;
331
5.12M
            if (Tc != 0) {
332
1.02M
                text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
333
1.02M
                if (current_font->pfont && current_font->pfont->WMode == 0) {
334
1.02M
                    text->delta_all.x = Tc;
335
1.02M
                    text->delta_all.y = 0;
336
1.02M
                } else {
337
1
                    text->delta_all.y = Tc;
338
1
                    text->delta_all.x = 0;
339
1
                }
340
1.02M
            }
341
8.45M
        } else {
342
8.45M
            gs_point pt;
343
344
8.45M
            x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
345
8.45M
            y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
346
8.45M
            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.45M
            memset(x_widths, 0x00, s->length * sizeof(float));
352
8.45M
            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.45M
            mat = current_font->pfont->FontMatrix;
358
359
35.0M
            for (i = 0;i < s->length; i++) {
360
                /* Get the width (in unscaled text units) */
361
26.5M
                if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
362
92.2k
                    width = current_font->MissingWidth;
363
26.4M
                else
364
26.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
26.5M
                gs_distance_transform(width, 0, &mat, &pt);
367
26.5M
                x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
368
                /* Add any Tc value */
369
26.5M
                x_widths[i] += Tc;
370
26.5M
            }
371
8.45M
            text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
372
8.45M
            text->x_widths = x_widths;
373
8.45M
            text->y_widths = y_widths;
374
8.45M
            text->widths_size = s->length * 2;
375
8.45M
        }
376
377
13.5M
        Tw = gs_currentwordspacing(ctx->pgs);
378
13.5M
        if (Tw != 0) {
379
874k
            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
874k
            if (current_font->pfont && current_font->pfont->WMode == 0) {
385
874k
                text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
386
874k
                text->delta_space.y = 0;
387
874k
            } else {
388
0
                text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
389
0
                text->delta_space.x = 0;
390
0
            }
391
874k
            text->space.s_char = 0x20;
392
874k
        }
393
394
13.5M
        if (current_font->pdfi_font_type == e_pdf_font_type3) {
395
47.4k
            text->operation |= TEXT_FROM_CHARS;
396
47.4k
            text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
397
47.4k
            if (!text->data.chars) {
398
0
                code = gs_note_error(gs_error_VMerror);
399
0
                goto text_params_error;
400
0
            }
401
402
236k
            for (i = 0; i < s->length; i++) {
403
188k
                ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
404
188k
            }
405
47.4k
        }
406
13.5M
        else {
407
13.5M
            text->operation |= TEXT_FROM_BYTES;
408
13.5M
            text->data.bytes = (const byte *)s->data;
409
13.5M
        }
410
13.5M
        text->size = s->length;
411
13.5M
    }
412
46
    else {
413
46
        code = gs_note_error(gs_error_invalidfont);
414
46
        goto text_params_error;
415
46
    }
416
417
13.5M
    return 0;
418
419
46
text_params_error:
420
46
    gs_free_object(ctx->memory, x_widths, "X widths array for text");
421
46
    text->x_widths = NULL;
422
46
    gs_free_object(ctx->memory, y_widths, "Y widths array for text");
423
46
    text->y_widths = NULL;
424
46
    gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars");
425
46
    text->data.chars = NULL;
426
46
    return code;
427
13.5M
}
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
13.4M
{
436
13.4M
    int code = 0;
437
13.4M
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
438
439
13.4M
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
440
13.4M
    if (code >= 0) {
441
13.4M
        penum->single_byte_space = true;
442
13.4M
        saved_penum = ctx->text.current_enum;
443
13.4M
        ctx->text.current_enum = penum;
444
13.4M
        code = gs_text_process(penum);
445
13.4M
        gs_text_release(ctx->pgs, penum, "pdfi_Tj");
446
13.4M
        ctx->text.current_enum = saved_penum;
447
13.4M
    }
448
13.4M
    return code;
449
13.4M
}
450
451
/* Mode 0 - fill */
452
static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
453
9.60M
{
454
9.60M
    int code;
455
456
    /* just draw the text */
457
9.60M
    text->operation |= TEXT_DO_DRAW;
458
459
9.60M
    code = pdfi_show_simple(ctx, text);
460
461
9.60M
    text->operation &= ~TEXT_DO_DRAW;
462
9.60M
    return code;
463
9.60M
}
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
7.60k
{
553
7.60k
    int code, restart = 0;
554
7.60k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
555
7.60k
    gs_point end_point, initial_point;
556
7.60k
    gx_device *dev = ctx->pgs->device;
557
7.60k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
558
559
7.60k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
560
561
    /* Capture the current position */
562
7.60k
    code = gs_currentpoint(ctx->pgs, &initial_point);
563
7.60k
    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.60k
    code = pdfi_gsave(ctx);
571
7.60k
    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.60k
    code = gs_newpath(ctx->pgs);
578
7.60k
    if (code < 0)
579
0
        goto Tr1_error;
580
7.60k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
581
7.60k
    if (code < 0)
582
0
        goto Tr1_error;
583
584
    /* Don't draw the text, create a path suitable for stroking */
585
7.60k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
586
587
    /* Run the text methods to create the path */
588
7.60k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
589
7.60k
    if (code < 0)
590
0
        goto Tr1_error;
591
592
7.60k
    penum->single_byte_space = true;
593
7.60k
    saved_penum = ctx->text.current_enum;
594
7.60k
    ctx->text.current_enum = penum;
595
7.60k
    code = gs_text_process(penum);
596
7.60k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
597
7.60k
    ctx->text.current_enum = saved_penum;
598
7.60k
    if (code < 0)
599
31
        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.56k
    code = gs_currentpoint(ctx->pgs, &end_point);
607
7.56k
    if (code < 0)
608
0
        goto Tr1_error;
609
7.56k
    if (talphabits != galphabits)
610
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
611
7.56k
    code = gs_fillstroke(ctx->pgs, &restart);
612
7.56k
    if (talphabits != galphabits)
613
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
614
615
7.60k
Tr1_error:
616
    /* And grestore back to where we started */
617
7.60k
    (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.60k
    if (code >= 0)
621
7.56k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
622
623
7.60k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
624
7.60k
    return code;
625
7.56k
}
626
627
static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
628
117k
{
629
117k
    int code;
630
117k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
631
632
    /* Don't draw the text */
633
117k
    text->operation |= TEXT_DO_NONE | TEXT_RENDER_MODE_3;
634
635
    /* Run the text methods to create the path */
636
117k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
637
117k
    if (code < 0)
638
0
        return code;
639
640
117k
    penum->single_byte_space = true;
641
117k
    saved_penum = ctx->text.current_enum;
642
117k
    ctx->text.current_enum = penum;
643
117k
    code = gs_text_process(penum);
644
117k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
645
117k
    ctx->text.current_enum = saved_penum;
646
647
117k
    return code;
648
117k
}
649
650
/* Prototype the basic 'clip' function, as the following routines will all use it */
651
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text);
652
653
static int pdfi_show_Tr_4(pdf_context *ctx, gs_text_params_t *text)
654
2.32k
{
655
2.32k
    int code;
656
2.32k
    gs_point initial_point;
657
658
    /* Capture the current position */
659
2.32k
    code = gs_currentpoint(ctx->pgs, &initial_point);
660
2.32k
    if (code < 0)
661
0
        return code;
662
663
2.32k
    ctx->text.TextClip = true;
664
    /* First fill the text */
665
2.32k
    code = pdfi_show_Tr_0(ctx, text);
666
2.32k
    if (code < 0)
667
0
        return code;
668
669
    /* Return the current point to the initial point */
670
2.32k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
671
2.32k
    if (code >= 0)
672
        /* And add the text to the acumulated path for clipping */
673
2.32k
        code = pdfi_show_Tr_7(ctx, text);
674
675
2.32k
    return code;
676
2.32k
}
677
678
static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
679
275
{
680
275
    int code;
681
275
    gs_point initial_point;
682
683
    /* Capture the current position */
684
275
    code = gs_currentpoint(ctx->pgs, &initial_point);
685
275
    if (code < 0)
686
0
        return code;
687
688
275
    ctx->text.TextClip = true;
689
    /* First stroke the text */
690
275
    code = pdfi_show_Tr_1(ctx, text);
691
275
    if (code < 0)
692
0
        return code;
693
694
    /* Return the current point to the initial point */
695
275
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
696
275
    if (code >= 0)
697
        /* And add the text to the acumulated path for clipping */
698
275
        code = pdfi_show_Tr_7(ctx, text);
699
700
275
    return code;
701
275
}
702
703
static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
704
314
{
705
314
    int code;
706
314
    gs_point initial_point;
707
708
    /* Capture the current position */
709
314
    code = gs_currentpoint(ctx->pgs, &initial_point);
710
314
    if (code < 0)
711
0
        return code;
712
713
314
    ctx->text.TextClip = true;
714
    /* First fill and stroke the text */
715
314
    code = pdfi_show_Tr_2(ctx, text);
716
314
    if (code < 0)
717
0
        return code;
718
719
    /* Return the current point to the initial point */
720
314
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
721
314
    if (code >= 0)
722
        /* And add the text to the acumulated path for clipping */
723
314
        code = pdfi_show_Tr_7(ctx, text);
724
725
314
    return code;
726
314
}
727
728
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
729
3.03k
{
730
3.03k
    int code;
731
3.03k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
732
733
3.03k
    ctx->text.TextClip = true;
734
    /* Don't draw the text, create a path suitable for filling */
735
3.03k
    text->operation |= TEXT_DO_TRUE_CHARPATH;
736
737
    /* Run the text methods to create the path */
738
3.03k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
739
3.03k
    if (code < 0)
740
0
        goto Tr7_error;
741
742
3.03k
    penum->single_byte_space = true;
743
3.03k
    saved_penum = ctx->text.current_enum;
744
3.03k
    ctx->text.current_enum = penum;
745
3.03k
    code = gs_text_process(penum);
746
3.03k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
747
3.03k
    ctx->text.current_enum = saved_penum;
748
749
3.03k
Tr7_error:
750
3.03k
    text->operation &= ~TEXT_DO_TRUE_CHARPATH;
751
3.03k
    return code;
752
3.03k
}
753
754
static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
755
3.85M
{
756
3.85M
    int Trmode = 0, code;
757
3.85M
    pdf_font *current_font = NULL;
758
3.85M
    gs_point initial_point;
759
760
3.85M
    current_font = pdfi_get_current_pdf_font(ctx);
761
3.85M
    if (current_font == NULL)
762
0
        return_error(gs_error_invalidfont);
763
764
    /* Capture the current position, in case we need it for clipping */
765
3.85M
    code = gs_currentpoint(ctx->pgs, &initial_point);
766
767
3.85M
    Trmode = gs_currenttextrenderingmode(ctx->pgs);
768
3.85M
    if (Trmode == 3) {
769
9.53k
        if (current_font->pdfi_font_type == e_pdf_font_type3)
770
1
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
771
9.53k
        else
772
9.53k
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
773
9.53k
    }
774
3.84M
    else
775
3.84M
        text->operation |= TEXT_RETURN_WIDTH | TEXT_DO_DRAW;
776
777
    /* If we're preserving the text rendering mode, then we don't run a separate
778
     * stroke operation, we do effectively run a fill. The fill setup loads the
779
     * fill device colour whch, for patterns, creates the pattern tile.
780
     * But, because we never load the stroke colour into a device colour, the
781
     * pattern tile never gets loaded, so we get an error trying to draw the
782
     * text.
783
     * We could load the stroke colour in gs_text_begin, but that's potentially
784
     * wasteful given that most of the time we won't be using the stroke colour
785
     * os I've chosen to load the stroke device colour explicitly here.
786
     * But, obviously, only when preserving a stroking text rendering mode.
787
     */
788
3.85M
    if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) {
789
891
        gs_swapcolors_quick(ctx->pgs);
790
791
891
        code = gx_set_dev_color(ctx->pgs);
792
891
        if (code != 0)
793
0
            return code;
794
795
891
        code = gs_gstate_color_load(ctx->pgs);
796
891
        if (code < 0)
797
0
            return code;
798
799
891
        gs_swapcolors_quick(ctx->pgs);
800
891
    }
801
802
    /* If we've switched to aemitting text with a 'clip' rendering mode then we
803
     * tell pdfwrite that here, so that it can emit a 'q', which it can later
804
     * (see pdfi_op_Q) restore to with a Q. It's the only way to get the clipping
805
     * right.
806
     */
807
3.85M
    if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) {
808
30
        gx_device *dev = gs_currentdevice_inline(ctx->pgs);
809
810
30
        ctx->text.TextClip = true;
811
30
        code = dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1);
812
30
        if (code < 0 && code != gs_error_undefined)
813
0
            return code;
814
30
  }
815
816
3.85M
    code = pdfi_show_simple(ctx, text);
817
3.85M
    return code;
818
3.85M
}
819
820
static int pdfi_show(pdf_context *ctx, pdf_string *s)
821
13.5M
{
822
13.5M
    int code = 0, SavedTextDepth = 0;
823
13.5M
    int code1 = 0;
824
13.5M
    gs_text_params_t text;
825
13.5M
    pdf_font *current_font = NULL;
826
13.5M
    int Trmode = 0;
827
13.5M
    int initial_gsave_level = ctx->pgs->level;
828
13.5M
    pdfi_trans_state_t state;
829
13.5M
    int outside_text_block = 0;
830
831
13.5M
    if (ctx->text.BlockDepth == 0) {
832
111k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
833
111k
        outside_text_block = 1;
834
111k
    }
835
836
13.5M
    if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
837
13.5M
     || hypot(ctx->pgs->ctm.yy, ctx->pgs->ctm.yx) == 0.0) {
838
        /* This can mean a font scaled to 0, which appears to be valid, or a degenerate
839
         * ctm/text matrix, which isn't. A degenrate matrix will have triggered a warning
840
         * before, so we just skip the operation here.
841
         */
842
11.7k
         return 0;
843
11.7k
    }
844
845
13.5M
    current_font = pdfi_get_current_pdf_font(ctx);
846
13.5M
    if (current_font == NULL)
847
14
        return_error(gs_error_invalidfont);
848
849
13.5M
    code = pdfi_show_set_params(ctx, s, &text);
850
13.5M
    if (code < 0)
851
46
        goto show_error;
852
853
    /* <sigh> Ugliness......
854
     * It seems that transparency can require that we set a transparency Group
855
     * during the course of a text operation, and that Group can, of course,
856
     * execute operations which are barred during text operations (because
857
     * the Group doesn't affect the text as such). So we need to not throw
858
     * errors if that happens. Do that by just setting the BlockDepth to 0.
859
     */
860
13.5M
    SavedTextDepth = ctx->text.BlockDepth;
861
13.5M
    ctx->text.BlockDepth = 0;
862
13.5M
    code = pdfi_trans_setup_text(ctx, &state, true);
863
13.5M
    ctx->text.BlockDepth = SavedTextDepth;
864
865
13.5M
    if (code >= 0) {
866
13.5M
        if (ctx->device_state.preserve_tr_mode) {
867
3.85M
        code = pdfi_show_Tr_preserve(ctx, &text);
868
9.72M
        } else {
869
9.72M
            Trmode = gs_currenttextrenderingmode(ctx->pgs);
870
871
            /* The spec says that we ignore text rendering modes other than 0 for
872
             * type 3 fonts, but Acrobat also honours mode 3 (do nothing)
873
             */
874
9.72M
            if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
875
234
                Trmode = 0;
876
877
9.72M
            switch(Trmode) {
878
9.59M
            case 0:
879
9.59M
                code = pdfi_show_Tr_0(ctx, &text);
880
9.59M
                break;
881
1.14k
            case 1:
882
1.14k
                code = pdfi_show_Tr_1(ctx, &text);
883
1.14k
                break;
884
7.28k
            case 2:
885
7.28k
                code = pdfi_show_Tr_2(ctx, &text);
886
7.28k
                break;
887
117k
            case 3:
888
117k
                code = pdfi_show_Tr_3(ctx, &text);
889
117k
                break;
890
2.32k
            case 4:
891
2.32k
                code = pdfi_show_Tr_4(ctx, &text);
892
2.32k
                break;
893
275
            case 5:
894
275
                code = pdfi_show_Tr_5(ctx, &text);
895
275
                break;
896
314
            case 6:
897
314
                code = pdfi_show_Tr_6(ctx, &text);
898
314
                break;
899
125
            case 7:
900
125
                code = pdfi_show_Tr_7(ctx, &text);
901
125
                break;
902
0
            default:
903
0
                break;
904
9.72M
            }
905
9.72M
        }
906
13.5M
        code1 = pdfi_trans_teardown_text(ctx, &state);
907
13.5M
        if (code == 0)
908
13.5M
            code = code1;
909
13.5M
    }
910
911
    /* We shouldn't need to do this, but..... It turns out that if we have text rendering mode 3 set
912
     * then gs_text_begin will execute a gsave, push the nulldevice and alter the saved gsave level.
913
     * If we then get an error while processing the text we don't gs_restore enough times which
914
     * leaves the nulldevice as the current device, and eats all the following content!
915
     * To avoid that, we save the gsave depth at the start of this routine, and grestore enough
916
     * times to get back to the same level. I think this is actually a bug in the graphics library
917
     * but it doesn't get exposed in normal usage so we'll just work around it here.
918
     */
919
13.5M
    while(ctx->pgs->level > initial_gsave_level)
920
388
        gs_grestore(ctx->pgs);
921
922
    /* If we were outside a text block when we started this function, then we effectively started
923
     * one by calling the show mechanism. Among other things, this can have pushed a transparency
924
     * group. We need to ensure that this is properly closed, so do the guts of the 'ET' operator
925
     * here (without adjusting the blockDepth, or giving more warnings). See Bug 707753. */
926
13.5M
    if (outside_text_block) {
927
105k
        code1 = do_ET(ctx);
928
105k
        if (code == 0)
929
100k
            code = code1;
930
105k
    }
931
932
13.5M
show_error:
933
13.5M
    if ((void *)text.data.chars != (void *)s->data)
934
47.5k
        gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
935
936
13.5M
    gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
937
13.5M
    gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
938
13.5M
    return code;
939
13.5M
}
940
941
/* NOTE: the bounding box this generates has llx and lly at 0,0,
942
   so is arguably not a "real" bounding box
943
 */
944
int pdfi_string_bbox(pdf_context *ctx, pdf_string *s, gs_rect *bboxout, gs_point *advance_width, bool for_stroke)
945
20.6k
{
946
20.6k
    int code = 0;
947
20.6k
    gx_device_bbox *bbdev;
948
20.6k
    pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
949
20.6k
    gs_matrix tmpmat, Trm, Trm_ctm;
950
20.6k
    gs_point cppt, startpt;
951
952
20.6k
    if (current_font == NULL)
953
0
        return_error(gs_error_invalidfont);
954
955
956
20.6k
    for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
957
958
20.6k
    if (ctx->devbbox == NULL) {
959
197
        bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
960
197
        if (bbdev == NULL)
961
0
           return_error(gs_error_VMerror);
962
197
        gx_device_bbox_init(bbdev, NULL, ctx->memory);
963
197
        ctx->devbbox = (gx_device *)bbdev;
964
197
        rc_increment(ctx->devbbox);
965
197
    }
966
20.4k
    else {
967
20.4k
        bbdev = (gx_device_bbox *)ctx->devbbox;
968
20.4k
    }
969
20.6k
    gx_device_retain((gx_device *)bbdev, true);
970
20.6k
    gx_device_bbox_set_white_opaque(bbdev, true);
971
972
20.6k
    code = pdfi_gsave(ctx);
973
20.6k
    if (code < 0) {
974
0
        gx_device_retain((gx_device *)bbdev, false);
975
0
        return code;
976
0
    }
977
    /* The bbox device needs a fairly high resolution to get accurate results */
978
20.6k
    gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
979
980
20.6k
    code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
981
20.6k
    if (code < 0)
982
0
        goto out;
983
984
20.6k
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
985
20.6k
    Trm.xy = 0;
986
20.6k
    Trm.yx = 0;
987
20.6k
    Trm.yy = ctx->pgs->PDFfontsize;
988
20.6k
    Trm.tx = 0;
989
20.6k
    Trm.ty = ctx->pgs->textrise;
990
991
20.6k
    memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
992
    /* We want to avoid any translations unrelated to the extents of the text */
993
20.6k
    tmpmat.tx = tmpmat.ty = 0;
994
20.6k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm);
995
996
20.6k
    memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
997
    /* As above */
998
20.6k
    tmpmat.tx = tmpmat.ty = 0;
999
20.6k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
1000
20.6k
    gs_setmatrix(ctx->pgs, &Trm_ctm);
1001
1002
20.6k
    gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
1003
1004
20.6k
    code = pdfi_gs_setgray(ctx, 1.0);
1005
20.6k
    if (code < 0)
1006
0
        goto out;
1007
1008
    /* The bbox device (not surprisingly) clips to the device width/height
1009
       so we have to offset our initial point to cope with glyphs that include
1010
       negative coordinates. Most significantly, descender features - hence the
1011
       larger y offset.
1012
       The offsets are guesses.
1013
     */
1014
20.6k
    startpt.x = ctx->pgs->PDFfontsize;
1015
20.6k
    startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1016
20.6k
    code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1017
20.6k
    if (code < 0)
1018
0
        goto out;
1019
1020
    /* Pretend to have at least one BT or pdfi_show will generate a spurious warning */
1021
20.6k
    ctx->text.BlockDepth++;
1022
20.6k
    code = pdfi_show(ctx, s);
1023
    /* And an ET */
1024
20.6k
    ctx->text.BlockDepth--;
1025
20.6k
    if (code < 0)
1026
0
        goto out;
1027
1028
20.6k
    code = gx_device_bbox_bbox(bbdev, bboxout);
1029
20.6k
    if (code < 0)
1030
0
        goto out;
1031
1032
20.6k
    bboxout->q.x -= bboxout->p.x;
1033
20.6k
    bboxout->q.y -= bboxout->p.y;
1034
20.6k
    bboxout->p.x = bboxout->p.y = 0;
1035
1036
20.6k
    code = gs_currentpoint(ctx->pgs, &cppt);
1037
20.6k
    if (code >= 0) {
1038
20.6k
        code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1039
20.6k
        if (code < 0)
1040
0
            goto out;
1041
20.6k
        advance_width->x = ctx->pgs->current_point.x - startpt.x;
1042
20.6k
        advance_width->y = ctx->pgs->current_point.y - startpt.y;
1043
20.6k
        code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1044
20.6k
    }
1045
20.6k
out:
1046
20.6k
    pdfi_grestore(ctx);
1047
20.6k
    (void)gs_closedevice((gx_device *)bbdev);
1048
20.6k
    gx_device_retain((gx_device *)bbdev, false);
1049
1050
20.6k
    return code;
1051
20.6k
}
1052
1053
int pdfi_Tj(pdf_context *ctx)
1054
1.67M
{
1055
1.67M
    int code = 0;
1056
1.67M
    pdf_string *s = NULL;
1057
1.67M
    gs_matrix saved, Trm;
1058
1.67M
    gs_point initial_point, current_point, pt;
1059
1.67M
    double linewidth = ctx->pgs->line_params.half_width;
1060
1.67M
    int initial_point_valid;
1061
1062
1.67M
    if (pdfi_count_stack(ctx) < 1)
1063
30.8k
        return_error(gs_error_stackunderflow);
1064
1065
1.64M
    if (pdfi_oc_is_off(ctx))
1066
1.65k
        goto exit;
1067
1068
1.64M
    s = (pdf_string *)ctx->stack_top[-1];
1069
1.64M
    if (pdfi_type_of(s) != PDF_STRING) {
1070
47.9k
        pdfi_pop(ctx, 1);
1071
47.9k
        return_error(gs_error_typecheck);
1072
47.9k
    }
1073
1074
    /* We can't rely on the stack reference because an error during
1075
       the text operation (i.e. retrieving objects for glyph metrics
1076
       may cause the stack to be cleared.
1077
     */
1078
1.59M
    pdfi_countup(s);
1079
1.59M
    pdfi_pop(ctx, 1);
1080
1081
    /* Save the CTM for later restoration */
1082
1.59M
    saved = ctm_only(ctx->pgs);
1083
1.59M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1084
1085
1.59M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1086
1.59M
    Trm.xy = 0;
1087
1.59M
    Trm.yx = 0;
1088
1.59M
    Trm.yy = ctx->pgs->PDFfontsize;
1089
1.59M
    Trm.tx = 0;
1090
1.59M
    Trm.ty = ctx->pgs->textrise;
1091
1092
1.59M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1093
1.59M
    if (code < 0)
1094
0
        goto exit;
1095
1096
1.59M
    if (!ctx->device_state.preserve_tr_mode) {
1097
1.38M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1098
1.38M
        if (code < 0)
1099
52.0k
            goto exit;
1100
1.33M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1101
1.33M
    } else {
1102
        /* We have to adjust the stroke width for pdfwrite so that we take into
1103
         * account the CTM, but we do not spply the font scaling. Because of
1104
         * the disconnect between pdfwrite and the interpreter, we also have to
1105
         * remove the scaling due to the resolution.
1106
         */
1107
208k
        gs_matrix devmatrix, matrix;
1108
208k
        gx_device *device = gs_currentdevice(ctx->pgs);
1109
1110
208k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1111
208k
        devmatrix.xy = 0;
1112
208k
        devmatrix.yx = 0;
1113
208k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1114
208k
        devmatrix.tx = 0;
1115
208k
        devmatrix.ty = 0;
1116
1117
208k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1118
208k
        if (code < 0)
1119
0
            goto exit;
1120
1121
208k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1122
208k
        if (code < 0)
1123
0
            goto exit;
1124
208k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1125
208k
    }
1126
1127
1.54M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1128
1.54M
    if (code < 0)
1129
0
        goto exit;
1130
1131
1.54M
    code = gs_setmatrix(ctx->pgs, &Trm);
1132
1.54M
    if (code < 0)
1133
0
        goto exit;
1134
1135
1.54M
    code = gs_moveto(ctx->pgs, 0, 0);
1136
1.54M
    if (code < 0)
1137
0
        goto Tj_error;
1138
1139
1.54M
    code = pdfi_show(ctx, s);
1140
1.54M
    if (code < 0)
1141
44.9k
        goto Tj_error;
1142
1143
1.50M
    ctx->pgs->line_params.half_width = linewidth;
1144
    /* Update the Text matrix with the current point, for the next operation
1145
     */
1146
1.50M
    (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1147
1.50M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1148
1.50M
    Trm.xy = 0;
1149
1.50M
    Trm.yx = 0;
1150
1.50M
    Trm.yy = ctx->pgs->PDFfontsize;
1151
1.50M
    Trm.tx = 0;
1152
1.50M
    Trm.ty = 0;
1153
1.50M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1154
1.50M
    if (code < 0)
1155
0
        goto Tj_error;
1156
1157
1.50M
    code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1158
1.50M
    if (code < 0)
1159
0
        goto Tj_error;
1160
1.50M
    ctx->pgs->textmatrix.tx += pt.x;
1161
1.50M
    ctx->pgs->textmatrix.ty += pt.y;
1162
1163
1.54M
Tj_error:
1164
    /* Restore the CTM to the saved value */
1165
1.54M
    (void)gs_setmatrix(ctx->pgs, &saved);
1166
    /* And restore the currentpoint */
1167
1.54M
    if (initial_point_valid)
1168
1.47M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1169
66.7k
    else
1170
66.7k
        code = gs_newpath(ctx->pgs);
1171
    /* And the line width */
1172
1.54M
    ctx->pgs->line_params.half_width = linewidth;
1173
1174
1.60M
 exit:
1175
1.60M
    pdfi_countdown(s);
1176
1.60M
    return code;
1177
1.54M
}
1178
1179
int pdfi_TJ(pdf_context *ctx)
1180
1.82M
{
1181
1.82M
    int code = 0, i;
1182
1.82M
    pdf_array *a = NULL;
1183
1.82M
    pdf_obj *o = NULL;
1184
1.82M
    double dx = 0;
1185
1.82M
    gs_point pt;
1186
1.82M
    gs_matrix saved, Trm;
1187
1.82M
    gs_point initial_point, current_point;
1188
1.82M
    double linewidth = ctx->pgs->line_params.half_width;
1189
1.82M
    pdf_font *current_font = NULL;
1190
1.82M
    int initial_point_valid;
1191
1192
1.82M
    current_font = pdfi_get_current_pdf_font(ctx);
1193
1.82M
    if (current_font == NULL)
1194
5.37k
        return_error(gs_error_invalidfont);
1195
1196
1.81M
    if (ctx->text.BlockDepth == 0) {
1197
62.4k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1198
62.4k
    }
1199
1200
1.81M
    if (pdfi_count_stack(ctx) < 1)
1201
14.3k
        return_error(gs_error_stackunderflow);
1202
1203
1.80M
    if (pdfi_oc_is_off(ctx))
1204
454
        goto exit;
1205
1206
1.80M
    a = (pdf_array *)ctx->stack_top[-1];
1207
1.80M
    if (pdfi_type_of(a) != PDF_ARRAY) {
1208
45.6k
        pdfi_pop(ctx, 1);
1209
45.6k
        return gs_note_error(gs_error_typecheck);
1210
45.6k
    }
1211
1.75M
    pdfi_countup(a);
1212
1.75M
    pdfi_pop(ctx, 1);
1213
1214
    /* Save the CTM for later restoration */
1215
1.75M
    saved = ctm_only(ctx->pgs);
1216
1.75M
    ctx->text.initial_current_point_valid = initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1217
1218
    /* Calculate the text rendering matrix, see section 1.7 PDF Reference
1219
     * page 409, section 5.3.3 Text Space details.
1220
     */
1221
1.75M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1222
1.75M
    Trm.xy = 0;
1223
1.75M
    Trm.yx = 0;
1224
1.75M
    Trm.yy = ctx->pgs->PDFfontsize;
1225
1.75M
    Trm.tx = 0;
1226
1.75M
    Trm.ty = ctx->pgs->textrise;
1227
1228
1.75M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1229
1.75M
    if (code < 0)
1230
0
        goto exit;
1231
1232
1.75M
    if (!ctx->device_state.preserve_tr_mode) {
1233
1.43M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1234
1.43M
        if (code < 0)
1235
11.2k
            goto exit;
1236
1.42M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1237
1.42M
    } else {
1238
        /* We have to adjust the stroke width for pdfwrite so that we take into
1239
         * account the CTM, but we do not spply the font scaling. Because of
1240
         * the disconnect between pdfwrite and the interpreter, we also have to
1241
         * remove the scaling due to the resolution.
1242
         */
1243
326k
        gs_matrix devmatrix, matrix;
1244
326k
        gx_device *device = gs_currentdevice(ctx->pgs);
1245
1246
326k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1247
326k
        devmatrix.xy = 0;
1248
326k
        devmatrix.yx = 0;
1249
326k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1250
326k
        devmatrix.tx = 0;
1251
326k
        devmatrix.ty = 0;
1252
1253
326k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1254
326k
        if (code < 0)
1255
0
            goto exit;
1256
1257
326k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1258
326k
        if (code < 0)
1259
0
            goto exit;
1260
326k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1261
326k
    }
1262
1263
1.74M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1264
1.74M
    if (code < 0)
1265
0
        goto exit;
1266
1.74M
    code = gs_setmatrix(ctx->pgs, &Trm);
1267
1.74M
    if (code < 0)
1268
0
        goto TJ_error;
1269
1270
1.74M
    code = gs_moveto(ctx->pgs, 0, 0);
1271
1.74M
    if (code < 0)
1272
0
        goto TJ_error;
1273
1274
24.0M
    for (i = 0; i < pdfi_array_size(a); i++) {
1275
22.3M
        code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1276
22.3M
        if (code < 0)
1277
1
            goto TJ_error;
1278
1279
22.3M
        if (pdfi_type_of(o) == PDF_STRING)
1280
12.0M
            code = pdfi_show(ctx, (pdf_string *)o);
1281
10.3M
        else {
1282
10.3M
            code = pdfi_obj_to_real(ctx, o, &dx);
1283
10.3M
            if (code < 0)
1284
85
                goto TJ_error;
1285
10.3M
            dx /= -1000;
1286
10.3M
            if (current_font->pfont && current_font->pfont->WMode == 0)
1287
10.3M
                code = gs_rmoveto(ctx->pgs, dx, 0);
1288
5.81k
            else
1289
5.81k
                code = gs_rmoveto(ctx->pgs, 0, dx);
1290
10.3M
        }
1291
22.3M
        pdfi_countdown(o);
1292
22.3M
        o = NULL;
1293
22.3M
        if (code < 0)
1294
42.4k
            goto TJ_error;
1295
22.3M
    }
1296
1297
    /* We don't want to update the Text matrix if we aren't in a text block, there
1298
     * is no point because the matrix isn't persistent between text blocks.
1299
     */
1300
1.70M
    if (ctx->text.BlockDepth > 0) {
1301
        /* Update the Text matrix with the current point, for the next operation
1302
         */
1303
1.68M
        (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1304
1.68M
        Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1305
1.68M
        Trm.xy = 0;
1306
1.68M
        Trm.yx = 0;
1307
1.68M
        Trm.yy = ctx->pgs->PDFfontsize;
1308
1.68M
        Trm.tx = 0;
1309
1.68M
        Trm.ty = 0;
1310
1.68M
        code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1311
1.68M
        if (code < 0)
1312
0
            goto TJ_error;
1313
1314
1.68M
        code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1315
1.68M
        if (code < 0)
1316
0
            goto TJ_error;
1317
1.68M
        ctx->pgs->textmatrix.tx += pt.x;
1318
1.68M
        ctx->pgs->textmatrix.ty += pt.y;
1319
1.68M
    }
1320
1.74M
TJ_error:
1321
1.74M
    pdfi_countdown(o);
1322
    /* Restore the CTM to the saved value */
1323
1.74M
    (void)gs_setmatrix(ctx->pgs, &saved);
1324
    /* And restore the currentpoint */
1325
1.74M
    if (initial_point_valid)
1326
1.71M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1327
31.1k
    else
1328
31.1k
        code = gs_newpath(ctx->pgs);
1329
    /* And the line width */
1330
1.74M
    ctx->pgs->line_params.half_width = linewidth;
1331
1332
1.75M
 exit:
1333
1.75M
    pdfi_countdown(a);
1334
1.75M
    return code;
1335
1.74M
}
1336
1337
static int pdfi_set_TL(pdf_context *ctx, double TL)
1338
403k
{
1339
403k
    return gs_settextleading(ctx->pgs, TL);
1340
403k
}
1341
1342
int pdfi_TL(pdf_context *ctx)
1343
12.1k
{
1344
12.1k
    int code;
1345
12.1k
    double d;
1346
1347
12.1k
    code = pdfi_destack_real(ctx, &d);
1348
12.1k
    if (code < 0)
1349
43
        return code;
1350
1351
12.0k
    return pdfi_set_TL(ctx, -d);
1352
12.1k
}
1353
1354
int pdfi_Tm(pdf_context *ctx)
1355
1.69M
{
1356
1.69M
    int code;
1357
1.69M
    float m[6];
1358
1.69M
    gs_matrix mat;
1359
1360
1.69M
    code = pdfi_destack_floats(ctx, m, 6);
1361
1.69M
    if (code < 0)
1362
173k
        return code;
1363
1364
1.52M
    if (ctx->text.BlockDepth == 0) {
1365
58.3k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1366
1367
58.3k
        gs_make_identity(&mat);
1368
58.3k
        code = gs_settextmatrix(ctx->pgs, &mat);
1369
58.3k
        if (code < 0)
1370
0
            return code;
1371
1372
58.3k
        code = gs_settextlinematrix(ctx->pgs, &mat);
1373
58.3k
        if (code < 0)
1374
0
            return code;
1375
58.3k
    }
1376
1377
1.52M
    if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1378
16.6k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1379
1380
1.52M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1381
1.52M
    if (code < 0)
1382
0
        return code;
1383
1384
1.52M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1385
1.52M
}
1386
1387
int pdfi_Tr(pdf_context *ctx)
1388
581k
{
1389
581k
    int code;
1390
581k
    int64_t mode;
1391
1392
581k
    code = pdfi_destack_int(ctx, &mode);
1393
581k
    if (code < 0)
1394
7.88k
        return code;
1395
1396
573k
    if (mode < 0 || mode > 7)
1397
4.61k
        return_error(gs_error_rangecheck);
1398
1399
    /* Detect attempts to switch from a clipping mode to a non-clipping
1400
     * mode, this is defined as invalid in the spec. (We don't warn if we haven't yet
1401
     * drawn any text in the clipping mode).
1402
     */
1403
569k
    if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1404
65
        pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1405
1406
569k
    if (ctx->device_state.preserve_tr_mode) {
1407
46.8k
        gs_settextrenderingmode(ctx->pgs, mode);
1408
46.8k
    } else
1409
522k
    {
1410
522k
        gs_point initial_point;
1411
1412
522k
        if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1413
407
            gs_settextrenderingmode(ctx->pgs, mode);
1414
            /* Capture the current position */
1415
407
            code = gs_currentpoint(ctx->pgs, &initial_point);
1416
            /* Start a new path (so our clip doesn't include any
1417
             * already extant path in the graphics state)
1418
             */
1419
407
            gs_newpath(ctx->pgs);
1420
407
            if (code >= 0)
1421
361
                gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1422
407
            code = 0;
1423
521k
        } else if (gs_currenttextrenderingmode(ctx->pgs) >= 4 && mode < 4 && ctx->text.BlockDepth != 0) {
1424
            /* If we are switching from a clipping mode to a non-clipping
1425
             * mode then behave as if we had an implicit ET to flush the
1426
             * accumulated text to a clip, then set the text rendering mode
1427
             * to the non-clip mode, and perform an implicit BT.
1428
             */
1429
67
            code = pdfi_ET(ctx);
1430
67
            if (code < 0)
1431
2
                return code;
1432
65
            gs_settextrenderingmode(ctx->pgs, mode);
1433
65
            code = pdfi_BT(ctx);
1434
65
            if (code < 0)
1435
0
                return code;
1436
65
        }
1437
521k
        else
1438
521k
            gs_settextrenderingmode(ctx->pgs, mode);
1439
522k
    }
1440
569k
    return code;
1441
569k
}
1442
1443
static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1444
3.62k
{
1445
3.62k
    return gs_settextrise(ctx->pgs, Ts);
1446
3.62k
}
1447
1448
int pdfi_Ts(pdf_context *ctx)
1449
4.03k
{
1450
4.03k
    int code;
1451
4.03k
    double d;
1452
1453
4.03k
    code = pdfi_destack_real(ctx, &d);
1454
4.03k
    if (code < 0)
1455
407
        return code;
1456
1457
3.62k
    return pdfi_set_Ts(ctx, d);
1458
4.03k
}
1459
1460
static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1461
153k
{
1462
153k
    return gs_setwordspacing(ctx->pgs, Tw);
1463
153k
}
1464
1465
int pdfi_Tw(pdf_context *ctx)
1466
154k
{
1467
154k
    int code;
1468
154k
    double d;
1469
1470
154k
    code = pdfi_destack_real(ctx, &d);
1471
154k
    if (code < 0)
1472
845
        return code;
1473
1474
153k
    return pdfi_set_Tw(ctx, d);
1475
154k
}
1476
1477
int pdfi_Tz(pdf_context *ctx)
1478
102k
{
1479
102k
    int code;
1480
102k
    double d;
1481
1482
102k
    code = pdfi_destack_real(ctx, &d);
1483
102k
    if (code < 0)
1484
1.85k
        return code;
1485
1486
100k
    return gs_settexthscaling(ctx->pgs, d);
1487
102k
}
1488
1489
int pdfi_singlequote(pdf_context *ctx)
1490
18.2k
{
1491
18.2k
    int code;
1492
1493
18.2k
    if (ctx->text.BlockDepth == 0) {
1494
8.22k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1495
8.22k
    }
1496
1497
18.2k
    code = pdfi_T_star(ctx);
1498
18.2k
    if (code < 0)
1499
0
        return code;
1500
1501
18.2k
    return pdfi_Tj(ctx);
1502
18.2k
}
1503
1504
int pdfi_doublequote(pdf_context *ctx)
1505
5.70k
{
1506
5.70k
    int code;
1507
5.70k
    double Tw, Tc;
1508
1509
5.70k
    if (ctx->text.BlockDepth == 0) {
1510
5.41k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1511
5.41k
    }
1512
1513
5.70k
    if (pdfi_count_stack(ctx) < 3) {
1514
5.30k
        pdfi_clearstack(ctx);
1515
5.30k
        return_error(gs_error_stackunderflow);
1516
5.30k
    }
1517
1518
400
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1519
172
        pdfi_pop(ctx, 3);
1520
172
        return gs_note_error(gs_error_typecheck);
1521
172
    }
1522
1523
228
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1524
228
    if (code < 0)
1525
2
        goto error;
1526
226
    code = pdfi_set_Tc(ctx, Tc);
1527
226
    if (code < 0)
1528
0
        goto error;
1529
1530
226
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1531
226
    if (code < 0)
1532
11
        goto error;
1533
215
    code = pdfi_set_Tw(ctx, Tw);
1534
215
    if (code < 0)
1535
0
        goto error;
1536
1537
215
    code = pdfi_T_star(ctx);
1538
215
    if (code < 0)
1539
0
        goto error;
1540
1541
215
    code = pdfi_Tj(ctx);
1542
    /* Tj pops one off the stack for us, leaving us 2 to go. */
1543
215
    pdfi_pop(ctx, 2);
1544
215
    return code;
1545
1546
13
error:
1547
13
    pdfi_pop(ctx, 3);
1548
13
    return code;
1549
215
}