Coverage Report

Created: 2026-10-13 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/splash/SplashClip.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SplashClip.cc
4
//
5
// Copyright 2003-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdlib.h>
12
#include <string.h>
13
#include "gmem.h"
14
#include "gmempp.h"
15
#include "SplashErrorCodes.h"
16
#include "SplashPath.h"
17
#include "SplashXPath.h"
18
#include "SplashXPathScanner.h"
19
#include "SplashClip.h"
20
21
//------------------------------------------------------------------------
22
23
// Compute x * y / 255, where x and y are in [0, 255].
24
67
static inline Guchar mul255(Guchar x, Guchar y) {
25
67
  int z;
26
27
67
  z = (int)x * (int)y;
28
67
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
67
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
48.1k
           int hardXMaxA, int hardYMaxA) {
37
48.1k
  int w;
38
39
48.1k
  hardXMin = hardXMinA;
40
48.1k
  hardYMin = hardYMinA;
41
48.1k
  hardXMax = hardXMaxA;
42
48.1k
  hardYMax = hardYMaxA;
43
48.1k
  xMin = hardXMin;
44
48.1k
  yMin = hardYMin;
45
48.1k
  xMax = hardXMax;
46
48.1k
  yMax = hardYMax;
47
48.1k
  intBoundsValid = gFalse;
48
48.1k
  paths = NULL;
49
48.1k
  eo = NULL;
50
48.1k
  scanners = NULL;
51
48.1k
  length = size = 0;
52
48.1k
  isSimple = gTrue;
53
48.1k
  prev = NULL;
54
48.1k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
48.1k
  buf = (Guchar *)gmalloc(w);
58
48.1k
}
59
60
94.0k
SplashClip::SplashClip(SplashClip *clip) {
61
94.0k
  int w;
62
63
94.0k
  hardXMin = clip->hardXMin;
64
94.0k
  hardYMin = clip->hardYMin;
65
94.0k
  hardXMax = clip->hardXMax;
66
94.0k
  hardYMax = clip->hardYMax;
67
94.0k
  xMin = clip->xMin;
68
94.0k
  yMin = clip->yMin;
69
94.0k
  xMax = clip->xMax;
70
94.0k
  yMax = clip->yMax;
71
94.0k
  xMinI = clip->xMinI;
72
94.0k
  yMinI = clip->yMinI;
73
94.0k
  xMaxI = clip->xMaxI;
74
94.0k
  yMaxI = clip->yMaxI;
75
94.0k
  intBoundsValid = clip->intBoundsValid;
76
94.0k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
94.0k
  paths = NULL;
78
94.0k
  eo = NULL;
79
94.0k
  scanners = NULL;
80
94.0k
  length = size = 0;
81
94.0k
  isSimple = clip->isSimple;
82
94.0k
  prev = clip;
83
94.0k
  if ((w = splashCeil(xMax)) <= 0) {
84
21.8k
    w = 1;
85
21.8k
  }
86
94.0k
  buf = (Guchar *)gmalloc(w);
87
94.0k
}
88
89
142k
SplashClip::~SplashClip() {
90
142k
  int i;
91
92
173k
  for (i = 0; i < length; ++i) {
93
31.0k
    delete scanners[i];
94
31.0k
    delete paths[i];
95
31.0k
  }
96
142k
  gfree(paths);
97
142k
  gfree(eo);
98
142k
  gfree(scanners);
99
142k
  gfree(buf);
100
142k
}
101
102
31.0k
void SplashClip::grow(int nPaths) {
103
31.0k
  if (length + nPaths > size) {
104
28.2k
    if (size == 0) {
105
28.2k
      size = 32;
106
28.2k
    }
107
28.2k
    while (size < length + nPaths) {
108
1
      size *= 2;
109
1
    }
110
28.2k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
28.2k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
28.2k
    scanners = (SplashXPathScanner **)
113
28.2k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
28.2k
  }
115
31.0k
}
116
117
void SplashClip::resetToRect(SplashCoord x0, SplashCoord y0,
118
0
           SplashCoord x1, SplashCoord y1) {
119
0
  int w, i;
120
121
0
  for (i = 0; i < length; ++i) {
122
0
    delete paths[i];
123
0
    delete scanners[i];
124
0
  }
125
0
  gfree(paths);
126
0
  gfree(eo);
127
0
  gfree(scanners);
128
0
  gfree(buf);
129
0
  paths = NULL;
130
0
  eo = NULL;
131
0
  scanners = NULL;
132
0
  length = size = 0;
133
0
  isSimple = gTrue;
134
0
  prev = NULL;
135
136
0
  if (x0 < x1) {
137
0
    xMin = x0;
138
0
    xMax = x1;
139
0
  } else {
140
0
    xMin = x1;
141
0
    xMax = x0;
142
0
  }
143
0
  if (y0 < y1) {
144
0
    yMin = y0;
145
0
    yMax = y1;
146
0
  } else {
147
0
    yMin = y1;
148
0
    yMax = y0;
149
0
  }
150
0
  intBoundsValid = gFalse;
151
0
  if ((w = splashCeil(xMax)) <= 0) {
152
0
    w = 1;
153
0
  }
154
0
  buf = (Guchar *)gmalloc(w);
155
0
}
156
157
SplashError SplashClip::clipToRect(SplashCoord x0, SplashCoord y0,
158
67.1k
           SplashCoord x1, SplashCoord y1) {
159
67.1k
  if (x0 < x1) {
160
28.2k
    if (x0 > xMin) {
161
32
      xMin = x0;
162
32
      intBoundsValid = gFalse;
163
32
    }
164
28.2k
    if (x1 < xMax) {
165
1.81k
      xMax = x1;
166
1.81k
      intBoundsValid = gFalse;
167
1.81k
    }
168
38.8k
  } else {
169
38.8k
    if (x1 > xMin) {
170
15.4k
      xMin = x1;
171
15.4k
      intBoundsValid = gFalse;
172
15.4k
    }
173
38.8k
    if (x0 < xMax) {
174
8.07k
      xMax = x0;
175
8.07k
      intBoundsValid = gFalse;
176
8.07k
    }
177
38.8k
  }
178
67.1k
  if (y0 < y1) {
179
39.4k
    if (y0 > yMin) {
180
10.4k
      yMin = y0;
181
10.4k
      intBoundsValid = gFalse;
182
10.4k
    }
183
39.4k
    if (y1 < yMax) {
184
1.06k
      yMax = y1;
185
1.06k
      intBoundsValid = gFalse;
186
1.06k
    }
187
39.4k
  } else {
188
27.6k
    if (y1 > yMin) {
189
5.82k
      yMin = y1;
190
5.82k
      intBoundsValid = gFalse;
191
5.82k
    }
192
27.6k
    if (y0 < yMax) {
193
5.56k
      yMax = y0;
194
5.56k
      intBoundsValid = gFalse;
195
5.56k
    }
196
27.6k
  }
197
67.1k
  return splashOk;
198
67.1k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
107k
           SplashStrokeAdjustMode strokeAdjust) {
204
107k
  SplashXPath *xPath;
205
107k
  SplashCoord t;
206
207
107k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
107k
        enablePathSimplification,
209
107k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
107k
  if (xPath->length == 0) {
213
8.82k
    xMin = yMin = 1;
214
8.82k
    xMax = yMax = 0;
215
8.82k
    intBoundsValid = gFalse;
216
8.82k
    delete xPath;
217
8.82k
    return splashOk;
218
8.82k
  }
219
220
  // check for a rectangle
221
98.2k
  if (xPath->isRect) {
222
67.1k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
67.1k
    delete xPath;
224
67.1k
    return splashOk;
225
67.1k
  }
226
227
31.0k
  grow(1);
228
31.0k
  paths[length] = xPath;
229
31.0k
  eo[length] = (Guchar)eoA;
230
31.0k
  if ((t = xPath->getXMin()) > xMin) {
231
1.38k
    xMin = t;
232
1.38k
  }
233
31.0k
  if ((t = xPath->getYMin()) > yMin) {
234
3.28k
    yMin = t;
235
3.28k
  }
236
31.0k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
6.89k
    xMax = t;
238
6.89k
  }
239
31.0k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
11.1k
    yMax = t;
241
11.1k
  }
242
31.0k
  intBoundsValid = gFalse;
243
31.0k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
31.0k
              splashCeil(yMax) - 1);
245
31.0k
  ++length;
246
31.0k
  isSimple = gFalse;
247
248
31.0k
  return splashOk;
249
98.2k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
653k
              SplashStrokeAdjustMode strokeAdjust) {
254
  // In general, this function tests the rectangle:
255
  //     x = [rectXMin, rectXMax + 1)    (note: coords are ints)
256
  //     y = [rectYMin, rectYMax + 1)
257
  // against the clipping region:
258
  //     x = [xMin, xMax)                (note: coords are fp)
259
  //     y = [yMin, yMax)
260
261
653k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
262
    // special case for stroke adjustment with a simple clipping
263
    // rectangle -- the clipping region is:
264
    //     x = [xMinI, xMaxI + 1)
265
    //     y = [yMinI, yMaxI + 1)
266
304k
    updateIntBounds(strokeAdjust);
267
304k
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
6.42k
      return splashClipAllOutside;
269
6.42k
    }
270
298k
    if (rectXMax + 1 <= xMinI ||
271
77.0k
  rectXMin >= xMaxI + 1 ||
272
64.6k
  rectYMax + 1 <= yMinI ||
273
292k
  rectYMin >= yMaxI + 1) {
274
292k
      return splashClipAllOutside;
275
292k
    }
276
6.37k
    if (rectXMin >= xMinI &&
277
3.80k
  rectXMax <= xMaxI &&
278
1.04k
  rectYMin >= yMinI &&
279
963
  rectYMax <= yMaxI) {
280
919
      return splashClipAllInside;
281
919
    }
282
348k
  } else {
283
348k
    if (xMin >= xMax || yMin >= yMax) {
284
321k
      return splashClipAllOutside;
285
321k
    }
286
26.8k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
9.28k
  (SplashCoord)rectXMin >= xMax ||
288
4.40k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
23.5k
  (SplashCoord)rectYMin >= yMax) {
290
23.5k
      return splashClipAllOutside;
291
23.5k
    }
292
3.27k
    if (isSimple &&
293
3.16k
  (SplashCoord)rectXMin >= xMin &&
294
1.46k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
1.18k
  (SplashCoord)rectYMin >= yMin &&
296
97
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
88
      return splashClipAllInside;
298
88
    }
299
3.27k
  }
300
8.63k
  return splashClipPartial;
301
653k
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
5.38k
        SplashStrokeAdjustMode strokeAdjust) {
305
5.38k
  SplashClip *clip;
306
5.38k
  SplashCoord d;
307
5.38k
  int x0a, x1a, x0b, x1b, x, i;
308
309
5.38k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
5.38k
  if (y < yMinI || y > yMaxI ||
314
5.38k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
5.38k
  if (x0 > xMinI) {
320
314
    x0a = x0;
321
5.07k
  } else {
322
5.07k
    x0a = xMinI;
323
5.07k
    memset(line + x0, 0, x0a - x0);
324
5.07k
  }
325
326
5.38k
  if (x1 < xMaxI) {
327
18
    x1a = x1;
328
5.36k
  } else {
329
5.36k
    x1a = xMaxI;
330
5.36k
    memset(line + x1a + 1, 0, x1 - x1a);
331
5.36k
  }
332
333
5.38k
  if (x0a > x1a) {
334
0
    return;
335
0
  }
336
337
  //--- clip to the floating point rectangle
338
  //    (if stroke adjustment is disabled)
339
340
5.38k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
1.79k
    if (x0a == xMinI) {
344
1.48k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
1.48k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
1.48k
    }
347
348
    // clip right edge (xMax)
349
1.79k
    if (x1a == xMaxI) {
350
1.78k
      d = xMax - (SplashCoord)xMaxI;
351
1.78k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
1.78k
    }
353
354
    // clip top edge (yMin)
355
1.79k
    if (y == yMinI) {
356
1.47k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
3.25k
      for (x = x0a; x <= x1a; ++x) {
358
1.77k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
1.77k
      }
360
1.47k
    }
361
362
    // clip bottom edge (yMax)
363
1.79k
    if (y == yMaxI) {
364
1.47k
      d = yMax - (SplashCoord)yMaxI;
365
3.25k
      for (x = x0a; x <= x1a; ++x) {
366
1.77k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
1.77k
      }
368
1.47k
    }
369
1.79k
  }
370
371
5.38k
  if (isSimple) {
372
5.31k
    return;
373
5.31k
  }
374
375
  //--- clip to the paths
376
377
166
  for (clip = this; clip; clip = clip->prev) {
378
166
    for (i = 0; i < clip->length; ++i) {
379
74
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
74
      if (x0a < x0b) {
381
7
  memset(line + x0a, 0, x0b - x0a);
382
7
      }
383
141
      for (x = x0b; x <= x1b; ++x) {
384
67
  line[x] = mul255(line[x], buf[x]);
385
67
      }
386
74
      if (x1b < x1a) {
387
3
  memset(line + x1b + 1, 0, x1a - x1b);
388
3
      }
389
74
    }
390
92
  }
391
74
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
1.67k
         SplashStrokeAdjustMode strokeAdjust) {
395
1.67k
  SplashClip *clip;
396
1.67k
  int x0a, x1a, x0b, x1b, x, i;
397
1.67k
  Guchar any;
398
399
1.67k
  updateIntBounds(strokeAdjust);
400
401
1.67k
  if (y < yMinI || y > yMaxI ||
402
1.67k
      x1 < xMinI || x0 > xMaxI) {
403
0
    if (x0 <= x1) {
404
0
      memset(line + x0, 0, x1 - x0 + 1);
405
0
    }
406
0
    return gFalse;
407
0
  }
408
409
1.67k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
1.67k
  } else {
412
1.67k
    x0a = xMinI;
413
1.67k
    memset(line + x0, 0, x0a - x0);
414
1.67k
  }
415
416
1.67k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
1.67k
  } else {
419
1.67k
    x1a = xMaxI;
420
1.67k
    memset(line + x1a + 1, 0, x1 - x1a);
421
1.67k
  }
422
423
1.67k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
1.67k
  if (isSimple) {
428
1.65k
    for (x = x0a; x <= x1a; ++x) {
429
1.65k
      if (line[x]) {
430
1.65k
  return gTrue;
431
1.65k
      }
432
1.65k
    }
433
0
    return gFalse;
434
1.65k
  }
435
436
14
  any = 0;
437
30
  for (clip = this; clip; clip = clip->prev) {
438
38
    for (i = 0; i < clip->length; ++i) {
439
22
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
22
      if (x0a < x0b) {
441
0
  memset(line + x0a, 0, x0b - x0a);
442
0
      }
443
44
      for (x = x0b; x <= x1b; ++x) {
444
22
  line[x] &= buf[x];
445
22
  any |= line[x];
446
22
      }
447
22
      if (x1b < x1a) {
448
0
  memset(line + x1b + 1, 0, x1a - x1b);
449
0
      }
450
22
    }
451
16
  }
452
453
14
  return any != 0;
454
1.67k
}
455
456
20.8k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
20.8k
  updateIntBounds(strokeAdjust);
458
20.8k
  return xMinI;
459
20.8k
}
460
461
20.8k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
20.8k
  updateIntBounds(strokeAdjust);
463
20.8k
  return xMaxI;
464
20.8k
}
465
466
20.7k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
20.7k
  updateIntBounds(strokeAdjust);
468
20.7k
  return yMinI;
469
20.7k
}
470
471
20.7k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
20.7k
  updateIntBounds(strokeAdjust);
473
20.7k
  return yMaxI;
474
20.7k
}
475
476
0
int SplashClip::getNumPaths() {
477
0
  SplashClip *clip;
478
0
  int n;
479
480
0
  n = 0;
481
0
  for (clip = this; clip; clip = clip->prev) {
482
0
    n += clip->length;
483
0
  }
484
0
  return n;
485
0
}
486
487
395k
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
395k
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
390k
    return;
490
390k
  }
491
4.99k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
4.45k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
4.45k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
4.45k
  } else {
495
540
    xMinI = splashFloor(xMin);
496
540
    yMinI = splashFloor(yMin);
497
540
    xMaxI = splashCeil(xMax);
498
540
    yMaxI = splashCeil(yMax);
499
540
  }
500
4.99k
  if (xMinI < hardXMin) {
501
6
    xMinI = hardXMin;
502
6
  }
503
4.99k
  if (yMinI < hardYMin) {
504
9
    yMinI = hardYMin;
505
9
  }
506
4.99k
  if (xMaxI > hardXMax) {
507
0
    xMaxI = hardXMax;
508
0
  }
509
4.99k
  if (yMaxI > hardYMax) {
510
0
    yMaxI = hardYMax;
511
0
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
4.99k
  --xMaxI;
514
4.99k
  --yMaxI;
515
4.99k
  intBoundsValid = gTrue;
516
4.99k
  intBoundsStrokeAdjust = strokeAdjust;
517
4.99k
}