Coverage Report

Created: 2026-03-07 07:37

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