Debian Codegraph @

libs/xpdf/xpdf-src/splash/SplashXPathScanner.cc

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