home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GXCPATH.C < prev    next >
C/C++ Source or Header  |  1992-08-09  |  22KB  |  721 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxcpath.c */
  21. /* Implementation of clipping paths */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxdevice.h"
  25. #include "gxfixed.h"
  26. #include "gzcolor.h"
  27. #include "gzpath.h"
  28. #include "gxcpath.h"
  29.  
  30. #define min_int (-1 << (sizeof(int) * 8 - 1))
  31. #define max_int (~min_int)
  32.  
  33. const uint gs_clip_path_sizeof = sizeof(gx_clip_path);
  34.  
  35. /* Imported procedures */
  36. gx_device *gs_currentdevice(P1(gs_state *));
  37. void gx_set_device_only(P2(gs_state *, gx_device *));
  38.  
  39. /* Forward references */
  40. private void clip_prepare(P1(gx_clip_list *));
  41.  
  42. private const gx_clip_list clip_list_empty =
  43. {  { 0, 0, min_int, min_int, min_int, min_int },
  44.    { 0, 0, min_int, max_int, 0, 0 },
  45.    { 0, 0, max_int, max_int, max_int, max_int },
  46.    0
  47. };
  48.  
  49. /* Debugging */
  50.  
  51. #ifdef DEBUG
  52. #  define clip_rect_print(str, ar)\
  53.     if ( gs_debug['q'] )\
  54.     dprintf6("[q]%s %lx: (%d,%d),(%d,%d)\n", str, (ulong)ar,\
  55.          (ar)->xmin, (ar)->ymin, (ar)->xmax, (ar)->ymax)
  56. #else
  57. #  define clip_rect_print(s, ar) 0
  58. #endif
  59.  
  60. #ifdef DEBUG
  61. /* Validate a clipping path that has gone through clip_prepare. */
  62. private void
  63. clip_list_validate(gx_clip_list *clp)
  64. {    gx_clip_rect *prev = &clp->first;
  65.     gx_clip_rect *ptr = prev;
  66.     int wrong = 0;
  67.     while ( ptr != 0 )
  68.       { if ( ptr->ymin > ptr->ymax || ptr->xmin > ptr->xmax ||
  69.         !(ptr->ymin >= prev->ymax ||
  70.           ptr->ymin == prev->ymin && ptr->ymax == prev->ymax &&
  71.           ptr->xmin >= prev->xmax)
  72.         )
  73.           { clip_rect_print("WRONG:", ptr);
  74.         wrong = 1;
  75.           }
  76.         prev = ptr, ptr = ptr->next;
  77.       }
  78. }
  79. #endif
  80.  
  81. /* ------ Clipping path accessing ------ */
  82.  
  83. /* Return the path of a clipping path. */
  84. int
  85. gx_cpath_path(gx_clip_path *pcpath, gx_path *ppath)
  86. {    if ( !pcpath->segments_valid )
  87.        {    int code = gx_clip_list_add_to_path(&pcpath->list, &pcpath->path);
  88.         if ( code < 0 ) return code;
  89.         pcpath->segments_valid = 1;
  90.        }
  91.     *ppath = pcpath->path;
  92.     return 0;
  93. }
  94.  
  95. /* Return the quick-check rectangle for a clipping path. */
  96. int
  97. gx_cpath_box_for_check(const gx_clip_path *pcpath, gs_fixed_rect *pbox)
  98. {    *pbox = pcpath->cbox;
  99.     return 0;
  100. }
  101.  
  102. /* Test if a clipping path includes a rectangle. */
  103. /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */
  104. int
  105. gx_cpath_includes_rectangle(register const gx_clip_path *pcpath,
  106.   fixed x0, fixed y0, fixed x1, fixed y1)
  107. {    return
  108.         (x0 <= x1 ?
  109.             (pcpath->cbox.p.x <= x0 && x1 <= pcpath->cbox.q.x) :
  110.             (pcpath->cbox.p.x <= x1 && x0 <= pcpath->cbox.q.x)) &&
  111.         (y0 <= y1 ?
  112.             (pcpath->cbox.p.y <= y0 && y1 <= pcpath->cbox.q.y) :
  113.             (pcpath->cbox.p.y <= y1 && y0 <= pcpath->cbox.q.y));
  114. }
  115.  
  116. /* Release a clipping path. */
  117. void
  118. gx_cpath_release(gx_clip_path *pcpath)
  119. {    if ( !pcpath->shares_list )
  120.         gx_clip_list_free(&pcpath->list, &pcpath->path.memory_procs);
  121.     gx_path_release(&pcpath->path);
  122. }
  123.  
  124. /* Share a clipping path. */
  125. void
  126. gx_cpath_share(gx_clip_path *pcpath)
  127. {    gx_path_share(&pcpath->path);
  128.     pcpath->shares_list = 1;
  129. }
  130.  
  131. /* Create a rectangular clipping path. */
  132. /* The supplied rectangle may not be oriented correctly, */
  133. /* but it will be oriented correctly upon return. */
  134. int
  135. gx_cpath_from_rectangle(gx_clip_path *pcpath, gs_fixed_rect *pbox, const gs_memory_procs *mp)
  136. {    gx_clip_list_from_rectangle(&pcpath->list, pbox);
  137.     pcpath->cbox = *pbox;
  138.     pcpath->segments_valid = 0;
  139.     pcpath->shares_list = 0;
  140.     gx_path_init(&pcpath->path, mp);
  141.     pcpath->path.bbox = *pbox;
  142.     return 0;
  143. }
  144.  
  145. /* Intersect a new clipping path with an old one. */
  146. /* Note that it may overwrite its path argument; return 1 in this case, */
  147. /* otherwise 0 for success, <0 for failure as usual. */
  148. int
  149. gx_cpath_intersect(gs_state *pgs, gx_clip_path *pcpath, gx_path *ppath, int rule)
  150. {    gs_fixed_rect old_box, new_box;
  151.     int code;
  152.     if ( gx_cpath_is_rectangle(pcpath, &old_box) &&
  153.         gx_path_is_rectangle(ppath, &new_box)
  154.        )
  155.        {    int changed = 0;
  156.         /* Intersect the two rectangles if necessary. */
  157.         if ( old_box.p.x > new_box.p.x )
  158.             new_box.p.x = old_box.p.x, changed = 1;
  159.         if ( old_box.p.y > new_box.p.y )
  160.             new_box.p.y = old_box.p.y, changed = 1;
  161.         if ( old_box.q.x < new_box.q.x )
  162.             new_box.q.x = old_box.q.x, changed = 1;
  163.         if ( old_box.q.y < new_box.q.y )
  164.             new_box.q.y = old_box.q.y, changed = 1;
  165.         if ( changed )
  166.            {    /* Store the new rectangle back into the new path. */
  167.             register segment *pseg =
  168.                 (segment *)ppath->first_subpath;
  169. #define set_pt(pqx,pqy)\
  170.   pseg->pt.x = new_box.pqx.x, pseg->pt.y = new_box.pqy.y
  171.             set_pt(p, p); pseg = pseg->next;
  172.             set_pt(q, p); pseg = pseg->next;
  173.             set_pt(q, q); pseg = pseg->next;
  174.             set_pt(p, q); pseg = pseg->next;
  175.             if ( pseg != 0 ) /* might be an open rectangle */
  176.               set_pt(p, p);
  177. #undef set_pt
  178.            }
  179.         ppath->bbox = new_box;
  180.         gx_clip_list_from_rectangle(&pcpath->list, &new_box);
  181.         pcpath->cbox = new_box;
  182.         pcpath->path = *ppath;
  183.         pcpath->segments_valid = 1;
  184.         code = 1;
  185.        }
  186.     else
  187.        {    /* Not a rectangle.  Intersect the slow way. */
  188.         gx_device_accum adev;
  189.         gx_device_color devc;
  190.         gx_device *save_dev = gs_currentdevice(pgs);
  191.         adev = gs_accum_device;
  192.         adev.memory_procs = pcpath->path.memory_procs;
  193.         (*adev.procs->open_device)((gx_device *)&adev);
  194.         devc.color1 = devc.color2 = 0;    /* arbitrary, but not */
  195.                     /* transparent */
  196.         devc.halftone_level = 0;
  197.         gx_set_device_only(pgs, (gx_device *)&adev);
  198.         code = gx_fill_path(ppath, &devc, pgs, rule, fixed_half);
  199.         gx_set_device_only(pgs, save_dev);
  200.         if ( code < 0 ) return code;
  201.         code = (*adev.procs->close_device)((gx_device *)&adev);
  202.         if ( code < 0 ) return code;
  203.         pcpath->list = adev.list;
  204.         gx_path_init(&pcpath->path, &pcpath->path.memory_procs);
  205.         pcpath->path.bbox.p.x = int2fixed(adev.bbox.p.x);
  206.         pcpath->path.bbox.p.y = int2fixed(adev.bbox.p.y);
  207.         pcpath->path.bbox.q.x = int2fixed(adev.bbox.q.x);
  208.         pcpath->path.bbox.q.y = int2fixed(adev.bbox.q.y);
  209.         /* Note that the result of the intersection might be */
  210.         /* a single rectangle.  This will cause clip_path_is_rect.. */
  211.         /* to return true.  This, in turn, requires that */
  212.         /* we set pcpath->cbox correctly. */
  213.         if ( clip_list_is_rectangle(&adev.list) )
  214.             pcpath->cbox = pcpath->path.bbox;
  215.         else
  216.            {    /* The quick check must fail. */
  217.             pcpath->cbox.p.x = pcpath->cbox.p.y = 0;
  218.             pcpath->cbox.q.x = pcpath->cbox.q.y = 0;
  219.            }
  220.         pcpath->segments_valid = 0;
  221.         pcpath->shares_list = 0;
  222.         code = 0;
  223.        }
  224.     return code;
  225. }
  226.  
  227. /* ------ Clipping list routines ------ */
  228.  
  229. /* Initialize a clip list. */
  230. void
  231. gx_clip_list_init(gx_clip_list *clp)
  232. {    *clp = clip_list_empty;
  233. }
  234.  
  235. /* Initialize a clip list to a rectangle. */
  236. /* The supplied rectangle may not be oriented correctly, */
  237. /* but it will be oriented correctly upon return. */
  238. void
  239. gx_clip_list_from_rectangle(gx_clip_list *clp, register gs_fixed_rect *rp)
  240. {    gx_clip_list_init(clp);
  241.     if ( rp->p.x > rp->q.x )
  242.       { fixed t = rp->p.x; rp->p.x = rp->q.x; rp->q.x = t; }
  243.     if ( rp->p.y > rp->q.y )
  244.       { fixed t = rp->p.y; rp->p.y = rp->q.y; rp->q.y = t; }
  245.     clp->sole.xmin = fixed2int_var(rp->p.x);
  246.     clp->sole.ymin = fixed2int_var(rp->p.y);
  247.     clp->sole.xmax = fixed2int_var_ceiling(rp->q.x);
  248.     clp->sole.ymax = fixed2int_var_ceiling(rp->q.y);
  249.     clp->count = 1;
  250. }
  251.  
  252. /* Add a clip list to a path. */
  253. /* The current implementation is very inefficient. */
  254. int
  255. gx_clip_list_add_to_path(gx_clip_list *clp, gx_path *ppath)
  256. {    gx_clip_rect *rp;
  257.     int code;
  258.     clip_prepare(clp);
  259.     for ( rp = &clp->first; rp != 0; rp = rp->next )
  260.        {    if ( rp->xmin < rp->xmax && rp->ymin < rp->ymax )
  261.            {    code = gx_path_add_rectangle(ppath,
  262.                     int2fixed(rp->xmin),
  263.                     int2fixed(rp->ymin),
  264.                     int2fixed(rp->xmax),
  265.                     int2fixed(rp->ymax));
  266.             if ( code < 0 ) return code;
  267.            }
  268.        }
  269.     return 0;
  270. }
  271.  
  272. /* Free a clip list. */
  273. void
  274. gx_clip_list_free(gx_clip_list *clp, const gs_memory_procs *mp)
  275. {    gx_clip_rect *rp = clp->last.prev;
  276.     if ( clp->count <= 1 ) return;
  277.     clip_prepare(clp);
  278.     while ( rp != &clp->first )
  279.        {    gx_clip_rect *prev = rp->prev;
  280.         (*mp->free)((char *)rp, 1, sizeof(gx_clip_rect), "gx_clip_list_free");
  281.         rp = prev;
  282.        }
  283.     gx_clip_list_init(clp);
  284. }
  285.  
  286. /* Prepare a clip list for enumeration, */
  287. /* by splicing pointers to account for possible relocation. */
  288. private void
  289. clip_prepare(register gx_clip_list *clp)
  290. {    if ( clp->count <= 1 )
  291.        {    clp->first.next = clp->last.prev = &clp->sole;
  292.         clp->sole.prev = &clp->first;
  293.         clp->sole.next = &clp->last;
  294.        }
  295.     else
  296.        {    clp->first.next->prev = &clp->first;
  297.         clp->last.prev->next = &clp->last;
  298.        }
  299. }
  300.  
  301. /* ------ Rectangle list accumulator ------ */
  302.  
  303. /* Device for accumulating a clipping region. */
  304. private dev_proc_open_device(accum_open);
  305. private dev_proc_close_device(accum_close);
  306. private dev_proc_fill_rectangle(accum_fill_rectangle);
  307.  
  308. /* The device descriptor */
  309. /* Many of these procedures won't be called; they are set to NULL. */
  310. private gx_device_procs accum_procs = {
  311.     accum_open,
  312.     NULL,                /* get_initial_matrix */
  313.     NULL,                /* sync_output */
  314.     NULL,                /* output_page */
  315.     accum_close,
  316.     NULL,                /* map_rgb_color */
  317.     NULL,                /* map_color_rgb */
  318.     accum_fill_rectangle,
  319.     NULL,                /* tile_rectangle */
  320.     NULL,                /* copy_mono */
  321.     NULL,                /* copy_color */
  322.     NULL,                /* draw_line */
  323.     NULL,                /* get_bits */
  324.     NULL,                /* get_props */
  325.     NULL                /* put_props */
  326. };
  327. gx_device_accum gs_accum_device =
  328. {    sizeof(gx_device_accum),
  329.     &accum_procs,
  330.     "clip list accumulator",
  331.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  332. };
  333. #define adev ((gx_device_accum *)dev)
  334.  
  335. /* Initialize the accumulation device. */
  336. private int
  337. accum_open(register gx_device *dev)
  338. {    gx_clip_list_init(&adev->list);
  339.     adev->last = &adev->list.first;
  340.     adev->bbox.p.x = adev->bbox.p.y = max_int;
  341.     adev->bbox.q.x = adev->bbox.q.y = min_int;
  342.     return 0;
  343. }
  344.  
  345. /* Close the accumulation device. */
  346. private int
  347. accum_close(gx_device *dev)
  348. {    if ( adev->list.count >= 2 )
  349.        {    /* 'sole' isn't good for much of anything, */
  350.         /* and it complicates the bookkeeping.... */
  351.         gx_clip_rect *last = adev->last;
  352.         gx_clip_rect *ar =
  353.           (gx_clip_rect *)(*adev->memory_procs.alloc)(1, sizeof(gx_clip_rect), "accum_close");
  354.         if ( ar == 0 ) return_error(gs_error_VMerror);
  355.         *ar = adev->list.sole;
  356.         adev->list.sole.prev->next = ar;
  357.         if ( last == &adev->list.sole )
  358.             last = ar;
  359.         else
  360.             adev->list.sole.next->prev = ar;
  361.         adev->list.last.prev = last;
  362.        }
  363. #ifdef DEBUG
  364. if ( gs_debug['q'] )
  365.    {    gx_clip_rect *rp = &adev->list.first;
  366.     adev->last->next = 0;
  367.     while ( rp != 0 )
  368.        {    clip_rect_print("   ", rp);
  369.         rp = rp->next;
  370.        }
  371.    }
  372.     clip_prepare(&adev->list); /* just for clip_list_validate */
  373.     clip_list_validate(&adev->list);
  374. #endif
  375.     return 0;
  376. }
  377.  
  378. /* Accumulate one rectangle. */
  379. #define accum_alloc(s, ar, px, py, qx, qy)\
  380.    {    ar = (adev->list.count == 0 ? &adev->list.sole :\
  381.        (gx_clip_rect *)(*adev->memory_procs.alloc)(1, sizeof(gx_clip_rect), "accum_rect"));\
  382.     if ( ar == 0 ) return_error(gs_error_VMerror);\
  383.     ar->xmin = px, ar->ymin = py, ar->xmax = qx, ar->ymax = qy;\
  384.     adev->list.count++;\
  385.     clip_rect_print(s, ar);\
  386.    }
  387. #define accum_add_last(ar)\
  388.     adev->last->next = ar, ar->prev = adev->last, adev->last = ar
  389. #define accum_add_after(ar, rprev)\
  390.     ar->prev = rprev, ar->next = rprev->next;\
  391.     if ( rprev != adev->last ) rprev->next->prev = ar;\
  392.     else adev->last = ar;\
  393.     rprev->next = ar
  394. #define accum_add_before(ar, rnext)\
  395.     ar->prev = rnext->prev, ar->next = rnext,\
  396.       rnext->prev->next = ar, rnext->prev = ar
  397. /* Add a rectangle to the list.  It would be wonderful if rectangles */
  398. /* were always presented in the correct order, but they aren't, */
  399. /* because the fill loop works by trapezoids, not by scan lines. */
  400. /* All we can count on is that they are disjoint and *approximately* */
  401. /* in order. */
  402. #undef adev
  403. private int
  404. accum_add_rect(gx_device_accum *adev, int x, int y, int xe, int ye)
  405. {    gx_clip_rect *nr, *ar, *rptr;
  406.     int ymin, ymax;
  407. top:    rptr = adev->last;
  408.     accum_alloc("accum", nr, x, y, xe, ye);
  409.     if ( y >= rptr->ymax ||
  410.         y == rptr->ymin && ye == rptr->ymax && x >= rptr->xmax
  411.        )
  412.        {    accum_add_last(nr);
  413.         return 0;
  414.        }
  415.     /* Work backwards till we find the insertion point. */
  416.     while ( ye <= rptr->ymin ) rptr = rptr->prev;
  417.     ymin = rptr->ymin;
  418.     ymax = rptr->ymax;
  419.     if ( ye > ymax )
  420.        {    if ( y >= ymax )
  421.            {    /* Insert between two bands. */
  422.             accum_add_after(nr, rptr);
  423.             return 0;
  424.            }
  425.         /* Split off the top part of the new rectangle. */
  426.         accum_alloc("a.top", ar, x, ymax, xe, ye);
  427.         accum_add_after(ar, rptr);
  428.         ye = nr->ymax = ymax;
  429.         clip_rect_print(" ymax", nr);
  430.        }
  431.     /* Here we know ymin < ye <= ymax; */
  432.     /* rptr points to the last node with this value of ymin/ymax. */
  433.     /* Split the existing band if necessary. */
  434.     if ( ye < ymax )
  435.        {    gx_clip_rect *rsplit = rptr;
  436.         while ( rsplit->ymax == ymax )
  437.            {    accum_alloc("s.top", ar, rsplit->xmin, ye, rsplit->xmax, ymax);
  438.             accum_add_after(ar, rptr);
  439.             rsplit->ymax = ye;
  440.             rsplit = rsplit->prev;
  441.            }
  442.         ymax = ye;
  443.        }
  444.     if ( y > ymin )
  445.        {    gx_clip_rect *rbot = rptr, *rsplit;
  446.         while ( rbot->prev->ymin == ymin )
  447.             rbot = rbot->prev;
  448.         for ( rsplit = rbot; ; )
  449.            {    accum_alloc("s.bot", ar, rsplit->xmin, ymin, rsplit->xmax, y);
  450.             accum_add_before(ar, rbot);
  451.             rsplit->ymin = y;
  452.             if ( rsplit == rptr ) break;
  453.             rsplit = rsplit->next;
  454.            }
  455.         ymin = y;
  456.        }
  457.     /* Search for the X insertion point. */
  458.     /* The new rectangle is guaranteed disjoint from all the old ones. */
  459.     while ( rptr->ymin == ymin && x < rptr->xmax )
  460.        {    rptr = rptr->prev;
  461.        }
  462.     if ( y < ymin )
  463.        {    /* Continue with the bottom part of the new rectangle. */
  464.         nr->ymin = ymin;
  465.         clip_rect_print(" ymin", nr);
  466.         accum_add_after(nr, rptr);
  467.         ye = ymin;
  468.         goto top;
  469.        }
  470.     accum_add_after(nr, rptr);
  471.     return 0;
  472. }
  473. #define adev ((gx_device_accum *)dev)
  474. private int
  475. accum_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  476.   gx_color_index color)
  477. {    int xe, ye;
  478.     if ( w <= 0 || h <= 0 ) return 0;
  479.     xe = x + w, ye = y + h;
  480.     /* Update the bounding box. */
  481.     if ( x < adev->bbox.p.x ) adev->bbox.p.x = x;
  482.     if ( y < adev->bbox.p.y ) adev->bbox.p.y = y;
  483.     if ( xe > adev->bbox.q.x ) adev->bbox.q.x = xe;
  484.     if ( ye > adev->bbox.q.y ) adev->bbox.q.y = ye;
  485.     return accum_add_rect(adev, x, y, xe, ye);
  486. }
  487.  
  488. /* ------ Rectangle list clipper ------ */
  489.  
  490. /* Device for clipping with a region. */
  491. private dev_proc_open_device(clip_open);
  492. private dev_proc_map_rgb_color(clip_map_rgb_color);
  493. private dev_proc_map_color_rgb(clip_map_color_rgb);
  494. private dev_proc_fill_rectangle(clip_fill_rectangle);
  495. private dev_proc_tile_rectangle(clip_tile_rectangle);
  496. private dev_proc_copy_mono(clip_copy_mono);
  497. private dev_proc_copy_color(clip_copy_color);
  498. private dev_proc_get_bits(clip_get_bits);
  499. private dev_proc_get_props(clip_get_props);
  500. private dev_proc_put_props(clip_put_props);
  501.  
  502. /* The device descriptor. */
  503. private gx_device_procs clip_procs = {
  504.     clip_open,
  505.     gx_default_get_initial_matrix,
  506.     gx_default_sync_output,
  507.     gx_default_output_page,
  508.     gx_default_close_device,
  509.     clip_map_rgb_color,
  510.     clip_map_color_rgb,
  511.     clip_fill_rectangle,
  512.     clip_tile_rectangle,
  513.     clip_copy_mono,
  514.     clip_copy_color,
  515.     gx_default_draw_line,
  516.     clip_get_bits,
  517.     clip_get_props,
  518.     clip_put_props
  519. };
  520. gx_device_clip gs_clip_device =
  521. {    sizeof(gx_device_clip),
  522.     &clip_procs,
  523.     "clipper",
  524.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  525. };
  526. #define rdev ((gx_device_clip *)dev)
  527.  
  528. /* Declare and initialize the cursor variables. */
  529. #ifdef DEBUG
  530. private ulong clip_in, clip_down, clip_down2, clip_up, clip_x, clip_no_x;
  531. #  define inc(v) v++
  532. #else
  533. #  define inc(v) 0
  534. #endif
  535. #define DECLARE_CLIP\
  536.   register gx_clip_rect *rptr = rdev->current.rptr;\
  537.   gx_device *tdev = rdev->target;
  538. /* Check whether the rectangle x,y,w,h falls within the current entry. */
  539. #define xywh_in_ryptr()\
  540.   ((y >= rptr->ymin && y + h <= rptr->ymax &&\
  541.     x >= rptr->xmin && x + w <= rptr->xmax) ? (inc(clip_in), 1) : 0)
  542. /*
  543.  * Warp the cursor forward or backward to the first rectangle row that
  544.  * could include a given y value.  Assumes rptr is set, and updates it.
  545.  * Specifically, after warp_cursor, y < rptr->ymax and y >= rptr->prev->ymax.
  546.  * Note that ye <= rptr->ymin is possible.
  547.  */
  548. #define warp_cursor(y)\
  549.   while ( (y) >= rptr->ymax ) { inc(clip_up); rptr = rptr->next; };\
  550.   while ( rptr->prev != 0 && (y) < rptr->prev->ymax )\
  551.    { inc(clip_down); rptr = rptr->prev; }
  552. /*
  553.  * Enumerate the rectangles of the x,w,y,h argument that fall within
  554.  * the clipping region.  Usage:
  555.  *    BEGIN_CLIP
  556.  *        (adjust for yc > y if necessary)
  557.  *    FOR_CLIP
  558.  *        ... xc, yc, xec, yec ... [must be an expression statement]
  559.  *    NEXT_CLIP
  560.  *        (about to set yc to yec)
  561.  *    END_CLIP
  562.  */
  563. #ifdef DEBUG
  564. #  define clip_2_print(str, v1, v2)\
  565.     if ( gs_debug['q'] ) dprintf2(str, v1, v2)
  566. #else
  567. #  define clip_2_print(str, v1, v2) 0
  568. #endif
  569. #define BEGIN_CLIP\
  570.     if ( w <= 0 || h <= 0 ) return 0;\
  571.    {    int yc;\
  572.     const int xe = x + w, ye = y + h;\
  573.     warp_cursor(y);\
  574.     rdev->current.rptr = rptr;\
  575.     yc = rptr->ymin;\
  576.     if ( yc < y ) yc = y;\
  577.     else if ( yc >= ye ) return 0;
  578. #define FOR_CLIP\
  579.     for ( ; ; )\
  580.        {    const int ymax = rptr->ymax;\
  581.         int yec = ymax;\
  582.         if ( yec > ye ) yec = ye;\
  583.         clip_2_print("[q]yc=%d yec=%d\n", yc, yec);\
  584.         do \
  585.            {    int xc = rptr->xmin;\
  586.             int xec = rptr->xmax;\
  587.             if ( xc < x ) xc = x;\
  588.             if ( xec > xe ) xec = xe;\
  589.             if ( xec > xc )\
  590.                {    int code;\
  591.                 clip_rect_print("match", rptr);\
  592.                 clip_2_print("[q]xc=%d xec=%d\n", xc, xec);\
  593.                 inc(clip_x);\
  594.                 code =
  595. #define NEXT_CLIP\
  596.                 if ( code < 0 ) return code;\
  597.                }\
  598.             else inc(clip_no_x);\
  599.            }\
  600.         while ( (rptr = rptr->next) != 0 && rptr->ymax == ymax );\
  601.         if ( rptr == 0 || (yec = rptr->ymin) >= ye ) break;
  602. #define END_CLIP\
  603.         yc = yec;\
  604.        }\
  605.    }
  606.  
  607. /* Open a clipping device */
  608. private int
  609. clip_open(register gx_device *dev)
  610. {    gx_device *tdev = rdev->target;
  611.     /* Fix up possible dangling pointers. */
  612.     clip_prepare(&rdev->list);
  613.     /* Initialize the cursor. */
  614.     rdev->current.rptr = &rdev->list.first;
  615.     rdev->color_info = tdev->color_info;
  616.     return 0;
  617. }
  618.  
  619. /* Forward non-displaying operations to the target device. */
  620. private gx_color_index
  621. clip_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  622.   gx_color_value b)
  623. {    gx_device *tdev = rdev->target;
  624.     return (*tdev->procs->map_rgb_color)(tdev, r, g, b);
  625. }
  626. private int
  627. clip_map_color_rgb(gx_device *dev, gx_color_index color,
  628.   gx_color_value prgb[3])
  629. {    gx_device *tdev = rdev->target;
  630.     return (*tdev->procs->map_color_rgb)(tdev, color, prgb);
  631. }
  632. private int
  633. clip_get_props(gx_device *dev, gs_prop_item *plist)
  634. {    gx_device *tdev = rdev->target;
  635.     return (*tdev->procs->get_props)(tdev, plist);
  636. }
  637. private int
  638. clip_put_props(gx_device *dev, gs_prop_item *plist, int count)
  639. {    gx_device *tdev = rdev->target;
  640.     return (*tdev->procs->put_props)(tdev, plist, count);
  641. }
  642.  
  643. /* Fill a rectangle */
  644. private int
  645. clip_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  646.   gx_color_index color)
  647. {    DECLARE_CLIP
  648.     dev_proc_fill_rectangle((*fill)) = tdev->procs->fill_rectangle;
  649.     if ( xywh_in_ryptr() )
  650.         return (*fill)(tdev, x, y, w, h, color);
  651.     BEGIN_CLIP
  652.     FOR_CLIP
  653.         (*fill)(tdev, xc, yc, xec - xc, yec - yc, color);
  654.     NEXT_CLIP
  655.     END_CLIP
  656.     return 0;
  657. }
  658.  
  659. /* Tile a rectangle */
  660. private int
  661. clip_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  662.   int x, int y, int w, int h,
  663.   gx_color_index color0, gx_color_index color1, int phase_x, int phase_y)
  664. {    DECLARE_CLIP
  665.     dev_proc_tile_rectangle((*fill)) = tdev->procs->tile_rectangle;
  666.     if ( xywh_in_ryptr() )
  667.         return (*fill)(tdev, tile, x, y, w, h, color0, color1, phase_x, phase_y);
  668.     BEGIN_CLIP
  669.     FOR_CLIP
  670.         (*fill)(tdev, tile, xc, yc, xec - xc, yec - yc, color0, color1, phase_x, phase_y);
  671.     NEXT_CLIP
  672.     END_CLIP
  673.     return 0;
  674. }
  675.  
  676. /* Copy a monochrome rectangle */
  677. private int
  678. clip_copy_mono(gx_device *dev,
  679.   const byte *data, int sourcex, int raster, gx_bitmap_id id,
  680.   int x, int y, int w, int h,
  681.   gx_color_index color0, gx_color_index color1)
  682. {    DECLARE_CLIP
  683.     dev_proc_copy_mono((*copy)) = tdev->procs->copy_mono;
  684.     if ( xywh_in_ryptr() )
  685.         return (*copy)(tdev, data, sourcex, raster, id, x, y, w, h, color0, color1);
  686.     BEGIN_CLIP
  687.         if ( yc > y ) data += (yc - y) * raster;
  688.     FOR_CLIP
  689.         (*copy)(tdev, data, sourcex + xc - x, raster, gx_no_bitmap_id, xc, yc, xec - xc, yec - yc, color0, color1);
  690.     NEXT_CLIP
  691.         data += (yec - yc) * raster;
  692.     END_CLIP
  693.     return 0;
  694. }
  695.  
  696. /* Copy a color rectangle */
  697. private int
  698. clip_copy_color(gx_device *dev,
  699.   const byte *data, int sourcex, int raster, gx_bitmap_id id,
  700.   int x, int y, int w, int h)
  701. {    DECLARE_CLIP
  702.     dev_proc_copy_color((*copy)) = tdev->procs->copy_color;
  703.     if ( xywh_in_ryptr() )
  704.         return (*copy)(tdev, data, sourcex, raster, id, x, y, w, h);
  705.     BEGIN_CLIP
  706.         if ( yc > y ) data += (yc - y) * raster;
  707.     FOR_CLIP
  708.         (*copy)(tdev, data, sourcex + xc - x, raster, gx_no_bitmap_id, xc, yc, xec - xc, yec - yc);
  709.     NEXT_CLIP
  710.         data += (yec - yc) * raster;
  711.     END_CLIP
  712.     return 0;
  713. }
  714.  
  715. /* Get bits back from the device. */
  716. private int
  717. clip_get_bits(gx_device *dev, int y, byte *data, uint size, int pad)
  718. {    gx_device *tdev = rdev->target;
  719.     return (*tdev->procs->get_bits)(tdev, y, data, size, pad);
  720. }
  721.