Coverage Report

Created: 2025-06-10 07:27

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