gxcpath.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /* Copyright (C) 1991, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxcpath.c,v 1.16 2005/07/27 22:34:40 igor Exp $ */
  14. /* Implementation of clipping paths, other than actual clipping */
  15. #include "gx.h"
  16. #include "gserrors.h"
  17. #include "gsstruct.h"
  18. #include "gsutil.h"
  19. #include "gsline.h"
  20. #include "gxdevice.h"
  21. #include "gxfixed.h"
  22. #include "gscoord.h" /* needs gsmatrix.h */
  23. #include "gxistate.h"
  24. #include "gzpath.h"
  25. #include "gzcpath.h"
  26. #include "gzacpath.h"
  27. /* Forward references */
  28. private void gx_clip_list_from_rectangle(gx_clip_list *, gs_fixed_rect *);
  29. /* Other structure types */
  30. public_st_clip_rect();
  31. private_st_clip_list();
  32. public_st_clip_path();
  33. private_st_clip_rect_list();
  34. public_st_device_clip();
  35. private_st_cpath_enum();
  36. private_st_cpath_path_list();
  37. /* GC procedures for gx_clip_path */
  38. private
  39. ENUM_PTRS_WITH(clip_path_enum_ptrs, gx_clip_path *cptr) return ENUM_USING(st_path, &cptr->path, sizeof(cptr->path), index - 2);
  40. case 0:
  41. return ENUM_OBJ((cptr->rect_list == &cptr->local_list ? 0 :
  42. cptr->rect_list));
  43. case 1:
  44. return ENUM_OBJ(cptr->path_list);
  45. ENUM_PTRS_END
  46. private
  47. RELOC_PTRS_WITH(clip_path_reloc_ptrs, gx_clip_path *cptr)
  48. {
  49. if (cptr->rect_list != &cptr->local_list)
  50. RELOC_VAR(cptr->rect_list);
  51. RELOC_VAR(cptr->path_list);
  52. RELOC_USING(st_path, &cptr->path, sizeof(gx_path));
  53. }
  54. RELOC_PTRS_END
  55. /* GC procedures for gx_device_clip */
  56. private
  57. ENUM_PTRS_WITH(device_clip_enum_ptrs, gx_device_clip *cptr)
  58. {
  59. if (index < st_clip_list_max_ptrs + 1)
  60. return ENUM_USING(st_clip_list, &cptr->list,
  61. sizeof(gx_clip_list), index - 1);
  62. return ENUM_USING(st_device_forward, vptr,
  63. sizeof(gx_device_forward),
  64. index - (st_clip_list_max_ptrs + 1));
  65. }
  66. case 0:
  67. ENUM_RETURN((cptr->current == &cptr->list.single ? NULL :
  68. (void *)cptr->current));
  69. ENUM_PTRS_END
  70. private
  71. RELOC_PTRS_WITH(device_clip_reloc_ptrs, gx_device_clip *cptr)
  72. {
  73. if (cptr->current == &cptr->list.single)
  74. cptr->current = &((gx_device_clip *)RELOC_OBJ(vptr))->list.single;
  75. else
  76. RELOC_PTR(gx_device_clip, current);
  77. RELOC_USING(st_clip_list, &cptr->list, sizeof(gx_clip_list));
  78. RELOC_USING(st_device_forward, vptr, sizeof(gx_device_forward));
  79. }
  80. RELOC_PTRS_END
  81. /* Define an empty clip list. */
  82. private const gx_clip_list clip_list_empty = {
  83. {0, 0, min_int, max_int, 0, 0},
  84. 0, 0, 0, 0, 0
  85. };
  86. /* ------ Clipping path memory management ------ */
  87. private rc_free_proc(rc_free_cpath_list);
  88. private rc_free_proc(rc_free_cpath_list_local);
  89. private rc_free_proc(rc_free_cpath_path_list);
  90. /*
  91. * Initialize those parts of the contents of a clip path that aren't
  92. * part of the path.
  93. */
  94. private void
  95. cpath_init_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox)
  96. {
  97. gx_clip_list_from_rectangle(&pcpath->rect_list->list, pbox);
  98. pcpath->inner_box = *pbox;
  99. pcpath->path_valid = false;
  100. pcpath->path.bbox = *pbox;
  101. gx_cpath_set_outer_box(pcpath);
  102. pcpath->id = gs_next_ids(pcpath->path.memory, 1); /* path changed => change id */
  103. }
  104. private void
  105. cpath_init_own_contents(gx_clip_path * pcpath)
  106. { /* We could make null_rect static, but then it couldn't be const. */
  107. gs_fixed_rect null_rect;
  108. null_rect.p.x = null_rect.p.y = null_rect.q.x = null_rect.q.y = 0;
  109. cpath_init_rectangle(pcpath, &null_rect);
  110. pcpath->path_list = NULL;
  111. }
  112. private void
  113. cpath_share_own_contents(gx_clip_path * pcpath, const gx_clip_path * shared)
  114. {
  115. pcpath->inner_box = shared->inner_box;
  116. pcpath->path_valid = shared->path_valid;
  117. pcpath->outer_box = shared->outer_box;
  118. pcpath->id = shared->id;
  119. }
  120. /* Allocate only the segments of a clipping path on the heap. */
  121. private int
  122. cpath_alloc_list(gx_clip_rect_list ** prlist, gs_memory_t * mem,
  123. client_name_t cname)
  124. {
  125. rc_alloc_struct_1(*prlist, gx_clip_rect_list, &st_clip_rect_list, mem,
  126. return_error(gs_error_VMerror), cname);
  127. (*prlist)->rc.free = rc_free_cpath_list;
  128. return 0;
  129. }
  130. int
  131. gx_cpath_init_contained_shared(gx_clip_path * pcpath,
  132. const gx_clip_path * shared, gs_memory_t * mem, client_name_t cname)
  133. {
  134. if (shared) {
  135. if (shared->path.segments == &shared->path.local_segments) {
  136. lprintf1("Attempt to share (local) segments of clip path 0x%lx!\n",
  137. (ulong) shared);
  138. return_error(gs_error_Fatal);
  139. }
  140. *pcpath = *shared;
  141. pcpath->path.memory = mem;
  142. pcpath->path.allocation = path_allocated_contained;
  143. rc_increment(pcpath->path.segments);
  144. rc_increment(pcpath->rect_list);
  145. rc_increment(pcpath->path_list);
  146. } else {
  147. int code = cpath_alloc_list(&pcpath->rect_list, mem, cname);
  148. if (code < 0)
  149. return code;
  150. code = gx_path_alloc_contained(&pcpath->path, mem, cname);
  151. if (code < 0) {
  152. gs_free_object(mem, pcpath->rect_list, cname);
  153. pcpath->rect_list = 0;
  154. return code;
  155. }
  156. cpath_init_own_contents(pcpath);
  157. }
  158. return 0;
  159. }
  160. #define gx_cpath_alloc_contents(pcpath, shared, mem, cname)\
  161. gx_cpath_init_contained_shared(pcpath, shared, mem, cname)
  162. /* Allocate all of a clipping path on the heap. */
  163. gx_clip_path *
  164. gx_cpath_alloc_shared(const gx_clip_path * shared, gs_memory_t * mem,
  165. client_name_t cname)
  166. {
  167. gx_clip_path *pcpath =
  168. gs_alloc_struct(mem, gx_clip_path, &st_clip_path, cname);
  169. int code;
  170. if (pcpath == 0)
  171. return 0;
  172. code = gx_cpath_alloc_contents(pcpath, shared, mem, cname);
  173. if (code < 0) {
  174. gs_free_object(mem, pcpath, cname);
  175. return 0;
  176. }
  177. pcpath->path.allocation = path_allocated_on_heap;
  178. return pcpath;
  179. }
  180. /* Initialize a stack-allocated clipping path. */
  181. int
  182. gx_cpath_init_local_shared(gx_clip_path * pcpath, const gx_clip_path * shared,
  183. gs_memory_t * mem)
  184. {
  185. if (shared) {
  186. if (shared->path.segments == &shared->path.local_segments) {
  187. lprintf1("Attempt to share (local) segments of clip path 0x%lx!\n",
  188. (ulong) shared);
  189. return_error(gs_error_Fatal);
  190. }
  191. pcpath->path = shared->path;
  192. pcpath->path.allocation = path_allocated_on_stack;
  193. rc_increment(pcpath->path.segments);
  194. pcpath->rect_list = shared->rect_list;
  195. rc_increment(pcpath->rect_list);
  196. pcpath->path_list = shared->path_list;
  197. rc_increment(pcpath->path_list);
  198. cpath_share_own_contents(pcpath, shared);
  199. } else {
  200. gx_path_init_local(&pcpath->path, mem);
  201. rc_init_free(&pcpath->local_list, mem, 1, rc_free_cpath_list_local);
  202. pcpath->rect_list = &pcpath->local_list;
  203. cpath_init_own_contents(pcpath);
  204. }
  205. return 0;
  206. }
  207. /* Unshare a clipping path. */
  208. int
  209. gx_cpath_unshare(gx_clip_path * pcpath)
  210. {
  211. int code = gx_path_unshare(&pcpath->path);
  212. gx_clip_rect_list *rlist = pcpath->rect_list;
  213. if (code < 0)
  214. return code;
  215. if (rlist->rc.ref_count > 1) {
  216. int code = cpath_alloc_list(&pcpath->rect_list, pcpath->path.memory,
  217. "gx_cpath_unshare");
  218. if (code < 0)
  219. return code;
  220. /* Copy the rectangle list. */
  221. /**************** NYI ****************/
  222. rc_decrement(rlist, "gx_cpath_unshare");
  223. }
  224. return code;
  225. }
  226. /* Free a clipping path. */
  227. void
  228. gx_cpath_free(gx_clip_path * pcpath, client_name_t cname)
  229. {
  230. rc_decrement(pcpath->rect_list, cname);
  231. rc_decrement(pcpath->path_list, cname);
  232. /* Clean up pointers for GC. */
  233. pcpath->rect_list = 0;
  234. pcpath->path_list = 0;
  235. {
  236. gx_path_allocation_t alloc = pcpath->path.allocation;
  237. if (alloc == path_allocated_on_heap) {
  238. pcpath->path.allocation = path_allocated_contained;
  239. gx_path_free(&pcpath->path, cname);
  240. gs_free_object(pcpath->path.memory, pcpath, cname);
  241. } else
  242. gx_path_free(&pcpath->path, cname);
  243. }
  244. }
  245. /* Assign a clipping path, preserving the source. */
  246. int
  247. gx_cpath_assign_preserve(gx_clip_path * pcpto, gx_clip_path * pcpfrom)
  248. {
  249. int code = gx_path_assign_preserve(&pcpto->path, &pcpfrom->path);
  250. gx_clip_rect_list *fromlist = pcpfrom->rect_list;
  251. gx_clip_rect_list *tolist = pcpto->rect_list;
  252. gx_path path;
  253. if (code < 0)
  254. return 0;
  255. if (fromlist == &pcpfrom->local_list) {
  256. /* We can't use pcpfrom's list object. */
  257. if (tolist == &pcpto->local_list || tolist->rc.ref_count > 1) {
  258. /* We can't use pcpto's list either. Allocate a new one. */
  259. int code = cpath_alloc_list(&tolist, tolist->rc.memory,
  260. "gx_cpath_assign");
  261. if (code < 0)
  262. return code;
  263. rc_decrement(pcpto->rect_list, "gx_cpath_assign");
  264. } else {
  265. /* Use pcpto's list object. */
  266. rc_free_cpath_list_local(tolist->rc.memory, tolist,
  267. "gx_cpath_assign");
  268. }
  269. tolist->list = fromlist->list;
  270. pcpfrom->rect_list = tolist;
  271. rc_increment(tolist);
  272. } else {
  273. /* We can use pcpfrom's list object. */
  274. rc_increment(fromlist);
  275. rc_decrement(pcpto->rect_list, "gx_cpath_assign");
  276. }
  277. rc_increment(pcpfrom->path_list);
  278. path = pcpto->path, *pcpto = *pcpfrom, pcpto->path = path;
  279. return 0;
  280. }
  281. /* Assign a clipping path, releasing the source. */
  282. int
  283. gx_cpath_assign_free(gx_clip_path * pcpto, gx_clip_path * pcpfrom)
  284. { /* For right now, just do assign + free. */
  285. int code = gx_cpath_assign_preserve(pcpto, pcpfrom);
  286. if (code < 0)
  287. return 0;
  288. gx_cpath_free(pcpfrom, "gx_cpath_assign_free");
  289. return 0;
  290. }
  291. /* Free the clipping list when its reference count goes to zero. */
  292. private void
  293. rc_free_cpath_list_local(gs_memory_t * mem, void *vrlist,
  294. client_name_t cname)
  295. {
  296. gx_clip_rect_list *rlist = (gx_clip_rect_list *) vrlist;
  297. gx_clip_list_free(&rlist->list, mem);
  298. }
  299. private void
  300. rc_free_cpath_list(gs_memory_t * mem, void *vrlist, client_name_t cname)
  301. {
  302. rc_free_cpath_list_local(mem, vrlist, cname);
  303. gs_free_object(mem, vrlist, cname);
  304. }
  305. private void
  306. rc_free_cpath_path_list(gs_memory_t * mem, void *vplist, client_name_t cname)
  307. {
  308. gx_cpath_path_list *plist = (gx_cpath_path_list *)vplist;
  309. rc_decrement(plist->next, cname);
  310. gx_path_free(&plist->path, cname);
  311. gs_free_object(plist->path.memory, plist, cname);
  312. }
  313. /* Allocate a new clip path list node. The created node has a ref count
  314. of 1, and "steals" the reference to next (i.e. does not increment
  315. its reference count). */
  316. private int
  317. gx_cpath_path_list_new(gs_memory_t *mem, gx_clip_path *pcpath, int rule,
  318. gx_path *ppfrom, gx_cpath_path_list *next, gx_cpath_path_list **pnew)
  319. {
  320. int code;
  321. client_name_t cname = "gx_cpath_path_list_new";
  322. gx_cpath_path_list *pcplist = gs_alloc_struct(mem, gx_cpath_path_list,
  323. &st_cpath_path_list, cname);
  324. if (pcplist == 0)
  325. return_error(gs_error_VMerror);
  326. rc_init_free(pcplist, mem, 1, rc_free_cpath_path_list);
  327. if (pcpath!=NULL && !pcpath->path_valid) {
  328. code = gx_path_init_contained_shared(&pcplist->path, NULL, mem, cname);
  329. if (code < 0)
  330. return code;
  331. code = gx_cpath_to_path(pcpath, &pcplist->path);
  332. } else {
  333. gx_path_init_local(&pcplist->path, mem);
  334. code = gx_path_assign_preserve(&pcplist->path, ppfrom);
  335. }
  336. if (code < 0)
  337. return code;
  338. pcplist->next = next;
  339. pcplist->rule = rule;
  340. *pnew = pcplist;
  341. return 0;
  342. }
  343. /* ------ Clipping path accessing ------ */
  344. /* Return the path of a clipping path. */
  345. int
  346. gx_cpath_to_path(gx_clip_path * pcpath, gx_path * ppath)
  347. {
  348. if (!pcpath->path_valid) {
  349. /* Synthesize a path. */
  350. gs_cpath_enum cenum;
  351. gs_fixed_point pts[3];
  352. gx_path rpath;
  353. int code;
  354. gx_path_init_local(&rpath, pcpath->path.memory);
  355. gx_cpath_enum_init(&cenum, pcpath);
  356. while ((code = gx_cpath_enum_next(&cenum, pts)) != 0) {
  357. switch (code) {
  358. case gs_pe_moveto:
  359. code = gx_path_add_point(&rpath, pts[0].x, pts[0].y);
  360. break;
  361. case gs_pe_lineto:
  362. code = gx_path_add_line_notes(&rpath, pts[0].x, pts[0].y,
  363. gx_cpath_enum_notes(&cenum));
  364. break;
  365. case gs_pe_curveto:
  366. code = gx_path_add_curve_notes(&rpath, pts[0].x, pts[0].y,
  367. pts[1].x, pts[1].y,
  368. pts[2].x, pts[2].y,
  369. gx_cpath_enum_notes(&cenum));
  370. break;
  371. case gs_pe_closepath:
  372. code = gx_path_close_subpath_notes(&rpath,
  373. gx_cpath_enum_notes(&cenum));
  374. break;
  375. default:
  376. if (code >= 0)
  377. code = gs_note_error(gs_error_unregistered);
  378. }
  379. if (code < 0)
  380. break;
  381. }
  382. if (code >= 0)
  383. code = gx_path_assign_free(&pcpath->path, &rpath);
  384. if (code < 0) {
  385. gx_path_free(&rpath, "gx_cpath_to_path error");
  386. return code;
  387. }
  388. pcpath->path_valid = true;
  389. }
  390. return gx_path_assign_preserve(ppath, &pcpath->path);
  391. }
  392. /* Return the inner and outer check rectangles for a clipping path. */
  393. /* Return true iff the path is a rectangle. */
  394. bool
  395. gx_cpath_inner_box(const gx_clip_path * pcpath, gs_fixed_rect * pbox)
  396. {
  397. *pbox = pcpath->inner_box;
  398. return clip_list_is_rectangle(gx_cpath_list(pcpath));
  399. }
  400. bool
  401. gx_cpath_outer_box(const gx_clip_path * pcpath, gs_fixed_rect * pbox)
  402. {
  403. *pbox = pcpath->outer_box;
  404. return clip_list_is_rectangle(gx_cpath_list(pcpath));
  405. }
  406. /* Test if a clipping path includes a rectangle. */
  407. /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */
  408. bool
  409. gx_cpath_includes_rectangle(register const gx_clip_path * pcpath,
  410. fixed x0, fixed y0, fixed x1, fixed y1)
  411. {
  412. return
  413. (x0 <= x1 ?
  414. (pcpath->inner_box.p.x <= x0 && x1 <= pcpath->inner_box.q.x) :
  415. (pcpath->inner_box.p.x <= x1 && x0 <= pcpath->inner_box.q.x)) &&
  416. (y0 <= y1 ?
  417. (pcpath->inner_box.p.y <= y0 && y1 <= pcpath->inner_box.q.y) :
  418. (pcpath->inner_box.p.y <= y1 && y0 <= pcpath->inner_box.q.y));
  419. }
  420. /* Set the outer clipping box to the path bounding box, */
  421. /* expanded to pixel boundaries. */
  422. void
  423. gx_cpath_set_outer_box(gx_clip_path * pcpath)
  424. {
  425. pcpath->outer_box.p.x = fixed_floor(pcpath->path.bbox.p.x);
  426. pcpath->outer_box.p.y = fixed_floor(pcpath->path.bbox.p.y);
  427. pcpath->outer_box.q.x = fixed_ceiling(pcpath->path.bbox.q.x);
  428. pcpath->outer_box.q.y = fixed_ceiling(pcpath->path.bbox.q.y);
  429. }
  430. /* Return the rectangle list of a clipping path (for local use only). */
  431. const gx_clip_list *
  432. gx_cpath_list(const gx_clip_path *pcpath)
  433. {
  434. return &pcpath->rect_list->list;
  435. }
  436. /* Internal non-const version of the same accessor. */
  437. inline private gx_clip_list *
  438. gx_cpath_list_private(gx_clip_path *pcpath)
  439. {
  440. return &pcpath->rect_list->list;
  441. }
  442. /* ------ Clipping path setting ------ */
  443. /* Create a rectangular clipping path. */
  444. /* The supplied rectangle may not be oriented correctly, */
  445. /* but it will be oriented correctly upon return. */
  446. private int
  447. cpath_set_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox)
  448. {
  449. gx_clip_rect_list *rlist = pcpath->rect_list;
  450. if (rlist->rc.ref_count <= 1)
  451. gx_clip_list_free(&rlist->list, rlist->rc.memory);
  452. else {
  453. int code = cpath_alloc_list(&pcpath->rect_list, pcpath->path.memory,
  454. "gx_cpath_from_rectangle");
  455. if (code < 0)
  456. return code;
  457. rc_decrement(rlist, "gx_cpath_from_rectangle");
  458. rlist = pcpath->rect_list;
  459. }
  460. cpath_init_rectangle(pcpath, pbox);
  461. return 0;
  462. }
  463. int
  464. gx_cpath_from_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox)
  465. {
  466. int code = gx_path_new(&pcpath->path);
  467. if (code < 0)
  468. return code;
  469. return cpath_set_rectangle(pcpath, pbox);
  470. }
  471. int
  472. gx_cpath_reset(gx_clip_path * pcpath)
  473. {
  474. gs_fixed_rect null_rect;
  475. null_rect.p.x = null_rect.p.y = null_rect.q.x = null_rect.q.y = 0;
  476. return gx_cpath_from_rectangle(pcpath, &null_rect);
  477. }
  478. /* If a clipping path is a rectangle, return the rectangle. */
  479. const gs_fixed_rect *
  480. cpath_is_rectangle(const gx_clip_path * pcpath)
  481. {
  482. if (pcpath->path_valid)
  483. return NULL;
  484. if (pcpath->inner_box.p.x != pcpath->path.bbox.p.x ||
  485. pcpath->inner_box.p.y != pcpath->path.bbox.p.y ||
  486. pcpath->inner_box.q.x != pcpath->path.bbox.q.x ||
  487. pcpath->inner_box.q.y != pcpath->path.bbox.q.y)
  488. return NULL;
  489. return &pcpath->inner_box;
  490. }
  491. /* Intersect a new clipping path with an old one. */
  492. /* Flatten the new path first (in a copy) if necessary. */
  493. int
  494. gx_cpath_clip(gs_state *pgs, gx_clip_path *pcpath,
  495. /*const*/ gx_path *ppath_orig, int rule)
  496. {
  497. return gx_cpath_intersect(pcpath, ppath_orig, rule,
  498. (gs_imager_state *)pgs);
  499. }
  500. int
  501. gx_cpath_intersect(gx_clip_path *pcpath, /*const*/ gx_path *ppath_orig,
  502. int rule, gs_imager_state *pis)
  503. {
  504. gx_path fpath;
  505. /*const*/ gx_path *ppath = ppath_orig;
  506. gs_fixed_rect old_box, new_box;
  507. int code;
  508. /* Flatten the path if necessary. */
  509. if (gx_path_has_curves_inline(ppath)) {
  510. gx_path_init_local(&fpath, pis->memory);
  511. code = gx_path_add_flattened_accurate(ppath, &fpath,
  512. gs_currentflat_inline(pis),
  513. pis->accurate_curves);
  514. if (code < 0)
  515. return code;
  516. ppath = &fpath;
  517. }
  518. if (gx_cpath_inner_box(pcpath, &old_box) &&
  519. ((code = gx_path_is_rectangle(ppath, &new_box)) ||
  520. gx_path_is_void(ppath))
  521. ) {
  522. int changed = 0;
  523. if (!code) {
  524. /* The new path is void. */
  525. if (gx_path_current_point(ppath, &new_box.p) < 0) {
  526. /* Use the user space origin (arbitrarily). */
  527. new_box.p.x = float2fixed(pis->ctm.tx);
  528. new_box.p.y = float2fixed(pis->ctm.ty);
  529. }
  530. new_box.q = new_box.p;
  531. changed = 1;
  532. } else {
  533. /* Intersect the two rectangles if necessary. */
  534. if (old_box.p.x > new_box.p.x)
  535. new_box.p.x = old_box.p.x, ++changed;
  536. if (old_box.p.y > new_box.p.y)
  537. new_box.p.y = old_box.p.y, ++changed;
  538. if (old_box.q.x < new_box.q.x)
  539. new_box.q.x = old_box.q.x, ++changed;
  540. if (old_box.q.y < new_box.q.y)
  541. new_box.q.y = old_box.q.y, ++changed;
  542. /* Check for a degenerate rectangle. */
  543. if (new_box.q.x < new_box.p.x || new_box.q.y < new_box.p.y)
  544. new_box.p = new_box.q, changed = 1;
  545. }
  546. if (changed == 4) {
  547. /* The new box/path is the same as the old. */
  548. return 0;
  549. }
  550. /* Release the existing path. */
  551. rc_decrement(pcpath->path_list, "gx_cpath_intersect");
  552. pcpath->path_list = NULL;
  553. gx_path_new(&pcpath->path);
  554. ppath->bbox = new_box;
  555. cpath_set_rectangle(pcpath, &new_box);
  556. if (changed == 0) {
  557. /* The path is valid; otherwise, defer constructing it. */
  558. gx_path_assign_preserve(&pcpath->path, ppath);
  559. pcpath->path_valid = true;
  560. }
  561. } else {
  562. /* New clip path is nontrivial. Intersect the slow way. */
  563. gx_cpath_path_list *next = pcpath->path_list;
  564. bool path_valid =
  565. gx_cpath_inner_box(pcpath, &old_box) &&
  566. gx_path_bbox(ppath, &new_box) >= 0 &&
  567. gx_cpath_includes_rectangle(pcpath,
  568. new_box.p.x, new_box.p.y,
  569. new_box.q.x, new_box.q.y);
  570. if (!path_valid && next == NULL) {
  571. code = gx_cpath_path_list_new(pcpath->path.memory, pcpath, pcpath->rule,
  572. &pcpath->path, NULL, &next);
  573. if (code < 0)
  574. goto ex;
  575. }
  576. code = gx_cpath_intersect_path_slow(pcpath, ppath, rule, pis);
  577. if (code < 0)
  578. goto ex;
  579. if (path_valid) {
  580. gx_path_assign_preserve(&pcpath->path, ppath_orig);
  581. pcpath->path_valid = true;
  582. } else {
  583. code = gx_cpath_path_list_new(pcpath->path.memory, NULL, rule,
  584. ppath_orig, next, &pcpath->path_list);
  585. }
  586. }
  587. ex:
  588. if (ppath != ppath_orig)
  589. gx_path_free(ppath, "gx_cpath_clip");
  590. return code;
  591. }
  592. /* Scale a clipping path by a power of 2. */
  593. int
  594. gx_cpath_scale_exp2_shared(gx_clip_path * pcpath, int log2_scale_x,
  595. int log2_scale_y, bool list_shared,
  596. bool segments_shared)
  597. {
  598. int code =
  599. (pcpath->path_valid ?
  600. gx_path_scale_exp2_shared(&pcpath->path, log2_scale_x, log2_scale_y,
  601. segments_shared) :
  602. 0);
  603. gx_clip_list *list = gx_cpath_list_private(pcpath);
  604. gx_clip_rect *pr;
  605. if (code < 0)
  606. return code;
  607. /* Scale the fixed entries. */
  608. gx_rect_scale_exp2(&pcpath->inner_box, log2_scale_x, log2_scale_y);
  609. gx_rect_scale_exp2(&pcpath->outer_box, log2_scale_x, log2_scale_y);
  610. if (!list_shared) {
  611. /* Scale the clipping list. */
  612. pr = list->head;
  613. if (pr == 0)
  614. pr = &list->single;
  615. for (; pr != 0; pr = pr->next)
  616. if (pr != list->head && pr != list->tail) {
  617. #define SCALE_V(v, s)\
  618. if ( pr->v != min_int && pr->v != max_int )\
  619. pr->v = (s >= 0 ? pr->v << s : pr->v >> -s)
  620. SCALE_V(xmin, log2_scale_x);
  621. SCALE_V(xmax, log2_scale_x);
  622. SCALE_V(ymin, log2_scale_y);
  623. SCALE_V(ymax, log2_scale_y);
  624. #undef SCALE_V
  625. }
  626. }
  627. pcpath->id = gs_next_ids(pcpath->path.memory, 1); /* path changed => change id */
  628. return 0;
  629. }
  630. /* ------ Clipping list routines ------ */
  631. /* Initialize a clip list. */
  632. void
  633. gx_clip_list_init(gx_clip_list * clp)
  634. {
  635. *clp = clip_list_empty;
  636. }
  637. /* Initialize a clip list to a rectangle. */
  638. /* The supplied rectangle may not be oriented correctly, */
  639. /* but it will be oriented correctly upon return. */
  640. private void
  641. gx_clip_list_from_rectangle(register gx_clip_list * clp,
  642. register gs_fixed_rect * rp)
  643. {
  644. gx_clip_list_init(clp);
  645. if (rp->p.x > rp->q.x) {
  646. fixed t = rp->p.x;
  647. rp->p.x = rp->q.x;
  648. rp->q.x = t;
  649. }
  650. if (rp->p.y > rp->q.y) {
  651. fixed t = rp->p.y;
  652. rp->p.y = rp->q.y;
  653. rp->q.y = t;
  654. }
  655. clp->single.xmin = clp->xmin = fixed2int_var(rp->p.x);
  656. clp->single.ymin = fixed2int_var(rp->p.y);
  657. /* Handle degenerate rectangles specially. */
  658. clp->single.xmax = clp->xmax =
  659. (rp->q.x == rp->p.x ? clp->single.xmin :
  660. fixed2int_var_ceiling(rp->q.x));
  661. clp->single.ymax =
  662. (rp->q.y == rp->p.y ? clp->single.ymin :
  663. fixed2int_var_ceiling(rp->q.y));
  664. clp->count = 1;
  665. }
  666. /* Start enumerating a clipping path. */
  667. int
  668. gx_cpath_enum_init(gs_cpath_enum * penum, gx_clip_path * pcpath)
  669. {
  670. if ((penum->using_path = pcpath->path_valid)) {
  671. gx_path_enum_init(&penum->path_enum, &pcpath->path);
  672. penum->rp = penum->visit = 0;
  673. } else {
  674. gx_path empty_path;
  675. gx_clip_list *clp = gx_cpath_list_private(pcpath);
  676. gx_clip_rect *head = (clp->count <= 1 ? &clp->single : clp->head);
  677. gx_clip_rect *rp;
  678. /* Initialize the pointers in the path_enum properly. */
  679. gx_path_init_local(&empty_path, pcpath->path.memory);
  680. gx_path_enum_init(&penum->path_enum, &empty_path);
  681. penum->visit = head;
  682. for (rp = head; rp != 0; rp = rp->next)
  683. rp->to_visit =
  684. (rp->xmin < rp->xmax && rp->ymin < rp->ymax ?
  685. visit_left | visit_right : 0);
  686. penum->rp = 0; /* scan will initialize */
  687. penum->any_rectangles = false;
  688. penum->state = cpe_scan;
  689. penum->have_line = false;
  690. }
  691. return 0;
  692. }
  693. /* Enumerate the next segment of a clipping path. */
  694. /* In general, this produces a path made up of zillions of tiny lines. */
  695. int
  696. gx_cpath_enum_next(gs_cpath_enum * penum, gs_fixed_point pts[3])
  697. {
  698. if (penum->using_path)
  699. return gx_path_enum_next(&penum->path_enum, pts);
  700. #define set_pt(xi, yi)\
  701. (pts[0].x = int2fixed(xi), pts[0].y = int2fixed(yi))
  702. #define set_line(xi, yi)\
  703. (penum->line_end.x = (xi), penum->line_end.y = (yi), penum->have_line = true)
  704. if (penum->have_line) {
  705. set_pt(penum->line_end.x, penum->line_end.y);
  706. penum->have_line = false;
  707. return gs_pe_lineto;
  708. } {
  709. gx_clip_rect *visit = penum->visit;
  710. gx_clip_rect *rp = penum->rp;
  711. cpe_visit_t first_visit = penum->first_visit;
  712. cpe_state_t state = penum->state;
  713. gx_clip_rect *look;
  714. int code;
  715. switch (state) {
  716. case cpe_scan:
  717. /* Look for the start of an edge to trace. */
  718. for (; visit != 0; visit = visit->next) {
  719. if (visit->to_visit & visit_left) {
  720. set_pt(visit->xmin, visit->ymin);
  721. first_visit = visit_left;
  722. state = cpe_left;
  723. } else if (visit->to_visit & visit_right) {
  724. set_pt(visit->xmax, visit->ymax);
  725. first_visit = visit_right;
  726. state = cpe_right;
  727. } else
  728. continue;
  729. rp = visit;
  730. code = gs_pe_moveto;
  731. penum->any_rectangles = true;
  732. goto out;
  733. }
  734. /* We've enumerated all the edges. */
  735. state = cpe_done;
  736. if (!penum->any_rectangles) {
  737. /* We didn't have any rectangles. */
  738. set_pt(fixed_0, fixed_0);
  739. code = gs_pe_moveto;
  740. break;
  741. }
  742. /* falls through */
  743. case cpe_done:
  744. /* All done. */
  745. code = 0;
  746. break;
  747. /* We can't use the BEGIN ... END hack here: we need to be able to break. */
  748. #define return_line(px, py)\
  749. set_pt(px, py); code = gs_pe_lineto; break
  750. case cpe_left:
  751. left: /* Trace upward along a left edge. */
  752. /* We're at the lower left corner of rp. */
  753. rp->to_visit &= ~visit_left;
  754. /* Look for an adjacent rectangle above rp. */
  755. for (look = rp;
  756. (look = look->next) != 0 &&
  757. (look->ymin == rp->ymin ||
  758. (look->ymin == rp->ymax && look->xmax <= rp->xmin));
  759. );
  760. /* Now we know look->ymin >= rp->ymax. */
  761. if (look == 0 || look->ymin > rp->ymax ||
  762. look->xmin >= rp->xmax
  763. ) { /* No adjacent rectangle, switch directions. */
  764. state =
  765. (rp == visit && first_visit == visit_right ? cpe_close :
  766. (set_line(rp->xmax, rp->ymax), cpe_right));
  767. return_line(rp->xmin, rp->ymax);
  768. }
  769. /* We found an adjacent rectangle. */
  770. /* See if it also adjoins a rectangle to the left of rp. */
  771. {
  772. gx_clip_rect *prev = rp->prev;
  773. gx_clip_rect *cur = rp;
  774. if (prev != 0 && prev->ymax == rp->ymax &&
  775. look->xmin < prev->xmax
  776. ) { /* There's an adjoining rectangle as well. */
  777. /* Switch directions. */
  778. rp = prev;
  779. state =
  780. (rp == visit && first_visit == visit_right ? cpe_close :
  781. (set_line(prev->xmax, prev->ymax), cpe_right));
  782. return_line(cur->xmin, cur->ymax);
  783. }
  784. rp = look;
  785. if (rp == visit && first_visit == visit_left)
  786. state = cpe_close;
  787. else if (rp->xmin == cur->xmin)
  788. goto left;
  789. else
  790. set_line(rp->xmin, rp->ymin);
  791. return_line(cur->xmin, cur->ymax);
  792. }
  793. case cpe_right:
  794. right: /* Trace downward along a right edge. */
  795. /* We're at the upper right corner of rp. */
  796. rp->to_visit &= ~visit_right;
  797. /* Look for an adjacent rectangle below rp. */
  798. for (look = rp;
  799. (look = look->prev) != 0 &&
  800. (look->ymax == rp->ymax ||
  801. (look->ymax == rp->ymin && look->xmin >= rp->xmax));
  802. );
  803. /* Now we know look->ymax <= rp->ymin. */
  804. if (look == 0 || look->ymax < rp->ymin ||
  805. look->xmax <= rp->xmin
  806. ) { /* No adjacent rectangle, switch directions. */
  807. state =
  808. (rp == visit && first_visit == visit_left ? cpe_close :
  809. (set_line(rp->xmin, rp->ymin), cpe_left));
  810. return_line(rp->xmax, rp->ymin);
  811. }
  812. /* We found an adjacent rectangle. */
  813. /* See if it also adjoins a rectangle to the right of rp. */
  814. {
  815. gx_clip_rect *next = rp->next;
  816. gx_clip_rect *cur = rp;
  817. if (next != 0 && next->ymin == rp->ymin &&
  818. look->xmax > next->xmin
  819. ) { /* There's an adjoining rectangle as well. */
  820. /* Switch directions. */
  821. rp = next;
  822. state =
  823. (rp == visit && first_visit == visit_left ? cpe_close :
  824. (set_line(next->xmin, next->ymin), cpe_left));
  825. return_line(cur->xmax, cur->ymin);
  826. }
  827. rp = look;
  828. if (rp == visit && first_visit == visit_right)
  829. state = cpe_close;
  830. else if (rp->xmax == cur->xmax)
  831. goto right;
  832. else
  833. set_line(rp->xmax, rp->ymax);
  834. return_line(cur->xmax, cur->ymin);
  835. }
  836. #undef return_line
  837. case cpe_close:
  838. /* We've gone all the way around an edge. */
  839. code = gs_pe_closepath;
  840. state = cpe_scan;
  841. break;
  842. default:
  843. return_error(gs_error_unknownerror);
  844. }
  845. out: /* Store the state before exiting. */
  846. penum->visit = visit;
  847. penum->rp = rp;
  848. penum->first_visit = first_visit;
  849. penum->state = state;
  850. return code;
  851. }
  852. #undef set_pt
  853. #undef set_line
  854. }
  855. segment_notes
  856. gx_cpath_enum_notes(const gs_cpath_enum * penum)
  857. {
  858. return sn_none;
  859. }
  860. /* Free a clip list. */
  861. void
  862. gx_clip_list_free(gx_clip_list * clp, gs_memory_t * mem)
  863. {
  864. gx_clip_rect *rp = clp->tail;
  865. while (rp != 0) {
  866. gx_clip_rect *prev = rp->prev;
  867. gs_free_object(mem, rp, "gx_clip_list_free");
  868. rp = prev;
  869. }
  870. gx_clip_list_init(clp);
  871. }
  872. /* ------ Debugging printout ------ */
  873. #ifdef DEBUG
  874. /* Print a clipping list. */
  875. private void
  876. gx_clip_list_print(const gx_clip_list *list)
  877. {
  878. const gx_clip_rect *pr;
  879. dlprintf3(" list count=%d xmin=%d xmax=%d\n",
  880. list->count, list->xmin, list->xmax);
  881. switch (list->count) {
  882. case 0:
  883. pr = 0;
  884. break;
  885. case 1:
  886. pr = &list->single;
  887. break;
  888. default:
  889. pr = list->head;
  890. }
  891. for (; pr != 0; pr = pr->next)
  892. dlprintf4(" rect: (%d,%d),(%d,%d)\n",
  893. pr->xmin, pr->ymin, pr->xmax, pr->ymax);
  894. }
  895. /* Print a clipping path */
  896. void
  897. gx_cpath_print(const gx_clip_path * pcpath)
  898. {
  899. if (pcpath->path_valid)
  900. gx_path_print(&pcpath->path);
  901. else
  902. dlputs(" (path not valid)\n");
  903. dlprintf4(" inner_box=(%g,%g),(%g,%g)\n",
  904. fixed2float(pcpath->inner_box.p.x),
  905. fixed2float(pcpath->inner_box.p.y),
  906. fixed2float(pcpath->inner_box.q.x),
  907. fixed2float(pcpath->inner_box.q.y));
  908. dlprintf4(" outer_box=(%g,%g),(%g,%g)",
  909. fixed2float(pcpath->outer_box.p.x),
  910. fixed2float(pcpath->outer_box.p.y),
  911. fixed2float(pcpath->outer_box.q.x),
  912. fixed2float(pcpath->outer_box.q.y));
  913. dprintf2(" rule=%d list.refct=%ld\n",
  914. pcpath->rule, pcpath->rect_list->rc.ref_count);
  915. gx_clip_list_print(gx_cpath_list(pcpath));
  916. }
  917. #endif /* DEBUG */