home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / gspath2 < prev    next >
Encoding:
Text File  |  1991-10-26  |  5.8 KB  |  211 lines

  1. /* Copyright (C) 1989, 1990, 1991 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. /* gspath2.c */
  21. /* Non-constructor path routines for GhostScript library */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gspath.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzpath.h"
  29. #include "gzdevice.h"
  30.  
  31. /* Forward declarations */
  32. private int common_clip(P2(gs_state *, int));
  33. private int set_clip_path(P3(gs_state *, gx_path *, int));
  34.  
  35. /* Path enumeration structure */
  36. struct gs_path_enum_s {
  37.     segment *pseg;
  38.     gs_state *pgs;
  39. };
  40.  
  41. /* Size of path enumeration structure, so clients can allocate */
  42. const uint gs_path_enum_sizeof = sizeof(gs_path_enum);
  43.  
  44. /* ------ Path transformers ------ */
  45.  
  46. int
  47. gs_flattenpath(gs_state *pgs)
  48. {    gx_path fpath;
  49.     int code;
  50.     if ( !pgs->path->curve_count ) return 0;    /* no curves */
  51.     code = gx_path_flatten(pgs->path, &fpath, pgs->flatness);
  52.     if ( code < 0 ) return code;
  53.     gx_path_release(pgs->path);
  54.     *pgs->path = fpath;
  55.     return 0;
  56. }
  57.  
  58. int
  59. gs_reversepath(gs_state *pgs)
  60. {    return_error(gs_error_undefined);        /* NYI */
  61. }
  62.  
  63. /* ------ Accessors ------ */
  64.  
  65. int
  66. gs_pathbbox(gs_state *pgs, gs_rect *pbox)
  67. {    gs_fixed_rect fbox;        /* box in device coordinates */
  68.     gs_rect dbox;
  69.     int code = gx_path_bbox(pgs->path, &fbox);
  70.     if ( code < 0 ) return code;
  71.     /* Transform the result back to user coordinates. */
  72.     dbox.p.x = fixed2float(fbox.p.x);
  73.     dbox.p.y = fixed2float(fbox.p.y);
  74.     dbox.q.x = fixed2float(fbox.q.x);
  75.     dbox.q.y = fixed2float(fbox.q.y);
  76.     return gs_bbox_transform_inverse(&dbox, &ctm_only(pgs), pbox);
  77. }
  78.  
  79. /* ------ Enumerators ------ */
  80.  
  81. /* Start enumerating a path */
  82. void
  83. gs_path_enum_init(gs_path_enum *penum, gs_state *pgs)
  84. {    penum->pseg = (segment *)pgs->path->first_subpath;
  85.     penum->pgs = pgs;
  86. }
  87.  
  88. /* Enumerate the next element of a path. */
  89. /* If the path is finished, return 0; */
  90. /* otherwise, return the element type. */
  91. int
  92. gs_path_enum_next(gs_path_enum *penum, gs_point ppts[3])
  93. {    segment *pseg = penum->pseg;
  94.     gs_state *pgs = penum->pgs;
  95.     gs_point pt;
  96.     int code;
  97.     if ( pseg == 0 ) return 0;    /* finished */
  98.     penum->pseg = pseg->next;
  99.     if ( pseg->type == s_line_close )
  100.       return gs_pe_closepath;
  101.     if ( (code = gs_itransform(pgs, fixed2float(pseg->pt.x),
  102.                    fixed2float(pseg->pt.y), &pt)) < 0 )
  103.       return code;
  104.     switch ( pseg->type )
  105.        {
  106.     case s_start:
  107.          ppts[0] = pt;
  108.          return gs_pe_moveto;
  109.     case s_line:
  110.          ppts[0] = pt;
  111.          return gs_pe_lineto;
  112.     case s_curve:
  113. #define pcurve ((curve_segment *)pseg)
  114.          if ( (code =
  115.            gs_itransform(pgs, fixed2float(pcurve->p1.x),
  116.                  fixed2float(pcurve->p1.y), &ppts[0])) < 0 ||
  117.           (code =
  118.            gs_itransform(pgs, fixed2float(pcurve->p2.x),
  119.                  fixed2float(pcurve->p2.y), &ppts[1])) < 0 )
  120.            return 0;
  121.          ppts[2] = pt;
  122.          return gs_pe_curveto;
  123. #undef pcurve
  124.     default:
  125.          lprintf1("bad type %x in gs_path_enum_next!\n", pseg->type);
  126.          gs_exit(1);
  127.        }
  128. }
  129.  
  130. /* ------ Clipping ------ */
  131.  
  132. int
  133. gs_clippath(gs_state *pgs)
  134. {    return gx_path_copy(pgs->clip_path, pgs->path);
  135. }
  136.  
  137. int
  138. gs_initclip(gs_state *pgs)
  139. {    register gx_device *dev = pgs->device->info;
  140.     gs_matrix_fixed imat;
  141.     gs_fixed_point ll, ur;
  142.     (*dev->procs->get_initial_matrix)(dev, (gs_matrix *)&imat);
  143.     update_matrix_fixed(imat);
  144.     gs_point_transform2fixed(&imat,
  145.                  dev->l_margin * 72,
  146.                  dev->b_margin * 72,
  147.                  &ll);
  148.     gs_point_transform2fixed(&imat,
  149.                  (dev->width / dev->x_pixels_per_inch - dev->r_margin) * 72,
  150.                  (dev->height / dev->y_pixels_per_inch - dev->t_margin) * 72,
  151.                  &ur);
  152.     return gx_clip_to_rectangle(pgs, ll.x, ll.y, ur.x, ur.y);
  153. }
  154.  
  155. int
  156. gs_clip(gs_state *pgs)
  157. {    return common_clip(pgs, gx_rule_winding_number);
  158. }
  159.  
  160. int
  161. gs_eoclip(gs_state *pgs)
  162. {    return common_clip(pgs, gx_rule_even_odd);
  163. }
  164.  
  165. /* ------ Internal routines for clipping ------ */
  166.  
  167. /* Establish a rectangle as the clipping path. */
  168. /* Used by initclip and by the character cache logic. */
  169. int
  170. gx_clip_to_rectangle(gs_state *pgs, fixed x0, fixed y0, fixed x1, fixed y1)
  171. {    gx_path cpath;
  172.     gx_path *ppath = &cpath;
  173.     int code;
  174.     gx_path_init(ppath, &pgs->memory_procs);
  175.     if ( (code = gx_path_add_rectangle(ppath, x0, y0, x1, y1)) < 0 )
  176.        {    gx_path_release(ppath);
  177.         return code;
  178.        }
  179.     gx_path_release(pgs->clip_path);
  180.     return set_clip_path(pgs, ppath, gx_rule_winding_number);
  181. }
  182.  
  183. /* Main clipping routine.  NOT CORRECT. */
  184. private int
  185. common_clip(gs_state *pgs, int rule)
  186. {    gx_path cpath;
  187.     int code = gx_path_flatten(pgs->path, &cpath, pgs->flatness);
  188.     if ( !code ) code = set_clip_path(pgs, &cpath, rule);
  189.     return code;
  190. }
  191. /* Set the clipping path.  If it is just a rectangle, */
  192. /* set the parameters for the quick clipping check. */
  193. private int
  194. set_clip_path(gs_state *pgs, register gx_path *ppath, int rule)
  195. {    if ( !gx_path_is_rectangle(ppath, &ppath->cbox) )
  196.        {    /* Not a rectangle, the quick check must fail */
  197.         ppath->cbox.p.x = ppath->cbox.p.y = 0;
  198.         ppath->cbox.q.x = ppath->cbox.q.y = 0;
  199.        }
  200.     /* Update the outer bounding box as well. */
  201.     gx_path_bbox(ppath, &ppath->bbox);
  202.     *pgs->clip_path = *ppath;
  203.     pgs->clip_rule = rule;
  204. #ifdef DEBUG
  205. if ( gs_debug['p'] )
  206.     dprintf("[p]Clipping path:\n"),
  207.     gx_path_print(pgs->clip_path);
  208. #endif
  209.     return 0;
  210. }
  211.