path.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #ifndef MUPDF_FITZ_PATH_H
  2. #define MUPDF_FITZ_PATH_H
  3. #include "mupdf/fitz/system.h"
  4. #include "mupdf/fitz/context.h"
  5. #include "mupdf/fitz/geometry.h"
  6. /*
  7. * Vector path buffer.
  8. * It can be stroked and dashed, or be filled.
  9. * It has a fill rule (nonzero or even_odd).
  10. *
  11. * When rendering, they are flattened, stroked and dashed straight
  12. * into the Global Edge List.
  13. */
  14. typedef struct fz_path_s fz_path;
  15. typedef struct fz_stroke_state_s fz_stroke_state;
  16. typedef enum fz_linecap_e
  17. {
  18. FZ_LINECAP_BUTT = 0,
  19. FZ_LINECAP_ROUND = 1,
  20. FZ_LINECAP_SQUARE = 2,
  21. FZ_LINECAP_TRIANGLE = 3
  22. } fz_linecap;
  23. typedef enum fz_linejoin_e
  24. {
  25. FZ_LINEJOIN_MITER = 0,
  26. FZ_LINEJOIN_ROUND = 1,
  27. FZ_LINEJOIN_BEVEL = 2,
  28. FZ_LINEJOIN_MITER_XPS = 3
  29. } fz_linejoin;
  30. struct fz_stroke_state_s
  31. {
  32. int refs;
  33. fz_linecap start_cap, dash_cap, end_cap;
  34. fz_linejoin linejoin;
  35. float linewidth;
  36. float miterlimit;
  37. float dash_phase;
  38. int dash_len;
  39. float dash_list[32];
  40. };
  41. typedef struct
  42. {
  43. /* Compulsory ones */
  44. void (*moveto)(fz_context *ctx, void *arg, float x, float y);
  45. void (*lineto)(fz_context *ctx, void *arg, float x, float y);
  46. void (*curveto)(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2, float x3, float y3);
  47. void (*closepath)(fz_context *ctx, void *arg);
  48. /* Optional ones */
  49. void (*quadto)(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2);
  50. void (*curvetov)(fz_context *ctx, void *arg, float x2, float y2, float x3, float y3);
  51. void (*curvetoy)(fz_context *ctx, void *arg, float x1, float y1, float x3, float y3);
  52. void (*rectto)(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2);
  53. } fz_path_walker;
  54. /*
  55. fz_walk_path: Walk the segments of a path, calling the
  56. appropriate callback function from a given set for each
  57. segment of the path.
  58. path: The path to walk.
  59. walker: The set of callback functions to use. The first
  60. 4 callback pointers in the set must be non-NULL. The
  61. subsequent ones can either be supplied, or can be left
  62. as NULL, in which case the top 4 functions will be
  63. called as appropriate to simulate them.
  64. arg: An opaque argument passed in to each callback.
  65. Exceptions will only be thrown if the underlying callback
  66. functions throw them.
  67. */
  68. void fz_walk_path(fz_context *ctx, const fz_path *path, const fz_path_walker *walker, void *arg);
  69. /*
  70. fz_new_path: Create an empty path, and return
  71. a reference to it.
  72. Throws exception on failure to allocate.
  73. */
  74. fz_path *fz_new_path(fz_context *ctx);
  75. /*
  76. fz_keep_path: Take an additional reference to
  77. a path.
  78. No modifications should be carried out on a path
  79. to which more than one reference is held, as
  80. this can cause race conditions.
  81. Never throws exceptions.
  82. */
  83. fz_path *fz_keep_path(fz_context *ctx, const fz_path *path);
  84. /*
  85. fz_drop_path: Drop a reference to a path,
  86. destroying the path if it is the last
  87. reference.
  88. Never throws exceptions.
  89. */
  90. void fz_drop_path(fz_context *ctx, const fz_path *path);
  91. /*
  92. fz_trim_path: Minimise the internal storage
  93. used by a path.
  94. As paths are constructed, the internal buffers
  95. grow. To avoid repeated reallocations they
  96. grow with some spare space. Once a path has
  97. been fully constructed, this call allows the
  98. excess space to be trimmed.
  99. Never throws exceptions.
  100. */
  101. void fz_trim_path(fz_context *ctx, fz_path *path);
  102. /*
  103. fz_packed_path_size: Return the number of
  104. bytes required to pack a path.
  105. Never throws exceptions.
  106. */
  107. int fz_packed_path_size(const fz_path *path);
  108. /*
  109. fz_pack_path: Pack a path into the given block.
  110. To minimise the size of paths, this function allows them to be
  111. packed into a buffer with other information. Paths can be used
  112. interchangeably regardless of how they are packed.
  113. pack: Pointer to a block of data to pack the path into. Should
  114. be aligned by the caller to the same alignment as required for
  115. a fz_path pointer.
  116. max: The number of bytes available in the block.
  117. If max < sizeof(fz_path) then an exception will
  118. be thrown. If max >= the value returned by
  119. fz_packed_path_size, then this call will never
  120. fail, except in low memory situations with large
  121. paths.
  122. path: The path to pack.
  123. Returns the number of bytes within the block used. Callers can
  124. access the packed path data by casting the value of pack on
  125. entry to be a fz_path *.
  126. Throws exceptions on failure to allocate, or if
  127. max < sizeof(fz_path).
  128. Implementation details: Paths can be 'unpacked', 'flat', or
  129. 'open'. Standard paths, as created are 'unpacked'. Paths that
  130. will pack into less than max bytes will be packed as 'flat',
  131. unless they are too large (where large indicates that they
  132. exceed some private implementation defined limits, currently
  133. including having more than 256 coordinates or commands).
  134. Large paths are 'open' packed as a header into the given block,
  135. plus pointers to other data blocks.
  136. Users should not have to care about whether paths are 'open'
  137. or 'flat' packed. Simply pack a path (if required), and then
  138. forget about the details.
  139. */
  140. int fz_pack_path(fz_context *ctx, uint8_t *pack, int max, const fz_path *path);
  141. /*
  142. fz_clone_path: Clone the data for a path.
  143. This is used in preference to fz_keep_path when a whole
  144. new copy of a path is required, rather than just a shared
  145. pointer. This probably indicates that the path is about to
  146. be modified.
  147. path: path to clone.
  148. Throws exceptions on failure to allocate.
  149. */
  150. fz_path *fz_clone_path(fz_context *ctx, fz_path *path);
  151. /*
  152. fz_currentpoint: Return the current point that a path has
  153. reached or (0,0) if empty.
  154. path: path to return the current point of.
  155. */
  156. fz_point fz_currentpoint(fz_context *ctx, fz_path *path);
  157. /*
  158. fz_moveto: Append a 'moveto' command to a path.
  159. This 'opens' a path.
  160. path: The path to modify.
  161. x, y: The coordinate to move to.
  162. Throws exceptions on failure to allocate.
  163. */
  164. void fz_moveto(fz_context *ctx, fz_path *path, float x, float y);
  165. /*
  166. fz_lineto: Append a 'lineto' command to an open path.
  167. path: The path to modify.
  168. x, y: The coordinate to line to.
  169. Throws exceptions on failure to allocate.
  170. */
  171. void fz_lineto(fz_context *ctx, fz_path *path, float x, float y);
  172. /*
  173. fz_rectto: Append a 'rectto' command to an open path.
  174. The rectangle is equivalent to:
  175. moveto x0 y0
  176. lineto x1 y0
  177. lineto x1 y1
  178. lineto x0 y1
  179. closepath
  180. path: The path to modify.
  181. x0, y0: First corner of the rectangle.
  182. x1, y1: Second corner of the rectangle.
  183. Throws exceptions on failure to allocate.
  184. */
  185. void fz_rectto(fz_context *ctx, fz_path *path, float x0, float y0, float x1, float y1);
  186. /*
  187. fz_quadto: Append a 'quadto' command to an open path. (For a
  188. quadratic bezier).
  189. path: The path to modify.
  190. x0, y0: The control coordinates for the quadratic curve.
  191. x1, y1: The end coordinates for the quadratic curve.
  192. Throws exceptions on failure to allocate.
  193. */
  194. void fz_quadto(fz_context *ctx, fz_path *path, float x0, float y0, float x1, float y1);
  195. /*
  196. fz_curveto: Append a 'curveto' command to an open path. (For a
  197. cubic bezier).
  198. path: The path to modify.
  199. x0, y0: The coordinates of the first control point for the
  200. curve.
  201. x1, y1: The coordinates of the second control point for the
  202. curve.
  203. x2, y2: The end coordinates for the curve.
  204. Throws exceptions on failure to allocate.
  205. */
  206. void fz_curveto(fz_context *ctx, fz_path *path, float x0, float y0, float x1, float y1, float x2, float y2);
  207. /*
  208. fz_curvetov: Append a 'curvetov' command to an open path. (For a
  209. cubic bezier with the first control coordinate equal to
  210. the start point).
  211. path: The path to modify.
  212. x1, y1: The coordinates of the second control point for the
  213. curve.
  214. x2, y2: The end coordinates for the curve.
  215. Throws exceptions on failure to allocate.
  216. */
  217. void fz_curvetov(fz_context *ctx, fz_path *path, float x1, float y1, float x2, float y2);
  218. /*
  219. fz_curvetoy: Append a 'curvetoy' command to an open path. (For a
  220. cubic bezier with the second control coordinate equal to
  221. the end point).
  222. path: The path to modify.
  223. x0, y0: The coordinates of the first control point for the
  224. curve.
  225. x2, y2: The end coordinates for the curve (and the second
  226. control coordinate).
  227. Throws exceptions on failure to allocate.
  228. */
  229. void fz_curvetoy(fz_context *ctx, fz_path *path, float x0, float y0, float x2, float y2);
  230. /*
  231. fz_closepath: Close the current subpath.
  232. path: The path to modify.
  233. Throws exceptions on failure to allocate, and illegal
  234. path closes (i.e. closing a non open path).
  235. */
  236. void fz_closepath(fz_context *ctx, fz_path *path);
  237. /*
  238. fz_transform_path: Transform a path by a given
  239. matrix.
  240. path: The path to modify (must not be a packed path).
  241. transform: The transform to apply.
  242. Throws exceptions if the path is packed, or on failure
  243. to allocate.
  244. */
  245. void fz_transform_path(fz_context *ctx, fz_path *path, const fz_matrix *transform);
  246. /*
  247. fz_bound_path: Return a bounding rectangle for a path.
  248. path: The path to bound.
  249. stroke: If NULL, the bounding rectangle given is for
  250. the filled path. If non-NULL the bounding rectangle
  251. given is for the path stroked with the given attributes.
  252. ctm: The matrix to apply to the path during stroking.
  253. r: Pointer to a fz_rect which will be used to hold
  254. the result.
  255. Returns r, updated to contain the bounding rectangle.
  256. */
  257. fz_rect *fz_bound_path(fz_context *ctx, const fz_path *path, const fz_stroke_state *stroke, const fz_matrix *ctm, fz_rect *r);
  258. fz_rect *fz_adjust_rect_for_stroke(fz_context *ctx, fz_rect *r, const fz_stroke_state *stroke, const fz_matrix *ctm);
  259. extern const fz_stroke_state fz_default_stroke_state;
  260. /*
  261. fz_new_stroke_state: Create a new (empty) stroke state
  262. structure (with no dash data) and return a reference to it.
  263. Throws exception on failure to allocate.
  264. */
  265. fz_stroke_state *fz_new_stroke_state(fz_context *ctx);
  266. /*
  267. fz_new_stroke_state_with_dash_len: Create a new (empty)
  268. stroke state structure, with room for dash data of the
  269. given length, and return a reference to it.
  270. len: The number of dash elements to allow room for.
  271. Throws exception on failure to allocate.
  272. */
  273. fz_stroke_state *fz_new_stroke_state_with_dash_len(fz_context *ctx, int len);
  274. /*
  275. fz_keep_stroke_state: Take an additional reference to
  276. a stroke state structure.
  277. No modifications should be carried out on a stroke
  278. state to which more than one reference is held, as
  279. this can cause race conditions.
  280. Never throws exceptions.
  281. */
  282. fz_stroke_state *fz_keep_stroke_state(fz_context *ctx, const fz_stroke_state *stroke);
  283. /*
  284. fz_drop_stroke_state: Drop a reference to a stroke
  285. state structure, destroying the structure if it is
  286. the last reference.
  287. Never throws exceptions.
  288. */
  289. void fz_drop_stroke_state(fz_context *ctx, const fz_stroke_state *stroke);
  290. /*
  291. fz_unshare_stroke_state: Given a reference to a
  292. (possibly) shared stroke_state structure, return
  293. a reference to an equivalent stroke_state structure
  294. that is guaranteed to be unshared (i.e. one that can
  295. safely be modified).
  296. shared: The reference to a (possibly) shared structure
  297. to unshare. Ownership of this reference is passed in
  298. to this function, even in the case of exceptions being
  299. thrown.
  300. Exceptions may be thrown in the event of failure to
  301. allocate if required.
  302. */
  303. fz_stroke_state *fz_unshare_stroke_state(fz_context *ctx, fz_stroke_state *shared);
  304. /*
  305. fz_unshare_stroke_state_with_dash_len: Given a reference to a
  306. (possibly) shared stroke_state structure, return a reference
  307. to a stroke_state structure (with room for a given amount of
  308. dash data) that is guaranteed to be unshared (i.e. one that
  309. can safely be modified).
  310. shared: The reference to a (possibly) shared structure
  311. to unshare. Ownership of this reference is passed in
  312. to this function, even in the case of exceptions being
  313. thrown.
  314. Exceptions may be thrown in the event of failure to
  315. allocate if required.
  316. */
  317. fz_stroke_state *fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared, int len);
  318. /*
  319. fz_clone_stroke_state: Create an identical stroke_state
  320. structure and return a reference to it.
  321. stroke: The stroke state reference to clone.
  322. Exceptions may be thrown in the event of a failure to
  323. allocate.
  324. */
  325. fz_stroke_state *fz_clone_stroke_state(fz_context *ctx, fz_stroke_state *stroke);
  326. /*
  327. fz_print_path: Print a debug representation of a path to
  328. a given output stream. Not for use in production builds.
  329. out: The output stream to print to.
  330. path: The path to output.
  331. indent: The amount to indent the output by.
  332. */
  333. void fz_print_path(fz_context *ctx, fz_output *out, fz_path *, int indent);
  334. #endif