home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / DOS_14 / GS252DVX.ZIP / GSPATH2.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  8KB  |  257 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. /* 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 "gxarith.h"
  27. #include "gxmatrix.h"
  28. #include "gzstate.h"
  29. #include "gzpath.h"
  30. #include "gzdevice.h"
  31.  
  32. /* Forward references */
  33. private int common_clip(P2(gs_state *, int));
  34. private int set_clip_path(P3(gs_state *, gx_clip_path *, int));
  35.  
  36. /* Path enumeration structure */
  37. struct gs_path_enum_s {
  38.     const segment *pseg;
  39.     const gs_state *pgs;
  40.     int moveto_done;    /* have we reported a final moveto yet? */
  41. };
  42.  
  43. /* Size of path enumeration structure, so clients can allocate */
  44. const uint gs_path_enum_sizeof = sizeof(gs_path_enum);
  45.  
  46. /* ------ Path transformers ------ */
  47.  
  48. int
  49. gs_flattenpath(gs_state *pgs)
  50. {    gx_path fpath;
  51.     int code;
  52.     if ( !pgs->path->curve_count ) return 0;    /* no curves */
  53.     code = gx_path_flatten(pgs->path, &fpath, pgs->flatness, 0);
  54.     if ( code < 0 ) return code;
  55.     gx_path_release(pgs->path);
  56.     *pgs->path = fpath;
  57.     return 0;
  58. }
  59.  
  60. int
  61. gs_reversepath(gs_state *pgs)
  62. {    gx_path rpath;
  63.     int code = gx_path_copy_reversed(pgs->path, &rpath, 1);
  64.     if ( code < 0 ) return code;
  65.     gx_path_release(pgs->path);
  66.     *pgs->path = rpath;
  67.     return 0;
  68. }
  69.  
  70. /* ------ Accessors ------ */
  71.  
  72. int
  73. gs_pathbbox(gs_state *pgs, gs_rect *pbox)
  74. {    gs_fixed_rect fbox;        /* box in device coordinates */
  75.     gs_rect dbox;
  76.     int code = gx_path_bbox(pgs->path, &fbox);
  77.     if ( code < 0 ) return code;
  78.     /* Transform the result back to user coordinates. */
  79.     dbox.p.x = fixed2float(fbox.p.x);
  80.     dbox.p.y = fixed2float(fbox.p.y);
  81.     dbox.q.x = fixed2float(fbox.q.x);
  82.     dbox.q.y = fixed2float(fbox.q.y);
  83.     return gs_bbox_transform_inverse(&dbox, &ctm_only(pgs), pbox);
  84. }
  85.  
  86. /* ------ Enumerators ------ */
  87.  
  88. /* Start enumerating a path */
  89. void
  90. gs_path_enum_init(gs_path_enum *penum, const gs_state *pgs)
  91. {    penum->pseg = (const segment *)pgs->path->first_subpath;
  92.     penum->pgs = pgs;
  93.     penum->moveto_done = 0;
  94. }
  95.  
  96. /* Enumerate the next element of a path. */
  97. /* If the path is finished, return 0; */
  98. /* otherwise, return the element type. */
  99. int
  100. gs_path_enum_next(gs_path_enum *penum, gs_point ppts[3])
  101. {    const segment *pseg = penum->pseg;
  102.     const gs_state *pgs = penum->pgs;
  103.     gs_point pt;
  104.     int code;
  105.     if ( pseg == 0 )
  106.     {    /* We've enumerated all the segments, but there might be */
  107.         /* a trailing moveto. */
  108.         const gx_path *ppath = pgs->path;
  109.         const subpath *psub = ppath->current_subpath;
  110.         if ( !ppath->subpath_open && ppath->position_valid &&
  111.              !penum->moveto_done && (psub == 0 || !psub->closed ||
  112.             /* If the last subpath was closed, we can't know */
  113.             /* if it was followed by a move to the same point. */
  114.             /* Just compare the coordinates and hope we're right. */
  115.              psub->last->pt.x != ppath->position.x ||
  116.              psub->last->pt.y != ppath->position.y)
  117.            )
  118.            {    penum->moveto_done = 1;
  119.             if ( (code = gs_itransform(pgs,
  120.                 fixed2float(ppath->position.x),
  121.                 fixed2float(ppath->position.y),
  122.                 &ppts[0])) < 0 )
  123.               return code;
  124.             return gs_pe_moveto;
  125.            }
  126.         return 0;
  127.     }
  128.     penum->pseg = pseg->next;
  129.     if ( pseg->type == s_line_close )
  130.       return gs_pe_closepath;
  131.     if ( (code = gs_itransform(pgs, fixed2float(pseg->pt.x),
  132.                    fixed2float(pseg->pt.y), &pt)) < 0 )
  133.       return code;
  134.     switch ( pseg->type )
  135.        {
  136.     case s_start:
  137.          ppts[0] = pt;
  138.          return gs_pe_moveto;
  139.     case s_line:
  140.          ppts[0] = pt;
  141.          return gs_pe_lineto;
  142.     case s_curve:
  143. #define pcurve ((const curve_segment *)pseg)
  144.          if ( (code =
  145.            gs_itransform(pgs, fixed2float(pcurve->p1.x),
  146.                  fixed2float(pcurve->p1.y), &ppts[0])) < 0 ||
  147.           (code =
  148.            gs_itransform(pgs, fixed2float(pcurve->p2.x),
  149.                  fixed2float(pcurve->p2.y), &ppts[1])) < 0 )
  150.            return 0;
  151.          ppts[2] = pt;
  152.          return gs_pe_curveto;
  153. #undef pcurve
  154.     default:
  155.          lprintf1("bad type %x in gs_path_enum_next!\n", pseg->type);
  156.          return_error(gs_error_Fatal);
  157.        }
  158. }
  159.  
  160. /* ------ Clipping ------ */
  161.  
  162. int
  163. gs_clippath(gs_state *pgs)
  164. {    gx_path path;
  165.     int code = gx_cpath_path(pgs->clip_path, &path);
  166.     if ( code < 0 ) return code;
  167.     return gx_path_copy(&path, pgs->path, 1);
  168. }
  169.  
  170. int
  171. gs_initclip(gs_state *pgs)
  172. {    register gx_device *dev = pgs->device->info;
  173.     gs_fixed_rect box;
  174.     if ( is_fzero2(dev->l_margin, dev->r_margin) &&
  175.          is_fzero2(dev->b_margin, dev->t_margin)
  176.        )
  177.        {    /* Shortcut, don't need to worry about density. */
  178.         box.p.x = box.p.y = 0;
  179.         box.q.x = int2fixed(dev->width);
  180.         box.q.y = int2fixed(dev->height);
  181.        }
  182.     else
  183.        {    /* Indent from bounding rectangle. */
  184.         gs_matrix_fixed imat;
  185.         (*dev->procs->get_initial_matrix)(dev, (gs_matrix *)&imat);
  186.         gs_update_matrix_fixed(&imat);
  187.         gs_point_transform2fixed(&imat,
  188.                      dev->l_margin * 72,
  189.                      dev->b_margin * 72,
  190.                      &box.p);
  191.         gs_point_transform2fixed(&imat,
  192.                      (dev->width / dev->x_pixels_per_inch - dev->r_margin) * 72,
  193.                      (dev->height / dev->y_pixels_per_inch - dev->t_margin) * 72,
  194.                      &box.q);
  195.        }
  196.     return gx_clip_to_rectangle(pgs, &box);
  197. }
  198.  
  199. int
  200. gs_clip(gs_state *pgs)
  201. {    return common_clip(pgs, gx_rule_winding_number);
  202. }
  203.  
  204. int
  205. gs_eoclip(gs_state *pgs)
  206. {    return common_clip(pgs, gx_rule_even_odd);
  207. }
  208.  
  209. private int
  210. common_clip(gs_state *pgs, int rule)
  211. {    gx_path fpath;
  212.     int code = gx_path_flatten(pgs->path, &fpath, pgs->flatness, 0);
  213.     if ( code < 0 ) return code;
  214.     code = gx_cpath_intersect(pgs, pgs->clip_path, &fpath, rule);
  215.     if ( code != 1 ) gx_path_release(&fpath);
  216.     if ( code < 0 ) return code;
  217.     return set_clip_path(pgs, pgs->clip_path, rule);
  218. }
  219.  
  220. /* Establish a rectangle as the clipping path. */
  221. /* Used by initclip and by the character cache logic. */
  222. int
  223. gx_clip_to_rectangle(gs_state *pgs, gs_fixed_rect *pbox)
  224. {    gx_clip_path cpath;
  225.     int code = gx_cpath_from_rectangle(&cpath, pbox, &pgs->memory_procs);
  226.     if ( code < 0 ) return code;
  227.     gx_cpath_release(pgs->clip_path);
  228.     return set_clip_path(pgs, &cpath, gx_rule_winding_number);
  229. }
  230.  
  231. /* Set the clipping path to the current path, without intersecting. */
  232. /* Currently only used by the insideness testing operators, */
  233. /* but might be used by viewclip eventually. */
  234. /* The algorithm is very inefficient; we'll improve it later if needed. */
  235. int
  236. gx_clip_to_path(gs_state *pgs)
  237. {    gs_fixed_rect bbox;
  238.     int code;
  239.     (code = gx_path_bbox(pgs->path, &bbox)) < 0 ||
  240.     (code = gx_clip_to_rectangle(pgs, &bbox)) < 0 ||
  241.     (code = gs_clip(pgs));
  242.     return code;
  243. }
  244.  
  245. /* Set the clipping path (internal). */
  246. private int
  247. set_clip_path(gs_state *pgs, gx_clip_path *pcpath, int rule)
  248. {    *pgs->clip_path = *pcpath;
  249.     pgs->clip_rule = rule;
  250. #ifdef DEBUG
  251. if ( gs_debug['p'] )
  252.     dprintf("[p]Clipping path:\n"),
  253.     gx_cpath_print(pcpath);
  254. #endif
  255.     return 0;
  256. }
  257.