Debian Codegraph @

src/com/github/weisj/jsvg/geometry/util/ReversePathIterator.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2023 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.geometry.util;
23
24 import java.awt.geom.IllegalPathStateException;
25 import java.awt.geom.PathIterator;
26
27 /**
28 * A path iterator which iterates over a path in the reverse direction.
29 * This is missing in the java.awt.geom package, although it's quite simple to implement.
30 * After initialization the original PathIterator is not used any longer.
31 * <p>
32 *
33 * @author <a href="mailto:[email protected]">Rammi</a>
34 * @version $Revision: 1.3 $
35 */
36 public final class ReversePathIterator implements PathIterator {
37 /** The winding rule. */
38 private final int windingRule;
39 /** The reversed coordinates. */
40 private final double[] coordinates;
41 /** The reversed segment types. */
42 private final int[] segmentTypes;
43 /** The index into the coordinates during iteration. */
44 private int coordIndex = 0;
45 /** The index into the segment types during iteration. */
46 private int segmentIndex = 0;
47
48 /**
49 * Create an inverted path iterator from a standard one, keeping the winding rule.
50 * @param original original iterator
51 */
52 public ReversePathIterator(PathIterator original) {
53 this(original, original.getWindingRule());
54 }
55
56 /**
57 * Create an inverted path iterator from a standard one.
58 * @param original original iterator
59 * @param windingRule winding rule of newly created iterator
60 */
61 public ReversePathIterator(PathIterator original, int windingRule) {
62 this.windingRule = windingRule;
63
64 double[] coords = new double[16];
65 int coordPos = 0;
66 int[] segTypes = new int[8];
67 int segPos = 0;
68 boolean first = true;
69
70 double[] temp = new double[6];
71 while (!original.isDone()) {
72 if (segPos == segTypes.length) {
73 // resize
74 int[] dummy = new int[2 * segPos];
75 System.arraycopy(segTypes, 0, dummy, 0, segPos);
76 segTypes = dummy;
77 }
78 final int segType = segTypes[segPos++] = original.currentSegment(temp);
79 if (first) {
80 if (segType != SEG_MOVETO) {
81 throw new IllegalPathStateException("missing initial moveto in path definition");
82 }
83 first = false;
84 }
85 final int copy = coordinatesForSegmentType(segType);
86 if (copy > 0) {
87 if (coordPos + copy > coords.length) {
88 // resize
89 double[] dummy = new double[coords.length * 2];
90 System.arraycopy(coords, 0, dummy, 0, coords.length);
91 coords = dummy;
92 }
93 for (int c = 0; c < copy; ++c) {
94 coords[coordPos++] = temp[c];
95 }
96 }
97 original.next();
98 }
99
100 // === reverse everything ===
101 // --- reverse coordinates ---
102 coordinates = new double[coordPos];
103 for (int p = coordPos / 2 - 1; p >= 0; --p) {
104 coordinates[2 * p] = coords[coordPos - 2 * p - 2];
105 coordinates[2 * p + 1] = coords[coordPos - 2 * p - 1];
106 }
107
108 // --- reverse segment types ---
109 segmentTypes = new int[segPos];
110 if (segPos > 0) {
111 boolean pendingClose = false;
112 int sr = 0;
113 segmentTypes[sr++] = SEG_MOVETO;
114 for (int s = segPos - 1; s > 0; --s) {
115 switch (segTypes[s]) {
116 case SEG_MOVETO:
117 if (pendingClose) {
118 pendingClose = false;
119 segmentTypes[sr++] = SEG_CLOSE;
120 }
121 segmentTypes[sr++] = SEG_MOVETO;
122 break;
123 case SEG_CLOSE:
124 pendingClose = true;
125 break;
126 default:
127 segmentTypes[sr++] = segTypes[s];
128 break;
129 }
130 }
131 if (pendingClose) {
132 segmentTypes[sr] = SEG_CLOSE;
133 }
134 }
135 }
136
137 /**
138 * Returns the winding rule for determining the interior of the
139 * path.
140 *
141 * @return the winding rule.
142 * @see #WIND_EVEN_ODD
143 * @see #WIND_NON_ZERO
144 */
145 @Override
146 public int getWindingRule() {
147 return windingRule;
148 }
149
150 /**
151 * Get the number of coordinates needed for a segment type.
152 * @param segtype segment type
153 * @return coordinates needed
154 */
155 private static int coordinatesForSegmentType(int segtype) {
156 switch (segtype) {
157 case SEG_MOVETO:
158 case SEG_LINETO:
159 return 2;
160 case SEG_QUADTO:
161 return 4;
162 case SEG_CUBICTO:
163 return 6;
164 default:
165 return 0;
166 }
167 }
168
169 /**
170 * Moves the iterator to the next segment of the path forwards
171 * along the primary direction of traversal as long as there are
172 * more points in that direction.
173 */
174 @Override
175 public void next() {
176 coordIndex += coordinatesForSegmentType(segmentTypes[segmentIndex++]);
177 }
178
179 /**
180 * Tests if the iteration is complete.
181 *
182 * @return <code>true</code> if all the segments have
183 * been read; <code>false</code> otherwise.
184 */
185 @Override
186 public boolean isDone() {
187 return segmentIndex >= segmentTypes.length;
188 }
189
190 /**
191 * Returns the coordinates and type of the current path segment in
192 * the iteration.
193 * The return value is the path-segment type:
194 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
195 * A double array of length 6 must be passed in and can be used to
196 * store the coordinates of the point(s).
197 * Each point is stored as a pair of double x,y coordinates.
198 * SEG_MOVETO and SEG_LINETO types returns one point,
199 * SEG_QUADTO returns two points,
200 * SEG_CUBICTO returns 3 points
201 * and SEG_CLOSE does not return any points.
202 *
203 * @param coords an array that holds the data returned from
204 * this method
205 * @return the path-segment type of the current path segment.
206 * @see #SEG_MOVETO
207 * @see #SEG_LINETO
208 * @see #SEG_QUADTO
209 * @see #SEG_CUBICTO
210 * @see #SEG_CLOSE
211 */
212 @Override
213 public int currentSegment(double[] coords) {
214 final int segmentType = segmentTypes[segmentIndex];
215 final int copy = coordinatesForSegmentType(segmentType);
216 if (copy > 0) {
217 System.arraycopy(coordinates, coordIndex, coords, 0, copy);
218 }
219 return segmentType;
220 }
221
222 /**
223 * Returns the coordinates and type of the current path segment in
224 * the iteration.
225 * The return value is the path-segment type:
226 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
227 * A float array of length 6 must be passed in and can be used to
228 * store the coordinates of the point(s).
229 * Each point is stored as a pair of float x,y coordinates.
230 * SEG_MOVETO and SEG_LINETO types returns one point,
231 * SEG_QUADTO returns two points,
232 * SEG_CUBICTO returns 3 points
233 * and SEG_CLOSE does not return any points.
234 *
235 * @param coords an array that holds the data returned from
236 * this method
237 * @return the path-segment type of the current path segment.
238 * @see #SEG_MOVETO
239 * @see #SEG_LINETO
240 * @see #SEG_QUADTO
241 * @see #SEG_CUBICTO
242 * @see #SEG_CLOSE
243 */
244 @Override
245 public int currentSegment(float[] coords) {
246 final int segmentType = segmentTypes[segmentIndex];
247 final int copy = coordinatesForSegmentType(segmentType);
248 if (copy > 0) {
249 for (int c = 0; c < copy; ++c) {
250 coords[c] = (float) coordinates[coordIndex + c];
251 }
252 }
253 return segmentType;
254 }
255 }