/src/xpdf-4.05/splash/SplashPath.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //======================================================================== |
2 | | // |
3 | | // SplashPath.cc |
4 | | // |
5 | | // Copyright 2003-2013 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include <string.h> |
12 | | #include "gmem.h" |
13 | | #include "gmempp.h" |
14 | | #include "SplashErrorCodes.h" |
15 | | #include "SplashPath.h" |
16 | | |
17 | | //------------------------------------------------------------------------ |
18 | | // SplashPath |
19 | | //------------------------------------------------------------------------ |
20 | | |
21 | | // A path can be in three possible states: |
22 | | // |
23 | | // 1. no current point -- zero or more finished subpaths |
24 | | // [curSubpath == length] |
25 | | // |
26 | | // 2. one point in subpath |
27 | | // [curSubpath == length - 1] |
28 | | // |
29 | | // 3. open subpath with two or more points |
30 | | // [curSubpath < length - 1] |
31 | | |
32 | 0 | SplashPath::SplashPath() { |
33 | 0 | pts = NULL; |
34 | 0 | flags = NULL; |
35 | 0 | length = size = 0; |
36 | 0 | curSubpath = 0; |
37 | 0 | hints = NULL; |
38 | 0 | hintsLength = hintsSize = 0; |
39 | 0 | } |
40 | | |
41 | 0 | SplashPath::SplashPath(SplashPath *path) { |
42 | 0 | length = path->length; |
43 | 0 | size = path->size; |
44 | 0 | pts = (SplashPathPoint *)gmallocn(size, sizeof(SplashPathPoint)); |
45 | 0 | flags = (Guchar *)gmallocn(size, sizeof(Guchar)); |
46 | 0 | memcpy(pts, path->pts, length * sizeof(SplashPathPoint)); |
47 | 0 | memcpy(flags, path->flags, length * sizeof(Guchar)); |
48 | 0 | curSubpath = path->curSubpath; |
49 | 0 | if (path->hints) { |
50 | 0 | hintsLength = hintsSize = path->hintsLength; |
51 | 0 | hints = (SplashPathHint *)gmallocn(hintsSize, sizeof(SplashPathHint)); |
52 | 0 | memcpy(hints, path->hints, hintsLength * sizeof(SplashPathHint)); |
53 | 0 | } else { |
54 | 0 | hints = NULL; |
55 | 0 | hintsLength = hintsSize = 0; |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | 0 | SplashPath::~SplashPath() { |
60 | 0 | gfree(pts); |
61 | 0 | gfree(flags); |
62 | 0 | gfree(hints); |
63 | 0 | } |
64 | | |
65 | | // Add space for <nPts> more points. |
66 | 0 | void SplashPath::grow(int nPts) { |
67 | 0 | if (length + nPts > size) { |
68 | 0 | if (size == 0) { |
69 | 0 | size = 32; |
70 | 0 | } |
71 | 0 | while (size < length + nPts) { |
72 | 0 | size *= 2; |
73 | 0 | } |
74 | 0 | pts = (SplashPathPoint *)greallocn(pts, size, sizeof(SplashPathPoint)); |
75 | 0 | flags = (Guchar *)greallocn(flags, size, sizeof(Guchar)); |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | 0 | void SplashPath::append(SplashPath *path) { |
80 | 0 | int i; |
81 | |
|
82 | 0 | curSubpath = length + path->curSubpath; |
83 | 0 | grow(path->length); |
84 | 0 | for (i = 0; i < path->length; ++i) { |
85 | 0 | pts[length] = path->pts[i]; |
86 | 0 | flags[length] = path->flags[i]; |
87 | 0 | ++length; |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | 0 | SplashError SplashPath::moveTo(SplashCoord x, SplashCoord y) { |
92 | 0 | if (onePointSubpath()) { |
93 | 0 | return splashErrBogusPath; |
94 | 0 | } |
95 | 0 | grow(1); |
96 | 0 | pts[length].x = x; |
97 | 0 | pts[length].y = y; |
98 | 0 | flags[length] = splashPathFirst | splashPathLast; |
99 | 0 | curSubpath = length++; |
100 | 0 | return splashOk; |
101 | 0 | } |
102 | | |
103 | 0 | SplashError SplashPath::lineTo(SplashCoord x, SplashCoord y) { |
104 | 0 | if (noCurrentPoint()) { |
105 | 0 | return splashErrNoCurPt; |
106 | 0 | } |
107 | 0 | flags[length-1] &= (Guchar)~splashPathLast; |
108 | 0 | grow(1); |
109 | 0 | pts[length].x = x; |
110 | 0 | pts[length].y = y; |
111 | 0 | flags[length] = splashPathLast; |
112 | 0 | ++length; |
113 | 0 | return splashOk; |
114 | 0 | } |
115 | | |
116 | | SplashError SplashPath::curveTo(SplashCoord x1, SplashCoord y1, |
117 | | SplashCoord x2, SplashCoord y2, |
118 | 0 | SplashCoord x3, SplashCoord y3) { |
119 | 0 | if (noCurrentPoint()) { |
120 | 0 | return splashErrNoCurPt; |
121 | 0 | } |
122 | 0 | flags[length-1] &= (Guchar)~splashPathLast; |
123 | 0 | grow(3); |
124 | 0 | pts[length].x = x1; |
125 | 0 | pts[length].y = y1; |
126 | 0 | flags[length] = splashPathCurve; |
127 | 0 | ++length; |
128 | 0 | pts[length].x = x2; |
129 | 0 | pts[length].y = y2; |
130 | 0 | flags[length] = splashPathCurve; |
131 | 0 | ++length; |
132 | 0 | pts[length].x = x3; |
133 | 0 | pts[length].y = y3; |
134 | 0 | flags[length] = splashPathLast; |
135 | 0 | ++length; |
136 | 0 | return splashOk; |
137 | 0 | } |
138 | | |
139 | 0 | SplashError SplashPath::close(GBool force) { |
140 | 0 | if (noCurrentPoint()) { |
141 | 0 | return splashErrNoCurPt; |
142 | 0 | } |
143 | 0 | if (force || |
144 | 0 | curSubpath == length - 1 || |
145 | 0 | pts[length - 1].x != pts[curSubpath].x || |
146 | 0 | pts[length - 1].y != pts[curSubpath].y) { |
147 | 0 | lineTo(pts[curSubpath].x, pts[curSubpath].y); |
148 | 0 | } |
149 | 0 | flags[curSubpath] |= splashPathClosed; |
150 | 0 | flags[length - 1] |= splashPathClosed; |
151 | 0 | curSubpath = length; |
152 | 0 | return splashOk; |
153 | 0 | } |
154 | | |
155 | | void SplashPath::addStrokeAdjustHint(int ctrl0, int ctrl1, |
156 | | int firstPt, int lastPt, |
157 | 0 | GBool projectingCap) { |
158 | 0 | if (hintsLength == hintsSize) { |
159 | 0 | hintsSize = hintsLength ? 2 * hintsLength : 8; |
160 | 0 | hints = (SplashPathHint *)greallocn(hints, hintsSize, |
161 | 0 | sizeof(SplashPathHint)); |
162 | 0 | } |
163 | 0 | hints[hintsLength].ctrl0 = ctrl0; |
164 | 0 | hints[hintsLength].ctrl1 = ctrl1; |
165 | 0 | hints[hintsLength].firstPt = firstPt; |
166 | 0 | hints[hintsLength].lastPt = lastPt; |
167 | 0 | hints[hintsLength].projectingCap = projectingCap; |
168 | 0 | ++hintsLength; |
169 | 0 | } |
170 | | |
171 | 0 | void SplashPath::offset(SplashCoord dx, SplashCoord dy) { |
172 | 0 | int i; |
173 | |
|
174 | 0 | for (i = 0; i < length; ++i) { |
175 | 0 | pts[i].x += dx; |
176 | 0 | pts[i].y += dy; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | 0 | GBool SplashPath::getCurPt(SplashCoord *x, SplashCoord *y) { |
181 | 0 | if (noCurrentPoint()) { |
182 | 0 | return gFalse; |
183 | 0 | } |
184 | 0 | *x = pts[length - 1].x; |
185 | 0 | *y = pts[length - 1].y; |
186 | 0 | return gTrue; |
187 | 0 | } |
188 | | |
189 | 0 | GBool SplashPath::containsZeroLengthSubpaths() { |
190 | 0 | GBool zeroLength; |
191 | 0 | int i; |
192 | |
|
193 | 0 | zeroLength = gTrue; // make gcc happy |
194 | 0 | for (i = 0; i < length; ++i) { |
195 | 0 | if (flags[i] & splashPathFirst) { |
196 | 0 | zeroLength = gTrue; |
197 | 0 | } else { |
198 | 0 | if (pts[i].x != pts[i-1].x || pts[i].y != pts[i-1].y) { |
199 | 0 | zeroLength = gFalse; |
200 | 0 | } |
201 | 0 | if (flags[i] & splashPathLast) { |
202 | 0 | if (zeroLength) { |
203 | 0 | return gTrue; |
204 | 0 | } |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } |
208 | 0 | return gFalse; |
209 | 0 | } |