gxacpath.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* Copyright (C) 1993, 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: gxacpath.c,v 1.10 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Accumulator for clipping paths */
  15. #include "gx.h"
  16. #include "gserrors.h"
  17. #include "gsrop.h"
  18. #include "gsstruct.h"
  19. #include "gsutil.h"
  20. #include "gsdcolor.h"
  21. #include "gxdevice.h"
  22. #include "gxfixed.h"
  23. #include "gxistate.h"
  24. #include "gzpath.h"
  25. #include "gxpaint.h"
  26. #include "gzcpath.h"
  27. #include "gzacpath.h"
  28. /* Device procedures */
  29. private dev_proc_open_device(accum_open);
  30. private dev_proc_close_device(accum_close);
  31. private dev_proc_fill_rectangle(accum_fill_rectangle);
  32. /* The device descriptor */
  33. /* Many of these procedures won't be called; they are set to NULL. */
  34. private const gx_device_cpath_accum gs_cpath_accum_device =
  35. {std_device_std_body(gx_device_cpath_accum, 0, "clip list accumulator",
  36. 0, 0, 1, 1),
  37. {accum_open,
  38. NULL,
  39. NULL,
  40. NULL,
  41. accum_close,
  42. NULL,
  43. NULL,
  44. accum_fill_rectangle,
  45. NULL,
  46. NULL,
  47. NULL,
  48. NULL,
  49. NULL,
  50. NULL,
  51. NULL,
  52. NULL,
  53. NULL,
  54. NULL,
  55. NULL,
  56. NULL,
  57. NULL,
  58. NULL,
  59. NULL,
  60. NULL,
  61. gx_default_fill_path,
  62. gx_default_stroke_path,
  63. NULL,
  64. gx_default_fill_trapezoid,
  65. gx_default_fill_parallelogram,
  66. gx_default_fill_triangle,
  67. gx_default_draw_thin_line,
  68. gx_default_begin_image,
  69. gx_default_image_data,
  70. gx_default_end_image,
  71. NULL,
  72. NULL,
  73. gx_get_largest_clipping_box,
  74. gx_default_begin_typed_image,
  75. NULL,
  76. NULL,
  77. NULL,
  78. NULL,
  79. gx_default_text_begin,
  80. gx_default_finish_copydevice,
  81. NULL,
  82. NULL,
  83. NULL,
  84. NULL,
  85. NULL,
  86. NULL,
  87. NULL,
  88. NULL,
  89. NULL
  90. }
  91. };
  92. /* Start accumulating a clipping path. */
  93. void
  94. gx_cpath_accum_begin(gx_device_cpath_accum * padev, gs_memory_t * mem)
  95. {
  96. gx_device_init((gx_device *) padev,
  97. (const gx_device *) & gs_cpath_accum_device,
  98. NULL /* allocated on stack */ , true);
  99. padev->list_memory = mem;
  100. (*dev_proc(padev, open_device)) ((gx_device *) padev);
  101. }
  102. void
  103. gx_cpath_accum_set_cbox(gx_device_cpath_accum * padev,
  104. const gs_fixed_rect * pbox)
  105. {
  106. padev->clip_box.p.x = fixed2int_var(pbox->p.x);
  107. padev->clip_box.p.y = fixed2int_var(pbox->p.y);
  108. padev->clip_box.q.x = fixed2int_var_ceiling(pbox->q.x);
  109. padev->clip_box.q.y = fixed2int_var_ceiling(pbox->q.y);
  110. }
  111. /* Finish accumulating a clipping path. */
  112. int
  113. gx_cpath_accum_end(const gx_device_cpath_accum * padev, gx_clip_path * pcpath)
  114. {
  115. int code = (*dev_proc(padev, close_device)) ((gx_device *) padev);
  116. /* Make an entire clipping path so we can use cpath_assign. */
  117. gx_clip_path apath;
  118. if (code < 0)
  119. return code;
  120. gx_cpath_init_local(&apath, padev->list_memory);
  121. apath.rect_list->list = padev->list;
  122. if (padev->list.count == 0)
  123. apath.path.bbox.p.x = apath.path.bbox.p.y =
  124. apath.path.bbox.q.x = apath.path.bbox.q.y = 0;
  125. else {
  126. apath.path.bbox.p.x = int2fixed(padev->bbox.p.x);
  127. apath.path.bbox.p.y = int2fixed(padev->bbox.p.y);
  128. apath.path.bbox.q.x = int2fixed(padev->bbox.q.x);
  129. apath.path.bbox.q.y = int2fixed(padev->bbox.q.y);
  130. }
  131. /* indicate that the bbox is accurate */
  132. apath.path.bbox_accurate = 1;
  133. /* Note that the result of the intersection might be */
  134. /* a single rectangle. This will cause clip_path_is_rect.. */
  135. /* to return true. This, in turn, requires that */
  136. /* we set apath.inner_box correctly. */
  137. if (clip_list_is_rectangle(&padev->list))
  138. apath.inner_box = apath.path.bbox;
  139. else {
  140. /* The quick check must fail. */
  141. apath.inner_box.p.x = apath.inner_box.p.y = 0;
  142. apath.inner_box.q.x = apath.inner_box.q.y = 0;
  143. }
  144. gx_cpath_set_outer_box(&apath);
  145. apath.path_valid = false;
  146. apath.id = gs_next_ids(padev->list_memory, 1); /* path changed => change id */
  147. gx_cpath_assign_free(pcpath, &apath);
  148. return 0;
  149. }
  150. /* Discard an accumulator in case of error. */
  151. void
  152. gx_cpath_accum_discard(gx_device_cpath_accum * padev)
  153. {
  154. gx_clip_list_free(&padev->list, padev->list_memory);
  155. }
  156. /* Intersect two clipping paths using an accumulator. */
  157. int
  158. gx_cpath_intersect_path_slow(gx_clip_path * pcpath, gx_path * ppath,
  159. int rule, gs_imager_state *pis)
  160. {
  161. gs_logical_operation_t save_lop = gs_current_logical_op_inline(pis);
  162. gx_device_cpath_accum adev;
  163. gx_device_color devc;
  164. gx_fill_params params;
  165. int code;
  166. gx_cpath_accum_begin(&adev, pcpath->path.memory);
  167. set_nonclient_dev_color(&devc, 0); /* arbitrary, but not transparent */
  168. gs_set_logical_op_inline(pis, lop_default);
  169. params.rule = rule;
  170. params.adjust.x = params.adjust.y = fixed_half;
  171. params.flatness = gs_currentflat_inline(pis);
  172. params.fill_zero_width = true;
  173. code = gx_fill_path_only(ppath, (gx_device *)&adev, pis,
  174. &params, &devc, pcpath);
  175. if (code < 0 || (code = gx_cpath_accum_end(&adev, pcpath)) < 0)
  176. gx_cpath_accum_discard(&adev);
  177. gs_set_logical_op_inline(pis, save_lop);
  178. return code;
  179. }
  180. /* ------ Device implementation ------ */
  181. #ifdef DEBUG
  182. /* Validate a clipping path after accumulation. */
  183. private bool
  184. clip_list_validate(const gx_clip_list * clp)
  185. {
  186. if (clp->count <= 1)
  187. return (clp->head == 0 && clp->tail == 0 &&
  188. clp->single.next == 0 && clp->single.prev == 0);
  189. else {
  190. const gx_clip_rect *prev = clp->head;
  191. const gx_clip_rect *ptr;
  192. bool ok = true;
  193. while ((ptr = prev->next) != 0) {
  194. if (ptr->ymin > ptr->ymax || ptr->xmin > ptr->xmax ||
  195. !(ptr->ymin >= prev->ymax ||
  196. (ptr->ymin == prev->ymin &&
  197. ptr->ymax == prev->ymax &&
  198. ptr->xmin >= prev->xmax)) ||
  199. ptr->prev != prev
  200. ) {
  201. clip_rect_print('q', "WRONG:", ptr);
  202. ok = false;
  203. }
  204. prev = ptr;
  205. }
  206. return ok && prev == clp->tail;
  207. }
  208. }
  209. #endif /* DEBUG */
  210. /* Initialize the accumulation device. */
  211. private int
  212. accum_open(register gx_device * dev)
  213. {
  214. gx_device_cpath_accum * const adev = (gx_device_cpath_accum *)dev;
  215. gx_clip_list_init(&adev->list);
  216. adev->bbox.p.x = adev->bbox.p.y = max_int;
  217. adev->bbox.q.x = adev->bbox.q.y = min_int;
  218. adev->clip_box.p.x = adev->clip_box.p.y = min_int;
  219. adev->clip_box.q.x = adev->clip_box.q.y = max_int;
  220. return 0;
  221. }
  222. /* Close the accumulation device. */
  223. private int
  224. accum_close(gx_device * dev)
  225. {
  226. gx_device_cpath_accum * const adev = (gx_device_cpath_accum *)dev;
  227. adev->list.xmin = adev->bbox.p.x;
  228. adev->list.xmax = adev->bbox.q.x;
  229. #ifdef DEBUG
  230. if (gs_debug_c('q')) {
  231. gx_clip_rect *rp =
  232. (adev->list.count <= 1 ? &adev->list.single : adev->list.head);
  233. dlprintf6("[q]list at 0x%lx, count=%d, head=0x%lx, tail=0x%lx, xrange=(%d,%d):\n",
  234. (ulong) & adev->list, adev->list.count,
  235. (ulong) adev->list.head, (ulong) adev->list.tail,
  236. adev->list.xmin, adev->list.xmax);
  237. while (rp != 0) {
  238. clip_rect_print('q', " ", rp);
  239. rp = rp->next;
  240. }
  241. }
  242. if (!clip_list_validate(&adev->list)) {
  243. lprintf1("[q]Bad clip list 0x%lx!\n", (ulong) & adev->list);
  244. return_error(gs_error_Fatal);
  245. }
  246. #endif
  247. return 0;
  248. }
  249. /* Accumulate one rectangle. */
  250. /* Allocate a rectangle to be added to the list. */
  251. static const gx_clip_rect clip_head_rect = {
  252. 0, 0, min_int, min_int, min_int, min_int
  253. };
  254. static const gx_clip_rect clip_tail_rect = {
  255. 0, 0, max_int, max_int, max_int, max_int
  256. };
  257. private gx_clip_rect *
  258. accum_alloc_rect(gx_device_cpath_accum * adev)
  259. {
  260. gs_memory_t *mem = adev->list_memory;
  261. gx_clip_rect *ar = gs_alloc_struct(mem, gx_clip_rect, &st_clip_rect,
  262. "accum_alloc_rect");
  263. if (ar == 0)
  264. return 0;
  265. if (adev->list.count == 2) {
  266. /* We're switching from a single rectangle to a list. */
  267. /* Allocate the head and tail entries. */
  268. gx_clip_rect *head = ar;
  269. gx_clip_rect *tail =
  270. gs_alloc_struct(mem, gx_clip_rect, &st_clip_rect,
  271. "accum_alloc_rect(tail)");
  272. gx_clip_rect *single =
  273. gs_alloc_struct(mem, gx_clip_rect, &st_clip_rect,
  274. "accum_alloc_rect(single)");
  275. ar = gs_alloc_struct(mem, gx_clip_rect, &st_clip_rect,
  276. "accum_alloc_rect(head)");
  277. if (tail == 0 || single == 0 || ar == 0) {
  278. gs_free_object(mem, ar, "accum_alloc_rect");
  279. gs_free_object(mem, single, "accum_alloc_rect(single)");
  280. gs_free_object(mem, tail, "accum_alloc_rect(tail)");
  281. gs_free_object(mem, head, "accum_alloc_rect(head)");
  282. return 0;
  283. }
  284. *head = clip_head_rect;
  285. head->next = single;
  286. *single = adev->list.single;
  287. single->prev = head;
  288. single->next = tail;
  289. *tail = clip_tail_rect;
  290. tail->prev = single;
  291. adev->list.head = head;
  292. adev->list.tail = tail;
  293. }
  294. return ar;
  295. }
  296. #define ACCUM_ALLOC(s, ar, px, py, qx, qy)\
  297. if (++(adev->list.count) == 1)\
  298. ar = &adev->list.single;\
  299. else if ((ar = accum_alloc_rect(adev)) == 0)\
  300. return_error(gs_error_VMerror);\
  301. ACCUM_SET(s, ar, px, py, qx, qy)
  302. #define ACCUM_SET(s, ar, px, py, qx, qy)\
  303. (ar)->xmin = px, (ar)->ymin = py, (ar)->xmax = qx, (ar)->ymax = qy;\
  304. clip_rect_print('Q', s, ar)
  305. /* Link or unlink a rectangle in the list. */
  306. #define ACCUM_ADD_LAST(ar)\
  307. ACCUM_ADD_BEFORE(ar, adev->list.tail)
  308. #define ACCUM_ADD_AFTER(ar, rprev)\
  309. ar->prev = (rprev), (ar->next = (rprev)->next)->prev = ar,\
  310. (rprev)->next = ar
  311. #define ACCUM_ADD_BEFORE(ar, rnext)\
  312. (ar->prev = (rnext)->prev)->next = ar, ar->next = (rnext),\
  313. (rnext)->prev = ar
  314. #define ACCUM_REMOVE(ar)\
  315. ar->next->prev = ar->prev, ar->prev->next = ar->next
  316. /* Free a rectangle that was removed from the list. */
  317. #define ACCUM_FREE(s, ar)\
  318. if (--(adev->list.count)) {\
  319. clip_rect_print('Q', s, ar);\
  320. gs_free_object(adev->list_memory, ar, "accum_rect");\
  321. }
  322. /*
  323. * Add a rectangle to the list. It would be wonderful if rectangles
  324. * were always disjoint and always presented in the correct order,
  325. * but they aren't: the fill loop works by trapezoids, not by scan lines,
  326. * and may produce slightly overlapping rectangles because of "fattening".
  327. * All we can count on is that they are approximately disjoint and
  328. * approximately in order.
  329. *
  330. * Because of the way the fill loop handles a path that is just a single
  331. * rectangle, we take special care to merge Y-adjacent rectangles when
  332. * this is possible.
  333. */
  334. private int
  335. accum_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  336. gx_color_index color)
  337. {
  338. gx_device_cpath_accum * const adev = (gx_device_cpath_accum *)dev;
  339. int xe = x + w, ye = y + h;
  340. gx_clip_rect *nr;
  341. gx_clip_rect *ar;
  342. register gx_clip_rect *rptr;
  343. int ymin, ymax;
  344. /* Clip the rectangle being added. */
  345. if (y < adev->clip_box.p.y)
  346. y = adev->clip_box.p.y;
  347. if (ye > adev->clip_box.q.y)
  348. ye = adev->clip_box.q.y;
  349. if (y >= ye)
  350. return 0;
  351. if (x < adev->clip_box.p.x)
  352. x = adev->clip_box.p.x;
  353. if (xe > adev->clip_box.q.x)
  354. xe = adev->clip_box.q.x;
  355. if (x >= xe)
  356. return 0;
  357. /* Update the bounding box. */
  358. if (x < adev->bbox.p.x)
  359. adev->bbox.p.x = x;
  360. if (y < adev->bbox.p.y)
  361. adev->bbox.p.y = y;
  362. if (xe > adev->bbox.q.x)
  363. adev->bbox.q.x = xe;
  364. if (ye > adev->bbox.q.y)
  365. adev->bbox.q.y = ye;
  366. top:
  367. if (adev->list.count == 0) { /* very first rectangle */
  368. adev->list.count = 1;
  369. ACCUM_SET("single", &adev->list.single, x, y, xe, ye);
  370. return 0;
  371. }
  372. if (adev->list.count == 1) { /* check for Y merging */
  373. rptr = &adev->list.single;
  374. if (x == rptr->xmin && xe == rptr->xmax &&
  375. y <= rptr->ymax && ye >= rptr->ymin
  376. ) {
  377. if (y < rptr->ymin)
  378. rptr->ymin = y;
  379. if (ye > rptr->ymax)
  380. rptr->ymax = ye;
  381. return 0;
  382. }
  383. }
  384. else
  385. rptr = adev->list.tail->prev;
  386. if (y >= rptr->ymax) {
  387. if (y == rptr->ymax && x == rptr->xmin && xe == rptr->xmax &&
  388. (rptr->prev == 0 || y != rptr->prev->ymax)
  389. ) {
  390. rptr->ymax = ye;
  391. return 0;
  392. }
  393. ACCUM_ALLOC("app.y", nr, x, y, xe, ye);
  394. ACCUM_ADD_LAST(nr);
  395. return 0;
  396. } else if (y == rptr->ymin && ye == rptr->ymax && x >= rptr->xmin) {
  397. if (x <= rptr->xmax) {
  398. if (xe > rptr->xmax)
  399. rptr->xmax = xe;
  400. return 0;
  401. }
  402. ACCUM_ALLOC("app.x", nr, x, y, xe, ye);
  403. ACCUM_ADD_LAST(nr);
  404. return 0;
  405. }
  406. ACCUM_ALLOC("accum", nr, x, y, xe, ye);
  407. rptr = adev->list.tail->prev;
  408. /* Work backwards till we find the insertion point. */
  409. while (ye <= rptr->ymin)
  410. rptr = rptr->prev;
  411. ymin = rptr->ymin;
  412. ymax = rptr->ymax;
  413. if (ye > ymax) {
  414. if (y >= ymax) { /* Insert between two bands. */
  415. ACCUM_ADD_AFTER(nr, rptr);
  416. return 0;
  417. }
  418. /* Split off the top part of the new rectangle. */
  419. ACCUM_ALLOC("a.top", ar, x, ymax, xe, ye);
  420. ACCUM_ADD_AFTER(ar, rptr);
  421. ye = nr->ymax = ymax;
  422. clip_rect_print('Q', " ymax", nr);
  423. }
  424. /* Here we know ymin < ye <= ymax; */
  425. /* rptr points to the last node with this value of ymin/ymax. */
  426. /* If necessary, split off the part of the existing band */
  427. /* that is above the new band. */
  428. if (ye < ymax) {
  429. gx_clip_rect *rsplit = rptr;
  430. while (rsplit->ymax == ymax) {
  431. ACCUM_ALLOC("s.top", ar, rsplit->xmin, ye, rsplit->xmax, ymax);
  432. ACCUM_ADD_AFTER(ar, rptr);
  433. rsplit->ymax = ye;
  434. rsplit = rsplit->prev;
  435. }
  436. ymax = ye;
  437. }
  438. /* Now ye = ymax. If necessary, split off the part of the */
  439. /* existing band that is below the new band. */
  440. if (y > ymin) {
  441. gx_clip_rect *rbot = rptr, *rsplit;
  442. while (rbot->prev->ymin == ymin)
  443. rbot = rbot->prev;
  444. for (rsplit = rbot;;) {
  445. ACCUM_ALLOC("s.bot", ar, rsplit->xmin, ymin, rsplit->xmax, y);
  446. ACCUM_ADD_BEFORE(ar, rbot);
  447. rsplit->ymin = y;
  448. if (rsplit == rptr)
  449. break;
  450. rsplit = rsplit->next;
  451. }
  452. ymin = y;
  453. }
  454. /* Now y <= ymin as well. (y < ymin is possible.) */
  455. nr->ymin = ymin;
  456. /* Search for the X insertion point. */
  457. for (; rptr->ymin == ymin; rptr = rptr->prev) {
  458. if (xe < rptr->xmin)
  459. continue; /* still too far to right */
  460. if (x > rptr->xmax)
  461. break; /* disjoint */
  462. /* The new rectangle overlaps an existing one. Merge them. */
  463. if (xe > rptr->xmax) {
  464. rptr->xmax = nr->xmax; /* might be > xe if */
  465. /* we already did a merge */
  466. clip_rect_print('Q', "widen", rptr);
  467. }
  468. ACCUM_FREE("free", nr);
  469. if (x >= rptr->xmin)
  470. goto out;
  471. /* Might overlap other rectangles to the left. */
  472. rptr->xmin = x;
  473. nr = rptr;
  474. ACCUM_REMOVE(rptr);
  475. clip_rect_print('Q', "merge", nr);
  476. }
  477. ACCUM_ADD_AFTER(nr, rptr);
  478. out:
  479. /* Check whether there are only 0 or 1 rectangles left. */
  480. if (adev->list.count <= 1) {
  481. /* We're switching from a list to at most 1 rectangle. */
  482. /* Free the head and tail entries. */
  483. gs_memory_t *mem = adev->list_memory;
  484. gx_clip_rect *single = adev->list.head->next;
  485. if (single != adev->list.tail) {
  486. adev->list.single = *single;
  487. gs_free_object(mem, single, "accum_free_rect(single)");
  488. adev->list.single.next = adev->list.single.prev = 0;
  489. }
  490. gs_free_object(mem, adev->list.tail, "accum_free_rect(tail)");
  491. gs_free_object(mem, adev->list.head, "accum_free_rect(head)");
  492. adev->list.head = 0;
  493. adev->list.tail = 0;
  494. }
  495. /* Check whether there is still more of the new band to process. */
  496. if (y < ymin) {
  497. /* Continue with the bottom part of the new rectangle. */
  498. clip_rect_print('Q', " ymin", nr);
  499. ye = ymin;
  500. goto top;
  501. }
  502. return 0;
  503. }