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

  1. /* Copyright (C) 1989, 1992, 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. /* gspath.c */
  20. /* Path construction routines for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gscoord.h"            /* requires gsmatrix.h */
  27. #include "gxpath.h"
  28. #include "gzstate.h"
  29.  
  30. /* Conversion parameters */
  31. #define degrees_to_radians (M_PI / 180.0)
  32.  
  33. /* ------ Miscellaneous ------ */
  34.  
  35. int
  36. gs_newpath(gs_state *pgs)
  37. {    gx_path_release(pgs->path);
  38.     gx_path_init(pgs->path, pgs->memory);
  39.     return 0;
  40. }
  41.  
  42. int
  43. gs_closepath(gs_state *pgs)
  44. {    return gx_path_close_subpath(pgs->path);
  45. }
  46.  
  47. int
  48. gs_upmergepath(gs_state *pgs)
  49. {    return gx_path_add_path(pgs->saved->path, pgs->path);
  50. }
  51.  
  52. /* ------ Points and lines ------ */
  53.  
  54. int
  55. gs_currentpoint(const gs_state *pgs, gs_point *ppt)
  56. {    gs_fixed_point pt;
  57.     int code = gx_path_current_point(pgs->path, &pt);
  58.     if ( code < 0 ) return code;
  59.     return gs_itransform((gs_state *)pgs,
  60.                  fixed2float(pt.x), fixed2float(pt.y), ppt);
  61. }
  62.  
  63. int
  64. gs_moveto(gs_state *pgs, floatp x, floatp y)
  65. {    int code;
  66.     gs_fixed_point pt;
  67.     if ( (code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) >= 0 )
  68.         code = gx_path_add_point(pgs->path, pt.x, pt.y);
  69.     return code;
  70. }
  71.  
  72. int
  73. gs_rmoveto(gs_state *pgs, floatp x, floatp y)
  74. {    int code;
  75.     gs_fixed_point dpt;
  76.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) >= 0 )
  77.         code = gx_path_add_relative_point(pgs->path, dpt.x, dpt.y);
  78.     return code;
  79. }
  80.  
  81. int
  82. gs_lineto(gs_state *pgs, floatp x, floatp y)
  83. {    int code;
  84.     gs_fixed_point pt;
  85.     if ( (code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) >= 0 )
  86.         code = gx_path_add_line(pgs->path, pt.x, pt.y);
  87.     return code;
  88. }
  89.  
  90. int
  91. gs_rlineto(gs_state *pgs, floatp x, floatp y)
  92. {    gs_fixed_point cpt, dpt;
  93.     int code = gx_path_current_point(pgs->path, &cpt);
  94.     if ( code < 0 ) return code;
  95.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) >= 0 )
  96.         code = gx_path_add_line(pgs->path, cpt.x + dpt.x, cpt.y + dpt.y);
  97.     return code;
  98. }
  99.  
  100. /* ------ Arcs ------ */
  101.  
  102. /* Forward declarations */
  103. /*
  104.  * Because of an obscure bug in the IBM RS/6000 compiler, the int argument
  105.  * for arc_either and arc_add must come before the floatp arguments.
  106.  */
  107. private int arc_either(P7(gs_state *, int,
  108.   floatp, floatp, floatp, floatp, floatp));
  109. private int arc_add(P9(gs_state *, int,
  110.   floatp, floatp, floatp, floatp, floatp, floatp, floatp));
  111.  
  112. int
  113. gs_arc(gs_state *pgs,
  114.   floatp xc, floatp yc, floatp r, floatp ang1, floatp ang2)
  115. {    return arc_either(pgs, 0, xc, yc, r, ang1, ang2);
  116. }
  117.  
  118. int
  119. gs_arcn(gs_state *pgs,
  120.   floatp xc, floatp yc, floatp r, floatp ang1, floatp ang2)
  121. {    return arc_either(pgs, 1, xc, yc, r, ang1, ang2);
  122. }
  123.  
  124. private int
  125. arc_either(gs_state *pgs, int clockwise,
  126.   floatp axc, floatp ayc, floatp arad, floatp aang1, floatp aang2)
  127. {    float ar = arad;
  128.     fixed ang1 = float2fixed(aang1), ang2 = float2fixed(aang2), adiff;
  129.     float ang1r;
  130.     float x0, y0, sin0, cos0;
  131.     float x3r, y3r;
  132.     int first = 1;
  133.     int code;
  134.     if ( ar < 0 )
  135.        {    ang1 += int2fixed(180);
  136.         ang2 += int2fixed(180);
  137.         ar = - ar;
  138.        }
  139. #define fixed90 int2fixed(90)
  140. #define fixed360 int2fixed(360)
  141.     ang1r = fixed2float(ang1 % fixed360) * degrees_to_radians;
  142.     sin0 = ar * sin(ang1r), cos0 = ar * cos(ang1r);
  143.     x0 = axc + cos0, y0 = ayc + sin0;
  144.     if ( clockwise )
  145.        {    /* Quadrant reduction */
  146.         while ( ang1 < ang2 ) ang2 -= fixed360;
  147.         while ( (adiff = ang2 - ang1) < -fixed90 )
  148.            {    float w = sin0; sin0 = -cos0; cos0 = w;
  149.             x3r = axc + cos0, y3r = ayc + sin0;
  150.             code = arc_add(pgs, first, ar, x0, y0, x3r, y3r,
  151.                 (x0 + cos0),
  152.                 (y0 + sin0));
  153.             if ( code < 0 ) return code;
  154.             x0 = x3r, y0 = y3r;
  155.             ang1 -= fixed90;
  156.             first = 0;
  157.            }
  158.        }
  159.     else
  160.        {    /* Quadrant reduction */
  161.         while ( ang2 < ang1 ) ang2 += fixed360;
  162.         while ( (adiff = ang2 - ang1) > fixed90 )
  163.            {    float w = cos0; cos0 = -sin0; sin0 = w;
  164.             x3r = axc + cos0, y3r = ayc + sin0;
  165.             code = arc_add(pgs, first, ar, x0, y0, x3r, y3r,
  166.                 (x0 + cos0),
  167.                 (y0 + sin0));
  168.             if ( code < 0 ) return code;
  169.             x0 = x3r, y0 = y3r;
  170.             ang1 += fixed90;
  171.             first = 0;
  172.            }
  173.        }
  174.     /* Compute the intersection of the tangents. */
  175.     /* We define xt and yt as separate variables to work around */
  176.     /* a floating point bug in one of the SPARC compilers. */
  177.        {    double trad =
  178.           tan(fixed2float(adiff) * (degrees_to_radians / 2));
  179.         double ang2r = fixed2float(ang2) * degrees_to_radians;
  180.         double xt = x0 - trad * sin0, yt = y0 + trad * cos0;
  181.         code = arc_add(pgs, first, ar, x0, y0,
  182.                    (axc + ar * cos(ang2r)),
  183.                    (ayc + ar * sin(ang2r)),
  184.                    xt, yt);
  185.        }
  186.     return code;
  187. }
  188.  
  189. int
  190. gs_arcto(gs_state *pgs,
  191.   floatp ax1, floatp ay1, floatp ax2, floatp ay2, floatp arad,
  192.   float *retxy)            /* float retxy[4] */
  193. {    float xt0, yt0, xt2, yt2;
  194.     gs_point up0;
  195. #define ax0 up0.x
  196. #define ay0 up0.y
  197.     int code;
  198.     if ( arad < 0 )
  199.         return_error(gs_error_undefinedresult);
  200.     /* Transform the current point back into user coordinates */
  201.     if ( (code = gs_currentpoint(pgs, &up0)) < 0 ) return code;
  202.        {    /* Now we have to compute the tangent points. */
  203.         /* Basically, the idea is to compute the tangent */
  204.         /* of the bisector by using tan(x+y) and tan(z/2) */
  205.         /* formulas, without ever using any trig. */
  206.         float dx0 = ax0 - ax1, dy0 = ay0 - ay1;
  207.         float dx2 = ax2 - ax1, dy2 = ay2 - ay1;
  208.         /* Compute the squared lengths from p1 to p0 and p2. */
  209.         double sql0 = dx0 * dx0 + dy0 * dy0;
  210.         double sql2 = dx2 * dx2 + dy2 * dy2;
  211.         /* Compute the distance from p1 to the tangent points. */
  212.         /* This is the only hairy part. */
  213.         double num = dy0 * dx2 - dy2 * dx0;
  214.         double denom = sqrt(sql0 * sql2) - (dx0 * dx2 + dy0 * dy2);
  215.         /* Check for collinear points. */
  216.         if ( fabs(num) < 1.0e-6 || fabs(denom) < 1.0e-6 )
  217.            {    gs_fixed_point pt;
  218.             code = gs_point_transform2fixed(&pgs->ctm, ax1, ay1, &pt);
  219.             if ( code >= 0 ) code = gx_path_add_line(pgs->path, pt.x, pt.y);
  220.             xt0 = xt2 = ax1;
  221.             yt0 = yt2 = ay1;
  222.            }
  223.         else        /* not collinear */
  224.            {    double dist = fabs(arad * num / denom);
  225.             double l0 = dist / sqrt(sql0), l2 = dist / sqrt(sql2);
  226.             xt0 = ax1 + dx0 * l0;
  227.             yt0 = ay1 + dy0 * l0;
  228.             xt2 = ax1 + dx2 * l2;
  229.             yt2 = ay1 + dy2 * l2;
  230.             code = arc_add(pgs, 1, arad, xt0, yt0, xt2, yt2, ax1, ay1);
  231.            }
  232.        }
  233.     if ( retxy != 0 )
  234.        {    retxy[0] = xt0;
  235.         retxy[1] = yt0;
  236.         retxy[2] = xt2;
  237.         retxy[3] = yt2;
  238.        }
  239.     return code;
  240. }
  241.  
  242. /* Internal routine for adding an arc to the path. */
  243. private int
  244. arc_add(gs_state *pgs, int first,
  245.   floatp r, floatp x0, floatp y0, floatp x3, floatp y3, floatp xt, floatp yt)
  246. {    gx_path *path = pgs->path;
  247.     floatp dx = xt - x0, dy = yt - y0;
  248.     floatp fraction;
  249.     gs_fixed_point p0, p3, pt, cpt;
  250.     int code;
  251.     /* Compute the fraction coefficient for the curve. */
  252.     /* See gx_path_add_arc for details. */
  253.     if ( fabs(r) < 1.0e-4 )        /* almost zero radius */
  254.        {    fraction = 0.0;
  255.        }
  256.     else
  257.        {    double ratio2 = (dx * dx + dy * dy) / (r * r);
  258.         fraction = (4.0/3.0) / (1 + sqrt(1 + ratio2));
  259.        }
  260.     if_debug8('r',
  261.           "[r]Arc f=%f p0=(%f,%f) pt=(%f,%f) p3=(%f,%f) first=%d\n",
  262.           fraction, x0, y0, xt, yt, x3, y3, first);
  263.     if (    (code = gs_point_transform2fixed(&pgs->ctm, x0, y0, &p0)) < 0 ||
  264.         (code = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3)) < 0 ||
  265.         (code = gs_point_transform2fixed(&pgs->ctm, xt, yt, &pt)) < 0 ||
  266.         (first && (code = (gx_path_current_point(path, &cpt) >= 0 ?
  267.              gx_path_add_line(path, p0.x, p0.y) :
  268.              gx_path_add_point(path, p0.x, p0.y))) < 0)
  269.        )
  270.         return code;
  271.     return gx_path_add_arc(path, p0.x, p0.y, p3.x, p3.y, pt.x, pt.y, fraction);
  272. }
  273.  
  274. /* ------ Curves ------ */
  275.  
  276. int
  277. gs_curveto(gs_state *pgs,
  278.   floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3)
  279. {    gs_fixed_point p1, p2, p3;
  280.     int code;
  281.     if (    (code = gs_point_transform2fixed(&pgs->ctm, x1, y1, &p1)) < 0 ||
  282.         (code = gs_point_transform2fixed(&pgs->ctm, x2, y2, &p2)) < 0 ||
  283.         (code = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3)) < 0
  284.        ) return code;
  285.     return gx_path_add_curve(pgs->path,
  286.         p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
  287. }
  288.  
  289. int
  290. gs_rcurveto(gs_state *pgs,
  291.   floatp dx1, floatp dy1, floatp dx2, floatp dy2, floatp dx3, floatp dy3)
  292. {    gs_fixed_point pt, p1, p2, p3;
  293.     int code = gx_path_current_point(pgs->path, &pt);
  294.     if ( code < 0 ) return code;
  295.     if (    (code = gs_distance_transform2fixed(&pgs->ctm, dx1, dy1, &p1)) < 0 ||
  296.         (code = gs_distance_transform2fixed(&pgs->ctm, dx2, dy2, &p2)) < 0 ||
  297.         (code = gs_distance_transform2fixed(&pgs->ctm, dx3, dy3, &p3)) < 0
  298.        ) return code;
  299.     return gx_path_add_curve(pgs->path,
  300.         pt.x + p1.x, pt.y + p1.y,
  301.         pt.x + p2.x, pt.y + p2.y,
  302.         pt.x + p3.x, pt.y + p3.y);
  303. }
  304.