Coverage Report

Created: 2026-04-09 07:06

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.86M
{
41
1.86M
    int code;
42
1.86M
    gs_matrix m;
43
1.86M
    bool illegal_BT = false;
44
45
1.86M
    if (ctx->text.BlockDepth != 0) {
46
40.5k
        illegal_BT = true;
47
40.5k
        if (ctx->text.TextClip) {
48
93
            gx_device *dev = gs_currentdevice_inline(ctx->pgs);
49
50
93
            ctx->text.TextClip = false;
51
93
            (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1);
52
93
        }
53
40.5k
        code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
54
40.5k
        if (code < 0)
55
0
            return code;
56
40.5k
    }
57
58
1.86M
    gs_make_identity(&m);
59
1.86M
    code = gs_settextmatrix(ctx->pgs, &m);
60
1.86M
    if (code < 0)
61
0
        return code;
62
63
1.86M
    code = gs_settextlinematrix(ctx->pgs, &m);
64
1.86M
    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.86M
    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
200
        gs_newpath(ctx->pgs);
75
200
    }
76
77
1.86M
    ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
78
1.86M
    if (!ctx->pgs->current_point_valid)
79
1.55M
        code = gs_moveto(ctx->pgs, 0, 0);
80
81
1.86M
    ctx->text.BlockDepth++;
82
83
1.86M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
84
460k
        gs_begin_transparency_text_group(ctx->pgs);
85
86
1.86M
    return code;
87
1.86M
}
88
89
static int do_ET(pdf_context *ctx)
90
1.81M
{
91
1.81M
    int code = 0;
92
1.81M
    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.81M
    if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
105
917
        gs_point initial_point;
106
107
917
        if  (!ctx->device_state.preserve_tr_mode) {
108
915
            ctx->text.TextClip = false;
109
            /* Capture the current position */
110
915
            code = gs_currentpoint(ctx->pgs, &initial_point);
111
915
            if (code >= 0 || code == gs_error_nocurrentpoint) {
112
913
                gs_point adjust;
113
913
                bool nocurrentpoint = code >= 0 ? false : true;
114
115
913
                gs_currentfilladjust(ctx->pgs, &adjust);
116
913
                code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
117
913
                if (code < 0)
118
0
                    return code;
119
120
913
                code = gs_clip(ctx->pgs);
121
913
                if (code >= 0)
122
913
                    copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
123
124
913
                code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
125
913
                if (code < 0)
126
0
                    return code;
127
128
913
                if (copy != NULL)
129
913
                    (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
130
131
913
                if (nocurrentpoint == false)
132
913
                    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
133
913
            }
134
915
        }
135
917
    }
136
1.81M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
137
464k
        gs_end_transparency_text_group(ctx->pgs);
138
139
1.81M
    if (!ctx->text.initial_current_point_valid)
140
1.03M
        gs_newpath(ctx->pgs);
141
1.81M
    return code;
142
1.81M
}
143
144
int pdfi_ET(pdf_context *ctx)
145
1.78M
{
146
1.78M
    if (ctx->text.BlockDepth == 0) {
147
74.1k
        int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
148
74.1k
        return code;
149
74.1k
    }
150
151
1.70M
    ctx->text.BlockDepth--;
152
153
1.70M
    return do_ET(ctx);
154
1.78M
}
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
34.4k
        int code;
163
34.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
34.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
699k
{
184
699k
    return gs_settextspacing(ctx->pgs, Tc);
185
699k
}
186
187
int pdfi_Tc(pdf_context *ctx)
188
712k
{
189
712k
    int code;
190
712k
    double d;
191
192
712k
    code = pdfi_destack_real(ctx, &d);
193
712k
    if (code < 0)
194
12.7k
        return code;
195
196
699k
    return pdfi_set_Tc(ctx, d);
197
712k
}
198
199
int pdfi_Td(pdf_context *ctx)
200
1.17M
{
201
1.17M
    int code;
202
1.17M
    double Txy[2];
203
1.17M
    gs_matrix m, mat;
204
205
1.17M
    code = pdfi_destack_reals(ctx, Txy, 2);
206
1.17M
    if (code < 0)
207
21.3k
        return code;
208
209
1.15M
    gs_make_identity(&m);
210
211
1.15M
    m.tx = Txy[0];
212
1.15M
    m.ty = Txy[1];
213
214
1.15M
    if (ctx->text.BlockDepth == 0) {
215
13.3k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL);
216
217
13.3k
        gs_make_identity(&mat);
218
13.3k
        code = gs_settextmatrix(ctx->pgs, &mat);
219
13.3k
        if (code < 0)
220
0
            return code;
221
222
13.3k
        code = gs_settextlinematrix(ctx->pgs, &mat);
223
13.3k
        if (code < 0)
224
0
            return code;
225
13.3k
    }
226
227
1.15M
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
228
1.15M
    if (code < 0)
229
0
        return code;
230
231
1.15M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
232
1.15M
    if (code < 0)
233
0
        return code;
234
235
1.15M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
236
1.15M
}
237
238
int pdfi_TD(pdf_context *ctx)
239
390k
{
240
390k
    int code;
241
390k
    double Txy[2];
242
390k
    gs_matrix m, mat;
243
244
390k
    gs_make_identity(&m);
245
246
390k
    code = pdfi_destack_reals(ctx, Txy, 2);
247
390k
    if (code < 0)
248
2.73k
        return code;
249
250
387k
    m.tx = Txy[0];
251
387k
    m.ty = Txy[1];
252
253
387k
    if (ctx->text.BlockDepth == 0) {
254
1.32k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
255
256
1.32k
        gs_make_identity(&mat);
257
1.32k
        code = gs_settextmatrix(ctx->pgs, &mat);
258
1.32k
        if (code < 0)
259
0
            return code;
260
261
1.32k
        code = gs_settextlinematrix(ctx->pgs, &mat);
262
1.32k
        if (code < 0)
263
0
            return code;
264
1.32k
    }
265
266
387k
    code = pdfi_set_TL(ctx, m.ty * 1.0f);
267
387k
    if (code < 0)
268
0
        return code;
269
270
387k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
271
387k
    if (code < 0)
272
0
        return code;
273
274
387k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
275
387k
    if (code < 0)
276
0
        return code;
277
278
387k
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
279
387k
}
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
11.2M
{
292
11.2M
    pdf_font *current_font = NULL;
293
11.2M
    gs_matrix mat;
294
11.2M
    float *x_widths = NULL, *y_widths = NULL, width;
295
11.2M
    double Tw = 0, Tc = 0;
296
11.2M
    int i, code;
297
298
11.2M
    text->data.chars = NULL;
299
11.2M
    text->x_widths = NULL;
300
11.2M
    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
11.2M
    current_font = pdfi_get_current_pdf_font(ctx);
308
309
11.2M
    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
11.2M
    Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
317
318
11.2M
    if (current_font->pdfi_font_type == e_pdf_font_type1 ||
319
3.91M
        current_font->pdfi_font_type == e_pdf_font_cff ||
320
2.52M
        current_font->pdfi_font_type == e_pdf_font_type3 ||
321
2.47M
        current_font->pdfi_font_type == e_pdf_font_cff ||
322
2.47M
        current_font->pdfi_font_type == e_pdf_font_truetype ||
323
1.35M
        current_font->pdfi_font_type == e_pdf_font_microtype ||
324
1.35M
        current_font->pdfi_font_type == e_pdf_font_type0)
325
11.2M
    {
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
11.2M
        if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
330
4.73M
            text->operation = TEXT_RETURN_WIDTH;
331
4.73M
            if (Tc != 0) {
332
974k
                text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
333
974k
                if (current_font->pfont && current_font->pfont->WMode == 0) {
334
974k
                    text->delta_all.x = Tc;
335
974k
                    text->delta_all.y = 0;
336
974k
                } else {
337
1
                    text->delta_all.y = Tc;
338
1
                    text->delta_all.x = 0;
339
1
                }
340
974k
            }
341
6.50M
        } else {
342
6.50M
            gs_point pt;
343
344
6.50M
            x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
345
6.50M
            y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
346
6.50M
            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
6.50M
            memset(x_widths, 0x00, s->length * sizeof(float));
352
6.50M
            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
6.50M
            mat = current_font->pfont->FontMatrix;
358
359
29.2M
            for (i = 0;i < s->length; i++) {
360
                /* Get the width (in unscaled text units) */
361
22.7M
                if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
362
97.2k
                    width = current_font->MissingWidth;
363
22.6M
                else
364
22.6M
                    width = current_font->Widths[s->data[i] - current_font->FirstChar];
365
                /* And convert the width into an appropriate value for the current environment */
366
22.7M
                gs_distance_transform(width, 0, &mat, &pt);
367
22.7M
                x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
368
                /* Add any Tc value */
369
22.7M
                x_widths[i] += Tc;
370
22.7M
            }
371
6.50M
            text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
372
6.50M
            text->x_widths = x_widths;
373
6.50M
            text->y_widths = y_widths;
374
6.50M
            text->widths_size = s->length * 2;
375
6.50M
        }
376
377
11.2M
        Tw = gs_currentwordspacing(ctx->pgs);
378
11.2M
        if (Tw != 0) {
379
877k
            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
877k
            if (current_font->pfont && current_font->pfont->WMode == 0) {
385
877k
                text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
386
877k
                text->delta_space.y = 0;
387
877k
            } else {
388
0
                text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
389
0
                text->delta_space.x = 0;
390
0
            }
391
877k
            text->space.s_char = 0x20;
392
877k
        }
393
394
11.2M
        if (current_font->pdfi_font_type == e_pdf_font_type3) {
395
44.9k
            text->operation |= TEXT_FROM_CHARS;
396
44.9k
            text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
397
44.9k
            if (!text->data.chars) {
398
0
                code = gs_note_error(gs_error_VMerror);
399
0
                goto text_params_error;
400
0
            }
401
402
220k
            for (i = 0; i < s->length; i++) {
403
175k
                ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
404
175k
            }
405
44.9k
        }
406
11.1M
        else {
407
11.1M
            text->operation |= TEXT_FROM_BYTES;
408
11.1M
            text->data.bytes = (const byte *)s->data;
409
11.1M
        }
410
11.2M
        text->size = s->length;
411
11.2M
    }
412
46
    else {
413
46
        code = gs_note_error(gs_error_invalidfont);
414
46
        goto text_params_error;
415
46
    }
416
417
11.2M
    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
11.2M
}
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
11.1M
{
436
11.1M
    int code = 0;
437
11.1M
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
438
439
11.1M
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
440
11.1M
    if (code >= 0) {
441
11.0M
        penum->single_byte_space = true;
442
11.0M
        saved_penum = ctx->text.current_enum;
443
11.0M
        ctx->text.current_enum = penum;
444
11.0M
        code = gs_text_process(penum);
445
11.0M
        gs_text_release(ctx->pgs, penum, "pdfi_Tj");
446
11.0M
        ctx->text.current_enum = saved_penum;
447
11.0M
    }
448
11.1M
    return code;
449
11.1M
}
450
451
/* Mode 0 - fill */
452
static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
453
10.2M
{
454
10.2M
    int code;
455
456
    /* just draw the text */
457
10.2M
    text->operation |= TEXT_DO_DRAW;
458
459
10.2M
    code = pdfi_show_simple(ctx, text);
460
461
10.2M
    text->operation &= ~TEXT_DO_DRAW;
462
10.2M
    return code;
463
10.2M
}
464
465
/* Mode 1 - stroke */
466
static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text)
467
1.53k
{
468
1.53k
    int code;
469
1.53k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
470
1.53k
    gs_point end_point, initial_point;
471
1.53k
    gx_device *dev = ctx->pgs->device;
472
1.53k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
473
474
1.53k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
475
476
    /* Capture the current position */
477
1.53k
    code = gs_currentpoint(ctx->pgs, &initial_point);
478
1.53k
    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.53k
    code = pdfi_gsave(ctx);
486
1.53k
    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.53k
    code = gs_newpath(ctx->pgs);
493
1.53k
    if (code < 0)
494
0
        goto Tr1_error;
495
1.53k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
496
1.53k
    if (code < 0)
497
0
        goto Tr1_error;
498
499
    /* Don't draw the text, create a path suitable for stroking */
500
1.53k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
501
502
    /* Run the text methods to create the path */
503
1.53k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
504
1.53k
    if (code < 0)
505
30
        goto Tr1_error;
506
507
1.50k
    penum->single_byte_space = true;
508
1.50k
    saved_penum = ctx->text.current_enum;
509
1.50k
    ctx->text.current_enum = penum;
510
1.50k
    code = gs_text_process(penum);
511
1.50k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
512
1.50k
    ctx->text.current_enum = saved_penum;
513
1.50k
    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.50k
    code = gs_currentpoint(ctx->pgs, &end_point);
522
1.50k
    if (code < 0)
523
0
        goto Tr1_error;
524
525
1.50k
    if (talphabits != galphabits)
526
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
527
528
    /* Change to the current stroking colour */
529
1.50k
    gs_swapcolors_quick(ctx->pgs);
530
    /* Finally, stroke the actual path */
531
1.50k
    code = gs_stroke(ctx->pgs);
532
    /* Switch back to the non-stroke colour */
533
1.50k
    gs_swapcolors_quick(ctx->pgs);
534
535
1.50k
    if (talphabits != galphabits)
536
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
537
538
1.53k
Tr1_error:
539
    /* And grestore back to where we started */
540
1.53k
    (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.53k
    if (code >= 0)
544
1.50k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
545
546
1.53k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
547
1.53k
    return code;
548
1.50k
}
549
550
/* Mode 2 - fill then stroke */
551
static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text)
552
7.76k
{
553
7.76k
    int code, restart = 0;
554
7.76k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
555
7.76k
    gs_point end_point, initial_point;
556
7.76k
    gx_device *dev = ctx->pgs->device;
557
7.76k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
558
559
7.76k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
560
561
    /* Capture the current position */
562
7.76k
    code = gs_currentpoint(ctx->pgs, &initial_point);
563
7.76k
    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.76k
    code = pdfi_gsave(ctx);
571
7.76k
    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.76k
    code = gs_newpath(ctx->pgs);
578
7.76k
    if (code < 0)
579
0
        goto Tr1_error;
580
7.76k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
581
7.76k
    if (code < 0)
582
0
        goto Tr1_error;
583
584
    /* Don't draw the text, create a path suitable for stroking */
585
7.76k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
586
587
    /* Run the text methods to create the path */
588
7.76k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
589
7.76k
    if (code < 0)
590
0
        goto Tr1_error;
591
592
7.76k
    penum->single_byte_space = true;
593
7.76k
    saved_penum = ctx->text.current_enum;
594
7.76k
    ctx->text.current_enum = penum;
595
7.76k
    code = gs_text_process(penum);
596
7.76k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
597
7.76k
    ctx->text.current_enum = saved_penum;
598
7.76k
    if (code < 0)
599
30
        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.73k
    code = gs_currentpoint(ctx->pgs, &end_point);
607
7.73k
    if (code < 0)
608
0
        goto Tr1_error;
609
7.73k
    if (talphabits != galphabits)
610
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
611
7.73k
    code = gs_fillstroke(ctx->pgs, &restart);
612
7.73k
    if (talphabits != galphabits)
613
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
614
615
7.76k
Tr1_error:
616
    /* And grestore back to where we started */
617
7.76k
    (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.76k
    if (code >= 0)
621
7.73k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
622
623
7.76k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
624
7.76k
    return code;
625
7.73k
}
626
627
static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
628
123k
{
629
123k
    int code;
630
123k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
631
632
    /* Don't draw the text */
633
123k
    text->operation |= TEXT_DO_NONE | TEXT_RENDER_MODE_3;
634
635
    /* Run the text methods to create the path */
636
123k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
637
123k
    if (code < 0)
638
0
        return code;
639
640
123k
    penum->single_byte_space = true;
641
123k
    saved_penum = ctx->text.current_enum;
642
123k
    ctx->text.current_enum = penum;
643
123k
    code = gs_text_process(penum);
644
123k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
645
123k
    ctx->text.current_enum = saved_penum;
646
647
123k
    return code;
648
123k
}
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.44k
{
655
2.44k
    int code;
656
2.44k
    gs_point initial_point;
657
658
    /* Capture the current position */
659
2.44k
    code = gs_currentpoint(ctx->pgs, &initial_point);
660
2.44k
    if (code < 0)
661
0
        return code;
662
663
2.44k
    ctx->text.TextClip = true;
664
    /* First fill the text */
665
2.44k
    code = pdfi_show_Tr_0(ctx, text);
666
2.44k
    if (code < 0)
667
0
        return code;
668
669
    /* Return the current point to the initial point */
670
2.44k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
671
2.44k
    if (code >= 0)
672
        /* And add the text to the acumulated path for clipping */
673
2.44k
        code = pdfi_show_Tr_7(ctx, text);
674
675
2.44k
    return code;
676
2.44k
}
677
678
static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
679
337
{
680
337
    int code;
681
337
    gs_point initial_point;
682
683
    /* Capture the current position */
684
337
    code = gs_currentpoint(ctx->pgs, &initial_point);
685
337
    if (code < 0)
686
0
        return code;
687
688
337
    ctx->text.TextClip = true;
689
    /* First stroke the text */
690
337
    code = pdfi_show_Tr_1(ctx, text);
691
337
    if (code < 0)
692
0
        return code;
693
694
    /* Return the current point to the initial point */
695
337
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
696
337
    if (code >= 0)
697
        /* And add the text to the acumulated path for clipping */
698
337
        code = pdfi_show_Tr_7(ctx, text);
699
700
337
    return code;
701
337
}
702
703
static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
704
324
{
705
324
    int code;
706
324
    gs_point initial_point;
707
708
    /* Capture the current position */
709
324
    code = gs_currentpoint(ctx->pgs, &initial_point);
710
324
    if (code < 0)
711
0
        return code;
712
713
324
    ctx->text.TextClip = true;
714
    /* First fill and stroke the text */
715
324
    code = pdfi_show_Tr_2(ctx, text);
716
324
    if (code < 0)
717
0
        return code;
718
719
    /* Return the current point to the initial point */
720
324
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
721
324
    if (code >= 0)
722
        /* And add the text to the acumulated path for clipping */
723
324
        code = pdfi_show_Tr_7(ctx, text);
724
725
324
    return code;
726
324
}
727
728
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
729
3.22k
{
730
3.22k
    int code;
731
3.22k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
732
733
3.22k
    ctx->text.TextClip = true;
734
    /* Don't draw the text, create a path suitable for filling */
735
3.22k
    text->operation |= TEXT_DO_TRUE_CHARPATH;
736
737
    /* Run the text methods to create the path */
738
3.22k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
739
3.22k
    if (code < 0)
740
0
        goto Tr7_error;
741
742
3.22k
    penum->single_byte_space = true;
743
3.22k
    saved_penum = ctx->text.current_enum;
744
3.22k
    ctx->text.current_enum = penum;
745
3.22k
    code = gs_text_process(penum);
746
3.22k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
747
3.22k
    ctx->text.current_enum = saved_penum;
748
749
3.22k
Tr7_error:
750
3.22k
    text->operation &= ~TEXT_DO_TRUE_CHARPATH;
751
3.22k
    return code;
752
3.22k
}
753
754
static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
755
841k
{
756
841k
    int Trmode = 0, code;
757
841k
    pdf_font *current_font = NULL;
758
841k
    gs_point initial_point;
759
760
841k
    current_font = pdfi_get_current_pdf_font(ctx);
761
841k
    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
841k
    code = gs_currentpoint(ctx->pgs, &initial_point);
766
767
841k
    Trmode = gs_currenttextrenderingmode(ctx->pgs);
768
841k
    if (Trmode == 3) {
769
4.18k
        if (current_font->pdfi_font_type == e_pdf_font_type3)
770
0
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
771
4.18k
        else
772
4.18k
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
773
4.18k
    }
774
837k
    else
775
837k
        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
841k
    if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) {
789
169
        gs_swapcolors_quick(ctx->pgs);
790
791
169
        code = gx_set_dev_color(ctx->pgs);
792
169
        if (code != 0)
793
0
            return code;
794
795
169
        code = gs_gstate_color_load(ctx->pgs);
796
169
        if (code < 0)
797
0
            return code;
798
799
169
        gs_swapcolors_quick(ctx->pgs);
800
169
    }
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
841k
    if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) {
808
2
        gx_device *dev = gs_currentdevice_inline(ctx->pgs);
809
810
2
        ctx->text.TextClip = true;
811
2
        code = dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1);
812
2
        if (code < 0 && code != gs_error_undefined)
813
0
            return code;
814
2
  }
815
816
841k
    code = pdfi_show_simple(ctx, text);
817
841k
    return code;
818
841k
}
819
820
static int pdfi_show(pdf_context *ctx, pdf_string *s)
821
11.2M
{
822
11.2M
    int code = 0, SavedTextDepth = 0;
823
11.2M
    int code1 = 0;
824
11.2M
    gs_text_params_t text;
825
11.2M
    pdf_font *current_font = NULL;
826
11.2M
    int Trmode = 0;
827
11.2M
    int initial_gsave_level = ctx->pgs->level;
828
11.2M
    pdfi_trans_state_t state;
829
11.2M
    int outside_text_block = 0;
830
831
11.2M
    if (ctx->text.BlockDepth == 0) {
832
112k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
833
112k
        outside_text_block = 1;
834
112k
    }
835
836
11.2M
    if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
837
11.2M
     || 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
8.90k
         return 0;
843
8.90k
    }
844
845
11.2M
    current_font = pdfi_get_current_pdf_font(ctx);
846
11.2M
    if (current_font == NULL)
847
12
        return_error(gs_error_invalidfont);
848
849
11.2M
    code = pdfi_show_set_params(ctx, s, &text);
850
11.2M
    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
11.2M
    SavedTextDepth = ctx->text.BlockDepth;
861
11.2M
    ctx->text.BlockDepth = 0;
862
11.2M
    code = pdfi_trans_setup_text(ctx, &state, true);
863
11.2M
    ctx->text.BlockDepth = SavedTextDepth;
864
865
11.2M
    if (code >= 0) {
866
11.2M
        if (ctx->device_state.preserve_tr_mode) {
867
841k
        code = pdfi_show_Tr_preserve(ctx, &text);
868
10.3M
        } else {
869
10.3M
            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
10.3M
            if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
875
233
                Trmode = 0;
876
877
10.3M
            switch(Trmode) {
878
10.2M
            case 0:
879
10.2M
                code = pdfi_show_Tr_0(ctx, &text);
880
10.2M
                break;
881
1.19k
            case 1:
882
1.19k
                code = pdfi_show_Tr_1(ctx, &text);
883
1.19k
                break;
884
7.44k
            case 2:
885
7.44k
                code = pdfi_show_Tr_2(ctx, &text);
886
7.44k
                break;
887
123k
            case 3:
888
123k
                code = pdfi_show_Tr_3(ctx, &text);
889
123k
                break;
890
2.44k
            case 4:
891
2.44k
                code = pdfi_show_Tr_4(ctx, &text);
892
2.44k
                break;
893
337
            case 5:
894
337
                code = pdfi_show_Tr_5(ctx, &text);
895
337
                break;
896
324
            case 6:
897
324
                code = pdfi_show_Tr_6(ctx, &text);
898
324
                break;
899
122
            case 7:
900
122
                code = pdfi_show_Tr_7(ctx, &text);
901
122
                break;
902
0
            default:
903
0
                break;
904
10.3M
            }
905
10.3M
        }
906
11.2M
        code1 = pdfi_trans_teardown_text(ctx, &state);
907
11.2M
        if (code == 0)
908
11.1M
            code = code1;
909
11.2M
    }
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
11.2M
    while(ctx->pgs->level > initial_gsave_level)
920
386
        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
11.2M
    if (outside_text_block) {
927
108k
        code1 = do_ET(ctx);
928
108k
        if (code == 0)
929
103k
            code = code1;
930
108k
    }
931
932
11.2M
show_error:
933
11.2M
    if ((void *)text.data.chars != (void *)s->data)
934
44.9k
        gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
935
936
11.2M
    gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
937
11.2M
    gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
938
11.2M
    return code;
939
11.2M
}
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
22.5k
{
946
22.5k
    int code = 0;
947
22.5k
    gx_device_bbox *bbdev;
948
22.5k
    pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
949
22.5k
    gs_matrix tmpmat, Trm, Trm_ctm;
950
22.5k
    gs_point cppt, startpt;
951
952
22.5k
    if (current_font == NULL)
953
0
        return_error(gs_error_invalidfont);
954
955
956
22.5k
    for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
957
958
22.5k
    if (ctx->devbbox == NULL) {
959
205
        bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
960
205
        if (bbdev == NULL)
961
0
           return_error(gs_error_VMerror);
962
205
        gx_device_bbox_init(bbdev, NULL, ctx->memory);
963
205
        ctx->devbbox = (gx_device *)bbdev;
964
205
        rc_increment(ctx->devbbox);
965
205
    }
966
22.3k
    else {
967
22.3k
        bbdev = (gx_device_bbox *)ctx->devbbox;
968
22.3k
    }
969
22.5k
    gx_device_retain((gx_device *)bbdev, true);
970
22.5k
    gx_device_bbox_set_white_opaque(bbdev, true);
971
972
22.5k
    code = pdfi_gsave(ctx);
973
22.5k
    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
22.5k
    gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
979
980
22.5k
    code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
981
22.5k
    if (code < 0)
982
0
        goto out;
983
984
22.5k
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
985
22.5k
    Trm.xy = 0;
986
22.5k
    Trm.yx = 0;
987
22.5k
    Trm.yy = ctx->pgs->PDFfontsize;
988
22.5k
    Trm.tx = 0;
989
22.5k
    Trm.ty = ctx->pgs->textrise;
990
991
22.5k
    memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
992
    /* We want to avoid any translations unrelated to the extents of the text */
993
22.5k
    tmpmat.tx = tmpmat.ty = 0;
994
22.5k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm);
995
996
22.5k
    memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
997
    /* As above */
998
22.5k
    tmpmat.tx = tmpmat.ty = 0;
999
22.5k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
1000
22.5k
    gs_setmatrix(ctx->pgs, &Trm_ctm);
1001
1002
22.5k
    gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
1003
1004
22.5k
    code = pdfi_gs_setgray(ctx, 1.0);
1005
22.5k
    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
22.5k
    startpt.x = ctx->pgs->PDFfontsize;
1015
22.5k
    startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1016
22.5k
    code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1017
22.5k
    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
22.5k
    ctx->text.BlockDepth++;
1022
22.5k
    code = pdfi_show(ctx, s);
1023
    /* And an ET */
1024
22.5k
    ctx->text.BlockDepth--;
1025
22.5k
    if (code < 0)
1026
0
        goto out;
1027
1028
22.5k
    code = gx_device_bbox_bbox(bbdev, bboxout);
1029
22.5k
    if (code < 0)
1030
0
        goto out;
1031
1032
22.5k
    bboxout->q.x -= bboxout->p.x;
1033
22.5k
    bboxout->q.y -= bboxout->p.y;
1034
22.5k
    bboxout->p.x = bboxout->p.y = 0;
1035
1036
22.5k
    code = gs_currentpoint(ctx->pgs, &cppt);
1037
22.5k
    if (code >= 0) {
1038
22.5k
        code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1039
22.5k
        if (code < 0)
1040
0
            goto out;
1041
22.5k
        advance_width->x = ctx->pgs->current_point.x - startpt.x;
1042
22.5k
        advance_width->y = ctx->pgs->current_point.y - startpt.y;
1043
22.5k
        code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1044
22.5k
    }
1045
22.5k
out:
1046
22.5k
    pdfi_grestore(ctx);
1047
22.5k
    (void)gs_closedevice((gx_device *)bbdev);
1048
22.5k
    gx_device_retain((gx_device *)bbdev, false);
1049
1050
22.5k
    return code;
1051
22.5k
}
1052
1053
int pdfi_Tj(pdf_context *ctx)
1054
1.69M
{
1055
1.69M
    int code = 0;
1056
1.69M
    pdf_string *s = NULL;
1057
1.69M
    gs_matrix saved, Trm;
1058
1.69M
    gs_point initial_point, current_point, pt;
1059
1.69M
    double linewidth = ctx->pgs->line_params.half_width;
1060
1.69M
    int initial_point_valid;
1061
1062
1.69M
    if (pdfi_count_stack(ctx) < 1)
1063
34.3k
        return_error(gs_error_stackunderflow);
1064
1065
1.66M
    if (pdfi_oc_is_off(ctx))
1066
2.06k
        goto exit;
1067
1068
1.65M
    s = (pdf_string *)ctx->stack_top[-1];
1069
1.65M
    if (pdfi_type_of(s) != PDF_STRING) {
1070
55.3k
        pdfi_pop(ctx, 1);
1071
55.3k
        return_error(gs_error_typecheck);
1072
55.3k
    }
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.60M
    pdfi_countup(s);
1079
1.60M
    pdfi_pop(ctx, 1);
1080
1081
    /* Save the CTM for later restoration */
1082
1.60M
    saved = ctm_only(ctx->pgs);
1083
1.60M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1084
1085
1.60M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1086
1.60M
    Trm.xy = 0;
1087
1.60M
    Trm.yx = 0;
1088
1.60M
    Trm.yy = ctx->pgs->PDFfontsize;
1089
1.60M
    Trm.tx = 0;
1090
1.60M
    Trm.ty = ctx->pgs->textrise;
1091
1092
1.60M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1093
1.60M
    if (code < 0)
1094
0
        goto exit;
1095
1096
1.60M
    if (!ctx->device_state.preserve_tr_mode) {
1097
1.53M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1098
1.53M
        if (code < 0)
1099
56.6k
            goto exit;
1100
1.48M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1101
1.48M
    } 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
64.7k
        gs_matrix devmatrix, matrix;
1108
64.7k
        gx_device *device = gs_currentdevice(ctx->pgs);
1109
1110
64.7k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1111
64.7k
        devmatrix.xy = 0;
1112
64.7k
        devmatrix.yx = 0;
1113
64.7k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1114
64.7k
        devmatrix.tx = 0;
1115
64.7k
        devmatrix.ty = 0;
1116
1117
64.7k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1118
64.7k
        if (code < 0)
1119
0
            goto exit;
1120
1121
64.7k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1122
64.7k
        if (code < 0)
1123
0
            goto exit;
1124
64.7k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1125
64.7k
    }
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
39.4k
        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
68.8k
    else
1170
68.8k
        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.68M
{
1181
1.68M
    int code = 0, i;
1182
1.68M
    pdf_array *a = NULL;
1183
1.68M
    pdf_obj *o = NULL;
1184
1.68M
    double dx = 0;
1185
1.68M
    gs_point pt;
1186
1.68M
    gs_matrix saved, Trm;
1187
1.68M
    gs_point initial_point, current_point;
1188
1.68M
    double linewidth = ctx->pgs->line_params.half_width;
1189
1.68M
    pdf_font *current_font = NULL;
1190
1.68M
    int initial_point_valid;
1191
1192
1.68M
    current_font = pdfi_get_current_pdf_font(ctx);
1193
1.68M
    if (current_font == NULL)
1194
5.19k
        return_error(gs_error_invalidfont);
1195
1196
1.68M
    if (ctx->text.BlockDepth == 0) {
1197
62.7k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1198
62.7k
    }
1199
1200
1.68M
    if (pdfi_count_stack(ctx) < 1)
1201
14.1k
        return_error(gs_error_stackunderflow);
1202
1203
1.66M
    if (pdfi_oc_is_off(ctx))
1204
486
        goto exit;
1205
1206
1.66M
    a = (pdf_array *)ctx->stack_top[-1];
1207
1.66M
    if (pdfi_type_of(a) != PDF_ARRAY) {
1208
44.9k
        pdfi_pop(ctx, 1);
1209
44.9k
        return gs_note_error(gs_error_typecheck);
1210
44.9k
    }
1211
1.62M
    pdfi_countup(a);
1212
1.62M
    pdfi_pop(ctx, 1);
1213
1214
    /* Save the CTM for later restoration */
1215
1.62M
    saved = ctm_only(ctx->pgs);
1216
1.62M
    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.62M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1222
1.62M
    Trm.xy = 0;
1223
1.62M
    Trm.yx = 0;
1224
1.62M
    Trm.yy = ctx->pgs->PDFfontsize;
1225
1.62M
    Trm.tx = 0;
1226
1.62M
    Trm.ty = ctx->pgs->textrise;
1227
1228
1.62M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1229
1.62M
    if (code < 0)
1230
0
        goto exit;
1231
1232
1.62M
    if (!ctx->device_state.preserve_tr_mode) {
1233
1.53M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1234
1.53M
        if (code < 0)
1235
11.9k
            goto exit;
1236
1.52M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1237
1.52M
    } 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
81.0k
        gs_matrix devmatrix, matrix;
1244
81.0k
        gx_device *device = gs_currentdevice(ctx->pgs);
1245
1246
81.0k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1247
81.0k
        devmatrix.xy = 0;
1248
81.0k
        devmatrix.yx = 0;
1249
81.0k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1250
81.0k
        devmatrix.tx = 0;
1251
81.0k
        devmatrix.ty = 0;
1252
1253
81.0k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1254
81.0k
        if (code < 0)
1255
0
            goto exit;
1256
1257
81.0k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1258
81.0k
        if (code < 0)
1259
0
            goto exit;
1260
81.0k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1261
81.0k
    }
1262
1263
1.60M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1264
1.60M
    if (code < 0)
1265
0
        goto exit;
1266
1.60M
    code = gs_setmatrix(ctx->pgs, &Trm);
1267
1.60M
    if (code < 0)
1268
0
        goto TJ_error;
1269
1270
1.60M
    code = gs_moveto(ctx->pgs, 0, 0);
1271
1.60M
    if (code < 0)
1272
0
        goto TJ_error;
1273
1274
19.3M
    for (i = 0; i < pdfi_array_size(a); i++) {
1275
17.8M
        code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1276
17.8M
        if (code < 0)
1277
2
            goto TJ_error;
1278
1279
17.8M
        if (pdfi_type_of(o) == PDF_STRING)
1280
9.68M
            code = pdfi_show(ctx, (pdf_string *)o);
1281
8.13M
        else {
1282
8.13M
            code = pdfi_obj_to_real(ctx, o, &dx);
1283
8.13M
            if (code < 0)
1284
80
                goto TJ_error;
1285
8.13M
            dx /= -1000;
1286
8.13M
            if (current_font->pfont && current_font->pfont->WMode == 0)
1287
8.13M
                code = gs_rmoveto(ctx->pgs, dx, 0);
1288
5.46k
            else
1289
5.46k
                code = gs_rmoveto(ctx->pgs, 0, dx);
1290
8.13M
        }
1291
17.8M
        pdfi_countdown(o);
1292
17.8M
        o = NULL;
1293
17.8M
        if (code < 0)
1294
38.0k
            goto TJ_error;
1295
17.8M
    }
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.57M
    if (ctx->text.BlockDepth > 0) {
1301
        /* Update the Text matrix with the current point, for the next operation
1302
         */
1303
1.55M
        (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1304
1.55M
        Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1305
1.55M
        Trm.xy = 0;
1306
1.55M
        Trm.yx = 0;
1307
1.55M
        Trm.yy = ctx->pgs->PDFfontsize;
1308
1.55M
        Trm.tx = 0;
1309
1.55M
        Trm.ty = 0;
1310
1.55M
        code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1311
1.55M
        if (code < 0)
1312
0
            goto TJ_error;
1313
1314
1.55M
        code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1315
1.55M
        if (code < 0)
1316
0
            goto TJ_error;
1317
1.55M
        ctx->pgs->textmatrix.tx += pt.x;
1318
1.55M
        ctx->pgs->textmatrix.ty += pt.y;
1319
1.55M
    }
1320
1.60M
TJ_error:
1321
1.60M
    pdfi_countdown(o);
1322
    /* Restore the CTM to the saved value */
1323
1.60M
    (void)gs_setmatrix(ctx->pgs, &saved);
1324
    /* And restore the currentpoint */
1325
1.60M
    if (initial_point_valid)
1326
1.57M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1327
30.9k
    else
1328
30.9k
        code = gs_newpath(ctx->pgs);
1329
    /* And the line width */
1330
1.60M
    ctx->pgs->line_params.half_width = linewidth;
1331
1332
1.62M
 exit:
1333
1.62M
    pdfi_countdown(a);
1334
1.62M
    return code;
1335
1.60M
}
1336
1337
static int pdfi_set_TL(pdf_context *ctx, double TL)
1338
401k
{
1339
401k
    return gs_settextleading(ctx->pgs, TL);
1340
401k
}
1341
1342
int pdfi_TL(pdf_context *ctx)
1343
14.0k
{
1344
14.0k
    int code;
1345
14.0k
    double d;
1346
1347
14.0k
    code = pdfi_destack_real(ctx, &d);
1348
14.0k
    if (code < 0)
1349
46
        return code;
1350
1351
14.0k
    return pdfi_set_TL(ctx, -d);
1352
14.0k
}
1353
1354
int pdfi_Tm(pdf_context *ctx)
1355
1.68M
{
1356
1.68M
    int code;
1357
1.68M
    float m[6];
1358
1.68M
    gs_matrix mat;
1359
1360
1.68M
    code = pdfi_destack_floats(ctx, m, 6);
1361
1.68M
    if (code < 0)
1362
187k
        return code;
1363
1364
1.49M
    if (ctx->text.BlockDepth == 0) {
1365
61.0k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1366
1367
61.0k
        gs_make_identity(&mat);
1368
61.0k
        code = gs_settextmatrix(ctx->pgs, &mat);
1369
61.0k
        if (code < 0)
1370
0
            return code;
1371
1372
61.0k
        code = gs_settextlinematrix(ctx->pgs, &mat);
1373
61.0k
        if (code < 0)
1374
0
            return code;
1375
61.0k
    }
1376
1377
1.49M
    if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1378
16.4k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1379
1380
1.49M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1381
1.49M
    if (code < 0)
1382
0
        return code;
1383
1384
1.49M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1385
1.49M
}
1386
1387
int pdfi_Tr(pdf_context *ctx)
1388
665k
{
1389
665k
    int code;
1390
665k
    int64_t mode;
1391
1392
665k
    code = pdfi_destack_int(ctx, &mode);
1393
665k
    if (code < 0)
1394
8.92k
        return code;
1395
1396
656k
    if (mode < 0 || mode > 7)
1397
5.16k
        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
651k
    if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1404
70
        pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1405
1406
651k
    if (ctx->device_state.preserve_tr_mode) {
1407
4.29k
        gs_settextrenderingmode(ctx->pgs, mode);
1408
4.29k
    } else
1409
647k
    {
1410
647k
        gs_point initial_point;
1411
1412
647k
        if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1413
429
            gs_settextrenderingmode(ctx->pgs, mode);
1414
            /* Capture the current position */
1415
429
            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
429
            gs_newpath(ctx->pgs);
1420
429
            if (code >= 0)
1421
377
                gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1422
429
            code = 0;
1423
646k
        } 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
78
            code = pdfi_ET(ctx);
1430
78
            if (code < 0)
1431
2
                return code;
1432
76
            gs_settextrenderingmode(ctx->pgs, mode);
1433
76
            code = pdfi_BT(ctx);
1434
76
            if (code < 0)
1435
0
                return code;
1436
76
        }
1437
646k
        else
1438
646k
            gs_settextrenderingmode(ctx->pgs, mode);
1439
647k
    }
1440
651k
    return code;
1441
651k
}
1442
1443
static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1444
3.35k
{
1445
3.35k
    return gs_settextrise(ctx->pgs, Ts);
1446
3.35k
}
1447
1448
int pdfi_Ts(pdf_context *ctx)
1449
3.76k
{
1450
3.76k
    int code;
1451
3.76k
    double d;
1452
1453
3.76k
    code = pdfi_destack_real(ctx, &d);
1454
3.76k
    if (code < 0)
1455
403
        return code;
1456
1457
3.35k
    return pdfi_set_Ts(ctx, d);
1458
3.76k
}
1459
1460
static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1461
154k
{
1462
154k
    return gs_setwordspacing(ctx->pgs, Tw);
1463
154k
}
1464
1465
int pdfi_Tw(pdf_context *ctx)
1466
155k
{
1467
155k
    int code;
1468
155k
    double d;
1469
1470
155k
    code = pdfi_destack_real(ctx, &d);
1471
155k
    if (code < 0)
1472
845
        return code;
1473
1474
154k
    return pdfi_set_Tw(ctx, d);
1475
155k
}
1476
1477
int pdfi_Tz(pdf_context *ctx)
1478
125k
{
1479
125k
    int code;
1480
125k
    double d;
1481
1482
125k
    code = pdfi_destack_real(ctx, &d);
1483
125k
    if (code < 0)
1484
2.27k
        return code;
1485
1486
122k
    return gs_settexthscaling(ctx->pgs, d);
1487
125k
}
1488
1489
int pdfi_singlequote(pdf_context *ctx)
1490
18.4k
{
1491
18.4k
    int code;
1492
1493
18.4k
    if (ctx->text.BlockDepth == 0) {
1494
9.94k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1495
9.94k
    }
1496
1497
18.4k
    code = pdfi_T_star(ctx);
1498
18.4k
    if (code < 0)
1499
0
        return code;
1500
1501
18.4k
    return pdfi_Tj(ctx);
1502
18.4k
}
1503
1504
int pdfi_doublequote(pdf_context *ctx)
1505
7.75k
{
1506
7.75k
    int code;
1507
7.75k
    double Tw, Tc;
1508
1509
7.75k
    if (ctx->text.BlockDepth == 0) {
1510
7.48k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1511
7.48k
    }
1512
1513
7.75k
    if (pdfi_count_stack(ctx) < 3) {
1514
7.36k
        pdfi_clearstack(ctx);
1515
7.36k
        return_error(gs_error_stackunderflow);
1516
7.36k
    }
1517
1518
392
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1519
189
        pdfi_pop(ctx, 3);
1520
189
        return gs_note_error(gs_error_typecheck);
1521
189
    }
1522
1523
203
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1524
203
    if (code < 0)
1525
3
        goto error;
1526
200
    code = pdfi_set_Tc(ctx, Tc);
1527
200
    if (code < 0)
1528
0
        goto error;
1529
1530
200
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1531
200
    if (code < 0)
1532
11
        goto error;
1533
189
    code = pdfi_set_Tw(ctx, Tw);
1534
189
    if (code < 0)
1535
0
        goto error;
1536
1537
189
    code = pdfi_T_star(ctx);
1538
189
    if (code < 0)
1539
0
        goto error;
1540
1541
189
    code = pdfi_Tj(ctx);
1542
    /* Tj pops one off the stack for us, leaving us 2 to go. */
1543
189
    pdfi_pop(ctx, 2);
1544
189
    return code;
1545
1546
14
error:
1547
14
    pdfi_pop(ctx, 3);
1548
14
    return code;
1549
189
}