home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXFILL.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  24KB  |  729 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gxfill.c */
  20. /* Lower-level path filling procedures */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gxdevice.h"
  27. #include "gzpath.h"
  28. #include "gzstate.h"
  29. #include "gxcpath.h"
  30. #include "gzdraw.h"        /* gz_fill_trapezoid_fixed prototype */
  31.  
  32. /* Import the fixed * fixed / fixed routine from gxdraw.c. */
  33. /* The second argument must be less than or equal to the third; */
  34. /* all must be non-negative, and the last must be non-zero. */
  35. extern fixed fixed_mult_quo(P3(fixed, fixed, fixed));
  36.  
  37. /* Define the structure for keeping track of active lines. */
  38. typedef struct active_line_s active_line;
  39. struct active_line_s {
  40.     gs_fixed_point start;        /* x,y where line starts */
  41.     gs_fixed_point end;        /* x,y where line ends */
  42.     fixed dx;            /* end.x - start.x */
  43. #define al_dx(alp) ((alp)->dx)
  44. #define al_dy(alp) ((alp)->end.y - (alp)->start.y)
  45.     fixed y_fast_max;        /* can do x_at_y in fixed point */
  46.                     /* if y <= y_fast_max */
  47. #define set_al_points(alp, startp, endp)\
  48.   (alp)->dx = (endp).x - (startp).x,\
  49.   (alp)->y_fast_max = max_fixed /\
  50.     (((alp)->dx >= 0 ? (alp)->dx : -(alp)->dx) | 1) + (startp).y,\
  51.   (alp)->start = startp, (alp)->end = endp
  52. #define al_x_at_y(alp, yv)\
  53.   ((yv) == (alp)->end.y ? (alp)->end.x :\
  54.    ((yv) <= (alp)->y_fast_max ?\
  55.     ((yv) - (alp)->start.y) * al_dx(alp) / al_dy(alp) :\
  56.     (stat(n_slow_x),\
  57.      fixed_mult_quo(al_dx(alp), (yv) - (alp)->start.y, al_dy(alp)))) +\
  58.    (alp)->start.x)
  59.     fixed x_current;        /* current x position */
  60.     fixed x_next;            /* x position at end of band */
  61.     const segment *pseg;        /* endpoint of this line */
  62.     int direction;            /* direction of line segment */
  63. #define dir_up 1
  64. #define dir_down (-1)
  65. /* "Pending" lines (not reached in the Y ordering yet) use next and prev */
  66. /* to order lines by increasing starting Y.  "Active" lines (being scanned) */
  67. /* use next and prev to order lines by increasing current X, or if the */
  68. /* current Xs are equal, by increasing final X. */
  69.     active_line *prev, *next;
  70. /* Link together active_lines allocated individually */
  71.     active_line *alloc_next;
  72. };
  73. gs_private_st_simple(st_active_line, active_line, "active_line");
  74.  
  75. /* Define the ordering criterion for active lines. */
  76. /* The xc argument is a copy of lp2->x_current. */
  77. #define x_precedes(lp1, lp2, xc)\
  78.   (lp1->x_current < xc || (lp1->x_current == xc &&\
  79.    (lp1->start.x > lp2->start.x || lp1->end.x < lp2->end.x)))
  80.  
  81. #ifdef DEBUG
  82. /* Internal procedures for printing active lines */
  83. private void
  84. print_active_line(const char *label, const active_line *alp)
  85. {    dprintf5("[f]%s 0x%lx(%d): x_current=%f x_next=%f\n",
  86.              label, (ulong)alp, alp->direction,
  87.              fixed2float(alp->x_current), fixed2float(alp->x_next));
  88.     dprintf5("    start=(%f,%f) pt_end=0x%lx(%f,%f)\n",
  89.              fixed2float(alp->start.x), fixed2float(alp->start.y),
  90.              (ulong)alp->pseg,
  91.              fixed2float(alp->end.x), fixed2float(alp->end.y));
  92.     dprintf2("    prev=0x%lx next=0x%lx\n",
  93.          (ulong)alp->prev, (ulong)alp->next);
  94. }
  95. private void
  96. print_line_list(const active_line *flp)
  97. {    const active_line *lp;
  98.     for ( lp = flp; lp != 0; lp = lp->next )
  99.        {    fixed xc = lp->x_current, xn = lp->x_next;
  100.         dprintf3("[f]0x%lx(%d): x_current/next=%g",
  101.                  (ulong)lp, lp->direction,
  102.                  fixed2float(xc));
  103.         if ( xn != xc ) dprintf1("/%g", fixed2float(xn));
  104.         dputc('\n');
  105.        }
  106. }
  107. #define print_al(label,alp)\
  108.   if ( gs_debug_c('F') ) print_active_line(label, alp)
  109. #else
  110. #define print_al(label,alp) DO_NOTHING
  111. #endif
  112.  
  113. /* Line list structure */
  114. struct line_list_s {
  115.     gs_memory_t *memory;
  116.     active_line *active_area;    /* allocated active_line list */
  117.     gs_fixed_rect box;        /* intersection of bounding boxes, */
  118.                     /* disregard lines outside this */
  119.     active_line *next_active;    /* next allocation slot */
  120.     active_line *limit;        /* limit of local allocation */
  121.     int close_count;        /* # of added closing lines */
  122.     active_line *y_list;        /* Y-sorted list of pending lines */
  123.     active_line *y_line;        /* most recently inserted line */
  124.     active_line x_head;        /* X-sorted list of active lines */
  125. #define x_list x_head.next
  126.         /* Put the arrays last so the scalars will have */
  127.         /* small displacements. */
  128.         /* Allocate a few active_lines locally */
  129.         /* to avoid round trips through the allocator. */
  130. #define max_local_active 20
  131.     active_line local_active[max_local_active];
  132. };
  133. typedef struct line_list_s line_list;
  134. typedef line_list _ss *ll_ptr;
  135.  
  136. /* Forward declarations */
  137. private int alloc_line_list(P2(ll_ptr, gs_memory_t *));
  138. private void unclose_path(P2(gx_path *, int));
  139. private void free_line_list(P1(ll_ptr));
  140. private int add_y_list(P2(gx_path *, ll_ptr));
  141. private int add_y_line(P4(const segment *, const segment *, int, ll_ptr));
  142. private int fill_loop(P5(const gx_device_color *, int, ll_ptr,
  143.   gs_state *, fixed));
  144. private void insert_x_new(P2(active_line *, ll_ptr));
  145. private int update_x_list(P2(active_line *, fixed));
  146.  
  147. /* Statistics */
  148. #ifdef DEBUG
  149. #define stat(x) (x++)
  150. #define statn(x,n) (x += (n))
  151. private long n_fill;
  152. private long n_fill_alloc;
  153. private long n_y_up;
  154. private long n_y_down;
  155. private long n_x_step;
  156. private long n_slow_x;
  157. private long n_iter;
  158. private long n_find_y;
  159. private long n_band;
  160. private long n_band_step;
  161. private long n_band_fill;
  162. #else
  163. #define stat(x) 0
  164. #define statn(x,n) 0
  165. #endif
  166.  
  167. /* General area filling algorithm. */
  168. /* The adjust parameter is a hack for keeping small characters */
  169. /* from coming out too faint: it specifies an amount by which to expand */
  170. /* all sides of every filled region. */
  171. int
  172. gx_fill_path(gx_path *ppath, gx_device_color *pdevc, gs_state *pgs,
  173.   int rule, fixed adjust)
  174. {    const gx_clip_path *pcpath = pgs->clip_path;
  175.     gs_fixed_rect cbox;
  176.     gx_path *pfpath;
  177.     gx_path ffpath;
  178.     int code;
  179.     line_list lst;
  180.     gx_device_clip cdev;
  181.     bool do_clip;
  182.     /* Fatten everything a little to make it look better. */
  183.     /****** This is something of a hack. ******/
  184.     if ( adjust == 0 )
  185.         adjust = pgs->fill_adjust;
  186.     /* Start by flattening the path.  We should do this on-the-fly.... */
  187.     if ( !ppath->curve_count )    /* don't need to flatten */
  188.         pfpath = ppath;
  189.     else
  190.        {    if ( (code = gx_path_flatten(ppath, &ffpath, pgs->flatness, (int)pgs->in_cachedevice)) < 0 ) return code;
  191.         pfpath = &ffpath;
  192.        }
  193.     /* Check the bounding boxes. */
  194. #define ibox lst.box
  195.     gx_path_bbox(pfpath, &ibox);
  196.     gx_cpath_box_for_check(pcpath, &cbox);
  197.     /* Check for a (nearly) empty rectangle.  This is a hack */
  198.     /* to work around a bug in the Microsoft Windows PostScript driver, */
  199.     /* which draws thin lines by filling zero-width rectangles, */
  200.     /* and in some other drivers that try to fill epsilon-width */
  201.     /* rectangles. */
  202.     if ( ibox.q.x - ibox.p.x < fixed_half && ibox.q.y > ibox.p.y )
  203.         adjust = arith_rshift_1(fixed_1 + ibox.p.x - ibox.q.x + 1);
  204.     else if ( ibox.q.y - ibox.p.y < fixed_half && ibox.q.x > ibox.p.x )
  205.         adjust = arith_rshift_1(fixed_1 + ibox.p.y - ibox.q.y + 1);
  206.     if ( ibox.q.y <= cbox.q.y && ibox.q.x <= cbox.q.x &&
  207.          ibox.p.y >= cbox.p.y && ibox.p.x >= cbox.p.x
  208.        )
  209.        {    /* Path lies entirely within clipping rectangle */
  210.         do_clip = false;
  211.        }
  212.     else
  213.        {    /* Intersect the path box and the clip bounding box. */
  214.         /* If the intersection is empty, this fill is a no-op. */
  215.         gs_fixed_rect bbox;
  216.         bbox = pcpath->path.bbox;
  217.         if ( bbox.p.x > ibox.p.x )
  218.           ibox.p.x = bbox.p.x;
  219.         if ( bbox.q.x < ibox.q.x )
  220.           ibox.q.x = bbox.q.x;
  221.         if ( bbox.p.y > ibox.p.y )
  222.           ibox.p.y = bbox.p.y;
  223.         if ( bbox.q.y < ibox.q.y )
  224.           ibox.q.y = bbox.q.y;
  225.         if ( ibox.p.x >= ibox.q.x || ibox.p.y >= ibox.q.y )
  226.         {    /* Intersection of boxes is empty! */
  227.             code = 0;
  228.             goto skip;
  229.         }
  230.         do_clip = true;
  231.        }
  232. #undef ibox
  233.     if ( !(code = alloc_line_list(&lst, pgs->memory)) )
  234.        {    gx_device *save_dev;
  235.         if ( (code = add_y_list(pfpath, &lst)) < 0 )
  236.             goto nope;
  237.         if ( do_clip )
  238.            {    /* Set up a clipping device. */
  239.             gx_device *dev = (gx_device *)&cdev;
  240.             save_dev = gs_currentdevice(pgs);
  241.             gx_make_clip_device(&cdev, &cdev, &pcpath->list);
  242.             cdev.target = save_dev;
  243.             gx_set_device_only(pgs, dev);
  244.             (*dev_proc(dev, open_device))(dev);
  245.            }
  246.         code = fill_loop(pdevc, rule, &lst, pgs, adjust);
  247.         if ( do_clip )
  248.             gx_set_device_only(pgs, save_dev);
  249. nope:        if ( lst.close_count != 0 )
  250.             unclose_path(pfpath, lst.close_count);
  251.         free_line_list(&lst);
  252.        }
  253. skip:    if ( pfpath != ppath )    /* had to flatten */
  254.         gx_path_release(pfpath);
  255. #ifdef DEBUG
  256. if ( gs_debug_c('f') )
  257.        {    dputs("[f]calls alloc    up   down  step slowx  iter  find  band bstep bfill\n");
  258.         dprintf4("   %5ld %5ld %5ld %5ld",
  259.             n_fill, n_fill_alloc, n_y_up, n_y_down);
  260.         dprintf4(" %5ld %5ld %5ld %5ld",
  261.             n_x_step, n_slow_x, n_iter, n_find_y);
  262.         dprintf3(" %5ld %5ld %5ld\n",
  263.             n_band, n_band_step, n_band_fill);
  264.        }
  265. #endif
  266.     return code;
  267. }
  268.  
  269. /* Initialize the line list for a (flattened) path. */
  270. private int
  271. alloc_line_list(ll_ptr ll, gs_memory_t *mem)
  272. {    ll->memory = mem;
  273.     ll->active_area = 0;
  274.     ll->next_active = ll->local_active;
  275.     ll->limit = ll->next_active + max_local_active;
  276.     ll->close_count = 0;
  277.     ll->y_list = 0;
  278.     ll->y_line = 0;
  279.     stat(n_fill);
  280.     return 0;
  281. }
  282.  
  283. /* Unlink any line_close segments added temporarily. */
  284. private void
  285. unclose_path(gx_path *ppath, int count)
  286. {    subpath *psub;
  287.     for ( psub = ppath->first_subpath; count != 0;
  288.           psub = (subpath *)psub->last->next
  289.         )
  290.       if ( psub->last == (segment *)&psub->closer )
  291.       {    segment *prev = psub->closer.prev, *next = psub->closer.next;
  292.         prev->next = next;
  293.         if ( next ) next->prev = prev;
  294.         psub->last = prev;
  295.         count--;
  296.       }
  297. }
  298.  
  299. /* Free the line list. */
  300. private void
  301. free_line_list(ll_ptr ll)
  302. {    gs_memory_t *mem = ll->memory;
  303.     active_line *alp;
  304.     /* Free any individually allocated active_lines. */
  305.     while ( (alp = ll->active_area) != 0 )
  306.     {    active_line *next = alp->alloc_next;
  307.         gs_free_object(mem, alp, "active line");
  308.         ll->active_area = next;
  309.     }
  310. }
  311.  
  312. /* Construct a Y-sorted list of lines for a (flattened) path. */
  313. /* We assume the path is non-empty.  Only include non-horizontal */
  314. /* lines where one endpoint is locally Y-minimal. */
  315. private int
  316. add_y_list(gx_path *ppath, ll_ptr ll)
  317. {    register segment *pseg = (segment *)ppath->first_subpath;
  318.     subpath *psub;
  319.     segment *plast;
  320.     int first_dir, prev_dir, dir;
  321.     segment *prev;
  322.     int close_count = 0;
  323.     /* fixed xmin = ll->box.p.x; */    /* not currently used */
  324.     fixed ymin = ll->box.p.y;
  325.     /* fixed xmax = ll->box.q.x; */    /* not currently used */
  326.     fixed ymax = ll->box.q.y;
  327.     int code;
  328.  
  329.     while ( pseg )
  330.        {    switch ( pseg->type )
  331.            {    /* No curves left */
  332.         case s_start:
  333.             psub = (subpath *)pseg;
  334.             plast = psub->last;
  335.             dir = 2;    /* hack to skip first line */
  336.             if ( plast->type != s_line_close )
  337.                {    /* Create a fake s_line_close */
  338.                 line_close_segment *lp = &psub->closer;
  339.                 segment *next = plast->next;
  340.                 lp->next = next;
  341.                 lp->prev = plast;
  342.                 plast->next = (segment *)lp;
  343.                 if ( next ) next->prev = (segment *)lp;
  344.                 lp->type = s_line_close;
  345.                 lp->pt = psub->pt;
  346.                 lp->sub = psub;
  347.                 psub->last = plast = (segment *)lp;
  348.                 ll->close_count++;
  349.                }
  350.             break;
  351.         default:        /* s_line, _close */
  352.            {    fixed iy = pseg->pt.y;
  353.             fixed py = prev->pt.y;
  354.             /* Lines falling entirely outside the ibox in Y */
  355.             /* are treated as though they were horizontal, */
  356.             /* i.e., they are never put on the list. */
  357. #define compute_dir(xo, xe, yo, ye)\
  358.   (ye > yo ? (ye <= ymin || yo >= ymax ? 0 : dir_up) :\
  359.    ye < yo ? (yo <= ymin || ye >= ymax ? 0 : dir_down) :\
  360.    0)
  361. #define add_dir_lines(prev2, prev, this, pdir, dir)\
  362.   if ( pdir )\
  363.    { if ( (code = add_y_line(prev2, prev, pdir, ll)) < 0 ) return code; }\
  364.   if ( dir )\
  365.    { if ( (code = add_y_line(prev, this, dir, ll)) < 0 ) return code; }
  366.             dir = compute_dir(prev->pt.x, pseg->pt.x, py, iy);
  367.             if ( dir > prev_dir )
  368.                {    add_dir_lines(prev->prev, prev, pseg, prev_dir, dir);
  369.                }
  370.             else if ( prev_dir == 2 )    /* first line */
  371.                 first_dir = dir;
  372.             if ( pseg == plast )
  373.                {    /* We skipped the first segment of the */
  374.                 /* subpath, so the last segment must */
  375.                 /* receive special consideration. */
  376.                 /* Note that we have `closed' all subpaths. */
  377.                 if ( first_dir > dir )
  378.                    {    add_dir_lines(prev, pseg, psub->next, dir, first_dir);
  379.                    }
  380.                }
  381.            }
  382. #undef compute_dir
  383. #undef add_dir_lines
  384.            }
  385.         prev = pseg;
  386.         prev_dir = dir;
  387.         pseg = pseg->next;
  388.        }
  389.     return close_count;
  390. }
  391. /* Internal routine to test a line segment and add it to the */
  392. /* pending list if appropriate. */
  393. private int
  394. add_y_line(const segment *prev_lp, const segment *lp, int dir, ll_ptr ll)
  395. {    gs_fixed_point this, prev;
  396.     register active_line *alp = ll->next_active;
  397.     fixed y_start;
  398.     if ( alp == ll->limit )
  399.        {    /* Allocate separately */
  400.         alp = gs_alloc_struct(ll->memory, active_line,
  401.                       &st_active_line, "active line");
  402.         if ( alp == 0 ) return_error(gs_error_VMerror);
  403.         alp->alloc_next = ll->active_area;
  404.         ll->active_area = alp;
  405.         stat(n_fill_alloc);
  406.        }
  407.     else
  408.         ll->next_active++;
  409.     this.x = lp->pt.x;
  410.     this.y = lp->pt.y;
  411.     prev.x = prev_lp->pt.x;
  412.     prev.y = prev_lp->pt.y;
  413.     if ( (alp->direction = dir) > 0 )
  414.        {    /* Upward line */
  415.         y_start = prev.y;
  416.         set_al_points(alp, prev, this);
  417.         alp->pseg = lp;
  418.        }
  419.     else
  420.        {    /* Downward line */
  421.         y_start = this.y;
  422.         set_al_points(alp, this, prev);
  423.         alp->pseg = prev_lp;
  424.        }
  425.     /* Insert the new line in the Y ordering */
  426.        {    register active_line *yp = ll->y_line;
  427.         register active_line *nyp;
  428.         if ( yp == 0 )
  429.            {    alp->next = alp->prev = 0;
  430.             ll->y_list = alp;
  431.            }
  432.         else if ( y_start >= yp->start.y )
  433.            {    /* Insert the new line after y_line */
  434.             while ( stat(n_y_up), (nyp = yp->next) != NULL && y_start > nyp->start.y )
  435.                 yp = nyp;
  436.             alp->next = nyp;
  437.             alp->prev = yp;
  438.             yp->next = alp;
  439.             if ( nyp ) nyp->prev = alp;
  440.            }
  441.         else
  442.            {    /* Insert the new line before y_line */
  443.             while ( stat(n_y_down), (nyp = yp->prev) != NULL && y_start < nyp->start.y )
  444.                 yp = nyp;
  445.             alp->prev = nyp;
  446.             alp->next = yp;
  447.             yp->prev = alp;
  448.             if ( nyp ) nyp->next = alp;
  449.             else ll->y_list = alp;
  450.            }
  451.        }
  452.     ll->y_line = alp;
  453.     print_al("add ", alp);
  454.     return 0;
  455. }
  456.  
  457. /* Main filling loop.  Takes lines off of y_list and adds them to */
  458. /* x_list as needed. */
  459. private int
  460. fill_loop(const gx_device_color *pdevc, int rule, ll_ptr ll,
  461.   gs_state *pgs, fixed adjust)
  462. {    fixed adj2 = adjust << 1;
  463.     active_line *yll = ll->y_list;
  464.     gs_fixed_point pmax;
  465.     fixed y;
  466.     int code;
  467.     if ( yll == 0 ) return 0;        /* empty list */
  468.     pmax = ll->box.q;
  469.     y = yll->start.y;            /* first Y value */
  470.     ll->x_list = 0;
  471.     ll->x_head.x_current = min_fixed;    /* stop backward scan */
  472.     while ( 1 )
  473.        {    fixed y1, y0;
  474.         active_line *endp, *alp, *stopx;
  475.         fixed x;
  476.         int draw;
  477.         stat(n_iter);
  478.         /* Check whether we've reached the maximum y. */
  479.         if ( y >= pmax.y ) break;
  480.         /* Move newly active lines from y to x list. */
  481.         while ( yll != 0 && yll->start.y == y )
  482.            {    active_line *ynext = yll->next;    /* insert smashes next/prev links */
  483.             insert_x_new(yll, ll);
  484.             yll = ynext;
  485.            }
  486.         if ( ll->x_list == 0 )
  487.            {    /* No active lines, skip to next start */
  488.             if ( yll == 0 ) break;    /* no lines left */
  489.             y = yll->start.y;
  490.             continue;
  491.            }
  492.         /* Find the next evaluation point. */
  493.         /* Start by finding the smallest y value */
  494.         /* at which any currently active line ends */
  495.         /* (or the next to-be-active line begins). */
  496.         y1 = (yll != 0 ? yll->start.y : pmax.y);
  497.         for ( alp = ll->x_list; alp != 0; alp = alp->next )
  498.           if ( alp->end.y < y1 ) y1 = alp->end.y;
  499. #ifdef DEBUG
  500. if ( gs_debug_c('F') )
  501.    {        dprintf2("[F]before loop: y=%f y1=%f:\n",
  502.                  fixed2float(y), fixed2float(y1));
  503.         print_line_list(ll->x_list);
  504.    }
  505. #endif
  506.         /* Now look for line intersections before y1. */
  507.         x = min_fixed;
  508.         y0 = y - adjust;
  509. #define have_pixels() (fixed_rounded(y0) < fixed_rounded(y1 + adjust))
  510.         draw = (have_pixels() ? 1 : -1);
  511.         /*
  512.          * Loop invariants:
  513.          *    alp = endp->next;
  514.          *    for all lines lp from stopx up to alp,
  515.          *      lp->x_next = al_x_at_y(lp, y1).
  516.          */
  517.         for ( alp = stopx = ll->x_list; stat(n_find_y), alp != 0;
  518.              endp = alp, alp = alp->next
  519.             )
  520.            {    fixed nx = al_x_at_y(alp, y1);
  521.             fixed dx_old, dx_den;
  522.             /* Check for intersecting lines. */
  523.             if ( nx >= x )
  524.                 x = nx;
  525.             else if
  526.                ( draw >= 0 && /* don't bother if no pixels */
  527.                  (dx_old = alp->x_current - endp->x_current) >= 0 &&
  528.                  (dx_den = dx_old + endp->x_next - nx) > dx_old
  529.                )
  530.                {    /* Make a good guess at the intersection */
  531.                 /* Y value using only local information. */
  532.                 fixed dy = y1 - y, y_new;
  533.                 if_debug3('f', "[f]cross: dy=%g, dx_old=%g, dx_new=%g\n",
  534.                   fixed2float(dy), fixed2float(dx_old),
  535.                   fixed2float(dx_den - dx_old));
  536.                 /* Do the computation in single precision */
  537.                 /* if the values are small enough. */
  538.                 y_new =
  539.                   ((dy | dx_old) < 1L << (size_of(fixed)*4-1) ?
  540.                    dy * dx_old / dx_den :
  541.                    fixed_mult_quo(dy, dx_old, dx_den))
  542.                   + y;
  543.                 /* The crossing value doesn't have to be */
  544.                 /* very accurate, but it does have to be */
  545.                 /* greater than y and less than y1. */
  546.                 if_debug3('f', "[f]cross y=%g, y_new=%g, y1=%g\n",
  547.                   fixed2float(y), fixed2float(y_new),
  548.                   fixed2float(y1));
  549.                 stopx = alp;
  550.                 if ( y_new <= y ) y_new = y + 1;
  551.                 if ( y_new < y1 )
  552.                    {    y1 = y_new;
  553.                     nx = al_x_at_y(alp, y1);
  554.                     draw = 0;
  555.                    }
  556.                 if ( nx > x ) x = nx;
  557.                }
  558.             alp->x_next = nx;
  559.            }
  560.         /* Recompute next_x for lines before the intersection. */
  561.         for ( alp = ll->x_list; alp != stopx; alp = alp->next )
  562.             alp->x_next = al_x_at_y(alp, y1);
  563. #ifdef DEBUG
  564. if ( gs_debug_c('F') )
  565.    {        dprintf1("[F]after loop: y1=%f\n", fixed2float(y1));
  566.         print_line_list(ll->x_list);
  567.    }
  568. #endif
  569.         /* Fill a multi-trapezoid band for the active lines. */
  570.         /* Don't bother if no pixel centers lie within the band. */
  571.         if ( draw > 0 || (draw == 0 && have_pixels()) )
  572.            {    active_line *alp = ll->x_list;
  573.             fixed height = y1 - y + adj2;
  574.             fixed xlbot, xltop;    /* as of last "outside" line */
  575.             int inside = 0;
  576.             stat(n_band);
  577.             x = min_fixed;
  578.             /* rule = -1 for winding number rule, i.e. */
  579.             /* we are inside if the winding number is non-zero; */
  580.             /* rule = 1 for even-odd rule, i.e. */
  581.             /* we are inside if the winding number is odd. */
  582.             /* Clever, eh? */
  583. #define inside_path_p() (inside & rule)
  584.             while ( alp != 0 )
  585.                {    fixed xbot = alp->x_current;
  586.                 fixed xtop = alp->x_next;
  587.                 print_al("step", alp);
  588.                 stat(n_band_step);
  589.                 if ( inside_path_p() )
  590.                  { inside += alp->direction;
  591.                    if ( !inside_path_p() )    /* about to go out */
  592.                     {    fixed wbot = xbot - xlbot + adj2;
  593.                     fixed wtop = xtop - xltop + adj2;
  594.                     stat(n_band_fill);
  595.                     /* If lines are temporarily out of */
  596.                     /* order, wtop might be negative. */
  597.                     /* Patch this up now. */
  598.                     if ( wtop < 0 )
  599.                        {    xltop += wtop >> 1;
  600.                         wtop = 0;
  601.                        }
  602.                     code = gz_fill_trapezoid_fixed(
  603.                          xlbot - adjust,
  604.                          wbot, y0,
  605.                          xltop - adjust, wtop,
  606.                          height, 0, pdevc, pgs);
  607.                     if ( code < 0 ) return code;
  608.                     }
  609.                  }
  610.                 else            /* outside */
  611.                    {    inside += alp->direction;
  612.                     if ( inside_path_p() )    /* about to go in */
  613.                         xlbot = xbot, xltop = xtop;
  614.                    }
  615.                 alp = alp->next;
  616.                }
  617.            }
  618.         code = update_x_list(ll->x_list, y1);
  619.         if ( code < 0 ) return code;
  620.         y = y1;
  621.        }
  622.     return 0;
  623. }
  624.  
  625. /* Insert a newly active line in the X ordering. */
  626. private void
  627. insert_x_new(active_line *alp, ll_ptr ll)
  628. {    register active_line *next;
  629.     register active_line *prev = &ll->x_head;
  630.     register fixed x = alp->start.x;
  631.     alp->x_current = x;
  632.     while ( stat(n_x_step),
  633.         (next = prev->next) != 0 && x_precedes(next, alp, x)
  634.            )
  635.         prev = next;
  636.     alp->next = next;
  637.     alp->prev = prev;
  638.     if ( next != 0 ) next->prev = alp;
  639.     prev->next = alp;
  640. }
  641.  
  642. /* Clean up after a pass through the main loop. */
  643. /* If any lines are out of order, re-sort them now. */
  644. /* Also drop any ended lines. */
  645. private int
  646. update_x_list(active_line *x_first, fixed y1)
  647. {    fixed x;
  648.     register active_line *alp;
  649.     active_line *nlp;
  650.     for ( x = min_fixed, alp = x_first; alp != 0; alp = nlp )
  651.        {    fixed nx = alp->x_current = alp->x_next;
  652.         nlp = alp->next;
  653.         if_debug4('F', "[F]check 0x%lx,x=%g 0x%lx,x=%g\n",
  654.               (ulong)alp->prev, fixed2float(x),
  655.               (ulong)alp, fixed2float(nx));
  656.         if ( alp->end.y == y1 )
  657.            {    /* Handle a line segment that just ended. */
  658.             const segment *pseg = alp->pseg;
  659.             const segment *next;
  660.             gs_fixed_point npt;
  661.             /*
  662.              * The computation of next relies on the fact that
  663.              * all subpaths have been closed.  When we cycle
  664.              * around to the other end of a subpath, we must be
  665.              * sure not to process the start/end point twice.
  666.              */
  667.             next =
  668.               (alp->direction == dir_up ?
  669.                (/* Upward line, go forward along path. */
  670.                 pseg->type == s_line_close ? /* end of subpath */
  671.                  ((line_close_segment *)pseg)->sub->next :
  672.                  pseg->next) :
  673.                (/* Downward line, go backward along path. */
  674.                 pseg->type == s_start ? /* start of subpath */
  675.                  ((subpath *)pseg)->last->prev :
  676.                  pseg->prev)
  677.               );
  678.             npt.y = next->pt.y;
  679.             if_debug5('F', "[F]ended 0x%lx: pseg=0x%lx y=%f next=0x%lx npt.y=%f\n",
  680.                  (ulong)alp, (ulong)pseg, fixed2float(pseg->pt.y),
  681.                  (ulong)next, fixed2float(npt.y));
  682.             if ( npt.y <= pseg->pt.y )
  683.                {    /* End of a line sequence */
  684.                 alp->prev->next = nlp;
  685.                 if ( nlp ) nlp->prev = alp->prev;
  686.                 if_debug1('F', "[F]drop 0x%lx\n", (ulong)alp);
  687.                 continue;
  688.                }
  689.             else
  690.                {    alp->pseg = next;
  691.                 npt.x = next->pt.x;
  692.                 set_al_points(alp, alp->end, npt);
  693.                 print_al("repl", alp);
  694.                }
  695.            }
  696.         if ( nx <= x )
  697.            {    /* Move alp backward in the list. */
  698.             active_line *prev = alp->prev;
  699.             active_line *next = nlp;
  700.             prev->next = next;
  701.             if ( next ) next->prev = prev;
  702.             while ( !x_precedes(prev, alp, nx) )
  703.                {    if_debug2('f', "[f]swap 0x%lx,0x%lx\n",
  704.                          (ulong)alp, (ulong)prev);
  705.                 next = prev, prev = prev->prev;
  706.                }
  707.             alp->next = next;
  708.             alp->prev = prev;
  709.             /* next might be null, if alp was in */
  710.             /* the correct spot already. */
  711.             if ( next ) next->prev = alp;
  712.             prev->next = alp;
  713.            }
  714.         else
  715.             x = nx;
  716.        }
  717. #ifdef DEBUG
  718. if ( gs_debug_c('f') )
  719.     for ( alp = x_first->prev->next; alp != 0; alp = alp->next )
  720.      if ( alp->next != 0 && alp->next->x_current < alp->x_current )
  721.        {    lprintf("[f]Lines out of order!\n");
  722.         print_active_line("   1:", alp);
  723.         print_active_line("   2:", alp->next);
  724.         return_error(gs_error_Fatal);
  725.        }
  726. #endif
  727.     return 0;
  728. }
  729.