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

  1. /* Copyright (C) 1989, 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. /* gspath2.c */
  20. /* Non-constructor path routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gxfixed.h"
  25. #include "gxarith.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzpath.h"
  29. #include "gscoord.h"            /* gs_itransform prototype */
  30.  
  31. /* Forward references */
  32. private int common_clip(P2(gs_state *, int));
  33. private int set_clip_path(P3(gs_state *, gx_clip_path *, int));
  34.  
  35. /* Path enumeration structure */
  36. struct gs_path_enum_s {
  37.     const segment *pseg;
  38.     const gs_state *pgs;
  39.     gx_path *copied_path;    /* a copy of the path, release when done */
  40.     bool moveto_done;    /* have we reported a final moveto yet? */
  41. };
  42. gs_private_st_ptrs3(st_gs_path_enum, gs_path_enum, "gs_path_enum",
  43.   path_enum_enum_ptrs, path_enum_reloc_ptrs, pseg, pgs, copied_path);
  44.  
  45. /* ------ Path transformers ------ */
  46.  
  47. int
  48. gs_flattenpath(gs_state *pgs)
  49. {    gx_path fpath;
  50.     int code;
  51.     if ( !pgs->path->curve_count ) return 0;    /* no curves */
  52.     code = gx_path_flatten(pgs->path, &fpath, pgs->flatness, 0);
  53.     if ( code < 0 ) return code;
  54.     gx_path_release(pgs->path);
  55.     *pgs->path = fpath;
  56.     return 0;
  57. }
  58.  
  59. int
  60. gs_reversepath(gs_state *pgs)
  61. {    gx_path rpath;
  62.     int code = gx_path_copy_reversed(pgs->path, &rpath, 1);
  63.     if ( code < 0 ) return code;
  64.     gx_path_release(pgs->path);
  65.     *pgs->path = rpath;
  66.     return 0;
  67. }
  68.  
  69. /* ------ Accessors ------ */
  70.  
  71. int
  72. gs_pathbbox(gs_state *pgs, gs_rect *pbox)
  73. {    gs_fixed_rect fbox;        /* box in device coordinates */
  74.     gs_rect dbox;
  75.     int code = gx_path_bbox(pgs->path, &fbox);
  76.     if ( code < 0 ) return code;
  77.     /* Transform the result back to user coordinates. */
  78.     dbox.p.x = fixed2float(fbox.p.x);
  79.     dbox.p.y = fixed2float(fbox.p.y);
  80.     dbox.q.x = fixed2float(fbox.q.x);
  81.     dbox.q.y = fixed2float(fbox.q.y);
  82.     return gs_bbox_transform_inverse(&dbox, &ctm_only(pgs), pbox);
  83. }
  84.  
  85. /* ------ Enumerators ------ */
  86.  
  87. /* Allocate a path enumerator. */
  88. gs_path_enum *
  89. gs_path_enum_alloc(gs_memory_t *mem, client_name_t cname)
  90. {    return gs_alloc_struct(mem, gs_path_enum, &st_gs_path_enum, cname);
  91. }
  92.  
  93. /* Start enumerating a path */
  94. int
  95. gs_path_enum_init(gs_path_enum *penum, const gs_state *pgs)
  96. {    gx_path *ppath = pgs->path;
  97.     int code;
  98.     penum->copied_path = gs_alloc_struct(pgs->memory, gx_path, &st_path,
  99.                          "gs_path_enum_init");
  100.     if ( penum->copied_path == 0 )
  101.         return_error(gs_error_VMerror);
  102.     code = gx_path_copy(ppath, penum->copied_path, 1);
  103.     if ( code < 0 )
  104.     {    gs_free_object(pgs->memory, penum->copied_path,
  105.                    "gs_path_enum_init");
  106.         return code;
  107.     }
  108.     penum->pgs = pgs;
  109.     penum->pseg = (const segment *)penum->copied_path->first_subpath;
  110.     penum->moveto_done = false;
  111.     return 0;
  112. }
  113.  
  114. /* Enumerate the next element of a path. */
  115. /* If the path is finished, return 0; */
  116. /* otherwise, return the element type. */
  117. int
  118. gs_path_enum_next(gs_path_enum *penum, gs_point ppts[3])
  119. {    const segment *pseg = penum->pseg;
  120.     const gs_state *pgs = penum->pgs;
  121.     gs_point pt;
  122.     int code;
  123.     if ( pseg == 0 )
  124.     {    /* We've enumerated all the segments, but there might be */
  125.         /* a trailing moveto. */
  126.         const gx_path *ppath = pgs->path;
  127.         if ( ppath->subpath_open < 0 && !penum->moveto_done )
  128.         {    /* Handle a trailing moveto */
  129.             penum->moveto_done = true;
  130.             if ( (code = gs_itransform((gs_state *)pgs,
  131.                     fixed2float(ppath->position.x),
  132.                     fixed2float(ppath->position.y),
  133.                     &ppts[0])) < 0
  134.                )
  135.                 return code;
  136.             return gs_pe_moveto;
  137.         }
  138.         return 0;
  139.     }
  140.     penum->pseg = pseg->next;
  141.     if ( pseg->type == s_line_close )
  142.       return gs_pe_closepath;
  143.     if ( (code = gs_itransform((gs_state *)pgs, fixed2float(pseg->pt.x),
  144.                    fixed2float(pseg->pt.y), &pt)) < 0 )
  145.       return code;
  146.     switch ( pseg->type )
  147.        {
  148.     case s_start:
  149.          ppts[0] = pt;
  150.          return gs_pe_moveto;
  151.     case s_line:
  152.          ppts[0] = pt;
  153.          return gs_pe_lineto;
  154.     case s_curve:
  155. #define pcurve ((const curve_segment *)pseg)
  156.          if ( (code =
  157.            gs_itransform((gs_state *)pgs, fixed2float(pcurve->p1.x),
  158.                  fixed2float(pcurve->p1.y), &ppts[0])) < 0 ||
  159.           (code =
  160.            gs_itransform((gs_state *)pgs, fixed2float(pcurve->p2.x),
  161.                  fixed2float(pcurve->p2.y), &ppts[1])) < 0 )
  162.            return 0;
  163.          ppts[2] = pt;
  164.          return gs_pe_curveto;
  165. #undef pcurve
  166.     default:
  167.          lprintf1("bad type %x in gs_path_enum_next!\n", pseg->type);
  168.          return_error(gs_error_Fatal);
  169.        }
  170. }
  171.  
  172. /* Clean up after a pathforall. */
  173. void
  174. gs_path_enum_cleanup(gs_path_enum *penum)
  175. {    if ( penum->copied_path != 0 )        /* don't do it twice ... */
  176.                         /* shouldn't be needed! */
  177.     {    gx_path_release(penum->copied_path);
  178.         gs_free_object(penum->pgs->memory, penum->copied_path,
  179.                    "gs_path_enum_cleanup");
  180.         penum->copied_path = 0;
  181.     }
  182. }
  183.  
  184. /* ------ Clipping ------ */
  185.  
  186. int
  187. gs_clippath(gs_state *pgs)
  188. {    gx_path path;
  189.     int code = gx_cpath_path(pgs->clip_path, &path);
  190.     if ( code < 0 ) return code;
  191.     return gx_path_copy(&path, pgs->path, 1);
  192. }
  193.  
  194. int
  195. gs_initclip(gs_state *pgs)
  196. {    register gx_device *dev = gs_currentdevice(pgs);
  197.     gs_rect bbox;
  198.     gs_fixed_rect box;
  199.     bbox.p.x = 0;
  200.     bbox.p.y = 0;
  201.     bbox.q.x = dev->width;
  202.     bbox.q.y = dev->height;
  203.     if ( !(is_fzero2(dev->l_margin, dev->r_margin) &&
  204.            is_fzero2(dev->b_margin, dev->t_margin))
  205.        )
  206.        {    /* Indent from bounding rectangle. */
  207.         gs_matrix imat;
  208.         (*dev_proc(dev, get_initial_matrix))(dev, &imat);
  209.         gs_bbox_transform_inverse(&bbox, &imat, &bbox);
  210.         bbox.p.x += dev->l_margin * 72;
  211.         bbox.p.y += dev->b_margin * 72;
  212.         bbox.q.x -= dev->r_margin * 72;
  213.         bbox.q.y -= dev->t_margin * 72;
  214.         gs_bbox_transform(&bbox, &imat, &bbox);
  215.        }
  216.     box.p.x = float2fixed(bbox.p.x);
  217.     box.p.y = float2fixed(bbox.p.y);
  218.     box.q.x = float2fixed(bbox.q.x);
  219.     box.q.y = float2fixed(bbox.q.y);
  220.     return gx_clip_to_rectangle(pgs, &box);
  221. }
  222.  
  223. int
  224. gs_clip(gs_state *pgs)
  225. {    return common_clip(pgs, gx_rule_winding_number);
  226. }
  227.  
  228. int
  229. gs_eoclip(gs_state *pgs)
  230. {    return common_clip(pgs, gx_rule_even_odd);
  231. }
  232.  
  233. private int
  234. common_clip(gs_state *pgs, int rule)
  235. {    gx_path fpath;
  236.     int code = gx_path_flatten(pgs->path, &fpath, pgs->flatness, 0);
  237.     if ( code < 0 ) return code;
  238.     code = gx_cpath_intersect(pgs, pgs->clip_path, &fpath, rule);
  239.     if ( code != 1 ) gx_path_release(&fpath);
  240.     if ( code < 0 ) return code;
  241.     return set_clip_path(pgs, pgs->clip_path, rule);
  242. }
  243.  
  244. /* Establish a rectangle as the clipping path. */
  245. /* Used by initclip and by the character and Pattern cache logic. */
  246. int
  247. gx_clip_to_rectangle(gs_state *pgs, gs_fixed_rect *pbox)
  248. {    gx_clip_path cpath;
  249.     int code = gx_cpath_from_rectangle(&cpath, pbox, pgs->memory);
  250.     if ( code < 0 ) return code;
  251.     gx_cpath_release(pgs->clip_path);
  252.     return set_clip_path(pgs, &cpath, gx_rule_winding_number);
  253. }
  254.  
  255. /* Set the clipping path to the current path, without intersecting. */
  256. /* Currently only used by the insideness testing operators, */
  257. /* but might be used by viewclip eventually. */
  258. /* The algorithm is very inefficient; we'll improve it later if needed. */
  259. int
  260. gx_clip_to_path(gs_state *pgs)
  261. {    gs_fixed_rect bbox;
  262.     int code;
  263.     if ( (code = gx_path_bbox(pgs->path, &bbox)) < 0 ||
  264.          (code = gx_clip_to_rectangle(pgs, &bbox)) < 0
  265.        )
  266.       return code;
  267.     return gs_clip(pgs);
  268. }
  269.  
  270. /* Set the clipping path (internal). */
  271. private int
  272. set_clip_path(gs_state *pgs, gx_clip_path *pcpath, int rule)
  273. {    *pgs->clip_path = *pcpath;
  274.     /* The following is a hack for the GC. */
  275.     pgs->clip_path->list.container_offset =
  276.       (char *)&pgs->clip_path->list - (char *)pgs->contents;
  277.     pgs->clip_rule = rule;
  278. #ifdef DEBUG
  279. if ( gs_debug_c('p') )
  280.   {    extern void gx_cpath_print(P1(const gx_clip_path *));
  281.     dprintf("[p]Clipping path:\n"),
  282.     gx_cpath_print(pcpath);
  283.   }
  284. #endif
  285.     return 0;
  286. }
  287.