Coverage Report

Created: 2026-08-14 07:34

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