/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 | 160k | #define pgs_lp gs_currentlineparams_inline(pgs) |
32 | | |
33 | | /* setlinewidth */ |
34 | | int |
35 | | gs_setlinewidth(gs_gstate * pgs, double width) |
36 | 107k | { |
37 | 107k | gx_set_line_width(pgs_lp, width); |
38 | 107k | return 0; |
39 | 107k | } |
40 | | |
41 | | /* currentlinewidth */ |
42 | | float |
43 | | gs_currentlinewidth(const gs_gstate * pgs) |
44 | 0 | { |
45 | 0 | return gx_current_line_width(pgs_lp); |
46 | 0 | } |
47 | | |
48 | | /* setlinecap (sets all 3 caps) */ |
49 | | int |
50 | | gs_setlinecap(gs_gstate * pgs, gs_line_cap cap) |
51 | 21.2k | { |
52 | 21.2k | if ((uint) cap > gs_line_cap_max) |
53 | 652 | return_error(gs_error_rangecheck); |
54 | 20.6k | pgs_lp->start_cap = cap; |
55 | 20.6k | pgs_lp->end_cap = cap; |
56 | 20.6k | pgs_lp->dash_cap = cap; |
57 | 20.6k | return 0; |
58 | 21.2k | } |
59 | | |
60 | | /* setlinestartcap */ |
61 | | int |
62 | | gs_setlinestartcap(gs_gstate * pgs, gs_line_cap cap) |
63 | 5.71k | { |
64 | 5.71k | if ((uint) cap > gs_line_cap_max) |
65 | 0 | return_error(gs_error_rangecheck); |
66 | 5.71k | pgs_lp->start_cap = cap; |
67 | 5.71k | return 0; |
68 | 5.71k | } |
69 | | |
70 | | /* setlineendcap */ |
71 | | int |
72 | | gs_setlineendcap(gs_gstate * pgs, gs_line_cap cap) |
73 | 5.71k | { |
74 | 5.71k | if ((uint) cap > gs_line_cap_max) |
75 | 0 | return_error(gs_error_rangecheck); |
76 | 5.71k | pgs_lp->end_cap = cap; |
77 | 5.71k | return 0; |
78 | 5.71k | } |
79 | | |
80 | | /* setlinedashcap */ |
81 | | int |
82 | | gs_setlinedashcap(gs_gstate * pgs, gs_line_cap cap) |
83 | 5.71k | { |
84 | 5.71k | if ((uint) cap > gs_line_cap_max) |
85 | 0 | return_error(gs_error_rangecheck); |
86 | 5.71k | pgs_lp->dash_cap = cap; |
87 | 5.71k | return 0; |
88 | 5.71k | } |
89 | | |
90 | | /* currentlinecap */ |
91 | | gs_line_cap |
92 | | gs_currentlinecap(const gs_gstate * pgs) |
93 | 0 | { |
94 | | /* This assumes that all caps are the same as start_cap - this will be |
95 | | * the case for postscript at least. */ |
96 | 0 | return pgs_lp->start_cap; |
97 | 0 | } |
98 | | |
99 | | /* setlinejoin */ |
100 | | int |
101 | | gs_setlinejoin(gs_gstate * pgs, gs_line_join join) |
102 | 27.7k | { |
103 | 27.7k | if ((uint) join > gs_line_join_max) |
104 | 273 | return_error(gs_error_rangecheck); |
105 | 27.5k | pgs_lp->join = join; |
106 | 27.5k | return 0; |
107 | 27.7k | } |
108 | | |
109 | | /* currentlinejoin */ |
110 | | gs_line_join |
111 | | gs_currentlinejoin(const gs_gstate * pgs) |
112 | 0 | { |
113 | 0 | return pgs_lp->join; |
114 | 0 | } |
115 | | |
116 | | /* setmiterlimit */ |
117 | | int |
118 | | gx_set_miter_limit(gx_line_params * plp, double limit) |
119 | 6.74k | { |
120 | 6.74k | if (limit < 1.0) |
121 | 6 | return_error(gs_error_rangecheck); |
122 | 6.73k | 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 | 6.73k | { |
132 | 6.73k | double limit_squared = limit * limit; |
133 | | |
134 | 6.73k | if (limit_squared < 2.0001 && limit_squared > 1.9999) |
135 | 0 | plp->miter_check = 1.0e6; |
136 | 6.73k | else |
137 | 6.73k | plp->miter_check = |
138 | 6.73k | sqrt(limit_squared - 1) * 2 / (limit_squared - 2); |
139 | 6.73k | } |
140 | 6.73k | return 0; |
141 | 6.74k | } |
142 | | int |
143 | | gs_setmiterlimit(gs_gstate * pgs, double limit) |
144 | 5.85k | { |
145 | 5.85k | return gx_set_miter_limit(pgs_lp, limit); |
146 | 5.85k | } |
147 | | |
148 | | /* currentmiterlimit */ |
149 | | float |
150 | | gs_currentmiterlimit(const gs_gstate * pgs) |
151 | 0 | { |
152 | 0 | return pgs_lp->miter_limit; |
153 | 0 | } |
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 | 34.1k | { |
160 | 34.1k | uint n = length; |
161 | 34.1k | const float *dfrom = pattern; |
162 | 34.1k | bool ink = true; |
163 | 34.1k | int index = 0; |
164 | 34.1k | float pattern_length = 0.0; |
165 | 34.1k | float dist_left; |
166 | 34.1k | float *ppat = dash->pattern; |
167 | | |
168 | | /* Check the dash pattern. */ |
169 | 64.3k | while (n--) { |
170 | 30.1k | float elt = *dfrom++; |
171 | | |
172 | 30.1k | if (elt < 0) |
173 | 0 | return_error(gs_error_rangecheck); |
174 | 30.1k | pattern_length += elt; |
175 | 30.1k | } |
176 | 34.1k | if (length == 0) { /* empty pattern */ |
177 | 26.0k | dist_left = 0.0; |
178 | 26.0k | if (mem && ppat) { |
179 | 715 | gs_free_object(mem, ppat, "gx_set_dash(old pattern)"); |
180 | 715 | ppat = NULL; |
181 | 715 | } |
182 | 26.0k | } else { |
183 | 8.09k | uint size = length * sizeof(float); |
184 | | |
185 | 8.09k | 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 | 16.1k | #define f_mod(a, b) ((a) - floor((a) / (b)) * (b)) |
190 | 8.09k | if (length & 1) { /* Odd and even repetitions of the pattern */ |
191 | | /* have opposite ink values! */ |
192 | 14 | float length2 = pattern_length * 2; |
193 | | |
194 | 14 | dist_left = f_mod(offset, length2); |
195 | | /* Rounding errors can leave dist_left > length2 */ |
196 | 14 | dist_left = f_mod(dist_left, length2); |
197 | 14 | if (dist_left >= pattern_length) |
198 | 0 | dist_left -= pattern_length, ink = !ink; |
199 | 8.08k | } else { |
200 | 8.08k | dist_left = f_mod(offset, pattern_length); |
201 | | /* Rounding errors can leave dist_left > length */ |
202 | 8.08k | dist_left = f_mod(dist_left, pattern_length); |
203 | 8.08k | } |
204 | 8.09k | while ((dist_left -= pattern[index]) >= 0 && |
205 | 8.09k | (dist_left > 0 || pattern[index] != 0) |
206 | 8.09k | ) |
207 | 0 | ink = !ink, index++; |
208 | 8.09k | if (mem != NULL) { |
209 | 7.74k | if (ppat == NULL) |
210 | 7.72k | ppat = (float *)gs_alloc_bytes(mem, size, |
211 | 7.74k | "gx_set_dash(pattern)"); |
212 | 21 | else if (length != dash->pattern_size) |
213 | 0 | ppat = gs_resize_object(mem, ppat, size, |
214 | 7.74k | "gx_set_dash(pattern)"); |
215 | 7.74k | } |
216 | 8.09k | if (ppat == NULL) |
217 | 0 | return_error(gs_error_VMerror); |
218 | 8.09k | if (ppat != pattern) |
219 | 7.88k | memcpy(ppat, pattern, length * sizeof(float)); |
220 | 8.09k | } |
221 | 34.1k | dash->pattern = ppat; |
222 | 34.1k | dash->pattern_size = length; |
223 | 34.1k | dash->offset = offset; |
224 | 34.1k | dash->pattern_length = pattern_length; |
225 | 34.1k | dash->init_ink_on = ink; |
226 | 34.1k | dash->init_index = index; |
227 | 34.1k | dash->init_dist_left = -dist_left; |
228 | 34.1k | return 0; |
229 | 34.1k | } |
230 | | int |
231 | | gs_setdash(gs_gstate * pgs, const float *pattern, uint length, double offset) |
232 | 25.6k | { |
233 | 25.6k | return gx_set_dash(&pgs_lp->dash, pattern, length, offset, |
234 | 25.6k | pgs->memory); |
235 | 25.6k | } |
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 | 0 | { |
251 | 0 | return pgs_lp->dash.offset; |
252 | 0 | } |
253 | | |
254 | | /* Internal accessor for line parameters */ |
255 | | const gx_line_params * |
256 | | gs_currentlineparams(const gs_gstate * pgs) |
257 | 237 | { |
258 | 237 | return gs_currentlineparams_inline(pgs); |
259 | 237 | } |
260 | | |
261 | | /* ------ Device-dependent parameters ------ */ |
262 | | |
263 | | /* setflat */ |
264 | | int |
265 | | gs_gstate_setflat(gs_gstate * pgs, double flat) |
266 | 6.91k | { |
267 | 6.91k | if (flat <= 0.2) |
268 | 983 | flat = 0.2; |
269 | 5.93k | else if (flat > 100) |
270 | 0 | flat = 100; |
271 | 6.91k | pgs->flatness = flat; |
272 | 6.91k | return 0; |
273 | 6.91k | } |
274 | | int |
275 | | gs_setflat(gs_gstate * pgs, double flat) |
276 | 4.07k | { |
277 | 4.07k | return gs_gstate_setflat(pgs, flat); |
278 | 4.07k | } |
279 | | |
280 | | /* currentflat */ |
281 | | float |
282 | | gs_currentflat(const gs_gstate * pgs) |
283 | 0 | { |
284 | 0 | return pgs->flatness; |
285 | 0 | } |
286 | | |
287 | | /* setstrokeadjust */ |
288 | | int |
289 | | gs_setstrokeadjust(gs_gstate * pgs, bool stroke_adjust) |
290 | 4.27k | { |
291 | 4.27k | pgs->stroke_adjust = stroke_adjust; |
292 | 4.27k | return 0; |
293 | 4.27k | } |
294 | | |
295 | | /* currentstrokeadjust */ |
296 | | bool |
297 | | gs_currentstrokeadjust(const gs_gstate * pgs) |
298 | 0 | { |
299 | 0 | return pgs->stroke_adjust; |
300 | 0 | } |
301 | | |
302 | | /* ------ Extensions ------ */ |
303 | | |
304 | | /* Device-independent */ |
305 | | |
306 | | /* setdashadapt */ |
307 | | void |
308 | | gs_setdashadapt(gs_gstate * pgs, bool adapt) |
309 | 5.71k | { |
310 | 5.71k | pgs_lp->dash.adapt = adapt; |
311 | 5.71k | } |
312 | | |
313 | | /* currentdashadapt */ |
314 | | bool |
315 | | gs_gstate_currentdashadapt(const gs_gstate * pgs) |
316 | 3.29k | { |
317 | 3.29k | return gs_currentlineparams_inline(pgs)->dash.adapt; |
318 | 3.29k | } |
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 | 5.71k | { |
329 | 5.71k | if (join < -1 || join > gs_line_join_max) |
330 | 0 | return_error(gs_error_rangecheck); |
331 | 5.71k | pgs_lp->curve_join = join; |
332 | 5.71k | return 0; |
333 | 5.71k | } |
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 | 5.71k | { |
348 | 5.71k | pgs->accurate_curves = accurate; |
349 | 5.71k | } |
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 | 6.62k | { |
367 | 6.62k | if (length < 0) |
368 | 0 | return_error(gs_error_rangecheck); |
369 | 6.62k | plp->dot_length = length; |
370 | 6.62k | plp->dot_length_absolute = absolute; |
371 | 6.62k | return 0; |
372 | 6.62k | } |
373 | | int |
374 | | gs_setdotlength(gs_gstate * pgs, double length, bool absolute) |
375 | 5.71k | { |
376 | 5.71k | return gx_set_dot_length(pgs_lp, length, absolute); |
377 | 5.71k | } |
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 | 5.71k | { |
395 | 5.71k | if (is_xxyy(&pgs->ctm) || is_xyyx(&pgs->ctm)) |
396 | 5.71k | return gs_currentmatrix(pgs, &pgs_lp->dot_orientation); |
397 | 5.71k | return_error(gs_error_rangecheck); |
398 | 5.71k | } |
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 | } |