Coverage Report

Created: 2026-07-04 07:15

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