Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gspaint.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Painting procedures for Ghostscript library */
18
#include "math_.h"    /* for fabs */
19
#include "gx.h"
20
#include "gpcheck.h"
21
#include "gserrors.h"
22
#include "gsropt.h"   /* for gxpaint.h */
23
#include "gxfixed.h"
24
#include "gxmatrix.h"   /* for gs_gstate */
25
#include "gspaint.h"
26
#include "gspath.h"
27
#include "gzpath.h"
28
#include "gxpaint.h"
29
#include "gxpcolor.h"   /* for do_fill_stroke */
30
#include "gzstate.h"
31
#include "gxdevice.h"
32
#include "gxdevmem.h"
33
#include "gzcpath.h"
34
#include "gxhldevc.h"
35
#include "gsutil.h"
36
#include "gxscanc.h"
37
#include "gxdevsop.h"
38
#include "gsicc_cms.h"
39
#include "gdevepo.h"
40
#include "assert_.h"
41
42
/* Define the nominal size for alpha buffers. */
43
#define abuf_nominal_SMALL 500
44
0
#define abuf_nominal_LARGE 2000
45
#if ARCH_SMALL_MEMORY
46
#  define abuf_nominal abuf_nominal_SMALL
47
#else
48
#ifdef DEBUG
49
#  define abuf_nominal\
50
     (gs_debug_c('.') ? abuf_nominal_SMALL : abuf_nominal_LARGE)
51
#else
52
0
#  define abuf_nominal abuf_nominal_LARGE
53
#endif
54
#endif
55
56
/* Erase the page */
57
int
58
gs_erasepage(gs_gstate * pgs)
59
193k
{
60
    /*
61
     * We can't just fill with device white; we must take the
62
     * transfer function into account.
63
     */
64
193k
    int code;
65
66
193k
    if ((code = gs_gsave(pgs)) < 0)
67
0
        return code;
68
193k
    if ((code = gs_setgray(pgs, 1.0)) >= 0) {
69
        /* Fill the page directly, ignoring clipping. */
70
193k
        code = gs_fillpage(pgs);
71
193k
    }
72
193k
    gs_grestore(pgs);
73
193k
    return code;
74
193k
}
75
76
/* Fill the page with the current color. */
77
int
78
gs_fillpage(gs_gstate * pgs)
79
1.16M
{
80
1.16M
    gx_device *dev = gs_currentdevice(pgs);
81
1.16M
    int code;
82
83
    /*
84
     * No need to check for returning error code,
85
     * existing device will continue to operate as before.
86
     */
87
1.16M
    epo_check_and_install(dev);
88
89
    /* Deliberately use the terminal device here */
90
1.16M
    if (dev_proc(dev, get_color_mapping_procs) ==  gx_error_get_color_mapping_procs) {
91
0
        emprintf1(dev->memory,
92
0
                  "\n   *** Error: No get_color_mapping_procs for device: %s\n",
93
0
                  dev->dname);
94
0
        return_error(gs_error_Fatal);
95
0
    }
96
    /* Processing a fill object operation, but this counts as "UNTOUCHED" */
97
1.16M
    gx_unset_dev_color(pgs);    /* force update so we pick up the new tag */
98
1.16M
    gx_unset_alt_dev_color(pgs);
99
1.16M
    dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_UNTOUCHED_TAG);
100
101
1.16M
    code = gx_set_dev_color(pgs);
102
1.16M
    if (code != 0)
103
0
        return code;
104
105
1.16M
    code = (*dev_proc(dev, fillpage))(dev, pgs, gs_currentdevicecolor_inline(pgs));
106
1.16M
    if (code < 0)
107
0
        return code;
108
109
    /* If GrayDetection is set, make sure monitoring is enabled. */
110
1.16M
    if (dev->icc_struct != NULL &&
111
1.16M
            dev->icc_struct->graydetection && !dev->icc_struct->pageneutralcolor) {
112
0
        dev->icc_struct->pageneutralcolor = true; /* start detecting again */
113
0
        code = gsicc_mcm_begin_monitor(pgs->icc_link_cache, dev);
114
0
    }
115
1.16M
    if (code < 0)
116
0
        return code;
117
1.16M
    return (*dev_proc(dev, sync_output)) (dev);
118
1.16M
}
119
/*
120
 * Set up an alpha buffer for a stroke or fill operation.  Return 0
121
 * if no buffer could be allocated, 1 if a buffer was installed,
122
 * or the usual negative error code.
123
 *
124
 * The fill/stroke code sets up a clipping device if needed; however,
125
 * since we scale up all the path coordinates, we either need to scale up
126
 * the clipping region, or do clipping after, rather than before,
127
 * alpha buffering.  Either of these is a little inconvenient, but
128
 * the former is less inconvenient.
129
 */
130
static int
131
scale_paths(gs_gstate * pgs, int log2_scale_x, int log2_scale_y, bool do_path)
132
0
{
133
    /*
134
     * Because of clip and clippath, any of path, clip_path, and view_clip
135
     * may be aliases for each other.  The only reliable way to detect
136
     * this is by comparing the segments pointers.  Note that we must
137
     * scale the non-segment parts of the paths even if the segments are
138
     * aliased.
139
     */
140
0
    const gx_path_segments *seg_clip =
141
0
        (pgs->clip_path->path_valid ? pgs->clip_path->path.segments : 0);
142
0
    const gx_clip_rect_list *list_clip = pgs->clip_path->rect_list;
143
0
    const gx_path_segments *seg_view_clip;
144
0
    const gx_clip_rect_list *list_view_clip;
145
0
    const gx_path_segments *seg_effective_clip =
146
0
        (pgs->effective_clip_path->path_valid ?
147
0
         pgs->effective_clip_path->path.segments : 0);
148
0
    const gx_clip_rect_list *list_effective_clip =
149
0
        pgs->effective_clip_path->rect_list;
150
151
0
    gx_cpath_scale_exp2_shared(pgs->clip_path, log2_scale_x, log2_scale_y,
152
0
                               false, false);
153
0
    if (pgs->view_clip != 0 && pgs->view_clip != pgs->clip_path) {
154
0
        seg_view_clip =
155
0
            (pgs->view_clip->path_valid ? pgs->view_clip->path.segments : 0);
156
0
        list_view_clip = pgs->view_clip->rect_list;
157
0
        gx_cpath_scale_exp2_shared(pgs->view_clip, log2_scale_x, log2_scale_y,
158
0
                                   list_view_clip == list_clip,
159
0
                                   seg_view_clip && seg_view_clip == seg_clip);
160
0
    } else
161
0
        seg_view_clip = 0, list_view_clip = 0;
162
0
    if (pgs->effective_clip_path != pgs->clip_path &&
163
0
        pgs->effective_clip_path != pgs->view_clip
164
0
        )
165
0
        gx_cpath_scale_exp2_shared(pgs->effective_clip_path, log2_scale_x,
166
0
                                   log2_scale_y,
167
0
                                   list_effective_clip == list_clip ||
168
0
                                   list_effective_clip == list_view_clip,
169
0
                                   seg_effective_clip &&
170
0
                                   (seg_effective_clip == seg_clip ||
171
0
                                    seg_effective_clip == seg_view_clip));
172
0
    if (do_path) {
173
0
        const gx_path_segments *seg_path = pgs->path->segments;
174
175
0
        gx_path_scale_exp2_shared(pgs->path, log2_scale_x, log2_scale_y,
176
0
                                  seg_path == seg_clip ||
177
0
                                  seg_path == seg_view_clip ||
178
0
                                  seg_path == seg_effective_clip);
179
0
    }
180
0
    return 0;
181
0
}
182
static void
183
scale_dash_pattern(gs_gstate * pgs, double scale)
184
0
{
185
0
    int i;
186
187
0
    for (i = 0; i < pgs->line_params.dash.pattern_size; ++i)
188
0
        pgs->line_params.dash.pattern[i] *= scale;
189
0
    pgs->line_params.dash.offset *= scale;
190
0
    pgs->line_params.dash.pattern_length *= scale;
191
0
    pgs->line_params.dash.init_dist_left *= scale;
192
0
    if (pgs->line_params.dot_length_absolute)
193
0
        pgs->line_params.dot_length *= scale;
194
0
}
195
196
/*
197
 Returns 0 for OK.
198
 Returns 1 for "OK, buffer needs releasing"
199
 Returns 2 for "Empty region"
200
 Returns -ve for error
201
 */
202
static int
203
alpha_buffer_init(gs_gstate * pgs, fixed extra_x, fixed extra_y, int alpha_bits,
204
                  bool devn)
205
0
{
206
0
    gx_device *dev = gs_currentdevice_inline(pgs);
207
0
    int log2_alpha_bits = ilog2(alpha_bits);
208
0
    gs_fixed_rect bbox;
209
0
    gs_int_rect ibox;
210
0
    uint width, raster, band_space;
211
0
    uint height, height2;
212
0
    gs_log2_scale_point log2_scale;
213
0
    gs_memory_t *mem;
214
0
    gx_device_memory *mdev;
215
216
0
    log2_scale.x = log2_scale.y = log2_alpha_bits;
217
0
    gx_path_bbox(pgs->path, &bbox);
218
0
    ibox.p.x = fixed2int(bbox.p.x - extra_x) - 1;
219
0
    ibox.p.y = fixed2int(bbox.p.y - extra_y) - 1;
220
0
    ibox.q.x = fixed2int_ceiling(bbox.q.x + extra_x) + 1;
221
0
    ibox.q.y = fixed2int_ceiling(bbox.q.y + extra_y) + 1;
222
0
    (void)dev_proc(dev, dev_spec_op)(dev, gxdso_restrict_bbox, &ibox, sizeof(ibox));
223
0
    if ((ibox.q.y <= ibox.p.y) || (ibox.q.x <= ibox.p.x))
224
0
        return 2;
225
0
    width = (ibox.q.x - ibox.p.x) << log2_scale.x;
226
0
    raster = bitmap_raster(width);
227
0
    band_space = raster << log2_scale.y;
228
0
    height2 = (ibox.q.y - ibox.p.y);
229
0
    height = (abuf_nominal / band_space);
230
0
    if (height == 0)
231
0
        height = 1;
232
0
    if (height > height2)
233
0
        height = height2;
234
0
    height <<= log2_scale.y;
235
    /* We may have to update the marking parameters if we have a pdf14 device
236
       as our target.  Need to do while dev is still active in pgs. Do this
237
       before allocating the device to simplify cleanup. */
238
0
    if (dev_proc(dev, dev_spec_op)(dev, gxdso_is_pdf14_device, NULL, 0) > 0) {
239
0
        int code = gs_update_trans_marking_params(pgs);
240
0
        if (code < 0)
241
0
            return code;
242
0
    }
243
0
    mem = pgs->memory;
244
0
    mdev = gs_alloc_struct_immovable(mem, gx_device_memory, &st_device_memory,
245
0
                           "alpha_buffer_init");
246
0
    if (mdev == 0)
247
0
        return 0;   /* if no room, don't buffer */
248
0
    gs_make_mem_abuf_device(mdev, mem, dev, &log2_scale,
249
0
                            alpha_bits, ibox.p.x << log2_scale.x, devn);
250
0
    mdev->width = width;
251
0
    mdev->height = height;
252
0
    mdev->bitmap_memory = mem;
253
    /* Set the horrible hacky flag that tells people that the width/height here
254
     * have been set for *our* convenience, rather than accurately depicting the
255
     * size of the device for callers. */
256
0
    mdev->non_strict_bounds = 1;
257
0
    if ((*dev_proc(mdev, open_device)) ((gx_device *) mdev) < 0) {
258
        /* No room for bits, punt. */
259
0
        gs_free_object(mem, mdev, "alpha_buffer_init");
260
0
        return 0;
261
0
    }
262
0
    gx_set_device_only(pgs, (gx_device *) mdev);
263
0
    scale_paths(pgs, log2_scale.x, log2_scale.y, true);
264
0
    return 1;
265
0
}
266
267
/* Release an alpha buffer. */
268
static int
269
alpha_buffer_release(gs_gstate * pgs, bool newpath)
270
0
{
271
0
    gx_device_memory *mdev =
272
0
        (gx_device_memory *) gs_currentdevice_inline(pgs);
273
0
    int code = (*dev_proc(mdev, close_device)) ((gx_device *) mdev);
274
275
0
    if (code >= 0)
276
0
        scale_paths(pgs, -mdev->log2_scale.x, -mdev->log2_scale.y,
277
0
                !(newpath && !gx_path_is_shared(pgs->path)));
278
    /* Reference counting will free mdev. */
279
0
    gx_set_device_only(pgs, mdev->target);
280
0
    return code;
281
0
}
282
283
/* Setup for black vector handling */
284
static inline bool black_vectors(gs_gstate *pgs, gx_device *dev)
285
77.3M
{
286
77.3M
    if (dev->icc_struct != NULL && dev->icc_struct->blackvector &&
287
0
        pgs->black_textvec_state == NULL) {
288
0
        return gsicc_setup_blacktextvec(pgs, dev, false);
289
0
    }
290
77.3M
    return false;
291
77.3M
}
292
293
static int do_fill(gs_gstate *pgs, int rule)
294
78.6M
{
295
78.6M
    int code, abits, acode, rcode = 0;
296
78.6M
    bool devn;
297
78.6M
    bool black_vector = false;
298
78.6M
    bool in_smask =
299
78.6M
        (dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_in_smask_construction, NULL, 0)) > 0;
300
301
    /* We need to distinguish text from vectors to set the object tag.
302
303
       To make that determination, we check for the show graphics state being stored
304
       in the current graphics state. This works even in the case of a glyph from a
305
       Type 3 Postscript/PDF font which has multiple, nested gsave/grestore pairs in
306
       the BuildGlyph/BuildChar procedure. Also, it works in the case of operating
307
       without a glyph cache or bypassing the cache because the glyph is too large or
308
       the cache being already full.
309
310
       Note that it doesn't work for a construction like:
311
       "(xyz) true charpath fill/stroke"
312
       where the show machinations have completed before we get to the fill operation.
313
       This has implications for how we handle PDF text rendering modes 1 and 2. To
314
       handle that, we'll have to add a flag to the path structure, or to the path
315
       segment structure (depending on how fine grained we require it to be).
316
     */
317
78.6M
    if (pgs->show_gstate == NULL && !in_smask) {
318
74.7M
        ensure_tag_is_set(pgs, pgs->device, GS_VECTOR_TAG); /* NB: may unset_dev_color */
319
74.7M
        black_vector = black_vectors(pgs, pgs->device); /* Set vector fill to black */
320
74.7M
    } else
321
3.86M
        ensure_tag_is_set(pgs, pgs->device, GS_TEXT_TAG); /* NB: may unset_dev_color */
322
323
78.6M
    code = gx_set_dev_color(pgs);
324
78.6M
    if (code != 0)
325
94.2k
        goto out;
326
78.5M
    code = gs_gstate_color_load(pgs);
327
78.5M
    if (code < 0)
328
0
        goto out;
329
330
78.5M
    if (pgs->overprint || dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_overprint_active, NULL, 0) > 0) {
331
206k
        gs_overprint_params_t op_params = { 0 };
332
333
206k
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
334
206k
            "[overprint] Fill Overprint\n");
335
206k
        code = gs_do_set_overprint(pgs);
336
206k
        if (code < 0)
337
0
            goto out;
338
339
206k
        op_params.op_state = GS_OP_STATE_FILL;
340
206k
        gs_gstate_update_overprint(pgs, &op_params);
341
206k
    }
342
343
78.5M
    abits = 0;
344
78.5M
    {
345
78.5M
        gx_device_color *col = gs_currentdevicecolor_inline(pgs);
346
78.5M
        devn = color_is_devn(col);
347
78.5M
        if (color_is_pure(col) || devn)
348
8.25M
            abits = alpha_buffer_bits(pgs);
349
78.5M
    }
350
78.5M
    if (abits > 1) {
351
0
        acode = alpha_buffer_init(pgs, pgs->fill_adjust.x,
352
0
                                  pgs->fill_adjust.y, abits, devn);
353
0
        if (acode == 2) { /* Special case for no fill required */
354
0
            code = 0;
355
0
            goto out;
356
0
        }
357
0
        if (acode < 0) {
358
0
            code = acode;
359
0
            goto out;
360
0
        }
361
0
    } else
362
78.5M
        acode = 0;
363
78.5M
    code = gx_fill_path(pgs->path, gs_currentdevicecolor_inline(pgs), pgs, rule,
364
78.5M
                        pgs->fill_adjust.x, pgs->fill_adjust.y);
365
78.5M
    if (acode > 0)
366
0
        rcode = alpha_buffer_release(pgs, code >= 0);
367
78.5M
    if (code >= 0 && rcode < 0)
368
0
        code = rcode;
369
370
78.6M
out:
371
78.6M
    if (black_vector) {
372
        /* Restore color */
373
0
        gsicc_restore_blacktextvec(pgs, false);
374
0
    }
375
376
78.6M
    return code;
377
78.5M
}
378
379
/* Fill the current path using a specified rule. */
380
static int
381
fill_with_rule(gs_gstate * pgs, int rule)
382
80.1M
{
383
80.1M
    int code;
384
385
    /* If we're inside a charpath, just merge the current path */
386
    /* into the parent's path. */
387
80.1M
    if (pgs->in_charpath)
388
0
        code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
389
0
                                     pgs->in_charpath);
390
            /* If we're rendering a glyph cached, the show machinery decides
391
             * whether to actually image it on the output or not, but uncached
392
             * will render directly to the output, so for text rendering
393
             * mode 3, we have to short circuit it here, but keep the
394
             * current point
395
             */
396
80.1M
    else if (gs_is_null_device(pgs->device)
397
78.6M
            || (pgs->show_gstate && pgs->text_rendering_mode == 3
398
1.45M
            && pgs->in_cachedevice == CACHE_DEVICE_NOT_CACHING)) {
399
        /* Handle separately to prevent gs_gstate_color_load - bug 688308. */
400
1.45M
        gs_newpath(pgs);
401
1.45M
        code = 0;
402
78.6M
    } else {
403
78.6M
        code = do_fill(pgs, rule);
404
78.6M
        if (code >= 0)
405
78.5M
            code = gs_newpath(pgs);
406
78.6M
    }
407
80.1M
    return code;
408
80.1M
}
409
/* Fill using the winding number rule */
410
int
411
gs_fill(gs_gstate * pgs)
412
8.74M
{
413
8.74M
    pgs->device->sgr.stroke_stored = false;
414
8.74M
    return fill_with_rule(pgs, gx_rule_winding_number);
415
8.74M
}
416
/* Fill using the even/odd rule */
417
int
418
gs_eofill(gs_gstate * pgs)
419
71.3M
{
420
71.3M
    pgs->device->sgr.stroke_stored = false;
421
71.3M
    return fill_with_rule(pgs, gx_rule_even_odd);
422
71.3M
}
423
424
static int
425
do_stroke(gs_gstate * pgs)
426
2.47M
{
427
2.47M
    int code, abits, acode, rcode = 0;
428
2.47M
    bool devn;
429
2.47M
    bool is_fill_correct = true;
430
2.47M
    bool black_vector = false;
431
2.47M
    bool in_smask =
432
2.47M
        (dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_in_smask_construction, NULL, 0)) > 0;
433
434
435
    /* We need to distinguish text from vectors to set the object tag.
436
437
       To make that determination, we check for the show graphics state being stored
438
       in the current graphics state. This works even in the case of a glyph from a
439
       Type 3 Postscript/PDF font which has multiple, nested gsave/grestore pairs in
440
       the BuildGlyph/BuildChar procedure. Also, it works in the case of operating
441
       without a glyph cache or bypassing the cache because the glyph is too large or
442
       the cache being already full.
443
444
       Note that it doesn't work for a construction like:
445
       "(xyz) true charpath fill/stroke"
446
       where the show machinations have completed before we get to the fill operation.
447
       This has implications for how we handle PDF text rendering modes 1 and 2. To
448
       handle that, we'll have to add a flag to the path structure, or to the path
449
       segment structure (depending on how fine grained we require it to be).
450
     */
451
2.47M
    if (pgs->show_gstate == NULL && !in_smask) {
452
2.46M
        ensure_tag_is_set(pgs, pgs->device, GS_VECTOR_TAG); /* NB: may unset_dev_color */
453
2.46M
        black_vector = black_vectors(pgs, pgs->device);
454
2.46M
    } else
455
3.12k
        ensure_tag_is_set(pgs, pgs->device, GS_TEXT_TAG); /* NB: may unset_dev_color */
456
457
2.47M
    code = gx_set_dev_color(pgs);
458
2.47M
    if (code != 0)
459
6.32k
        goto out;
460
2.46M
    code = gs_gstate_color_load(pgs);
461
2.46M
    if (code < 0)
462
0
        goto out;
463
464
465
2.46M
    if (pgs->stroke_overprint || dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_overprint_active, NULL, 0) > 0) {
466
122k
        gs_overprint_params_t op_params = { 0 };
467
468
        /* PS2 does not have the concept of fill and stroke colors. Here we need to possibly correct
469
           for that in the graphic state during this operation */
470
122k
        if (pgs->is_fill_color) {
471
0
            is_fill_correct = false;
472
0
            pgs->is_fill_color = false;
473
0
        }
474
475
476
122k
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
477
122k
            "[overprint] Stroke Overprint\n");
478
122k
        code = gs_do_set_overprint(pgs);
479
122k
        if (code < 0) {
480
0
            if (!is_fill_correct) {
481
0
                pgs->is_fill_color = true;
482
0
            }
483
0
            goto out;
484
0
        }
485
486
122k
        op_params.op_state = GS_OP_STATE_STROKE;
487
122k
        gs_gstate_update_overprint(pgs, &op_params);
488
122k
    }
489
490
2.46M
    abits = 0;
491
2.46M
    {
492
2.46M
        gx_device_color *col = gs_currentdevicecolor_inline(pgs);
493
2.46M
        devn = color_is_devn(col);
494
2.46M
        if (color_is_pure(col) || devn)
495
2.27M
            abits = alpha_buffer_bits(pgs);
496
2.46M
    }
497
2.46M
    if (abits > 1) {
498
        /*
499
         * Expand the bounding box by the line width.
500
         * This is expensive to compute, so we only do it
501
         * if we know we're going to buffer.
502
         */
503
0
        float xxyy = fabs(pgs->ctm.xx) + fabs(pgs->ctm.yy);
504
0
        float xyyx = fabs(pgs->ctm.xy) + fabs(pgs->ctm.yx);
505
0
        float scale = (float)(1 << (abits / 2));
506
0
        float orig_width = gs_currentlinewidth(pgs);
507
0
        float new_width = orig_width * scale;
508
0
        fixed extra_adjust =
509
0
                float2fixed(max(xxyy, xyyx) * new_width / 2);
510
0
        float orig_flatness = gs_currentflat(pgs);
511
0
        gx_path spath;
512
513
        /* Scale up the line width, dash pattern, and flatness. */
514
0
        if (extra_adjust < fixed_1)
515
0
            extra_adjust = fixed_1;
516
0
        acode = alpha_buffer_init(pgs,
517
0
                                  pgs->fill_adjust.x + extra_adjust,
518
0
                                  pgs->fill_adjust.y + extra_adjust,
519
0
                                  abits, devn);
520
0
        if (acode == 2) {            /* Special code meaning no fill required */
521
0
            if (!is_fill_correct) {
522
0
                pgs->is_fill_color = true;
523
0
            }
524
0
            code = 0;
525
0
            goto out;
526
0
        }
527
0
        if (acode < 0) {
528
0
            if (!is_fill_correct) {
529
0
                pgs->is_fill_color = true;
530
0
            }
531
0
            code = acode;
532
0
            goto out;
533
0
        }
534
0
        gs_setlinewidth(pgs, new_width);
535
0
        scale_dash_pattern(pgs, scale);
536
0
        gs_setflat(pgs, (double)(orig_flatness * scale));
537
        /*
538
         * The alpha-buffer device requires that we fill the
539
         * entire path as a single unit.
540
         */
541
0
        gx_path_init_local(&spath, pgs->memory);
542
0
        code = gx_stroke_add(pgs->path, &spath, pgs, false);
543
0
        gs_setlinewidth(pgs, orig_width);
544
0
        scale_dash_pattern(pgs, 1.0 / scale);
545
0
        if (code >= 0)
546
0
            code = gx_fill_path(&spath, gs_currentdevicecolor_inline(pgs), pgs,
547
0
                                gx_rule_winding_number,
548
0
                                pgs->fill_adjust.x,
549
0
                                pgs->fill_adjust.y);
550
0
        gs_setflat(pgs, orig_flatness);
551
0
        gx_path_free(&spath, "gs_stroke");
552
0
        if (acode > 0)
553
0
            rcode = alpha_buffer_release(pgs, code >= 0);
554
0
    } else
555
2.46M
        code = gx_stroke_fill(pgs->path, pgs);
556
2.46M
    if (code >= 0 && rcode < 0)
557
0
        code = rcode;
558
559
2.46M
    if (!is_fill_correct) {
560
0
        pgs->is_fill_color = true;
561
0
    }
562
563
2.47M
out:
564
2.47M
    if (black_vector) {
565
        /* Restore color */
566
0
        gsicc_restore_blacktextvec(pgs, false);
567
0
    }
568
2.47M
    return code;
569
2.46M
}
570
571
/* Stroke the current path */
572
int
573
gs_stroke(gs_gstate * pgs)
574
5.09M
{
575
5.09M
    int code;
576
577
    /*
578
     * If we're inside a charpath, just merge the current path
579
     * into the parent's path.
580
     */
581
5.09M
    if (pgs->in_charpath) {
582
492k
        if (pgs->in_charpath == cpm_true_charpath) {
583
            /*
584
             * A stroke inside a true charpath should do the
585
             * equivalent of strokepath.
586
             */
587
492k
            code = gs_strokepath(pgs);
588
492k
            if (code < 0)
589
0
                return code;
590
492k
        }
591
492k
        code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
592
492k
                                     pgs->in_charpath);
593
492k
        if (code < 0)
594
0
            return code;
595
492k
    }
596
5.09M
    if (!gs_is_null_device(pgs->device)) {
597
2.47M
        code = do_stroke(pgs);
598
2.47M
        if (code < 0)
599
6.33k
            return code;
600
2.47M
    }
601
5.09M
    return gs_newpath(pgs);
602
5.09M
}
603
604
/* Compute the stroked outline of the current path */
605
static int
606
gs_strokepath_aux(gs_gstate * pgs, bool traditional)
607
493k
{
608
493k
    gx_path spath;
609
493k
    int code;
610
611
493k
    gx_path_init_local(&spath, pgs->path->memory);
612
493k
    code = gx_stroke_add(pgs->path, &spath, pgs, traditional);
613
493k
    if (code < 0) {
614
0
        gx_path_free(&spath, "gs_strokepath");
615
0
        return code;
616
0
    }
617
493k
    pgs->device->sgr.stroke_stored = false;
618
493k
    code = gx_path_assign_free(pgs->path, &spath);
619
493k
    if (code < 0)
620
0
        return code;
621
    /* NB: needs testing with PCL */
622
493k
    if (gx_path_is_void(pgs->path))
623
3.57k
        pgs->current_point_valid = false;
624
489k
    else {
625
489k
        gx_setcurrentpoint(pgs, fixed2float(spath.position.x), fixed2float(spath.position.y));
626
489k
    }
627
493k
    return 0;
628
629
493k
}
630
631
int
632
gs_strokepath(gs_gstate * pgs)
633
493k
{
634
493k
    return gs_strokepath_aux(pgs, true);
635
493k
}
636
637
int
638
gs_strokepath2(gs_gstate * pgs)
639
0
{
640
0
    return gs_strokepath_aux(pgs, false);
641
0
}
642
643
static int do_fill_stroke(gs_gstate *pgs, int rule, int *restart)
644
61.0k
{
645
61.0k
    int code, abits, acode = 0, rcode = 0;
646
61.0k
    bool devn;
647
61.0k
    float orig_width, scale, orig_flatness;
648
61.0k
    bool black_vector = false;
649
61.0k
    bool in_smask =
650
61.0k
        (dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_in_smask_construction, NULL, 0)) > 0;
651
61.0k
    gs_logical_operation_t orig_lop = pgs->log_op;
652
653
    /* It is either our first time, or the stroke was a pattern and
654
       we are coming back from the error if restart < 1 (0 is first
655
       time, 1 stroke is set, and we only need to finish out fill */
656
61.0k
    if (pgs->is_fill_color) {
657
        /* if the fill_color is a pattern, make sure the tile is locked so that */
658
        /* it does not get evicted as the stroke color is loaded.   */
659
61.0k
        if (gx_dc_is_pattern1_color(gs_currentdevicecolor_inline(pgs))) {
660
262
            gs_id id;
661
662
262
            if(gs_currentdevicecolor_inline(pgs)->colors.pattern.p_tile != NULL) {
663
154
                id = gs_currentdevicecolor_inline(pgs)->colors.pattern.p_tile->id;
664
154
                code = dev_proc(pgs->device, lock_pattern)(pgs->device, pgs, id, true);
665
154
            } else {
666
108
                code = 0;
667
108
            }
668
262
      if (code < 0)
669
0
    return code; /* lock failed -- tile not in cache? */
670
262
        }
671
61.0k
        gs_swapcolors_quick(pgs); /* switch to the stroke color */
672
61.0k
    }
673
674
61.0k
    if (*restart < 1) {
675
676
        /* We need to distinguish text from vectors to set the object tag.
677
678
           To make that determination, we check for the show graphics state being stored
679
           in the current graphics state. This works even in the case of a glyph from a
680
           Type 3 Postscript/PDF font which has multiple, nested gsave/grestore pairs in
681
           the BuildGlyph/BuildChar procedure. Also, it works in the case of operating
682
           without a glyph cache or bypassing the cache because the glyph is too large or
683
           the cache being already full.
684
685
           Note that it doesn't work for a construction like:
686
           "(xyz) true charpath fill/stroke"
687
           where the show machinations have completed before we get to the fill operation.
688
           This has implications for how we handle PDF text rendering modes 1 and 2. To
689
           handle that, we'll have to add a flag to the path structure, or to the path
690
           segment structure (depending on how fine grained we require it to be).
691
         */
692
61.0k
        if (pgs->show_gstate == NULL && !in_smask) {
693
60.5k
            ensure_tag_is_set(pgs, pgs->device, GS_VECTOR_TAG); /* NB: may unset_dev_color */
694
60.5k
            black_vector = black_vectors(pgs, pgs->device);
695
60.5k
        } else
696
485
            ensure_tag_is_set(pgs, pgs->device, GS_TEXT_TAG); /* NB: may unset_dev_color */
697
698
        /* if we are at restart == 0, we set the stroke color. */
699
61.0k
        code = gx_set_dev_color(pgs);
700
61.0k
        if (code != 0)
701
246
            goto out2;    /* may be gs_error_Remap_color or real error */
702
60.7k
        code = gs_gstate_color_load(pgs);
703
60.7k
        if (code < 0)
704
0
            goto out2;
705
        /* If this was a pattern color, make sure and lock it in the pattern_cache */
706
60.7k
        if (gx_dc_is_pattern1_color(gs_currentdevicecolor_inline(pgs))) {
707
0
            gs_id id;
708
709
0
            if(gs_currentdevicecolor_inline(pgs)->colors.pattern.p_tile != NULL) {
710
0
                id = gs_currentdevicecolor_inline(pgs)->colors.pattern.p_tile->id;
711
0
                code = dev_proc(pgs->device, lock_pattern)(pgs->device, pgs, id, true);
712
0
            } else {
713
0
                code = 0;
714
0
            }
715
0
            if (code < 0)
716
0
                goto out2; /* lock failed -- tile not in cache? */
717
0
        }
718
60.7k
    }
719
720
60.7k
    if (pgs->stroke_overprint || dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_overprint_active, NULL, 0) > 0) {
721
59
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
722
59
            "[overprint] StrokeFill Stroke Set Overprint\n");
723
59
        code = gs_do_set_overprint(pgs);
724
59
        if (code < 0)
725
0
            goto out2;
726
59
    }
727
60.7k
    *restart = 1;   /* finished, successfully with stroke_color */
728
729
60.7k
    gs_swapcolors_quick(pgs); /* switch to fill color */
730
731
    /* Have to set the fill color too */
732
60.7k
    if (pgs->show_gstate == NULL)
733
60.2k
        ensure_tag_is_set(pgs, pgs->device, GS_VECTOR_TAG);  /* NB: may unset_dev_color */
734
472
    else
735
472
        ensure_tag_is_set(pgs, pgs->device, GS_TEXT_TAG); /* NB: may unset_dev_color */
736
737
60.7k
    code = gx_set_dev_color(pgs);
738
60.7k
    if (code != 0) {
739
529
        goto out2;
740
529
    }
741
60.2k
    code = gs_gstate_color_load(pgs);
742
60.2k
    if (code < 0) {
743
        /* color is set for fill, but a failure here is a problem */
744
        /* i.e., something other than error_Remap_Color */
745
0
        *restart = 2; /* we shouldn't re-enter with '2' */
746
0
        goto out;
747
0
    }
748
749
60.2k
    if (pgs->overprint || dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_overprint_active, NULL, 0) > 0) {
750
70
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
751
70
            "[overprint] StrokeFill Fill Set Overprint\n");
752
70
        code = gs_do_set_overprint(pgs);
753
70
        if (code < 0)
754
0
            goto out;   /* fatal */
755
70
    }
756
757
60.2k
    abits = 0;
758
60.2k
    {
759
60.2k
        gx_device_color *col_fill = gs_currentdevicecolor_inline(pgs);
760
60.2k
        gx_device_color *col_stroke = gs_swappeddevicecolor_inline(pgs);
761
60.2k
        devn = color_is_devn(col_fill) && color_is_devn(col_stroke);
762
        /* could be devn and masked_devn */
763
60.2k
        if ((color_is_pure(col_fill) && color_is_pure(col_stroke)) || devn)
764
51.0k
            abits = alpha_buffer_bits(pgs);
765
60.2k
    }
766
60.2k
    if (abits > 1) {
767
        /*
768
         * Expand the bounding box by the line width.
769
         * This is expensive to compute, so we only do it
770
         * if we know we're going to buffer.
771
         */
772
0
        float new_width;
773
0
        fixed extra_adjust;
774
0
        float xxyy = fabs(pgs->ctm.xx) + fabs(pgs->ctm.yy);
775
0
        float xyyx = fabs(pgs->ctm.xy) + fabs(pgs->ctm.yx);
776
0
        pgs->log_op |= lop_pdf14; /* Force stroking to happen all in 1 go */
777
0
        scale = (float)(1 << (abits / 2));
778
0
        orig_width = gs_currentlinewidth(pgs);
779
0
        new_width = orig_width * scale;
780
0
        extra_adjust =
781
0
                float2fixed(max(xxyy, xyyx) * new_width / 2);
782
0
        orig_flatness = gs_currentflat(pgs);
783
784
        /* Scale up the line width, dash pattern, and flatness. */
785
0
        if (extra_adjust < fixed_1)
786
0
            extra_adjust = fixed_1;
787
0
        acode = alpha_buffer_init(pgs,
788
0
                                  pgs->fill_adjust.x + extra_adjust,
789
0
                                  pgs->fill_adjust.y + extra_adjust,
790
0
                                  abits, devn);
791
0
        if (acode == 2) /* Special case for no fill required */
792
0
            goto out;
793
0
        if (acode < 0)
794
0
            goto out;
795
0
        gs_setlinewidth(pgs, new_width);
796
0
        scale_dash_pattern(pgs, scale);
797
0
        gs_setflat(pgs, (double)(orig_flatness * scale));
798
0
    } else
799
60.2k
        acode = 0;
800
60.2k
    code = gx_fill_stroke_path(pgs, rule);
801
60.2k
    if (abits > 1)
802
0
    {
803
0
        gs_setlinewidth(pgs, orig_width);
804
0
        scale_dash_pattern(pgs, 1.0 / scale);
805
0
        gs_setflat(pgs, orig_flatness);
806
0
        acode = alpha_buffer_release(pgs, code >= 0);
807
0
        pgs->log_op = orig_lop;
808
0
    }
809
60.2k
    if (pgs->is_fill_color) {
810
        /* The color _should_ be the fill color, so make sure it is unlocked  */
811
60.2k
        if (gx_dc_is_pattern1_color(gs_currentdevicecolor_inline(pgs))) {
812
1.13k
            gs_id id;
813
814
1.13k
            if(gs_currentdevicecolor_inline(pgs)->colors.pattern.p_tile != NULL) {
815
540
                id = gs_currentdevicecolor_inline(pgs)->colors.pattern.p_tile->id;
816
540
                code = dev_proc(pgs->device, lock_pattern)(pgs->device, pgs, id, false);
817
599
            } else {
818
599
                code = 0;
819
599
            }
820
1.13k
      if (code < 0)
821
0
        goto out2; /* lock failed -- tile not in cache? */
822
1.13k
        }
823
60.2k
    }
824
60.2k
out:
825
60.2k
    if (black_vector) {
826
        /* Restore color */
827
0
        gsicc_restore_blacktextvec(pgs, false);
828
0
    }
829
830
60.2k
    if (gx_dc_is_pattern1_color(gs_swappeddevicecolor_inline(pgs))) {
831
0
        gs_id id;
832
833
0
        if (gs_swappeddevicecolor_inline(pgs)->colors.pattern.p_tile != NULL) {
834
0
            id = gs_swappeddevicecolor_inline(pgs)->colors.pattern.p_tile->id;
835
0
            rcode = dev_proc(pgs->device, lock_pattern)(pgs->device, pgs, id, false);
836
0
            if (rcode < 0)
837
0
          return rcode; /* unlock failed -- shouldn't be possible */
838
0
        } else {
839
0
            code = 0;
840
0
        }
841
0
    }
842
60.2k
    if (code >= 0 && acode < 0)
843
0
        code = acode;
844
60.2k
    return code;
845
846
775
out2:
847
775
    if (black_vector) {
848
        /* Restore color */
849
0
        gsicc_restore_blacktextvec(pgs, false);
850
0
    }
851
775
    return code;
852
60.2k
}
853
854
/* Fill the current path using a specified rule. */
855
static int
856
fill_stroke_with_rule(gs_gstate * pgs, int rule, int *restart)
857
61.0k
{
858
61.0k
    int code;
859
860
    /* If we're inside a charpath, just merge the current path */
861
    /* into the parent's path. */
862
61.0k
    if (pgs->in_charpath) {
863
        /* If we're rendering a glyph cached, the show machinery decides
864
         * whether to actually image it on the output or not, but uncached
865
         * will render directly to the output, so for text rendering
866
         * mode 3, we have to short circuit it here, but keep the
867
         * current point
868
         */
869
0
        *restart = 0;
870
0
        code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
871
0
                                     pgs->in_charpath);
872
0
        if (code < 0)
873
0
            return code;
874
0
        if (pgs->in_charpath == cpm_true_charpath) {
875
            /*
876
             * A stroke inside a true charpath should do the
877
             * equivalent of strokepath.
878
             */
879
0
            code = gs_strokepath(pgs);
880
0
            if (code < 0)
881
0
                return code;
882
0
            code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
883
0
                                         pgs->in_charpath);
884
0
            if (code < 0)
885
0
                return code;
886
0
        }
887
0
    }
888
61.0k
    else if (gs_is_null_device(pgs->device) ||
889
61.0k
             (pgs->show_gstate && pgs->text_rendering_mode == 3 &&
890
19
              pgs->in_cachedevice == CACHE_DEVICE_NOT_CACHING)) {
891
        /* Text Rendering Mode = 3 => Neither stroke, nor fill */
892
        /* Handle separately to prevent gs_gstate_color_load - bug 688308. */
893
19
        *restart = 0;
894
19
        gs_newpath(pgs);
895
19
        code = 0;
896
61.0k
    } else {
897
61.0k
        code = do_fill_stroke(pgs, rule, restart);
898
61.0k
        if (code >= 0)
899
60.2k
            gs_newpath(pgs);
900
61.0k
    }
901
61.0k
    return code;
902
61.0k
}
903
/* Fill using the winding number rule */
904
int
905
gs_fillstroke(gs_gstate * pgs, int *restart)
906
58.8k
{
907
58.8k
    pgs->device->sgr.stroke_stored = false;
908
58.8k
    return fill_stroke_with_rule(pgs, gx_rule_winding_number, restart);
909
58.8k
}
910
/* Fill using the even/odd rule */
911
int
912
gs_eofillstroke(gs_gstate * pgs, int *restart)
913
2.14k
{
914
2.14k
    pgs->device->sgr.stroke_stored = false;
915
2.14k
    return fill_stroke_with_rule(pgs, gx_rule_even_odd, restart);
916
2.14k
}