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/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
8.98M
#define aaVert  4
33
1.16M
#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
51.6k
               int yMinA, int yMaxA) {
63
51.6k
  xPath = xPathA;
64
51.6k
  eoMask = eo ? 1 : 0xffffffff;
65
51.6k
  yMin = yMinA;
66
51.6k
  yMax = yMaxA;
67
51.6k
  if (xPath->isRect) {
68
3.54k
    rectX0I = splashFloor(xPath->rectX0);
69
3.54k
    rectY0I = splashFloor(xPath->rectY0);
70
3.54k
    rectX1I = splashFloor(xPath->rectX1);
71
3.54k
    rectY1I = splashFloor(xPath->rectY1);
72
3.54k
  }
73
74
51.6k
  pre = &preSeg;
75
51.6k
  post = &postSeg;
76
51.6k
  pre->mx = xPath->xMin - 1;
77
51.6k
  post->mx = xPath->xMax + 1;
78
79
51.6k
  resetDone = gFalse;
80
51.6k
  resetAA = gFalse;
81
51.6k
}
82
83
51.6k
SplashXPathScanner::~SplashXPathScanner() {
84
51.6k
}
85
86
void SplashXPathScanner::insertSegmentBefore(SplashXPathSeg *s,
87
321k
               SplashXPathSeg *sNext) {
88
321k
  SplashXPathSeg *sPrev;
89
90
321k
  sPrev = sNext->prev;
91
321k
  sPrev->next = s;
92
321k
  s->prev = sPrev;
93
321k
  s->next = sNext;
94
321k
  sNext->prev = s;
95
321k
}
96
97
5.82k
void SplashXPathScanner::removeSegment(SplashXPathSeg *s) {
98
5.82k
  SplashXPathSeg *sPrev, *sNext;
99
100
5.82k
  sPrev = s->prev;
101
5.82k
  sNext = s->next;
102
5.82k
  sPrev->next = sNext;
103
5.82k
  sNext->prev = sPrev;
104
5.82k
  s->prev = s->next = NULL;
105
5.82k
}
106
107
void SplashXPathScanner::moveSegmentAfter(SplashXPathSeg *s,
108
46.6k
            SplashXPathSeg *sPrev) {
109
46.6k
  SplashXPathSeg *sNext;
110
111
46.6k
  s->prev->next = s->next;
112
46.6k
  s->next->prev = s->prev;
113
114
46.6k
  sNext = sPrev->next;
115
46.6k
  sPrev->next = s;
116
46.6k
  s->prev = sPrev;
117
46.6k
  s->next = sNext;
118
46.6k
  sNext->prev = s;
119
46.6k
}
120
121
5.71k
void SplashXPathScanner::reset(GBool aa, GBool aaChanged) {
122
5.71k
  SplashXPathSeg *seg;
123
5.71k
  SplashCoord y;
124
5.71k
  int i;
125
126
  //--- initialize segment parameters
127
7.91M
  for (i = 0; i < xPath->length; ++i) {
128
7.90M
    seg = &xPath->segs[i];
129
7.90M
    if (aa) {
130
4.50M
      if (aaChanged) {
131
4.43M
  seg->iy = splashFloor(seg->y0 * aaVert);
132
4.43M
      }
133
4.50M
      y = (SplashCoord)(seg->iy + 1) / (SplashCoord)aaVert;
134
4.50M
    } else {
135
3.40M
      if (aaChanged) {
136
3.37M
  seg->iy = splashFloor(seg->y0);
137
3.37M
      }
138
3.40M
      y = (SplashCoord)(seg->iy + 1);
139
3.40M
    }
140
7.90M
    seg->sx0 = seg->x0;
141
7.90M
    if (seg->y1 <= y) {
142
3.60M
      seg->sx1 = seg->x1;
143
4.30M
    } else {
144
4.30M
      seg->sx1 = seg->x0 + (y - seg->y0) * seg->dxdy;
145
4.30M
    }
146
7.90M
    seg->mx = (seg->sx0 <= seg->sx1) ? seg->sx0 : seg->sx1;
147
7.90M
    seg->prev = seg->next = NULL;
148
7.90M
  }
149
150
  //--- sort the inactive segments by iy, mx
151
5.71k
  if (aaChanged) {
152
5.65k
#if HAVE_STD_SORT
153
5.65k
    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
5.65k
  }
159
160
  //--- initialize the active list
161
5.71k
  pre->prev = NULL;
162
5.71k
  pre->next = post;
163
5.71k
  post->prev = pre;
164
5.71k
  post->next = NULL;
165
166
  //--- initialize the scan state
167
5.71k
  nextSeg = 0;
168
5.71k
  if (xPath->length) {
169
4.92k
    yBottomI = xPath->segs[0].iy;
170
4.92k
    if (aa) {
171
3.24k
      yBottomI -= yBottomI % aaVert;
172
3.24k
    }
173
4.92k
  } else {
174
789
    yBottomI = 0;
175
789
  }
176
5.71k
  yTopI = yBottomI - 1;
177
5.71k
  if (aa) {
178
4.03k
    yTop = (SplashCoord)yTopI / (SplashCoord)aaVert;
179
4.03k
    yBottom = (SplashCoord)yBottomI / (SplashCoord)aaVert;
180
4.03k
  } else {
181
1.67k
    yTop = (SplashCoord)yTopI;
182
1.67k
    yBottom = (SplashCoord)yBottomI;
183
1.67k
  }
184
185
5.71k
  resetDone = gTrue;
186
5.71k
  resetAA = aa;
187
5.71k
}
188
189
3.93k
void SplashXPathScanner::skip(int newYBottomI, GBool aa) {
190
3.93k
  SplashXPathSeg *s0, *s1,*s2;
191
3.93k
  int iy;
192
193
3.93k
  yTopI = newYBottomI - 1;
194
3.93k
  yBottomI = newYBottomI;
195
3.93k
  if (aa) {
196
2.32k
    yTop = (SplashCoord)yTopI / (SplashCoord)aaVert;
197
2.32k
    yBottom = (SplashCoord)yBottomI / (SplashCoord)aaVert;
198
2.32k
  } else {
199
1.61k
    yTop = (SplashCoord)yTopI;
200
1.61k
    yBottom = (SplashCoord)yBottomI;
201
1.61k
  }
202
203
  //--- remove finished segments; update sx0, sx1, mx for active segments
204
3.93k
  s0 = pre->next;
205
3.93k
  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
3.93k
  s0 = pre->next;
232
3.93k
  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
1.55M
  while (nextSeg < xPath->length && xPath->segs[nextSeg].iy <= yTopI) {
249
    // the first inactive segment determines the next scanline to process
250
1.54M
    iy = xPath->segs[nextSeg].iy;
251
1.54M
    s0 = pre->next;
252
3.37M
    do {
253
3.37M
      s1 = &xPath->segs[nextSeg];
254
3.37M
      ++nextSeg;
255
3.37M
      if (s1->y1 < yTop) {
256
3.06M
  continue;
257
3.06M
      }
258
309k
      if (s1->y0 >= yTop) {
259
68
  s1->sx0 = s1->x0;
260
309k
      } else {
261
309k
  s1->sx0 = s1->x0 + (yTop - s1->y0) * s1->dxdy;
262
309k
      }
263
309k
      if (s1->y1 <= yBottom) {
264
1.49k
  s1->sx1 = s1->x1;
265
308k
      } else {
266
308k
  s1->sx1 = s1->x0 + (yBottom - s1->y0) * s1->dxdy;
267
308k
      }
268
309k
      s1->mx = (s1->sx0 <= s1->sx1) ? s1->sx0 : s1->sx1;
269
309k
      insertSegmentBefore(s1, s0);
270
3.37M
    } while (nextSeg < xPath->length && xPath->segs[nextSeg].iy <= iy);
271
1.54M
  }
272
3.93k
}
273
274
14.3k
void SplashXPathScanner::advance(GBool aa) {
275
14.3k
  SplashXPathSeg *s, *sNext, *s1;
276
277
14.3k
  yTopI = yBottomI;
278
14.3k
  yTop = yBottom;
279
14.3k
  yBottomI = yTopI + 1;
280
14.3k
  if (aa) {
281
12.7k
    yBottom = (SplashCoord)yBottomI / (SplashCoord)aaVert;
282
12.7k
  } else {
283
1.61k
    yBottom = (SplashCoord)yBottomI;
284
1.61k
  }
285
286
14.3k
  s = pre->next;
287
1.25M
  while (s != post) {
288
1.24M
    sNext = s->next;
289
290
    // check for a finished segment
291
1.24M
    if (s->y1 < yTop) {
292
5.82k
      removeSegment(s);
293
294
1.23M
    } else {
295
296
      // compute sx0, sx1, mx
297
1.23M
      s->sx0 = s->sx1;
298
1.23M
      if (s->y1 <= yBottom) {
299
4.57k
  s->sx1 = s->x1;
300
1.23M
      } else {
301
1.23M
  s->sx1 = s->x0 + (yBottom - s->y0) * s->dxdy;
302
1.23M
      }
303
1.23M
      s->mx = (s->sx0 <= s->sx1) ? s->sx0 : s->sx1;
304
305
      // check for intersection
306
1.23M
      if (s->mx < s->prev->mx) {
307
77.8M
  for (s1 = s->prev->prev; s->mx < s1->mx; s1 = s1->prev) ;
308
46.6k
  moveSegmentAfter(s, s1);
309
46.6k
      }
310
1.23M
    }
311
312
1.24M
    s = sNext;
313
1.24M
  }
314
315
  // insert new segments
316
14.3k
  s = pre->next;
317
25.8k
  while (nextSeg < xPath->length && xPath->segs[nextSeg].iy <= yTopI) {
318
11.4k
    s1 = &xPath->segs[nextSeg];
319
11.4k
    ++nextSeg;
320
67.0k
    while (s1->mx > s->mx) {
321
55.5k
      s = s->next;
322
55.5k
    }
323
11.4k
    insertSegmentBefore(s1, s);
324
11.4k
  }
325
14.3k
}
326
327
void SplashXPathScanner::generatePixels(int x0, int x1, Guchar *line,
328
12.7k
          int *xMin, int *xMax) {
329
12.7k
  SplashXPathSeg *s;
330
12.7k
  int fillCount, x, xEnd, ix0, ix1, t;
331
332
12.7k
  fillCount = 0;
333
12.7k
  x = x0 * aaHoriz;
334
12.7k
  xEnd = (x1 + 1) * aaHoriz;
335
292k
  for (s = pre->next; s != post && x < xEnd; s = s->next) {
336
280k
    ix0 = splashFloor(s->sx0 * aaHoriz);
337
280k
    ix1 = splashFloor(s->sx1 * aaHoriz);
338
280k
    if (ix0 > ix1) {
339
62.8k
      t = ix0;  ix0 = ix1;  ix1 = t;
340
62.8k
    }
341
280k
    if (!(fillCount & eoMask)) {
342
27.0k
      if (ix0 > x) {
343
4.54k
  x = ix0;
344
4.54k
      }
345
27.0k
    }
346
280k
    if (ix1 >= xEnd) {
347
6.13k
      ix1 = xEnd - 1;
348
6.13k
    }
349
280k
    if (x / aaHoriz < *xMin) {
350
2.09k
      *xMin = x / aaHoriz;
351
2.09k
    }
352
280k
    if (ix1 / aaHoriz > *xMax) {
353
2.36k
      *xMax = ix1 / aaHoriz;
354
2.36k
    }
355
295k
    for (; x <= ix1; ++x) {
356
15.0k
      ++line[x / aaHoriz];
357
15.0k
    }
358
280k
    if (s->y0 <= yTop && s->y1 > yTop) {
359
279k
      fillCount += s->count;
360
279k
    }
361
280k
  }
362
12.7k
}
363
364
void SplashXPathScanner::generatePixelsBinary(int x0, int x1, Guchar *line,
365
1.61k
                int *xMin, int *xMax) {
366
1.61k
  SplashXPathSeg *s;
367
1.61k
  int fillCount, x, xEnd, ix0, ix1, t;
368
369
1.61k
  fillCount = 0;
370
1.61k
  x = x0;
371
1.61k
  xEnd = x1 + 1;
372
3.24k
  for (s = pre->next; s != post && x < xEnd; s = s->next) {
373
1.63k
    ix0 = splashFloor(s->sx0);
374
1.63k
    ix1 = splashFloor(s->sx1);
375
1.63k
    if (ix0 > ix1) {
376
511
      t = ix0;  ix0 = ix1;  ix1 = t;
377
511
    }
378
1.63k
    if (!(fillCount & eoMask)) {
379
1.63k
      if (ix0 > x) {
380
113
  x = ix0;
381
113
      }
382
1.63k
    }
383
1.63k
    if (ix1 >= xEnd) {
384
113
      ix1 = xEnd - 1;
385
113
    }
386
1.63k
    if (x < *xMin) {
387
1.49k
      *xMin = x;
388
1.49k
    }
389
1.63k
    if (ix1 > *xMax) {
390
1.61k
      *xMax = ix1;
391
1.61k
    }
392
3.13k
    for (; x <= ix1; ++x) {
393
1.49k
      line[x] = 255;
394
1.49k
    }
395
1.63k
    if (s->y0 <= yTop && s->y1 > yTop) {
396
1.61k
      fillCount += s->count;
397
1.61k
    }
398
1.63k
  }
399
1.61k
}
400
401
void SplashXPathScanner::drawRectangleSpan(Guchar *line, int y,
402
             int x0, int x1,
403
840
             int *xMin, int *xMax) {
404
840
  SplashCoord edge;
405
840
  Guchar pix;
406
840
  int xe, x;
407
408
840
  if (rectX0I > x1 || rectX1I < x0) {
409
0
    return;
410
0
  }
411
412
840
  *xMin = x0 <= rectX0I ? rectX0I : x0;
413
840
  *xMax = rectX1I <= x1 ? rectX1I : x1;
414
415
  //--- upper edge
416
840
  if (y == rectY0I) {
417
418
    // handle a rectangle less than one pixel high
419
11
    if (rectY0I == rectY1I) {
420
0
      edge = xPath->rectY1 - xPath->rectY0;
421
11
    } else {
422
11
      edge = (SplashCoord)1 - (xPath->rectY0 - rectY0I);
423
11
    }
424
425
    // upper left corner
426
11
    if (x0 <= rectX0I) {
427
11
      pix = (Guchar)splashCeil(edge
428
11
             * ((SplashCoord)1 - (xPath->rectX0 - rectX0I))
429
11
             * 255);
430
11
      if (pix < 16) {
431
0
  pix = 16;
432
0
      }
433
11
      line[rectX0I] = pix;
434
11
      x = rectX0I + 1;
435
11
    } else {
436
0
      x = x0;
437
0
    }
438
439
    // upper right corner
440
11
    if (rectX1I <= x1) {
441
0
      pix = (Guchar)splashCeil(edge * (xPath->rectX1 - rectX1I) * 255);
442
0
      if (pix < 16) {
443
0
  pix = 16;
444
0
      }
445
0
      line[rectX1I] = pix;
446
0
      xe = rectX1I - 1;
447
11
    } else {
448
11
      xe = x1;
449
11
    }
450
451
    // upper edge
452
11
    pix = (Guchar)splashCeil(edge * 255);
453
11
    if (pix < 16) {
454
0
      pix = 16;
455
0
    }
456
11
    for (; x <= xe; ++x) {
457
0
      line[x] = pix;
458
0
    }
459
460
  //--- lower edge
461
829
  } else if (y == rectY1I) {
462
5
    edge = xPath->rectY1 - rectY1I;
463
464
    // lower left corner
465
5
    if (x0 <= rectX0I) {
466
4
      pix = (Guchar)splashCeil(edge
467
4
             * ((SplashCoord)1 - (xPath->rectX0 - rectX0I))
468
4
             * 255);
469
4
      if (pix < 16) {
470
1
  pix = 16;
471
1
      }
472
4
      line[rectX0I] = pix;
473
4
      x = rectX0I + 1;
474
4
    } else {
475
1
      x = x0;
476
1
    }
477
478
    // lower right corner
479
5
    if (rectX1I <= x1) {
480
4
      pix = (Guchar)splashCeil(edge * (xPath->rectX1 - rectX1I) * 255);
481
4
      if (pix < 16) {
482
4
  pix = 16;
483
4
      }
484
4
      line[rectX1I] = pix;
485
4
      xe = rectX1I - 1;
486
4
    } else {
487
1
      xe = x1;
488
1
    }
489
490
    // lower edge
491
5
    pix = (Guchar)splashCeil(edge * 255);
492
5
    if (pix < 16) {
493
2
      pix = 16;
494
2
    }
495
6
    for (; x <= xe; ++x) {
496
1
      line[x] = pix;
497
1
    }
498
499
  //--- between the upper and lower edges
500
824
  } else if (y > rectY0I && y < rectY1I) {
501
502
    // left edge
503
813
    if (x0 <= rectX0I) {
504
363
      pix = (Guchar)splashCeil(((SplashCoord)1 - (xPath->rectX0 - rectX0I))
505
363
             * 255);
506
363
      if (pix < 16) {
507
0
  pix = 16;
508
0
      }
509
363
      line[rectX0I] = pix;
510
363
      x = rectX0I + 1;
511
450
    } else {
512
450
      x = x0;
513
450
    }
514
515
    // right edge
516
813
    if (rectX1I <= x1) {
517
24
      pix = (Guchar)splashCeil((xPath->rectX1 - rectX1I) * 255);
518
24
      if (pix < 16) {
519
20
  pix = 16;
520
20
      }
521
24
      line[rectX1I] = pix;
522
24
      xe = rectX1I - 1;
523
789
    } else {
524
789
      xe = x1;
525
789
    }
526
527
    // middle
528
1.26k
    for (; x <= xe; ++x) {
529
450
      line[x] = 255;
530
450
    }
531
813
  }
532
840
}
533
534
void SplashXPathScanner::drawRectangleSpanBinary(Guchar *line, int y,
535
             int x0, int x1,
536
68
             int *xMin, int *xMax) {
537
68
  int xe, x;
538
539
68
  if (y >= rectY0I && y <= rectY1I) {
540
68
    if (x0 <= rectX0I) {
541
68
      x = rectX0I;
542
68
    } else {
543
0
      x = x0;
544
0
    }
545
68
    *xMin = x;
546
68
    if (rectX1I <= x1) {
547
68
      xe = rectX1I;
548
68
    } else {
549
0
      xe = x1;
550
0
    }
551
68
    *xMax = xe;
552
136
    for (; x <= xe; ++x) {
553
68
      line[x] = 255;
554
68
    }
555
68
  }
556
68
}
557
558
void SplashXPathScanner::getSpan(Guchar *line, int y, int x0, int x1,
559
4.03k
         int *xMin, int *xMax) {
560
4.03k
  int iy, x, k;
561
562
4.03k
  iy = y * aaVert;
563
4.03k
  if (!resetDone || !resetAA) {
564
3.99k
    reset(gTrue, gTrue);
565
3.99k
  } else if (yBottomI > iy) {
566
45
    reset(gTrue, gFalse);
567
45
  }
568
4.03k
  memset(line + x0, 0, x1 - x0 + 1);
569
570
4.03k
  *xMin = x1 + 1;
571
4.03k
  *xMax = x0 - 1;
572
573
4.03k
  if (xPath->isRect) {
574
840
    drawRectangleSpan(line, y, x0, x1, xMin, xMax);
575
840
    return;
576
840
  }
577
578
3.19k
  if (yBottomI < iy) {
579
2.32k
    skip(iy, gTrue);
580
2.32k
  }
581
15.9k
  for (k = 0; k < aaVert; ++k, ++iy) {
582
12.7k
    advance(gTrue);
583
12.7k
    generatePixels(x0, x1, line, xMin, xMax);
584
12.7k
  }
585
586
3.19k
#if !ANTIALIAS_256
587
5.25k
  for (x = *xMin; x <= *xMax; ++x) {
588
2.05k
    line[x] = map16to255[line[x]];
589
2.05k
  }
590
3.19k
#endif
591
3.19k
}
592
593
void SplashXPathScanner::getSpanBinary(Guchar *line, int y, int x0, int x1,
594
1.67k
               int *xMin, int *xMax) {
595
1.67k
  int iy;
596
597
1.67k
  iy = y;
598
1.67k
  if (!resetDone || resetAA) {
599
1.66k
    reset(gFalse, gTrue);
600
1.66k
  } else if (yBottomI > iy) {
601
16
    reset(gFalse, gFalse);
602
16
  }
603
1.67k
  memset(line + x0, 0, x1 - x0 + 1);
604
605
1.67k
  *xMin = x1 + 1;
606
1.67k
  *xMax = x0 - 1;
607
608
1.67k
  if (xPath->isRect) {
609
68
    drawRectangleSpanBinary(line, y, x0, x1, xMin, xMax);
610
68
    return;
611
68
  }
612
613
1.61k
  if (yBottomI < iy) {
614
1.61k
    skip(iy, gFalse);
615
1.61k
  }
616
1.61k
  advance(gFalse);
617
1.61k
  generatePixelsBinary(x0, x1, line, xMin, xMax);
618
1.61k
}