Coverage Report

Created: 2025-06-24 07:01

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