Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/gsline.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-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
17
/* Line parameter operators for Ghostscript library */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gxfixed.h"    /* ditto */
23
#include "gxmatrix.h"   /* for gzstate */
24
#include "gzstate.h"
25
#include "gscoord.h"    /* for currentmatrix, setmatrix */
26
#include "gsline.h"   /* for prototypes */
27
#include "gzline.h"
28
29
/* ------ Device-independent parameters ------ */
30
31
18.4M
#define pgs_lp gs_currentlineparams_inline(pgs)
32
33
/* setlinewidth */
34
int
35
gs_setlinewidth(gs_gstate * pgs, double width)
36
4.00M
{
37
4.00M
    gx_set_line_width(pgs_lp, width);
38
4.00M
    return 0;
39
4.00M
}
40
41
/* currentlinewidth */
42
float
43
gs_currentlinewidth(const gs_gstate * pgs)
44
604
{
45
604
    return gx_current_line_width(pgs_lp);
46
604
}
47
48
/* setlinecap (sets all 3 caps) */
49
int
50
gs_setlinecap(gs_gstate * pgs, gs_line_cap cap)
51
1.11M
{
52
1.11M
    if ((uint) cap > gs_line_cap_max)
53
23.9k
        return_error(gs_error_rangecheck);
54
1.09M
    pgs_lp->start_cap = cap;
55
1.09M
    pgs_lp->end_cap   = cap;
56
1.09M
    pgs_lp->dash_cap  = cap;
57
1.09M
    return 0;
58
1.11M
}
59
60
/* setlinestartcap */
61
int
62
gs_setlinestartcap(gs_gstate * pgs, gs_line_cap cap)
63
1.32M
{
64
1.32M
    if ((uint) cap > gs_line_cap_max)
65
0
        return_error(gs_error_rangecheck);
66
1.32M
    pgs_lp->start_cap = cap;
67
1.32M
    return 0;
68
1.32M
}
69
70
/* setlineendcap */
71
int
72
gs_setlineendcap(gs_gstate * pgs, gs_line_cap cap)
73
1.32M
{
74
1.32M
    if ((uint) cap > gs_line_cap_max)
75
0
        return_error(gs_error_rangecheck);
76
1.32M
    pgs_lp->end_cap = cap;
77
1.32M
    return 0;
78
1.32M
}
79
80
/* setlinedashcap */
81
int
82
gs_setlinedashcap(gs_gstate * pgs, gs_line_cap cap)
83
1.32M
{
84
1.32M
    if ((uint) cap > gs_line_cap_max)
85
0
        return_error(gs_error_rangecheck);
86
1.32M
    pgs_lp->dash_cap = cap;
87
1.32M
    return 0;
88
1.32M
}
89
90
/* currentlinecap */
91
gs_line_cap
92
gs_currentlinecap(const gs_gstate * pgs)
93
263
{
94
    /* This assumes that all caps are the same as start_cap - this will be
95
     * the case for postscript at least. */
96
263
    return pgs_lp->start_cap;
97
263
}
98
99
/* setlinejoin */
100
int
101
gs_setlinejoin(gs_gstate * pgs, gs_line_join join)
102
2.32M
{
103
2.32M
    if ((uint) join > gs_line_join_max)
104
17.7k
        return_error(gs_error_rangecheck);
105
2.30M
    pgs_lp->join = join;
106
2.30M
    return 0;
107
2.32M
}
108
109
/* currentlinejoin */
110
gs_line_join
111
gs_currentlinejoin(const gs_gstate * pgs)
112
1.35k
{
113
1.35k
    return pgs_lp->join;
114
1.35k
}
115
116
/* setmiterlimit */
117
int
118
gx_set_miter_limit(gx_line_params * plp, double limit)
119
1.38M
{
120
1.38M
    if (limit < 1.0)
121
30
        return_error(gs_error_rangecheck);
122
1.38M
    plp->miter_limit = limit;
123
    /*
124
     * Compute the miter check value.  The supplied miter limit is an
125
     * upper bound on 1/sin(phi/2); we convert this to a lower bound on
126
     * tan(phi).  Note that if phi > pi/2, this is negative.  We use the
127
     * half-angle and angle-sum formulas here to avoid the trig functions.
128
     * We also need a special check for phi/2 close to pi/4.
129
     * Some C compilers can't handle this as a conditional expression....
130
     */
131
1.38M
    {
132
1.38M
        double limit_squared = limit * limit;
133
134
1.38M
        if (limit_squared < 2.0001 && limit_squared > 1.9999)
135
0
            plp->miter_check = 1.0e6;
136
1.38M
        else
137
1.38M
            plp->miter_check =
138
1.38M
                sqrt(limit_squared - 1) * 2 / (limit_squared - 2);
139
1.38M
    }
140
1.38M
    return 0;
141
1.38M
}
142
int
143
gs_setmiterlimit(gs_gstate * pgs, double limit)
144
1.36M
{
145
1.36M
    return gx_set_miter_limit(pgs_lp, limit);
146
1.36M
}
147
148
/* currentmiterlimit */
149
float
150
gs_currentmiterlimit(const gs_gstate * pgs)
151
58
{
152
58
    return pgs_lp->miter_limit;
153
58
}
154
155
/* setdash */
156
int
157
gx_set_dash(gx_dash_params * dash, const float *pattern, uint length,
158
            double offset, gs_memory_t * mem)
159
2.94M
{
160
2.94M
    uint n = length;
161
2.94M
    const float *dfrom = pattern;
162
2.94M
    bool ink = true;
163
2.94M
    int index = 0;
164
2.94M
    float pattern_length = 0.0;
165
2.94M
    float dist_left;
166
2.94M
    float *ppat = dash->pattern;
167
168
    /* Check the dash pattern. */
169
4.23M
    while (n--) {
170
1.29M
        float elt = *dfrom++;
171
172
1.29M
        if (elt < 0)
173
0
            return_error(gs_error_rangecheck);
174
1.29M
        pattern_length += elt;
175
1.29M
    }
176
2.94M
    if (length == 0) {   /* empty pattern */
177
2.29M
        dist_left = 0.0;
178
2.29M
        if (mem && ppat) {
179
30.1k
            gs_free_object(mem, ppat, "gx_set_dash(old pattern)");
180
30.1k
            ppat = NULL;
181
30.1k
        }
182
2.29M
    } else {
183
642k
        uint size = length * sizeof(float);
184
185
642k
        if (pattern_length == 0)
186
1.31k
            return_error(gs_error_rangecheck);
187
        /* Compute the initial index, ink_on, and distance left */
188
        /* in the pattern, according to the offset. */
189
1.28M
#define f_mod(a, b) ((a) - floor((a) / (b)) * (b))
190
641k
        if (length & 1) { /* Odd and even repetitions of the pattern */
191
            /* have opposite ink values! */
192
282k
            float length2 = pattern_length * 2;
193
194
282k
            dist_left = f_mod(offset, length2);
195
            /* Rounding errors can leave dist_left > length2 */
196
282k
            dist_left = f_mod(dist_left, length2);
197
282k
            if (dist_left >= pattern_length)
198
0
                dist_left -= pattern_length, ink = !ink;
199
358k
        } else {
200
358k
            dist_left = f_mod(offset, pattern_length);
201
            /* Rounding errors can leave dist_left > length */
202
358k
            dist_left = f_mod(dist_left, pattern_length);
203
358k
        }
204
641k
        if (dist_left > pattern_length)
205
0
            return_error(gs_error_rangecheck);
206
641k
        while ((dist_left -= pattern[index]) >= 0 &&
207
641k
               (dist_left > 0 || pattern[index] != 0)
208
641k
            )
209
187
            ink = !ink, index++;
210
641k
        if (mem != NULL) {
211
278k
            if (ppat == NULL)
212
267k
                ppat = (float *)gs_alloc_bytes(mem, size,
213
278k
                                               "gx_set_dash(pattern)");
214
11.4k
            else if (length != dash->pattern_size)
215
12
                ppat = gs_resize_object(mem, ppat, size,
216
278k
                                        "gx_set_dash(pattern)");
217
278k
        }
218
641k
        if (ppat == NULL)
219
0
            return_error(gs_error_VMerror);
220
641k
        if (ppat != pattern)
221
293k
            memcpy(ppat, pattern, length * sizeof(float));
222
641k
    }
223
2.93M
    dash->pattern = ppat;
224
2.93M
    dash->pattern_size = length;
225
2.93M
    dash->offset = offset;
226
2.93M
    dash->pattern_length = pattern_length;
227
2.93M
    dash->init_ink_on = ink;
228
2.93M
    dash->init_index = index;
229
2.93M
    dash->init_dist_left = -dist_left;
230
2.93M
    return 0;
231
2.94M
}
232
int
233
gs_setdash(gs_gstate * pgs, const float *pattern, uint length, double offset)
234
2.21M
{
235
2.21M
    return gx_set_dash(&pgs_lp->dash, pattern, length, offset,
236
2.21M
                       pgs->memory);
237
2.21M
}
238
239
/* currentdash */
240
uint
241
gs_currentdash_length(const gs_gstate * pgs)
242
0
{
243
0
    return pgs_lp->dash.pattern_size;
244
0
}
245
const float *
246
gs_currentdash_pattern(const gs_gstate * pgs)
247
0
{
248
0
    return pgs_lp->dash.pattern;
249
0
}
250
float
251
gs_currentdash_offset(const gs_gstate * pgs)
252
1.68k
{
253
1.68k
    return pgs_lp->dash.offset;
254
1.68k
}
255
256
/* Internal accessor for line parameters */
257
const gx_line_params *
258
gs_currentlineparams(const gs_gstate * pgs)
259
34.6k
{
260
34.6k
    return gs_currentlineparams_inline(pgs);
261
34.6k
}
262
263
/* ------ Device-dependent parameters ------ */
264
265
/* setflat */
266
int
267
gs_gstate_setflat(gs_gstate * pgs, double flat)
268
7.40M
{
269
7.40M
    if (flat <= 0.2)
270
6.78M
        flat = 0.2;
271
622k
    else if (flat > 100)
272
40
        flat = 100;
273
7.40M
    pgs->flatness = flat;
274
7.40M
    return 0;
275
7.40M
}
276
int
277
gs_setflat(gs_gstate * pgs, double flat)
278
566k
{
279
566k
    return gs_gstate_setflat(pgs, flat);
280
566k
}
281
282
/* currentflat */
283
float
284
gs_currentflat(const gs_gstate * pgs)
285
105
{
286
105
    return pgs->flatness;
287
105
}
288
289
/* setstrokeadjust */
290
int
291
gs_setstrokeadjust(gs_gstate * pgs, bool stroke_adjust)
292
572k
{
293
572k
    pgs->stroke_adjust = stroke_adjust;
294
572k
    return 0;
295
572k
}
296
297
/* currentstrokeadjust */
298
bool
299
gs_currentstrokeadjust(const gs_gstate * pgs)
300
6
{
301
6
    return pgs->stroke_adjust;
302
6
}
303
304
/* ------ Extensions ------ */
305
306
/* Device-independent */
307
308
/* setdashadapt */
309
void
310
gs_setdashadapt(gs_gstate * pgs, bool adapt)
311
1.32M
{
312
1.32M
    pgs_lp->dash.adapt = adapt;
313
1.32M
}
314
315
/* currentdashadapt */
316
bool
317
gs_gstate_currentdashadapt(const gs_gstate * pgs)
318
362k
{
319
362k
    return gs_currentlineparams_inline(pgs)->dash.adapt;
320
362k
}
321
bool
322
gs_currentdashadapt(const gs_gstate * pgs)
323
0
{
324
0
    return gs_gstate_currentdashadapt((const gs_gstate *)pgs);
325
0
}
326
327
/* setcurvejoin */
328
int
329
gs_setcurvejoin(gs_gstate * pgs, int join)
330
1.32M
{
331
1.32M
    if (join < -1 || join > gs_line_join_max)
332
0
        return_error(gs_error_rangecheck);
333
1.32M
    pgs_lp->curve_join = join;
334
1.32M
    return 0;
335
1.32M
}
336
337
/* currentcurvejoin */
338
int
339
gs_currentcurvejoin(const gs_gstate * pgs)
340
0
{
341
0
    return pgs_lp->curve_join;
342
0
}
343
344
/* Device-dependent */
345
346
/* setaccuratecurves */
347
void
348
gs_setaccuratecurves(gs_gstate * pgs, bool accurate)
349
1.32M
{
350
1.32M
    pgs->accurate_curves = accurate;
351
1.32M
}
352
353
/* currentaccuratecurves */
354
bool
355
gs_gstate_currentaccuratecurves(const gs_gstate * pgs)
356
0
{
357
0
    return pgs->accurate_curves;
358
0
}
359
bool
360
gs_currentaccuratecurves(const gs_gstate * pgs)
361
0
{
362
0
    return gs_gstate_currentaccuratecurves((const gs_gstate *)pgs);
363
0
}
364
365
/* setdotlength */
366
int
367
gx_set_dot_length(gx_line_params * plp, double length, bool absolute)
368
1.80M
{
369
1.80M
    if (length < 0)
370
0
        return_error(gs_error_rangecheck);
371
1.80M
    plp->dot_length = length;
372
1.80M
    plp->dot_length_absolute = absolute;
373
1.80M
    return 0;
374
1.80M
}
375
int
376
gs_setdotlength(gs_gstate * pgs, double length, bool absolute)
377
1.32M
{
378
1.32M
    return gx_set_dot_length(pgs_lp, length, absolute);
379
1.32M
}
380
381
/* currentdotlength */
382
float
383
gs_currentdotlength(const gs_gstate * pgs)
384
0
{
385
0
    return pgs_lp->dot_length;
386
0
}
387
bool
388
gs_currentdotlength_absolute(const gs_gstate * pgs)
389
0
{
390
0
    return pgs_lp->dot_length_absolute;
391
0
}
392
393
/* setdotorientation */
394
int
395
gs_setdotorientation(gs_gstate *pgs)
396
1.32M
{
397
1.32M
    if (is_xxyy(&pgs->ctm) || is_xyyx(&pgs->ctm))
398
1.32M
        return gs_currentmatrix(pgs, &pgs_lp->dot_orientation);
399
1.32M
    return_error(gs_error_rangecheck);
400
1.32M
}
401
402
/* dotorientation */
403
int
404
gs_dotorientation(gs_gstate *pgs)
405
0
{
406
0
    return gs_setmatrix(pgs, &pgs_lp->dot_orientation);
407
0
}