draw-rasterize.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "mupdf/fitz.h"
  23. #include "draw-imp.h"
  24. #include "pixmap-imp.h"
  25. #include <string.h>
  26. void fz_init_aa_context(fz_context *ctx)
  27. {
  28. #ifndef AA_BITS
  29. ctx->aa.hscale = 17;
  30. ctx->aa.vscale = 15;
  31. ctx->aa.scale = 256;
  32. ctx->aa.bits = 8;
  33. ctx->aa.text_bits = 8;
  34. #endif
  35. }
  36. int
  37. fz_aa_level(fz_context *ctx)
  38. {
  39. return fz_aa_bits;
  40. }
  41. int
  42. fz_graphics_aa_level(fz_context *ctx)
  43. {
  44. return fz_aa_bits;
  45. }
  46. int
  47. fz_text_aa_level(fz_context *ctx)
  48. {
  49. return fz_aa_text_bits;
  50. }
  51. int
  52. fz_rasterizer_graphics_aa_level(fz_rasterizer *ras)
  53. {
  54. return fz_rasterizer_aa_bits(ras);
  55. }
  56. int
  57. fz_rasterizer_text_aa_level(fz_rasterizer *ras)
  58. {
  59. return fz_rasterizer_aa_text_bits(ras);
  60. }
  61. void
  62. fz_set_rasterizer_text_aa_level(fz_context *ctx, fz_aa_context *aa, int level)
  63. {
  64. #ifdef AA_BITS
  65. if (level != fz_aa_bits)
  66. {
  67. if (fz_aa_bits == 10)
  68. fz_warn(ctx, "Only the Any-part-of-a-pixel rasterizer was compiled in");
  69. else if (fz_aa_bits == 9)
  70. fz_warn(ctx, "Only the Centre-of-a-pixel rasterizer was compiled in");
  71. else
  72. fz_warn(ctx, "Only the %d bit anti-aliasing rasterizer was compiled in", fz_aa_bits);
  73. }
  74. #else
  75. if (level > 8)
  76. aa->text_bits = 0;
  77. else if (level > 6)
  78. aa->text_bits = 8;
  79. else if (level > 4)
  80. aa->text_bits = 6;
  81. else if (level > 2)
  82. aa->text_bits = 4;
  83. else if (level > 0)
  84. aa->text_bits = 2;
  85. else
  86. aa->text_bits = 0;
  87. #endif
  88. }
  89. void
  90. fz_set_rasterizer_graphics_aa_level(fz_context *ctx, fz_aa_context *aa, int level)
  91. {
  92. #ifdef AA_BITS
  93. if (level != fz_aa_bits)
  94. {
  95. if (fz_aa_bits == 10)
  96. fz_warn(ctx, "Only the Any-part-of-a-pixel rasterizer was compiled in");
  97. else if (fz_aa_bits == 9)
  98. fz_warn(ctx, "Only the Centre-of-a-pixel rasterizer was compiled in");
  99. else
  100. fz_warn(ctx, "Only the %d bit anti-aliasing rasterizer was compiled in", fz_aa_bits);
  101. }
  102. #else
  103. if (level == 9 || level == 10)
  104. {
  105. aa->hscale = 1;
  106. aa->vscale = 1;
  107. aa->bits = level;
  108. }
  109. else if (level > 6)
  110. {
  111. aa->hscale = 17;
  112. aa->vscale = 15;
  113. aa->bits = 8;
  114. }
  115. else if (level > 4)
  116. {
  117. aa->hscale = 8;
  118. aa->vscale = 8;
  119. aa->bits = 6;
  120. }
  121. else if (level > 2)
  122. {
  123. aa->hscale = 5;
  124. aa->vscale = 3;
  125. aa->bits = 4;
  126. }
  127. else if (level > 0)
  128. {
  129. aa->hscale = 2;
  130. aa->vscale = 2;
  131. aa->bits = 2;
  132. }
  133. else
  134. {
  135. aa->hscale = 1;
  136. aa->vscale = 1;
  137. aa->bits = 0;
  138. }
  139. aa->scale = 0xFF00 / (aa->hscale * aa->vscale);
  140. #endif
  141. }
  142. void
  143. fz_set_aa_level(fz_context *ctx, int level)
  144. {
  145. fz_set_rasterizer_graphics_aa_level(ctx, &ctx->aa, level);
  146. fz_set_rasterizer_text_aa_level(ctx, &ctx->aa, level);
  147. }
  148. void
  149. fz_set_text_aa_level(fz_context *ctx, int level)
  150. {
  151. fz_set_rasterizer_text_aa_level(ctx, &ctx->aa, level);
  152. }
  153. void
  154. fz_set_graphics_aa_level(fz_context *ctx, int level)
  155. {
  156. fz_set_rasterizer_graphics_aa_level(ctx, &ctx->aa, level);
  157. }
  158. void
  159. fz_set_graphics_min_line_width(fz_context *ctx, float min_line_width)
  160. {
  161. ctx->aa.min_line_width = min_line_width;
  162. }
  163. float
  164. fz_graphics_min_line_width(fz_context *ctx)
  165. {
  166. return ctx->aa.min_line_width;
  167. }
  168. float
  169. fz_rasterizer_graphics_min_line_width(fz_rasterizer *ras)
  170. {
  171. return ras->aa.min_line_width;
  172. }
  173. fz_irect
  174. fz_bound_rasterizer(fz_context *ctx, const fz_rasterizer *rast)
  175. {
  176. fz_irect bbox;
  177. const int hscale = fz_rasterizer_aa_hscale(rast);
  178. const int vscale = fz_rasterizer_aa_vscale(rast);
  179. if (rast->bbox.x1 < rast->bbox.x0 || rast->bbox.y1 < rast->bbox.y0)
  180. {
  181. bbox = fz_empty_irect;
  182. }
  183. else
  184. {
  185. bbox.x0 = fz_idiv(rast->bbox.x0, hscale);
  186. bbox.y0 = fz_idiv(rast->bbox.y0, vscale);
  187. bbox.x1 = fz_idiv_up(rast->bbox.x1, hscale);
  188. bbox.y1 = fz_idiv_up(rast->bbox.y1, vscale);
  189. }
  190. return bbox;
  191. }
  192. fz_rect fz_scissor_rasterizer(fz_context *ctx, const fz_rasterizer *rast)
  193. {
  194. fz_rect r;
  195. const int hscale = fz_rasterizer_aa_hscale(rast);
  196. const int vscale = fz_rasterizer_aa_vscale(rast);
  197. r.x0 = ((float)rast->clip.x0) / hscale;
  198. r.y0 = ((float)rast->clip.y0) / vscale;
  199. r.x1 = ((float)rast->clip.x1) / hscale;
  200. r.y1 = ((float)rast->clip.y1) / vscale;
  201. return r;
  202. }
  203. static fz_irect fz_clip_rasterizer(fz_context *ctx, const fz_rasterizer *rast)
  204. {
  205. fz_irect r;
  206. const int hscale = fz_rasterizer_aa_hscale(rast);
  207. const int vscale = fz_rasterizer_aa_vscale(rast);
  208. r.x0 = fz_idiv(rast->clip.x0, hscale);
  209. r.y0 = fz_idiv(rast->clip.y0, vscale);
  210. r.x1 = fz_idiv_up(rast->clip.x1, hscale);
  211. r.y1 = fz_idiv_up(rast->clip.y1, vscale);
  212. return r;
  213. }
  214. int fz_reset_rasterizer(fz_context *ctx, fz_rasterizer *rast, fz_irect clip)
  215. {
  216. const int hscale = fz_rasterizer_aa_hscale(rast);
  217. const int vscale = fz_rasterizer_aa_vscale(rast);
  218. if (fz_is_infinite_irect(clip))
  219. {
  220. rast->clip.x0 = rast->clip.y0 = BBOX_MIN;
  221. rast->clip.x1 = rast->clip.y1 = BBOX_MAX;
  222. }
  223. else {
  224. rast->clip.x0 = clip.x0 * hscale;
  225. rast->clip.x1 = clip.x1 * hscale;
  226. rast->clip.y0 = clip.y0 * vscale;
  227. rast->clip.y1 = clip.y1 * vscale;
  228. }
  229. rast->bbox.x0 = rast->bbox.y0 = BBOX_MAX;
  230. rast->bbox.x1 = rast->bbox.y1 = BBOX_MIN;
  231. if (rast->fns.reset)
  232. return rast->fns.reset(ctx, rast);
  233. return 0;
  234. }
  235. void *fz_new_rasterizer_of_size(fz_context *ctx, int size, const fz_rasterizer_fns *fns)
  236. {
  237. fz_rasterizer *rast = fz_calloc(ctx, 1, size);
  238. rast->fns = *fns;
  239. rast->clip.x0 = rast->clip.y0 = BBOX_MIN;
  240. rast->clip.x1 = rast->clip.y1 = BBOX_MAX;
  241. rast->bbox.x0 = rast->bbox.y0 = BBOX_MAX;
  242. rast->bbox.x1 = rast->bbox.y1 = BBOX_MIN;
  243. return rast;
  244. }
  245. fz_rasterizer *fz_new_rasterizer(fz_context *ctx, const fz_aa_context *aa)
  246. {
  247. fz_rasterizer *r;
  248. int bits;
  249. #ifdef AA_BITS
  250. bits = AA_BITS;
  251. #else
  252. if (aa == NULL)
  253. aa = &ctx->aa;
  254. bits = aa->bits;
  255. #endif
  256. if (bits == 10)
  257. r = fz_new_edgebuffer(ctx, FZ_EDGEBUFFER_ANY_PART_OF_PIXEL);
  258. else if (bits == 9)
  259. r = fz_new_edgebuffer(ctx, FZ_EDGEBUFFER_CENTER_OF_PIXEL);
  260. else
  261. r = fz_new_gel(ctx);
  262. #ifndef AA_BITS
  263. r->aa = *aa;
  264. #endif
  265. return r;
  266. }
  267. void fz_convert_rasterizer(fz_context *ctx, fz_rasterizer *r, int eofill, fz_pixmap *pix, unsigned char *colorbv, fz_overprint *eop)
  268. {
  269. fz_irect clip = fz_bound_rasterizer(ctx, r);
  270. clip = fz_intersect_irect(clip, fz_pixmap_bbox_no_ctx(pix));
  271. clip = fz_intersect_irect(clip, fz_clip_rasterizer(ctx, r));
  272. if (!fz_is_empty_irect(clip))
  273. r->fns.convert(ctx, r, eofill, &clip, pix, colorbv, eop);
  274. }