/src/ghostpdl/base/gsline.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2021 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 | | /* 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 | 7.34M | #define pgs_lp gs_currentlineparams_inline(pgs) |
32 | | |
33 | | /* setlinewidth */ |
34 | | int |
35 | | gs_setlinewidth(gs_gstate * pgs, double width) |
36 | 1.22M | { |
37 | 1.22M | gx_set_line_width(pgs_lp, width); |
38 | 1.22M | return 0; |
39 | 1.22M | } |
40 | | |
41 | | /* currentlinewidth */ |
42 | | float |
43 | | gs_currentlinewidth(const gs_gstate * pgs) |
44 | 541 | { |
45 | 541 | return gx_current_line_width(pgs_lp); |
46 | 541 | } |
47 | | |
48 | | /* setlinecap (sets all 3 caps) */ |
49 | | int |
50 | | gs_setlinecap(gs_gstate * pgs, gs_line_cap cap) |
51 | 185k | { |
52 | 185k | if ((uint) cap > gs_line_cap_max) |
53 | 1.65k | return_error(gs_error_rangecheck); |
54 | 184k | pgs_lp->start_cap = cap; |
55 | 184k | pgs_lp->end_cap = cap; |
56 | 184k | pgs_lp->dash_cap = cap; |
57 | 184k | return 0; |
58 | 185k | } |
59 | | |
60 | | /* setlinestartcap */ |
61 | | int |
62 | | gs_setlinestartcap(gs_gstate * pgs, gs_line_cap cap) |
63 | 637k | { |
64 | 637k | if ((uint) cap > gs_line_cap_max) |
65 | 0 | return_error(gs_error_rangecheck); |
66 | 637k | pgs_lp->start_cap = cap; |
67 | 637k | return 0; |
68 | 637k | } |
69 | | |
70 | | /* setlineendcap */ |
71 | | int |
72 | | gs_setlineendcap(gs_gstate * pgs, gs_line_cap cap) |
73 | 637k | { |
74 | 637k | if ((uint) cap > gs_line_cap_max) |
75 | 0 | return_error(gs_error_rangecheck); |
76 | 637k | pgs_lp->end_cap = cap; |
77 | 637k | return 0; |
78 | 637k | } |
79 | | |
80 | | /* setlinedashcap */ |
81 | | int |
82 | | gs_setlinedashcap(gs_gstate * pgs, gs_line_cap cap) |
83 | 637k | { |
84 | 637k | if ((uint) cap > gs_line_cap_max) |
85 | 0 | return_error(gs_error_rangecheck); |
86 | 637k | pgs_lp->dash_cap = cap; |
87 | 637k | return 0; |
88 | 637k | } |
89 | | |
90 | | /* currentlinecap */ |
91 | | gs_line_cap |
92 | | gs_currentlinecap(const gs_gstate * pgs) |
93 | 110 | { |
94 | | /* This assumes that all caps are the same as start_cap - this will be |
95 | | * the case for postscript at least. */ |
96 | 110 | return pgs_lp->start_cap; |
97 | 110 | } |
98 | | |
99 | | /* setlinejoin */ |
100 | | int |
101 | | gs_setlinejoin(gs_gstate * pgs, gs_line_join join) |
102 | 816k | { |
103 | 816k | if ((uint) join > gs_line_join_max) |
104 | 1.88k | return_error(gs_error_rangecheck); |
105 | 814k | pgs_lp->join = join; |
106 | 814k | return 0; |
107 | 816k | } |
108 | | |
109 | | /* currentlinejoin */ |
110 | | gs_line_join |
111 | | gs_currentlinejoin(const gs_gstate * pgs) |
112 | 186 | { |
113 | 186 | return pgs_lp->join; |
114 | 186 | } |
115 | | |
116 | | /* setmiterlimit */ |
117 | | int |
118 | | gx_set_miter_limit(gx_line_params * plp, double limit) |
119 | 668k | { |
120 | 668k | if (limit < 1.0) |
121 | 13 | return_error(gs_error_rangecheck); |
122 | 668k | 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 | 668k | { |
132 | 668k | double limit_squared = limit * limit; |
133 | | |
134 | 668k | if (limit_squared < 2.0001 && limit_squared > 1.9999) |
135 | 0 | plp->miter_check = 1.0e6; |
136 | 668k | else |
137 | 668k | plp->miter_check = |
138 | 668k | sqrt(limit_squared - 1) * 2 / (limit_squared - 2); |
139 | 668k | } |
140 | 668k | return 0; |
141 | 668k | } |
142 | | int |
143 | | gs_setmiterlimit(gs_gstate * pgs, double limit) |
144 | 656k | { |
145 | 656k | return gx_set_miter_limit(pgs_lp, limit); |
146 | 656k | } |
147 | | |
148 | | /* currentmiterlimit */ |
149 | | float |
150 | | gs_currentmiterlimit(const gs_gstate * pgs) |
151 | 15 | { |
152 | 15 | return pgs_lp->miter_limit; |
153 | 15 | } |
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 | 929k | { |
160 | 929k | uint n = length; |
161 | 929k | const float *dfrom = pattern; |
162 | 929k | bool ink = true; |
163 | 929k | int index = 0; |
164 | 929k | float pattern_length = 0.0; |
165 | 929k | float dist_left; |
166 | 929k | float *ppat = dash->pattern; |
167 | | |
168 | | /* Check the dash pattern. */ |
169 | 1.07M | while (n--) { |
170 | 145k | float elt = *dfrom++; |
171 | | |
172 | 145k | if (elt < 0) |
173 | 0 | return_error(gs_error_rangecheck); |
174 | 145k | pattern_length += elt; |
175 | 145k | } |
176 | 929k | if (length == 0) { /* empty pattern */ |
177 | 859k | dist_left = 0.0; |
178 | 859k | if (mem && ppat) { |
179 | 3.91k | gs_free_object(mem, ppat, "gx_set_dash(old pattern)"); |
180 | 3.91k | ppat = NULL; |
181 | 3.91k | } |
182 | 859k | } else { |
183 | 69.9k | uint size = length * sizeof(float); |
184 | | |
185 | 69.9k | if (pattern_length == 0) |
186 | 0 | return_error(gs_error_rangecheck); |
187 | | /* Compute the initial index, ink_on, and distance left */ |
188 | | /* in the pattern, according to the offset. */ |
189 | 139k | #define f_mod(a, b) ((a) - floor((a) / (b)) * (b)) |
190 | 69.9k | if (length & 1) { /* Odd and even repetitions of the pattern */ |
191 | | /* have opposite ink values! */ |
192 | 24.8k | float length2 = pattern_length * 2; |
193 | | |
194 | 24.8k | dist_left = f_mod(offset, length2); |
195 | | /* Rounding errors can leave dist_left > length2 */ |
196 | 24.8k | dist_left = f_mod(dist_left, length2); |
197 | 24.8k | if (dist_left >= pattern_length) |
198 | 0 | dist_left -= pattern_length, ink = !ink; |
199 | 45.1k | } else { |
200 | 45.1k | dist_left = f_mod(offset, pattern_length); |
201 | | /* Rounding errors can leave dist_left > length */ |
202 | 45.1k | dist_left = f_mod(dist_left, pattern_length); |
203 | 45.1k | } |
204 | 69.9k | while ((dist_left -= pattern[index]) >= 0 && |
205 | 69.9k | (dist_left > 0 || pattern[index] != 0) |
206 | 69.9k | ) |
207 | 0 | ink = !ink, index++; |
208 | 69.9k | if (mem != NULL) { |
209 | 54.5k | if (ppat == NULL) |
210 | 53.4k | ppat = (float *)gs_alloc_bytes(mem, size, |
211 | 54.5k | "gx_set_dash(pattern)"); |
212 | 1.17k | else if (length != dash->pattern_size) |
213 | 0 | ppat = gs_resize_object(mem, ppat, size, |
214 | 54.5k | "gx_set_dash(pattern)"); |
215 | 54.5k | } |
216 | 69.9k | if (ppat == NULL) |
217 | 0 | return_error(gs_error_VMerror); |
218 | 69.9k | if (ppat != pattern) |
219 | 57.8k | memcpy(ppat, pattern, length * sizeof(float)); |
220 | 69.9k | } |
221 | 929k | dash->pattern = ppat; |
222 | 929k | dash->pattern_size = length; |
223 | 929k | dash->offset = offset; |
224 | 929k | dash->pattern_length = pattern_length; |
225 | 929k | dash->init_ink_on = ink; |
226 | 929k | dash->init_index = index; |
227 | 929k | dash->init_dist_left = -dist_left; |
228 | 929k | return 0; |
229 | 929k | } |
230 | | int |
231 | | gs_setdash(gs_gstate * pgs, const float *pattern, uint length, double offset) |
232 | 860k | { |
233 | 860k | return gx_set_dash(&pgs_lp->dash, pattern, length, offset, |
234 | 860k | pgs->memory); |
235 | 860k | } |
236 | | |
237 | | /* currentdash */ |
238 | | uint |
239 | | gs_currentdash_length(const gs_gstate * pgs) |
240 | 0 | { |
241 | 0 | return pgs_lp->dash.pattern_size; |
242 | 0 | } |
243 | | const float * |
244 | | gs_currentdash_pattern(const gs_gstate * pgs) |
245 | 0 | { |
246 | 0 | return pgs_lp->dash.pattern; |
247 | 0 | } |
248 | | float |
249 | | gs_currentdash_offset(const gs_gstate * pgs) |
250 | 499 | { |
251 | 499 | return pgs_lp->dash.offset; |
252 | 499 | } |
253 | | |
254 | | /* Internal accessor for line parameters */ |
255 | | const gx_line_params * |
256 | | gs_currentlineparams(const gs_gstate * pgs) |
257 | 10.7k | { |
258 | 10.7k | return gs_currentlineparams_inline(pgs); |
259 | 10.7k | } |
260 | | |
261 | | /* ------ Device-dependent parameters ------ */ |
262 | | |
263 | | /* setflat */ |
264 | | int |
265 | | gs_gstate_setflat(gs_gstate * pgs, double flat) |
266 | 1.18M | { |
267 | 1.18M | if (flat <= 0.2) |
268 | 933k | flat = 0.2; |
269 | 247k | else if (flat > 100) |
270 | 14 | flat = 100; |
271 | 1.18M | pgs->flatness = flat; |
272 | 1.18M | return 0; |
273 | 1.18M | } |
274 | | int |
275 | | gs_setflat(gs_gstate * pgs, double flat) |
276 | 267k | { |
277 | 267k | return gs_gstate_setflat(pgs, flat); |
278 | 267k | } |
279 | | |
280 | | /* currentflat */ |
281 | | float |
282 | | gs_currentflat(const gs_gstate * pgs) |
283 | 50 | { |
284 | 50 | return pgs->flatness; |
285 | 50 | } |
286 | | |
287 | | /* setstrokeadjust */ |
288 | | int |
289 | | gs_setstrokeadjust(gs_gstate * pgs, bool stroke_adjust) |
290 | 307k | { |
291 | 307k | pgs->stroke_adjust = stroke_adjust; |
292 | 307k | return 0; |
293 | 307k | } |
294 | | |
295 | | /* currentstrokeadjust */ |
296 | | bool |
297 | | gs_currentstrokeadjust(const gs_gstate * pgs) |
298 | 4 | { |
299 | 4 | return pgs->stroke_adjust; |
300 | 4 | } |
301 | | |
302 | | /* ------ Extensions ------ */ |
303 | | |
304 | | /* Device-independent */ |
305 | | |
306 | | /* setdashadapt */ |
307 | | void |
308 | | gs_setdashadapt(gs_gstate * pgs, bool adapt) |
309 | 637k | { |
310 | 637k | pgs_lp->dash.adapt = adapt; |
311 | 637k | } |
312 | | |
313 | | /* currentdashadapt */ |
314 | | bool |
315 | | gs_gstate_currentdashadapt(const gs_gstate * pgs) |
316 | 228k | { |
317 | 228k | return gs_currentlineparams_inline(pgs)->dash.adapt; |
318 | 228k | } |
319 | | bool |
320 | | gs_currentdashadapt(const gs_gstate * pgs) |
321 | 0 | { |
322 | 0 | return gs_gstate_currentdashadapt((const gs_gstate *)pgs); |
323 | 0 | } |
324 | | |
325 | | /* setcurvejoin */ |
326 | | int |
327 | | gs_setcurvejoin(gs_gstate * pgs, int join) |
328 | 637k | { |
329 | 637k | if (join < -1 || join > gs_line_join_max) |
330 | 0 | return_error(gs_error_rangecheck); |
331 | 637k | pgs_lp->curve_join = join; |
332 | 637k | return 0; |
333 | 637k | } |
334 | | |
335 | | /* currentcurvejoin */ |
336 | | int |
337 | | gs_currentcurvejoin(const gs_gstate * pgs) |
338 | 0 | { |
339 | 0 | return pgs_lp->curve_join; |
340 | 0 | } |
341 | | |
342 | | /* Device-dependent */ |
343 | | |
344 | | /* setaccuratecurves */ |
345 | | void |
346 | | gs_setaccuratecurves(gs_gstate * pgs, bool accurate) |
347 | 637k | { |
348 | 637k | pgs->accurate_curves = accurate; |
349 | 637k | } |
350 | | |
351 | | /* currentaccuratecurves */ |
352 | | bool |
353 | | gs_gstate_currentaccuratecurves(const gs_gstate * pgs) |
354 | 0 | { |
355 | 0 | return pgs->accurate_curves; |
356 | 0 | } |
357 | | bool |
358 | | gs_currentaccuratecurves(const gs_gstate * pgs) |
359 | 0 | { |
360 | 0 | return gs_gstate_currentaccuratecurves((const gs_gstate *)pgs); |
361 | 0 | } |
362 | | |
363 | | /* setdotlength */ |
364 | | int |
365 | | gx_set_dot_length(gx_line_params * plp, double length, bool absolute) |
366 | 664k | { |
367 | 664k | if (length < 0) |
368 | 0 | return_error(gs_error_rangecheck); |
369 | 664k | plp->dot_length = length; |
370 | 664k | plp->dot_length_absolute = absolute; |
371 | 664k | return 0; |
372 | 664k | } |
373 | | int |
374 | | gs_setdotlength(gs_gstate * pgs, double length, bool absolute) |
375 | 637k | { |
376 | 637k | return gx_set_dot_length(pgs_lp, length, absolute); |
377 | 637k | } |
378 | | |
379 | | /* currentdotlength */ |
380 | | float |
381 | | gs_currentdotlength(const gs_gstate * pgs) |
382 | 0 | { |
383 | 0 | return pgs_lp->dot_length; |
384 | 0 | } |
385 | | bool |
386 | | gs_currentdotlength_absolute(const gs_gstate * pgs) |
387 | 0 | { |
388 | 0 | return pgs_lp->dot_length_absolute; |
389 | 0 | } |
390 | | |
391 | | /* setdotorientation */ |
392 | | int |
393 | | gs_setdotorientation(gs_gstate *pgs) |
394 | 637k | { |
395 | 637k | if (is_xxyy(&pgs->ctm) || is_xyyx(&pgs->ctm)) |
396 | 637k | return gs_currentmatrix(pgs, &pgs_lp->dot_orientation); |
397 | 637k | return_error(gs_error_rangecheck); |
398 | 637k | } |
399 | | |
400 | | /* dotorientation */ |
401 | | int |
402 | | gs_dotorientation(gs_gstate *pgs) |
403 | 0 | { |
404 | 0 | return gs_setmatrix(pgs, &pgs_lp->dot_orientation); |
405 | 0 | } |