1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
//========================================================================
//
// SplashXPathScanner.cc
//
//========================================================================
#include <config.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <stdlib.h>
#include "goo/gmem.h"
#include "SplashMath.h"
#include "SplashXPath.h"
#include "SplashXPathScanner.h"
//------------------------------------------------------------------------
struct SplashIntersect {
int x0, x1; // intersection of segment with [y, y+1)
int count; // EO/NZWN counter increment
};
static int cmpIntersect(const void *p0, const void *p1) {
return ((SplashIntersect *)p0)->x0 - ((SplashIntersect *)p1)->x0;
}
//------------------------------------------------------------------------
// SplashXPathScanner
//------------------------------------------------------------------------
SplashXPathScanner::SplashXPathScanner(SplashXPath *xPathA, GBool eoA) {
SplashXPathSeg *seg;
SplashCoord xMinFP, yMinFP, xMaxFP, yMaxFP;
int i;
xPath = xPathA;
eo = eoA;
// compute the bbox
seg = &xPath->segs[0];
if (seg->x0 <= seg->x1) {
xMinFP = seg->x0;
xMaxFP = seg->x1;
} else {
xMinFP = seg->x1;
xMaxFP = seg->x0;
}
if (seg->flags & splashXPathFlip) {
yMinFP = seg->y1;
yMaxFP = seg->y0;
} else {
yMinFP = seg->y0;
yMaxFP = seg->y1;
}
for (i = 1; i < xPath->length; ++i) {
seg = &xPath->segs[i];
if (seg->x0 < xMinFP) {
xMinFP = seg->x0;
} else if (seg->x0 > xMaxFP) {
xMaxFP = seg->x0;
}
if (seg->x1 < xMinFP) {
xMinFP = seg->x1;
} else if (seg->x1 > xMaxFP) {
xMaxFP = seg->x1;
}
if (seg->flags & splashXPathFlip) {
if (seg->y0 > yMaxFP) {
yMaxFP = seg->y0;
}
} else {
if (seg->y1 > yMaxFP) {
yMaxFP = seg->y1;
}
}
}
xMin = splashFloor(xMinFP);
xMax = splashFloor(xMaxFP);
yMin = splashFloor(yMinFP);
yMax = splashFloor(yMaxFP);
interY = 0;
xPathIdx = 0;
inter = NULL;
interLen = interSize = 0;
computeIntersections(yMin);
}
SplashXPathScanner::~SplashXPathScanner() {
gfree(inter);
}
void SplashXPathScanner::getSpanBounds(int y, int *spanXMin, int *spanXMax) {
if (interY != y) {
computeIntersections(y);
}
if (interLen > 0) {
*spanXMin = inter[0].x0;
*spanXMax = inter[interLen - 1].x1;
} else {
*spanXMin = xMax + 1;
*spanXMax = xMax;
}
}
GBool SplashXPathScanner::test(int x, int y) {
int count, i;
if (interY != y) {
computeIntersections(y);
}
count = 0;
for (i = 0; i < interLen && inter[i].x0 <= x; ++i) {
if (x <= inter[i].x1) {
return gTrue;
}
count += inter[i].count;
}
return eo ? (count & 1) : (count != 0);
}
GBool SplashXPathScanner::testSpan(int x0, int x1, int y) {
int count, xx1, i;
if (interY != y) {
computeIntersections(y);
}
count = 0;
for (i = 0; i < interLen && inter[i].x1 < x0; ++i) {
count += inter[i].count;
}
// invariant: the subspan [x0,xx1] is inside the path
xx1 = x0 - 1;
while (xx1 < x1) {
if (i >= interLen) {
return gFalse;
}
if (inter[i].x0 > xx1 + 1 &&
!(eo ? (count & 1) : (count != 0))) {
return gFalse;
}
if (inter[i].x1 > xx1) {
xx1 = inter[i].x1;
}
count += inter[i].count;
++i;
}
return gTrue;
}
GBool SplashXPathScanner::getNextSpan(int y, int *x0, int *x1) {
int xx0, xx1;
if (interY != y) {
computeIntersections(y);
}
if (interIdx >= interLen) {
return gFalse;
}
xx0 = inter[interIdx].x0;
xx1 = inter[interIdx].x1;
interCount += inter[interIdx].count;
++interIdx;
while (interIdx < interLen &&
(inter[interIdx].x0 <= xx1 ||
(eo ? (interCount & 1) : (interCount != 0)))) {
if (inter[interIdx].x1 > xx1) {
xx1 = inter[interIdx].x1;
}
interCount += inter[interIdx].count;
++interIdx;
}
*x0 = xx0;
*x1 = xx1;
return gTrue;
}
void SplashXPathScanner::computeIntersections(int y) {
SplashCoord xSegMin, xSegMax, ySegMin, ySegMax, xx0, xx1;
SplashXPathSeg *seg;
int i, j;
// find the first segment that intersects [y, y+1)
i = (y >= interY) ? xPathIdx : 0;
while (i < xPath->length &&
xPath->segs[i].y0 < y && xPath->segs[i].y1 < y) {
++i;
}
xPathIdx = i;
// find all of the segments that intersect [y, y+1) and create an
// Intersect element for each one
interLen = 0;
for (j = i; j < xPath->length; ++j) {
seg = &xPath->segs[j];
if (seg->flags & splashXPathFlip) {
ySegMin = seg->y1;
ySegMax = seg->y0;
} else {
ySegMin = seg->y0;
ySegMax = seg->y1;
}
// ensure that: ySegMin < y+1
// y <= ySegMax
if (ySegMin >= y + 1) {
break;
}
if (ySegMax < y) {
continue;
}
if (interLen == interSize) {
if (interSize == 0) {
interSize = 16;
} else {
interSize *= 2;
}
inter = (SplashIntersect *)grealloc(inter,
interSize * sizeof(SplashIntersect));
}
if (seg->flags & splashXPathHoriz) {
xx0 = seg->x0;
xx1 = seg->x1;
} else if (seg->flags & splashXPathVert) {
xx0 = xx1 = seg->x0;
} else {
if (seg->x0 < seg->x1) {
xSegMin = seg->x0;
xSegMax = seg->x1;
} else {
xSegMin = seg->x1;
xSegMax = seg->x0;
}
// intersection with top edge
xx0 = seg->x0 + ((SplashCoord)y - seg->y0) * seg->dxdy;
// intersection with bottom edge
xx1 = seg->x0 + ((SplashCoord)y + 1 - seg->y0) * seg->dxdy;
// the segment may not actually extend to the top and/or bottom edges
if (xx0 < xSegMin) {
xx0 = xSegMin;
} else if (xx0 > xSegMax) {
xx0 = xSegMax;
}
if (xx1 < xSegMin) {
xx1 = xSegMin;
} else if (xx1 > xSegMax) {
xx1 = xSegMax;
}
}
if (xx0 < xx1) {
inter[interLen].x0 = splashFloor(xx0);
inter[interLen].x1 = splashFloor(xx1);
} else {
inter[interLen].x0 = splashFloor(xx1);
inter[interLen].x1 = splashFloor(xx0);
}
if (ySegMin <= y && y < ySegMax && !(seg->flags & splashXPathHoriz)) {
inter[interLen].count = eo ? 1
: (seg->flags & splashXPathFlip) ? 1 : -1;
} else {
inter[interLen].count = 0;
}
++interLen;
}
qsort(inter, interLen, sizeof(SplashIntersect), &cmpIntersect);
interY = y;
interIdx = 0;
interCount = 0;
}
|