home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxacpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  15.2 KB  |  524 lines

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