Coverage Report

Created: 2026-07-16 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/splash/SplashXPathScanner.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SplashXPathScanner.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
#if HAVE_STD_SORT
14
#include <algorithm>
15
#endif
16
#include "gmem.h"
17
#include "gmempp.h"
18
#include "GList.h"
19
#include "SplashMath.h"
20
#include "SplashXPath.h"
21
#include "SplashXPathScanner.h"
22
23
//------------------------------------------------------------------------
24
25
#if ANTIALIAS_256
26
27
#define aaVert  15
28
#define aaHoriz 17
29
30
#else
31
32
55.5M
#define aaVert  4
33
12.5M
#define aaHoriz 4
34
35
// 4x4 oversampling tends to generate alpha (coverage) values that are
36
// too high, so we reduce them here. (Yes, this is a kludge.)
37
static Guchar map16to255[17] = {
38
  0,
39
  16 / 2,
40
  32 / 2,
41
  48 / 2,
42
  64 - 32,
43
  80 - 32,
44
  96 - 32,
45
  112 - 32,
46
  128 - 32,
47
  143 - 32,
48
  159 - 32,
49
  175 - 32,
50
  191 - 32,
51
  207 - 32,
52
  223 - 32,
53
  239 - 32,
54
  255
55
};
56
57
#endif
58
59
//------------------------------------------------------------------------
60
61
SplashXPathScanner::SplashXPathScanner(SplashXPath *xPathA, GBool eo,
62
154k
               int yMinA, int yMaxA) {
63
154k
  xPath = xPathA;
64
154k
  eoMask = eo ? 1 : 0xffffffff;
65
154k
  yMin = yMinA;
66
154k
  yMax = yMaxA;
67
154k
  if (xPath->isRect) {
68
20.2k
    rectX0I = splashFloor(xPath->rectX0);
69
20.2k
    rectY0I = splashFloor(xPath->rectY0);
70
20.2k
    rectX1I = splashFloor(xPath->rectX1);
71
20.2k
    rectY1I = splashFloor(xPath->rectY1);
72
20.2k
  }
73
74
154k
  pre = &preSeg;
75
154k
  post = &postSeg;
76
154k
  pre->mx = xPath->xMin - 1;
77
154k
  post->mx = xPath->xMax + 1;
78
79
154k
  resetDone = gFalse;
80
154k
  resetAA = gFalse;
81
154k
}
82
83
154k
SplashXPathScanner::~SplashXPathScanner() {
84
154k
}
85
86
void SplashXPathScanner::insertSegmentBefore(SplashXPathSeg *s,
87
3.46M
               SplashXPathSeg *sNext) {
88
3.46M
  SplashXPathSeg *sPrev;
89
90
3.46M
  sPrev = sNext->prev;
91
3.46M
  sPrev->next = s;
92
3.46M
  s->prev = sPrev;
93
3.46M
  s->next = sNext;
94
3.46M
  sNext->prev = s;
95
3.46M
}
96
97
1.89M
void SplashXPathScanner::removeSegment(SplashXPathSeg *s) {
98
1.89M
  SplashXPathSeg *sPrev, *sNext;
99
100
1.89M
  sPrev = s->prev;
101
1.89M
  sNext = s->next;
102
1.89M
  sPrev->next = sNext;
103
1.89M
  sNext->prev = sPrev;
104
1.89M
  s->prev = s->next = NULL;
105
1.89M
}
106
107
void SplashXPathScanner::moveSegmentAfter(SplashXPathSeg *s,
108
219k
            SplashXPathSeg *sPrev) {
109
219k
  SplashXPathSeg *sNext;
110
111
219k
  s->prev->next = s->next;
112
219k
  s->next->prev = s->prev;
113
114
219k
  sNext = sPrev->next;
115
219k
  sPrev->next = s;
116
219k
  s->prev = sPrev;
117
219k
  s->next = sNext;
118
219k
  sNext->prev = s;
119
219k
}
120
121
108k
void SplashXPathScanner::reset(GBool aa, GBool aaChanged) {
122
108k
  SplashXPathSeg *seg;
123
108k
  SplashCoord y;
124
108k
  int i;
125
126
  //--- initialize segment parameters
127
38.3M
  for (i = 0; i < xPath->length; ++i) {
128
38.2M
    seg = &xPath->segs[i];
129
38.2M
    if (aa) {
130
27.9M
      if (aaChanged) {
131
26.2M
  seg->iy = splashFloor(seg->y0 * aaVert);
132
26.2M
      }
133
27.9M
      y = (SplashCoord)(seg->iy + 1) / (SplashCoord)aaVert;
134
27.9M
    } else {
135
10.3M
      if (aaChanged) {
136
8.07M
  seg->iy = splashFloor(seg->y0);
137
8.07M
      }
138
10.3M
      y = (SplashCoord)(seg->iy + 1);
139
10.3M
    }
140
38.2M
    seg->sx0 = seg->x0;
141
38.2M
    if (seg->y1 <= y) {
142
21.0M
      seg->sx1 = seg->x1;
143
21.0M
    } else {
144
17.2M
      seg->sx1 = seg->x0 + (y - seg->y0) * seg->dxdy;
145
17.2M
    }
146
38.2M
    seg->mx = (seg->sx0 <= seg->sx1) ? seg->sx0 : seg->sx1;
147
38.2M
    seg->prev = seg->next = NULL;
148
38.2M
  }
149
150
  //--- sort the inactive segments by iy, mx
151
108k
  if (aaChanged) {
152
46.7k
#if HAVE_STD_SORT
153
46.7k
    std::sort(xPath->segs, xPath->segs + xPath->length, &SplashXPathSeg::cmpMX);
154
#else
155
    qsort(xPath->segs, xPath->length, sizeof(SplashXPathSeg),
156
    &SplashXPathSeg::cmpMX);
157
#endif
158
46.7k
  }
159
160
  //--- initialize the active list
161
108k
  pre->prev = NULL;
162
108k
  pre->next = post;
163
108k
  post->prev = pre;
164
108k
  post->next = NULL;
165
166
  //--- initialize the scan state
167
108k
  nextSeg = 0;
168
108k
  if (xPath->length) {
169
105k
    yBottomI = xPath->segs[0].iy;
170
105k
    if (aa) {
171
97.4k
      yBottomI -= yBottomI % aaVert;
172
97.4k
    }
173
105k
  } else {
174
2.59k
    yBottomI = 0;
175
2.59k
  }
176
108k
  yTopI = yBottomI - 1;
177
108k
  if (aa) {
178
100k
    yTop = (SplashCoord)yTopI / (SplashCoord)aaVert;
179
100k
    yBottom = (SplashCoord)yBottomI / (SplashCoord)aaVert;
180
100k
  } else {
181
7.99k
    yTop = (SplashCoord)yTopI;
182
7.99k
    yBottom = (SplashCoord)yBottomI;
183
7.99k
  }
184
185
108k
  resetDone = gTrue;
186
108k
  resetAA = aa;
187
108k
}
188
189
86.4k
void SplashXPathScanner::skip(int newYBottomI, GBool aa) {
190
86.4k
  SplashXPathSeg *s0, *s1,*s2;
191
86.4k
  int iy;
192
193
86.4k
  yTopI = newYBottomI - 1;
194
86.4k
  yBottomI = newYBottomI;
195
86.4k
  if (aa) {
196
79.6k
    yTop = (SplashCoord)yTopI / (SplashCoord)aaVert;
197
79.6k
    yBottom = (SplashCoord)yBottomI / (SplashCoord)aaVert;
198
79.6k
  } else {
199
6.79k
    yTop = (SplashCoord)yTopI;
200
6.79k
    yBottom = (SplashCoord)yBottomI;
201
6.79k
  }
202
203
  //--- remove finished segments; update sx0, sx1, mx for active segments
204
86.4k
  s0 = pre->next;
205
86.4k
  while (s0 != post) {
206
0
    s1 = s0->next;
207
208
    // check for a finished segment
209
0
    if (s0->y1 < yTop) {
210
0
      removeSegment(s0);
211
212
    // compute sx0, sx1, mx
213
0
    } else {
214
0
      if (s0->y0 >= yTop) {
215
0
  s0->sx0 = s0->x0;
216
0
      } else {
217
0
  s0->sx0 = s0->x0 + (yTop - s0->y0) * s0->dxdy;
218
0
      }
219
0
      if (s0->y1 <= yBottom) {
220
0
  s0->sx1 = s0->x1;
221
0
      } else {
222
0
  s0->sx1 = s0->x0 + (yBottom - s0->y0) * s0->dxdy;
223
0
      }
224
0
      s0->mx = (s0->sx0 <= s0->sx1) ? s0->sx0 : s0->sx1;
225
0
    }
226
227
0
    s0 = s1;
228
0
  }
229
230
  //--- check for intersections
231
86.4k
  s0 = pre->next;
232
86.4k
  if (s0 != post) {
233
0
    s1 = s0->next;
234
0
    while (s1 != post) {
235
0
      if (s1->mx < s0->mx) {
236
0
  for (s2 = s0->prev; s1->mx < s2->mx; s2 = s2->prev) ;
237
0
  moveSegmentAfter(s1, s2);
238
0
      } else {
239
0
  s0 = s1;
240
0
      }
241
0
      s1 = s0->next;
242
0
    }
243
0
  }
244
245
  //--- insert new segments with a merge sort
246
  // - this has to be done one (sub)scanline at a time, because the
247
  //   inactive list is bin-sorted by (sub)scanline
248
6.09M
  while (nextSeg < xPath->length && xPath->segs[nextSeg].iy <= yTopI) {
249
    // the first inactive segment determines the next scanline to process
250
6.01M
    iy = xPath->segs[nextSeg].iy;
251
6.01M
    s0 = pre->next;
252
15.7M
    do {
253
15.7M
      s1 = &xPath->segs[nextSeg];
254
15.7M
      ++nextSeg;
255
15.7M
      if (s1->y1 < yTop) {
256
14.5M
  continue;
257
14.5M
      }
258
1.18M
      if (s1->y0 >= yTop) {
259
911
  s1->sx0 = s1->x0;
260
1.18M
      } else {
261
1.18M
  s1->sx0 = s1->x0 + (yTop - s1->y0) * s1->dxdy;
262
1.18M
      }
263
1.18M
      if (s1->y1 <= yBottom) {
264
9.96k
  s1->sx1 = s1->x1;
265
1.17M
      } else {
266
1.17M
  s1->sx1 = s1->x0 + (yBottom - s1->y0) * s1->dxdy;
267
1.17M
      }
268
1.18M
      s1->mx = (s1->sx0 <= s1->sx1) ? s1->sx0 : s1->sx1;
269
1.18M
      insertSegmentBefore(s1, s0);
270
15.7M
    } while (nextSeg < xPath->length && xPath->segs[nextSeg].iy <= iy);
271
6.01M
  }
272
86.4k
}
273
274
350k
void SplashXPathScanner::advance(GBool aa) {
275
350k
  SplashXPathSeg *s, *sNext, *s1;
276
277
350k
  yTopI = yBottomI;
278
350k
  yTop = yBottom;
279
350k
  yBottomI = yTopI + 1;
280
350k
  if (aa) {
281
342k
    yBottom = (SplashCoord)yBottomI / (SplashCoord)aaVert;
282
342k
  } else {
283
7.95k
    yBottom = (SplashCoord)yBottomI;
284
7.95k
  }
285
286
350k
  s = pre->next;
287
6.94M
  while (s != post) {
288
6.59M
    sNext = s->next;
289
290
    // check for a finished segment
291
6.59M
    if (s->y1 < yTop) {
292
1.89M
      removeSegment(s);
293
294
4.70M
    } else {
295
296
      // compute sx0, sx1, mx
297
4.70M
      s->sx0 = s->sx1;
298
4.70M
      if (s->y1 <= yBottom) {
299
10.0k
  s->sx1 = s->x1;
300
4.69M
      } else {
301
4.69M
  s->sx1 = s->x0 + (yBottom - s->y0) * s->dxdy;
302
4.69M
      }
303
4.70M
      s->mx = (s->sx0 <= s->sx1) ? s->sx0 : s->sx1;
304
305
      // check for intersection
306
4.70M
      if (s->mx < s->prev->mx) {
307
86.1M
  for (s1 = s->prev->prev; s->mx < s1->mx; s1 = s1->prev) ;
308
219k
  moveSegmentAfter(s, s1);
309
219k
      }
310
4.70M
    }
311
312
6.59M
    s = sNext;
313
6.59M
  }
314
315
  // insert new segments
316
350k
  s = pre->next;
317
2.62M
  while (nextSeg < xPath->length && xPath->segs[nextSeg].iy <= yTopI) {
318
2.27M
    s1 = &xPath->segs[nextSeg];
319
2.27M
    ++nextSeg;
320
2.28M
    while (s1->mx > s->mx) {
321
6.33k
      s = s->next;
322
6.33k
    }
323
2.27M
    insertSegmentBefore(s1, s);
324
2.27M
  }
325
350k
}
326
327
void SplashXPathScanner::generatePixels(int x0, int x1, Guchar *line,
328
342k
          int *xMin, int *xMax) {
329
342k
  SplashXPathSeg *s;
330
342k
  int fillCount, x, xEnd, ix0, ix1, t;
331
332
342k
  fillCount = 0;
333
342k
  x = x0 * aaHoriz;
334
342k
  xEnd = (x1 + 1) * aaHoriz;
335
3.16M
  for (s = pre->next; s != post && x < xEnd; s = s->next) {
336
2.82M
    ix0 = splashFloor(s->sx0 * aaHoriz);
337
2.82M
    ix1 = splashFloor(s->sx1 * aaHoriz);
338
2.82M
    if (ix0 > ix1) {
339
531k
      t = ix0;  ix0 = ix1;  ix1 = t;
340
531k
    }
341
2.82M
    if (!(fillCount & eoMask)) {
342
686k
      if (ix0 > x) {
343
29.7k
  x = ix0;
344
29.7k
      }
345
686k
    }
346
2.82M
    if (ix1 >= xEnd) {
347
52.3k
      ix1 = xEnd - 1;
348
52.3k
    }
349
2.82M
    if (x / aaHoriz < *xMin) {
350
80.8k
      *xMin = x / aaHoriz;
351
80.8k
    }
352
2.82M
    if (ix1 / aaHoriz > *xMax) {
353
81.2k
      *xMax = ix1 / aaHoriz;
354
81.2k
    }
355
3.22M
    for (; x <= ix1; ++x) {
356
407k
      ++line[x / aaHoriz];
357
407k
    }
358
2.82M
    if (s->y0 <= yTop && s->y1 > yTop) {
359
2.75M
      fillCount += s->count;
360
2.75M
    }
361
2.82M
  }
362
342k
}
363
364
void SplashXPathScanner::generatePixelsBinary(int x0, int x1, Guchar *line,
365
7.95k
                int *xMin, int *xMax) {
366
7.95k
  SplashXPathSeg *s;
367
7.95k
  int fillCount, x, xEnd, ix0, ix1, t;
368
369
7.95k
  fillCount = 0;
370
7.95k
  x = x0;
371
7.95k
  xEnd = x1 + 1;
372
19.2k
  for (s = pre->next; s != post && x < xEnd; s = s->next) {
373
11.2k
    ix0 = splashFloor(s->sx0);
374
11.2k
    ix1 = splashFloor(s->sx1);
375
11.2k
    if (ix0 > ix1) {
376
1.69k
      t = ix0;  ix0 = ix1;  ix1 = t;
377
1.69k
    }
378
11.2k
    if (!(fillCount & eoMask)) {
379
8.91k
      if (ix0 > x) {
380
3
  x = ix0;
381
3
      }
382
8.91k
    }
383
11.2k
    if (ix1 >= xEnd) {
384
750
      ix1 = xEnd - 1;
385
750
    }
386
11.2k
    if (x < *xMin) {
387
7.86k
      *xMin = x;
388
7.86k
    }
389
11.2k
    if (ix1 > *xMax) {
390
7.75k
      *xMax = ix1;
391
7.75k
    }
392
19.0k
    for (; x <= ix1; ++x) {
393
7.75k
      line[x] = 255;
394
7.75k
    }
395
11.2k
    if (s->y0 <= yTop && s->y1 > yTop) {
396
8.86k
      fillCount += s->count;
397
8.86k
    }
398
11.2k
  }
399
7.95k
}
400
401
void SplashXPathScanner::drawRectangleSpan(Guchar *line, int y,
402
             int x0, int x1,
403
14.4k
             int *xMin, int *xMax) {
404
14.4k
  SplashCoord edge;
405
14.4k
  Guchar pix;
406
14.4k
  int xe, x;
407
408
14.4k
  if (rectX0I > x1 || rectX1I < x0) {
409
182
    return;
410
182
  }
411
412
14.2k
  *xMin = x0 <= rectX0I ? rectX0I : x0;
413
14.2k
  *xMax = rectX1I <= x1 ? rectX1I : x1;
414
415
  //--- upper edge
416
14.2k
  if (y == rectY0I) {
417
418
    // handle a rectangle less than one pixel high
419
2.90k
    if (rectY0I == rectY1I) {
420
2.29k
      edge = xPath->rectY1 - xPath->rectY0;
421
2.29k
    } else {
422
609
      edge = (SplashCoord)1 - (xPath->rectY0 - rectY0I);
423
609
    }
424
425
    // upper left corner
426
2.90k
    if (x0 <= rectX0I) {
427
2.70k
      pix = (Guchar)splashCeil(edge
428
2.70k
             * ((SplashCoord)1 - (xPath->rectX0 - rectX0I))
429
2.70k
             * 255);
430
2.70k
      if (pix < 16) {
431
2.09k
  pix = 16;
432
2.09k
      }
433
2.70k
      line[rectX0I] = pix;
434
2.70k
      x = rectX0I + 1;
435
2.70k
    } else {
436
202
      x = x0;
437
202
    }
438
439
    // upper right corner
440
2.90k
    if (rectX1I <= x1) {
441
2.69k
      pix = (Guchar)splashCeil(edge * (xPath->rectX1 - rectX1I) * 255);
442
2.69k
      if (pix < 16) {
443
2.69k
  pix = 16;
444
2.69k
      }
445
2.69k
      line[rectX1I] = pix;
446
2.69k
      xe = rectX1I - 1;
447
2.69k
    } else {
448
210
      xe = x1;
449
210
    }
450
451
    // upper edge
452
2.90k
    pix = (Guchar)splashCeil(edge * 255);
453
2.90k
    if (pix < 16) {
454
2.29k
      pix = 16;
455
2.29k
    }
456
3.10k
    for (; x <= xe; ++x) {
457
202
      line[x] = pix;
458
202
    }
459
460
  //--- lower edge
461
11.3k
  } else if (y == rectY1I) {
462
95
    edge = xPath->rectY1 - rectY1I;
463
464
    // lower left corner
465
95
    if (x0 <= rectX0I) {
466
95
      pix = (Guchar)splashCeil(edge
467
95
             * ((SplashCoord)1 - (xPath->rectX0 - rectX0I))
468
95
             * 255);
469
95
      if (pix < 16) {
470
92
  pix = 16;
471
92
      }
472
95
      line[rectX0I] = pix;
473
95
      x = rectX0I + 1;
474
95
    } else {
475
0
      x = x0;
476
0
    }
477
478
    // lower right corner
479
95
    if (rectX1I <= x1) {
480
95
      pix = (Guchar)splashCeil(edge * (xPath->rectX1 - rectX1I) * 255);
481
95
      if (pix < 16) {
482
95
  pix = 16;
483
95
      }
484
95
      line[rectX1I] = pix;
485
95
      xe = rectX1I - 1;
486
95
    } else {
487
0
      xe = x1;
488
0
    }
489
490
    // lower edge
491
95
    pix = (Guchar)splashCeil(edge * 255);
492
95
    if (pix < 16) {
493
92
      pix = 16;
494
92
    }
495
95
    for (; x <= xe; ++x) {
496
0
      line[x] = pix;
497
0
    }
498
499
  //--- between the upper and lower edges
500
11.2k
  } else if (y > rectY0I && y < rectY1I) {
501
502
    // left edge
503
10.9k
    if (x0 <= rectX0I) {
504
8.19k
      pix = (Guchar)splashCeil(((SplashCoord)1 - (xPath->rectX0 - rectX0I))
505
8.19k
             * 255);
506
8.19k
      if (pix < 16) {
507
0
  pix = 16;
508
0
      }
509
8.19k
      line[rectX0I] = pix;
510
8.19k
      x = rectX0I + 1;
511
8.19k
    } else {
512
2.75k
      x = x0;
513
2.75k
    }
514
515
    // right edge
516
10.9k
    if (rectX1I <= x1) {
517
5.71k
      pix = (Guchar)splashCeil((xPath->rectX1 - rectX1I) * 255);
518
5.71k
      if (pix < 16) {
519
5.51k
  pix = 16;
520
5.51k
      }
521
5.71k
      line[rectX1I] = pix;
522
5.71k
      xe = rectX1I - 1;
523
5.71k
    } else {
524
5.23k
      xe = x1;
525
5.23k
    }
526
527
    // middle
528
13.6k
    for (; x <= xe; ++x) {
529
2.68k
      line[x] = 255;
530
2.68k
    }
531
10.9k
  }
532
14.2k
}
533
534
void SplashXPathScanner::drawRectangleSpanBinary(Guchar *line, int y,
535
             int x0, int x1,
536
39
             int *xMin, int *xMax) {
537
39
  int xe, x;
538
539
39
  if (y >= rectY0I && y <= rectY1I) {
540
39
    if (x0 <= rectX0I) {
541
39
      x = rectX0I;
542
39
    } else {
543
0
      x = x0;
544
0
    }
545
39
    *xMin = x;
546
39
    if (rectX1I <= x1) {
547
39
      xe = rectX1I;
548
39
    } else {
549
0
      xe = x1;
550
0
    }
551
39
    *xMax = xe;
552
78
    for (; x <= xe; ++x) {
553
39
      line[x] = 255;
554
39
    }
555
39
  }
556
39
}
557
558
void SplashXPathScanner::getSpan(Guchar *line, int y, int x0, int x1,
559
100k
         int *xMin, int *xMax) {
560
100k
  int iy, x, k;
561
562
100k
  iy = y * aaVert;
563
100k
  if (!resetDone || !resetAA) {
564
41.0k
    reset(gTrue, gTrue);
565
58.9k
  } else if (yBottomI > iy) {
566
58.9k
    reset(gTrue, gFalse);
567
58.9k
  }
568
100k
  memset(line + x0, 0, x1 - x0 + 1);
569
570
100k
  *xMin = x1 + 1;
571
100k
  *xMax = x0 - 1;
572
573
100k
  if (xPath->isRect) {
574
14.4k
    drawRectangleSpan(line, y, x0, x1, xMin, xMax);
575
14.4k
    return;
576
14.4k
  }
577
578
85.6k
  if (yBottomI < iy) {
579
79.6k
    skip(iy, gTrue);
580
79.6k
  }
581
428k
  for (k = 0; k < aaVert; ++k, ++iy) {
582
342k
    advance(gTrue);
583
342k
    generatePixels(x0, x1, line, xMin, xMax);
584
342k
  }
585
586
85.6k
#if !ANTIALIAS_256
587
165k
  for (x = *xMin; x <= *xMax; ++x) {
588
79.4k
    line[x] = map16to255[line[x]];
589
79.4k
  }
590
85.6k
#endif
591
85.6k
}
592
593
void SplashXPathScanner::getSpanBinary(Guchar *line, int y, int x0, int x1,
594
7.99k
               int *xMin, int *xMax) {
595
7.99k
  int iy;
596
597
7.99k
  iy = y;
598
7.99k
  if (!resetDone || resetAA) {
599
5.61k
    reset(gFalse, gTrue);
600
5.61k
  } else if (yBottomI > iy) {
601
2.37k
    reset(gFalse, gFalse);
602
2.37k
  }
603
7.99k
  memset(line + x0, 0, x1 - x0 + 1);
604
605
7.99k
  *xMin = x1 + 1;
606
7.99k
  *xMax = x0 - 1;
607
608
7.99k
  if (xPath->isRect) {
609
39
    drawRectangleSpanBinary(line, y, x0, x1, xMin, xMax);
610
39
    return;
611
39
  }
612
613
7.95k
  if (yBottomI < iy) {
614
6.79k
    skip(iy, gFalse);
615
6.79k
  }
616
7.95k
  advance(gFalse);
617
7.95k
  generatePixelsBinary(x0, x1, line, xMin, xMax);
618
7.95k
}