Coverage Report

Created: 2025-08-28 07:06

/src/ghostpdl/pdf/pdf_text.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2018-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/* Text operations for the PDF interpreter */
17
18
#include "pdf_int.h"
19
#include "pdf_array.h"
20
#include "pdf_text.h"
21
#include "pdf_image.h"
22
#include "pdf_colour.h"
23
#include "pdf_stack.h"
24
#include "pdf_font.h"
25
#include "pdf_font_types.h"
26
#include "pdf_gstate.h"
27
#include "pdf_trans.h"
28
#include "pdf_optcontent.h"
29
30
#include "gsstate.h"
31
#include "gsmatrix.h"
32
#include "gdevbbox.h"
33
#include "gspaint.h"        /* For gs_fill() and friends */
34
#include "gscoord.h"        /* For gs_setmatrix() */
35
#include "gxdevsop.h"               /* For special ops */
36
37
static int pdfi_set_TL(pdf_context *ctx, double TL);
38
39
int pdfi_BT(pdf_context *ctx)
40
3.10M
{
41
3.10M
    int code;
42
3.10M
    gs_matrix m;
43
3.10M
    bool illegal_BT = false;
44
45
3.10M
    if (ctx->text.BlockDepth != 0) {
46
250k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_NESTEDTEXTBLOCK, "pdfi_BT", NULL);
47
250k
        illegal_BT = true;
48
250k
    }
49
50
3.10M
    gs_make_identity(&m);
51
3.10M
    code = gs_settextmatrix(ctx->pgs, &m);
52
3.10M
    if (code < 0)
53
0
        return code;
54
55
3.10M
    code = gs_settextlinematrix(ctx->pgs, &m);
56
3.10M
    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.10M
    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
46
        gs_newpath(ctx->pgs);
67
46
    }
68
69
3.10M
    ctx->text.initial_current_point_valid = ctx->pgs->current_point_valid;
70
3.10M
    if (!ctx->pgs->current_point_valid)
71
3.04M
        code = gs_moveto(ctx->pgs, 0, 0);
72
73
3.10M
    ctx->text.BlockDepth++;
74
75
3.10M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs) && !illegal_BT)
76
958k
        gs_begin_transparency_text_group(ctx->pgs);
77
78
3.10M
    return code;
79
3.10M
}
80
81
static int do_ET(pdf_context *ctx)
82
3.05M
{
83
3.05M
    int code = 0;
84
3.05M
    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.05M
    if (ctx->text.BlockDepth == 0 && gs_currenttextrenderingmode(ctx->pgs) >= 4) {
97
338
        gs_point initial_point;
98
99
338
        if  (!ctx->device_state.preserve_tr_mode) {
100
328
            ctx->text.TextClip = false;
101
            /* Capture the current position */
102
328
            code = gs_currentpoint(ctx->pgs, &initial_point);
103
328
            if (code >= 0 || code == gs_error_nocurrentpoint) {
104
328
                gs_point adjust;
105
328
                bool nocurrentpoint = code >= 0 ? false : true;
106
107
328
                gs_currentfilladjust(ctx->pgs, &adjust);
108
328
                code = gs_setfilladjust(ctx->pgs, (double)0.0, (double)0.0);
109
328
                if (code < 0)
110
0
                    return code;
111
112
328
                code = gs_clip(ctx->pgs);
113
328
                if (code >= 0)
114
328
                    copy = gx_cpath_alloc_shared(ctx->pgs->clip_path, ctx->memory, "save clip path");
115
116
328
                code = gs_setfilladjust(ctx->pgs, adjust.x, adjust.y);
117
328
                if (code < 0)
118
0
                    return code;
119
120
328
                if (copy != NULL)
121
328
                    (void)gx_cpath_assign_free(ctx->pgs->clip_path, copy);
122
123
328
                if (nocurrentpoint == false)
124
323
                    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
125
328
            }
126
328
        }
127
338
    }
128
3.05M
    if (ctx->page.has_transparency && gs_currenttextknockout(ctx->pgs))
129
993k
        gs_end_transparency_text_group(ctx->pgs);
130
131
3.05M
    if (!ctx->text.initial_current_point_valid)
132
3.01M
        gs_newpath(ctx->pgs);
133
3.05M
    return code;
134
3.05M
}
135
136
int pdfi_ET(pdf_context *ctx)
137
3.04M
{
138
3.04M
    if (ctx->text.BlockDepth == 0) {
139
63.4k
        int code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_ETNOTEXTBLOCK, "pdfi_ET", NULL);
140
63.4k
        return code;
141
63.4k
    }
142
143
2.97M
    ctx->text.BlockDepth--;
144
145
2.97M
    return do_ET(ctx);
146
3.04M
}
147
148
int pdfi_T_star(pdf_context *ctx)
149
198k
{
150
198k
    int code;
151
198k
    gs_matrix m, mat;
152
153
198k
    if (ctx->text.BlockDepth == 0) {
154
23.5k
        int code;
155
23.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
23.5k
    }
158
159
198k
    gs_make_identity(&m);
160
198k
    m.ty += ctx->pgs->textleading;
161
162
198k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
163
198k
    if (code < 0)
164
0
        return code;
165
166
198k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
167
198k
    if (code < 0)
168
0
        return code;
169
170
198k
    code = gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
171
198k
    return code;
172
198k
}
173
174
static int pdfi_set_Tc(pdf_context *ctx, double Tc)
175
997k
{
176
997k
    return gs_settextspacing(ctx->pgs, Tc);
177
997k
}
178
179
int pdfi_Tc(pdf_context *ctx)
180
1.01M
{
181
1.01M
    int code;
182
1.01M
    double d;
183
184
1.01M
    code = pdfi_destack_real(ctx, &d);
185
1.01M
    if (code < 0)
186
19.7k
        return code;
187
188
996k
    return pdfi_set_Tc(ctx, d);
189
1.01M
}
190
191
int pdfi_Td(pdf_context *ctx)
192
2.38M
{
193
2.38M
    int code;
194
2.38M
    double Txy[2];
195
2.38M
    gs_matrix m, mat;
196
197
2.38M
    code = pdfi_destack_reals(ctx, Txy, 2);
198
2.38M
    if (code < 0)
199
29.9k
        return code;
200
201
2.35M
    gs_make_identity(&m);
202
203
2.35M
    m.tx = Txy[0];
204
2.35M
    m.ty = Txy[1];
205
206
2.35M
    if (ctx->text.BlockDepth == 0) {
207
11.0k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Td", NULL);
208
209
11.0k
        gs_make_identity(&mat);
210
11.0k
        code = gs_settextmatrix(ctx->pgs, &mat);
211
11.0k
        if (code < 0)
212
0
            return code;
213
214
11.0k
        code = gs_settextlinematrix(ctx->pgs, &mat);
215
11.0k
        if (code < 0)
216
0
            return code;
217
11.0k
    }
218
219
2.35M
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
220
2.35M
    if (code < 0)
221
0
        return code;
222
223
2.35M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
224
2.35M
    if (code < 0)
225
0
        return code;
226
227
2.35M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
228
2.35M
}
229
230
int pdfi_TD(pdf_context *ctx)
231
501k
{
232
501k
    int code;
233
501k
    double Txy[2];
234
501k
    gs_matrix m, mat;
235
236
501k
    gs_make_identity(&m);
237
238
501k
    code = pdfi_destack_reals(ctx, Txy, 2);
239
501k
    if (code < 0)
240
3.35k
        return code;
241
242
497k
    m.tx = Txy[0];
243
497k
    m.ty = Txy[1];
244
245
497k
    if (ctx->text.BlockDepth == 0) {
246
982
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TD", NULL);
247
248
982
        gs_make_identity(&mat);
249
982
        code = gs_settextmatrix(ctx->pgs, &mat);
250
982
        if (code < 0)
251
0
            return code;
252
253
982
        code = gs_settextlinematrix(ctx->pgs, &mat);
254
982
        if (code < 0)
255
0
            return code;
256
982
    }
257
258
497k
    code = pdfi_set_TL(ctx, m.ty * 1.0f);
259
497k
    if (code < 0)
260
0
        return code;
261
262
497k
    code = gs_matrix_multiply(&m, &ctx->pgs->textlinematrix, &mat);
263
497k
    if (code < 0)
264
0
        return code;
265
266
497k
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&mat);
267
497k
    if (code < 0)
268
0
        return code;
269
270
497k
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&mat);
271
497k
}
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
21.4M
{
284
21.4M
    pdf_font *current_font = NULL;
285
21.4M
    gs_matrix mat;
286
21.4M
    float *x_widths = NULL, *y_widths = NULL, width;
287
21.4M
    double Tw = 0, Tc = 0;
288
21.4M
    int i, code;
289
290
21.4M
    text->data.chars = NULL;
291
21.4M
    text->x_widths = NULL;
292
21.4M
    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
21.4M
    current_font = pdfi_get_current_pdf_font(ctx);
300
301
21.4M
    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
21.4M
    Tc = gs_currenttextspacing(ctx->pgs) / ctx->pgs->PDFfontsize;
309
310
21.4M
    if (current_font->pdfi_font_type == e_pdf_font_type1 ||
311
21.4M
        current_font->pdfi_font_type == e_pdf_font_cff ||
312
21.4M
        current_font->pdfi_font_type == e_pdf_font_type3 ||
313
21.4M
        current_font->pdfi_font_type == e_pdf_font_cff ||
314
21.4M
        current_font->pdfi_font_type == e_pdf_font_truetype ||
315
21.4M
        current_font->pdfi_font_type == e_pdf_font_microtype ||
316
21.4M
        current_font->pdfi_font_type == e_pdf_font_type0)
317
21.4M
    {
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
21.4M
        if (current_font->pdfi_font_type == e_pdf_font_type0 || current_font->Widths == NULL) {
322
8.90M
            text->operation = TEXT_RETURN_WIDTH;
323
8.90M
            if (Tc != 0) {
324
1.73M
                text->operation |= TEXT_ADD_TO_ALL_WIDTHS;
325
1.73M
                if (current_font->pfont && current_font->pfont->WMode == 0) {
326
1.73M
                    text->delta_all.x = Tc;
327
1.73M
                    text->delta_all.y = 0;
328
1.73M
                } else {
329
1
                    text->delta_all.y = Tc;
330
1
                    text->delta_all.x = 0;
331
1
                }
332
1.73M
            }
333
12.5M
        } else {
334
12.5M
            gs_point pt;
335
336
12.5M
            x_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "X widths array for text");
337
12.5M
            y_widths = (float *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(float), "Y widths array for text");
338
12.5M
            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
12.5M
            memset(x_widths, 0x00, s->length * sizeof(float));
344
12.5M
            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
12.5M
            mat = current_font->pfont->FontMatrix;
350
351
57.4M
            for (i = 0;i < s->length; i++) {
352
                /* Get the width (in unscaled text units) */
353
44.9M
                if (s->data[i] < current_font->FirstChar || s->data[i] > current_font->LastChar)
354
130k
                    width = current_font->MissingWidth;
355
44.8M
                else
356
44.8M
                    width = current_font->Widths[s->data[i] - current_font->FirstChar];
357
                /* And convert the width into an appropriate value for the current environment */
358
44.9M
                gs_distance_transform(width, 0, &mat, &pt);
359
44.9M
                x_widths[i] = hypot(pt.x, pt.y) * (width < 0 ? -1.0 : 1.0);
360
                /* Add any Tc value */
361
44.9M
                x_widths[i] += Tc;
362
44.9M
            }
363
12.5M
            text->operation = TEXT_RETURN_WIDTH | TEXT_REPLACE_WIDTHS;
364
12.5M
            text->x_widths = x_widths;
365
12.5M
            text->y_widths = y_widths;
366
12.5M
            text->widths_size = s->length * 2;
367
12.5M
        }
368
369
21.4M
        Tw = gs_currentwordspacing(ctx->pgs);
370
21.4M
        if (Tw != 0) {
371
1.17M
            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.17M
            if (current_font->pfont && current_font->pfont->WMode == 0) {
377
1.17M
                text->delta_space.x = Tw / ctx->pgs->PDFfontsize;
378
1.17M
                text->delta_space.y = 0;
379
1.17M
            } else {
380
0
                text->delta_space.y = Tw / ctx->pgs->PDFfontsize;
381
0
                text->delta_space.x = 0;
382
0
            }
383
1.17M
            text->space.s_char = 0x20;
384
1.17M
        }
385
386
21.4M
        if (current_font->pdfi_font_type == e_pdf_font_type3) {
387
52.9k
            text->operation |= TEXT_FROM_CHARS;
388
52.9k
            text->data.chars = (const gs_char *)gs_alloc_bytes(ctx->memory, (size_t)s->length * sizeof(gs_char), "string gs_chars");
389
52.9k
            if (!text->data.chars) {
390
0
                code = gs_note_error(gs_error_VMerror);
391
0
                goto text_params_error;
392
0
            }
393
394
260k
            for (i = 0; i < s->length; i++) {
395
207k
                ((gs_char *)text->data.chars)[i] = (gs_char)s->data[i];
396
207k
            }
397
52.9k
        }
398
21.3M
        else {
399
21.3M
            text->operation |= TEXT_FROM_BYTES;
400
21.3M
            text->data.bytes = (const byte *)s->data;
401
21.3M
        }
402
21.4M
        text->size = s->length;
403
21.4M
    }
404
36
    else {
405
36
        code = gs_note_error(gs_error_invalidfont);
406
36
        goto text_params_error;
407
36
    }
408
409
21.4M
    return 0;
410
411
36
text_params_error:
412
36
    gs_free_object(ctx->memory, x_widths, "X widths array for text");
413
36
    text->x_widths = NULL;
414
36
    gs_free_object(ctx->memory, y_widths, "Y widths array for text");
415
36
    text->y_widths = NULL;
416
36
    gs_free_object(ctx->memory, (void *)text->data.chars, "string gs_chars");
417
36
    text->data.chars = NULL;
418
36
    return code;
419
21.4M
}
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
21.1M
{
428
21.1M
    int code = 0;
429
21.1M
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
430
431
21.1M
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
432
21.1M
    if (code >= 0) {
433
21.1M
        penum->single_byte_space = true;
434
21.1M
        saved_penum = ctx->text.current_enum;
435
21.1M
        ctx->text.current_enum = penum;
436
21.1M
        code = gs_text_process(penum);
437
21.1M
        gs_text_release(ctx->pgs, penum, "pdfi_Tj");
438
21.1M
        ctx->text.current_enum = saved_penum;
439
21.1M
    }
440
21.1M
    return code;
441
21.1M
}
442
443
/* Mode 0 - fill */
444
static int pdfi_show_Tr_0(pdf_context *ctx, gs_text_params_t *text)
445
18.3M
{
446
18.3M
    int code;
447
448
    /* just draw the text */
449
18.3M
    text->operation |= TEXT_DO_DRAW;
450
451
18.3M
    code = pdfi_show_simple(ctx, text);
452
453
18.3M
    text->operation &= ~TEXT_DO_DRAW;
454
18.3M
    return code;
455
18.3M
}
456
457
/* Mode 1 - stroke */
458
static int pdfi_show_Tr_1(pdf_context *ctx, gs_text_params_t *text)
459
1.64k
{
460
1.64k
    int code;
461
1.64k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
462
1.64k
    gs_point end_point, initial_point;
463
1.64k
    gx_device *dev = ctx->pgs->device;
464
1.64k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
465
466
1.64k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
467
468
    /* Capture the current position */
469
1.64k
    code = gs_currentpoint(ctx->pgs, &initial_point);
470
1.64k
    if (code < 0)
471
0
        return code;
472
473
    /* We don't want to disturb the current path, so do a gsave now
474
     * We will grestore back to this point after we have stroked the
475
     * text, which will leave any current path unchanged.
476
     */
477
1.64k
    code = pdfi_gsave(ctx);
478
1.64k
    if (code < 0)
479
0
        goto Tr1_error;
480
481
    /* Start a new path (so our stroke doesn't include any
482
     * already extant path in the graphics state)
483
     */
484
1.64k
    code = gs_newpath(ctx->pgs);
485
1.64k
    if (code < 0)
486
0
        goto Tr1_error;
487
1.64k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
488
1.64k
    if (code < 0)
489
0
        goto Tr1_error;
490
491
    /* Don't draw the text, create a path suitable for stroking */
492
1.64k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
493
494
    /* Run the text methods to create the path */
495
1.64k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
496
1.64k
    if (code < 0)
497
30
        goto Tr1_error;
498
499
1.61k
    penum->single_byte_space = true;
500
1.61k
    saved_penum = ctx->text.current_enum;
501
1.61k
    ctx->text.current_enum = penum;
502
1.61k
    code = gs_text_process(penum);
503
1.61k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
504
1.61k
    ctx->text.current_enum = saved_penum;
505
1.61k
    if (code < 0)
506
0
        goto Tr1_error;
507
508
    /* After a stroke operation there is no current point and we need
509
     * it to be set as if we had drawn the text. So capture it now
510
     * and we will append a 'move' to the current point when we have
511
     * finished the stroke.
512
     */
513
1.61k
    code = gs_currentpoint(ctx->pgs, &end_point);
514
1.61k
    if (code < 0)
515
0
        goto Tr1_error;
516
517
1.61k
    if (talphabits != galphabits)
518
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
519
520
    /* Change to the current stroking colour */
521
1.61k
    gs_swapcolors_quick(ctx->pgs);
522
    /* Finally, stroke the actual path */
523
1.61k
    code = gs_stroke(ctx->pgs);
524
    /* Switch back to the non-stroke colour */
525
1.61k
    gs_swapcolors_quick(ctx->pgs);
526
527
1.61k
    if (talphabits != galphabits)
528
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
529
530
1.64k
Tr1_error:
531
    /* And grestore back to where we started */
532
1.64k
    (void)pdfi_grestore(ctx);
533
    /* If everything went well, then move the current point to the
534
     * position we captured at the end of the path creation */
535
1.64k
    if (code >= 0)
536
1.61k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
537
538
1.64k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
539
1.64k
    return code;
540
1.61k
}
541
542
/* Mode 2 - fill then stroke */
543
static int pdfi_show_Tr_2(pdf_context *ctx, gs_text_params_t *text)
544
11.2k
{
545
11.2k
    int code, restart = 0;
546
11.2k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
547
11.2k
    gs_point end_point, initial_point;
548
11.2k
    gx_device *dev = ctx->pgs->device;
549
11.2k
    int galphabits = dev->color_info.anti_alias.graphics_bits, talphabits = dev->color_info.anti_alias.text_bits;
550
551
11.2k
    end_point.x = end_point.y = initial_point.x = initial_point.y = 0;
552
553
    /* Capture the current position */
554
11.2k
    code = gs_currentpoint(ctx->pgs, &initial_point);
555
11.2k
    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
11.2k
    code = pdfi_gsave(ctx);
563
11.2k
    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
11.2k
    code = gs_newpath(ctx->pgs);
570
11.2k
    if (code < 0)
571
0
        goto Tr1_error;
572
11.2k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
573
11.2k
    if (code < 0)
574
0
        goto Tr1_error;
575
576
    /* Don't draw the text, create a path suitable for stroking */
577
11.2k
    text->operation |= TEXT_DO_FALSE_CHARPATH;
578
579
    /* Run the text methods to create the path */
580
11.2k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
581
11.2k
    if (code < 0)
582
0
        goto Tr1_error;
583
584
11.2k
    penum->single_byte_space = true;
585
11.2k
    saved_penum = ctx->text.current_enum;
586
11.2k
    ctx->text.current_enum = penum;
587
11.2k
    code = gs_text_process(penum);
588
11.2k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
589
11.2k
    ctx->text.current_enum = saved_penum;
590
11.2k
    if (code < 0)
591
51
        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
11.2k
    code = gs_currentpoint(ctx->pgs, &end_point);
599
11.2k
    if (code < 0)
600
0
        goto Tr1_error;
601
11.2k
    if (talphabits != galphabits)
602
0
        dev->color_info.anti_alias.graphics_bits = talphabits;
603
11.2k
    code = gs_fillstroke(ctx->pgs, &restart);
604
11.2k
    if (talphabits != galphabits)
605
0
        dev->color_info.anti_alias.graphics_bits = galphabits;
606
607
11.2k
Tr1_error:
608
    /* And grestore back to where we started */
609
11.2k
    (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
11.2k
    if (code >= 0)
613
11.2k
        code = gs_moveto(ctx->pgs, end_point.x, end_point.y);
614
615
11.2k
    text->operation &= ~TEXT_DO_FALSE_CHARPATH;
616
11.2k
    return code;
617
11.2k
}
618
619
static int pdfi_show_Tr_3(pdf_context *ctx, gs_text_params_t *text)
620
260k
{
621
260k
    int code;
622
260k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
623
624
    /* Don't draw the text */
625
260k
    text->operation |= TEXT_DO_NONE | TEXT_RENDER_MODE_3;
626
627
    /* Run the text methods to create the path */
628
260k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
629
260k
    if (code < 0)
630
0
        return code;
631
632
260k
    penum->single_byte_space = true;
633
260k
    saved_penum = ctx->text.current_enum;
634
260k
    ctx->text.current_enum = penum;
635
260k
    code = gs_text_process(penum);
636
260k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
637
260k
    ctx->text.current_enum = saved_penum;
638
639
260k
    return code;
640
260k
}
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.62k
{
647
2.62k
    int code;
648
2.62k
    gs_point initial_point;
649
650
    /* Capture the current position */
651
2.62k
    code = gs_currentpoint(ctx->pgs, &initial_point);
652
2.62k
    if (code < 0)
653
0
        return code;
654
655
2.62k
    ctx->text.TextClip = true;
656
    /* First fill the text */
657
2.62k
    code = pdfi_show_Tr_0(ctx, text);
658
2.62k
    if (code < 0)
659
0
        return code;
660
661
    /* Return the current point to the initial point */
662
2.62k
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
663
2.62k
    if (code >= 0)
664
        /* And add the text to the acumulated path for clipping */
665
2.62k
        code = pdfi_show_Tr_7(ctx, text);
666
667
2.62k
    return code;
668
2.62k
}
669
670
static int pdfi_show_Tr_5(pdf_context *ctx, gs_text_params_t *text)
671
364
{
672
364
    int code;
673
364
    gs_point initial_point;
674
675
    /* Capture the current position */
676
364
    code = gs_currentpoint(ctx->pgs, &initial_point);
677
364
    if (code < 0)
678
0
        return code;
679
680
364
    ctx->text.TextClip = true;
681
    /* First stroke the text */
682
364
    code = pdfi_show_Tr_1(ctx, text);
683
364
    if (code < 0)
684
0
        return code;
685
686
    /* Return the current point to the initial point */
687
364
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
688
364
    if (code >= 0)
689
        /* And add the text to the acumulated path for clipping */
690
364
        code = pdfi_show_Tr_7(ctx, text);
691
692
364
    return code;
693
364
}
694
695
static int pdfi_show_Tr_6(pdf_context *ctx, gs_text_params_t *text)
696
309
{
697
309
    int code;
698
309
    gs_point initial_point;
699
700
    /* Capture the current position */
701
309
    code = gs_currentpoint(ctx->pgs, &initial_point);
702
309
    if (code < 0)
703
0
        return code;
704
705
309
    ctx->text.TextClip = true;
706
    /* First fill and stroke the text */
707
309
    code = pdfi_show_Tr_2(ctx, text);
708
309
    if (code < 0)
709
0
        return code;
710
711
    /* Return the current point to the initial point */
712
309
    code = gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
713
309
    if (code >= 0)
714
        /* And add the text to the acumulated path for clipping */
715
309
        code = pdfi_show_Tr_7(ctx, text);
716
717
309
    return code;
718
309
}
719
720
static int pdfi_show_Tr_7(pdf_context *ctx, gs_text_params_t *text)
721
3.41k
{
722
3.41k
    int code;
723
3.41k
    gs_text_enum_t *penum=NULL, *saved_penum=NULL;
724
725
3.41k
    ctx->text.TextClip = true;
726
    /* Don't draw the text, create a path suitable for filling */
727
3.41k
    text->operation |= TEXT_DO_TRUE_CHARPATH;
728
729
    /* Run the text methods to create the path */
730
3.41k
    code = gs_text_begin(ctx->pgs, text, ctx->memory, &penum);
731
3.41k
    if (code < 0)
732
0
        goto Tr7_error;
733
734
3.41k
    penum->single_byte_space = true;
735
3.41k
    saved_penum = ctx->text.current_enum;
736
3.41k
    ctx->text.current_enum = penum;
737
3.41k
    code = gs_text_process(penum);
738
3.41k
    gs_text_release(ctx->pgs, penum, "pdfi_Tj");
739
3.41k
    ctx->text.current_enum = saved_penum;
740
741
3.41k
Tr7_error:
742
3.41k
    text->operation &= ~TEXT_DO_TRUE_CHARPATH;
743
3.41k
    return code;
744
3.41k
}
745
746
static int pdfi_show_Tr_preserve(pdf_context *ctx, gs_text_params_t *text)
747
2.79M
{
748
2.79M
    int Trmode = 0, code;
749
2.79M
    pdf_font *current_font = NULL;
750
2.79M
    gs_point initial_point;
751
752
2.79M
    current_font = pdfi_get_current_pdf_font(ctx);
753
2.79M
    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
2.79M
    code = gs_currentpoint(ctx->pgs, &initial_point);
758
759
2.79M
    Trmode = gs_currenttextrenderingmode(ctx->pgs);
760
2.79M
    if (Trmode == 3) {
761
15.0k
        if (current_font->pdfi_font_type == e_pdf_font_type3)
762
1
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_CHARS | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
763
15.0k
        else
764
15.0k
            text->operation = TEXT_RETURN_WIDTH | TEXT_FROM_BYTES | TEXT_DO_NONE | TEXT_RENDER_MODE_3;
765
15.0k
    }
766
2.78M
    else
767
2.78M
        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
2.79M
    if (Trmode == 1 || Trmode == 2 || Trmode == 5 || Trmode == 6) {
781
997
        gs_swapcolors_quick(ctx->pgs);
782
783
997
        code = gx_set_dev_color(ctx->pgs);
784
997
        if (code != 0)
785
0
            return code;
786
787
997
        code = gs_gstate_color_load(ctx->pgs);
788
997
        if (code < 0)
789
0
            return code;
790
791
997
        gs_swapcolors_quick(ctx->pgs);
792
997
    }
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
2.79M
    if (Trmode >= 4 && current_font->pdfi_font_type != e_pdf_font_type3 && ctx->text.TextClip == 0) {
800
14
        gx_device *dev = gs_currentdevice_inline(ctx->pgs);
801
802
14
        ctx->text.TextClip = true;
803
14
        dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)ctx->pgs, 1);
804
14
  }
805
806
2.79M
    code = pdfi_show_simple(ctx, text);
807
2.79M
    return code;
808
2.79M
}
809
810
static int pdfi_show(pdf_context *ctx, pdf_string *s)
811
21.4M
{
812
21.4M
    int code = 0, SavedTextDepth = 0;
813
21.4M
    int code1 = 0;
814
21.4M
    gs_text_params_t text;
815
21.4M
    pdf_font *current_font = NULL;
816
21.4M
    int Trmode = 0;
817
21.4M
    int initial_gsave_level = ctx->pgs->level;
818
21.4M
    pdfi_trans_state_t state;
819
21.4M
    int outside_text_block = 0;
820
821
21.4M
    if (ctx->text.BlockDepth == 0) {
822
79.0k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_show", NULL);
823
79.0k
        outside_text_block = 1;
824
79.0k
    }
825
826
21.4M
    if (hypot(ctx->pgs->ctm.xx, ctx->pgs->ctm.xy) == 0.0
827
21.4M
     || 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
15.2k
         return 0;
833
15.2k
    }
834
835
21.4M
    current_font = pdfi_get_current_pdf_font(ctx);
836
21.4M
    if (current_font == NULL)
837
60
        return_error(gs_error_invalidfont);
838
839
21.4M
    code = pdfi_show_set_params(ctx, s, &text);
840
21.4M
    if (code < 0)
841
36
        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
21.4M
    SavedTextDepth = ctx->text.BlockDepth;
851
21.4M
    ctx->text.BlockDepth = 0;
852
21.4M
    code = pdfi_trans_setup_text(ctx, &state, true);
853
21.4M
    ctx->text.BlockDepth = SavedTextDepth;
854
855
21.4M
    if (code >= 0) {
856
21.4M
        if (ctx->device_state.preserve_tr_mode) {
857
2.79M
        code = pdfi_show_Tr_preserve(ctx, &text);
858
18.6M
        } else {
859
18.6M
            Trmode = gs_currenttextrenderingmode(ctx->pgs);
860
861
            /* The spec says that we ignore text rendering modes other than 0 for
862
             * type 3 fonts, but Acrobat also honours mode 3 (do nothing)
863
             */
864
18.6M
            if (current_font->pdfi_font_type == e_pdf_font_type3 && Trmode != 0 && Trmode != 3)
865
239
                Trmode = 0;
866
867
18.6M
            switch(Trmode) {
868
18.3M
            case 0:
869
18.3M
                code = pdfi_show_Tr_0(ctx, &text);
870
18.3M
                break;
871
1.27k
            case 1:
872
1.27k
                code = pdfi_show_Tr_1(ctx, &text);
873
1.27k
                break;
874
10.9k
            case 2:
875
10.9k
                code = pdfi_show_Tr_2(ctx, &text);
876
10.9k
                break;
877
260k
            case 3:
878
260k
                code = pdfi_show_Tr_3(ctx, &text);
879
260k
                break;
880
2.62k
            case 4:
881
2.62k
                code = pdfi_show_Tr_4(ctx, &text);
882
2.62k
                break;
883
364
            case 5:
884
364
                code = pdfi_show_Tr_5(ctx, &text);
885
364
                break;
886
309
            case 6:
887
309
                code = pdfi_show_Tr_6(ctx, &text);
888
309
                break;
889
113
            case 7:
890
113
                code = pdfi_show_Tr_7(ctx, &text);
891
113
                break;
892
0
            default:
893
0
                break;
894
18.6M
            }
895
18.6M
        }
896
21.4M
        code1 = pdfi_trans_teardown_text(ctx, &state);
897
21.4M
        if (code == 0)
898
21.3M
            code = code1;
899
21.4M
    }
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
21.4M
    while(ctx->pgs->level > initial_gsave_level)
910
348
        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
21.4M
    if (outside_text_block) {
917
74.3k
        code1 = do_ET(ctx);
918
74.3k
        if (code == 0)
919
70.4k
            code = code1;
920
74.3k
    }
921
922
21.4M
show_error:
923
21.4M
    if ((void *)text.data.chars != (void *)s->data)
924
53.0k
        gs_free_object(ctx->memory, (void *)text.data.chars, "string gs_chars");
925
926
21.4M
    gs_free_object(ctx->memory, (void *)text.x_widths, "Free X widths array on error");
927
21.4M
    gs_free_object(ctx->memory, (void *)text.y_widths, "Free Y widths array on error");
928
21.4M
    return code;
929
21.4M
}
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
25.2k
{
936
25.2k
    int code = 0;
937
25.2k
    gx_device_bbox *bbdev;
938
25.2k
    pdf_font *current_font = pdfi_get_current_pdf_font(ctx);
939
25.2k
    gs_matrix tmpmat, Trm, Trm_ctm;
940
25.2k
    gs_point cppt, startpt;
941
942
25.2k
    if (current_font == NULL)
943
0
        return_error(gs_error_invalidfont);
944
945
946
25.2k
    for_stroke = current_font->pdfi_font_type == e_pdf_font_type3 ? false : for_stroke;
947
948
25.2k
    if (ctx->devbbox == NULL) {
949
208
        bbdev = gs_alloc_struct_immovable(ctx->memory, gx_device_bbox, &st_device_bbox, "pdfi_string_bbox(bbdev)");
950
208
        if (bbdev == NULL)
951
0
           return_error(gs_error_VMerror);
952
208
        gx_device_bbox_init(bbdev, NULL, ctx->memory);
953
208
        ctx->devbbox = (gx_device *)bbdev;
954
208
        rc_increment(ctx->devbbox);
955
208
    }
956
24.9k
    else {
957
24.9k
        bbdev = (gx_device_bbox *)ctx->devbbox;
958
24.9k
    }
959
25.2k
    gx_device_retain((gx_device *)bbdev, true);
960
25.2k
    gx_device_bbox_set_white_opaque(bbdev, true);
961
962
25.2k
    code = pdfi_gsave(ctx);
963
25.2k
    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
25.2k
    gx_device_set_resolution((gx_device *)bbdev, 720.0, 720.0);
969
970
25.2k
    code = gs_setdevice_no_erase(ctx->pgs, (gx_device *)bbdev);
971
25.2k
    if (code < 0)
972
0
        goto out;
973
974
25.2k
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
975
25.2k
    Trm.xy = 0;
976
25.2k
    Trm.yx = 0;
977
25.2k
    Trm.yy = ctx->pgs->PDFfontsize;
978
25.2k
    Trm.tx = 0;
979
25.2k
    Trm.ty = ctx->pgs->textrise;
980
981
25.2k
    memcpy(&tmpmat, &ctx->pgs->textmatrix, sizeof(tmpmat));
982
    /* We want to avoid any translations unrelated to the extents of the text */
983
25.2k
    tmpmat.tx = tmpmat.ty = 0;
984
25.2k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm);
985
986
25.2k
    memcpy(&tmpmat, &ctm_only(ctx->pgs), sizeof(tmpmat));
987
    /* As above */
988
25.2k
    tmpmat.tx = tmpmat.ty = 0;
989
25.2k
    gs_matrix_multiply(&Trm, &tmpmat, &Trm_ctm);
990
25.2k
    gs_setmatrix(ctx->pgs, &Trm_ctm);
991
992
25.2k
    gs_settextrenderingmode(ctx->pgs, for_stroke ? 2 : 0);
993
994
25.2k
    code = pdfi_gs_setgray(ctx, 1.0);
995
25.2k
    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
25.2k
    startpt.x = ctx->pgs->PDFfontsize;
1005
25.2k
    startpt.y = ctx->pgs->PDFfontsize * 16.0 * (ctx->pgs->textrise >= 0 ? 1 : -ctx->pgs->textrise);
1006
25.2k
    code = gs_moveto(ctx->pgs, startpt.x, startpt.y);
1007
25.2k
    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
25.2k
    ctx->text.BlockDepth++;
1012
25.2k
    code = pdfi_show(ctx, s);
1013
    /* And an ET */
1014
25.2k
    ctx->text.BlockDepth--;
1015
25.2k
    if (code < 0)
1016
0
        goto out;
1017
1018
25.2k
    code = gx_device_bbox_bbox(bbdev, bboxout);
1019
25.2k
    if (code < 0)
1020
0
        goto out;
1021
1022
25.2k
    bboxout->q.x -= bboxout->p.x;
1023
25.2k
    bboxout->q.y -= bboxout->p.y;
1024
25.2k
    bboxout->p.x = bboxout->p.y = 0;
1025
1026
25.2k
    code = gs_currentpoint(ctx->pgs, &cppt);
1027
25.2k
    if (code >= 0) {
1028
25.2k
        code = gs_point_transform(startpt.x, startpt.y, &ctm_only(ctx->pgs), &startpt);
1029
25.2k
        if (code < 0)
1030
0
            goto out;
1031
25.2k
        advance_width->x = ctx->pgs->current_point.x - startpt.x;
1032
25.2k
        advance_width->y = ctx->pgs->current_point.y - startpt.y;
1033
25.2k
        code = gs_point_transform_inverse(advance_width->x, advance_width->y, &tmpmat, advance_width);
1034
25.2k
    }
1035
25.2k
out:
1036
25.2k
    pdfi_grestore(ctx);
1037
25.2k
    (void)gs_closedevice((gx_device *)bbdev);
1038
25.2k
    gx_device_retain((gx_device *)bbdev, false);
1039
1040
25.2k
    return code;
1041
25.2k
}
1042
1043
int pdfi_Tj(pdf_context *ctx)
1044
2.77M
{
1045
2.77M
    int code = 0;
1046
2.77M
    pdf_string *s = NULL;
1047
2.77M
    gs_matrix saved, Trm;
1048
2.77M
    gs_point initial_point, current_point, pt;
1049
2.77M
    double linewidth = ctx->pgs->line_params.half_width;
1050
2.77M
    int initial_point_valid;
1051
1052
2.77M
    if (pdfi_count_stack(ctx) < 1)
1053
40.5k
        return_error(gs_error_stackunderflow);
1054
1055
2.73M
    if (pdfi_oc_is_off(ctx))
1056
2.71k
        goto exit;
1057
1058
2.73M
    s = (pdf_string *)ctx->stack_top[-1];
1059
2.73M
    if (pdfi_type_of(s) != PDF_STRING) {
1060
61.4k
        pdfi_pop(ctx, 1);
1061
61.4k
        return_error(gs_error_typecheck);
1062
61.4k
    }
1063
1064
    /* We can't rely on the stack reference because an error during
1065
       the text operation (i.e. retrieving objects for glyph metrics
1066
       may cause the stack to be cleared.
1067
     */
1068
2.67M
    pdfi_countup(s);
1069
2.67M
    pdfi_pop(ctx, 1);
1070
1071
    /* Save the CTM for later restoration */
1072
2.67M
    saved = ctm_only(ctx->pgs);
1073
2.67M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1074
1075
2.67M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1076
2.67M
    Trm.xy = 0;
1077
2.67M
    Trm.yx = 0;
1078
2.67M
    Trm.yy = ctx->pgs->PDFfontsize;
1079
2.67M
    Trm.tx = 0;
1080
2.67M
    Trm.ty = ctx->pgs->textrise;
1081
1082
2.67M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1083
2.67M
    if (code < 0)
1084
0
        goto exit;
1085
1086
2.67M
    if (!ctx->device_state.preserve_tr_mode) {
1087
2.40M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1088
2.40M
        if (code < 0)
1089
57.4k
            goto exit;
1090
2.35M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1091
2.35M
    } 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
263k
        gs_matrix devmatrix, matrix;
1098
263k
        gx_device *device = gs_currentdevice(ctx->pgs);
1099
1100
263k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1101
263k
        devmatrix.xy = 0;
1102
263k
        devmatrix.yx = 0;
1103
263k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1104
263k
        devmatrix.tx = 0;
1105
263k
        devmatrix.ty = 0;
1106
1107
263k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1108
263k
        if (code < 0)
1109
0
            goto exit;
1110
1111
263k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1112
263k
        if (code < 0)
1113
0
            goto exit;
1114
263k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1115
263k
    }
1116
1117
2.61M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1118
2.61M
    if (code < 0)
1119
0
        goto exit;
1120
1121
2.61M
    code = gs_setmatrix(ctx->pgs, &Trm);
1122
2.61M
    if (code < 0)
1123
0
        goto exit;
1124
1125
2.61M
    code = gs_moveto(ctx->pgs, 0, 0);
1126
2.61M
    if (code < 0)
1127
0
        goto Tj_error;
1128
1129
2.61M
    code = pdfi_show(ctx, s);
1130
2.61M
    if (code < 0)
1131
54.9k
        goto Tj_error;
1132
1133
2.56M
    ctx->pgs->line_params.half_width = linewidth;
1134
    /* Update the Text matrix with the current point, for the next operation
1135
     */
1136
2.56M
    (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1137
2.56M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1138
2.56M
    Trm.xy = 0;
1139
2.56M
    Trm.yx = 0;
1140
2.56M
    Trm.yy = ctx->pgs->PDFfontsize;
1141
2.56M
    Trm.tx = 0;
1142
2.56M
    Trm.ty = 0;
1143
2.56M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1144
2.56M
    if (code < 0)
1145
0
        goto Tj_error;
1146
1147
2.56M
    code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1148
2.56M
    if (code < 0)
1149
0
        goto Tj_error;
1150
2.56M
    ctx->pgs->textmatrix.tx += pt.x;
1151
2.56M
    ctx->pgs->textmatrix.ty += pt.y;
1152
1153
2.61M
Tj_error:
1154
    /* Restore the CTM to the saved value */
1155
2.61M
    (void)gs_setmatrix(ctx->pgs, &saved);
1156
    /* And restore the currentpoint */
1157
2.61M
    if (initial_point_valid)
1158
2.50M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1159
106k
    else
1160
106k
        code = gs_newpath(ctx->pgs);
1161
    /* And the line width */
1162
2.61M
    ctx->pgs->line_params.half_width = linewidth;
1163
1164
2.67M
 exit:
1165
2.67M
    pdfi_countdown(s);
1166
2.67M
    return code;
1167
2.61M
}
1168
1169
int pdfi_TJ(pdf_context *ctx)
1170
2.94M
{
1171
2.94M
    int code = 0, i;
1172
2.94M
    pdf_array *a = NULL;
1173
2.94M
    pdf_obj *o = NULL;
1174
2.94M
    double dx = 0;
1175
2.94M
    gs_point pt;
1176
2.94M
    gs_matrix saved, Trm;
1177
2.94M
    gs_point initial_point, current_point;
1178
2.94M
    double linewidth = ctx->pgs->line_params.half_width;
1179
2.94M
    pdf_font *current_font = NULL;
1180
2.94M
    int initial_point_valid;
1181
1182
2.94M
    current_font = pdfi_get_current_pdf_font(ctx);
1183
2.94M
    if (current_font == NULL)
1184
7.57k
        return_error(gs_error_invalidfont);
1185
1186
2.93M
    if (ctx->text.BlockDepth == 0) {
1187
41.7k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_TJ", NULL);
1188
41.7k
    }
1189
1190
2.93M
    if (pdfi_count_stack(ctx) < 1)
1191
17.3k
        return_error(gs_error_stackunderflow);
1192
1193
2.91M
    if (pdfi_oc_is_off(ctx))
1194
872
        goto exit;
1195
1196
2.91M
    a = (pdf_array *)ctx->stack_top[-1];
1197
2.91M
    if (pdfi_type_of(a) != PDF_ARRAY) {
1198
56.0k
        pdfi_pop(ctx, 1);
1199
56.0k
        return gs_note_error(gs_error_typecheck);
1200
56.0k
    }
1201
2.86M
    pdfi_countup(a);
1202
2.86M
    pdfi_pop(ctx, 1);
1203
1204
    /* Save the CTM for later restoration */
1205
2.86M
    saved = ctm_only(ctx->pgs);
1206
2.86M
    initial_point_valid = (gs_currentpoint(ctx->pgs, &initial_point) >= 0);
1207
1208
    /* Calculate the text rendering matrix, see section 1.7 PDF Reference
1209
     * page 409, section 5.3.3 Text Space details.
1210
     */
1211
2.86M
    Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1212
2.86M
    Trm.xy = 0;
1213
2.86M
    Trm.yx = 0;
1214
2.86M
    Trm.yy = ctx->pgs->PDFfontsize;
1215
2.86M
    Trm.tx = 0;
1216
2.86M
    Trm.ty = ctx->pgs->textrise;
1217
1218
2.86M
    code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1219
2.86M
    if (code < 0)
1220
0
        goto exit;
1221
1222
2.86M
    if (!ctx->device_state.preserve_tr_mode) {
1223
2.55M
        code = gs_distance_transform_inverse(ctx->pgs->line_params.half_width, 0, &Trm, &pt);
1224
2.55M
        if (code < 0)
1225
12.0k
            goto exit;
1226
2.53M
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1227
2.53M
    } 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
309k
        gs_matrix devmatrix, matrix;
1234
309k
        gx_device *device = gs_currentdevice(ctx->pgs);
1235
1236
309k
        devmatrix.xx = 72.0 / device->HWResolution[0];
1237
309k
        devmatrix.xy = 0;
1238
309k
        devmatrix.yx = 0;
1239
309k
        devmatrix.yy = 72.0 / device->HWResolution[1];
1240
309k
        devmatrix.tx = 0;
1241
309k
        devmatrix.ty = 0;
1242
1243
309k
        code = gs_matrix_multiply(&saved, &devmatrix, &matrix);
1244
309k
        if (code < 0)
1245
0
            goto exit;
1246
1247
309k
        code = gs_distance_transform(ctx->pgs->line_params.half_width, 0, &matrix, &pt);
1248
309k
        if (code < 0)
1249
0
            goto exit;
1250
309k
        ctx->pgs->line_params.half_width = sqrt((pt.x * pt.x) + (pt.y * pt.y));
1251
309k
    }
1252
1253
2.84M
    code = gs_matrix_multiply(&Trm, &ctm_only(ctx->pgs), &Trm);
1254
2.84M
    if (code < 0)
1255
0
        goto exit;
1256
2.84M
    code = gs_setmatrix(ctx->pgs, &Trm);
1257
2.84M
    if (code < 0)
1258
0
        goto TJ_error;
1259
1260
2.84M
    code = gs_moveto(ctx->pgs, 0, 0);
1261
2.84M
    if (code < 0)
1262
0
        goto TJ_error;
1263
1264
37.7M
    for (i = 0; i < pdfi_array_size(a); i++) {
1265
34.9M
        code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1266
34.9M
        if (code < 0)
1267
1
            goto TJ_error;
1268
1269
34.9M
        if (pdfi_type_of(o) == PDF_STRING)
1270
18.8M
            code = pdfi_show(ctx, (pdf_string *)o);
1271
16.0M
        else {
1272
16.0M
            code = pdfi_obj_to_real(ctx, o, &dx);
1273
16.0M
            if (code < 0)
1274
128
                goto TJ_error;
1275
16.0M
            dx /= -1000;
1276
16.0M
            if (current_font->pfont && current_font->pfont->WMode == 0)
1277
16.0M
                code = gs_rmoveto(ctx->pgs, dx, 0);
1278
7.16k
            else
1279
7.16k
                code = gs_rmoveto(ctx->pgs, 0, dx);
1280
16.0M
        }
1281
34.9M
        pdfi_countdown(o);
1282
34.9M
        o = NULL;
1283
34.9M
        if (code < 0)
1284
49.2k
            goto TJ_error;
1285
34.9M
    }
1286
1287
    /* We don't want to update the Text matrix if we aren't in a text block, there
1288
     * is no point because the matrix isn't persistent between text blocks.
1289
     */
1290
2.79M
    if (ctx->text.BlockDepth > 0) {
1291
        /* Update the Text matrix with the current point, for the next operation
1292
         */
1293
2.78M
        (void)gs_currentpoint(ctx->pgs, &current_point); /* Always valid */
1294
2.78M
        Trm.xx = ctx->pgs->PDFfontsize * (ctx->pgs->texthscaling / 100);
1295
2.78M
        Trm.xy = 0;
1296
2.78M
        Trm.yx = 0;
1297
2.78M
        Trm.yy = ctx->pgs->PDFfontsize;
1298
2.78M
        Trm.tx = 0;
1299
2.78M
        Trm.ty = 0;
1300
2.78M
        code = gs_matrix_multiply(&Trm, &ctx->pgs->textmatrix, &Trm);
1301
2.78M
        if (code < 0)
1302
0
            goto TJ_error;
1303
1304
2.78M
        code = gs_distance_transform(current_point.x, current_point.y, &Trm, &pt);
1305
2.78M
        if (code < 0)
1306
0
            goto TJ_error;
1307
2.78M
        ctx->pgs->textmatrix.tx += pt.x;
1308
2.78M
        ctx->pgs->textmatrix.ty += pt.y;
1309
2.78M
    }
1310
2.84M
TJ_error:
1311
2.84M
    pdfi_countdown(o);
1312
    /* Restore the CTM to the saved value */
1313
2.84M
    (void)gs_setmatrix(ctx->pgs, &saved);
1314
    /* And restore the currentpoint */
1315
2.84M
    if (initial_point_valid)
1316
2.81M
        (void)gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1317
36.9k
    else
1318
36.9k
        code = gs_newpath(ctx->pgs);
1319
    /* And the line width */
1320
2.84M
    ctx->pgs->line_params.half_width = linewidth;
1321
1322
2.86M
 exit:
1323
2.86M
    pdfi_countdown(a);
1324
2.86M
    return code;
1325
2.84M
}
1326
1327
static int pdfi_set_TL(pdf_context *ctx, double TL)
1328
524k
{
1329
524k
    return gs_settextleading(ctx->pgs, TL);
1330
524k
}
1331
1332
int pdfi_TL(pdf_context *ctx)
1333
26.7k
{
1334
26.7k
    int code;
1335
26.7k
    double d;
1336
1337
26.7k
    code = pdfi_destack_real(ctx, &d);
1338
26.7k
    if (code < 0)
1339
50
        return code;
1340
1341
26.7k
    return pdfi_set_TL(ctx, -d);
1342
26.7k
}
1343
1344
int pdfi_Tm(pdf_context *ctx)
1345
2.67M
{
1346
2.67M
    int code;
1347
2.67M
    float m[6];
1348
2.67M
    gs_matrix mat;
1349
1350
2.67M
    code = pdfi_destack_floats(ctx, m, 6);
1351
2.67M
    if (code < 0)
1352
229k
        return code;
1353
1354
2.44M
    if (ctx->text.BlockDepth == 0) {
1355
38.4k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_Tm", NULL);
1356
1357
38.4k
        gs_make_identity(&mat);
1358
38.4k
        code = gs_settextmatrix(ctx->pgs, &mat);
1359
38.4k
        if (code < 0)
1360
0
            return code;
1361
1362
38.4k
        code = gs_settextlinematrix(ctx->pgs, &mat);
1363
38.4k
        if (code < 0)
1364
0
            return code;
1365
38.4k
    }
1366
1367
2.44M
    if (hypot(m[0], m[1]) == 0.0 || hypot(m[3], m[2]) == 0.0)
1368
27.6k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_DEGENERATETM, "pdfi_Tm", NULL);
1369
1370
2.44M
    code = gs_settextmatrix(ctx->pgs, (gs_matrix *)&m);
1371
2.44M
    if (code < 0)
1372
0
        return code;
1373
1374
2.44M
    return gs_settextlinematrix(ctx->pgs, (gs_matrix *)&m);
1375
2.44M
}
1376
1377
int pdfi_Tr(pdf_context *ctx)
1378
811k
{
1379
811k
    int code;
1380
811k
    int64_t mode;
1381
1382
811k
    code = pdfi_destack_int(ctx, &mode);
1383
811k
    if (code < 0)
1384
8.97k
        return code;
1385
1386
802k
    if (mode < 0 || mode > 7)
1387
4.66k
        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
798k
    if (gs_currenttextrenderingmode(ctx->pgs) > 3 && mode < 4 && ctx->text.BlockDepth != 0 && ctx->text.TextClip)
1394
287
        pdfi_set_warning(ctx, 0, NULL, W_PDF_BADTRSWITCH, "pdfi_Tr", NULL);
1395
1396
798k
    if (ctx->device_state.preserve_tr_mode) {
1397
49.7k
        gs_settextrenderingmode(ctx->pgs, mode);
1398
49.7k
    } else
1399
748k
    {
1400
748k
        gs_point initial_point;
1401
1402
748k
        if (gs_currenttextrenderingmode(ctx->pgs) < 4 && mode >= 4 && ctx->text.BlockDepth != 0) {
1403
808
            gs_settextrenderingmode(ctx->pgs, mode);
1404
            /* Capture the current position */
1405
808
            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
808
            gs_newpath(ctx->pgs);
1410
808
            if (code >= 0)
1411
624
                gs_moveto(ctx->pgs, initial_point.x, initial_point.y);
1412
808
            code = 0;
1413
747k
        } 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
410
            code = pdfi_ET(ctx);
1420
410
            if (code < 0)
1421
0
                return code;
1422
410
            gs_settextrenderingmode(ctx->pgs, mode);
1423
410
            code = pdfi_BT(ctx);
1424
410
            if (code < 0)
1425
0
                return code;
1426
410
        }
1427
747k
        else
1428
747k
            gs_settextrenderingmode(ctx->pgs, mode);
1429
748k
    }
1430
798k
    return code;
1431
798k
}
1432
1433
static int pdfi_set_Ts(pdf_context *ctx, double Ts)
1434
4.96k
{
1435
4.96k
    return gs_settextrise(ctx->pgs, Ts);
1436
4.96k
}
1437
1438
int pdfi_Ts(pdf_context *ctx)
1439
5.47k
{
1440
5.47k
    int code;
1441
5.47k
    double d;
1442
1443
5.47k
    code = pdfi_destack_real(ctx, &d);
1444
5.47k
    if (code < 0)
1445
507
        return code;
1446
1447
4.96k
    return pdfi_set_Ts(ctx, d);
1448
5.47k
}
1449
1450
static int pdfi_set_Tw(pdf_context *ctx, double Tw)
1451
199k
{
1452
199k
    return gs_setwordspacing(ctx->pgs, Tw);
1453
199k
}
1454
1455
int pdfi_Tw(pdf_context *ctx)
1456
200k
{
1457
200k
    int code;
1458
200k
    double d;
1459
1460
200k
    code = pdfi_destack_real(ctx, &d);
1461
200k
    if (code < 0)
1462
1.32k
        return code;
1463
1464
199k
    return pdfi_set_Tw(ctx, d);
1465
200k
}
1466
1467
int pdfi_Tz(pdf_context *ctx)
1468
240k
{
1469
240k
    int code;
1470
240k
    double d;
1471
1472
240k
    code = pdfi_destack_real(ctx, &d);
1473
240k
    if (code < 0)
1474
3.02k
        return code;
1475
1476
237k
    return gs_settexthscaling(ctx->pgs, d);
1477
240k
}
1478
1479
int pdfi_singlequote(pdf_context *ctx)
1480
20.4k
{
1481
20.4k
    int code;
1482
1483
20.4k
    if (ctx->text.BlockDepth == 0) {
1484
7.26k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_singlequote", NULL);
1485
7.26k
    }
1486
1487
20.4k
    code = pdfi_T_star(ctx);
1488
20.4k
    if (code < 0)
1489
0
        return code;
1490
1491
20.4k
    return pdfi_Tj(ctx);
1492
20.4k
}
1493
1494
int pdfi_doublequote(pdf_context *ctx)
1495
7.28k
{
1496
7.28k
    int code;
1497
7.28k
    double Tw, Tc;
1498
1499
7.28k
    if (ctx->text.BlockDepth == 0) {
1500
5.85k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TEXTOPNOBT, "pdfi_T_doublequote", NULL);
1501
5.85k
    }
1502
1503
7.28k
    if (pdfi_count_stack(ctx) < 3) {
1504
6.23k
        pdfi_clearstack(ctx);
1505
6.23k
        return_error(gs_error_stackunderflow);
1506
6.23k
    }
1507
1508
1.05k
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STRING) {
1509
300
        pdfi_pop(ctx, 3);
1510
300
        return gs_note_error(gs_error_typecheck);
1511
300
    }
1512
1513
754
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-2], &Tc);
1514
754
    if (code < 0)
1515
2
        goto error;
1516
752
    code = pdfi_set_Tc(ctx, Tc);
1517
752
    if (code < 0)
1518
0
        goto error;
1519
1520
752
    code = pdfi_obj_to_real(ctx, ctx->stack_top[-3], &Tw);
1521
752
    if (code < 0)
1522
21
        goto error;
1523
731
    code = pdfi_set_Tw(ctx, Tw);
1524
731
    if (code < 0)
1525
0
        goto error;
1526
1527
731
    code = pdfi_T_star(ctx);
1528
731
    if (code < 0)
1529
0
        goto error;
1530
1531
731
    code = pdfi_Tj(ctx);
1532
    /* Tj pops one off the stack for us, leaving us 2 to go. */
1533
731
    pdfi_pop(ctx, 2);
1534
731
    return code;
1535
1536
23
error:
1537
23
    pdfi_pop(ctx, 3);
1538
23
    return code;
1539
731
}