Coverage Report

Created: 2026-08-08 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gsdps1.c
Line
Count
Source
1
/* Copyright (C) 2001-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
17
/* Display PostScript graphics additions for Ghostscript library */
18
#include "math_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gsmatrix.h"   /* for gscoord.h */
22
#include "gscoord.h"
23
#include "gspaint.h"
24
#include "gxdevice.h"
25
#include "gxfixed.h"
26
#include "gxmatrix.h"
27
#include "gxhldevc.h"
28
#include "gspath.h"
29
#include "gspath2.h"    /* defines interface */
30
#include "gzpath.h"
31
#include "gzcpath.h"
32
#include "gzstate.h"
33
#include "gsutil.h"
34
#include "gxdevsop.h"
35
36
/*
37
 * Define how much rounding slop setbbox should leave,
38
 * in device coordinates.  Because of rounding in transforming
39
 * path coordinates to fixed point, the minimum realistic value is:
40
 *
41
 *      #define box_rounding_slop_fixed (fixed_epsilon)
42
 *
43
 * But even this isn't enough to compensate for cumulative rounding error
44
 * in rmoveto or rcurveto.  Instead, we somewhat arbitrarily use:
45
 */
46
24
#define box_rounding_slop_fixed (fixed_epsilon * 3)
47
48
/* ------ Graphics state ------ */
49
50
/* Set the bounding box for the current path. */
51
int
52
gs_setbbox(gs_gstate * pgs, double llx, double lly, double urx, double ury)
53
19
{
54
19
    gs_rect ubox, dbox;
55
19
    gs_fixed_rect obox, bbox;
56
19
    gx_path *ppath = pgs->path;
57
19
    int code;
58
59
19
    if (llx > urx || lly > ury)
60
6
        return_error(gs_error_rangecheck);
61
    /* Transform box to device coordinates. */
62
13
    ubox.p.x = llx;
63
13
    ubox.p.y = lly;
64
13
    ubox.q.x = urx;
65
13
    ubox.q.y = ury;
66
13
    if ((code = gs_bbox_transform(&ubox, &ctm_only(pgs), &dbox)) < 0)
67
0
        return code;
68
    /* Round the corners in opposite directions. */
69
    /* Because we can't predict the magnitude of the dbox values, */
70
    /* we add/subtract the slop after fixing. */
71
13
    if (dbox.p.x < fixed2float(min_fixed + box_rounding_slop_fixed) ||
72
11
        dbox.p.y < fixed2float(min_fixed + box_rounding_slop_fixed) ||
73
10
        dbox.q.x >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon) ||
74
8
        dbox.q.y >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon)
75
13
        )
76
7
        return_error(gs_error_limitcheck);
77
6
    bbox.p.x =
78
6
        (fixed) floor(dbox.p.x * fixed_scale) - box_rounding_slop_fixed;
79
6
    bbox.p.y =
80
6
        (fixed) floor(dbox.p.y * fixed_scale) - box_rounding_slop_fixed;
81
6
    bbox.q.x =
82
6
        (fixed) ceil(dbox.q.x * fixed_scale) + box_rounding_slop_fixed;
83
6
    bbox.q.y =
84
6
        (fixed) ceil(dbox.q.y * fixed_scale) + box_rounding_slop_fixed;
85
6
    if (gx_path_bbox_set(ppath, &obox) >= 0) { /* Take the union of the bboxes. */
86
1
        ppath->bbox.p.x = min(obox.p.x, bbox.p.x);
87
1
        ppath->bbox.p.y = min(obox.p.y, bbox.p.y);
88
1
        ppath->bbox.q.x = max(obox.q.x, bbox.q.x);
89
1
        ppath->bbox.q.y = max(obox.q.y, bbox.q.y);
90
5
    } else {     /* empty path *//* Just set the bbox. */
91
5
        ppath->bbox = bbox;
92
5
    }
93
6
    ppath->bbox_set = 1;
94
6
    return 0;
95
13
}
96
97
/* ------ Rectangles ------ */
98
99
/* Append a list of rectangles to a path. */
100
static int
101
gs_rectappend_compat(gs_gstate * pgs, const gs_rect * pr, uint count, bool clip)
102
356k
{
103
356k
    bool CPSI_mode = gs_currentcpsimode(pgs->memory);
104
105
712k
    for (; count != 0; count--, pr++) {
106
356k
        double px = pr->p.x, py = pr->p.y, qx = pr->q.x, qy = pr->q.y;
107
356k
        int code;
108
109
356k
        if (CPSI_mode) {
110
            /* We believe that the result must be independent
111
               on the device initial matrix.
112
               Particularly for the correct dashing
113
               the starting point and the contour direction
114
               must be same with any device initial matrix.
115
               Only way to provide it is to choose the starting point
116
               and the direction in the user space. */
117
0
            if (clip) {
118
                /* CPSI starts a clippath with the upper right corner. */
119
                /* Debugged with CET 11-11.PS page 6 item much13.*/
120
0
                if ((code = gs_moveto(pgs, qx, qy)) < 0 ||
121
0
                    (code = gs_lineto(pgs, qx, py)) < 0 ||
122
0
                    (code = gs_lineto(pgs, px, py)) < 0 ||
123
0
                    (code = gs_lineto(pgs, px, qy)) < 0 ||
124
0
                    (code = gs_closepath(pgs)) < 0
125
0
                    )
126
0
                    return code;
127
0
            } else {
128
                /* Debugged with CET 12-12.PS page 10 item more20.*/
129
0
                if (px > qx) {
130
0
                    px = qx; qx = pr->p.x;
131
0
                }
132
0
                if (py > qy) {
133
0
                    py = qy; qy = pr->p.y;
134
0
                }
135
0
                if ((code = gs_moveto(pgs, px, py)) < 0 ||
136
0
                    (code = gs_lineto(pgs, qx, py)) < 0 ||
137
0
                    (code = gs_lineto(pgs, qx, qy)) < 0 ||
138
0
                    (code = gs_lineto(pgs, px, qy)) < 0 ||
139
0
                    (code = gs_closepath(pgs)) < 0
140
0
                    )
141
0
                    return code;
142
0
            }
143
356k
        } else {
144
            /* Ensure counter-clockwise drawing. */
145
356k
            if ((qx >= px) != (qy >= py))
146
4.95k
                qx = px, px = pr->q.x; /* swap x values */
147
356k
            if ((code = gs_moveto(pgs, px, py)) < 0 ||
148
356k
                (code = gs_lineto(pgs, qx, py)) < 0 ||
149
356k
                (code = gs_lineto(pgs, qx, qy)) < 0 ||
150
356k
                (code = gs_lineto(pgs, px, qy)) < 0 ||
151
356k
                (code = gs_closepath(pgs)) < 0
152
356k
                )
153
0
                return code;
154
356k
        }
155
356k
    }
156
356k
    return 0;
157
356k
}
158
int
159
gs_rectappend(gs_gstate * pgs, const gs_rect * pr, uint count)
160
39.4k
{
161
39.4k
    return gs_rectappend_compat(pgs, pr, count, false);
162
39.4k
}
163
164
/* Clip to a list of rectangles. */
165
int
166
gs_rectclip(gs_gstate * pgs, const gs_rect * pr, uint count)
167
316k
{
168
316k
    int code;
169
316k
    gx_path save;
170
171
316k
    gx_path_init_local(&save, pgs->memory);
172
316k
    gx_path_assign_preserve(&save, pgs->path);
173
316k
    gs_newpath(pgs);
174
316k
    if ((code = gs_rectappend_compat(pgs, pr, count, true)) < 0 ||
175
316k
        (code = gs_clip(pgs)) < 0
176
316k
        ) {
177
0
        gx_path_assign_free(pgs->path, &save);
178
0
        return code;
179
0
    }
180
316k
    gx_path_free(&save, "gs_rectclip");
181
316k
    gs_newpath(pgs);
182
316k
    return 0;
183
316k
}
184
185
/* Setup for black vector handling */
186
static inline bool black_vectors(gs_gstate *pgs, gx_device *dev)
187
47.2k
{
188
47.2k
    if (dev->icc_struct != NULL && dev->icc_struct->blackvector &&
189
0
        pgs->black_textvec_state == NULL) {
190
0
        return gsicc_setup_blacktextvec(pgs, dev, false);
191
0
    }
192
47.2k
    return false;
193
47.2k
}
194
195
/* Fill a list of rectangles. */
196
/* We take the trouble to do this efficiently in the simple cases. */
197
int
198
gs_rectfill(gs_gstate * pgs, const gs_rect * pr, uint count)
199
47.2k
{
200
47.2k
    const gs_rect *rlist = pr;
201
47.2k
    gx_clip_path *pcpath;
202
47.2k
    uint rcount = count;
203
47.2k
    int code;
204
47.2k
    gx_device * pdev = pgs->device;
205
47.2k
    gx_device_color *pdc = gs_currentdevicecolor_inline(pgs);
206
47.2k
    const gs_gstate *pgs2 = (const gs_gstate *)pgs;
207
47.2k
    bool hl_color_available = gx_hld_is_hl_color_available(pgs2, pdc);
208
47.2k
    bool hl_color = (hl_color_available && (dev_proc(pdev, dev_spec_op)(pdev, gxdso_supports_hlcolor, NULL, 0) > 0));
209
47.2k
    bool center_of_pixel = (pgs->fill_adjust.x == 0 && pgs->fill_adjust.y == 0);
210
47.2k
    bool black_vector = false;
211
212
    /* Processing a fill object operation */
213
47.2k
    ensure_tag_is_set(pgs, pgs->device, GS_VECTOR_TAG); /* NB: may unset_dev_color */
214
215
47.2k
    black_vector = black_vectors(pgs, pgs->device); /* Set vector fill to black */
216
217
47.2k
    code = gx_set_dev_color(pgs);
218
47.2k
    if (code != 0)
219
2
        goto exit;
220
221
47.2k
    if ( !(pgs->device->page_uses_transparency ||
222
47.2k
          dev_proc(pgs->device, dev_spec_op)(pgs->device,
223
47.2k
              gxdso_is_pdf14_device, &(pgs->device),
224
47.2k
              sizeof(pgs->device))) &&
225
46.8k
        (is_fzero2(pgs->ctm.xy, pgs->ctm.yx) ||
226
11
         is_fzero2(pgs->ctm.xx, pgs->ctm.yy)) &&
227
46.7k
        gx_effective_clip_path(pgs, &pcpath) >= 0 &&
228
46.7k
        clip_list_is_rectangle(gx_cpath_list(pcpath)) &&
229
46.7k
        (hl_color ||
230
37.1k
         pdc->type == gx_dc_type_pure ||
231
10.7k
         pdc->type == gx_dc_type_ht_binary ||
232
5.23k
         pdc->type == gx_dc_type_ht_colored) &&
233
45.1k
        gs_gstate_color_load(pgs) >= 0 &&
234
45.1k
        (*dev_proc(pdev, get_alpha_bits)) (pdev, go_graphics)
235
45.1k
        <= 1 &&
236
45.1k
        (!pgs->overprint || !gs_currentcolor_eopm(pgs))
237
47.2k
        ) {
238
45.1k
        uint i;
239
45.1k
        gs_fixed_rect clip_rect;
240
241
45.1k
        gx_cpath_inner_box(pcpath, &clip_rect);
242
        /* We should never plot anything for an empty clip rectangle */
243
45.1k
        if ((clip_rect.p.x >= clip_rect.q.x) &&
244
26
            (clip_rect.p.y >= clip_rect.q.y))
245
26
            goto exit;
246
83.3k
        for (i = 0; i < count; ++i) {
247
45.1k
            gs_fixed_point p, q;
248
45.1k
            gs_fixed_rect draw_rect;
249
250
45.1k
            if (gs_point_transform2fixed(&pgs->ctm, pr[i].p.x, pr[i].p.y, &p) < 0 ||
251
43.3k
                gs_point_transform2fixed(&pgs->ctm, pr[i].q.x, pr[i].q.y, &q) < 0
252
45.1k
                ) {   /* Switch to the slow algorithm. */
253
6.36k
                goto slow;
254
6.36k
            }
255
38.7k
            draw_rect.p.x = min(p.x, q.x);
256
38.7k
            draw_rect.p.y = min(p.y, q.y);
257
38.7k
            draw_rect.q.x = max(p.x, q.x);
258
38.7k
            draw_rect.q.y = max(p.y, q.y);
259
38.7k
            if (hl_color) {
260
7.82k
                rect_intersect(draw_rect, clip_rect);
261
                /* We do pass on 0 extant rectangles to high level
262
                   devices.  It isn't clear how a client and an output
263
                   device should interact if one uses a center of
264
                   pixel algorithm and the other uses any part of
265
                   pixel.  For now we punt and just pass the high
266
                   level rectangle on without adjustment. */
267
7.82k
                if (draw_rect.p.x <= draw_rect.q.x &&
268
7.38k
                    draw_rect.p.y <= draw_rect.q.y) {
269
7.23k
                    code = dev_proc(pdev, fill_rectangle_hl_color)(pdev,
270
7.23k
                             &draw_rect, pgs2, pdc, pcpath);
271
7.23k
                    if (code < 0)
272
0
                        goto exit;
273
7.23k
                }
274
30.9k
            } else {
275
30.9k
                int x, y, w, h;
276
277
30.9k
                rect_intersect(draw_rect, clip_rect);
278
30.9k
                if (center_of_pixel) {
279
4.72k
                    draw_rect.p.x = fixed_rounded(draw_rect.p.x);
280
4.72k
                    draw_rect.p.y = fixed_rounded(draw_rect.p.y);
281
4.72k
                    draw_rect.q.x = fixed_rounded(draw_rect.q.x);
282
4.72k
                    draw_rect.q.y = fixed_rounded(draw_rect.q.y);
283
26.2k
                } else { /* any part of pixel rule - touched */
284
26.2k
                    draw_rect.p.x = fixed_floor(draw_rect.p.x);
285
26.2k
                    draw_rect.p.y = fixed_floor(draw_rect.p.y);
286
26.2k
                    draw_rect.q.x = fixed_ceiling(draw_rect.q.x);
287
26.2k
                    draw_rect.q.y = fixed_ceiling(draw_rect.q.y);
288
26.2k
                }
289
30.9k
                x = fixed2int(draw_rect.p.x);
290
30.9k
                y = fixed2int(draw_rect.p.y);
291
30.9k
                w = fixed2int(draw_rect.q.x) - x;
292
30.9k
                h = fixed2int(draw_rect.q.y) - y;
293
                /* clients that use the "any part of pixel" rule also
294
                   fill 0 areas.  This is true of current graphics
295
                   library clients but not a general rule.  */
296
30.9k
                if (!center_of_pixel) {
297
26.2k
                    if (w == 0)
298
4.11k
                        w = 1;
299
                    /* yes Adobe Acrobat 8, seems to back up the y
300
                       coordinate when the width is 0, sigh. */
301
26.2k
                    if (h == 0) {
302
1.89k
                        y--;
303
1.89k
                        h = 1;
304
1.89k
                    }
305
26.2k
                }
306
30.9k
                if (gx_fill_rectangle(x, y, w, h, pdc, pgs) < 0)
307
635
                    goto slow;
308
30.9k
            }
309
38.7k
        }
310
38.1k
        code = 0;
311
38.1k
        goto exit;
312
6.99k
      slow:rlist = pr + i;
313
6.99k
        rcount = count - i;
314
9.00k
    } {
315
9.00k
        bool do_save = !gx_path_is_null(pgs->path);
316
317
9.00k
        if (do_save) {
318
830
            if ((code = gs_gsave(pgs)) < 0)
319
0
                goto exit;
320
830
            code = gs_newpath(pgs);
321
830
        }
322
9.00k
        if ((code >= 0) &&
323
9.00k
            (((code = gs_rectappend(pgs, rlist, rcount)) < 0) ||
324
9.00k
            ((code = gs_fill(pgs)) < 0))
325
9.00k
            )
326
9.00k
            DO_NOTHING;
327
9.00k
        if (do_save)
328
830
            gs_grestore(pgs);
329
8.17k
        else if (code < 0)
330
0
            gs_newpath(pgs);
331
9.00k
    }
332
333
47.2k
exit:
334
47.2k
    if (black_vector) {
335
        /* Restore color */
336
0
        gsicc_restore_blacktextvec(pgs, false);
337
0
    }
338
47.2k
    return code;
339
9.00k
}
340
341
/* Stroke a list of rectangles. */
342
/* (We could do this a lot more efficiently.) */
343
int
344
gs_rectstroke(gs_gstate * pgs, const gs_rect * pr, uint count,
345
              const gs_matrix * pmat)
346
2.15k
{
347
2.15k
    bool do_save = pmat != NULL || !gx_path_is_null(pgs->path);
348
2.15k
    int code;
349
350
2.15k
    if (do_save) {
351
18
        if ((code = gs_gsave(pgs)) < 0)
352
0
            return code;
353
18
        gs_newpath(pgs);
354
18
    }
355
2.15k
    if ((code = gs_rectappend(pgs, pr, count)) < 0 ||
356
2.15k
        (pmat != NULL && (code = gs_concat(pgs, pmat)) < 0) ||
357
2.15k
        (code = gs_stroke(pgs)) < 0
358
2.15k
        )
359
2.15k
        DO_NOTHING;
360
2.15k
    if (do_save)
361
18
        gs_grestore(pgs);
362
2.13k
    else if (code < 0)
363
0
        gs_newpath(pgs);
364
2.15k
    return code;
365
2.15k
}