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 / GXPATH.C < prev    next >
C/C++ Source or Header  |  1992-07-14  |  13KB  |  416 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. /* gxpath.c */
  21. /* Internal path construction routines for Ghostscript library */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gzpath.h"
  26.  
  27. /* These routines all assume that all points are */
  28. /* already in device coordinates, and in fixed representation. */
  29. /* As usual, they return either 0 or a (negative) error code. */
  30.  
  31. /* Forward references */
  32. private subpath *path_alloc_copy(P1(gx_path *));
  33. private int gx_path_new_subpath(P1(gx_path *));
  34. #ifdef DEBUG
  35. void gx_print_segment(P1(const segment *));
  36. #endif
  37.  
  38. /* ------ Initialize/free paths ------ */
  39.  
  40. /* Initialize a path */
  41. void
  42. gx_path_init(register gx_path *ppath, const gs_memory_procs *pprocs)
  43. {    ppath->memory_procs = *pprocs;
  44.     ppath->box_last = 0;
  45.     ppath->position_valid = 0;
  46.     ppath->first_subpath = ppath->current_subpath = 0;
  47.     ppath->subpath_count = 0;
  48.     ppath->curve_count = 0;
  49.     ppath->subpath_open = 0;
  50.     ppath->shares_segments = 0;
  51. }
  52.  
  53. /* Release the contents of a path.  We do this in reverse order */
  54. /* so as to maximize LIFO allocator behavior. */
  55. void
  56. gx_path_release(gx_path *ppath)
  57. {    segment *pseg;
  58.     if ( ppath->first_subpath == 0 ) return;    /* empty path */
  59.     if ( ppath->shares_segments ) return;    /* segments are shared */
  60.     pseg = (segment *)ppath->current_subpath->last;
  61.     while ( pseg )
  62.        {    segment *prev = pseg->prev;
  63.         static uint sizes[] = { segment_type_sizes };
  64. #ifdef DEBUG
  65. if ( gs_debug['p'] )
  66.         dprintf("[p]release"), gx_print_segment(pseg);
  67. #endif
  68.         (*ppath->memory_procs.free)((char *)pseg, 1, sizes[(int)pseg->type], "gx_path_release");
  69.         pseg = prev;
  70.        }
  71.     ppath->first_subpath = 0;    /* prevent re-release */
  72. }
  73.  
  74. /* Mark a path as shared */
  75. void
  76. gx_path_share(gx_path *ppath)
  77. {    if ( ppath->first_subpath ) ppath->shares_segments = 1;
  78. }
  79.  
  80. /* ------ Incremental path building ------ */
  81.  
  82. /* Macro for opening the current subpath. */
  83. /* ppath points to the path; psub has been set to ppath->current_subpath. */
  84. #define path_open()\
  85.     if ( !ppath->subpath_open )\
  86.        {    int code;\
  87.         if ( !ppath->position_valid )\
  88.           return_error(gs_error_nocurrentpoint);\
  89.         code = gx_path_new_subpath(ppath);\
  90.         if ( code < 0 ) return code;\
  91.         psub = ppath->current_subpath;\
  92.        }
  93.  
  94. /* Macros for allocating path segments. */
  95. /* Note that they assume that ppath points to the path, */
  96. /* and that psub points to the current subpath. */
  97. /* We have to split the macro into two because of limitations */
  98. /* on the size of a single statement (sigh). */
  99. #define p_alloc(pseg,size)\
  100.   if_debug2('A', "[p]%lx<%u>\n", (ulong)pseg, size)
  101. #define path_unshare()\
  102.   if(ppath->shares_segments)\
  103.     if(!(psub = path_alloc_copy(ppath)))return_error(gs_error_limitcheck)
  104. #define path_alloc_segment(pseg,ctype,stype,cname)\
  105.   path_unshare();\
  106.   if( !(pseg = (ctype *)(*ppath->memory_procs.alloc)(1, sizeof(ctype), cname)) )\
  107.     return_error(gs_error_limitcheck);\
  108.   p_alloc((char *)pseg, sizeof(ctype));\
  109.   pseg->type = stype, pseg->next = 0
  110. #define path_alloc_link(pseg)\
  111.   { segment *prev = psub->last;\
  112.     prev->next = (segment *)pseg;\
  113.     pseg->prev = prev;\
  114.     psub->last = (segment *)pseg;\
  115.   }
  116.  
  117. /* Open a new subpath */
  118. private int
  119. gx_path_new_subpath(gx_path *ppath)
  120. {    subpath *psub = ppath->current_subpath;
  121.     register subpath *spp;
  122.     path_alloc_segment(spp, subpath, s_start, "gx_path_new_subpath");
  123.     spp->last = (segment *)spp;
  124.     spp->curve_count = 0;
  125.     spp->closed = 0;
  126.     spp->pt = ppath->position;
  127.     ppath->subpath_open = 1;
  128.     if ( !psub )            /* first subpath */
  129.        {    ppath->first_subpath = spp;
  130.         spp->prev = 0;
  131.        }
  132.     else
  133.        {    segment *prev = psub->last;
  134.         prev->next = (segment *)spp;
  135.         spp->prev = prev;
  136.        }
  137.     ppath->current_subpath = spp;
  138.     ppath->subpath_count++;
  139. #ifdef DEBUG
  140. if ( gs_debug['p'] )
  141.     dprintf("[p]"), gx_print_segment((const segment *)spp);
  142. #endif
  143.     return 0;
  144. }
  145.  
  146. /* Add a point to the current path (moveto). */
  147. int
  148. gx_path_add_point(register gx_path *ppath, fixed x, fixed y)
  149. {    ppath->subpath_open = 0;
  150.     ppath->position_valid = 1;
  151.     ppath->position.x = x;
  152.     ppath->position.y = y;
  153.     return 0;
  154. }
  155.  
  156. /* Add a relative point to the current path (rmoveto). */
  157. int
  158. gx_path_add_relative_point(register gx_path *ppath, fixed dx, fixed dy)
  159. {    if ( !ppath->position_valid )
  160.       return_error(gs_error_nocurrentpoint);
  161.     ppath->subpath_open = 0;
  162.     ppath->position.x += dx;
  163.     ppath->position.y += dy;
  164.     return 0;
  165. }
  166.  
  167. /* Set the segment point and the current point in the path. */
  168. /* Assumes ppath points to the path. */
  169. #define path_set_point(pseg, fx, fy)\
  170.     (pseg)->pt.x = ppath->position.x = (fx),\
  171.     (pseg)->pt.y = ppath->position.y = (fy)
  172.  
  173. /* Add a line to the current path (lineto). */
  174. int
  175. gx_path_add_line(gx_path *ppath, fixed x, fixed y)
  176. {    subpath *psub = ppath->current_subpath;
  177.     register line_segment *lp;
  178.     path_open();
  179.     path_alloc_segment(lp, line_segment, s_line, "gx_path_add_line");
  180.     path_alloc_link(lp);
  181.     path_set_point(lp, x, y);
  182. #ifdef DEBUG
  183. if ( gs_debug['p'] )
  184.     dprintf("[p]"), gx_print_segment((segment *)lp);
  185. #endif
  186.     return 0;
  187. }
  188.  
  189. /* Add a rectangle to the current path. */
  190. /* This is a special case of adding a parallelogram. */
  191. int
  192. gx_path_add_rectangle(gx_path *ppath, fixed x0, fixed y0, fixed x1, fixed y1)
  193. {    return gx_path_add_pgram(ppath, x0, y0, x0, y1, x1, y1);
  194. }
  195.  
  196. /* Add a parallelogram to the current path. */
  197. /* This is equivalent to an add_point, three add_lines, */
  198. /* and a close_subpath. */
  199. int
  200. gx_path_add_pgram(gx_path *ppath,
  201.   fixed x0, fixed y0, fixed x1, fixed y1, fixed x2, fixed y2)
  202. {    int code;
  203.      if (    (code = gx_path_add_point(ppath, x0, y0)) < 0 ||
  204.         (code = gx_path_add_line(ppath, x1, y1)) < 0 ||
  205.         (code = gx_path_add_line(ppath, x2, y2)) < 0 ||
  206.         (code = gx_path_add_line(ppath, x0 + x2 - x1, y0 + y2 - y1)) < 0 ||
  207.         (code = gx_path_close_subpath(ppath)) < 0
  208.        ) return code;
  209.     return 0;
  210. }
  211.  
  212. /* Add a curve to the current path (curveto). */
  213. int
  214. gx_path_add_curve(gx_path *ppath,
  215.   fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3)
  216. {    subpath *psub = ppath->current_subpath;
  217.     register curve_segment *lp;
  218.     path_open();
  219.     path_alloc_segment(lp, curve_segment, s_curve, "gx_path_add_curve");
  220.     path_alloc_link(lp);
  221.     lp->p1.x = x1;
  222.     lp->p1.y = y1;
  223.     lp->p2.x = x2;
  224.     lp->p2.y = y2;
  225.     path_set_point(lp, x3, y3);
  226.     psub->curve_count++;
  227.     ppath->curve_count++;
  228. #ifdef DEBUG
  229. if ( gs_debug['p'] )
  230.     dprintf("[p]"), gx_print_segment((segment *)lp);
  231. #endif
  232.     return 0;
  233. }
  234.  
  235. /*
  236.  * Add an approximation of an arc to the current path.
  237.  * Parameters are the initial and final points of the arc,
  238.  * and the point at which the extended tangents meet.
  239.  * We assume that the arc is less than a semicircle.
  240.  * The arc may go either clockwise or counterclockwise.
  241.  * The approximation is a very simple one: a single curve
  242.  * whose other two control points are a fraction F of the way
  243.  * to the intersection of the tangents, where
  244.  *    F = (4/3)(1 / (1 + sqrt(1+(d/r)^2)))
  245.  * where r is the radius and d is the distance from either tangent
  246.  * point to the intersection of the tangents.  This produces
  247.  * a curve whose center point, as well as its ends, lies on
  248.  * the desired arc.
  249.  *
  250.  * Because F has to be computed in user space, we let the client
  251.  * compute it and pass it in as an argument.
  252.  */
  253. int
  254. gx_path_add_arc(gx_path *ppath,
  255.   fixed x0, fixed y0, fixed x3, fixed y3, fixed xt, fixed yt, floatp fraction)
  256. {    return gx_path_add_curve(ppath,
  257.             x0 + (fixed)((xt - x0) * fraction),
  258.             y0 + (fixed)((yt - y0) * fraction),
  259.             x3 + (fixed)((xt - x3) * fraction),
  260.             y3 + (fixed)((yt - y3) * fraction),
  261.             x3, y3);
  262. }
  263.  
  264. /* Append a path to another path, and reset the first path. */
  265. /* Currently this is only used to append a path to its parent */
  266. /* (the path in the previous graphics context). */
  267. int
  268. gx_path_add_path(gx_path *ppath, gx_path *ppfrom)
  269. {    subpath *psub = ppath->current_subpath;
  270.     path_unshare();
  271.     if ( ppfrom->first_subpath )    /* i.e. ppfrom not empty */
  272.        {    if ( ppath->first_subpath )    /* i.e. ppath not empty */
  273.            {    segment *pseg = psub->last;
  274.             segment *pfseg = (segment *)ppfrom->first_subpath;
  275.             pseg->next = pfseg;
  276.             pfseg->prev = pseg;
  277.            }
  278.         else
  279.             ppath->first_subpath = ppfrom->first_subpath;
  280.         ppath->current_subpath = ppfrom->current_subpath;
  281.        }
  282.     /* Transfer the remaining state. */
  283.     ppath->subpath_count += ppfrom->subpath_count;
  284.     ppath->curve_count += ppfrom->curve_count;
  285.     ppath->position = ppfrom->position;
  286.     ppath->position_valid = ppfrom->position_valid;
  287.     ppath->subpath_open = ppfrom->subpath_open;
  288.     /* Reset the source path. */
  289.     ppfrom->first_subpath = 0;
  290.     return 0;
  291. }
  292.  
  293. /* Close the current subpath. */
  294. int
  295. gx_path_close_subpath(gx_path *ppath)
  296. {    subpath *psub = ppath->current_subpath;
  297.     register line_close_segment *lp;
  298.     if ( !ppath->subpath_open ) return 0;
  299.     path_alloc_segment(lp, line_close_segment, s_line_close,
  300.                "gx_path_close_subpath");
  301.     path_alloc_link(lp);
  302.     path_set_point(lp, psub->pt.x, psub->pt.y);
  303.     lp->sub = psub;
  304.     psub->closed = 1;
  305.     ppath->subpath_open = 0;
  306. #ifdef DEBUG
  307. if ( gs_debug['p'] )
  308.     if ( lp != 0 )
  309.       dprintf("[p]"), gx_print_segment((segment *)lp);
  310. #endif
  311.     return 0;
  312. }
  313.  
  314. /* ------ Internal routines ------ */
  315.  
  316. /* Copy the current path, because it was shared. */
  317. /* Return a pointer to the current subpath, or 0. */
  318. private subpath *
  319. path_alloc_copy(gx_path *ppath)
  320. {    gx_path path_new;
  321.     int code;
  322.     code = gx_path_copy(ppath, &path_new, 1);
  323.     if ( code < 0 ) return 0;
  324.     *ppath = path_new;
  325.     ppath->shares_segments = 0;
  326.     return ppath->current_subpath;
  327. }
  328.  
  329. /* ------ Debugging printout ------ */
  330.  
  331. #ifdef DEBUG
  332.  
  333. /* Forward references */
  334. void gx_path_print(P1(const gx_path *));
  335.  
  336. /* Print out a path with a label */
  337. void
  338. gx_dump_path(const gx_path *ppath, const char *tag)
  339. {    dprintf2("[p]Path %lx %s:\n", (ulong)ppath, tag);
  340.     gx_path_print(ppath);
  341. }
  342.  
  343. /* Print a path */
  344. void
  345. gx_path_print(const gx_path *ppath)
  346. {    const segment *pseg = (const segment *)ppath->first_subpath;
  347.     dprintf4("   subpaths=%d, curves=%d, point=(%f,%f)\n",
  348.          ppath->subpath_count, ppath->curve_count,
  349.          fixed2float(ppath->position.x),
  350.          fixed2float(ppath->position.y));
  351.     dprintf5("   box=(%f,%f),(%f,%f) last=%lx\n",
  352.          fixed2float(ppath->bbox.p.x), fixed2float(ppath->bbox.p.y),
  353.          fixed2float(ppath->bbox.q.x), fixed2float(ppath->bbox.q.y),
  354.          (ulong)ppath->box_last);
  355.     while ( pseg )
  356.        {    gx_print_segment(pseg);
  357.         pseg = pseg->next;
  358.        }
  359. }
  360. void
  361. gx_print_segment(const segment *pseg)
  362. {    char out[80];
  363.     sprintf(out, "   %lx<%lx,%lx>: %%s (%6g,%6g) ",
  364.         (ulong)pseg, (ulong)pseg->prev, (ulong)pseg->next,
  365.         fixed2float(pseg->pt.x), fixed2float(pseg->pt.y));
  366.     switch ( pseg->type )
  367.        {
  368.     case s_start:
  369. #define psub ((const subpath *)pseg)
  370.         dprintf1(out, "start");
  371.         dprintf2("#curves=%d last=%lx",
  372.              psub->curve_count, (ulong)psub->last);
  373. #undef psub
  374.         break;
  375.     case s_curve:
  376.         dprintf1(out, "curve");
  377. #define pcur ((const curve_segment *)pseg)
  378.         dprintf4("\n\tp1=(%f,%f) p2=(%f,%f)",
  379.              fixed2float(pcur->p1.x), fixed2float(pcur->p1.y),
  380.              fixed2float(pcur->p2.x), fixed2float(pcur->p2.y));
  381. #undef pcur
  382.         break;
  383.     case s_line:
  384.         dprintf1(out, "line");
  385.         break;
  386.     case s_line_close:
  387. #define plc ((const line_close_segment *)pseg)
  388.         dprintf1(out, "close");
  389.         dprintf1(" %lx", (ulong)(plc->sub));
  390. #undef plc
  391.         break;
  392.     default:
  393.        {    char t[20];
  394.         sprintf(t, "type 0x%x", pseg->type);
  395.         dprintf1(out, t);
  396.        }
  397.        }
  398.     dputc('\n');
  399. }
  400.  
  401. /* Print a clipping path */
  402. void
  403. gx_cpath_print(const gx_clip_path *pcpath)
  404. {    if ( pcpath->segments_valid )
  405.         gx_path_print(&pcpath->path);
  406.     else
  407.         dputs("   (segments not valid)\n");
  408.     dprintf5("   cbox=(%f,%f),(%f,%f) count=%d\n",
  409.          fixed2float(pcpath->cbox.p.x), fixed2float(pcpath->cbox.p.y),
  410.          fixed2float(pcpath->cbox.q.x), fixed2float(pcpath->cbox.q.y),
  411.          pcpath->list.count);
  412.     /****** SHOULD PRINT CLIP LIST ******/
  413. }
  414.  
  415. #endif                    /* DEBUG */
  416.