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

  1. /* Copyright (C) 1992, 1993 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. /* gxpcopy.c */
  20. /* Path copying and flattening for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"            /* for gscoord.h */
  25. #include "gscoord.h"
  26. #include "gxfixed.h"
  27. #include "gxarith.h"
  28. #include "gzline.h"
  29. #include "gzpath.h"
  30.  
  31. /* Forward declarations */
  32. private int copy_path(P4(const gx_path *, gx_path *, fixed, bool));
  33. private int flatten_internal(P8(gx_path *,
  34.   fixed, fixed, fixed, fixed, fixed, fixed, fixed));
  35. private int flatten_sample(P8(gx_path *, int,
  36.   fixed, fixed, fixed, fixed, fixed, fixed));
  37.  
  38. /* Copy a path */
  39. int
  40. gx_path_copy(const gx_path *ppath_old, gx_path *ppath, bool init)
  41. {    return copy_path(ppath_old, ppath, max_fixed, init);
  42. }
  43.  
  44. /* Flatten a path. */
  45. int
  46. gx_path_flatten(const gx_path *ppath_old, gx_path *ppath, floatp flatness,
  47.   bool in_BuildCharGlyph)
  48. {    return copy_path(ppath_old, ppath,
  49.              (in_BuildCharGlyph ? fixed_0 : float2fixed(flatness)),
  50.              true);
  51. }
  52.  
  53. /* Add a flattened curve to a path. */
  54. int
  55. gx_path_add_flattened_curve(gx_path *ppath,
  56.   fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3,
  57.   floatp flatness)
  58. {    return flatten_internal(ppath, x1, y1, x2, y2, x3, y3,
  59.                 float2fixed(flatness));
  60. }
  61.  
  62. /* Copy a path, optionally flattening it. */
  63. /* If the copy fails, free the new path. */
  64. private int
  65. copy_path(const gx_path *ppath_old, gx_path *ppath, fixed fixed_flat,
  66.   bool init)
  67. {    gx_path old;
  68.     const segment *pseg;
  69.     int code;
  70. #ifdef DEBUG
  71. if ( gs_debug_c('p') )
  72.     gx_dump_path(ppath_old, "before copy_path");
  73. #endif
  74.     old = *ppath_old;
  75.     if ( init )
  76.         gx_path_init(ppath, ppath_old->memory);
  77.     pseg = (const segment *)(old.first_subpath);
  78.     while ( pseg )
  79.        {    switch ( pseg->type )
  80.            {
  81.         case s_start:
  82.             code = gx_path_add_point(ppath, pseg->pt.x, pseg->pt.y);
  83.             break;
  84.         case s_curve:
  85.            {    curve_segment *pc = (curve_segment *)pseg;
  86.             if ( fixed_flat == max_fixed )    /* don't flatten */
  87.                 code = gx_path_add_curve(ppath,
  88.                     pc->p1.x, pc->p1.y,
  89.                     pc->p2.x, pc->p2.y,
  90.                     pc->pt.x, pc->pt.y);
  91.             else
  92.                 code = flatten_internal(ppath,
  93.                     pc->p1.x, pc->p1.y,
  94.                     pc->p2.x, pc->p2.y,
  95.                     pc->pt.x, pc->pt.y,
  96.                     fixed_flat);
  97.             break;
  98.            }
  99.         case s_line:
  100.             code = gx_path_add_line(ppath, pseg->pt.x, pseg->pt.y);
  101.             break;
  102.         case s_line_close:
  103.             code = gx_path_close_subpath(ppath);
  104.             break;
  105.            }
  106.         if ( code )
  107.            {    gx_path_release(ppath);
  108.             if ( ppath == ppath_old )
  109.                 *ppath = old;
  110.             return code;
  111.            }
  112.         pseg = pseg->next;
  113.     }
  114.     if ( old.subpath_open < 0 )
  115.         gx_path_add_point(ppath, old.position.x, old.position.y);
  116. #ifdef DEBUG
  117. if ( gs_debug_c('p') )
  118.     gx_dump_path(ppath, "after copy_path");
  119. #endif
  120.     return 0;
  121. }
  122.  
  123. /*
  124.  * To calculate how many points to sample along a path in order to
  125.  * approximate it to the desired degree of flatness, we define
  126.  *    dist((x,y)) = abs(x) + abs(y);
  127.  * then the number of points we need is
  128.  *    N = 1 + sqrt(3/4 * D / flatness),
  129.  * where
  130.  *    D = max(dist(p0 - 2*p1 + p2), dist(p1 - 2*p2 + p3)).
  131.  * Since we are going to use a power of 2 for the number of intervals,
  132.  * we can avoid the square root by letting
  133.  *    N = 1 + 2^(ceiling(log2(3/4 * D / flatness) / 2)).
  134.  * (Reference: DEC Paris Research Laboratory report #1, May 1989.)
  135.  *
  136.  * We treat two cases specially.  First, if the curve is very
  137.  * short, we halve the flatness, to avoid turning short shallow curves
  138.  * into short straight lines.  Second, if the curve forms part of a
  139.  * character, or the flatness is less than half a pixel, we let
  140.  *    N = 1 + 2 * max(abs(x3-x0), abs(y3-y0)).
  141.  * This is probably too conservative, but it produces good results.
  142.  */
  143. private int
  144. flatten_internal(gx_path *ppath, fixed x1, fixed y1, fixed x2, fixed y2,
  145.   fixed x3, fixed y3, fixed fixed_flat)
  146. {    const fixed
  147.         x0 = ppath->position.x,
  148.         y0 = ppath->position.y;
  149.     fixed
  150.         x03 = x0 - x3,
  151.         y03 = y0 - y3;
  152.     int k;
  153.     if ( x03 < 0 ) x03 = -x03;
  154.     if ( y03 < 0 ) y03 = -y03;
  155.     if ( (x03 | y03) < int2fixed(16) )
  156.         fixed_flat >>= 1;
  157.     if ( fixed_flat < fixed_half )
  158.     {    /* Use the conservative method. */
  159.         fixed m = max(x03, y03);
  160.         for ( k = 1; m > fixed_1; )
  161.             k++, m >>= 1;
  162.     }
  163.     else
  164.     {    const fixed
  165.             x12 = x1 - x2,
  166.             y12 = y1 - y2,
  167.             dx0 = x0 - x1 - x12,
  168.             dy0 = y0 - y1 - y12,
  169.             dx1 = x12 - x2 + x3,
  170.             dy1 = y12 - y2 + y3,
  171.             adx0 = any_abs(dx0),
  172.             ady0 = any_abs(dy0),
  173.             adx1 = any_abs(dx1),
  174.             ady1 = any_abs(dy1);
  175.         fixed
  176.             d = max(adx0, adx1) + max(ady0, ady1);
  177.         fixed q;
  178.         if_debug6('2', "[2]d01=%g,%g d12=%g,%g d23=%g,%g\n",
  179.               fixed2float(x1 - x0), fixed2float(y1 - y0),
  180.               fixed2float(-x12), fixed2float(-y12),
  181.               fixed2float(x3 - x2), fixed2float(y3 - y2));
  182.         if_debug2('2', "     D=%f, flat=%f,",
  183.               fixed2float(d), fixed2float(fixed_flat));
  184.         d -= d >> 2;            /* 3/4 * D */
  185.         if ( d < (fixed)1 << (sizeof(fixed) * 8 - _fixed_shift - 1) )
  186.             q = (d << _fixed_shift) / fixed_flat;
  187.         else
  188.             q = float2fixed((float)d / fixed_flat);
  189.         /* Now we want to set k = ceiling(log2(q) / 2). */
  190.         for ( k = 0; q > fixed_1; )
  191.             k++, q >>= 2;
  192.         if_debug1('2', " k=%d\n", k);
  193.     }
  194.     return flatten_sample(ppath, k, x1, y1, x2, y2, x3, y3);
  195. }
  196.  
  197. /* Maximum number of points for sampling if we want accurate rasterizing. */
  198. /* 2^(k_sample_max*3)-1 must fit into a uint with a bit to spare. */
  199. #define k_sample_max ((size_of(int) * 8 - 1) / 3)
  200.  
  201. /* Flatten a segment of the path by repeated sampling. */
  202. /* 2^k is the number of lines to produce (i.e., the number of points - 1, */
  203. /* including the endpoints); we require k >= 1. */
  204. /* If k or any of the coefficient values are too large, */
  205. /* use recursive subdivision to whittle them down. */
  206. private int
  207. flatten_sample(gx_path *ppath, int k,
  208.   fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3)
  209. {    fixed x0, y0;
  210.     fixed cx, bx, ax, cy, by, ay;
  211.     fixed ptx, pty;
  212.     fixed x, y;
  213.     /*
  214.      * If all the coefficients lie between min_fast and max_fast,
  215.      * we can do everything in fixed point.  In this case we compute
  216.      * successive values by finite differences, using the formulas:
  217.         x(t) =
  218.           a*t^3 + b*t^2 + c*t + d =>
  219.         dx(t) = x(t+e)-x(t) =
  220.           a*(3*t^2*e + 3*t*e^2 + e^3) + b*(2*t*e + e^2) + c*e =
  221.           (3*a*e)*t^2 + (3*a*e^2 + 2*b*e)*t + (a*e^3 + b*e^2 + c*e) =>
  222.         d2x(t) = dx(t+e)-dx(t) =
  223.           (3*a*e)*(2*t*e + e^2) + (3*a*e^2 + 2*b*e)*e =
  224.           (6*a*e^2)*t + (6*a*e^3 + 2*b*e^2) =>
  225.         d3x(t) = d2x(t+e)-d2x(t) =
  226.           6*a*e^3;
  227.         x(0) = d, dx(0) = (a*e^3 + b*e^2 + c*e),
  228.           d2x(0) = 6*a*e^3 + 2*b*e^2;
  229.      * In these formulas, e = 1/2^k; of course, there are separate
  230.      * computations for the x and y values.
  231.      */
  232.     uint i;
  233.     /*
  234.      * We do exact rational arithmetic to avoid accumulating error.
  235.      * Each quantity is represented as I+R/M, where I is an "integer"
  236.      * and the "remainder" R lies in the range 0 <= R < M=2^(3*k).
  237.      * Note that R may temporarily exceed M; for this reason,
  238.      * we require that M have at least one free high-order bit.
  239.      * To reduce the number of variables, we don't actually compute M,
  240.      * only M-1 (rmask).
  241.      */
  242.     uint rmask;        /* M-1 */
  243.     fixed idx, idy, id2x, id2y, id3x, id3y;        /* I */
  244.     uint rx, ry, rdx, rdy, rd2x, rd2y, rd3x, rd3y;    /* R */
  245.  
  246. top:    x0 = ppath->position.x;
  247.     y0 = ppath->position.y;
  248. #ifdef DEBUG
  249. if ( gs_debug_c('3') )
  250.     dprintf4("[3]x0=%f y0=%f x1=%f y1=%f\n",
  251.          fixed2float(x0), fixed2float(y0),
  252.          fixed2float(x1), fixed2float(y1)),
  253.     dprintf4("   x2=%f y2=%f x3=%f y3=%f\n",
  254.          fixed2float(x2), fixed2float(y2),
  255.          fixed2float(x3), fixed2float(y3));
  256. #endif
  257.     {    /* We spell out some multiplies by 3, */
  258.         /* for the benefit of compilers that don't optimize this. */
  259.         fixed x01, x12, y01, y12;
  260.         x01 = x1 - x0;
  261.         cx = (x01 << 1) + x01;        /* 3*(x1-x0) */
  262.         x12 = x2 - x1;
  263.         bx = (x12 << 1) + x12 - cx;    /* 3*(x2-2*x1+x0) */
  264.         ax = x3 - bx - cx - x0;        /* x3-3*x2+3*x1-x0 */
  265.         y01 = y1 - y0;
  266.         cy = (y01 << 1) + y01;
  267.         y12 = y2 - y1;
  268.         by = (y12 << 1) + y12 - cy;
  269.         ay = y3 - by - cy - y0;
  270.     }
  271.  
  272.     if_debug6('3', "[3]ax=%f bx=%f cx=%f\n   ay=%f by=%f cy=%f\n",
  273.           fixed2float(ax), fixed2float(bx), fixed2float(cx),
  274.           fixed2float(ay), fixed2float(by), fixed2float(cy));
  275. #define max_fast (max_fixed / 6)
  276. #define min_fast (-max_fast)
  277. #define in_range(v) (v < max_fast && v > min_fast)
  278.     if ( k == 0 )
  279.     {    /* The curve is very short, or anomalous in some way. */
  280.         /* Just add a line and exit. */
  281.         return gx_path_add_line(ppath, x3, y3);
  282.     }
  283.     if ( k <= (sizeof(int) * 8 - 1) / 3 &&    /* so M fits */
  284.          in_range(ax) && in_range(ay) &&
  285.          in_range(bx) && in_range(by) &&
  286.          in_range(cx) && in_range(cy)
  287.        )
  288.     {    x = x0, y = y0;
  289.         rx = ry = 0;
  290.         /* Fast check for n == 3, a common special case */
  291.         /* for small characters. */
  292.         if ( k == 1 )
  293.         {
  294. #define poly2(a,b,c)\
  295.   arith_rshift_1(arith_rshift_1(arith_rshift_1(a) + b) + c)
  296.             idx = poly2(ax, bx, cx),
  297.             idy = poly2(ay, by, cy),
  298.             rdx = rdy = 0;
  299.             rmask = 7;        /* for accum check */
  300. #undef poly2
  301.         }
  302.         else
  303.         {    fixed bx2 = bx << 1, by2 = by << 1;
  304.             fixed ax6 = ((ax << 1) + ax) << 1,
  305.                   ay6 = ((ay << 1) + ay) << 1;
  306. #define adjust_rem(r, q)\
  307.   if ( r > rmask ) q ++, r &= rmask
  308.             const int k2 = k << 1;
  309.             const int k3 = k2 + k;
  310.             rmask = (1 << k3) - 1;
  311.             /* We can compute all the remainders as ints, */
  312.             /* because we know they don't exceed M. */
  313.             /* cx/y terms */
  314.             idx = arith_rshift(cx, k),
  315.               idy = arith_rshift(cy, k);
  316.             rdx = ((uint)cx << k2) & rmask,
  317.               rdy = ((uint)cy << k2) & rmask;
  318.             /* bx/y terms */
  319.             id2x = arith_rshift(bx2, k2),
  320.               id2y = arith_rshift(by2, k2);
  321.             rd2x = ((uint)bx2 << k) & rmask,
  322.               rd2y = ((uint)by2 << k) & rmask;
  323.             idx += arith_rshift_1(id2x),
  324.               idy += arith_rshift_1(id2y);
  325.             rdx += ((uint)bx << k) & rmask,
  326.               rdy += ((uint)by << k) & rmask;
  327.             adjust_rem(rdx, idx);
  328.             adjust_rem(rdy, idy);
  329.             /* ax/y terms */
  330.             idx += arith_rshift(ax, k3),
  331.               idy += arith_rshift(ay, k3);
  332.             rdx += (uint)ax & rmask,
  333.               rdy += (uint)ay & rmask;
  334.             adjust_rem(rdx, idx);
  335.             adjust_rem(rdy, idy);
  336.             id2x += id3x = arith_rshift(ax6, k3),
  337.               id2y += id3y = arith_rshift(ay6, k3);
  338.             rd2x += rd3x = (uint)ax6 & rmask,
  339.               rd2y += rd3y = (uint)ay6 & rmask;
  340.             adjust_rem(rd2x, id2x);
  341.             adjust_rem(rd2y, id2y);
  342. #undef adjust_rem
  343.         }
  344.     }
  345.     else
  346.     {    /* Curve is too long.  Break into two pieces and recur. */
  347.         /* Algorithm is from "The Beta2-split: A special case of */
  348.         /* the Beta-spline Curve and Surface Representation," */
  349.         /* B. A. Barsky and A. D. DeRose, IEEE, 1985, */
  350.         /* courtesy of Crispin Goswell. */
  351.  
  352.         /* We have to define midpoint carefully to avoid overflow. */
  353.         /* (If it overflows, something really pathological is going */
  354.         /* on, but we could get infinite recursion that way....) */
  355. #define midpoint(a,b)\
  356.   (arith_rshift_1(a) + arith_rshift_1(b) + ((a) & (b) & 1))
  357.         k--;
  358.         {    fixed x0 = ppath->position.x, y0 = ppath->position.y;
  359.             fixed x01 = midpoint(x0, x1), y01 = midpoint(y0, y1);
  360.             fixed x12 = midpoint(x1, x2), y12 = midpoint(y1, y2);
  361.             fixed x02 = midpoint(x01, x12), y02 = midpoint(y01, y12);
  362.             int code;
  363.             /* Update x/y1 and x/y2 now for the second half. */
  364.             x2 = midpoint(x2, x3), y2 = midpoint(y2, y3);
  365.             x1 = midpoint(x12, x2), y1 = midpoint(y12, y2);
  366.             code = flatten_sample(ppath, k, x01, y01, x02, y02,
  367.                     midpoint(x02, x1), midpoint(y02, y1));
  368.             if ( code < 0 ) return code;
  369.         }
  370.         goto top;
  371.     }
  372.     if_debug1('2', "[2]sampling k=%d\n", k);
  373.     ptx = x0, pty = y0;
  374.     for ( i = (1 << k) - 1; ; )
  375.     {    int code;
  376. #ifdef DEBUG
  377. if ( gs_debug_c('3') )
  378.         dprintf4("[3]dx=%f+%d, dy=%f+%d\n",
  379.              fixed2float(idx), rdx,
  380.              fixed2float(idy), rdy),
  381.         dprintf4("   d2x=%f+%d, d2y=%f+%d\n",
  382.              fixed2float(id2x), rd2x,
  383.              fixed2float(id2y), rd2y),
  384.         dprintf4("   d3x=%f+%d, d3y=%f+%d\n",
  385.              fixed2float(id3x), rd3x,
  386.              fixed2float(id3y), rd3y);
  387. #endif
  388. #define accum(i, r, di, dr)\
  389.   if ( (r += dr) > rmask ) r &= rmask, i += di + 1;\
  390.   else i += di
  391.         accum(x, rx, idx, rdx);
  392.         accum(y, ry, idy, rdy);
  393.         if_debug3('3', "[3]%s x=%g, y=%g\n",
  394.               (((x ^ ptx) | (y ^ pty)) & float2fixed(-0.5) ?
  395.                "add" : "skip"),
  396.               fixed2float(x), fixed2float(y));
  397.         /* Skip very short segments */
  398.         if ( ((x ^ ptx) | (y ^ pty)) & float2fixed(-0.5) )
  399.         {    if ( (code = gx_path_add_line(ppath, x, y)) < 0 )
  400.                 return code;
  401.             ptx = x, pty = y;
  402.         }
  403.         if ( --i == 0 )
  404.             break;        /* don't bother with last accum */
  405.         accum(idx, rdx, id2x, rd2x);
  406.         accum(id2x, rd2x, id3x, rd3x);
  407.         accum(idy, rdy, id2y, rd2y);
  408.         accum(id2y, rd2y, id3y, rd3y);
  409. #undef accum
  410.     }
  411.     if_debug2('3', "[3]last x=%g, y=%g\n",
  412.           fixed2float(x3), fixed2float(y3));
  413.     return gx_path_add_line(ppath, x3, y3);
  414. }
  415.  
  416. /*
  417.  *    The rest of this file is an analysis that will eventually
  418.  *    allow us to rasterize curves on the fly, by finding points
  419.  *    where Y reaches a local maximum or minimum, which allows us to
  420.  *    divide the curve into locally Y-monotonic sections.
  421.  */
  422.  
  423. /*
  424.     Let y(t) = a*t^3 + b*t^2 + c*t + d, 0 <= t <= 1.
  425.     Then dy(t) = 3*a*t^2 + 2*b*t + c.
  426.     y(t) has a local minimum or maximum (or inflection point)
  427.     precisely where dy(t) = 0.  Now the roots of dy(t) are
  428.         ( -2*b +/- sqrt(4*b^2 - 12*a*c) ) / 6*a
  429.        =    ( -b +/- sqrt(b*2 - 3*a*c) ) / 3*a
  430.     (Note that real roots exist iff b^2 >= 3*a*c.)
  431.     We want to know if these lie in the range (0..1).
  432.     (We don't care about the endpoints.)  Call such a root
  433.     a "valid zero."  Since computing the roots is expensive, we would
  434.     like to have a cheap a priori test as to whether they exist.
  435.     We proceed as follows:
  436.         If sign(3*a + 2*b + c) ~= sign(c), a valid zero exists,
  437.           since dy(0) and dy(1) have opposite signs and hence
  438.           dy(t) must be zero somewhere in the interval [0..1].
  439.         If sign(a) = sign(b), no valid zero exists,
  440.           since dy is monotonic on [0..1] and has the same sign
  441.           at both endpoints.
  442.     Otherwise, dy(t) may be non-monotonic on [0..1]; it has valid zeros
  443.       iff there is an extremum in this interval and the extremum
  444.       is of the opposite sign from c.
  445.     To find this out, we look for the local extremum of dy(t) by observing
  446.         d2y(t) = 6*a*t + 2*b
  447.     which has a zero only at
  448.         t1 = -b / 3*a
  449.     Now if t1 <= 0 or t1 >= 1, no valid zero exists.  Otherwise,
  450.     we compute
  451.         dy(t1) = c - (b^2 / 3*a)
  452.     Then a valid zero exists (at t1) iff sign(dy(t1)) ~= sign(c).
  453.  */
  454.  
  455. /* Expand a dashed path into explicit segments. */
  456. /* The path contains no curves. */
  457. private int subpath_expand_dashes(P3(const subpath *, gx_path *, gs_state *));
  458. int
  459. gx_path_expand_dashes(const gx_path *ppath_old, gx_path *ppath, gs_state *pgs)
  460. {    const subpath *psub;
  461.     if ( gs_currentdash_length(pgs) == 0 )
  462.       return gx_path_copy(ppath_old, ppath, true);
  463.     gx_path_init(ppath, ppath_old->memory);
  464.     for ( psub = ppath_old->first_subpath; psub != 0;
  465.           psub = (const subpath *)psub->last->next
  466.         )
  467.       {    int code = subpath_expand_dashes(psub, ppath, pgs);
  468.         if ( code < 0 )
  469.           {    gx_path_release(ppath);
  470.             return code;
  471.           }
  472.       }
  473.     return 0;
  474. }
  475. private int
  476. subpath_expand_dashes(const subpath *psub, gx_path *ppath, gs_state *pgs)
  477. {    const gx_dash_params *dash = &gs_currentlineparams(pgs)->dash;
  478.     const float *pattern = dash->pattern;
  479.     int count, ink_on, index;
  480.     float dist_left;
  481.     fixed x0 = psub->pt.x, y0 = psub->pt.y;
  482.     fixed x, y;
  483.     const segment *pseg;
  484.     int wrap = (dash->init_ink_on && psub->is_closed ? -1 : 0);
  485.     int drawing = wrap;
  486.     int code;
  487.     if ( (code = gx_path_add_point(ppath, x0, y0)) < 0 )
  488.         return code;
  489.     /* To do the right thing at the beginning of a closed path, */
  490.     /* we have to skip any initial line, and then redo it at */
  491.     /* the end of the path.  Drawing = -1 while skipping, */
  492.     /* 0 while drawing normally, and 1 on the second round. */
  493. top:    count = dash->pattern_size;
  494.     ink_on = dash->init_ink_on;
  495.     index = dash->init_index;
  496.     dist_left = dash->init_dist_left;
  497.     x = x0, y = y0;
  498.     pseg = (const segment *)psub;
  499.     while ( (pseg = pseg->next) != 0 && pseg->type != s_start )
  500.        {    fixed sx = pseg->pt.x, sy = pseg->pt.y;
  501.         fixed udx = sx - x, udy = sy - y;
  502.         float length, dx, dy;
  503.         float dist;
  504.         if ( !(udx | udy) )    /* degenerate */
  505.             dx = 0, dy = 0, length = 0;
  506.         else
  507.            {    gs_point d;
  508.             dx = udx, dy = udy;    /* scaled as fixed */
  509.             gs_idtransform(pgs, dx, dy, &d);
  510.             length = hypot(d.x, d.y) * (1 / (float)int2fixed(1));
  511.            }
  512.         dist = length;
  513.         while ( dist > dist_left )
  514.            {    /* We are using up the dash element */
  515.             float fraction = dist_left / length;
  516.             fixed nx = x + (fixed)(dx * fraction);
  517.             fixed ny = y + (fixed)(dy * fraction);
  518.             if ( ink_on )
  519.                {    if ( drawing >= 0 )
  520.                   code = gx_path_add_line(ppath, nx, ny);
  521.                }
  522.             else
  523.                {    if ( drawing > 0 ) return 0;    /* done */
  524.                 code = gx_path_add_point(ppath, nx, ny);
  525.                 drawing = 0;
  526.                }
  527.             if ( code < 0 ) return code;
  528.             dist -= dist_left;
  529.             ink_on = !ink_on;
  530.             if ( ++index == count ) index = 0;
  531.             dist_left = pattern[index];
  532.             x = nx, y = ny;
  533.            }
  534.         dist_left -= dist;
  535.         /* Handle the last dash of a segment. */
  536.         if ( ink_on )
  537.            {    if ( drawing >= 0 )
  538.               code =
  539.                 (pseg->type == s_line_close && drawing > 0 ?
  540.                  gx_path_close_subpath(ppath) :
  541.                  gx_path_add_line(ppath, sx, sy));
  542.            }
  543.         else
  544.            {    if ( drawing > 0 ) return 0;    /* done */
  545.             code = gx_path_add_point(ppath, sx, sy);
  546.             drawing = 0;
  547.            }
  548.         if ( code < 0 ) return code;
  549.         x = sx, y = sy;
  550.        }
  551.     /* Check for wraparound. */
  552.     if ( wrap && drawing <= 0 )
  553.        {    /* We skipped some initial lines. */
  554.         /* Go back and do them now. */
  555.         drawing = 1;
  556.         goto top;
  557.        }
  558.     return 0;
  559. }
  560.