draw-imp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Copyright (C) 2004-2021 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_DRAW_IMP_H
  23. #define MUPDF_DRAW_IMP_H
  24. #define BBOX_MIN -(1<<20)
  25. #define BBOX_MAX (1<<20)
  26. /* divide and floor towards -inf */
  27. static inline int fz_idiv(int a, int b)
  28. {
  29. return a < 0 ? (a - b + 1) / b : a / b;
  30. }
  31. /* divide and ceil towards inf */
  32. static inline int fz_idiv_up(int a, int b)
  33. {
  34. return a < 0 ? a / b : (a + b - 1) / b;
  35. }
  36. #ifdef AA_BITS
  37. #define fz_aa_scale 0
  38. #define fz_rasterizer_aa_scale(ras) 0
  39. #if AA_BITS > 6
  40. #define AA_SCALE(s, x) (x)
  41. #define fz_aa_hscale 17
  42. #define fz_aa_vscale 15
  43. #define fz_aa_bits 8
  44. #define fz_aa_text_bits 8
  45. #define fz_rasterizer_aa_hscale(ras) 17
  46. #define fz_rasterizer_aa_vscale(ras) 15
  47. #define fz_rasterizer_aa_bits(ras) 8
  48. #define fz_rasterizer_aa_text_bits(ras) 8
  49. #elif AA_BITS > 4
  50. #define AA_SCALE(s, x) ((x * 255) >> 6)
  51. #define fz_aa_hscale 8
  52. #define fz_aa_vscale 8
  53. #define fz_aa_bits 6
  54. #define fz_aa_text_bits 6
  55. #define fz_rasterizer_aa_hscale(ras) 8
  56. #define fz_rasterizer_aa_vscale(ras) 8
  57. #define fz_rasterizer_aa_bits(ras) 6
  58. #define fz_rasterizer_aa_text_bits(ras) 6
  59. #elif AA_BITS > 2
  60. #define AA_SCALE(s, x) (x * 17)
  61. #define fz_aa_hscale 5
  62. #define fz_aa_vscale 3
  63. #define fz_aa_bits 4
  64. #define fz_aa_text_bits 4
  65. #define fz_rasterizer_aa_hscale(ras) 5
  66. #define fz_rasterizer_aa_vscale(ras) 3
  67. #define fz_rasterizer_aa_bits(ras) 4
  68. #define fz_rasterizer_aa_text_bits(ras) 4
  69. #elif AA_BITS > 0
  70. #define AA_SCALE(s, x) ((x * 255) >> 2)
  71. #define fz_aa_hscale 2
  72. #define fz_aa_vscale 2
  73. #define fz_aa_bits 2
  74. #define fz_aa_text_bits 2
  75. #define fz_rasterizer_aa_hscale(ras) 2
  76. #define fz_rasterizer_aa_vscale(ras) 2
  77. #define fz_rasterizer_aa_bits(ras) 2
  78. #define fz_rasterizer_aa_text_bits(ras) 2
  79. #else
  80. #define AA_SCALE(s, x) (x * 255)
  81. #define fz_aa_hscale 1
  82. #define fz_aa_vscale 1
  83. #define fz_aa_bits 0
  84. #define fz_aa_text_bits 0
  85. #define fz_rasterizer_aa_hscale(ras) 1
  86. #define fz_rasterizer_aa_vscale(ras) 1
  87. #define fz_rasterizer_aa_bits(ras) 0
  88. #define fz_rasterizer_aa_text_bits(ras) 0
  89. #endif
  90. #else
  91. #define AA_SCALE(scale, x) ((x * scale) >> 8)
  92. #define fz_aa_hscale (ctx->aa.hscale)
  93. #define fz_aa_vscale (ctx->aa.vscale)
  94. #define fz_aa_scale (ctx->aa.scale)
  95. #define fz_aa_bits (ctx->aa.bits)
  96. #define fz_aa_text_bits (ctx->aa.text_bits)
  97. #define fz_rasterizer_aa_hscale(ras) ((ras)->aa.hscale)
  98. #define fz_rasterizer_aa_vscale(ras) ((ras)->aa.vscale)
  99. #define fz_rasterizer_aa_scale(ras) ((ras)->aa.scale)
  100. #define fz_rasterizer_aa_bits(ras) ((ras)->aa.bits)
  101. #define fz_rasterizer_aa_text_bits(ras) ((ras)->aa.text_bits)
  102. #endif
  103. /* If AA_BITS is defined, then we assume constant N bits of antialiasing. We
  104. * will attempt to provide at least that number of bits of accuracy in the
  105. * antialiasing (to a maximum of 8). If it is defined to be 0 then no
  106. * antialiasing is done. If it is undefined to we will leave the antialiasing
  107. * accuracy as a run time choice.
  108. */
  109. struct fz_overprint
  110. {
  111. /* Bit i set -> never alter this color */
  112. uint32_t mask[(FZ_MAX_COLORS+31)/32];
  113. };
  114. static void inline fz_set_overprint(fz_overprint *op, int i)
  115. {
  116. op->mask[i>>5] |= 1<<(i&31);
  117. }
  118. static int inline fz_overprint_component(const fz_overprint *op, int i)
  119. {
  120. return ((op->mask[i>>5]>>(i & 31)) & 1) == 0;
  121. }
  122. static int inline fz_overprint_required(const fz_overprint *op)
  123. {
  124. int i;
  125. if (op == NULL)
  126. return 0;
  127. for (i = 0; i < (FZ_MAX_COLORS+31)/32; i++)
  128. if (op->mask[i] != 0)
  129. return 1;
  130. return 0;
  131. }
  132. typedef struct fz_rasterizer fz_rasterizer;
  133. typedef void (fz_rasterizer_drop_fn)(fz_context *ctx, fz_rasterizer *r);
  134. typedef int (fz_rasterizer_reset_fn)(fz_context *ctx, fz_rasterizer *r);
  135. typedef void (fz_rasterizer_postindex_fn)(fz_context *ctx, fz_rasterizer *r);
  136. typedef void (fz_rasterizer_insert_fn)(fz_context *ctx, fz_rasterizer *r, float x0, float y0, float x1, float y1, int rev);
  137. typedef void (fz_rasterizer_insert_rect_fn)(fz_context *ctx, fz_rasterizer *r, float fx0, float fy0, float fx1, float fy1);
  138. typedef void (fz_rasterizer_gap_fn)(fz_context *ctx, fz_rasterizer *r);
  139. typedef fz_irect *(fz_rasterizer_bound_fn)(fz_context *ctx, const fz_rasterizer *r, fz_irect *bbox);
  140. typedef void (fz_rasterizer_fn)(fz_context *ctx, fz_rasterizer *r, int eofill, const fz_irect *clip, fz_pixmap *pix, unsigned char *colorbv, fz_overprint *eop);
  141. typedef int (fz_rasterizer_is_rect_fn)(fz_context *ctx, fz_rasterizer *r);
  142. typedef struct
  143. {
  144. fz_rasterizer_drop_fn *drop;
  145. fz_rasterizer_reset_fn *reset;
  146. fz_rasterizer_postindex_fn *postindex;
  147. fz_rasterizer_insert_fn *insert;
  148. fz_rasterizer_insert_rect_fn *rect;
  149. fz_rasterizer_gap_fn *gap;
  150. fz_rasterizer_fn *convert;
  151. fz_rasterizer_is_rect_fn *is_rect;
  152. int reusable;
  153. } fz_rasterizer_fns;
  154. struct fz_rasterizer
  155. {
  156. fz_rasterizer_fns fns;
  157. fz_aa_context aa;
  158. fz_irect clip; /* Specified clip rectangle */
  159. fz_irect bbox; /* Measured bbox of path while stroking/filling */
  160. };
  161. /*
  162. When rasterizing a shape, we first create a rasterizer then
  163. run through the edges of the shape, feeding them in.
  164. For a fill, this is easy as we just run along the path, feeding
  165. edges as we go.
  166. For a stroke, this is trickier, as we feed in edges from
  167. alternate sides of the stroke as we proceed along it. It is only
  168. when we reach the end of a subpath that we know whether we need
  169. an initial cap, or whether the list of edges match up.
  170. To identify whether a given edge fed in is forward or reverse,
  171. we tag it with a 'rev' value.
  172. Consider the following simplified example:
  173. Consider a simple path A, B, C, D, close.
  174. +------->-------+ The outside edge of this shape is the
  175. | A B | forward edge. This is fed into the rasterizer
  176. | +---<---+ | in order, with rev=0.
  177. | | | |
  178. ^ v ^ v The inside edge of this shape is the reverse
  179. | | | | edge. These edges are generated as we step
  180. | +--->---+ | through the path in clockwise order, but
  181. | D C | conceptually the path runs the other way.
  182. +-------<-------+ These are fed into the rasterizer in clockwise
  183. order, with rev=1.
  184. Consider another path, this time an open one: A,B,C,D
  185. +--->-------+ The outside edge of this shape is again the
  186. * A B | forward edge. This is fed into the rasterizer
  187. +---<---+ | in order, with rev=0.
  188. | |
  189. ^ v The inside edge of this shape is the reverse
  190. | | edge. These edges are generated as we step
  191. +--->---+ | through the path in clockwise order, but
  192. ^ D C | conceptually the path runs the other way.
  193. +---<-------+ These are fed into the rasterizer in clockwise
  194. order, with rev=1.
  195. At the end of the path, we realise that this is an open path, and we
  196. therefore have to put caps on. The cap at 'D' is easy, because it's
  197. a simple continuation of the rev=0 edge list that joins to the end
  198. of the rev=1 edge list.
  199. The cap at 'A' is trickier; it either needs to be (an) edge(s) prepended
  200. to the rev=0 list or the rev=1 list. We signal this special case by
  201. sending them with the special value rev=2.
  202. The "edge" rasterizer ignores these values. The "edgebuffer" rasterizer
  203. needs to use them to ensure that edges are correctly joined together
  204. to allow for any part of a pixel operation.
  205. */
  206. /*
  207. fz_new_rasterizer: Create a new rasterizer instance.
  208. This encapsulates a scan converter.
  209. A single rasterizer instance can be used to scan convert many
  210. things.
  211. aa: The antialiasing settings to use (or NULL).
  212. */
  213. fz_rasterizer *fz_new_rasterizer(fz_context *ctx, const fz_aa_context *aa);
  214. /*
  215. fz_drop_rasterizer: Dispose of a rasterizer once
  216. finished with.
  217. */
  218. static inline void fz_drop_rasterizer(fz_context *ctx, fz_rasterizer *r)
  219. {
  220. if (r)
  221. r->fns.drop(ctx, r);
  222. }
  223. /*
  224. fz_reset_rasterizer: Reset a rasterizer, ready to scan convert
  225. a new shape.
  226. clip: A pointer to a (device space) clipping rectangle.
  227. Returns 1 if a indexing pass is required, or 0 if not.
  228. After this, the edges should be 'inserted' into the rasterizer.
  229. */
  230. int fz_reset_rasterizer(fz_context *ctx, fz_rasterizer *r, fz_irect clip);
  231. /*
  232. fz_insert_rasterizer: Insert an edge into a rasterizer.
  233. x0, y0: Initial point
  234. x1, y1: Final point
  235. rev: 'reverse' value, 0, 1 or 2. See above.
  236. */
  237. static inline void fz_insert_rasterizer(fz_context *ctx, fz_rasterizer *r, float x0, float y0, float x1, float y1, int rev)
  238. {
  239. r->fns.insert(ctx, r, x0, y0, x1, y1, rev);
  240. }
  241. /*
  242. fz_insert_rasterizer_rect: Insert a rectangle into a rasterizer.
  243. x0, y0: One corner of the rectangle.
  244. x1, y1: The opposite corner of the rectangle.
  245. The rectangle inserted is conceptually:
  246. (x0,y0)->(x1,y0)->(x1,y1)->(x0,y1)->(x0,y0).
  247. This method is only used for axis aligned rectangles,
  248. and enables rasterizers to perform special 'anti-dropout'
  249. processing to ensure that horizontal artifacts aren't
  250. lost.
  251. */
  252. static inline void fz_insert_rasterizer_rect(fz_context *ctx, fz_rasterizer *r, float x0, float y0, float x1, float y1)
  253. {
  254. r->fns.rect(ctx, r, x0, y0, x1, y1);
  255. }
  256. /*
  257. fz_gap_rasterizer: Called to indicate that there is a gap
  258. in the lists of edges fed into the rasterizer (i.e. when
  259. a path hits a move).
  260. */
  261. static inline void fz_gap_rasterizer(fz_context *ctx, fz_rasterizer *r)
  262. {
  263. if (r->fns.gap)
  264. r->fns.gap(ctx, r);
  265. }
  266. /*
  267. fz_antidropout_rasterizer: Detect whether antidropout
  268. behaviour is required with this rasterizer.
  269. Returns 1 if required, 0 otherwise.
  270. */
  271. static inline int fz_antidropout_rasterizer(fz_context *ctx, fz_rasterizer *r)
  272. {
  273. return r->fns.rect != NULL;
  274. }
  275. /*
  276. fz_postindex_rasterizer: Called to signify the end of the
  277. indexing phase.
  278. After this has been called, the edges should be inserted
  279. again.
  280. */
  281. static inline void fz_postindex_rasterizer(fz_context *ctx, fz_rasterizer *r)
  282. {
  283. if (r->fns.postindex)
  284. r->fns.postindex(ctx, r);
  285. }
  286. /*
  287. fz_bound_rasterizer: Once a set of edges has been fed into a
  288. rasterizer, the (device space) bounding box can be retrieved.
  289. */
  290. fz_irect fz_bound_rasterizer(fz_context *ctx, const fz_rasterizer *rast);
  291. /*
  292. fz_scissor_rasterizer: Retrieve the clipping box with which the
  293. rasterizer was reset.
  294. */
  295. fz_rect fz_scissor_rasterizer(fz_context *ctx, const fz_rasterizer *rast);
  296. /*
  297. fz_convert_rasterizer: Convert the set of edges that have
  298. been fed in, into pixels within the pixmap.
  299. eofill: Fill rule; True for even odd, false for non zero.
  300. pix: The pixmap to fill into.
  301. colorbv: The color components corresponding to the pixmap.
  302. eop: effective overprint.
  303. */
  304. void fz_convert_rasterizer(fz_context *ctx, fz_rasterizer *r, int eofill, fz_pixmap *pix, unsigned char *colorbv, fz_overprint *eop);
  305. /*
  306. fz_is_rect_rasterizer: Detect if the edges fed into a
  307. rasterizer make up a simple rectangle.
  308. */
  309. static inline int fz_is_rect_rasterizer(fz_context *ctx, fz_rasterizer *r)
  310. {
  311. return r->fns.is_rect(ctx, r);
  312. }
  313. void *fz_new_rasterizer_of_size(fz_context *ctx, int size, const fz_rasterizer_fns *fns);
  314. #define fz_new_derived_rasterizer(C,M,F) \
  315. ((M*)Memento_label(fz_new_rasterizer_of_size(C, sizeof(M), F), #M))
  316. /*
  317. fz_rasterizer_text_aa_level: Get the number of bits of
  318. antialiasing we are using for text in a given rasterizer.
  319. Between 0 and 8.
  320. */
  321. int fz_rasterizer_text_aa_level(fz_rasterizer *ras);
  322. /*
  323. fz_set_rasterizer_text_aa_level: Set the number of bits of
  324. antialiasing we should use for text in a given configuration.
  325. bits: The number of bits of antialiasing to use (values are clamped
  326. to within the 0 to 8 range).
  327. */
  328. void fz_set_rasterizer_text_aa_level(fz_context *ctx, fz_aa_context *aa, int bits);
  329. /*
  330. fz_rasterizer_graphics_aa_level: Get the number of bits of
  331. antialiasing we are using for graphics in a given rasterizer.
  332. Between 0 and 8.
  333. */
  334. int fz_rasterizer_graphics_aa_level(fz_rasterizer *ras);
  335. /*
  336. fz_set_rasterizer_graphics_aa_level: Set the number of bits of
  337. antialiasing we should use for graphics in a given rasterizer.
  338. bits: The number of bits of antialiasing to use (values are clamped
  339. to within the 0 to 8 range).
  340. */
  341. void fz_set_rasterizer_graphics_aa_level(fz_context *ctx, fz_aa_context *aa, int bits);
  342. /*
  343. fz_rasterizer_graphics_min_line_width: Get the minimum line
  344. width to be used for stroked lines in a given rasterizer.
  345. min_line_width: The minimum line width to use (in pixels).
  346. */
  347. float fz_rasterizer_graphics_min_line_width(fz_rasterizer *ras);
  348. /*
  349. fz_set_rasterizer_graphics_min_line_width: Set the minimum line
  350. width to be used for stroked lines in a given configuration.
  351. min_line_width: The minimum line width to use (in pixels).
  352. */
  353. void fz_set_rasterizer_graphics_min_line_width(fz_context *ctx, fz_aa_context *aa, float min_line_width);
  354. fz_rasterizer *fz_new_gel(fz_context *ctx);
  355. typedef enum
  356. {
  357. FZ_EDGEBUFFER_ANY_PART_OF_PIXEL,
  358. FZ_EDGEBUFFER_CENTER_OF_PIXEL
  359. } fz_edgebuffer_rule;
  360. fz_rasterizer *fz_new_edgebuffer(fz_context *ctx, fz_edgebuffer_rule rule);
  361. int fz_flatten_fill_path(fz_context *ctx, fz_rasterizer *rast, const fz_path *path, fz_matrix ctm, float flatness, fz_irect scissor, fz_irect *bbox);
  362. int fz_flatten_stroke_path(fz_context *ctx, fz_rasterizer *rast, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, float flatness, float linewidth, fz_irect scissor, fz_irect *bbox);
  363. fz_irect *fz_bound_path_accurate(fz_context *ctx, fz_irect *bbox, fz_irect scissor, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, float flatness, float linewidth);
  364. typedef void (fz_solid_color_painter_t)(unsigned char * FZ_RESTRICT dp, int n, int w, const unsigned char * FZ_RESTRICT color, int da, const fz_overprint * FZ_RESTRICT eop);
  365. typedef void (fz_span_painter_t)(unsigned char * FZ_RESTRICT dp, int da, const unsigned char * FZ_RESTRICT sp, int sa, int n, int w, int alpha, const fz_overprint * FZ_RESTRICT eop);
  366. typedef void (fz_span_color_painter_t)(unsigned char * FZ_RESTRICT dp, const unsigned char * FZ_RESTRICT mp, int n, int w, const unsigned char * FZ_RESTRICT color, int da, const fz_overprint * FZ_RESTRICT eop);
  367. fz_solid_color_painter_t *fz_get_solid_color_painter(int n, const unsigned char * FZ_RESTRICT color, int da, const fz_overprint * FZ_RESTRICT eop);
  368. fz_span_painter_t *fz_get_span_painter(int da, int sa, int n, int alpha, const fz_overprint * FZ_RESTRICT eop);
  369. fz_span_color_painter_t *fz_get_span_color_painter(int n, int da, const unsigned char * FZ_RESTRICT color, const fz_overprint * FZ_RESTRICT eop);
  370. void fz_paint_image(fz_context *ctx, fz_pixmap * FZ_RESTRICT dst, const fz_irect * FZ_RESTRICT scissor, fz_pixmap * FZ_RESTRICT shape, fz_pixmap * FZ_RESTRICT group_alpha, fz_pixmap * FZ_RESTRICT img, fz_matrix ctm, int alpha, int lerp_allowed, const fz_overprint * FZ_RESTRICT eop);
  371. void fz_paint_image_with_color(fz_context *ctx, fz_pixmap * FZ_RESTRICT dst, const fz_irect * FZ_RESTRICT scissor, fz_pixmap * FZ_RESTRICT shape, fz_pixmap * FZ_RESTRICT group_alpha, fz_pixmap * FZ_RESTRICT img, fz_matrix ctm, const unsigned char * FZ_RESTRICT colorbv, int lerp_allowed, const fz_overprint * FZ_RESTRICT eop);
  372. void fz_paint_pixmap(fz_pixmap * FZ_RESTRICT dst, const fz_pixmap * FZ_RESTRICT src, int alpha);
  373. void fz_paint_pixmap_alpha(fz_pixmap * FZ_RESTRICT dst, const fz_pixmap * FZ_RESTRICT src, int alpha);
  374. void fz_paint_pixmap_with_mask(fz_pixmap * FZ_RESTRICT dst, const fz_pixmap * FZ_RESTRICT src, const fz_pixmap * FZ_RESTRICT msk);
  375. void fz_paint_over_pixmap_with_mask(fz_pixmap * FZ_RESTRICT dst, const fz_pixmap * FZ_RESTRICT src, const fz_pixmap * FZ_RESTRICT msk);
  376. void fz_paint_pixmap_with_bbox(fz_pixmap * FZ_RESTRICT dst, const fz_pixmap * FZ_RESTRICT src, int alpha, fz_irect bbox);
  377. void fz_paint_pixmap_with_overprint(fz_pixmap * FZ_RESTRICT dst, const fz_pixmap * FZ_RESTRICT src, const fz_overprint * FZ_RESTRICT eop);
  378. void fz_blend_pixmap(fz_context *ctx, fz_pixmap * FZ_RESTRICT dst, fz_pixmap * FZ_RESTRICT src, int alpha, int blendmode, int isolated, const fz_pixmap * FZ_RESTRICT shape);
  379. void fz_blend_pixmap_knockout(fz_context *ctx, fz_pixmap * FZ_RESTRICT dst, fz_pixmap * FZ_RESTRICT src, const fz_pixmap * FZ_RESTRICT shape);
  380. void fz_paint_glyph(const unsigned char * FZ_RESTRICT colorbv, fz_pixmap * FZ_RESTRICT dst, unsigned char * FZ_RESTRICT dp, const fz_glyph * FZ_RESTRICT glyph, int w, int h, int skip_x, int skip_y, const fz_overprint * FZ_RESTRICT eop);
  381. #endif