Coverage Report

Created: 2025-04-22 06:20

/src/libspectre/ghostscript/base/gsdps1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2020 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, 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
0
#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
0
{
54
0
    gs_rect ubox, dbox;
55
0
    gs_fixed_rect obox, bbox;
56
0
    gx_path *ppath = pgs->path;
57
0
    int code;
58
59
0
    if (llx > urx || lly > ury)
60
0
        return_error(gs_error_rangecheck);
61
    /* Transform box to device coordinates. */
62
0
    ubox.p.x = llx;
63
0
    ubox.p.y = lly;
64
0
    ubox.q.x = urx;
65
0
    ubox.q.y = ury;
66
0
    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
0
    if (dbox.p.x < fixed2float(min_fixed + box_rounding_slop_fixed) ||
72
0
        dbox.p.y < fixed2float(min_fixed + box_rounding_slop_fixed) ||
73
0
        dbox.q.x >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon) ||
74
0
        dbox.q.y >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon)
75
0
        )
76
0
        return_error(gs_error_limitcheck);
77
0
    bbox.p.x =
78
0
        (fixed) floor(dbox.p.x * fixed_scale) - box_rounding_slop_fixed;
79
0
    bbox.p.y =
80
0
        (fixed) floor(dbox.p.y * fixed_scale) - box_rounding_slop_fixed;
81
0
    bbox.q.x =
82
0
        (fixed) ceil(dbox.q.x * fixed_scale) + box_rounding_slop_fixed;
83
0
    bbox.q.y =
84
0
        (fixed) ceil(dbox.q.y * fixed_scale) + box_rounding_slop_fixed;
85
0
    if (gx_path_bbox_set(ppath, &obox) >= 0) { /* Take the union of the bboxes. */
86
0
        ppath->bbox.p.x = min(obox.p.x, bbox.p.x);
87
0
        ppath->bbox.p.y = min(obox.p.y, bbox.p.y);
88
0
        ppath->bbox.q.x = max(obox.q.x, bbox.q.x);
89
0
        ppath->bbox.q.y = max(obox.q.y, bbox.q.y);
90
0
    } else {     /* empty path *//* Just set the bbox. */
91
0
        ppath->bbox = bbox;
92
0
    }
93
0
    ppath->bbox_set = 1;
94
0
    return 0;
95
0
}
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
0
{
103
0
    bool CPSI_mode = gs_currentcpsimode(pgs->memory);
104
105
0
    for (; count != 0; count--, pr++) {
106
0
        double px = pr->p.x, py = pr->p.y, qx = pr->q.x, qy = pr->q.y;
107
0
        int code;
108
109
0
        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
0
        } else {
144
            /* Ensure counter-clockwise drawing. */
145
0
            if ((qx >= px) != (qy >= py))
146
0
                qx = px, px = pr->q.x; /* swap x values */
147
0
            if ((code = gs_moveto(pgs, px, py)) < 0 ||
148
0
                (code = gs_lineto(pgs, qx, py)) < 0 ||
149
0
                (code = gs_lineto(pgs, qx, qy)) < 0 ||
150
0
                (code = gs_lineto(pgs, px, qy)) < 0 ||
151
0
                (code = gs_closepath(pgs)) < 0
152
0
                )
153
0
                return code;
154
0
        }
155
0
    }
156
0
    return 0;
157
0
}
158
int
159
gs_rectappend(gs_gstate * pgs, const gs_rect * pr, uint count)
160
0
{
161
0
    return gs_rectappend_compat(pgs, pr, count, false);
162
0
}
163
164
/* Clip to a list of rectangles. */
165
int
166
gs_rectclip(gs_gstate * pgs, const gs_rect * pr, uint count)
167
0
{
168
0
    int code;
169
0
    gx_path save;
170
171
0
    gx_path_init_local(&save, pgs->memory);
172
0
    gx_path_assign_preserve(&save, pgs->path);
173
0
    gs_newpath(pgs);
174
0
    if ((code = gs_rectappend_compat(pgs, pr, count, true)) < 0 ||
175
0
        (code = gs_clip(pgs)) < 0
176
0
        ) {
177
0
        gx_path_assign_free(pgs->path, &save);
178
0
        return code;
179
0
    }
180
0
    gx_path_free(&save, "gs_rectclip");
181
0
    gs_newpath(pgs);
182
0
    return 0;
183
0
}
184
185
/* Fill a list of rectangles. */
186
/* We take the trouble to do this efficiently in the simple cases. */
187
int
188
gs_rectfill(gs_gstate * pgs, const gs_rect * pr, uint count)
189
0
{
190
0
    const gs_rect *rlist = pr;
191
0
    gx_clip_path *pcpath;
192
0
    uint rcount = count;
193
0
    int code;
194
0
    gx_device * pdev = pgs->device;
195
0
    gx_device_color *pdc = gs_currentdevicecolor_inline(pgs);
196
0
    const gs_gstate *pgs2 = (const gs_gstate *)pgs;
197
0
    bool hl_color_available = gx_hld_is_hl_color_available(pgs2, pdc);
198
0
    bool hl_color = (hl_color_available &&
199
0
                dev_proc(pdev, dev_spec_op)(pdev, gxdso_supports_hlcolor, 
200
0
                                  NULL, 0));
201
0
    bool center_of_pixel = (pgs->fill_adjust.x == 0 && pgs->fill_adjust.y == 0);
202
203
    /* Processing a fill object operation */
204
0
    ensure_tag_is_set(pgs, pgs->device, GS_PATH_TAG); /* NB: may unset_dev_color */
205
206
0
    code = gx_set_dev_color(pgs);
207
0
    if (code != 0)
208
0
        return code;
209
0
    if ((is_fzero2(pgs->ctm.xy, pgs->ctm.yx) ||
210
0
         is_fzero2(pgs->ctm.xx, pgs->ctm.yy)) &&
211
0
        gx_effective_clip_path(pgs, &pcpath) >= 0 &&
212
0
        clip_list_is_rectangle(gx_cpath_list(pcpath)) &&
213
0
        (hl_color ||
214
0
         pdc->type == gx_dc_type_pure ||
215
0
         pdc->type == gx_dc_type_ht_binary ||
216
0
         pdc->type == gx_dc_type_ht_colored) &&
217
0
        gs_gstate_color_load(pgs) >= 0 &&
218
0
        (*dev_proc(pdev, get_alpha_bits)) (pdev, go_graphics)
219
0
        <= 1 &&
220
0
        (!pgs->overprint || !gs_currentcolor_eopm(pgs))
221
0
        ) {
222
0
        uint i;
223
0
        gs_fixed_rect clip_rect;
224
225
0
        gx_cpath_inner_box(pcpath, &clip_rect);
226
        /* We should never plot anything for an empty clip rectangle */
227
0
        if ((clip_rect.p.x >= clip_rect.q.x) &&
228
0
            (clip_rect.p.y >= clip_rect.q.y))
229
0
            return 0;
230
0
        for (i = 0; i < count; ++i) {
231
0
            gs_fixed_point p, q;
232
0
            gs_fixed_rect draw_rect;
233
234
0
            if (gs_point_transform2fixed(&pgs->ctm, pr[i].p.x, pr[i].p.y, &p) < 0 ||
235
0
                gs_point_transform2fixed(&pgs->ctm, pr[i].q.x, pr[i].q.y, &q) < 0
236
0
                ) {   /* Switch to the slow algorithm. */
237
0
                goto slow;
238
0
            }
239
0
            draw_rect.p.x = min(p.x, q.x);
240
0
            draw_rect.p.y = min(p.y, q.y);
241
0
            draw_rect.q.x = max(p.x, q.x);
242
0
            draw_rect.q.y = max(p.y, q.y);
243
0
            if (hl_color) {
244
0
                rect_intersect(draw_rect, clip_rect);
245
                /* We do pass on 0 extant rectangles to high level
246
                   devices.  It isn't clear how a client and an output
247
                   device should interact if one uses a center of
248
                   pixel algorithm and the other uses any part of
249
                   pixel.  For now we punt and just pass the high
250
                   level rectangle on without adjustment. */
251
0
                if (draw_rect.p.x <= draw_rect.q.x &&
252
0
                    draw_rect.p.y <= draw_rect.q.y) {
253
0
                    code = dev_proc(pdev, fill_rectangle_hl_color)(pdev,
254
0
                             &draw_rect, pgs2, pdc, pcpath);
255
0
                    if (code < 0)
256
0
                        return code;
257
0
                }
258
0
            } else {
259
0
                int x, y, w, h;
260
261
0
                rect_intersect(draw_rect, clip_rect);
262
0
                if (center_of_pixel) {
263
0
                    draw_rect.p.x = fixed_rounded(draw_rect.p.x);
264
0
                    draw_rect.p.y = fixed_rounded(draw_rect.p.y);
265
0
                    draw_rect.q.x = fixed_rounded(draw_rect.q.x);
266
0
                    draw_rect.q.y = fixed_rounded(draw_rect.q.y);
267
0
                } else { /* any part of pixel rule - touched */
268
0
                    draw_rect.p.x = fixed_floor(draw_rect.p.x);
269
0
                    draw_rect.p.y = fixed_floor(draw_rect.p.y);
270
0
                    draw_rect.q.x = fixed_ceiling(draw_rect.q.x);
271
0
                    draw_rect.q.y = fixed_ceiling(draw_rect.q.y);
272
0
                }
273
0
                x = fixed2int(draw_rect.p.x);
274
0
                y = fixed2int(draw_rect.p.y);
275
0
                w = fixed2int(draw_rect.q.x) - x;
276
0
                h = fixed2int(draw_rect.q.y) - y;
277
                /* clients that use the "any part of pixel" rule also
278
                   fill 0 areas.  This is true of current graphics
279
                   library clients but not a general rule.  */
280
0
                if (!center_of_pixel) {
281
0
                    if (w == 0)
282
0
                        w = 1;
283
                    /* yes Adobe Acrobat 8, seems to back up the y
284
                       coordinate when the width is 0, sigh. */
285
0
                    if (h == 0) {
286
0
                        y--;
287
0
                        h = 1;
288
0
                    }
289
0
                }
290
0
                if (gx_fill_rectangle(x, y, w, h, pdc, pgs) < 0)
291
0
                    goto slow;
292
0
            }
293
0
        }
294
0
        return 0;
295
0
      slow:rlist = pr + i;
296
0
        rcount = count - i;
297
0
    } {
298
0
        bool do_save = !gx_path_is_null(pgs->path);
299
300
0
        if (do_save) {
301
0
            if ((code = gs_gsave(pgs)) < 0)
302
0
                return code;
303
0
            code = gs_newpath(pgs);
304
0
        }
305
0
        if ((code >= 0) &&
306
0
            (((code = gs_rectappend(pgs, rlist, rcount)) < 0) ||
307
0
            ((code = gs_fill(pgs)) < 0))
308
0
            )
309
0
            DO_NOTHING;
310
0
        if (do_save)
311
0
            gs_grestore(pgs);
312
0
        else if (code < 0)
313
0
            gs_newpath(pgs);
314
0
    }
315
0
    return code;
316
0
}
317
318
/* Stroke a list of rectangles. */
319
/* (We could do this a lot more efficiently.) */
320
int
321
gs_rectstroke(gs_gstate * pgs, const gs_rect * pr, uint count,
322
              const gs_matrix * pmat)
323
0
{
324
0
    bool do_save = pmat != NULL || !gx_path_is_null(pgs->path);
325
0
    int code;
326
327
0
    if (do_save) {
328
0
        if ((code = gs_gsave(pgs)) < 0)
329
0
            return code;
330
0
        gs_newpath(pgs);
331
0
    }
332
0
    if ((code = gs_rectappend(pgs, pr, count)) < 0 ||
333
0
        (pmat != NULL && (code = gs_concat(pgs, pmat)) < 0) ||
334
0
        (code = gs_stroke(pgs)) < 0
335
0
        )
336
0
        DO_NOTHING;
337
0
    if (do_save)
338
0
        gs_grestore(pgs);
339
0
    else if (code < 0)
340
0
        gs_newpath(pgs);
341
0
    return code;
342
0
}