gxpath.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxpath.h,v 1.16 2005/08/22 14:29:18 igor Exp $ */
  14. /* Fixed-point path procedures */
  15. /* Requires gxfixed.h */
  16. #ifndef gxpath_INCLUDED
  17. # define gxpath_INCLUDED
  18. #include "gscpm.h"
  19. #include "gslparam.h"
  20. #include "gspenum.h"
  21. #include "gsrect.h"
  22. /* The routines and types in this interface use */
  23. /* device, rather than user, coordinates, and fixed-point, */
  24. /* rather than floating, representation. */
  25. /* Opaque type for a path */
  26. #ifndef gx_path_DEFINED
  27. # define gx_path_DEFINED
  28. typedef struct gx_path_s gx_path;
  29. #endif
  30. /* Define the two insideness rules */
  31. #define gx_rule_winding_number (-1)
  32. #define gx_rule_even_odd 1
  33. /* Define 'notes' that describe the role of a path segment. */
  34. /* These are only for internal use; a normal segment's notes are 0. */
  35. typedef enum {
  36. sn_none = 0,
  37. sn_not_first = 1, /* segment is in curve/arc and not first */
  38. sn_from_arc = 2 /* segment is part of an arc */
  39. } segment_notes;
  40. /* Debugging routines */
  41. #ifdef DEBUG
  42. void gx_dump_path(const gx_path *, const char *);
  43. void gx_path_print(const gx_path *);
  44. #endif
  45. /* Path memory management */
  46. /*
  47. * Path memory management is unfortunately a little tricky. The
  48. * implementation details are in gzpath.h: we only present the API here.
  49. *
  50. * Path objects per se may be allocated in 3 different ways: on the
  51. * C stack, as separate objects in the heap, or (for the graphics state
  52. * only) contained in a larger heap-allocated object.
  53. *
  54. * Any number of paths may share segments. The segments are stored in
  55. * their own, reference-counted object, and are freed when there are no
  56. * more references to that object.
  57. */
  58. /*
  59. * Allocate a path on the heap, and initialize it. If shared is NULL,
  60. * allocate a segments object; if shared is an existing path, share its
  61. * segments.
  62. */
  63. gx_path *gx_path_alloc_shared(const gx_path * shared, gs_memory_t * mem,
  64. client_name_t cname);
  65. #define gx_path_alloc(mem, cname)\
  66. gx_path_alloc_shared(NULL, mem, cname)
  67. /*
  68. * Initialize a path contained in an already-heap-allocated object,
  69. * optionally allocating its segments.
  70. */
  71. int gx_path_init_contained_shared(gx_path * ppath, const gx_path * shared,
  72. gs_memory_t * mem, client_name_t cname);
  73. #define gx_path_alloc_contained(ppath, mem, cname)\
  74. gx_path_init_contained_shared(ppath, NULL, mem, cname)
  75. /*
  76. * Initialize a stack-allocated path. This doesn't allocate anything,
  77. * but may still share the segments. Note that it returns an error if
  78. * asked to share the segments of another local path.
  79. */
  80. int gx_path_init_local_shared(gx_path * ppath, const gx_path * shared,
  81. gs_memory_t * mem);
  82. #define gx_path_init_local(ppath, mem)\
  83. (void)gx_path_init_local_shared(ppath, NULL, mem) /* can't fail */
  84. /*
  85. * Initialize a stack-allocated pseudo-path for computing a bbox
  86. * for a dynamic path.
  87. */
  88. void gx_path_init_bbox_accumulator(gx_path * ppath);
  89. /*
  90. * Ensure that a path owns its segments, by copying the segments if
  91. * they currently have multiple references.
  92. */
  93. int gx_path_unshare(gx_path * ppath);
  94. /*
  95. * Free a path by releasing its segments if they have no more references.
  96. * This also frees the path object iff it was allocated by gx_path_alloc.
  97. */
  98. void gx_path_free(gx_path * ppath, client_name_t cname);
  99. /*
  100. * Assign one path to another, adjusting reference counts appropriately.
  101. * Note that this requires that segments of the two paths (but not the path
  102. * objects themselves) were allocated with the same allocator. Note also
  103. * that if ppfrom is stack-allocated, ppto is not, and ppto's segments are
  104. * currently shared, gx_path_assign must do the equivalent of a
  105. * gx_path_new(ppto), which allocates a new segments object for ppto.
  106. */
  107. int gx_path_assign_preserve(gx_path * ppto, gx_path * ppfrom);
  108. /*
  109. * Assign one path to another and free the first path at the same time.
  110. * (This may do less work than assign_preserve + free.)
  111. */
  112. int gx_path_assign_free(gx_path * ppto, gx_path * ppfrom);
  113. /* Path constructors */
  114. /* Note that all path constructors have an implicit initial gx_path_unshare. */
  115. int gx_path_new(gx_path *),
  116. gx_path_add_point(gx_path *, fixed, fixed),
  117. gx_path_add_relative_point(gx_path *, fixed, fixed),
  118. gx_path_add_line_notes(gx_path *, fixed, fixed, segment_notes),
  119. gx_path_add_lines_notes(gx_path *, const gs_fixed_point *, int, segment_notes),
  120. gx_path_add_rectangle(gx_path *, fixed, fixed, fixed, fixed),
  121. gx_path_add_char_path(gx_path *, gx_path *, gs_char_path_mode),
  122. gx_path_add_curve_notes(gx_path *, fixed, fixed, fixed, fixed, fixed, fixed, segment_notes),
  123. gx_path_add_partial_arc_notes(gx_path *, fixed, fixed, fixed, fixed, floatp, segment_notes),
  124. gx_path_add_path(gx_path *, gx_path *),
  125. gx_path_close_subpath_notes(gx_path *, segment_notes),
  126. /* We have to remove the 'subpath' from the following name */
  127. /* to keep it unique in the first 23 characters. */
  128. gx_path_pop_close_notes(gx_path *, segment_notes);
  129. /* Access path state flags */
  130. byte gx_path_get_state_flags(gx_path *ppath);
  131. void gx_path_set_state_flags(gx_path *ppath, byte flags);
  132. bool gx_path_is_drawing(gx_path *ppath);
  133. /*
  134. * The last argument to gx_path_add_partial_arc is a fraction for computing
  135. * the curve parameters. Here is the correct value for quarter-circles.
  136. * (stroke uses this to draw round caps and joins.)
  137. */
  138. #define quarter_arc_fraction 0.55228474983079334
  139. /*
  140. * Backward-compatible constructors that don't take a notes argument.
  141. */
  142. #define gx_path_add_line(ppath, x, y)\
  143. gx_path_add_line_notes(ppath, x, y, sn_none)
  144. #define gx_path_add_lines(ppath, pts, count)\
  145. gx_path_add_lines_notes(ppath, pts, count, sn_none)
  146. #define gx_path_add_curve(ppath, x1, y1, x2, y2, x3, y3)\
  147. gx_path_add_curve_notes(ppath, x1, y1, x2, y2, x3, y3, sn_none)
  148. #define gx_path_add_partial_arc(ppath, x3, y3, xt, yt, fraction)\
  149. gx_path_add_partial_arc_notes(ppath, x3, y3, xt, yt, fraction, sn_none)
  150. #define gx_path_close_subpath(ppath)\
  151. gx_path_close_subpath_notes(ppath, sn_none)
  152. #define gx_path_pop_close_subpath(ppath)\
  153. gx_path_pop_close_notes(ppath, sn_none)
  154. typedef enum {
  155. pco_none = 0,
  156. pco_monotonize = 1, /* make curves monotonic */
  157. pco_accurate = 2, /* flatten with accurate tangents at ends */
  158. pco_for_stroke = 4, /* flatten taking line width into account */
  159. pco_small_curves = 8 /* make curves small */
  160. } gx_path_copy_options;
  161. /* Path accessors */
  162. gx_path *gx_current_path(const gs_state *);
  163. int gx_path_current_point(const gx_path *, gs_fixed_point *),
  164. gx_path_bbox(gx_path *, gs_fixed_rect *),
  165. gx_path_bbox_set(gx_path *, gs_fixed_rect *);
  166. int gx_path_subpath_start_point(const gx_path *, gs_fixed_point *);
  167. bool gx_path_has_curves(const gx_path *),
  168. gx_path_is_void(const gx_path *), /* no segments */
  169. gx_path_is_null(const gx_path *), /* nothing at all */
  170. gx_path__check_curves(const gx_path * ppath, gx_path_copy_options options, fixed fixed_flat);
  171. typedef enum {
  172. prt_none = 0,
  173. prt_open = 1, /* only 3 sides */
  174. prt_fake_closed = 2, /* 4 lines, no closepath */
  175. prt_closed = 3 /* 3 or 4 lines + closepath */
  176. } gx_path_rectangular_type;
  177. gx_path_rectangular_type
  178. gx_path_is_rectangular(const gx_path *, gs_fixed_rect *);
  179. #define gx_path_is_rectangle(ppath, pbox)\
  180. (gx_path_is_rectangular(ppath, pbox) != prt_none)
  181. /* Inline versions of the above */
  182. #define gx_path_is_null_inline(ppath)\
  183. (gx_path_is_void(ppath) && !path_position_valid(ppath))
  184. /* Path transformers */
  185. /* The imager state is only needed when flattening for stroke. */
  186. #ifndef gs_imager_state_DEFINED
  187. # define gs_imager_state_DEFINED
  188. typedef struct gs_imager_state_s gs_imager_state;
  189. #endif
  190. int gx_path_copy_reducing(const gx_path * ppath_old, gx_path * ppath_new,
  191. fixed fixed_flatness, const gs_imager_state *pis,
  192. gx_path_copy_options options);
  193. #define gx_path_copy(old, new)\
  194. gx_path_copy_reducing(old, new, max_fixed, NULL, pco_none)
  195. #define gx_path_add_flattened(old, new, flatness)\
  196. gx_path_copy_reducing(old, new, float2fixed(flatness), NULL, pco_none)
  197. #define gx_path_add_flattened_accurate(old, new, flatness, accurate)\
  198. gx_path_copy_reducing(old, new, float2fixed(flatness), NULL,\
  199. (accurate ? pco_accurate : pco_none))
  200. #define gx_path_add_flattened_for_stroke(old, new, flatness, pis)\
  201. gx_path_copy_reducing(old, new, float2fixed(flatness), pis,\
  202. (pis->accurate_curves ?\
  203. pco_accurate | pco_for_stroke : pco_for_stroke))
  204. #define gx_path_add_monotonized(old, new)\
  205. gx_path_copy_reducing(old, new, max_fixed, NULL, pco_monotonize)
  206. int gx_path_add_dash_expansion(const gx_path * /*old*/, gx_path * /*new*/,
  207. const gs_imager_state *),
  208. gx_path_copy_reversed(const gx_path * /*old*/, gx_path * /*new*/),
  209. gx_path_translate(gx_path *, fixed, fixed),
  210. gx_path_scale_exp2_shared(gx_path *ppath, int log2_scale_x,
  211. int log2_scale_y, bool segments_shared);
  212. void gx_point_scale_exp2(gs_fixed_point *, int, int),
  213. gx_rect_scale_exp2(gs_fixed_rect *, int, int);
  214. /* Path enumerator */
  215. /* This interface does not make a copy of the path. */
  216. /* Do not use gs_path_enum_cleanup with this interface! */
  217. int gx_path_enum_init(gs_path_enum *, const gx_path *);
  218. int gx_path_enum_next(gs_path_enum *, gs_fixed_point[3]); /* 0 when done */
  219. segment_notes
  220. gx_path_enum_notes(const gs_path_enum *);
  221. bool gx_path_enum_backup(gs_path_enum *);
  222. /* An auxiliary function to add a path point with a specified transformation. */
  223. int gs_moveto_aux(gs_imager_state *pis, gx_path *ppath, floatp x, floatp y);
  224. int gx_setcurrentpoint_from_path(gs_imager_state *pis, gx_path *path);
  225. /* Path optimization for the filling algorithm. */
  226. int gx_path_merge_contacting_contours(gx_path *ppath);
  227. /* ------ Clipping paths ------ */
  228. /* Opaque type for a clipping path */
  229. #ifndef gx_clip_path_DEFINED
  230. # define gx_clip_path_DEFINED
  231. typedef struct gx_clip_path_s gx_clip_path;
  232. #endif
  233. /* Graphics state clipping */
  234. int gx_clip_to_rectangle(gs_state *, gs_fixed_rect *);
  235. int gx_clip_to_path(gs_state *);
  236. int gx_default_clip_box(const gs_state *, gs_fixed_rect *);
  237. int gx_effective_clip_path(gs_state *, gx_clip_path **);
  238. /* Opaque type for a clip list. */
  239. #ifndef gx_clip_list_DEFINED
  240. # define gx_clip_list_DEFINED
  241. typedef struct gx_clip_list_s gx_clip_list;
  242. #endif
  243. /* Opaque type for a clipping path enumerator. */
  244. typedef struct gs_cpath_enum_s gs_cpath_enum;
  245. /*
  246. * Provide similar memory management for clip paths to what we have for
  247. * paths (see above for details).
  248. */
  249. gx_clip_path *gx_cpath_alloc_shared(const gx_clip_path * shared,
  250. gs_memory_t * mem, client_name_t cname);
  251. #define gx_cpath_alloc(mem, cname)\
  252. gx_cpath_alloc_shared(NULL, mem, cname)
  253. int gx_cpath_init_contained_shared(gx_clip_path * pcpath,
  254. const gx_clip_path * shared,
  255. gs_memory_t * mem,
  256. client_name_t cname);
  257. #define gx_cpath_alloc_contained(pcpath, mem, cname)\
  258. gx_cpath_init_contained_shared(pcpath, NULL, mem, cname)
  259. int gx_cpath_init_local_shared(gx_clip_path * pcpath,
  260. const gx_clip_path * shared,
  261. gs_memory_t * mem);
  262. #define gx_cpath_init_local(pcpath, mem)\
  263. (void)gx_cpath_init_local_shared(pcpath, NULL, mem) /* can't fail */
  264. int gx_cpath_unshare(gx_clip_path * pcpath);
  265. void gx_cpath_free(gx_clip_path * pcpath, client_name_t cname);
  266. int gx_cpath_assign_preserve(gx_clip_path * pcpto, gx_clip_path * pcpfrom);
  267. int gx_cpath_assign_free(gx_clip_path * pcpto, gx_clip_path * pcpfrom);
  268. /* Clip path constructors and accessors */
  269. int
  270. gx_cpath_reset(gx_clip_path *), /* from_rectangle ((0,0),(0,0)) */
  271. gx_cpath_from_rectangle(gx_clip_path *, gs_fixed_rect *),
  272. gx_cpath_clip(gs_state *, gx_clip_path *, /*const*/ gx_path *, int),
  273. gx_cpath_intersect(gx_clip_path *, /*const*/ gx_path *, int,
  274. gs_imager_state *),
  275. gx_cpath_scale_exp2_shared(gx_clip_path *pcpath, int log2_scale_x,
  276. int log2_scale_y, bool list_shared,
  277. bool segments_shared),
  278. gx_cpath_to_path(gx_clip_path *, gx_path *);
  279. bool
  280. gx_cpath_inner_box(const gx_clip_path *, gs_fixed_rect *),
  281. gx_cpath_outer_box(const gx_clip_path *, gs_fixed_rect *),
  282. gx_cpath_includes_rectangle(const gx_clip_path *, fixed, fixed,
  283. fixed, fixed);
  284. const gs_fixed_rect *cpath_is_rectangle(const gx_clip_path * pcpath);
  285. /* Enumerate a clipping path. This interface does not copy the path. */
  286. /* However, it does write into the path's "visited" flags. */
  287. int gx_cpath_enum_init(gs_cpath_enum *, gx_clip_path *);
  288. int gx_cpath_enum_next(gs_cpath_enum *, gs_fixed_point[3]); /* 0 when done */
  289. segment_notes
  290. gx_cpath_enum_notes(const gs_cpath_enum *);
  291. #ifdef DEBUG
  292. void gx_cpath_print(const gx_clip_path *);
  293. #endif
  294. #endif /* gxpath_INCLUDED */