Debian Codegraph @

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

raw
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 SplashPath::SplashPath() {
33 pts = NULL;
34 flags = NULL;
35 length = size = 0;
36 curSubpath = 0;
37 hints = NULL;
38 hintsLength = hintsSize = 0;
39 }
40
41 SplashPath::SplashPath(SplashPath *path) {
42 length = path->length;
43 size = path->size;
44 pts = (SplashPathPoint *)gmallocn(size, sizeof(SplashPathPoint));
45 flags = (Guchar *)gmallocn(size, sizeof(Guchar));
46 memcpy(pts, path->pts, length * sizeof(SplashPathPoint));
47 memcpy(flags, path->flags, length * sizeof(Guchar));
48 curSubpath = path->curSubpath;
49 if (path->hints) {
50 hintsLength = hintsSize = path->hintsLength;
51 hints = (SplashPathHint *)gmallocn(hintsSize, sizeof(SplashPathHint));
52 memcpy(hints, path->hints, hintsLength * sizeof(SplashPathHint));
53 } else {
54 hints = NULL;
55 hintsLength = hintsSize = 0;
56 }
57 }
58
59 SplashPath::~SplashPath() {
60 gfree(pts);
61 gfree(flags);
62 gfree(hints);
63 }
64
65 // Add space for <nPts> more points.
66 void SplashPath::grow(int nPts) {
67 if (length + nPts > size) {
68 if (size == 0) {
69 size = 32;
70 }
71 while (size < length + nPts) {
72 size *= 2;
73 }
74 pts = (SplashPathPoint *)greallocn(pts, size, sizeof(SplashPathPoint));
75 flags = (Guchar *)greallocn(flags, size, sizeof(Guchar));
76 }
77 }
78
79 void SplashPath::append(SplashPath *path) {
80 int i;
81
82 curSubpath = length + path->curSubpath;
83 grow(path->length);
84 for (i = 0; i < path->length; ++i) {
85 pts[length] = path->pts[i];
86 flags[length] = path->flags[i];
87 ++length;
88 }
89 }
90
91 SplashError SplashPath::moveTo(SplashCoord x, SplashCoord y) {
92 if (onePointSubpath()) {
93 return splashErrBogusPath;
94 }
95 grow(1);
96 pts[length].x = x;
97 pts[length].y = y;
98 flags[length] = splashPathFirst | splashPathLast;
99 curSubpath = length++;
100 return splashOk;
101 }
102
103 SplashError SplashPath::lineTo(SplashCoord x, SplashCoord y) {
104 if (noCurrentPoint()) {
105 return splashErrNoCurPt;
106 }
107 flags[length-1] &= (Guchar)~splashPathLast;
108 grow(1);
109 pts[length].x = x;
110 pts[length].y = y;
111 flags[length] = splashPathLast;
112 ++length;
113 return splashOk;
114 }
115
116 SplashError SplashPath::curveTo(SplashCoord x1, SplashCoord y1,
117 SplashCoord x2, SplashCoord y2,
118 SplashCoord x3, SplashCoord y3) {
119 if (noCurrentPoint()) {
120 return splashErrNoCurPt;
121 }
122 flags[length-1] &= (Guchar)~splashPathLast;
123 grow(3);
124 pts[length].x = x1;
125 pts[length].y = y1;
126 flags[length] = splashPathCurve;
127 ++length;
128 pts[length].x = x2;
129 pts[length].y = y2;
130 flags[length] = splashPathCurve;
131 ++length;
132 pts[length].x = x3;
133 pts[length].y = y3;
134 flags[length] = splashPathLast;
135 ++length;
136 return splashOk;
137 }
138
139 SplashError SplashPath::close(GBool force) {
140 if (noCurrentPoint()) {
141 return splashErrNoCurPt;
142 }
143 if (force ||
144 curSubpath == length - 1 ||
145 pts[length - 1].x != pts[curSubpath].x ||
146 pts[length - 1].y != pts[curSubpath].y) {
147 lineTo(pts[curSubpath].x, pts[curSubpath].y);
148 }
149 flags[curSubpath] |= splashPathClosed;
150 flags[length - 1] |= splashPathClosed;
151 curSubpath = length;
152 return splashOk;
153 }
154
155 void SplashPath::addStrokeAdjustHint(int ctrl0, int ctrl1,
156 int firstPt, int lastPt,
157 GBool projectingCap) {
158 if (hintsLength == hintsSize) {
159 hintsSize = hintsLength ? 2 * hintsLength : 8;
160 hints = (SplashPathHint *)greallocn(hints, hintsSize,
161 sizeof(SplashPathHint));
162 }
163 hints[hintsLength].ctrl0 = ctrl0;
164 hints[hintsLength].ctrl1 = ctrl1;
165 hints[hintsLength].firstPt = firstPt;
166 hints[hintsLength].lastPt = lastPt;
167 hints[hintsLength].projectingCap = projectingCap;
168 ++hintsLength;
169 }
170
171 void SplashPath::offset(SplashCoord dx, SplashCoord dy) {
172 int i;
173
174 for (i = 0; i < length; ++i) {
175 pts[i].x += dx;
176 pts[i].y += dy;
177 }
178 }
179
180 GBool SplashPath::getCurPt(SplashCoord *x, SplashCoord *y) {
181 if (noCurrentPoint()) {
182 return gFalse;
183 }
184 *x = pts[length - 1].x;
185 *y = pts[length - 1].y;
186 return gTrue;
187 }
188
189 GBool SplashPath::containsZeroLengthSubpaths() {
190 GBool zeroLength;
191 int i;
192
193 zeroLength = gTrue; // make gcc happy
194 for (i = 0; i < length; ++i) {
195 if (flags[i] & splashPathFirst) {
196 zeroLength = gTrue;
197 } else {
198 if (pts[i].x != pts[i-1].x || pts[i].y != pts[i-1].y) {
199 zeroLength = gFalse;
200 }
201 if (flags[i] & splashPathLast) {
202 if (zeroLength) {
203 return gTrue;
204 }
205 }
206 }
207 }
208 return gFalse;
209 }