path.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #ifndef MUPDF_FITZ_PATH_H
  23. #define MUPDF_FITZ_PATH_H
  24. #include "mupdf/fitz/system.h"
  25. #include "mupdf/fitz/context.h"
  26. #include "mupdf/fitz/geometry.h"
  27. /**
  28. * Vector path buffer.
  29. * It can be stroked and dashed, or be filled.
  30. * It has a fill rule (nonzero or even_odd).
  31. *
  32. * When rendering, they are flattened, stroked and dashed straight
  33. * into the Global Edge List.
  34. */
  35. typedef struct fz_path fz_path;
  36. typedef enum
  37. {
  38. FZ_LINECAP_BUTT = 0,
  39. FZ_LINECAP_ROUND = 1,
  40. FZ_LINECAP_SQUARE = 2,
  41. FZ_LINECAP_TRIANGLE = 3
  42. } fz_linecap;
  43. typedef enum
  44. {
  45. FZ_LINEJOIN_MITER = 0,
  46. FZ_LINEJOIN_ROUND = 1,
  47. FZ_LINEJOIN_BEVEL = 2,
  48. FZ_LINEJOIN_MITER_XPS = 3
  49. } fz_linejoin;
  50. typedef struct
  51. {
  52. int refs;
  53. fz_linecap start_cap, dash_cap, end_cap;
  54. fz_linejoin linejoin;
  55. float linewidth;
  56. float miterlimit;
  57. float dash_phase;
  58. int dash_len;
  59. float dash_list[FZ_FLEXIBLE_ARRAY];
  60. } fz_stroke_state;
  61. typedef struct
  62. {
  63. /* Compulsory ones */
  64. void (*moveto)(fz_context *ctx, void *arg, float x, float y);
  65. void (*lineto)(fz_context *ctx, void *arg, float x, float y);
  66. void (*curveto)(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2, float x3, float y3);
  67. void (*closepath)(fz_context *ctx, void *arg);
  68. /* Optional ones */
  69. void (*quadto)(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2);
  70. void (*curvetov)(fz_context *ctx, void *arg, float x2, float y2, float x3, float y3);
  71. void (*curvetoy)(fz_context *ctx, void *arg, float x1, float y1, float x3, float y3);
  72. void (*rectto)(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2);
  73. } fz_path_walker;
  74. /**
  75. Walk the segments of a path, calling the
  76. appropriate callback function from a given set for each
  77. segment of the path.
  78. path: The path to walk.
  79. walker: The set of callback functions to use. The first
  80. 4 callback pointers in the set must be non-NULL. The
  81. subsequent ones can either be supplied, or can be left
  82. as NULL, in which case the top 4 functions will be
  83. called as appropriate to simulate them.
  84. arg: An opaque argument passed in to each callback.
  85. Exceptions will only be thrown if the underlying callback
  86. functions throw them.
  87. */
  88. void fz_walk_path(fz_context *ctx, const fz_path *path, const fz_path_walker *walker, void *arg);
  89. /**
  90. Create a new (empty) path structure.
  91. */
  92. fz_path *fz_new_path(fz_context *ctx);
  93. /**
  94. Increment the reference count. Returns the same pointer.
  95. All paths can be kept, regardless of their packing type.
  96. Never throws exceptions.
  97. */
  98. fz_path *fz_keep_path(fz_context *ctx, const fz_path *path);
  99. /**
  100. Decrement the reference count. When the reference count hits
  101. zero, free the path.
  102. All paths can be dropped, regardless of their packing type.
  103. Packed paths do not own the blocks into which they are packed
  104. so dropping them does not free those blocks.
  105. Never throws exceptions.
  106. */
  107. void fz_drop_path(fz_context *ctx, const fz_path *path);
  108. /**
  109. Minimise the internal storage used by a path.
  110. As paths are constructed, the internal buffers
  111. grow. To avoid repeated reallocations they
  112. grow with some spare space. Once a path has
  113. been fully constructed, this call allows the
  114. excess space to be trimmed.
  115. */
  116. void fz_trim_path(fz_context *ctx, fz_path *path);
  117. /**
  118. Return the number of bytes required to pack a path.
  119. */
  120. int fz_packed_path_size(const fz_path *path);
  121. /**
  122. Pack a path into the given block.
  123. To minimise the size of paths, this function allows them to be
  124. packed into a buffer with other information. Paths can be used
  125. interchangeably regardless of how they are packed.
  126. pack: Pointer to a block of data to pack the path into. Should
  127. be aligned by the caller to the same alignment as required for
  128. a fz_path pointer.
  129. path: The path to pack.
  130. Returns the number of bytes within the block used. Callers can
  131. access the packed path data by casting the value of pack on
  132. entry to be a fz_path *.
  133. Throws exceptions on failure to allocate.
  134. Implementation details: Paths can be 'unpacked', 'flat', or
  135. 'open'. Standard paths, as created are 'unpacked'. Paths
  136. will be packed as 'flat', unless they are too large
  137. (where large indicates that they exceed some private
  138. implementation defined limits, currently including having
  139. more than 256 coordinates or commands).
  140. Large paths are 'open' packed as a header into the given block,
  141. plus pointers to other data blocks.
  142. Users should not have to care about whether paths are 'open'
  143. or 'flat' packed. Simply pack a path (if required), and then
  144. forget about the details.
  145. */
  146. size_t fz_pack_path(fz_context *ctx, uint8_t *pack, const fz_path *path);
  147. /**
  148. Clone the data for a path.
  149. This is used in preference to fz_keep_path when a whole
  150. new copy of a path is required, rather than just a shared
  151. pointer. This probably indicates that the path is about to
  152. be modified.
  153. path: path to clone.
  154. Throws exceptions on failure to allocate.
  155. */
  156. fz_path *fz_clone_path(fz_context *ctx, fz_path *path);
  157. /**
  158. Return the current point that a path has
  159. reached or (0,0) if empty.
  160. path: path to return the current point of.
  161. */
  162. fz_point fz_currentpoint(fz_context *ctx, fz_path *path);
  163. /**
  164. Append a 'moveto' command to a path.
  165. This 'opens' a path.
  166. path: The path to modify.
  167. x, y: The coordinate to move to.
  168. Throws exceptions on failure to allocate, or attempting to
  169. modify a packed path.
  170. */
  171. void fz_moveto(fz_context *ctx, fz_path *path, float x, float y);
  172. /**
  173. Append a 'lineto' command to an open path.
  174. path: The path to modify.
  175. x, y: The coordinate to line to.
  176. Throws exceptions on failure to allocate, or attempting to
  177. modify a packed path.
  178. */
  179. void fz_lineto(fz_context *ctx, fz_path *path, float x, float y);
  180. /**
  181. Append a 'rectto' command to an open path.
  182. The rectangle is equivalent to:
  183. moveto x0 y0
  184. lineto x1 y0
  185. lineto x1 y1
  186. lineto x0 y1
  187. closepath
  188. path: The path to modify.
  189. x0, y0: First corner of the rectangle.
  190. x1, y1: Second corner of the rectangle.
  191. Throws exceptions on failure to allocate, or attempting to
  192. modify a packed path.
  193. */
  194. void fz_rectto(fz_context *ctx, fz_path *path, float x0, float y0, float x1, float y1);
  195. /**
  196. Append a 'quadto' command to an open path. (For a
  197. quadratic bezier).
  198. path: The path to modify.
  199. x0, y0: The control coordinates for the quadratic curve.
  200. x1, y1: The end coordinates for the quadratic curve.
  201. Throws exceptions on failure to allocate, or attempting to
  202. modify a packed path.
  203. */
  204. void fz_quadto(fz_context *ctx, fz_path *path, float x0, float y0, float x1, float y1);
  205. /**
  206. Append a 'curveto' command to an open path. (For a
  207. cubic bezier).
  208. path: The path to modify.
  209. x0, y0: The coordinates of the first control point for the
  210. curve.
  211. x1, y1: The coordinates of the second control point for the
  212. curve.
  213. x2, y2: The end coordinates for the curve.
  214. Throws exceptions on failure to allocate, or attempting to
  215. modify a packed path.
  216. */
  217. void fz_curveto(fz_context *ctx, fz_path *path, float x0, float y0, float x1, float y1, float x2, float y2);
  218. /**
  219. Append a 'curvetov' command to an open path. (For a
  220. cubic bezier with the first control coordinate equal to
  221. the start point).
  222. path: The path to modify.
  223. x1, y1: The coordinates of the second control point for the
  224. curve.
  225. x2, y2: The end coordinates for the curve.
  226. Throws exceptions on failure to allocate, or attempting to
  227. modify a packed path.
  228. */
  229. void fz_curvetov(fz_context *ctx, fz_path *path, float x1, float y1, float x2, float y2);
  230. /**
  231. Append a 'curvetoy' command to an open path. (For a
  232. cubic bezier with the second control coordinate equal to
  233. the end point).
  234. path: The path to modify.
  235. x0, y0: The coordinates of the first control point for the
  236. curve.
  237. x2, y2: The end coordinates for the curve (and the second
  238. control coordinate).
  239. Throws exceptions on failure to allocate, or attempting to
  240. modify a packed path.
  241. */
  242. void fz_curvetoy(fz_context *ctx, fz_path *path, float x0, float y0, float x2, float y2);
  243. /**
  244. Close the current subpath.
  245. path: The path to modify.
  246. Throws exceptions on failure to allocate, attempting to modify
  247. a packed path, and illegal path closes (i.e. closing a non open
  248. path).
  249. */
  250. void fz_closepath(fz_context *ctx, fz_path *path);
  251. /**
  252. Transform a path by a given
  253. matrix.
  254. path: The path to modify (must not be a packed path).
  255. transform: The transform to apply.
  256. Throws exceptions if the path is packed, or on failure
  257. to allocate.
  258. */
  259. void fz_transform_path(fz_context *ctx, fz_path *path, fz_matrix transform);
  260. /**
  261. Return a bounding rectangle for a path.
  262. path: The path to bound.
  263. stroke: If NULL, the bounding rectangle given is for
  264. the filled path. If non-NULL the bounding rectangle
  265. given is for the path stroked with the given attributes.
  266. ctm: The matrix to apply to the path during stroking.
  267. r: Pointer to a fz_rect which will be used to hold
  268. the result.
  269. Returns r, updated to contain the bounding rectangle.
  270. */
  271. fz_rect fz_bound_path(fz_context *ctx, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm);
  272. /**
  273. Given a rectangle (assumed to be the bounding box for a path),
  274. expand it to allow for the expansion of the bbox that would be
  275. seen by stroking the path with the given stroke state and
  276. transform.
  277. */
  278. fz_rect fz_adjust_rect_for_stroke(fz_context *ctx, fz_rect rect, const fz_stroke_state *stroke, fz_matrix ctm);
  279. /**
  280. A sane 'default' stroke state.
  281. */
  282. FZ_DATA extern const fz_stroke_state fz_default_stroke_state;
  283. /**
  284. Create a new (empty) stroke state structure (with no dash
  285. data) and return a reference to it.
  286. Throws exception on failure to allocate.
  287. */
  288. fz_stroke_state *fz_new_stroke_state(fz_context *ctx);
  289. /**
  290. Create a new (empty) stroke state structure, with room for
  291. dash data of the given length, and return a reference to it.
  292. len: The number of dash elements to allow room for.
  293. Throws exception on failure to allocate.
  294. */
  295. fz_stroke_state *fz_new_stroke_state_with_dash_len(fz_context *ctx, int len);
  296. /**
  297. Take an additional reference to a stroke state structure.
  298. No modifications should be carried out on a stroke
  299. state to which more than one reference is held, as
  300. this can cause race conditions.
  301. */
  302. fz_stroke_state *fz_keep_stroke_state(fz_context *ctx, const fz_stroke_state *stroke);
  303. int fz_stroke_state_eq(fz_context *ctx, const fz_stroke_state *a, const fz_stroke_state *b);
  304. /**
  305. Drop a reference to a stroke state structure, destroying the
  306. structure if it is the last reference.
  307. */
  308. void fz_drop_stroke_state(fz_context *ctx, const fz_stroke_state *stroke);
  309. /**
  310. Given a reference to a (possibly) shared stroke_state structure,
  311. return a reference to an equivalent stroke_state structure
  312. that is guaranteed to be unshared (i.e. one that can
  313. safely be modified).
  314. shared: The reference to a (possibly) shared structure
  315. to unshare. Ownership of this reference is passed in
  316. to this function, even in the case of exceptions being
  317. thrown.
  318. Exceptions may be thrown in the event of failure to
  319. allocate if required.
  320. */
  321. fz_stroke_state *fz_unshare_stroke_state(fz_context *ctx, fz_stroke_state *shared);
  322. /**
  323. Given a reference to a (possibly) shared stroke_state structure,
  324. return a reference to a stroke_state structure (with room for a
  325. given amount of dash data) that is guaranteed to be unshared
  326. (i.e. one that can safely be modified).
  327. shared: The reference to a (possibly) shared structure
  328. to unshare. Ownership of this reference is passed in
  329. to this function, even in the case of exceptions being
  330. thrown.
  331. Exceptions may be thrown in the event of failure to
  332. allocate if required.
  333. */
  334. fz_stroke_state *fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared, int len);
  335. /**
  336. Create an identical stroke_state structure and return a
  337. reference to it.
  338. stroke: The stroke state reference to clone.
  339. Exceptions may be thrown in the event of a failure to
  340. allocate.
  341. */
  342. fz_stroke_state *fz_clone_stroke_state(fz_context *ctx, const fz_stroke_state *stroke);
  343. fz_linecap fz_linecap_from_string(const char *s);
  344. const char *fz_string_from_linecap(fz_linecap cap);
  345. fz_linejoin fz_linejoin_from_string(const char *s);
  346. const char *fz_string_from_linejoin(fz_linejoin join);
  347. /**
  348. Check whether a given path, under the given transform
  349. is an axis-aligned rectangle.
  350. We accept zero width or height rectangles, so
  351. "move 100, 100; line 200, 100" would count as
  352. a rectangle too.
  353. */
  354. int fz_path_is_rect(fz_context *ctx, const fz_path *path, fz_matrix ctm);
  355. /**
  356. Check whether a given path, under the given transform
  357. is an axis-aligned rectangle.
  358. We accept zero width or height rectangles, so
  359. "move 100, 100; line 200, 100" would count as
  360. a rectangle too.
  361. bounds = NULL, or place to return the rectangle
  362. bounds if the path is a rectangle.
  363. */
  364. int fz_path_is_rect_with_bounds(fz_context *ctx, const fz_path *path, fz_matrix ctm, fz_rect *bounds);
  365. #endif