Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/pdf/pdf_path.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2018-2023 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
/* Path operations for the PDF interpreter */
17
18
#include "pdf_int.h"
19
#include "pdf_font_types.h"
20
#include "pdf_gstate.h"
21
#include "pdf_path.h"
22
#include "pdf_stack.h"
23
#include "pdf_trans.h"
24
#include "gstypes.h"
25
#include "pdf_optcontent.h"
26
#include "gspath.h"         /* For gs_moveto() and friends */
27
#include "gspaint.h"        /* For gs_fill() and friends */
28
29
typedef enum path_segment_e {
30
    pdfi_moveto_seg,
31
    pdfi_lineto_seg,
32
    pdfi_curveto_seg,
33
    pdfi_re_seg,
34
    pdfi_v_curveto_seg,
35
    pdfi_y_curveto_seg,
36
    pdfi_closepath_seg
37
} pdfi_path_segment;
38
39
static int StorePathSegment(pdf_context *ctx, pdfi_path_segment segment, double *pts)
40
1.36M
{
41
1.36M
    int size = 0;
42
43
1.36M
    switch (segment)
44
1.36M
    {
45
397k
        case pdfi_moveto_seg:
46
992k
        case pdfi_lineto_seg:
47
992k
            size = 2;
48
992k
            break;
49
159k
        case pdfi_re_seg:
50
160k
        case pdfi_v_curveto_seg:
51
162k
        case pdfi_y_curveto_seg:
52
162k
            size = 4;
53
162k
                break;
54
173k
        case pdfi_curveto_seg:
55
173k
            size = 6;
56
173k
            break;
57
41.0k
        case pdfi_closepath_seg:
58
41.0k
            break;
59
0
        default:
60
0
            return_error(gs_error_undefined);
61
0
            break;
62
1.36M
    }
63
1.36M
    if (ctx->PathSegments == NULL) {
64
368k
        ctx->PathSegments = (char *)gs_alloc_bytes(ctx->memory, 1024, "StorePathSegment");
65
368k
        if (ctx->PathSegments == NULL)
66
0
            return_error(gs_error_VMerror);
67
368k
        ctx->PathSegmentsCurrent = ctx->PathSegments;
68
368k
        ctx->PathSegmentsTop = ctx->PathSegments + 1024;
69
368k
    }
70
1.36M
    if (ctx->PathSegmentsCurrent == ctx->PathSegmentsTop) {
71
223
        char *new_accumulator = NULL;
72
223
        uint64_t old_size;
73
74
223
        old_size = ctx->PathSegmentsCurrent - ctx->PathSegments;
75
223
        new_accumulator = (char *)gs_alloc_bytes(ctx->memory, old_size + 1024, "StorePathSegment");
76
223
        if (new_accumulator == NULL)
77
0
            return_error(gs_error_VMerror);
78
223
        memcpy(new_accumulator, ctx->PathSegments, old_size);
79
223
        ctx->PathSegmentsCurrent = new_accumulator + old_size;
80
223
        gs_free_object(ctx->memory, ctx->PathSegments, "StorePathSegment");
81
223
        ctx->PathSegments = new_accumulator;
82
223
        ctx->PathSegmentsTop = ctx->PathSegments + old_size + 1024;
83
223
    }
84
85
1.36M
    if (ctx->PathPts == NULL) {
86
368k
        ctx->PathPts = (double *)gs_alloc_bytes(ctx->memory, 4096, "StorePathSegment");
87
368k
        if (ctx->PathPts == NULL)
88
0
            return_error(gs_error_VMerror);
89
368k
        ctx->PathPtsCurrent = ctx->PathPts;
90
368k
        ctx->PathPtsTop = ctx->PathPts + (4096 / sizeof(double));
91
368k
    }
92
1.36M
    if (ctx->PathPtsCurrent + size > ctx->PathPtsTop) {
93
1.56k
        double *new_accumulator = NULL;
94
1.56k
        uint64_t old_size;
95
96
1.56k
        old_size = (char *)ctx->PathPtsCurrent - (char *)ctx->PathPts;
97
1.56k
        new_accumulator = (double *)gs_alloc_bytes(ctx->memory, old_size + 4096, "StorePathSegment");
98
1.56k
        if (new_accumulator == NULL)
99
0
            return_error(gs_error_VMerror);
100
1.56k
        memcpy(new_accumulator, ctx->PathPts, old_size);
101
1.56k
        ctx->PathPtsCurrent = new_accumulator + (old_size / sizeof(double));
102
1.56k
        gs_free_object(ctx->memory, ctx->PathPts, "StorePathSegment");
103
1.56k
        ctx->PathPts = new_accumulator;
104
1.56k
        ctx->PathPtsTop = ctx->PathPts + ((old_size + 4096) / sizeof(double));
105
1.56k
    }
106
107
1.36M
    *(ctx->PathSegmentsCurrent++) = (char)segment;
108
1.36M
    switch (segment)
109
1.36M
    {
110
397k
        case pdfi_moveto_seg:
111
992k
        case pdfi_lineto_seg:
112
992k
            memcpy(ctx->PathPtsCurrent, pts, 2 * sizeof(double));
113
992k
            ctx->PathPtsCurrent += 2;
114
992k
            break;
115
159k
        case pdfi_re_seg:
116
160k
        case pdfi_v_curveto_seg:
117
162k
        case pdfi_y_curveto_seg:
118
162k
            memcpy(ctx->PathPtsCurrent, pts, 4 * sizeof(double));
119
162k
            ctx->PathPtsCurrent += 4;
120
162k
            break;
121
173k
        case pdfi_curveto_seg:
122
173k
            memcpy(ctx->PathPtsCurrent, pts, 6 * sizeof(double));
123
173k
            ctx->PathPtsCurrent += 6;
124
173k
            break;
125
41.0k
        case pdfi_closepath_seg:
126
41.0k
            break;
127
1.36M
    }
128
1.36M
    return 0;
129
1.36M
}
130
131
static int ApplyStoredPath(pdf_context *ctx)
132
380k
{
133
380k
    int code = 0;
134
380k
    char *op = NULL;
135
380k
    double *dpts = NULL;
136
380k
    double *current;
137
138
380k
    if (ctx->PathSegments == NULL)
139
18.0k
        return 0;
140
141
362k
    if (ctx->PathPts == NULL) {
142
0
        code = gs_note_error(gs_error_unknownerror);
143
0
        goto error;
144
0
    }
145
146
362k
    if (ctx->pgs->current_point_valid) {
147
722
        code = gs_newpath(ctx->pgs);
148
722
        if (code < 0)
149
0
            goto error;
150
722
    }
151
152
362k
    op = ctx->PathSegments;
153
362k
    dpts = ctx->PathPts;
154
362k
    current = dpts; /* Stop stupid compilers complaining. */
155
156
1.60M
    while (op < ctx->PathSegmentsCurrent) {
157
1.24M
        if (dpts > ctx->PathPtsCurrent) {
158
0
            code = gs_note_error(gs_error_unknownerror);
159
0
            goto error;
160
0
        }
161
162
1.24M
        switch(*op++) {
163
373k
            case pdfi_moveto_seg:
164
373k
                code = gs_moveto(ctx->pgs, dpts[0], dpts[1]);
165
373k
                current = dpts;
166
373k
                dpts+= 2;
167
373k
                break;
168
523k
            case pdfi_lineto_seg:
169
523k
                code = gs_lineto(ctx->pgs, dpts[0], dpts[1]);
170
523k
                current = dpts;
171
523k
                dpts+= 2;
172
523k
                break;
173
155k
            case pdfi_re_seg:
174
155k
                code = gs_moveto(ctx->pgs, dpts[0], dpts[1]);
175
155k
                if (code >= 0) {
176
155k
                    code = gs_rlineto(ctx->pgs, dpts[2], 0);
177
155k
                    if (code >= 0) {
178
155k
                        code = gs_rlineto(ctx->pgs, 0, dpts[3]);
179
155k
                        if (code >= 0) {
180
155k
                            code = gs_rlineto(ctx->pgs, -dpts[2], 0);
181
155k
                            if (code >= 0)
182
155k
                                code = gs_closepath(ctx->pgs);
183
155k
                        }
184
155k
                    }
185
155k
                }
186
155k
                current = dpts;
187
155k
                dpts+= 4;
188
155k
                break;
189
1.39k
            case pdfi_v_curveto_seg:
190
1.39k
                code = gs_curveto(ctx->pgs, current[0], current[1], dpts[0], dpts[1], dpts[2], dpts[3]);
191
1.39k
                current = dpts + 2;
192
1.39k
                dpts+= 4;
193
1.39k
                break;
194
1.44k
            case pdfi_y_curveto_seg:
195
1.44k
                code = gs_curveto(ctx->pgs, dpts[0], dpts[1], dpts[2], dpts[3], dpts[2], dpts[3]);
196
1.44k
                current = dpts + 2;
197
1.44k
                dpts+= 4;
198
1.44k
                break;
199
152k
            case pdfi_curveto_seg:
200
152k
                code = gs_curveto(ctx->pgs, dpts[0], dpts[1], dpts[2], dpts[3], dpts[4], dpts[5]);
201
152k
                current = dpts + 4;
202
152k
                dpts+= 6;
203
152k
                break;
204
38.2k
            case pdfi_closepath_seg:
205
38.2k
                code = gs_closepath(ctx->pgs);
206
38.2k
                break;
207
0
            default:
208
0
                code = gs_note_error(gs_error_rangecheck);
209
0
                break;
210
1.24M
        }
211
1.24M
        if (code < 0)
212
6.87k
            break;
213
1.24M
    }
214
215
362k
error:
216
362k
    gs_free_object(ctx->memory, ctx->PathSegments, "ApplyStoredPath");
217
362k
    ctx->PathSegmentsTop = ctx->PathSegmentsCurrent = ctx->PathSegments = NULL;
218
362k
    gs_free_object(ctx->memory, ctx->PathPts, "ApplyStoredPath");
219
362k
    ctx->PathPtsTop = ctx->PathPtsCurrent = ctx->PathPts = NULL;
220
362k
    return code;
221
362k
}
222
223
int pdfi_moveto (pdf_context *ctx)
224
490k
{
225
490k
    int code;
226
490k
    double xy[2];
227
228
490k
    if (ctx->text.BlockDepth != 0)
229
3.45k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_moveto", NULL);
230
231
490k
    code = pdfi_destack_reals(ctx, xy, 2);
232
490k
    if (code < 0)
233
92.7k
        return code;
234
235
397k
    return StorePathSegment(ctx, pdfi_moveto_seg, (double *)&xy);
236
490k
}
237
238
int pdfi_lineto (pdf_context *ctx)
239
641k
{
240
641k
    int code;
241
641k
    double xy[2];
242
243
641k
    if (ctx->text.BlockDepth != 0)
244
9.53k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_lineto", NULL);
245
246
641k
    code = pdfi_destack_reals(ctx, xy, 2);
247
641k
    if (code < 0)
248
46.7k
        return code;
249
250
594k
    return StorePathSegment(ctx, pdfi_lineto_seg, (double *)&xy);
251
641k
}
252
253
static int pdfi_fill_inner(pdf_context *ctx, bool use_eofill)
254
69.0k
{
255
69.0k
    int code=0, code1;
256
69.0k
    pdfi_trans_state_t state;
257
258
69.0k
    if (ctx->text.BlockDepth != 0)
259
2.66k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_fill_inner", NULL);
260
261
69.0k
    if (pdfi_oc_is_off(ctx))
262
2.97k
        goto exit;
263
264
66.0k
    code = ApplyStoredPath(ctx);
265
66.0k
    if (code < 0)
266
445
        return code;
267
268
65.5k
    code = pdfi_trans_setup(ctx, &state, NULL, TRANSPARENCY_Caller_Fill);
269
65.5k
    if (code == 0) {
270
        /* If we don't gsave/grestore round the fill, then the file
271
         * /tests_private/pdf/sumatra/954_-_dashed_lines_hardly_visible.pdf renders
272
         * incorrectly. However we must not gsave/grestore round the trans_setup
273
         * trans_teardown, because that might set pgs->soft_mask_id and if we restore
274
         * back to a point where that is not set then pdfwrite doesn't work properly.
275
         */
276
65.5k
        code = pdfi_gsave(ctx);
277
65.5k
        if (code < 0) goto exit;
278
279
65.5k
        if (use_eofill)
280
4.39k
            code = gs_eofill(ctx->pgs);
281
61.2k
        else
282
61.2k
            code = gs_fill(ctx->pgs);
283
65.5k
        code1 = pdfi_grestore(ctx);
284
65.5k
        if (code == 0) code = code1;
285
286
65.5k
        code1 = pdfi_trans_teardown(ctx, &state);
287
65.5k
        if (code == 0) code = code1;
288
65.5k
    }
289
290
68.5k
 exit:
291
68.5k
    code1 = pdfi_newpath(ctx);
292
68.5k
    if (code == 0) code = code1;
293
294
68.5k
    return code;
295
65.5k
}
296
297
int pdfi_fill(pdf_context *ctx)
298
64.5k
{
299
64.5k
    return pdfi_fill_inner(ctx, false);
300
64.5k
}
301
302
int pdfi_eofill(pdf_context *ctx)
303
4.46k
{
304
4.46k
    return pdfi_fill_inner(ctx, true);
305
4.46k
}
306
307
int pdfi_stroke(pdf_context *ctx)
308
269k
{
309
269k
    int code=0, code1;
310
269k
    pdfi_trans_state_t state;
311
312
269k
    if (ctx->text.BlockDepth != 0)
313
5.71k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_stroke", NULL);
314
315
269k
    if (pdfi_oc_is_off(ctx))
316
5
        goto exit;
317
318
269k
    code = ApplyStoredPath(ctx);
319
269k
    if (code < 0)
320
4.93k
        return code;
321
322
264k
    gs_swapcolors_quick(ctx->pgs);
323
264k
    code = pdfi_trans_setup(ctx, &state, NULL, TRANSPARENCY_Caller_Stroke);
324
264k
    if (code == 0) {
325
264k
        code = pdfi_gsave(ctx);
326
264k
        if (code < 0) goto exit;
327
328
264k
        code = gs_stroke(ctx->pgs);
329
330
264k
        code1 = pdfi_grestore(ctx);
331
264k
        if (code == 0) code = code1;
332
333
264k
        code1 = pdfi_trans_teardown(ctx, &state);
334
264k
        if (code == 0) code = code1;
335
264k
    }
336
264k
    gs_swapcolors_quick(ctx->pgs);
337
338
264k
 exit:
339
264k
    code1 = pdfi_newpath(ctx);
340
264k
    if (code == 0) code = code1;
341
342
264k
    return code;
343
264k
}
344
345
int pdfi_closepath_stroke(pdf_context *ctx)
346
1.97k
{
347
1.97k
    int code;
348
349
1.97k
    if (ctx->text.BlockDepth != 0)
350
267
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_closepath_stroke", NULL);
351
352
1.97k
    code = StorePathSegment(ctx, pdfi_closepath_seg, NULL);
353
1.97k
    if (code < 0)
354
0
        return code;
355
356
1.97k
    return pdfi_stroke(ctx);
357
1.97k
}
358
359
int pdfi_curveto(pdf_context *ctx)
360
187k
{
361
187k
    int code;
362
187k
    double Values[6];
363
364
187k
    code = pdfi_destack_reals(ctx, Values, 6);
365
187k
    if (code < 0)
366
13.6k
        return code;
367
368
173k
    if (ctx->text.BlockDepth != 0)
369
439
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_curveto", NULL);
370
371
173k
    return StorePathSegment(ctx, pdfi_curveto_seg, (double *)&Values);
372
187k
}
373
374
int pdfi_v_curveto(pdf_context *ctx)
375
1.52k
{
376
1.52k
    int code;
377
1.52k
    double Values[4];
378
379
1.52k
    code = pdfi_destack_reals(ctx, Values, 4);
380
1.52k
    if (code < 0)
381
117
        return code;
382
383
1.41k
    if (ctx->text.BlockDepth != 0)
384
2
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_v_curveto", NULL);
385
386
1.41k
    return StorePathSegment(ctx, pdfi_v_curveto_seg, (double *)&Values);
387
1.52k
}
388
389
int pdfi_y_curveto(pdf_context *ctx)
390
2.83k
{
391
2.83k
    int code;
392
2.83k
    double Values[4];
393
394
2.83k
    code = pdfi_destack_reals(ctx, Values, 4);
395
2.83k
    if (code < 0)
396
1.35k
        return code;
397
398
1.48k
    if (ctx->text.BlockDepth != 0)
399
2
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_y_curveto", NULL);
400
401
1.48k
    return StorePathSegment(ctx, pdfi_y_curveto_seg, (double *)&Values);
402
2.83k
}
403
404
int pdfi_closepath(pdf_context *ctx)
405
37.8k
{
406
37.8k
    if (ctx->text.BlockDepth != 0)
407
1.98k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_closepath", NULL);
408
409
37.8k
    return StorePathSegment(ctx, pdfi_closepath_seg, NULL);
410
37.8k
}
411
412
int pdfi_newpath(pdf_context *ctx)
413
427k
{
414
427k
    int code = 0, code1;
415
416
    /* This code is to deal with the wacky W and W* operators */
417
427k
    if (ctx->clip_active) {
418
44.1k
        if (ctx->PathSegments != NULL) {
419
42.5k
            code = ApplyStoredPath(ctx);
420
42.5k
            if (code < 0)
421
1.23k
                return code;
422
42.5k
        }
423
42.8k
        if (ctx->pgs->current_point_valid) {
424
41.7k
            if (ctx->do_eoclip)
425
3.70k
                code = gs_eoclip(ctx->pgs);
426
38.0k
            else
427
38.0k
                code = gs_clip(ctx->pgs);
428
41.7k
        }
429
42.8k
    }
430
426k
    ctx->clip_active = false;
431
432
426k
    if (ctx->PathSegments != NULL){
433
5.05k
        gs_free_object(ctx->memory, ctx->PathSegments, "ApplyStoredPath");
434
5.05k
        ctx->PathSegmentsTop = ctx->PathSegmentsCurrent = ctx->PathSegments = NULL;
435
5.05k
        gs_free_object(ctx->memory, ctx->PathPts, "ApplyStoredPath");
436
5.05k
        ctx->PathPtsTop = ctx->PathPtsCurrent = ctx->PathPts = NULL;
437
5.05k
    }
438
439
426k
    code1 = gs_newpath(ctx->pgs);
440
426k
    if (code == 0) code = code1;
441
442
426k
    if (ctx->text.BlockDepth != 0)
443
11.5k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_newpath", NULL);
444
445
426k
    return code;
446
427k
}
447
448
int pdfi_b(pdf_context *ctx)
449
1.15k
{
450
1.15k
    int code;
451
452
1.15k
    if (ctx->text.BlockDepth != 0)
453
1.02k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_b", NULL);
454
455
1.15k
    code = StorePathSegment(ctx, pdfi_closepath_seg, NULL);
456
1.15k
    if (code < 0)
457
0
        return code;
458
459
1.15k
    return pdfi_B(ctx);
460
1.15k
}
461
462
int pdfi_b_star(pdf_context *ctx)
463
23
{
464
23
    int code;
465
466
23
    if (ctx->text.BlockDepth != 0)
467
0
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_b_star", NULL);
468
469
23
    code = StorePathSegment(ctx, pdfi_closepath_seg, NULL);
470
23
    if (code < 0)
471
0
        return code;
472
473
23
    return pdfi_B_star(ctx);
474
23
}
475
476
/* common code for B and B* */
477
static int pdfi_B_inner(pdf_context *ctx, bool use_eofill)
478
3.08k
{
479
3.08k
    int code=0, code1=0;
480
3.08k
    pdfi_trans_state_t state;
481
482
3.08k
    if (ctx->text.BlockDepth != 0)
483
2.16k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_B_inner", NULL);
484
485
3.08k
    if (pdfi_oc_is_off(ctx))
486
4
        goto exit;
487
488
3.07k
    code = ApplyStoredPath(ctx);
489
3.07k
    if (code < 0)
490
263
        return code;
491
492
2.81k
    code = pdfi_trans_setup(ctx, &state, NULL, TRANSPARENCY_Caller_FillStroke);
493
2.81k
    if (code == 0) {
494
2.81k
        code = pdfi_gsave(ctx);
495
2.81k
        if (code < 0) goto exit;
496
497
2.81k
        if (use_eofill)
498
206
            code = gs_eofillstroke(ctx->pgs, &code1);
499
2.60k
        else
500
2.60k
            code = gs_fillstroke(ctx->pgs, &code1);
501
502
2.81k
        code1 = pdfi_grestore(ctx);
503
2.81k
        if (code == 0) code = code1;
504
505
2.81k
        code1 = pdfi_trans_teardown(ctx, &state);
506
2.81k
        if (code >= 0) code = code1;
507
2.81k
    }
508
509
2.81k
 exit:
510
2.81k
    code1 = pdfi_newpath(ctx);
511
2.81k
    if (code == 0) code = code1;
512
513
2.81k
    return code;
514
2.81k
}
515
516
int pdfi_B(pdf_context *ctx)
517
2.87k
{
518
2.87k
    return pdfi_B_inner(ctx, false);
519
2.87k
}
520
521
int pdfi_B_star(pdf_context *ctx)
522
210
{
523
210
    return pdfi_B_inner(ctx, true);
524
210
}
525
526
int pdfi_clip(pdf_context *ctx)
527
0
{
528
0
    int code = gs_clip(ctx->pgs);
529
530
0
    if (ctx->text.BlockDepth != 0)
531
0
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_B_clip", NULL);
532
533
0
    return code;
534
0
}
535
536
int pdfi_eoclip(pdf_context *ctx)
537
0
{
538
0
    int code = gs_eoclip(ctx->pgs);
539
540
0
    if (ctx->text.BlockDepth != 0)
541
0
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_eoclip", NULL);
542
543
0
     return code;
544
0
}
545
546
int pdfi_rectpath(pdf_context *ctx)
547
165k
{
548
165k
    int code;
549
165k
    double Values[4];
550
551
165k
    code = pdfi_destack_reals(ctx, Values, 4);
552
165k
    if (code < 0)
553
5.76k
        return code;
554
555
159k
    if (ctx->text.BlockDepth != 0)
556
3.54k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_OPINVALIDINTEXT, "pdfi_rectpath", NULL);
557
558
159k
    return StorePathSegment(ctx, pdfi_re_seg, (double *)&Values);
559
165k
}