/src/ghostpdl/base/gxcpath.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2022 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 | | /* Implementation of clipping paths, other than actual clipping */ |
18 | | #include "gx.h" |
19 | | #include "string_.h" |
20 | | #include "gserrors.h" |
21 | | #include "gsstruct.h" |
22 | | #include "gsutil.h" |
23 | | #include "gsline.h" |
24 | | #include "gxdevice.h" |
25 | | #include "gxfixed.h" |
26 | | #include "gxpaint.h" |
27 | | #include "gscoord.h" /* needs gsmatrix.h */ |
28 | | #include "gxgstate.h" |
29 | | #include "gzpath.h" |
30 | | #include "gzcpath.h" |
31 | | #include "gzacpath.h" |
32 | | |
33 | | /* Forward references */ |
34 | | static void gx_clip_list_from_rectangle(gx_clip_list *, gs_fixed_rect *); |
35 | | |
36 | | /* Other structure types */ |
37 | | public_st_clip_rect(); |
38 | | public_st_clip_list(); |
39 | | public_st_clip_path(); |
40 | | private_st_clip_rect_list(); |
41 | | public_st_device_clip(); |
42 | | private_st_cpath_path_list(); |
43 | | |
44 | | /* GC procedures for gx_clip_path */ |
45 | | static |
46 | 7.65M | ENUM_PTRS_WITH(clip_path_enum_ptrs, gx_clip_path *cptr) return ENUM_USING(st_path, &cptr->path, sizeof(cptr->path), index - 3); |
47 | | |
48 | 1.09M | case 0: |
49 | 1.09M | return ENUM_OBJ((cptr->rect_list == &cptr->local_list ? 0 : |
50 | 0 | cptr->rect_list)); |
51 | 1.09M | case 1: |
52 | 1.09M | return ENUM_OBJ(cptr->path_list); |
53 | 1.09M | case 2: |
54 | 1.09M | return ENUM_OBJ((cptr->cached == &cptr->rect_list->list.single ? 0 : |
55 | 7.65M | cptr->cached)); |
56 | 7.65M | ENUM_PTRS_END |
57 | | static |
58 | 1.09M | RELOC_PTRS_WITH(clip_path_reloc_ptrs, gx_clip_path *cptr) |
59 | 1.09M | { |
60 | 1.09M | if (cptr->rect_list != &cptr->local_list) |
61 | 1.09M | RELOC_VAR(cptr->rect_list); |
62 | 1.09M | RELOC_VAR(cptr->path_list); |
63 | 1.09M | if (cptr->cached != &cptr->rect_list->list.single) |
64 | 1.09M | RELOC_VAR(cptr->cached); |
65 | 1.09M | RELOC_USING(st_path, &cptr->path, sizeof(gx_path)); |
66 | 1.09M | } |
67 | 1.09M | RELOC_PTRS_END |
68 | | |
69 | | /* GC procedures for gx_device_clip */ |
70 | | static |
71 | 0 | ENUM_PTRS_WITH(device_clip_enum_ptrs, gx_device_clip *cptr) |
72 | 0 | { |
73 | 0 | if (index < st_clip_list_max_ptrs + 2) |
74 | 0 | return ENUM_USING(st_clip_list, &cptr->list, |
75 | 0 | sizeof(gx_clip_list), index - 2); |
76 | 0 | return ENUM_USING(st_device_forward, vptr, |
77 | 0 | sizeof(gx_device_forward), |
78 | 0 | index - (st_clip_list_max_ptrs + 2)); |
79 | 0 | } |
80 | 0 | case 0: |
81 | 0 | ENUM_RETURN((cptr->current == &cptr->list.single ? NULL : |
82 | 0 | (void *)cptr->current)); |
83 | 0 | case 1: |
84 | 0 | ENUM_RETURN((cptr->cpath)); |
85 | 0 | ENUM_PTRS_END |
86 | | static |
87 | 0 | RELOC_PTRS_WITH(device_clip_reloc_ptrs, gx_device_clip *cptr) |
88 | 0 | { |
89 | 0 | if (cptr->current == &cptr->list.single) |
90 | 0 | cptr->current = &((gx_device_clip *)RELOC_OBJ(vptr))->list.single; |
91 | 0 | else |
92 | 0 | RELOC_PTR(gx_device_clip, current); |
93 | 0 | RELOC_PTR(gx_device_clip, cpath); |
94 | 0 | RELOC_USING(st_clip_list, &cptr->list, sizeof(gx_clip_list)); |
95 | 0 | RELOC_USING(st_device_forward, vptr, sizeof(gx_device_forward)); |
96 | 0 | } |
97 | 0 | RELOC_PTRS_END |
98 | | |
99 | | /* Define an empty clip list. */ |
100 | | static const gx_clip_list clip_list_empty = { |
101 | | { |
102 | | 0, /* next */ |
103 | | 0, /* prev */ |
104 | | min_int, /* ymin */ |
105 | | max_int, /* ymax */ |
106 | | 0, /* xmin */ |
107 | | 0, /* xmax */ |
108 | | 0 /* to_visit */ |
109 | | }, /* single */ |
110 | | 0, /* head */ |
111 | | 0, /* tail */ |
112 | | 0, /* insert */ |
113 | | 0, /* xmin */ |
114 | | 0, /* xmax */ |
115 | | 0, /* count */ |
116 | | 0 /* transpose = false */ |
117 | | }; |
118 | | |
119 | | /* ------ Clipping path memory management ------ */ |
120 | | |
121 | | static rc_free_proc(rc_free_cpath_list); |
122 | | static rc_free_proc(rc_free_cpath_list_local); |
123 | | static rc_free_proc(rc_free_cpath_path_list); |
124 | | |
125 | | /* |
126 | | * Initialize those parts of the contents of a clip path that aren't |
127 | | * part of the path. |
128 | | */ |
129 | | static void |
130 | | cpath_init_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox) |
131 | 7.66M | { |
132 | 7.66M | gx_clip_list_from_rectangle(&pcpath->rect_list->list, pbox); |
133 | 7.66M | pcpath->inner_box = *pbox; |
134 | 7.66M | pcpath->path_valid = false; |
135 | 7.66M | pcpath->path_fill_adjust.x = 0; |
136 | 7.66M | pcpath->path_fill_adjust.y = 0; |
137 | 7.66M | pcpath->path.bbox = *pbox; |
138 | 7.66M | gx_cpath_set_outer_box(pcpath); |
139 | 7.66M | pcpath->id = gs_next_ids(pcpath->path.memory, 1); /* path changed => change id */ |
140 | 7.66M | pcpath->cached = NULL; |
141 | 7.66M | } |
142 | | static void |
143 | | cpath_init_own_contents(gx_clip_path * pcpath) |
144 | 1.96M | { /* We could make null_rect static, but then it couldn't be const. */ |
145 | 1.96M | gs_fixed_rect null_rect; |
146 | | |
147 | 1.96M | null_rect.p.x = null_rect.p.y = null_rect.q.x = null_rect.q.y = 0; |
148 | 1.96M | cpath_init_rectangle(pcpath, &null_rect); |
149 | 1.96M | pcpath->path_list = NULL; |
150 | 1.96M | } |
151 | | static void |
152 | | cpath_share_own_contents(gx_clip_path * pcpath, const gx_clip_path * shared) |
153 | 438k | { |
154 | 438k | pcpath->inner_box = shared->inner_box; |
155 | 438k | pcpath->path_valid = shared->path_valid; |
156 | 438k | pcpath->path_fill_adjust = shared->path_fill_adjust; |
157 | 438k | pcpath->outer_box = shared->outer_box; |
158 | 438k | pcpath->id = shared->id; |
159 | 438k | pcpath->cached = NULL; |
160 | 438k | } |
161 | | |
162 | | /* Allocate only the segments of a clipping path on the heap. */ |
163 | | static int |
164 | | cpath_alloc_list(gx_clip_rect_list ** prlist, gs_memory_t * mem, |
165 | | client_name_t cname) |
166 | 3.55M | { |
167 | 3.55M | rc_alloc_struct_1(*prlist, gx_clip_rect_list, &st_clip_rect_list, mem, |
168 | 3.55M | return_error(gs_error_VMerror), cname); |
169 | 3.55M | (*prlist)->rc.free = rc_free_cpath_list; |
170 | 3.55M | return 0; |
171 | 3.55M | } |
172 | | int |
173 | | gx_cpath_init_contained_shared(gx_clip_path * pcpath, |
174 | | const gx_clip_path * shared, gs_memory_t * mem, client_name_t cname) |
175 | 12.5M | { |
176 | 12.5M | if (shared) { |
177 | 12.2M | if (shared->path.segments == &shared->path.local_segments) { |
178 | 0 | lprintf1("Attempt to share (local) segments of clip path "PRI_INTPTR"!\n", |
179 | 0 | (intptr_t)shared); |
180 | 0 | return_error(gs_error_Fatal); |
181 | 0 | } |
182 | 12.2M | *pcpath = *shared; |
183 | 12.2M | pcpath->path.memory = mem; |
184 | 12.2M | pcpath->path.allocation = path_allocated_contained; |
185 | 12.2M | rc_increment(pcpath->path.segments); |
186 | 12.2M | rc_increment(pcpath->rect_list); |
187 | 12.2M | rc_increment(pcpath->path_list); |
188 | 12.2M | } else { |
189 | 276k | int code = cpath_alloc_list(&pcpath->rect_list, mem, cname); |
190 | | |
191 | 276k | if (code < 0) |
192 | 0 | return code; |
193 | 276k | code = gx_path_alloc_contained(&pcpath->path, mem, cname); |
194 | 276k | if (code < 0) { |
195 | 0 | gs_free_object(mem, pcpath->rect_list, cname); |
196 | 0 | pcpath->rect_list = 0; |
197 | 0 | return code; |
198 | 0 | } |
199 | 276k | cpath_init_own_contents(pcpath); |
200 | 276k | } |
201 | 12.5M | return 0; |
202 | 12.5M | } |
203 | | #define gx_cpath_alloc_contents(pcpath, shared, mem, cname)\ |
204 | 12.5M | gx_cpath_init_contained_shared(pcpath, shared, mem, cname) |
205 | | |
206 | | /* Allocate all of a clipping path on the heap. */ |
207 | | gx_clip_path * |
208 | | gx_cpath_alloc_shared(const gx_clip_path * shared, gs_memory_t * mem, |
209 | | client_name_t cname) |
210 | 12.5M | { |
211 | 12.5M | gx_clip_path *pcpath = |
212 | 12.5M | gs_alloc_struct(mem, gx_clip_path, &st_clip_path, cname); |
213 | 12.5M | int code; |
214 | | |
215 | 12.5M | if (pcpath == 0) |
216 | 0 | return 0; |
217 | 12.5M | code = gx_cpath_alloc_contents(pcpath, shared, mem, cname); |
218 | 12.5M | if (code < 0) { |
219 | 0 | gs_free_object(mem, pcpath, cname); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 12.5M | pcpath->path.allocation = path_allocated_on_heap; |
223 | 12.5M | return pcpath; |
224 | 12.5M | } |
225 | | |
226 | | /* Initialize a stack-allocated clipping path. */ |
227 | | int |
228 | | gx_cpath_init_local_shared_nested(gx_clip_path * pcpath, |
229 | | const gx_clip_path * shared, |
230 | | gs_memory_t * mem, |
231 | | bool safely_nested) |
232 | 2.13M | { |
233 | 2.13M | if (shared) { |
234 | 438k | if ((shared->path.segments == &shared->path.local_segments) && |
235 | 438k | !safely_nested) { |
236 | 0 | lprintf1("Attempt to share (local) segments of clip path "PRI_INTPTR"!\n", |
237 | 0 | (intptr_t)shared); |
238 | 0 | return_error(gs_error_Fatal); |
239 | 0 | } |
240 | 438k | pcpath->path = shared->path; |
241 | 438k | pcpath->path.allocation = path_allocated_on_stack; |
242 | 438k | rc_increment(pcpath->path.segments); |
243 | 438k | pcpath->rect_list = shared->rect_list; |
244 | 438k | rc_increment(pcpath->rect_list); |
245 | 438k | pcpath->path_list = shared->path_list; |
246 | 438k | rc_increment(pcpath->path_list); |
247 | 438k | cpath_share_own_contents(pcpath, shared); |
248 | 438k | pcpath->rule = shared->rule; |
249 | 1.69M | } else { |
250 | 1.69M | gx_path_init_local(&pcpath->path, mem); |
251 | 1.69M | rc_init_free(&pcpath->local_list, mem, 1, rc_free_cpath_list_local); |
252 | 1.69M | pcpath->rect_list = &pcpath->local_list; |
253 | 1.69M | cpath_init_own_contents(pcpath); |
254 | 1.69M | } |
255 | 2.13M | return 0; |
256 | 2.13M | } |
257 | | |
258 | | int |
259 | | gx_cpath_init_local_shared(gx_clip_path * pcpath, const gx_clip_path * shared, |
260 | | gs_memory_t * mem) |
261 | 1.61M | { |
262 | 1.61M | return gx_cpath_init_local_shared_nested(pcpath, shared, mem, 0); |
263 | 1.61M | } |
264 | | |
265 | | /* Unshare a clipping path. */ |
266 | | int |
267 | | gx_cpath_unshare(gx_clip_path * pcpath) |
268 | 0 | { |
269 | 0 | int code = gx_path_unshare(&pcpath->path); |
270 | 0 | gx_clip_rect_list *rlist = pcpath->rect_list; |
271 | |
|
272 | 0 | if (code < 0) |
273 | 0 | return code; |
274 | 0 | if (rlist->rc.ref_count > 1) { |
275 | 0 | int code = cpath_alloc_list(&pcpath->rect_list, pcpath->path.memory, |
276 | 0 | "gx_cpath_unshare"); |
277 | |
|
278 | 0 | if (code < 0) |
279 | 0 | return code; |
280 | | /* Copy the rectangle list. */ |
281 | | /**************** NYI ****************/ |
282 | | /* Until/Unless we implement this, NULL out the list */ |
283 | 0 | memset(&pcpath->rect_list->list, 0x00, sizeof(gx_clip_list)); |
284 | 0 | rc_decrement(rlist, "gx_cpath_unshare"); |
285 | 0 | } |
286 | 0 | return code; |
287 | 0 | } |
288 | | |
289 | | /* Free a clipping path. */ |
290 | | void |
291 | | gx_cpath_free(gx_clip_path * pcpath, client_name_t cname) |
292 | 27.9M | { |
293 | 27.9M | if (pcpath == 0L) |
294 | 12.6M | return; |
295 | | |
296 | 15.3M | rc_decrement(pcpath->rect_list, cname); |
297 | 15.3M | rc_decrement(pcpath->path_list, cname); |
298 | | /* Clean up pointers for GC. */ |
299 | 15.3M | pcpath->rect_list = 0; |
300 | 15.3M | pcpath->path_list = 0; |
301 | 15.3M | { |
302 | 15.3M | gx_path_allocation_t alloc = pcpath->path.allocation; |
303 | | |
304 | 15.3M | if (alloc == path_allocated_on_heap) { |
305 | 12.5M | pcpath->path.allocation = path_allocated_contained; |
306 | 12.5M | gx_path_free(&pcpath->path, cname); |
307 | 12.5M | gs_free_object(pcpath->path.memory, pcpath, cname); |
308 | 12.5M | } else |
309 | 2.78M | gx_path_free(&pcpath->path, cname); |
310 | 15.3M | } |
311 | 15.3M | } |
312 | | |
313 | | /* Assign a clipping path, preserving the source. */ |
314 | | int |
315 | | gx_cpath_assign_preserve(gx_clip_path * pcpto, gx_clip_path * pcpfrom) |
316 | 980k | { |
317 | 980k | int code = gx_path_assign_preserve(&pcpto->path, &pcpfrom->path); |
318 | 980k | gx_clip_rect_list *fromlist = pcpfrom->rect_list; |
319 | 980k | gx_clip_rect_list *tolist = pcpto->rect_list; |
320 | 980k | gx_path path; |
321 | | |
322 | 980k | if (code < 0) |
323 | 0 | return 0; |
324 | 980k | if (fromlist == &pcpfrom->local_list) { |
325 | | /* We can't use pcpfrom's list object. */ |
326 | 961k | if (tolist == &pcpto->local_list || tolist->rc.ref_count > 1) { |
327 | | /* We can't use pcpto's list either. Allocate a new one. */ |
328 | 404k | int code = cpath_alloc_list(&tolist, tolist->rc.memory, |
329 | 404k | "gx_cpath_assign"); |
330 | | |
331 | 404k | if (code < 0) { |
332 | 0 | rc_decrement(pcpto->path.segments, "gx_path_assign"); |
333 | 0 | return code; |
334 | 0 | } |
335 | 404k | rc_decrement(pcpto->rect_list, "gx_cpath_assign"); |
336 | 557k | } else { |
337 | | /* Use pcpto's list object. */ |
338 | 557k | rc_free_cpath_list_local(tolist->rc.memory, tolist, |
339 | 557k | "gx_cpath_assign"); |
340 | 557k | } |
341 | 961k | tolist->list = fromlist->list; |
342 | 961k | pcpfrom->rect_list = tolist; |
343 | 961k | rc_increment(tolist); |
344 | 961k | } else { |
345 | | /* We can use pcpfrom's list object. */ |
346 | 18.2k | rc_increment(fromlist); |
347 | 18.2k | rc_decrement(pcpto->rect_list, "gx_cpath_assign"); |
348 | 18.2k | } |
349 | 980k | rc_increment(pcpfrom->path_list); |
350 | 980k | rc_decrement(pcpto->path_list, "gx_cpath_assign"); |
351 | 980k | path = pcpto->path, *pcpto = *pcpfrom, pcpto->path = path; |
352 | 980k | return 0; |
353 | 980k | } |
354 | | |
355 | | /* Assign a clipping path, releasing the source. */ |
356 | | int |
357 | | gx_cpath_assign_free(gx_clip_path * pcpto, gx_clip_path * pcpfrom) |
358 | 961k | { /* For right now, just do assign + free. */ |
359 | 961k | int code = gx_cpath_assign_preserve(pcpto, pcpfrom); |
360 | | |
361 | 961k | if (code < 0) |
362 | 0 | return code; |
363 | 961k | gx_cpath_free(pcpfrom, "gx_cpath_assign_free"); |
364 | 961k | return 0; |
365 | 961k | } |
366 | | |
367 | | /* Free the clipping list when its reference count goes to zero. */ |
368 | | static void |
369 | | rc_free_cpath_list_local(gs_memory_t * mem, void *vrlist, |
370 | | client_name_t cname) |
371 | 4.84M | { |
372 | 4.84M | gx_clip_rect_list *rlist = (gx_clip_rect_list *) vrlist; |
373 | | |
374 | 4.84M | gx_clip_list_free(&rlist->list, mem); |
375 | 4.84M | } |
376 | | static void |
377 | | rc_free_cpath_list(gs_memory_t * mem, void *vrlist, client_name_t cname) |
378 | 3.55M | { |
379 | 3.55M | rc_free_cpath_list_local(mem, vrlist, cname); |
380 | 3.55M | gs_free_object(mem, vrlist, cname); |
381 | 3.55M | } |
382 | | |
383 | | static void |
384 | | rc_free_cpath_path_list(gs_memory_t * mem, void *vplist, client_name_t cname) |
385 | 478k | { |
386 | 478k | gx_cpath_path_list *plist = (gx_cpath_path_list *)vplist; |
387 | 478k | rc_decrement(plist->next, cname); |
388 | 478k | gx_path_free(&plist->path, cname); |
389 | 478k | gs_free_object(plist->path.memory, plist, cname); |
390 | 478k | } |
391 | | |
392 | | /* Allocate a new clip path list node. The created node has a ref count |
393 | | of 1, and "steals" the reference to next (i.e. does not increment |
394 | | its reference count). */ |
395 | | static int |
396 | | gx_cpath_path_list_new(gs_memory_t *mem, gx_clip_path *pcpath, int rule, |
397 | | gx_path *ppfrom, gx_cpath_path_list *next, gx_cpath_path_list **pnew) |
398 | 478k | { |
399 | 478k | int code; |
400 | 478k | client_name_t cname = "gx_cpath_path_list_new"; |
401 | 478k | gx_cpath_path_list *pcplist = gs_alloc_struct(mem, gx_cpath_path_list, |
402 | 478k | &st_cpath_path_list, cname); |
403 | | |
404 | 478k | if (pcplist == 0) |
405 | 0 | return_error(gs_error_VMerror); |
406 | 478k | rc_init_free(pcplist, mem, 1, rc_free_cpath_path_list); |
407 | 478k | if (pcpath!=NULL && !pcpath->path_valid) { |
408 | 413k | code = gx_path_init_contained_shared(&pcplist->path, NULL, mem, cname); |
409 | 413k | if (code < 0) { |
410 | 0 | gs_free_object(mem, pcplist, "gx_cpath_path_list_new"); |
411 | 0 | return code; |
412 | 0 | } |
413 | 413k | code = gx_cpath_to_path(pcpath, &pcplist->path); |
414 | 413k | } else { |
415 | 64.6k | gx_path_init_local(&pcplist->path, mem); |
416 | 64.6k | code = gx_path_assign_preserve(&pcplist->path, ppfrom); |
417 | 64.6k | } |
418 | 478k | if (code < 0) |
419 | 0 | return code; |
420 | 478k | pcplist->next = next; |
421 | 478k | rc_increment(next); |
422 | 478k | pcplist->rule = rule; |
423 | 478k | *pnew = pcplist; |
424 | 478k | return 0; |
425 | 478k | } |
426 | | |
427 | | /* ------ Clipping path accessing ------ */ |
428 | | |
429 | | /* Synthesize a path from a clipping path. */ |
430 | | int |
431 | | gx_cpath_to_path_synthesize(const gx_clip_path * pcpath, gx_path * ppath) |
432 | 420k | { |
433 | | /* Synthesize a path. */ |
434 | 420k | gs_cpath_enum cenum; |
435 | 420k | gs_fixed_point pts[3]; |
436 | 420k | int code; |
437 | | |
438 | 420k | gx_cpath_enum_init(&cenum, pcpath); |
439 | 2.94M | while ((code = gx_cpath_enum_next(&cenum, pts)) != 0) { |
440 | 2.52M | switch (code) { |
441 | 426k | case gs_pe_moveto: |
442 | 426k | code = gx_path_add_point(ppath, pts[0].x, pts[0].y); |
443 | 426k | break; |
444 | 1.82M | case gs_pe_lineto: |
445 | 1.82M | code = gx_path_add_line_notes(ppath, pts[0].x, pts[0].y, |
446 | 1.82M | gx_cpath_enum_notes(&cenum)); |
447 | 1.82M | break; |
448 | 0 | case gs_pe_gapto: |
449 | 0 | code = gx_path_add_gap_notes(ppath, pts[0].x, pts[0].y, |
450 | 0 | gx_cpath_enum_notes(&cenum)); |
451 | 0 | break; |
452 | 0 | case gs_pe_curveto: |
453 | 0 | code = gx_path_add_curve_notes(ppath, pts[0].x, pts[0].y, |
454 | 0 | pts[1].x, pts[1].y, |
455 | 0 | pts[2].x, pts[2].y, |
456 | 0 | gx_cpath_enum_notes(&cenum)); |
457 | 0 | break; |
458 | 274k | case gs_pe_closepath: |
459 | 274k | code = gx_path_close_subpath_notes(ppath, |
460 | 274k | gx_cpath_enum_notes(&cenum)); |
461 | 274k | break; |
462 | 0 | default: |
463 | 0 | if (code >= 0) |
464 | 0 | code = gs_note_error(gs_error_unregistered); |
465 | 2.52M | } |
466 | 2.52M | if (code < 0) |
467 | 0 | break; |
468 | 2.52M | } |
469 | 420k | return 0; |
470 | 420k | } |
471 | | |
472 | | /* Return the path of a clipping path. */ |
473 | | int |
474 | | gx_cpath_to_path(gx_clip_path * pcpath, gx_path * ppath) |
475 | 498k | { |
476 | 498k | if (!pcpath->path_valid) { |
477 | 420k | gx_path rpath; |
478 | 420k | int code; |
479 | | |
480 | 420k | gx_path_init_local(&rpath, pcpath->path.memory); |
481 | 420k | code = gx_cpath_to_path_synthesize(pcpath, &rpath); |
482 | 420k | if (code < 0) { |
483 | 0 | gx_path_free(&rpath, "gx_cpath_to_path error"); |
484 | 0 | return code; |
485 | 0 | } |
486 | 420k | code = gx_path_assign_free(&pcpath->path, &rpath); |
487 | 420k | if (code < 0) |
488 | 0 | return code; |
489 | 420k | pcpath->path_valid = true; |
490 | 420k | pcpath->path_fill_adjust.x = 0; |
491 | 420k | pcpath->path_fill_adjust.y = 0; |
492 | 420k | } |
493 | 498k | return gx_path_assign_preserve(ppath, &pcpath->path); |
494 | 498k | } |
495 | | /* Return the inner and outer check rectangles for a clipping path. */ |
496 | | /* Return true iff the path is a rectangle. */ |
497 | | bool |
498 | | gx_cpath_inner_box(const gx_clip_path * pcpath, gs_fixed_rect * pbox) |
499 | 13.2M | { |
500 | 13.2M | *pbox = pcpath->inner_box; |
501 | 13.2M | return clip_list_is_rectangle(gx_cpath_list(pcpath)); |
502 | 13.2M | } |
503 | | bool |
504 | | gx_cpath_outer_box(const gx_clip_path * pcpath, gs_fixed_rect * pbox) |
505 | 12.1M | { |
506 | 12.1M | *pbox = pcpath->outer_box; |
507 | 12.1M | return clip_list_is_rectangle(gx_cpath_list(pcpath)); |
508 | 12.1M | } |
509 | | |
510 | | /* Test if a clipping path includes a rectangle. */ |
511 | | /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */ |
512 | | bool |
513 | | gx_cpath_includes_rectangle(register const gx_clip_path * pcpath, |
514 | | fixed x0, fixed y0, fixed x1, fixed y1) |
515 | 3.15M | { |
516 | 3.15M | return |
517 | 3.15M | (x0 <= x1 ? |
518 | 3.15M | (pcpath->inner_box.p.x <= x0 && x1 <= pcpath->inner_box.q.x) : |
519 | 3.15M | (pcpath->inner_box.p.x <= x1 && x0 <= pcpath->inner_box.q.x)) && |
520 | 3.15M | (y0 <= y1 ? |
521 | 2.95M | (pcpath->inner_box.p.y <= y0 && y1 <= pcpath->inner_box.q.y) : |
522 | 2.95M | (pcpath->inner_box.p.y <= y1 && y0 <= pcpath->inner_box.q.y)); |
523 | 3.15M | } |
524 | | |
525 | | /* Set the outer clipping box to the path bounding box, */ |
526 | | /* expanded to pixel boundaries. */ |
527 | | void |
528 | | gx_cpath_set_outer_box(gx_clip_path * pcpath) |
529 | 8.62M | { |
530 | 8.62M | pcpath->outer_box.p.x = fixed_floor(pcpath->path.bbox.p.x); |
531 | 8.62M | pcpath->outer_box.p.y = fixed_floor(pcpath->path.bbox.p.y); |
532 | 8.62M | pcpath->outer_box.q.x = fixed_ceiling(pcpath->path.bbox.q.x); |
533 | 8.62M | pcpath->outer_box.q.y = fixed_ceiling(pcpath->path.bbox.q.y); |
534 | 8.62M | } |
535 | | |
536 | | /* Return the rectangle list of a clipping path (for local use only). */ |
537 | | const gx_clip_list * |
538 | | gx_cpath_list(const gx_clip_path *pcpath) |
539 | 30.0M | { |
540 | 30.0M | return &pcpath->rect_list->list; |
541 | 30.0M | } |
542 | | /* Internal non-const version of the same accessor. */ |
543 | | static inline gx_clip_list * |
544 | | gx_cpath_list_private(const gx_clip_path *pcpath) |
545 | 425k | { |
546 | 425k | return &pcpath->rect_list->list; |
547 | 425k | } |
548 | | |
549 | | /* ------ Clipping path setting ------ */ |
550 | | |
551 | | /* Create a rectangular clipping path. */ |
552 | | /* The supplied rectangle may not be oriented correctly, */ |
553 | | /* but it will be oriented correctly upon return. */ |
554 | | static int |
555 | | cpath_set_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox) |
556 | 5.69M | { |
557 | 5.69M | gx_clip_rect_list *rlist = pcpath->rect_list; |
558 | | |
559 | 5.69M | if (rlist->rc.ref_count <= 1) |
560 | 2.82M | gx_clip_list_free(&rlist->list, rlist->rc.memory); |
561 | 2.87M | else { |
562 | 2.87M | int code = cpath_alloc_list(&pcpath->rect_list, pcpath->path.memory, |
563 | 2.87M | "gx_cpath_from_rectangle"); |
564 | | |
565 | 2.87M | if (code < 0) { |
566 | 0 | pcpath->rect_list = rlist; |
567 | 0 | return code; |
568 | 0 | } |
569 | 2.87M | rc_decrement(rlist, "gx_cpath_from_rectangle"); |
570 | 2.87M | rlist = pcpath->rect_list; |
571 | 2.87M | } |
572 | 5.69M | cpath_init_rectangle(pcpath, pbox); |
573 | 5.69M | return 0; |
574 | 5.69M | } |
575 | | int |
576 | | gx_cpath_from_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox) |
577 | 4.85M | { |
578 | 4.85M | int code = gx_path_new(&pcpath->path); |
579 | | |
580 | 4.85M | if (code < 0) |
581 | 0 | return code; |
582 | 4.85M | return cpath_set_rectangle(pcpath, pbox); |
583 | 4.85M | } |
584 | | int |
585 | | gx_cpath_reset(gx_clip_path * pcpath) |
586 | 815k | { |
587 | 815k | gs_fixed_rect null_rect; |
588 | | |
589 | 815k | null_rect.p.x = null_rect.p.y = null_rect.q.x = null_rect.q.y = 0; |
590 | 815k | rc_decrement(pcpath->path_list, "gx_cpath_reset"); |
591 | 815k | return gx_cpath_from_rectangle(pcpath, &null_rect); |
592 | 815k | } |
593 | | |
594 | | /* If a clipping path is a rectangle, return the rectangle. |
595 | | * If its a rectangular path, also return the rectangle. |
596 | | */ |
597 | | gx_path_rectangular_type cpath_is_rectangle(const gx_clip_path * pcpath, gs_fixed_rect *rect) |
598 | 73.0k | { |
599 | 73.0k | if (pcpath->path_valid) { |
600 | 73.0k | return gx_path_is_rectangle((const gx_path *)&pcpath->path, rect); |
601 | 73.0k | } |
602 | 0 | if (pcpath->inner_box.p.x != pcpath->path.bbox.p.x || |
603 | 0 | pcpath->inner_box.p.y != pcpath->path.bbox.p.y || |
604 | 0 | pcpath->inner_box.q.x != pcpath->path.bbox.q.x || |
605 | 0 | pcpath->inner_box.q.y != pcpath->path.bbox.q.y) |
606 | 0 | return prt_none; |
607 | 0 | rect->p.x = pcpath->inner_box.p.x; |
608 | 0 | rect->p.y = pcpath->inner_box.p.y; |
609 | 0 | rect->q.x = pcpath->inner_box.q.x; |
610 | 0 | rect->q.y = pcpath->inner_box.q.y; |
611 | 0 | return prt_closed; |
612 | 0 | } |
613 | | |
614 | | /* Intersect a new clipping path with an old one. */ |
615 | | /* Flatten the new path first (in a copy) if necessary. */ |
616 | | int |
617 | | gx_cpath_clip(gs_gstate *pgs, gx_clip_path *pcpath, |
618 | | /*const*/ gx_path *ppath_orig, int rule) |
619 | 484k | { |
620 | 484k | return gx_cpath_intersect(pcpath, ppath_orig, rule, pgs); |
621 | 484k | } |
622 | | |
623 | | int |
624 | | gx_cpath_ensure_path_list(gx_clip_path *pcpath) |
625 | 464k | { |
626 | 464k | if (pcpath == NULL || pcpath->path_list) |
627 | 43.6k | return 0; |
628 | 421k | return gx_cpath_path_list_new(pcpath->path.memory, pcpath, pcpath->rule, |
629 | 421k | &pcpath->path, NULL, &pcpath->path_list); |
630 | 464k | } |
631 | | |
632 | | int |
633 | | gx_cpath_intersect_with_params(gx_clip_path *pcpath, /*const*/ gx_path *ppath_orig, |
634 | | int rule, gs_gstate *pgs, const gx_fill_params * params) |
635 | 955k | { |
636 | 955k | gx_path fpath; |
637 | 955k | /*const*/ gx_path *ppath = ppath_orig; |
638 | 955k | gs_fixed_rect old_box, new_box; |
639 | 955k | int code; |
640 | 955k | int pcpath_is_rect; |
641 | | |
642 | 955k | pcpath->cached = NULL; |
643 | | /* Flatten the path if necessary. */ |
644 | 955k | if (gx_path_has_curves_inline(ppath)) { |
645 | 30.3k | gx_path_init_local(&fpath, pgs->memory); |
646 | 30.3k | code = gx_path_add_flattened_accurate(ppath, &fpath, |
647 | 30.3k | gs_currentflat_inline(pgs), |
648 | 30.3k | pgs->accurate_curves); |
649 | 30.3k | if (code < 0) |
650 | 2 | return code; |
651 | 30.3k | ppath = &fpath; |
652 | 30.3k | } |
653 | | |
654 | 955k | pcpath_is_rect = gx_cpath_inner_box(pcpath, &old_box); |
655 | 955k | if (pcpath_is_rect && |
656 | 955k | ((code = gx_path_is_rectangle(ppath, &new_box)) || |
657 | 949k | gx_path_is_void(ppath)) |
658 | 955k | ) { |
659 | 857k | int changed = 0; |
660 | | |
661 | 857k | if (!code) { |
662 | | /* The new path is void. */ |
663 | 52.5k | if (gx_path_current_point(ppath, &new_box.p) < 0) { |
664 | | /* Use the user space origin (arbitrarily). */ |
665 | 48.9k | new_box.p.x = float2fixed(pgs->ctm.tx); |
666 | 48.9k | new_box.p.y = float2fixed(pgs->ctm.ty); |
667 | 48.9k | } |
668 | 52.5k | new_box.q = new_box.p; |
669 | 52.5k | changed = 1; |
670 | 804k | } else { |
671 | 804k | { /* Apply same adjustment as for filling the path. */ |
672 | 804k | gs_fixed_point adjust = params != NULL ? params->adjust : pgs->fill_adjust; |
673 | 804k | fixed adjust_xl, adjust_xu, adjust_yl, adjust_yu; |
674 | | |
675 | 804k | if (adjust.x == -1) |
676 | 0 | adjust_xl = adjust_xu = adjust_yl = adjust_yu = 0; |
677 | 804k | else { |
678 | 804k | adjust_xl = (adjust.x == fixed_half ? fixed_half - fixed_epsilon : adjust.x); |
679 | 804k | adjust_yl = (adjust.y == fixed_half ? fixed_half - fixed_epsilon : adjust.y); |
680 | 804k | adjust_xu = adjust.x; |
681 | 804k | adjust_yu = adjust.y; |
682 | 804k | } |
683 | 804k | new_box.p.x = int2fixed(fixed2int_pixround(new_box.p.x - adjust_xl)); |
684 | 804k | new_box.p.y = int2fixed(fixed2int_pixround(new_box.p.y - adjust_yl)); |
685 | 804k | new_box.q.x = int2fixed(fixed2int_pixround(new_box.q.x + adjust_xu)); |
686 | 804k | new_box.q.y = int2fixed(fixed2int_pixround(new_box.q.y + adjust_yu)); |
687 | 804k | } |
688 | | /* Intersect the two rectangles if necessary. */ |
689 | 804k | if (old_box.p.x > new_box.p.x) |
690 | 153k | new_box.p.x = old_box.p.x, ++changed; |
691 | 804k | if (old_box.p.y > new_box.p.y) |
692 | 75.3k | new_box.p.y = old_box.p.y, ++changed; |
693 | 804k | if (old_box.q.x < new_box.q.x) |
694 | 76.3k | new_box.q.x = old_box.q.x, ++changed; |
695 | 804k | if (old_box.q.y < new_box.q.y) |
696 | 85.0k | new_box.q.y = old_box.q.y, ++changed; |
697 | | /* Check for a degenerate rectangle. */ |
698 | 804k | if (new_box.q.x < new_box.p.x || new_box.q.y < new_box.p.y) |
699 | 50.9k | new_box.p = new_box.q, changed = 1; |
700 | 804k | } |
701 | 857k | if (changed == 4) { |
702 | | /* The new box/path is the same as the old. */ |
703 | 11.6k | return 0; |
704 | 11.6k | } |
705 | | /* Release the existing path. */ |
706 | 845k | rc_decrement(pcpath->path_list, "gx_cpath_intersect"); |
707 | 845k | pcpath->path_list = NULL; |
708 | 845k | gx_path_new(&pcpath->path); |
709 | 845k | ppath->bbox = new_box; |
710 | 845k | cpath_set_rectangle(pcpath, &new_box); |
711 | 845k | if (changed == 0) { |
712 | | /* The path is valid; otherwise, defer constructing it. */ |
713 | 563k | gx_path_assign_preserve(&pcpath->path, ppath); |
714 | 563k | pcpath->path_valid = true; |
715 | 563k | pcpath->path_fill_adjust = params != NULL ? params->adjust : pgs->fill_adjust; |
716 | 563k | } |
717 | 845k | } else { |
718 | | /* New clip path is nontrivial. Intersect the slow way. */ |
719 | 98.4k | gx_cpath_path_list *next = NULL; |
720 | 98.4k | bool path_valid = |
721 | 98.4k | pcpath_is_rect && |
722 | 98.4k | gx_path_bbox(ppath, &new_box) >= 0 && |
723 | 98.4k | gx_cpath_includes_rectangle(pcpath, |
724 | 91.8k | new_box.p.x, new_box.p.y, |
725 | 91.8k | new_box.q.x, new_box.q.y); |
726 | | |
727 | 98.4k | if (!path_valid && next == NULL) { |
728 | | /* gx_cpaths should generally have a path_list set within |
729 | | * them. In some cases (filled images), they may not. Ensure |
730 | | * that they do, and remember the path_list */ |
731 | 57.0k | code = gx_cpath_ensure_path_list(pcpath); |
732 | 57.0k | if (code < 0) |
733 | 0 | goto ex; |
734 | | /* gx_cpath_intersect_path_slow NULLs pcpath->path_list, so |
735 | | * remember it here. */ |
736 | 57.0k | next = pcpath->path_list; |
737 | 57.0k | rc_increment(next); |
738 | 57.0k | } |
739 | 98.4k | code = gx_cpath_intersect_path_slow(pcpath, (params != NULL ? ppath_orig : ppath), |
740 | 98.4k | rule, pgs, params); |
741 | 98.4k | if (code < 0) { |
742 | 2 | rc_decrement(next, "gx_cpath_clip"); |
743 | 2 | goto ex; |
744 | 2 | } |
745 | 98.4k | if (path_valid) { |
746 | 41.3k | gx_path_assign_preserve(&pcpath->path, ppath_orig); |
747 | 41.3k | pcpath->path_valid = true; |
748 | 41.3k | pcpath->path_fill_adjust = params != NULL ? params->adjust : pgs->fill_adjust; |
749 | 41.3k | pcpath->rule = rule; |
750 | 57.0k | } else { |
751 | 57.0k | code = gx_cpath_path_list_new(pcpath->path.memory, NULL, rule, |
752 | 57.0k | ppath_orig, next, &pcpath->path_list); |
753 | 57.0k | } |
754 | 98.4k | rc_decrement(next, "gx_cpath_clip"); |
755 | 98.4k | } |
756 | 944k | ex: |
757 | 944k | if (ppath != ppath_orig) |
758 | 30.3k | gx_path_free(ppath, "gx_cpath_clip"); |
759 | 944k | return code; |
760 | 955k | } |
761 | | int |
762 | | gx_cpath_intersect(gx_clip_path *pcpath, /*const*/ gx_path *ppath_orig, |
763 | | int rule, gs_gstate *pgs) |
764 | 506k | { |
765 | 506k | return gx_cpath_intersect_with_params(pcpath, ppath_orig, |
766 | 506k | rule, pgs, NULL); |
767 | 506k | } |
768 | | |
769 | | /* Scale a clipping path by a power of 2. */ |
770 | | int |
771 | | gx_cpath_scale_exp2_shared(gx_clip_path * pcpath, int log2_scale_x, |
772 | | int log2_scale_y, bool list_shared, |
773 | | bool segments_shared) |
774 | 0 | { |
775 | 0 | int code = |
776 | 0 | (pcpath->path_valid ? |
777 | 0 | gx_path_scale_exp2_shared(&pcpath->path, log2_scale_x, log2_scale_y, |
778 | 0 | segments_shared) : |
779 | 0 | 0); |
780 | 0 | gx_clip_list *list = gx_cpath_list_private(pcpath); |
781 | 0 | gx_clip_rect *pr; |
782 | |
|
783 | 0 | if (code < 0) |
784 | 0 | return code; |
785 | | /* Scale the fixed entries. */ |
786 | 0 | gx_rect_scale_exp2(&pcpath->inner_box, log2_scale_x, log2_scale_y); |
787 | 0 | gx_rect_scale_exp2(&pcpath->outer_box, log2_scale_x, log2_scale_y); |
788 | 0 | if (!list_shared) { |
789 | | /* Scale the clipping list. */ |
790 | 0 | pr = list->head; |
791 | 0 | if (pr == 0) |
792 | 0 | pr = &list->single; |
793 | 0 | for (; pr != 0; pr = pr->next) |
794 | 0 | if (pr != list->head && pr != list->tail) { |
795 | |
|
796 | 0 | #define SCALE_V(v, s)\ |
797 | 0 | if ( pr->v != min_int && pr->v != max_int )\ |
798 | 0 | pr->v = (s >= 0 ? pr->v << s : pr->v >> -s) |
799 | |
|
800 | 0 | SCALE_V(xmin, log2_scale_x); |
801 | 0 | SCALE_V(xmax, log2_scale_x); |
802 | 0 | SCALE_V(ymin, log2_scale_y); |
803 | 0 | SCALE_V(ymax, log2_scale_y); |
804 | 0 | #undef SCALE_V |
805 | 0 | } |
806 | 0 | if (log2_scale_x > 0) { |
807 | 0 | list->xmin <<= log2_scale_x; |
808 | 0 | list->xmax <<= log2_scale_x; |
809 | 0 | } else { |
810 | 0 | list->xmin = arith_rshift(list->xmin, -log2_scale_x); |
811 | 0 | list->xmax = arith_rshift(list->xmax, -log2_scale_x); |
812 | 0 | } |
813 | 0 | } |
814 | 0 | pcpath->id = gs_next_ids(pcpath->path.memory, 1); /* path changed => change id */ |
815 | 0 | return 0; |
816 | 0 | } |
817 | | |
818 | | /* ------ Clipping list routines ------ */ |
819 | | |
820 | | /* Initialize a clip list. */ |
821 | | void |
822 | | gx_clip_list_init(gx_clip_list * clp) |
823 | 16.2M | { |
824 | 16.2M | *clp = clip_list_empty; |
825 | 16.2M | } |
826 | | |
827 | | /* Initialize a clip list to a rectangle. */ |
828 | | /* The supplied rectangle may not be oriented correctly, */ |
829 | | /* but it will be oriented correctly upon return. */ |
830 | | static void |
831 | | gx_clip_list_from_rectangle(register gx_clip_list * clp, |
832 | | register gs_fixed_rect * rp) |
833 | 7.66M | { |
834 | 7.66M | gx_clip_list_init(clp); |
835 | 7.66M | if (rp->p.x > rp->q.x) { |
836 | 0 | fixed t = rp->p.x; |
837 | |
|
838 | 0 | rp->p.x = rp->q.x; |
839 | 0 | rp->q.x = t; |
840 | 0 | } |
841 | 7.66M | if (rp->p.y > rp->q.y) { |
842 | 0 | fixed t = rp->p.y; |
843 | |
|
844 | 0 | rp->p.y = rp->q.y; |
845 | 0 | rp->q.y = t; |
846 | 0 | } |
847 | 7.66M | clp->single.xmin = clp->xmin = fixed2int_var(rp->p.x); |
848 | 7.66M | clp->single.ymin = fixed2int_var(rp->p.y); |
849 | | /* Handle degenerate rectangles specially. */ |
850 | 7.66M | clp->single.xmax = clp->xmax = |
851 | 7.66M | (rp->q.x == rp->p.x ? clp->single.xmin : |
852 | 7.66M | fixed2int_var_ceiling(rp->q.x)); |
853 | 7.66M | clp->single.ymax = |
854 | 7.66M | (rp->q.y == rp->p.y ? clp->single.ymin : |
855 | 7.66M | fixed2int_var_ceiling(rp->q.y)); |
856 | 7.66M | clp->count = 1; |
857 | 7.66M | } |
858 | | |
859 | | /* Start enumerating a clipping path. */ |
860 | | int |
861 | | gx_cpath_enum_init(gs_cpath_enum * penum, const gx_clip_path * pcpath) |
862 | 481k | { |
863 | 481k | if ((penum->using_path = pcpath->path_valid)) { |
864 | 56.6k | gx_path_enum_init(&penum->path_enum, &pcpath->path); |
865 | 56.6k | penum->rp = penum->visit = 0; |
866 | 56.6k | penum->first_visit = visit_left; |
867 | 425k | } else { |
868 | 425k | gx_path empty_path; |
869 | 425k | gx_clip_list *clp = gx_cpath_list_private(pcpath); |
870 | 425k | gx_clip_rect *head = (clp->count <= 1 ? &clp->single : clp->head); |
871 | 425k | gx_clip_rect *rp; |
872 | | |
873 | | /* Initialize the pointers in the path_enum properly. */ |
874 | 425k | gx_path_init_local(&empty_path, pcpath->path.memory); |
875 | 425k | gx_path_enum_init(&penum->path_enum, &empty_path); |
876 | 425k | penum->first_visit = visit_left; |
877 | 425k | penum->visit = head; |
878 | 1.58M | for (rp = head; rp != 0; rp = rp->next) |
879 | 1.16M | rp->to_visit = |
880 | 1.16M | (rp->xmin < rp->xmax && rp->ymin < rp->ymax ? |
881 | 993k | visit_left | visit_right : 0); |
882 | 425k | penum->rp = 0; /* scan will initialize */ |
883 | 425k | penum->any_rectangles = false; |
884 | 425k | penum->state = cpe_scan; |
885 | 425k | penum->have_line = false; |
886 | 425k | } |
887 | 481k | return 0; |
888 | 481k | } |
889 | | |
890 | | /* Enumerate the next segment of a clipping path. */ |
891 | | /* In general, this produces a path made up of zillions of tiny lines. */ |
892 | | int |
893 | | gx_cpath_enum_next(gs_cpath_enum * penum, gs_fixed_point pts[3]) |
894 | 3.06M | { |
895 | 3.06M | if (penum->using_path) |
896 | 99.6k | return gx_path_enum_next(&penum->path_enum, pts); |
897 | 2.96M | #define set_pt(xi, yi)\ |
898 | 2.96M | (pts[0].x = int2fixed(xi), pts[0].y = int2fixed(yi)) |
899 | 2.96M | #define set_line(xi, yi)\ |
900 | 2.96M | (penum->line_end.x = (xi), penum->line_end.y = (yi), penum->have_line = true) |
901 | 2.96M | if (penum->have_line) { |
902 | 782k | set_pt(penum->line_end.x, penum->line_end.y); |
903 | 782k | penum->have_line = false; |
904 | 782k | return gs_pe_lineto; |
905 | 2.18M | } { |
906 | 2.18M | gx_clip_rect *visit = penum->visit; |
907 | 2.18M | gx_clip_rect *rp = penum->rp; |
908 | 2.18M | cpe_visit_t first_visit = penum->first_visit; |
909 | 2.18M | cpe_state_t state = penum->state; |
910 | 2.18M | gx_clip_rect *look; |
911 | 2.18M | int code; |
912 | | |
913 | 2.18M | switch (state) { |
914 | | |
915 | 700k | case cpe_scan: |
916 | | /* Look for the start of an edge to trace. */ |
917 | 1.80M | for (; visit != 0; visit = visit->next) { |
918 | 1.38M | if (visit->to_visit & visit_left) { |
919 | 277k | set_pt(visit->xmin, visit->ymin); |
920 | 277k | first_visit = visit_left; |
921 | 277k | state = cpe_left; |
922 | 1.10M | } else if (visit->to_visit & visit_right) { |
923 | 1.81k | set_pt(visit->xmax, visit->ymax); |
924 | 1.81k | first_visit = visit_right; |
925 | 1.81k | state = cpe_right; |
926 | 1.81k | } else |
927 | 1.10M | continue; |
928 | 278k | rp = visit; |
929 | 278k | code = gs_pe_moveto; |
930 | 278k | penum->any_rectangles = true; |
931 | 278k | goto out; |
932 | 1.38M | } |
933 | | /* We've enumerated all the edges. */ |
934 | 421k | state = cpe_done; |
935 | 421k | if (!penum->any_rectangles) { |
936 | | /* We didn't have any rectangles. */ |
937 | 152k | set_pt(fixed_0, fixed_0); |
938 | 152k | code = gs_pe_moveto; |
939 | 152k | break; |
940 | 152k | } |
941 | | /* falls through */ |
942 | | |
943 | 421k | case cpe_done: |
944 | | /* All done. */ |
945 | 421k | code = 0; |
946 | 421k | break; |
947 | | |
948 | | /* We can't use the BEGIN ... END hack here: we need to be able to break. */ |
949 | 0 | #define return_line(px, py)\ |
950 | 1.05M | set_pt(px, py); code = gs_pe_lineto; break |
951 | | |
952 | 531k | case cpe_left: |
953 | | |
954 | 934k | left: /* Trace upward along a left edge. */ |
955 | | /* We're at the lower left corner of rp. */ |
956 | 934k | rp->to_visit &= ~visit_left; |
957 | | /* Look for an adjacent rectangle above rp. */ |
958 | 934k | for (look = rp; |
959 | 6.28M | (look = look->next) != 0 && |
960 | 6.28M | (look->ymin == rp->ymin || |
961 | 6.02M | (look->ymin == rp->ymax && look->xmax <= rp->xmin)); |
962 | 5.35M | ); |
963 | | /* Now we know look->ymin >= rp->ymax. */ |
964 | 934k | if (look == 0 || look->ymin > rp->ymax || |
965 | 934k | look->xmin >= rp->xmax |
966 | 934k | ) { /* No adjacent rectangle, switch directions. */ |
967 | 276k | state = |
968 | 276k | (rp == visit && first_visit == visit_right ? cpe_close : |
969 | 276k | (set_line(rp->xmax, rp->ymax), cpe_right)); |
970 | 276k | return_line(rp->xmin, rp->ymax); |
971 | 276k | } |
972 | | /* We found an adjacent rectangle. */ |
973 | | /* See if it also adjoins a rectangle to the left of rp. */ |
974 | 657k | { |
975 | 657k | gx_clip_rect *prev = rp->prev; |
976 | 657k | gx_clip_rect *cur = rp; |
977 | | |
978 | 657k | if (prev != 0 && prev->ymax == rp->ymax && |
979 | 657k | look->xmin < prev->xmax |
980 | 657k | ) { /* There's an adjoining rectangle as well. */ |
981 | | /* Switch directions. */ |
982 | 3.70k | rp = prev; |
983 | 3.70k | state = |
984 | 3.70k | (rp == visit && first_visit == visit_right ? cpe_close : |
985 | 3.70k | (set_line(prev->xmax, prev->ymax), cpe_right)); |
986 | 3.70k | return_line(cur->xmin, cur->ymax); |
987 | 3.70k | } |
988 | 654k | rp = look; |
989 | 654k | if (rp == visit && first_visit == visit_left) |
990 | 0 | state = cpe_close; |
991 | 654k | else if (rp->xmin == cur->xmin) |
992 | 402k | goto left; |
993 | 251k | else |
994 | 251k | set_line(rp->xmin, rp->ymin); |
995 | 654k | return_line(cur->xmin, cur->ymax); |
996 | 654k | } |
997 | | |
998 | 527k | case cpe_right: |
999 | | |
1000 | 933k | right: /* Trace downward along a right edge. */ |
1001 | | /* We're at the upper right corner of rp. */ |
1002 | 933k | rp->to_visit &= ~visit_right; |
1003 | | /* Look for an adjacent rectangle below rp. */ |
1004 | 933k | for (look = rp; |
1005 | 6.28M | (look = look->prev) != 0 && |
1006 | 6.28M | (look->ymax == rp->ymax || |
1007 | 6.02M | (look->ymax == rp->ymin && look->xmin >= rp->xmax)); |
1008 | 5.35M | ); |
1009 | | /* Now we know look->ymax <= rp->ymin. */ |
1010 | 933k | if (look == 0 || look->ymax < rp->ymin || |
1011 | 933k | look->xmax <= rp->xmin |
1012 | 933k | ) { /* No adjacent rectangle, switch directions. */ |
1013 | 275k | state = |
1014 | 275k | (rp == visit && first_visit == visit_left ? cpe_close : |
1015 | 275k | (set_line(rp->xmin, rp->ymin), cpe_left)); |
1016 | 275k | return_line(rp->xmax, rp->ymin); |
1017 | 275k | } |
1018 | | /* We found an adjacent rectangle. */ |
1019 | | /* See if it also adjoins a rectangle to the right of rp. */ |
1020 | 658k | { |
1021 | 658k | gx_clip_rect *next = rp->next; |
1022 | 658k | gx_clip_rect *cur = rp; |
1023 | | |
1024 | 658k | if (next != 0 && next->ymin == rp->ymin && |
1025 | 658k | look->xmax > next->xmin |
1026 | 658k | ) { /* There's an adjoining rectangle as well. */ |
1027 | | /* Switch directions. */ |
1028 | 4.30k | rp = next; |
1029 | 4.30k | state = |
1030 | 4.30k | (rp == visit && first_visit == visit_left ? cpe_close : |
1031 | 4.30k | (set_line(next->xmin, next->ymin), cpe_left)); |
1032 | 4.30k | return_line(cur->xmax, cur->ymin); |
1033 | 4.30k | } |
1034 | 654k | rp = look; |
1035 | 654k | if (rp == visit && first_visit == visit_right) |
1036 | 1.62k | state = cpe_close; |
1037 | 652k | else if (rp->xmax == cur->xmax) |
1038 | 406k | goto right; |
1039 | 245k | else |
1040 | 245k | set_line(rp->xmax, rp->ymax); |
1041 | 654k | return_line(cur->xmax, cur->ymin); |
1042 | 654k | } |
1043 | | |
1044 | 0 | #undef return_line |
1045 | | |
1046 | 275k | case cpe_close: |
1047 | | /* We've gone all the way around an edge. */ |
1048 | 275k | code = gs_pe_closepath; |
1049 | 275k | state = cpe_scan; |
1050 | 275k | break; |
1051 | | |
1052 | 0 | default: |
1053 | 0 | return_error(gs_error_unknownerror); |
1054 | 2.18M | } |
1055 | | |
1056 | 2.18M | out: /* Store the state before exiting. */ |
1057 | 2.18M | penum->visit = visit; |
1058 | 2.18M | penum->rp = rp; |
1059 | 2.18M | penum->first_visit = first_visit; |
1060 | 2.18M | penum->state = state; |
1061 | 2.18M | return code; |
1062 | 2.18M | } |
1063 | 2.18M | #undef set_pt |
1064 | 2.18M | #undef set_line |
1065 | 2.18M | } |
1066 | | segment_notes |
1067 | | gx_cpath_enum_notes(const gs_cpath_enum * penum) |
1068 | 2.09M | { |
1069 | 2.09M | return sn_none; |
1070 | 2.09M | } |
1071 | | |
1072 | | /* Free a clip list. */ |
1073 | | void |
1074 | | gx_clip_list_free(gx_clip_list * clp, gs_memory_t * mem) |
1075 | 7.66M | { |
1076 | 7.66M | gx_clip_rect *rp = clp->tail; |
1077 | | |
1078 | 15.8M | while (rp != 0) { |
1079 | 8.22M | gx_clip_rect *prev = rp->prev; |
1080 | | |
1081 | 8.22M | gs_free_object(mem, rp, "gx_clip_list_free"); |
1082 | 8.22M | rp = prev; |
1083 | 8.22M | } |
1084 | 7.66M | gx_clip_list_init(clp); |
1085 | 7.66M | } |
1086 | | |
1087 | | /* Check whether a rectangle has a non-empty intersection with a clipping patch. */ |
1088 | | bool |
1089 | | gx_cpath_rect_visible(gx_clip_path * pcpath, gs_int_rect *prect) |
1090 | 55.4k | { |
1091 | 55.4k | const gx_clip_rect *pr; |
1092 | 55.4k | const gx_clip_list *list = &pcpath->rect_list->list; |
1093 | | |
1094 | 55.4k | switch (list->count) { |
1095 | 361 | case 0: |
1096 | 361 | return false; |
1097 | 54.8k | case 1: |
1098 | 54.8k | pr = &list->single; |
1099 | 54.8k | break; |
1100 | 233 | default: |
1101 | 233 | pr = list->head; |
1102 | 55.4k | } |
1103 | 141k | for (; pr != 0; pr = pr->next) { |
1104 | 132k | if (pr->xmin > prect->q.x) |
1105 | 20.3k | continue; |
1106 | 112k | if (pr->xmax < prect->p.x) |
1107 | 52.5k | continue; |
1108 | 59.9k | if (pr->ymin > prect->q.y) |
1109 | 10.6k | continue; |
1110 | 49.3k | if (pr->ymax < prect->p.y) |
1111 | 2.59k | continue; |
1112 | 46.7k | return true; |
1113 | 49.3k | } |
1114 | 8.37k | return false; |
1115 | 55.1k | } |
1116 | | |
1117 | | int |
1118 | | gx_cpath_copy(const gx_clip_path * from, gx_clip_path * pcpath) |
1119 | 15.8k | { /* *pcpath must be initialized. */ |
1120 | 15.8k | gx_clip_rect *r, *s; |
1121 | 15.8k | gx_clip_list *l = &pcpath->rect_list->list; |
1122 | | |
1123 | 15.8k | pcpath->path_valid = false; |
1124 | | /* NOTE: pcpath->path still contains the old path. */ |
1125 | 15.8k | if (pcpath->path_list) |
1126 | 15.8k | rc_decrement(pcpath->path_list, "gx_cpath_copy"); |
1127 | 15.8k | pcpath->path_list = NULL; |
1128 | 15.8k | pcpath->rule = from->rule; |
1129 | 15.8k | pcpath->outer_box = from->outer_box; |
1130 | 15.8k | pcpath->inner_box = from->inner_box; |
1131 | 15.8k | pcpath->cached = NULL; |
1132 | 15.8k | l->single = from->rect_list->list.single; |
1133 | 530k | for (r = from->rect_list->list.head; r != NULL; r = r->next) { |
1134 | 514k | if (pcpath->rect_list->rc.memory == NULL) |
1135 | 0 | s = gs_alloc_struct(from->rect_list->rc.memory, gx_clip_rect, &st_clip_rect, "gx_cpath_copy"); |
1136 | 514k | else |
1137 | 514k | s = gs_alloc_struct(pcpath->rect_list->rc.memory, gx_clip_rect, &st_clip_rect, "gx_cpath_copy"); |
1138 | | |
1139 | 514k | if (s == NULL) |
1140 | 0 | return_error(gs_error_VMerror); |
1141 | 514k | *s = *r; |
1142 | 514k | s->next = NULL; |
1143 | 514k | if (l->tail) { |
1144 | 514k | s->prev = l->tail; |
1145 | 514k | l->tail->next = s; |
1146 | 514k | } else { |
1147 | 172 | l->head = s; |
1148 | 172 | s->prev = NULL; |
1149 | 172 | } |
1150 | 514k | l->tail = s; |
1151 | 514k | } |
1152 | 15.8k | l->count = from->rect_list->list.count; |
1153 | 15.8k | return 0; |
1154 | 15.8k | } |
1155 | | |
1156 | | /* ------ Debugging printout ------ */ |
1157 | | |
1158 | | #ifdef DEBUG |
1159 | | |
1160 | | /* Print a clipping list. */ |
1161 | | static void |
1162 | | gx_clip_list_print(const gs_memory_t *mem, const gx_clip_list *list) |
1163 | | { |
1164 | | const gx_clip_rect *pr; |
1165 | | |
1166 | | dmlprintf3(mem, " list count=%d xmin=%d xmax=%d\n", |
1167 | | list->count, list->xmin, list->xmax); |
1168 | | switch (list->count) { |
1169 | | case 0: |
1170 | | pr = 0; |
1171 | | break; |
1172 | | case 1: |
1173 | | pr = &list->single; |
1174 | | break; |
1175 | | default: |
1176 | | pr = list->head; |
1177 | | } |
1178 | | for (; pr != 0; pr = pr->next) |
1179 | | dmlprintf4(mem, " rect: (%d,%d),(%d,%d)\n", |
1180 | | pr->xmin, pr->ymin, pr->xmax, pr->ymax); |
1181 | | } |
1182 | | |
1183 | | /* Print a clipping path */ |
1184 | | void |
1185 | | gx_cpath_print(const gs_memory_t *mem, const gx_clip_path * pcpath) |
1186 | | { |
1187 | | if (pcpath->path_valid) |
1188 | | gx_path_print(&pcpath->path); |
1189 | | else |
1190 | | dmlputs(mem, " (path not valid)\n"); |
1191 | | dmlprintf4(mem, " inner_box=(%g,%g),(%g,%g)\n", |
1192 | | fixed2float(pcpath->inner_box.p.x), |
1193 | | fixed2float(pcpath->inner_box.p.y), |
1194 | | fixed2float(pcpath->inner_box.q.x), |
1195 | | fixed2float(pcpath->inner_box.q.y)); |
1196 | | dmlprintf4(mem, " outer_box=(%g,%g),(%g,%g)", |
1197 | | fixed2float(pcpath->outer_box.p.x), |
1198 | | fixed2float(pcpath->outer_box.p.y), |
1199 | | fixed2float(pcpath->outer_box.q.x), |
1200 | | fixed2float(pcpath->outer_box.q.y)); |
1201 | | dmprintf2(mem, " rule=%d list.refct=%ld\n", |
1202 | | pcpath->rule, pcpath->rect_list->rc.ref_count); |
1203 | | gx_clip_list_print(mem, gx_cpath_list(pcpath)); |
1204 | | } |
1205 | | |
1206 | | #endif /* DEBUG */ |