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 / GXSTROKE.C < prev    next >
C/C++ Source or Header  |  1992-09-10  |  26KB  |  759 lines

  1. /* Copyright (C) 1989, 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. /* gxstroke.c */
  21. /* Path stroking procedures for Ghostscript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "gpcheck.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gscoord.h"
  30. #include "gzstate.h"
  31. #include "gzdevice.h"
  32. #include "gzcolor.h"            /* requires gxdevice.h */
  33. #include "gzline.h"
  34. #include "gzpath.h"
  35.  
  36. /* Define the filling adjustment that actually produces no adjustment. */
  37. #define fill_no_adjust ((fixed)1)
  38.  
  39. /*
  40.  * Structure for a partial line (passed to the drawing routine).
  41.  * Two of these are required to do joins right.
  42.  * Each endpoint includes the two ends of the cap as well,
  43.  * and the deltas for square and round cap computation.
  44.  *
  45.  * The deltas (co, cdelta, ce) are in clockwise order in device space
  46.  * around the endpoint p: they are one-half the line width (suitably
  47.  * transformed) at 90 degrees counter-clockwise, straight ahead,
  48.  * and 90 degrees clockwise from the oriented line o->e,
  49.  * where "90 degrees" is measured in *user* coordinates.
  50.  * Note that the values at o are the negatives of the values at e.
  51.  *
  52.  * Initially, only o.p, e.p, o.cdelta, width, and thin are set.
  53.  * compute_caps fills in the rest when needed.
  54.  */
  55. typedef gs_fixed_point _ss *p_ptr;
  56. typedef struct endpoint_s {
  57.     gs_fixed_point p;        /* the end of the line */
  58.     gs_fixed_point co, ce;        /* ends of the cap, p +/- width */
  59.     gs_fixed_point cdelta;        /* +/- cap length */
  60. } endpoint;
  61. typedef endpoint _ss *ep_ptr;
  62. typedef struct partial_line_s {
  63.     endpoint o;            /* starting coordinate */
  64.     endpoint e;            /* ending coordinate */
  65.     gs_fixed_point width;        /* one-half line width, see above */
  66.     int thin;            /* true if minimum-width line */
  67. } partial_line;
  68. typedef partial_line _ss *pl_ptr;
  69.  
  70. /* Procedures that stroke a partial_line (the first argument). */
  71. /* If both partial_lines are non-null, the procedure creates */
  72. /* an appropriate join; otherwise, the procedure creates an */
  73. /* end cap.  If the first int is 0, the procedure also starts with */
  74. /* an appropriate cap. */
  75. private int near stroke_add(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *));
  76. private int near stroke_fill(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *));
  77.  
  78. /* Other forward declarations */
  79. private int near stroke(P4(const gx_path *, gx_path *,
  80.   int near (*)(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *)),
  81.   gs_state *));
  82. private void near adjust_stroke(P2(pl_ptr, gs_state *));
  83. private int near expand_dashes(P3(const subpath *, gx_path *, gs_state *));
  84. private void near compute_caps(P1(pl_ptr));
  85. private int near add_capped(P4(gx_path *, gs_line_cap,
  86.   int (*)(P3(gx_path *, fixed, fixed)),
  87.   ep_ptr));
  88.  
  89. /* Stroke a path for drawing or saving */
  90. int
  91. gx_stroke_fill(const gx_path *ppath, gs_state *pgs)
  92. {    return stroke(ppath, (gx_path *)0, stroke_fill, pgs);
  93. }
  94. int
  95. gx_stroke_add(const gx_path *ppath, gx_path *to_path, gs_state *pgs)
  96. {    int code = stroke(ppath, to_path, stroke_add, pgs);
  97.     if ( code < 0 ) return code;
  98.     if ( !ppath->subpath_open && ppath->position_valid )
  99.         code = gx_path_add_point(to_path, ppath->position.x,
  100.                      ppath->position.y);
  101.     return code;
  102. }
  103.  
  104. /* Fill a partial stroked path. */
  105. /* Free variables: code, to_path, ppath, stroke_path_body, pgs, exit (label). */
  106. #define fill_stroke_path()\
  107. if(to_path==&stroke_path_body && !gx_path_is_void_inline(&stroke_path_body))\
  108. { code = gx_fill_path(to_path, pgs->dev_color, pgs, gx_rule_winding_number,\
  109.     fill_no_adjust);\
  110.   gx_path_release(to_path);\
  111.   if ( code < 0 ) goto exit;\
  112.   gx_path_init(to_path, &ppath->memory_procs);\
  113. }
  114.  
  115. /* Stroke a path.  Call line_proc (stroke_add or stroke_fill) */
  116. /* for each line segment. */
  117. private int near
  118. stroke(const gx_path *ppath, gx_path *to_path,
  119.   int near (*line_proc)(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *)),
  120.   gs_state *pgs)
  121. {    const subpath *psub;
  122.     const subpath *save_psub = 0;
  123.     int code = 0;
  124.     const line_params *lp = pgs->line_params;
  125.     int dash_count = lp->dash.pattern_size;
  126.     gx_path fpath, dpath;
  127.     gx_path stroke_path_body;
  128.     float xx = pgs->ctm.xx, xy = pgs->ctm.xy;
  129.     float yx = pgs->ctm.yx, yy = pgs->ctm.yy;
  130.     int skewed = !is_fzero2(xy, yx);
  131.     int uniform = (skewed ? 0 : xx == yy ? 1 : xx == -yy ? -1 : 0);
  132.     /*
  133.      * We are dealing with a reflected coordinate system
  134.      * if (1,0) is counter-clockwise from (0,1).
  135.      * See the note in stroke_add for the algorithm.
  136.      */    
  137.     int reflected =
  138.       (uniform ? uniform > 0 :
  139.        skewed ? xy * yx < xx * yy :
  140.        (xx < 0) == (yy < 0));
  141.     float line_width = lp->width;    /* this is *half* the line width! */
  142.     int always_thin;
  143.     double line_width_and_scale, line_width_scale_xx;
  144. #ifdef DEBUG
  145. if ( gs_debug['o'] )
  146.    {    int count = lp->dash.pattern_size;
  147.     int i;
  148.     dprintf3("[o]half_width=%f, cap=%d, join=%d,\n",
  149.          lp->width, (int)lp->cap, (int)lp->join);
  150.     dprintf2("   miter_limit=%f, miter_check=%f,\n",
  151.          lp->miter_limit, lp->miter_check);
  152.     dprintf1("   dash pattern=%d", count);
  153.     for ( i = 0; i < count; i++ )
  154.         dprintf1(",%f", lp->dash.pattern[i]);
  155.     dprintf4(",\n   offset=%f, init(ink_on=%d, index=%d, dist_left=%f)\n",
  156.          lp->dash.offset, lp->dash.init_ink_on, lp->dash.init_index,
  157.          lp->dash.init_dist_left);
  158.    }
  159. #endif
  160.     if ( line_width < 0 ) line_width = -line_width;
  161.     if ( is_fzero(line_width) )
  162.         always_thin = 1;
  163.     else if ( !skewed )
  164.        {    float xxa = xx, yya = yy;
  165.         if ( xxa < 0 ) xxa = -xxa;
  166.         if ( yya < 0 ) yya = -yya;
  167.         always_thin = (max(xxa, yya) * line_width < 0.5);
  168.        }
  169.     else
  170.        {    /* The check is more complicated, but it's worth it. */
  171.         float xsq = xx * xx + xy * xy;
  172.         float ysq = yx * yx + yy * yy;
  173.         float cross = xx * yx + xy * yy;
  174.         if ( cross < 0 ) cross = 0;
  175.         always_thin =
  176.           ((max(xsq, ysq) + cross) * line_width * line_width < 0.5);
  177.        }
  178.     line_width_and_scale = line_width * (double)int2fixed(1);
  179.     if ( !always_thin && uniform )
  180.     {    /* Precompute a value we'll need later. */
  181.         line_width_scale_xx = line_width_and_scale * xx;
  182.         if ( line_width_scale_xx < 0 )
  183.           line_width_scale_xx = -line_width_scale_xx;
  184.     }
  185.     if_debug5('o', "[o]ctm=(%g,%g,%g,%g) thin=%d\n",
  186.           xx, xy, yx, yy, always_thin);
  187.     /* Start by flattening the path.  We should do this on-the-fly.... */
  188.     if ( !ppath->curve_count )    /* don't need to flatten */
  189.        {    psub = ppath->first_subpath;
  190.         if ( !psub ) return 0;
  191.        }
  192.     else
  193.        {    if ( (code = gx_path_flatten(ppath, &fpath, pgs->flatness, (int)pgs->in_cachedevice)) < 0 )
  194.            return code;
  195.         psub = fpath.first_subpath;
  196.        }
  197.     if ( dash_count )
  198.         gx_path_init(&dpath, &ppath->memory_procs);
  199.     if ( to_path == 0 )
  200.     {    /* We might try to defer this if it's expensive.... */
  201.         to_path = &stroke_path_body;
  202.         gx_path_init(to_path, &ppath->memory_procs);
  203.     }
  204.     for ( ; ; )
  205.      { const line_segment *pline;
  206.        fixed x, y;
  207.        partial_line pl, pl_prev, pl_first;
  208.        int first = 0;
  209.        int index = 0;
  210.        if ( !psub )
  211.         {    /* Might just be the end of a dash expansion. */
  212.         if ( save_psub )
  213.            {    gx_path_release(&dpath);
  214.             psub = (const subpath *)save_psub->last->next;
  215.             if ( !psub ) break;
  216.             gx_path_init(&dpath, &ppath->memory_procs);
  217.             save_psub = 0;
  218.            }
  219.         else        /* all done */
  220.             break;
  221.         }
  222.        if ( dash_count && !save_psub )
  223.         {    code = expand_dashes(psub, &dpath, pgs);
  224.         if ( code < 0 ) goto exit;
  225.         save_psub = (subpath *)psub;
  226.         psub = dpath.first_subpath;
  227.         continue;        /* psub might be null */
  228.         }
  229.        pline = (const line_segment *)(psub->next);
  230.        x = psub->pt.x;
  231.        y = psub->pt.y;
  232.        while ( pline != 0 && pline->type != s_start )
  233.         {    fixed sx = pline->pt.x;
  234.         fixed sy = pline->pt.y;
  235.         /* Compute the width parameters in device space. */
  236.         /* We work with unscaled values, for speed. */
  237.         pl.o.p.x = x, pl.o.p.y = y;
  238.         pl.e.p.x = sx, pl.e.p.y = sy;
  239.         if ( !always_thin )
  240.            {    fixed udx = sx - x, udy = sy - y;
  241.             if ( !(udx | udy) )    /* degenerate */
  242.              { /* Only consider a degenerate segment */
  243.                /* if the entire subpath is degenerate and */
  244.                /* we are using round caps or joins. */
  245.                if ( index != 0 || (pline->next != 0 &&
  246.                  pline->next->type != s_start) ||
  247.                 (lp->cap != gs_cap_round &&
  248.                  lp->join != gs_join_round)
  249.                   )
  250.                  goto nd;
  251.                /* Pick an arbitrary orientation. */
  252.                udx = int2fixed(1);
  253.              }
  254.             if ( uniform != 0 )
  255.                {    /* We can save a lot of work in this case. */
  256.                 float dpx = udx, dpy = udy;
  257.                  float wl = line_width_scale_xx /
  258.                     hypot(dpx, dpy);
  259.                 pl.e.cdelta.x = (fixed)(dpx * wl);
  260.                 pl.e.cdelta.y = (fixed)(dpy * wl);
  261.                 pl.width.x = -pl.e.cdelta.y;
  262.                 pl.width.y = pl.e.cdelta.x;
  263.                 pl.thin = 0;    /* if not always_thin, */
  264.                         /* then never thin. */
  265.                }
  266.             else
  267.                {    gs_point dpt;    /* unscaled */
  268.                 float wl;
  269.                 gs_idtransform_inline(pgs,
  270.                           (float)udx, (float)udy, &dpt);
  271.                 wl = line_width_and_scale /
  272.                     hypot(dpt.x, dpt.y);
  273.                 /* Construct the width vector in */
  274.                 /* user space, still unscaled. */
  275.                 dpt.x *= wl;
  276.                 dpt.y *= wl;
  277.                 /*
  278.                  * We now compute both perpendicular
  279.                  * and (optionally) parallel half-widths,
  280.                  * as deltas in device space.  We use
  281.                  * a fixed-point, unscaled version of
  282.                  * gs_dtransform.  The second computation
  283.                  * folds in a 90-degree rotation (in user
  284.                  * space, before transforming) in the
  285.                  * direction that corresponds to clockwise
  286.                  * in device space.
  287.                  */
  288.                 pl.e.cdelta.x = (fixed)(dpt.x * xx);
  289.                 pl.e.cdelta.y = (fixed)(dpt.y * yy);
  290.                 if ( skewed )
  291.                   pl.e.cdelta.x += (fixed)(dpt.y * yx),
  292.                   pl.e.cdelta.y += (fixed)(dpt.x * xy);
  293.                 if ( reflected )
  294.                   dpt.x = -dpt.x, dpt.y = -dpt.y;
  295.                 pl.width.x = (fixed)(dpt.y * xx),
  296.                 pl.width.y = -(fixed)(dpt.x * yy);
  297.                 if ( skewed )
  298.                   pl.width.x -= (fixed)(dpt.x * yx),
  299.                   pl.width.y += (fixed)(dpt.y * xy);
  300.                 pl.thin =
  301.                   any_abs(pl.width.x) + any_abs(pl.width.y) <
  302.                     float2fixed(0.75);
  303.                }
  304.             if ( !pl.thin )
  305.             {    adjust_stroke(&pl, pgs);
  306.                 compute_caps(&pl);
  307.             }
  308.            }
  309.         else            /* always_thin */
  310.             pl.e.cdelta.x = pl.e.cdelta.y = 0,
  311.             pl.width.x = pl.width.y = 0,
  312.             pl.thin = 1;
  313.         if ( first++ == 0 ) pl_first = pl;
  314.         if ( index++ )
  315.         {    code = (*line_proc)(to_path,
  316.                     (psub->closed ? 1 : index - 2),
  317.                     &pl_prev, &pl, pgs);
  318.             if ( code < 0 ) goto exit;
  319.             fill_stroke_path();
  320.         }
  321.         pl_prev = pl;
  322.         x = sx, y = sy;
  323. nd:        pline = (const line_segment *)(pline->next);
  324.         }
  325.        if ( index )
  326.         {    /* If closed, join back to start, else cap */
  327.         code = (*line_proc)(to_path, index - 1, &pl_prev,
  328.                  (psub->closed ? &pl_first : (pl_ptr)0), pgs);
  329.         if ( code < 0 ) goto exit;
  330.         fill_stroke_path();
  331.         }
  332.        psub = (const subpath *)pline;
  333.      }
  334. exit:    if ( to_path == &stroke_path_body )
  335.         gx_path_release(to_path);    /* (only needed if error) */
  336.     if ( dash_count ) gx_path_release(&dpath);
  337.     if ( ppath->curve_count ) gx_path_release(&fpath);
  338.     return code;
  339. }
  340.  
  341. /* ------ Internal routines ------ */
  342.  
  343. /* Adjust the endpoints and width of a stroke segment */
  344. /* to achieve more uniform rendering. */
  345. /* Only o.p, e.p, o.cdelta, and width have been set. */
  346. private void near
  347. adjust_stroke(pl_ptr plp, gs_state *pgs)
  348. {    fixed _ss *pw;
  349.     fixed _ss *pov;
  350.     fixed _ss *pev;
  351.     fixed w2;
  352.     if ( !pgs->stroke_adjust && plp->width.x != 0 && plp->width.y != 0 )
  353.         return;        /* don't adjust */
  354.     if ( any_abs(plp->width.x) < any_abs(plp->width.y) )
  355.     {    /* More horizontal stroke */
  356.         pw = &plp->width.y, pov = &plp->o.p.y, pev = &plp->e.p.y;
  357.     }
  358.     else
  359.     {    /* More vertical stroke */
  360.         pw = &plp->width.x, pov = &plp->o.p.x, pev = &plp->e.p.x;
  361.     }
  362.     /* Round the larger component of the width up or down, */
  363.     /* whichever way produces a result closer to the correct width. */
  364.     /* Note that just rounding the larger component */
  365.     /* may not produce the correct result. */
  366.     w2 = fixed_rounded(*pw << 1);        /* full line width */
  367.     if ( w2 == 0 && *pw != 0 )
  368.     {    /* Make sure thin lines don't disappear. */
  369.         w2 = (*pw < 0 ? -fixed_1 : fixed_1);
  370.     }
  371.     *pw = arith_rshift_1(w2);
  372.     /* Only adjust the endpoints if the line is horizontal or vertical. */
  373.     if ( *pov == *pev )
  374.     {    if ( w2 & fixed_1 )    /* odd width, move to half-pixel */
  375.         {    *pov = *pev = fixed_truncated(*pov) + fixed_half;
  376.         }
  377.         else            /* even width, move to pixel */
  378.         {    *pov = *pev = fixed_rounded(*pov);
  379.         }
  380.     }
  381. }
  382.  
  383. /* Expand a dashed subpath into explicit segments. */
  384. /* The subpath contains no curves. */
  385. private int near
  386. expand_dashes(const subpath *psub, gx_path *ppath, gs_state *pgs)
  387. {    const dash_params *dash = &pgs->line_params->dash;
  388.     const float *pattern = dash->pattern;
  389.     int count, ink_on, index;
  390.     float dist_left;
  391.     fixed x0 = psub->pt.x, y0 = psub->pt.y;
  392.     fixed x, y;
  393.     const segment *pseg;
  394.     int wrap = (dash->init_ink_on && psub->closed ? -1 : 0);
  395.     int drawing = wrap;
  396.     int code;
  397.     if ( (code = gx_path_add_point(ppath, x0, y0)) < 0 )
  398.         return code;
  399.     /* To do the right thing at the beginning of a closed path, */
  400.     /* we have to skip any initial line, and then redo it at */
  401.     /* the end of the path.  Drawing = -1 while skipping, */
  402.     /* 0 while drawing normally, and 1 on the second round. */
  403. top:    count = dash->pattern_size;
  404.     ink_on = dash->init_ink_on;
  405.     index = dash->init_index;
  406.     dist_left = dash->init_dist_left;
  407.     x = x0, y = y0;
  408.     pseg = (const segment *)psub;
  409.     while ( (pseg = pseg->next) != 0 && pseg->type != s_start )
  410.        {    fixed sx = pseg->pt.x, sy = pseg->pt.y;
  411.         fixed udx = sx - x, udy = sy - y;
  412.         float length, dx, dy;
  413.         float dist;
  414.         if ( !(udx | udy) )    /* degenerate */
  415.             dx = 0, dy = 0, length = 0;
  416.         else
  417.            {    gs_point d;
  418.             dx = udx, dy = udy;    /* scaled as fixed */
  419.             gs_idtransform_inline(pgs, dx, dy, &d);
  420.             length = hypot(d.x, d.y) * (1 / (float)int2fixed(1));
  421.            }
  422.         dist = length;
  423.         while ( dist > dist_left )
  424.            {    /* We are using up the dash element */
  425.             float fraction = dist_left / length;
  426.             fixed nx = x + (fixed)(dx * fraction);
  427.             fixed ny = y + (fixed)(dy * fraction);
  428.             if ( ink_on )
  429.                {    if ( drawing >= 0 )
  430.                   code = gx_path_add_line(ppath, nx, ny);
  431.                }
  432.             else
  433.                {    if ( drawing > 0 ) return 0;    /* done */
  434.                 code = gx_path_add_point(ppath, nx, ny);
  435.                 drawing = 0;
  436.                }
  437.             if ( code < 0 ) return code;
  438.             dist -= dist_left;
  439.             ink_on = !ink_on;
  440.             if ( ++index == count ) index = 0;
  441.             dist_left = pattern[index];
  442.             x = nx, y = ny;
  443.            }
  444.         dist_left -= dist;
  445.         /* Handle the last dash of a segment. */
  446.         if ( ink_on )
  447.            {    if ( drawing >= 0 )
  448.               code =
  449.                 (pseg->type == s_line_close && drawing > 0 ?
  450.                  gx_path_close_subpath(ppath) :
  451.                  gx_path_add_line(ppath, sx, sy));
  452.            }
  453.         else
  454.            {    if ( drawing > 0 ) return 0;    /* done */
  455.             code = gx_path_add_point(ppath, sx, sy);
  456.             drawing = 0;
  457.            }
  458.         if ( code < 0 ) return code;
  459.         x = sx, y = sy;
  460.        }
  461.     /* Check for wraparound. */
  462.     if ( wrap && drawing <= 0 )
  463.        {    /* We skipped some initial lines. */
  464.         /* Go back and do them now. */
  465.         drawing = 1;
  466.         goto top;
  467.        }
  468.     return 0;
  469. }
  470.  
  471. /* Compute the intersection of two lines.  This is a messy algorithm */
  472. /* that somehow ought to be useful in more places than just here.... */
  473. private void
  474. line_intersect(
  475.     p_ptr pp1,                /* point on 1st line */
  476.     p_ptr pd1,                /* slope of 1st line (dx,dy) */
  477.     p_ptr pp2,                /* point on 2nd line */
  478.     p_ptr pd2,                /* slope of 2nd line */
  479.     p_ptr pi)                /* return intersection here */
  480. {    /* We don't have to do any scaling, the factors all work out right. */
  481.     float u1 = pd1->x, v1 = pd1->y;
  482.     float u2 = pd2->x, v2 = pd2->y;
  483.     double denom = u1 * v2 - u2 * v1;
  484.     double num1 = v1 * pp1->x - u1 * pp1->y;
  485.     double num2 = v2 * pp2->x - u2 * pp2->y;
  486.     double xnum = u1 * num2 - u2 * num1;
  487.     double ynum = v1 * num2 - v2 * num1;
  488.     double max_result = any_abs(denom) * (double)max_fixed;
  489. #ifdef DEBUG
  490. if ( gs_debug['o'] )
  491.    {    dprintf4("[o]Intersect %f,%f(%f/%f)",
  492.          fixed2float(pp1->x), fixed2float(pp1->y),
  493.          fixed2float(pd1->x), fixed2float(pd1->y));
  494.     dprintf4(" & %f,%f(%f/%f),\n",
  495.          fixed2float(pp2->x), fixed2float(pp2->y),
  496.          fixed2float(pd2->x), fixed2float(pd2->y));
  497.     dprintf4("\txnum=%f ynum=%f denom=%f max_result=%f ->\n",
  498.          xnum, ynum, denom, max_result);
  499.    }
  500. #endif
  501.     /* Check for degenerate result. */
  502.     if ( denom == 0 || any_abs(xnum) > max_result || any_abs(ynum) > max_result )
  503.        {    /* The lines are nearly parallel, */
  504.         /* or one of them has zero length.  Punt. */
  505.         *pi = *pp1;
  506.         if_debug0('o', "\tdegenerate!\n");
  507.        }
  508.     else
  509.        {    pi->x = (fixed)(xnum / denom);
  510.         pi->y = (fixed)(ynum / denom);
  511.         if_debug2('o', "\t%f,%f\n",
  512.               fixed2float(pi->x), fixed2float(pi->y));
  513.        }
  514. }
  515.  
  516. #define lix plp->o.p.x
  517. #define liy plp->o.p.y
  518. #define litox plp->e.p.x
  519. #define litoy plp->e.p.y
  520. #define trsign(v, c) ((v) >= 0 ? (c) : -(c))
  521.  
  522. /* Set up the width and delta parameters for a thin line. */
  523. /* We only approximate the width and height. */
  524. private void near
  525. set_thin_widths(register pl_ptr plp)
  526. {    fixed dx = litox - lix, dy = litoy - liy;
  527.     if ( any_abs(dx) > any_abs(dy) )
  528.     {    plp->width.x = plp->e.cdelta.y = 0;
  529.         plp->width.y = -(plp->e.cdelta.x =
  530.             trsign(dx, -fixed_half));
  531.     }
  532.     else
  533.     {    plp->width.y = plp->e.cdelta.x = 0;
  534.         plp->width.x = -(plp->e.cdelta.y =
  535.             trsign(dy, -fixed_half));
  536.     }
  537. }
  538.  
  539. /* Draw a line on the device. */
  540. private int near
  541. stroke_fill(gx_path *ppath, int first, register pl_ptr plp, pl_ptr nplp,
  542.   gs_state *pgs)
  543. {    if ( plp->thin )
  544.        {    /* Minimum-width line, don't have to be careful. */
  545.         /* We do have to check for the entire line being */
  546.         /* within the clipping rectangle, allowing for some */
  547.         /* slop at the ends. */
  548.         fixed dx = litox - lix, dy = litoy - liy;
  549. #define slop int2fixed(2)
  550.         fixed xslop = trsign(dx, slop);
  551.         fixed yslop = trsign(dy, slop);
  552.         if ( gx_cpath_includes_rectangle(pgs->clip_path,
  553.                 lix - xslop, liy - yslop,
  554.                 litox + xslop, litoy + yslop) )
  555.         {    gp_check_interrupts();
  556.             return gz_draw_line_fixed(lix, liy, litox, litoy,
  557.                 pgs->dev_color, pgs);
  558.         }
  559. #undef slop
  560. #undef trsign
  561.         /* We didn't set up the endpoint parameters before, */
  562.         /* because the line was thin.  Do it now. */
  563.         set_thin_widths(plp);
  564.        }
  565.     /* General case: construct a path for the fill algorithm. */
  566.     return stroke_add(ppath, first, plp, nplp, pgs);
  567. }
  568.  
  569. #undef lix
  570. #undef liy
  571. #undef litox
  572. #undef litoy
  573.  
  574. /* Add a segment to the path.  This handles all the complex cases. */
  575. private int near add_capped(P4(gx_path *, gs_line_cap, int (*)(P3(gx_path *, fixed, fixed)), ep_ptr));
  576. private int near
  577. stroke_add(gx_path *ppath, int first, register pl_ptr plp, pl_ptr nplp,
  578.   gs_state *pgs)
  579. {    int code;
  580.     if ( plp->thin )
  581.        {    /* We didn't set up the endpoint parameters before, */
  582.         /* because the line was thin.  Do it now. */
  583.         set_thin_widths(plp);
  584.         adjust_stroke(plp, pgs);
  585.         compute_caps(plp);
  586.        }
  587.     if ( (code = add_capped(ppath, (first == 0 ? pgs->line_params->cap : gs_cap_butt), gx_path_add_point, &plp->o)) < 0 )
  588.         return code;
  589.     if ( nplp == 0 )
  590.        {    code = add_capped(ppath, pgs->line_params->cap, gx_path_add_line, &plp->e);
  591.        }
  592.     else if ( pgs->line_params->join == gs_join_round )
  593.        {    code = add_capped(ppath, gs_cap_round, gx_path_add_line, &plp->e);
  594.        }
  595.     else if ( nplp->thin )        /* no join */
  596.       {    code = add_capped(ppath, gs_cap_butt, gx_path_add_line, &plp->e);
  597.       }
  598.     else                /* join_bevel or join_miter */
  599.        {    gs_fixed_point jp1, jp2;
  600.         /*
  601.          * Set np to whichever of nplp->o.co or .ce
  602.          * is outside the current line.  We observe that
  603.          * point (x2,y2) is counter-clockwise from (x1,y1)
  604.          * relative to the origin iff x1*y2 < x2*y1.
  605.          * In this case x1,y1 are plp->width,
  606.          * x2,y2 are nplp->width, and the origin is
  607.          * their common point (plp->e.p, nplp->o.p).
  608.          */
  609.         float wx1 = plp->width.x, wy1 = plp->width.y;
  610.         float wx2 = nplp->width.x, wy2 = nplp->width.y;
  611.         int ccw = wx1 * wy2 < wx2 * wy1;
  612.         p_ptr outp, np, np1, np2;
  613.         /* Initialize for a bevel join. */
  614.         jp1.x = plp->e.co.x, jp1.y = plp->e.co.y;
  615.         jp2.x = plp->e.ce.x, jp2.y = plp->e.ce.y;
  616.         if ( ccw )
  617.             outp = &jp2, np2 = np = &nplp->o.co, np1 = &plp->e.p;
  618.         else
  619.             outp = &jp1, np1 = np = &nplp->o.ce, np2 = &plp->e.p;
  620.         if_debug1('o', "[o]use %s\n", (ccw ? "co (ccw)" : "ce (cw)"));
  621.         /* Don't bother with the miter check if the two */
  622.         /* points to be joined are very close together, */
  623.         /* namely, in the same square half-pixel. */
  624.         if ( pgs->line_params->join == gs_join_miter &&
  625.              !(fixed2long(outp->x << 1) == fixed2long(np->x << 1) &&
  626.                fixed2long(outp->y << 1) == fixed2long(np->y << 1))
  627.            )
  628.           { /*
  629.              * Check whether a miter join is appropriate.
  630.              * Let a, b be the angles of the two lines.
  631.              * We check tan(a-b) against the miter_check
  632.              * by using the following formula:
  633.              * If tan(a)=u1/v1 and tan(b)=u2/v2, then
  634.              * tan(a-b) = (u1*v2 - u2*v1) / (u1*u2 + v1*v2).
  635.              * We can do all the computations unscaled,
  636.              * because we're only concerned with ratios.
  637.              */
  638.             float u1 = plp->e.cdelta.x, v1 = plp->e.cdelta.y;
  639.             float u2 = nplp->o.cdelta.x, v2 = nplp->o.cdelta.y;
  640.             float num = u1 * v2 - u2 * v1;
  641.             float denom = u1 * u2 + v1 * v2;
  642.             float check = pgs->line_params->miter_check;
  643.             /*
  644.              * We will want either tan(a-b) or tan(b-a)
  645.              * depending on the orientations of the lines.
  646.              * Fortunately we know the relative orientations already.
  647.              */
  648.             if ( !ccw )        /* have plp - nplp, want vice versa */
  649.             num = -num;
  650. #ifdef DEBUG
  651. if ( gs_debug['o'] )
  652.                    {    dprintf4("[o]Miter check: u1/v1=%f/%f, u2/v2=%f/%f,\n",
  653.                  u1, v1, u2, v2);
  654.                         dprintf3("        num=%f, denom=%f, check=%f\n",
  655.                  num, denom, check);
  656.                    }
  657. #endif
  658.             /* Use a miter if num / denom >= check. */
  659.             /* If check > 0, num < 0 always passes; */
  660.             /* if check < 0, num >= 0 always fails. */
  661.             if ( denom < 0 ) num = -num, denom = -denom;
  662.             if ( check > 0 ?
  663.             (num < 0 || num >= denom * check) :
  664.             (num < 0 && num >= denom * check)
  665.                )
  666.                {    /* OK to use a miter join. */
  667.                 if_debug0('o', "        ... passes.\n");
  668.                 /* Compute the intersection of */
  669.                 /* the extended edge lines. */
  670.                 line_intersect(outp, &plp->e.cdelta, np,
  671.                            &nplp->o.cdelta, outp);
  672.                }
  673.            }
  674.         if ( (code = gx_path_add_line(ppath, jp1.x, jp1.y)) < 0 ||
  675.              (code = gx_path_add_line(ppath, np1->x, np1->y)) < 0 ||
  676.              (code = gx_path_add_line(ppath, np2->x, np2->y)) < 0 ||
  677.              (code = gx_path_add_line(ppath, jp2.x, jp2.y)) < 0
  678.            )
  679.             return code;
  680.        }
  681.     if ( code < 0 || (code = gx_path_close_subpath(ppath)) < 0 )
  682.         return code;
  683.     return 0;
  684. }
  685.  
  686. /* Routines for cap computations */
  687.  
  688. /* Compute the endpoints of the two caps of a segment. */
  689. private void near
  690. compute_caps(register pl_ptr plp)
  691. {    fixed wx2 = plp->width.x;
  692.     fixed wy2 = plp->width.y;
  693.     plp->o.co.x = plp->o.p.x + wx2, plp->o.co.y = plp->o.p.y + wy2;
  694.     plp->o.cdelta.x = -plp->e.cdelta.x,
  695.       plp->o.cdelta.y = -plp->e.cdelta.y;
  696.     plp->o.ce.x = plp->o.p.x - wx2, plp->o.ce.y = plp->o.p.y - wy2;
  697.     plp->e.co.x = plp->e.p.x - wx2, plp->e.co.y = plp->e.p.y - wy2;
  698.     plp->e.ce.x = plp->e.p.x + wx2, plp->e.ce.y = plp->e.p.y + wy2;
  699. #ifdef DEBUG
  700. if ( gs_debug['o'] )
  701.     dprintf4("[o]Stroke o=(%f,%f) e=(%f,%f)\n",
  702.          fixed2float(plp->o.p.x), fixed2float(plp->o.p.y),
  703.          fixed2float(plp->e.p.x), fixed2float(plp->e.p.y)),
  704.     dprintf4("\twxy=(%f,%f) lxy=(%f,%f)\n",
  705.          fixed2float(wx2), fixed2float(wy2),
  706.          fixed2float(plp->e.cdelta.x), fixed2float(plp->e.cdelta.y));
  707. #endif
  708. }
  709.  
  710. /* Add a properly capped line endpoint to the path. */
  711. /* The first point may require either moveto or lineto. */
  712. private int near
  713. add_capped(gx_path *ppath, gs_line_cap type,
  714.   int (*add_proc)(P3(gx_path *, fixed, fixed)), /* gx_path_add_point/line */
  715.   register ep_ptr endp)
  716. {    int code;
  717. #define px endp->p.x
  718. #define py endp->p.y
  719. #define xo endp->co.x
  720. #define yo endp->co.y
  721. #define xe endp->ce.x
  722. #define ye endp->ce.y
  723. #define cdx endp->cdelta.x
  724. #define cdy endp->cdelta.y
  725. #ifdef DEBUG
  726. if ( gs_debug['o'] )
  727.     dprintf4("[o]cap: p=(%g,%g), co=(%g,%g),\n",
  728.          fixed2float(px), fixed2float(py),
  729.          fixed2float(xo), fixed2float(yo)),
  730.     dprintf4("[o]\tce=(%g,%g), cd=(%g,%g)\n",
  731.          fixed2float(xe), fixed2float(ye),
  732.          fixed2float(cdx), fixed2float(cdy));
  733. #endif
  734.     switch ( type )
  735.        {
  736.     case gs_cap_round:
  737.        {    fixed xm = px + cdx;
  738.         fixed ym = py + cdy;
  739.         if (    (code = (*add_proc)(ppath, xo, yo)) < 0 ||
  740.             (code = gx_path_add_arc(ppath, xo, yo, xm, ym,
  741.                 xo + cdx, yo + cdy, quarter_arc_fraction)) < 0 ||
  742.             (code = gx_path_add_arc(ppath, xm, ym, xe, ye,
  743.                 xe + cdx, ye + cdy, quarter_arc_fraction)) < 0
  744.            ) return code;
  745.        }
  746.         break;
  747.     case gs_cap_square:
  748.         if (    (code = (*add_proc)(ppath, xo + cdx, yo + cdy)) < 0 ||
  749.             (code = gx_path_add_line(ppath, xe + cdx, ye + cdy)) < 0
  750.            ) return code;
  751.         break;
  752.     case gs_cap_butt:
  753.         if (    (code = (*add_proc)(ppath, xo, yo)) < 0 ||
  754.             (code = gx_path_add_line(ppath, xe, ye)) < 0
  755.            ) return code;
  756.        }
  757.     return code;
  758. }
  759.