Coverage Report

Created: 2025-11-16 07:40

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-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
3.69M
{
41
3.69M
    int code;
42
3.69M
    gs_matrix m;
43
3.69M
    bool illegal_BT = false;
44
45
3.69M
    if (ctx->text.BlockDepth != 0) {
46
276k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
47
276k
        illegal_BT = true;
48
276k
    }
49
50
3.69M
    gs_make_identity(&m);
51
3.69M
    code = gs_settextmatrix(ctx->pgs, &m);
52
3.69M
    if (code < 0)
53
0
        return code;
54
55
3.69M
    code = gs_settextlinematrix(ctx->pgs, &m);
56
3.69M
    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
3.69M
    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
90
        gs_newpath(ctx->pgs);
67
90
    }
68
69
3.69M
    ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
70
3.69M
    if (!ctx->pgs->current_point_valid)
71
3.61M
        code = gs_moveto(ctx->pgs, 0, 0);
72
73
3.69M
    ctx->text.BlockDepth++;
74
75
3.69M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
76
1.12M
        gs_begin_transparency_text_group(ctx->pgs);
77
78
3.69M
    return code;
79
3.69M
}
80
81
static int do_ET(pdf_context *ctx)
82
3.63M
{
83
3.63M
    int code = 0;
84
3.63M
    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
3.63M
    if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
97
410
        gs_point initial_point;
98
99
410
        if  (!ctx->device_state.preserve_tr_mode) {
100
396
            ctx->text.TextClip = false;
101
            /* Capture the current position */
102
396
            code = gs_currentpoint(ctx->pgs, &initial_point);
103
396
            if (code >= 0 || code == gs_error_nocurrentpoint) {
104
396
                gs_point adjust;
105
396
                bool nocurrentpoint = code >= 0 ? false : true;
106
107
396
                gs_currentfilladjust(ctx->pgs, &adjust);
108
396
                code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
109
396
                if (code < 0)
110
0
                    return code;
111
112
396
                code = gs_clip(ctx->pgs);
113
396
                if (code >= 0)
114
396
                    copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
115
116
396
                code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
117
396
                if (code < 0)
118
0
                    return code;
119
120
396
                if (copy != NULL)
121
396
                    (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
122
123
396
                if (nocurrentpoint == false)
124
391
                    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
125
396
            }
126
396
        }
127
410
    }
128
3.63M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
129
1.16M
        gs_end_transparency_text_group(ctx->pgs);
130
131
3.63M
    if (!ctx->text.initial_current_point_valid)
132
3.58M
        gs_newpath(ctx->pgs);
133
3.63M
    return code;
134
3.63M
}
135
136
int pdfi_ET(pdf_context *ctx)
137
3.62M
{
138
3.62M
    if (ctx->text.BlockDepth == 0) {
139
72.6k
        int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
140
72.6k
        return code;
141
72.6k
    }
142
143
3.54M
    ctx->text.BlockDepth--;
144
145
3.54M
    return do_ET(ctx);
146
3.62M
}
147
148
int pdfi_T_star(pdf_context *ctx)
149
216k
{
150
216k
    int code;
151
216k
    gs_matrix m, mat;
152
153
216k
    if (ctx->text.BlockDepth == 0) {
154
25.5k
        int code;
155
25.5k
        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
25.5k
    }
158
159
216k
    gs_make_identity(&m);
160
216k
    m.ty += ctx->pgs->textleading;
161
162
216k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
163
216k
    if (code < 0)
164
0
        return code;
165
166
216k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
167
216k
    if (code < 0)
168
0
        return code;
169
170
216k
    code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
171
216k
    return code;
172
216k
}
173
174
static int pdfi_set_Tc(pdf_context *ctx, double Tc)
175
1.10M
{
176
1.10M
    return gs_settextspacing(ctx->pgs, Tc);
177
1.10M
}
178
179
int pdfi_Tc(pdf_context *ctx)
180
1.12M
{
181
1.12M
    int code;
182
1.12M
    double d;
183
184
1.12M
    code = pdfi_destack_real(ctx, &d);
185
1.12M
    if (code < 0)
186
23.6k
        return code;
187
188
1.10M
    return pdfi_set_Tc(ctx, d);
189
1.12M
}
190
191
int pdfi_Td(pdf_context *ctx)
192
2.89M
{
193
2.89M
    int code;
194
2.89M
    double Txy[2];
195
2.89M
    gs_matrix m, mat;
196
197
2.89M
    code = pdfi_destack_reals(ctx, Txy, 2);
198
2.89M
    if (code < 0)
199
35.5k
        return code;
200
201
2.86M
    gs_make_identity(&m);
202
203
2.86M
    m.tx = Txy[0];
204
2.86M
    m.ty = Txy[1];
205
206
2.86M
    if (ctx->text.BlockDepth == 0) {
207
12.8k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL);
208
209
12.8k
        gs_make_identity(&mat);
210
12.8k
        code = gs_settextmatrix(ctx->pgs, &mat);
211
12.8k
        if (code < 0)
212
0
            return code;
213
214
12.8k
        code = gs_settextlinematrix(ctx->pgs, &mat);
215
12.8k
        if (code < 0)
216
0
            return code;
217
12.8k
    }
218
219
2.86M
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
220
2.86M
    if (code < 0)
221
0
        return code;
222
223
2.86M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
224
2.86M
    if (code < 0)
225
0
        return code;
226
227
2.86M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
228
2.86M
}
229
230
int pdfi_TD(pdf_context *ctx)
231
549k
{
232
549k
    int code;
233
549k
    double Txy[2];
234
549k
    gs_matrix m, mat;
235
236
549k
    gs_make_identity(&m);
237
238
549k
    code = pdfi_destack_reals(ctx, Txy, 2);
239
549k
    if (code < 0)
240
3.93k
        return code;
241
242
546k
    m.tx = Txy[0];
243
546k
    m.ty = Txy[1];
244
245
546k
    if (ctx->text.BlockDepth == 0) {
246
1.14k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
247
248
1.14k
        gs_make_identity(&mat);
249
1.14k
        code = gs_settextmatrix(ctx->pgs, &mat);
250
1.14k
        if (code < 0)
251
0
            return code;
252
253
1.14k
        code = gs_settextlinematrix(ctx->pgs, &mat);
254
1.14k
        if (code < 0)
255
0
            return code;
256
1.14k
    }
257
258
546k
    code = pdfi_set_TL(ctx, m.ty * 1.0f);
259
546k
    if (code < 0)
260
0
        return code;
261
262
546k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
263
546k
    if (code < 0)
264
0
        return code;
265
266
546k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
267
546k
    if (code < 0)
268
0
        return code;
269
270
546k
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
271
546k
}
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
26.8M
{
284
26.8M
    pdf_font *current_font = NULL;
285
26.8M
    gs_matrix mat;
286
26.8M
    float *x_widths = NULL, *y_widths = NULL, width;
287
26.8M
    double Tw = 0, Tc = 0;
288
26.8M
    int i, code;
289
290
26.8M
    text->data.chars = NULL;
291
26.8M
    text->x_widths = NULL;
292
26.8M
    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
26.8M
    current_font = pdfi_get_current_pdf_font(ctx);
300
301
26.8M
    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
26.8M
    Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
309
310
26.8M
    if (current_font->pdfi_font_type == e_pdf_font_type1 ||
311
8.41M
        current_font->pdfi_font_type == e_pdf_font_cff ||
312
6.07M
        current_font->pdfi_font_type == e_pdf_font_type3 ||
313
6.01M
        current_font->pdfi_font_type == e_pdf_font_cff ||
314
6.01M
        current_font->pdfi_font_type == e_pdf_font_truetype ||
315
2.25M
        current_font->pdfi_font_type == e_pdf_font_microtype ||
316
2.25M
        current_font->pdfi_font_type == e_pdf_font_type0)
317
26.8M
    {
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
26.8M
        if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
322
10.3M
            text->operation = TEXT_RETURN_WIDTH;
323
10.3M
            if (Tc != 0) {
324
2.00M
                text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
325
2.00M
                if (current_font->pfont && current_font->pfont->WMode == 0) {
326
2.00M
                    text->delta_all.x = Tc;
327
2.00M
                    text->delta_all.y = 0;
328
2.00M
                } else {
329
2
                    text->delta_all.y = Tc;
330
2
                    text->delta_all.x = 0;
331
2
                }
332
2.00M
            }
333
16.4M
        } else {
334
16.4M
            gs_point pt;
335
336
16.4M
            x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
337
16.4M
            y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
338
16.4M
            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
16.4M
            memset(x_widths, 0x00, s->length * sizeof(float));
344
16.4M
            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
16.4M
            mat = current_font->pfont->FontMatrix;
350
351
71.9M
            for (i = 0;i < s->length; i++) {
352
                /* Get the width (in unscaled text units) */
353
55.4M
                if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
354
153k
                    width = current_font->MissingWidth;
355
55.2M
                else
356
55.2M
                    width = current_font->Widths[s->data[i] - current_font->FirstChar];
357
                /* And convert the width into an appropriate value for the current environment */
358
55.4M
                gs_distance_transform(width, 0, &mat, &pt);
359
55.4M
                x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
360
                /* Add any Tc value */
361
55.4M
                x_widths[i] += Tc;
362
55.4M
            }
363
16.4M
            text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
364
16.4M
            text->x_widths = x_widths;
365
16.4M
            text->y_widths = y_widths;
366
16.4M
            text->widths_size = s->length * 2;
367
16.4M
        }
368
369
26.8M
        Tw = gs_currentwordspacing(ctx->pgs);
370
26.8M
        if (Tw != 0) {
371
1.28M
            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
1.28M
            if (current_font->pfont && current_font->pfont->WMode == 0) {
377
1.28M
                text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
378
1.28M
                text->delta_space.y = 0;
379
1.28M
            } else {
380
0
                text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
381
0
                text->delta_space.x = 0;
382
0
            }
383
1.28M
            text->space.s_char = 0x20;
384
1.28M
        }
385
386
26.8M
        if (current_font->pdfi_font_type == e_pdf_font_type3) {
387
54.3k
            text->operation |= TEXT_FROM_CHARS;
388
54.3k
            text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
389
54.3k
            if (!text->data.chars) {
390
0
                code = gs_note_error(gs_error_VMerror);
391
0
                goto text_params_error;
392
0
            }
393
394
272k
            for (i = 0; i < s->length; i++) {
395
217k
                ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
396
217k
            }
397
54.3k
        }
398
26.7M
        else {
399
26.7M
            text->operation |= TEXT_FROM_BYTES;
400
26.7M
            text->data.bytes = (const byte *)s->data;
401
26.7M
        }
402
26.8M
        text->size = s->length;
403
26.8M
    }
404
46
    else {
405
46
        code = gs_note_error(gs_error_invalidfont);
406
46
        goto text_params_error;
407
46
    }
408
409
26.8M
    return 0;
410
411
46
text_params_error:
412
46
    gs_free_object(ctx->memory, x_widths, "X widths array for text");
413
46
    text->x_widths = NULL;
414
46
    gs_free_object(ctx->memory, y_widths, "Y widths array for text");
415
46
    text->y_widths = NULL;
416
46
    gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars");
417
46
    text->data.chars = NULL;
418
46
    return code;
419
26.8M
}
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
26.5M
{
428
26.5M
    int code = 0;
429
26.5M
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
430
431
26.5M
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
432
26.5M
    if (code >= 0) {
433
26.4M
        penum->single_byte_space = true;
434
26.4M
        saved_penum = ctx->text.current_enum;
435
26.4M
        ctx->text.current_enum = penum;
436
26.4M
        code = gs_text_process(penum);
437
26.4M
        gs_text_release(ctx->pgs, penum, "pdfi_Tj");
438
26.4M
        ctx->text.current_enum = saved_penum;
439
26.4M
    }
440
26.5M
    return code;
441
26.5M
}
442
443
/* Mode 0 - fill */
444
static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
445
23.1M
{
446
23.1M
    int code;
447
448
    /* just draw the text */
449
23.1M
    text->operation |= TEXT_DO_DRAW;
450
451
23.1M
    code = pdfi_show_simple(ctx, text);
452
453
23.1M
    text->operation &= ~TEXT_DO_DRAW;
454
23.1M
    return code;
455
23.1M
}
456
457
/* Mode 1 - stroke */
458
static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text)
459
2.06k
{
460
2.06k
    int code;
461
2.06k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
462
2.06k
    gs_point end_point, initial_point;
463
2.06k
    gx_device *dev = ctx->pgs->device;
464
2.06k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
465
466
2.06k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
467
468
    /* Capture the current position */
469
2.06k
    code = gs_currentpoint(ctx->pgs, &initial_point);
470
2.06k
    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
2.06k
    code = pdfi_gsave(ctx);
478
2.06k
    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
2.06k
    code = gs_newpath(ctx->pgs);
485
2.06k
    if (code < 0)
486
0
        goto Tr1_error;
487
2.06k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
488
2.06k
    if (code < 0)
489
0
        goto Tr1_error;
490
491
    /* Don't draw the text, create a path suitable for stroking */
492
2.06k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
493
494
    /* Run the text methods to create the path */
495
2.06k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
496
2.06k
    if (code < 0)
497
30
        goto Tr1_error;
498
499
2.03k
    penum->single_byte_space = true;
500
2.03k
    saved_penum = ctx->text.current_enum;
501
2.03k
    ctx->text.current_enum = penum;
502
2.03k
    code = gs_text_process(penum);
503
2.03k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
504
2.03k
    ctx->text.current_enum = saved_penum;
505
2.03k
    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
2.03k
    code = gs_currentpoint(ctx->pgs, &end_point);
514
2.03k
    if (code < 0)
515
0
        goto Tr1_error;
516
517
2.03k
    if (talphabits != galphabits)
518
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
519
520
    /* Change to the current stroking colour */
521
2.03k
    gs_swapcolors_quick(ctx->pgs);
522
    /* Finally, stroke the actual path */
523
2.03k
    code = gs_stroke(ctx->pgs);
524
    /* Switch back to the non-stroke colour */
525
2.03k
    gs_swapcolors_quick(ctx->pgs);
526
527
2.03k
    if (talphabits != galphabits)
528
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
529
530
2.06k
Tr1_error:
531
    /* And grestore back to where we started */
532
2.06k
    (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
2.06k
    if (code >= 0)
536
2.03k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
537
538
2.06k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
539
2.06k
    return code;
540
2.03k
}
541
542
/* Mode 2 - fill then stroke */
543
static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text)
544
12.5k
{
545
12.5k
    int code, restart = 0;
546
12.5k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
547
12.5k
    gs_point end_point, initial_point;
548
12.5k
    gx_device *dev = ctx->pgs->device;
549
12.5k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
550
551
12.5k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
552
553
    /* Capture the current position */
554
12.5k
    code = gs_currentpoint(ctx->pgs, &initial_point);
555
12.5k
    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
12.5k
    code = pdfi_gsave(ctx);
563
12.5k
    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
12.5k
    code = gs_newpath(ctx->pgs);
570
12.5k
    if (code < 0)
571
0
        goto Tr1_error;
572
12.5k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
573
12.5k
    if (code < 0)
574
0
        goto Tr1_error;
575
576
    /* Don't draw the text, create a path suitable for stroking */
577
12.5k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
578
579
    /* Run the text methods to create the path */
580
12.5k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
581
12.5k
    if (code < 0)
582
3
        goto Tr1_error;
583
584
12.5k
    penum->single_byte_space = true;
585
12.5k
    saved_penum = ctx->text.current_enum;
586
12.5k
    ctx->text.current_enum = penum;
587
12.5k
    code = gs_text_process(penum);
588
12.5k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
589
12.5k
    ctx->text.current_enum = saved_penum;
590
12.5k
    if (code < 0)
591
66
        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
12.4k
    code = gs_currentpoint(ctx->pgs, &end_point);
599
12.4k
    if (code < 0)
600
0
        goto Tr1_error;
601
12.4k
    if (talphabits != galphabits)
602
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
603
12.4k
    code = gs_fillstroke(ctx->pgs, &restart);
604
12.4k
    if (talphabits != galphabits)
605
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
606
607
12.5k
Tr1_error:
608
    /* And grestore back to where we started */
609
12.5k
    (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
12.5k
    if (code >= 0)
613
12.4k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
614
615
12.5k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
616
12.5k
    return code;
617
12.4k
}
618
619
static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
620
312k
{
621
312k
    int code;
622
312k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
623
624
    /* Don't draw the text */
625
312k
    text->operation |= TEXT_DO_NONE | TEXT_RENDER_MODE_3;
626
627
    /* Run the text methods to create the path */
628
312k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
629
312k
    if (code < 0)
630
0
        return code;
631
632
312k
    penum->single_byte_space = true;
633
312k
    saved_penum = ctx->text.current_enum;
634
312k
    ctx->text.current_enum = penum;
635
312k
    code = gs_text_process(penum);
636
312k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
637
312k
    ctx->text.current_enum = saved_penum;
638
639
312k
    return code;
640
312k
}
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.95k
{
647
2.95k
    int code;
648
2.95k
    gs_point initial_point;
649
650
    /* Capture the current position */
651
2.95k
    code = gs_currentpoint(ctx->pgs, &initial_point);
652
2.95k
    if (code < 0)
653
0
        return code;
654
655
2.95k
    ctx->text.TextClip = true;
656
    /* First fill the text */
657
2.95k
    code = pdfi_show_Tr_0(ctx, text);
658
2.95k
    if (code < 0)
659
0
        return code;
660
661
    /* Return the current point to the initial point */
662
2.95k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
663
2.95k
    if (code >= 0)
664
        /* And add the text to the acumulated path for clipping */
665
2.95k
        code = pdfi_show_Tr_7(ctx, text);
666
667
2.95k
    return code;
668
2.95k
}
669
670
static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
671
423
{
672
423
    int code;
673
423
    gs_point initial_point;
674
675
    /* Capture the current position */
676
423
    code = gs_currentpoint(ctx->pgs, &initial_point);
677
423
    if (code < 0)
678
0
        return code;
679
680
423
    ctx->text.TextClip = true;
681
    /* First stroke the text */
682
423
    code = pdfi_show_Tr_1(ctx, text);
683
423
    if (code < 0)
684
0
        return code;
685
686
    /* Return the current point to the initial point */
687
423
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
688
423
    if (code >= 0)
689
        /* And add the text to the acumulated path for clipping */
690
423
        code = pdfi_show_Tr_7(ctx, text);
691
692
423
    return code;
693
423
}
694
695
static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
696
382
{
697
382
    int code;
698
382
    gs_point initial_point;
699
700
    /* Capture the current position */
701
382
    code = gs_currentpoint(ctx->pgs, &initial_point);
702
382
    if (code < 0)
703
0
        return code;
704
705
382
    ctx->text.TextClip = true;
706
    /* First fill and stroke the text */
707
382
    code = pdfi_show_Tr_2(ctx, text);
708
382
    if (code < 0)
709
0
        return code;
710
711
    /* Return the current point to the initial point */
712
382
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
713
382
    if (code >= 0)
714
        /* And add the text to the acumulated path for clipping */
715
382
        code = pdfi_show_Tr_7(ctx, text);
716
717
382
    return code;
718
382
}
719
720
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
721
3.91k
{
722
3.91k
    int code;
723
3.91k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
724
725
3.91k
    ctx->text.TextClip = true;
726
    /* Don't draw the text, create a path suitable for filling */
727
3.91k
    text->operation |= TEXT_DO_TRUE_CHARPATH;
728
729
    /* Run the text methods to create the path */
730
3.91k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
731
3.91k
    if (code < 0)
732
0
        goto Tr7_error;
733
734
3.91k
    penum->single_byte_space = true;
735
3.91k
    saved_penum = ctx->text.current_enum;
736
3.91k
    ctx->text.current_enum = penum;
737
3.91k
    code = gs_text_process(penum);
738
3.91k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
739
3.91k
    ctx->text.current_enum = saved_penum;
740
741
3.91k
Tr7_error:
742
3.91k
    text->operation &= ~TEXT_DO_TRUE_CHARPATH;
743
3.91k
    return code;
744
3.91k
}
745
746
static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
747
3.34M
{
748
3.34M
    int Trmode = 0, code;
749
3.34M
    pdf_font *current_font = NULL;
750
3.34M
    gs_point initial_point;
751
752
3.34M
    current_font = pdfi_get_current_pdf_font(ctx);
753
3.34M
    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
3.34M
    code = gs_currentpoint(ctx->pgs, &initial_point);
758
759
3.34M
    Trmode = gs_currenttextrenderingmode(ctx->pgs);
760
3.34M
    if (Trmode == 3) {
761
15.5k
        if (current_font->pdfi_font_type == e_pdf_font_type3)
762
2
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
763
15.5k
        else
764
15.5k
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
765
15.5k
    }
766
3.33M
    else
767
3.33M
        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
3.34M
    if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) {
781
1.19k
        gs_swapcolors_quick(ctx->pgs);
782
783
1.19k
        code = gx_set_dev_color(ctx->pgs);
784
1.19k
        if (code != 0)
785
0
            return code;
786
787
1.19k
        code = gs_gstate_color_load(ctx->pgs);
788
1.19k
        if (code < 0)
789
0
            return code;
790
791
1.19k
        gs_swapcolors_quick(ctx->pgs);
792
1.19k
    }
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
3.34M
    if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) {
800
18
        gx_device *dev = gs_currentdevice_inline(ctx->pgs);
801
802
18
        ctx->text.TextClip = true;
803
18
        dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1);
804
18
  }
805
806
3.34M
    code = pdfi_show_simple(ctx, text);
807
3.34M
    return code;
808
3.34M
}
809
810
static int pdfi_show(pdf_context *ctx, pdf_string *s)
811
26.8M
{
812
26.8M
    int code = 0, SavedTextDepth = 0;
813
26.8M
    int code1 = 0;
814
26.8M
    gs_text_params_t text;
815
26.8M
    pdf_font *current_font = NULL;
816
26.8M
    int Trmode = 0;
817
26.8M
    int initial_gsave_level = ctx->pgs->level;
818
26.8M
    pdfi_trans_state_t state;
819
26.8M
    int outside_text_block = 0;
820
821
26.8M
    if (ctx->text.BlockDepth == 0) {
822
91.8k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
823
91.8k
        outside_text_block = 1;
824
91.8k
    }
825
826
26.8M
    if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
827
26.8M
     || 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
17.1k
         return 0;
833
17.1k
    }
834
835
26.8M
    current_font = pdfi_get_current_pdf_font(ctx);
836
26.8M
    if (current_font == NULL)
837
60
        return_error(gs_error_invalidfont);
838
839
26.8M
    code = pdfi_show_set_params(ctx, s, &text);
840
26.8M
    if (code < 0)
841
46
        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
26.8M
    SavedTextDepth = ctx->text.BlockDepth;
851
26.8M
    ctx->text.BlockDepth = 0;
852
26.8M
    code = pdfi_trans_setup_text(ctx, &state, true);
853
26.8M
    ctx->text.BlockDepth = SavedTextDepth;
854
855
26.8M
    if (code >= 0) {
856
26.8M
        if (ctx->device_state.preserve_tr_mode) {
857
3.34M
        code = pdfi_show_Tr_preserve(ctx, &text);
858
23.4M
        } else {
859
23.4M
            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
23.4M
            if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
865
354
                Trmode = 0;
866
867
23.4M
            switch(Trmode) {
868
23.1M
            case 0:
869
23.1M
                code = pdfi_show_Tr_0(ctx, &text);
870
23.1M
                break;
871
1.63k
            case 1:
872
1.63k
                code = pdfi_show_Tr_1(ctx, &text);
873
1.63k
                break;
874
12.1k
            case 2:
875
12.1k
                code = pdfi_show_Tr_2(ctx, &text);
876
12.1k
                break;
877
312k
            case 3:
878
312k
                code = pdfi_show_Tr_3(ctx, &text);
879
312k
                break;
880
2.95k
            case 4:
881
2.95k
                code = pdfi_show_Tr_4(ctx, &text);
882
2.95k
                break;
883
423
            case 5:
884
423
                code = pdfi_show_Tr_5(ctx, &text);
885
423
                break;
886
382
            case 6:
887
382
                code = pdfi_show_Tr_6(ctx, &text);
888
382
                break;
889
148
            case 7:
890
148
                code = pdfi_show_Tr_7(ctx, &text);
891
148
                break;
892
0
            default:
893
0
                break;
894
23.4M
            }
895
23.4M
        }
896
26.8M
        code1 = pdfi_trans_teardown_text(ctx, &state);
897
26.8M
        if (code == 0)
898
26.7M
            code = code1;
899
26.8M
    }
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
26.8M
    while(ctx->pgs->level > initial_gsave_level)
910
540
        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
26.8M
    if (outside_text_block) {
917
86.9k
        code1 = do_ET(ctx);
918
86.9k
        if (code == 0)
919
82.3k
            code = code1;
920
86.9k
    }
921
922
26.8M
show_error:
923
26.8M
    if ((void *)text.data.chars != (void *)s->data)
924
54.3k
        gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
925
926
26.8M
    gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
927
26.8M
    gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
928
26.8M
    return code;
929
26.8M
}
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
26.7k
{
936
26.7k
    int code = 0;
937
26.7k
    gx_device_bbox *bbdev;
938
26.7k
    pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
939
26.7k
    gs_matrix tmpmat, Trm, Trm_ctm;
940
26.7k
    gs_point cppt, startpt;
941
942
26.7k
    if (current_font == NULL)
943
0
        return_error(gs_error_invalidfont);
944
945
946
26.7k
    for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
947
948
26.7k
    if (ctx->devbbox == NULL) {
949
295
        bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
950
295
        if (bbdev == NULL)
951
0
           return_error(gs_error_VMerror);
952
295
        gx_device_bbox_init(bbdev, NULL, ctx->memory);
953
295
        ctx->devbbox = (gx_device *)bbdev;
954
295
        rc_increment(ctx->devbbox);
955
295
    }
956
26.4k
    else {
957
26.4k
        bbdev = (gx_device_bbox *)ctx->devbbox;
958
26.4k
    }
959
26.7k
    gx_device_retain((gx_device *)bbdev, true);
960
26.7k
    gx_device_bbox_set_white_opaque(bbdev, true);
961
962
26.7k
    code = pdfi_gsave(ctx);
963
26.7k
    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
26.7k
    gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
969
970
26.7k
    code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
971
26.7k
    if (code < 0)
972
0
        goto out;
973
974
26.7k
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
975
26.7k
    Trm.xy = 0;
976
26.7k
    Trm.yx = 0;
977
26.7k
    Trm.yy = ctx->pgs->PDFfontsize;
978
26.7k
    Trm.tx = 0;
979
26.7k
    Trm.ty = ctx->pgs->textrise;
980
981
26.7k
    memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
982
    /* We want to avoid any translations unrelated to the extents of the text */
983
26.7k
    tmpmat.tx = tmpmat.ty = 0;
984
26.7k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm);
985
986
26.7k
    memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
987
    /* As above */
988
26.7k
    tmpmat.tx = tmpmat.ty = 0;
989
26.7k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
990
26.7k
    gs_setmatrix(ctx->pgs, &Trm_ctm);
991
992
26.7k
    gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
993
994
26.7k
    code = pdfi_gs_setgray(ctx, 1.0);
995
26.7k
    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
26.7k
    startpt.x = ctx->pgs->PDFfontsize;
1005
26.7k
    startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1006
26.7k
    code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1007
26.7k
    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
26.7k
    ctx->text.BlockDepth++;
1012
26.7k
    code = pdfi_show(ctx, s);
1013
    /* And an ET */
1014
26.7k
    ctx->text.BlockDepth--;
1015
26.7k
    if (code < 0)
1016
0
        goto out;
1017
1018
26.7k
    code = gx_device_bbox_bbox(bbdev, bboxout);
1019
26.7k
    if (code < 0)
1020
0
        goto out;
1021
1022
26.7k
    bboxout->q.x -= bboxout->p.x;
1023
26.7k
    bboxout->q.y -= bboxout->p.y;
1024
26.7k
    bboxout->p.x = bboxout->p.y = 0;
1025
1026
26.7k
    code = gs_currentpoint(ctx->pgs, &cppt);
1027
26.7k
    if (code >= 0) {
1028
26.7k
        code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1029
26.7k
        if (code < 0)
1030
0
            goto out;
1031
26.7k
        advance_width->x = ctx->pgs->current_point.x - startpt.x;
1032
26.7k
        advance_width->y = ctx->pgs->current_point.y - startpt.y;
1033
26.7k
        code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1034
26.7k
    }
1035
26.7k
out:
1036
26.7k
    pdfi_grestore(ctx);
1037
26.7k
    (void)gs_closedevice((gx_device *)bbdev);
1038
26.7k
    gx_device_retain((gx_device *)bbdev, false);
1039
1040
26.7k
    return code;
1041
26.7k
}
1042
1043
int pdfi_Tj(pdf_context *ctx)
1044
3.20M
{
1045
3.20M
    int code = 0;
1046
3.20M
    pdf_string *s = NULL;
1047
3.20M
    gs_matrix saved, Trm;
1048
3.20M
    gs_point initial_point, current_point, pt;
1049
3.20M
    double linewidth = ctx->pgs->line_params.half_width;
1050
3.20M
    int initial_point_valid;
1051
1052
3.20M
    if (pdfi_count_stack(ctx) < 1)
1053
47.6k
        return_error(gs_error_stackunderflow);
1054
1055
3.15M
    if (pdfi_oc_is_off(ctx))
1056
3.49k
        goto exit;
1057
1058
3.14M
    s = (pdf_string *)ctx->stack_top[-1];
1059
3.14M
    if (pdfi_type_of(s) != PDF_STRING) {
1060
81.0k
        pdfi_pop(ctx, 1);
1061
81.0k
        return_error(gs_error_typecheck);
1062
81.0k
    }
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
3.06M
    pdfi_countup(s);
1069
3.06M
    pdfi_pop(ctx, 1);
1070
1071
    /* Save the CTM for later restoration */
1072
3.06M
    saved = ctm_only(ctx->pgs);
1073
3.06M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1074
1075
3.06M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1076
3.06M
    Trm.xy = 0;
1077
3.06M
    Trm.yx = 0;
1078
3.06M
    Trm.yy = ctx->pgs->PDFfontsize;
1079
3.06M
    Trm.tx = 0;
1080
3.06M
    Trm.ty = ctx->pgs->textrise;
1081
1082
3.06M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1083
3.06M
    if (code < 0)
1084
0
        goto exit;
1085
1086
3.06M
    if (!ctx->device_state.preserve_tr_mode) {
1087
2.77M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1088
2.77M
        if (code < 0)
1089
69.4k
            goto exit;
1090
2.70M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1091
2.70M
    } 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
290k
        gs_matrix devmatrix, matrix;
1098
290k
        gx_device *device = gs_currentdevice(ctx->pgs);
1099
1100
290k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1101
290k
        devmatrix.xy = 0;
1102
290k
        devmatrix.yx = 0;
1103
290k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1104
290k
        devmatrix.tx = 0;
1105
290k
        devmatrix.ty = 0;
1106
1107
290k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1108
290k
        if (code < 0)
1109
0
            goto exit;
1110
1111
290k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1112
290k
        if (code < 0)
1113
0
            goto exit;
1114
290k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1115
290k
    }
1116
1117
2.99M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1118
2.99M
    if (code < 0)
1119
0
        goto exit;
1120
1121
2.99M
    code = gs_setmatrix(ctx->pgs, &Trm);
1122
2.99M
    if (code < 0)
1123
0
        goto exit;
1124
1125
2.99M
    code = gs_moveto(ctx->pgs, 0, 0);
1126
2.99M
    if (code < 0)
1127
0
        goto Tj_error;
1128
1129
2.99M
    code = pdfi_show(ctx, s);
1130
2.99M
    if (code < 0)
1131
58.5k
        goto Tj_error;
1132
1133
2.94M
    ctx->pgs->line_params.half_width = linewidth;
1134
    /* Update the Text matrix with the current point, for the next operation
1135
     */
1136
2.94M
    (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1137
2.94M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1138
2.94M
    Trm.xy = 0;
1139
2.94M
    Trm.yx = 0;
1140
2.94M
    Trm.yy = ctx->pgs->PDFfontsize;
1141
2.94M
    Trm.tx = 0;
1142
2.94M
    Trm.ty = 0;
1143
2.94M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1144
2.94M
    if (code < 0)
1145
0
        goto Tj_error;
1146
1147
2.94M
    code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1148
2.94M
    if (code < 0)
1149
0
        goto Tj_error;
1150
2.94M
    ctx->pgs->textmatrix.tx += pt.x;
1151
2.94M
    ctx->pgs->textmatrix.ty += pt.y;
1152
1153
2.99M
Tj_error:
1154
    /* Restore the CTM to the saved value */
1155
2.99M
    (void)gs_setmatrix(ctx->pgs, &saved);
1156
    /* And restore the currentpoint */
1157
2.99M
    if (initial_point_valid)
1158
2.87M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1159
122k
    else
1160
122k
        code = gs_newpath(ctx->pgs);
1161
    /* And the line width */
1162
2.99M
    ctx->pgs->line_params.half_width = linewidth;
1163
1164
3.07M
 exit:
1165
3.07M
    pdfi_countdown(s);
1166
3.07M
    return code;
1167
2.99M
}
1168
1169
int pdfi_TJ(pdf_context *ctx)
1170
3.50M
{
1171
3.50M
    int code = 0, i;
1172
3.50M
    pdf_array *a = NULL;
1173
3.50M
    pdf_obj *o = NULL;
1174
3.50M
    double dx = 0;
1175
3.50M
    gs_point pt;
1176
3.50M
    gs_matrix saved, Trm;
1177
3.50M
    gs_point initial_point, current_point;
1178
3.50M
    double linewidth = ctx->pgs->line_params.half_width;
1179
3.50M
    pdf_font *current_font = NULL;
1180
3.50M
    int initial_point_valid;
1181
1182
3.50M
    current_font = pdfi_get_current_pdf_font(ctx);
1183
3.50M
    if (current_font == NULL)
1184
8.37k
        return_error(gs_error_invalidfont);
1185
1186
3.49M
    if (ctx->text.BlockDepth == 0) {
1187
48.3k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1188
48.3k
    }
1189
1190
3.49M
    if (pdfi_count_stack(ctx) < 1)
1191
20.6k
        return_error(gs_error_stackunderflow);
1192
1193
3.47M
    if (pdfi_oc_is_off(ctx))
1194
1.07k
        goto exit;
1195
1196
3.47M
    a = (pdf_array *)ctx->stack_top[-1];
1197
3.47M
    if (pdfi_type_of(a) != PDF_ARRAY) {
1198
63.8k
        pdfi_pop(ctx, 1);
1199
63.8k
        return gs_note_error(gs_error_typecheck);
1200
63.8k
    }
1201
3.41M
    pdfi_countup(a);
1202
3.41M
    pdfi_pop(ctx, 1);
1203
1204
    /* Save the CTM for later restoration */
1205
3.41M
    saved = ctm_only(ctx->pgs);
1206
3.41M
    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
3.41M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1212
3.41M
    Trm.xy = 0;
1213
3.41M
    Trm.yx = 0;
1214
3.41M
    Trm.yy = ctx->pgs->PDFfontsize;
1215
3.41M
    Trm.tx = 0;
1216
3.41M
    Trm.ty = ctx->pgs->textrise;
1217
1218
3.41M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1219
3.41M
    if (code < 0)
1220
0
        goto exit;
1221
1222
3.41M
    if (!ctx->device_state.preserve_tr_mode) {
1223
3.04M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1224
3.04M
        if (code < 0)
1225
13.9k
            goto exit;
1226
3.03M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1227
3.03M
    } 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
367k
        gs_matrix devmatrix, matrix;
1234
367k
        gx_device *device = gs_currentdevice(ctx->pgs);
1235
1236
367k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1237
367k
        devmatrix.xy = 0;
1238
367k
        devmatrix.yx = 0;
1239
367k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1240
367k
        devmatrix.tx = 0;
1241
367k
        devmatrix.ty = 0;
1242
1243
367k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1244
367k
        if (code < 0)
1245
0
            goto exit;
1246
1247
367k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1248
367k
        if (code < 0)
1249
0
            goto exit;
1250
367k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1251
367k
    }
1252
1253
3.39M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1254
3.39M
    if (code < 0)
1255
0
        goto exit;
1256
3.39M
    code = gs_setmatrix(ctx->pgs, &Trm);
1257
3.39M
    if (code < 0)
1258
0
        goto TJ_error;
1259
1260
3.39M
    code = gs_moveto(ctx->pgs, 0, 0);
1261
3.39M
    if (code < 0)
1262
0
        goto TJ_error;
1263
1264
47.7M
    for (i = 0; i < pdfi_array_size(a); i++) {
1265
44.3M
        code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1266
44.3M
        if (code < 0)
1267
1
            goto TJ_error;
1268
1269
44.3M
        if (pdfi_type_of(o) == PDF_STRING)
1270
23.8M
            code = pdfi_show(ctx, (pdf_string *)o);
1271
20.5M
        else {
1272
20.5M
            code = pdfi_obj_to_real(ctx, o, &dx);
1273
20.5M
            if (code < 0)
1274
134
                goto TJ_error;
1275
20.5M
            dx /= -1000;
1276
20.5M
            if (current_font->pfont && current_font->pfont->WMode == 0)
1277
20.5M
                code = gs_rmoveto(ctx->pgs, dx, 0);
1278
8.99k
            else
1279
8.99k
                code = gs_rmoveto(ctx->pgs, 0, dx);
1280
20.5M
        }
1281
44.3M
        pdfi_countdown(o);
1282
44.3M
        o = NULL;
1283
44.3M
        if (code < 0)
1284
55.7k
            goto TJ_error;
1285
44.3M
    }
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
3.34M
    if (ctx->text.BlockDepth > 0) {
1291
        /* Update the Text matrix with the current point, for the next operation
1292
         */
1293
3.32M
        (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1294
3.32M
        Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1295
3.32M
        Trm.xy = 0;
1296
3.32M
        Trm.yx = 0;
1297
3.32M
        Trm.yy = ctx->pgs->PDFfontsize;
1298
3.32M
        Trm.tx = 0;
1299
3.32M
        Trm.ty = 0;
1300
3.32M
        code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1301
3.32M
        if (code < 0)
1302
0
            goto TJ_error;
1303
1304
3.32M
        code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1305
3.32M
        if (code < 0)
1306
0
            goto TJ_error;
1307
3.32M
        ctx->pgs->textmatrix.tx += pt.x;
1308
3.32M
        ctx->pgs->textmatrix.ty += pt.y;
1309
3.32M
    }
1310
3.39M
TJ_error:
1311
3.39M
    pdfi_countdown(o);
1312
    /* Restore the CTM to the saved value */
1313
3.39M
    (void)gs_setmatrix(ctx->pgs, &saved);
1314
    /* And restore the currentpoint */
1315
3.39M
    if (initial_point_valid)
1316
3.35M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1317
42.8k
    else
1318
42.8k
        code = gs_newpath(ctx->pgs);
1319
    /* And the line width */
1320
3.39M
    ctx->pgs->line_params.half_width = linewidth;
1321
1322
3.41M
 exit:
1323
3.41M
    pdfi_countdown(a);
1324
3.41M
    return code;
1325
3.39M
}
1326
1327
static int pdfi_set_TL(pdf_context *ctx, double TL)
1328
576k
{
1329
576k
    return gs_settextleading(ctx->pgs, TL);
1330
576k
}
1331
1332
int pdfi_TL(pdf_context *ctx)
1333
30.8k
{
1334
30.8k
    int code;
1335
30.8k
    double d;
1336
1337
30.8k
    code = pdfi_destack_real(ctx, &d);
1338
30.8k
    if (code < 0)
1339
53
        return code;
1340
1341
30.8k
    return pdfi_set_TL(ctx, -d);
1342
30.8k
}
1343
1344
int pdfi_Tm(pdf_context *ctx)
1345
3.08M
{
1346
3.08M
    int code;
1347
3.08M
    float m[6];
1348
3.08M
    gs_matrix mat;
1349
1350
3.08M
    code = pdfi_destack_floats(ctx, m, 6);
1351
3.08M
    if (code < 0)
1352
267k
        return code;
1353
1354
2.81M
    if (ctx->text.BlockDepth == 0) {
1355
47.3k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1356
1357
47.3k
        gs_make_identity(&mat);
1358
47.3k
        code = gs_settextmatrix(ctx->pgs, &mat);
1359
47.3k
        if (code < 0)
1360
0
            return code;
1361
1362
47.3k
        code = gs_settextlinematrix(ctx->pgs, &mat);
1363
47.3k
        if (code < 0)
1364
0
            return code;
1365
47.3k
    }
1366
1367
2.81M
    if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1368
34.1k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1369
1370
2.81M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1371
2.81M
    if (code < 0)
1372
0
        return code;
1373
1374
2.81M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1375
2.81M
}
1376
1377
int pdfi_Tr(pdf_context *ctx)
1378
992k
{
1379
992k
    int code;
1380
992k
    int64_t mode;
1381
1382
992k
    code = pdfi_destack_int(ctx, &mode);
1383
992k
    if (code < 0)
1384
13.4k
        return code;
1385
1386
978k
    if (mode < 0 || mode > 7)
1387
7.98k
        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
970k
    if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1394
327
        pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1395
1396
970k
    if (ctx->device_state.preserve_tr_mode) {
1397
66.3k
        gs_settextrenderingmode(ctx->pgs, mode);
1398
66.3k
    } else
1399
904k
    {
1400
904k
        gs_point initial_point;
1401
1402
904k
        if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1403
1.13k
            gs_settextrenderingmode(ctx->pgs, mode);
1404
            /* Capture the current position */
1405
1.13k
            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
1.13k
            gs_newpath(ctx->pgs);
1410
1.13k
            if (code >= 0)
1411
899
                gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1412
1.13k
            code = 0;
1413
903k
        } 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
699
            code = pdfi_ET(ctx);
1420
699
            if (code < 0)
1421
0
                return code;
1422
699
            gs_settextrenderingmode(ctx->pgs, mode);
1423
699
            code = pdfi_BT(ctx);
1424
699
            if (code < 0)
1425
0
                return code;
1426
699
        }
1427
902k
        else
1428
902k
            gs_settextrenderingmode(ctx->pgs, mode);
1429
904k
    }
1430
970k
    return code;
1431
970k
}
1432
1433
static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1434
5.91k
{
1435
5.91k
    return gs_settextrise(ctx->pgs, Ts);
1436
5.91k
}
1437
1438
int pdfi_Ts(pdf_context *ctx)
1439
6.49k
{
1440
6.49k
    int code;
1441
6.49k
    double d;
1442
1443
6.49k
    code = pdfi_destack_real(ctx, &d);
1444
6.49k
    if (code < 0)
1445
572
        return code;
1446
1447
5.91k
    return pdfi_set_Ts(ctx, d);
1448
6.49k
}
1449
1450
static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1451
220k
{
1452
220k
    return gs_setwordspacing(ctx->pgs, Tw);
1453
220k
}
1454
1455
int pdfi_Tw(pdf_context *ctx)
1456
221k
{
1457
221k
    int code;
1458
221k
    double d;
1459
1460
221k
    code = pdfi_destack_real(ctx, &d);
1461
221k
    if (code < 0)
1462
1.52k
        return code;
1463
1464
219k
    return pdfi_set_Tw(ctx, d);
1465
221k
}
1466
1467
int pdfi_Tz(pdf_context *ctx)
1468
295k
{
1469
295k
    int code;
1470
295k
    double d;
1471
1472
295k
    code = pdfi_destack_real(ctx, &d);
1473
295k
    if (code < 0)
1474
3.56k
        return code;
1475
1476
291k
    return gs_settexthscaling(ctx->pgs, d);
1477
295k
}
1478
1479
int pdfi_singlequote(pdf_context *ctx)
1480
23.1k
{
1481
23.1k
    int code;
1482
1483
23.1k
    if (ctx->text.BlockDepth == 0) {
1484
8.95k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1485
8.95k
    }
1486
1487
23.1k
    code = pdfi_T_star(ctx);
1488
23.1k
    if (code < 0)
1489
0
        return code;
1490
1491
23.1k
    return pdfi_Tj(ctx);
1492
23.1k
}
1493
1494
int pdfi_doublequote(pdf_context *ctx)
1495
9.07k
{
1496
9.07k
    int code;
1497
9.07k
    double Tw, Tc;
1498
1499
9.07k
    if (ctx->text.BlockDepth == 0) {
1500
7.56k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1501
7.56k
    }
1502
1503
9.07k
    if (pdfi_count_stack(ctx) < 3) {
1504
7.92k
        pdfi_clearstack(ctx);
1505
7.92k
        return_error(gs_error_stackunderflow);
1506
7.92k
    }
1507
1508
1.15k
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1509
333
        pdfi_pop(ctx, 3);
1510
333
        return gs_note_error(gs_error_typecheck);
1511
333
    }
1512
1513
824
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1514
824
    if (code < 0)
1515
4
        goto error;
1516
820
    code = pdfi_set_Tc(ctx, Tc);
1517
820
    if (code < 0)
1518
0
        goto error;
1519
1520
820
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1521
820
    if (code < 0)
1522
21
        goto error;
1523
799
    code = pdfi_set_Tw(ctx, Tw);
1524
799
    if (code < 0)
1525
0
        goto error;
1526
1527
799
    code = pdfi_T_star(ctx);
1528
799
    if (code < 0)
1529
0
        goto error;
1530
1531
799
    code = pdfi_Tj(ctx);
1532
    /* Tj pops one off the stack for us, leaving us 2 to go. */
1533
799
    pdfi_pop(ctx, 2);
1534
799
    return code;
1535
1536
25
error:
1537
25
    pdfi_pop(ctx, 3);
1538
25
    return code;
1539
799
}