1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.util.*;
29
30 /**
31 * The {@code FlatteningPathIterator} class returns a flattened view of
32 * another {@link PathIterator} object. Other {@link java.awt.Shape Shape}
33 * classes can use this class to provide flattening behavior for their paths
34 * without having to perform the interpolation calculations themselves.
35 *
36 * @author Jim Graham
37 */
38 public class FlatteningPathIterator implements PathIterator {
39 static final int GROW_SIZE = 24; // Multiple of cubic & quad curve size
40
41 PathIterator src; // The source iterator
42
43 double squareflat; // Square of the flatness parameter
44 // for testing against squared lengths
45
46 int limit; // Maximum number of recursion levels
47
48 double hold[] = new double[14]; // The cache of interpolated coords
49 // Note that this must be long enough
50 // to store a full cubic segment and
51 // a relative cubic segment to avoid
52 // aliasing when copying the coords
53 // of a curve to the end of the array.
54 // This is also serendipitously equal
55 // to the size of a full quad segment
56 // and 2 relative quad segments.
57
58 double curx, cury; // The ending x,y of the last segment
59
60 double movx, movy; // The x,y of the last move segment
61
62 int holdType; // The type of the curve being held
63 // for interpolation
64
65 int holdEnd; // The index of the last curve segment
66 // being held for interpolation
67
68 int holdIndex; // The index of the curve segment
69 // that was last interpolated. This
70 // is the curve segment ready to be
71 // returned in the next call to
72 // currentSegment().
73
74 int levels[]; // The recursion level at which
75 // each curve being held in storage
76 // was generated.
77
78 int levelIndex; // The index of the entry in the
79 // levels array of the curve segment
80 // at the holdIndex
81
82 boolean done; // True when iteration is done
83
84 /**
85 * Constructs a new {@code FlatteningPathIterator} object that
86 * flattens a path as it iterates over it. The iterator does not
87 * subdivide any curve read from the source iterator to more than
88 * 10 levels of subdivision which yields a maximum of 1024 line
89 * segments per curve.
90 * @param src the original unflattened path being iterated over
91 * @param flatness the maximum allowable distance between the
92 * control points and the flattened curve
93 */
94 public FlatteningPathIterator(PathIterator src, double flatness) {
158 return src.getWindingRule();
159 }
160
161 /**
162 * Tests if the iteration is complete.
163 * @return {@code true} if all the segments have
164 * been read; {@code false} otherwise.
165 */
166 public boolean isDone() {
167 return done;
168 }
169
170 /*
171 * Ensures that the hold array can hold up to (want) more values.
172 * It is currently holding (hold.length - holdIndex) values.
173 */
174 void ensureHoldCapacity(int want) {
175 if (holdIndex - want < 0) {
176 int have = hold.length - holdIndex;
177 int newsize = hold.length + GROW_SIZE;
178 double newhold[] = new double[newsize];
179 System.arraycopy(hold, holdIndex,
180 newhold, holdIndex + GROW_SIZE,
181 have);
182 hold = newhold;
183 holdIndex += GROW_SIZE;
184 holdEnd += GROW_SIZE;
185 }
186 }
187
188 /**
189 * Moves the iterator to the next segment of the path forwards
190 * along the primary direction of traversal as long as there are
191 * more points in that direction.
192 */
193 public void next() {
194 next(true);
195 }
196
197 private void next(boolean doNext) {
198 int level;
|
1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.util.*;
29
30 /**
31 * The {@code FlatteningPathIterator} class returns a flattened view of
32 * another {@link PathIterator} object. Other {@link java.awt.Shape Shape}
33 * classes can use this class to provide flattening behavior for their paths
34 * without having to perform the interpolation calculations themselves.
35 *
36 * @author Jim Graham
37 */
38 public class FlatteningPathIterator implements PathIterator {
39 static final int GROW_SIZE = 24; // Multiple of cubic & quad curve size
40
41 PathIterator src; // The source iterator
42
43 double squareflat; // Square of the flatness parameter
44 // for testing against squared lengths
45
46 int limit; // Maximum number of recursion levels
47
48 double[] hold = new double[14]; // The cache of interpolated coords
49 // Note that this must be long enough
50 // to store a full cubic segment and
51 // a relative cubic segment to avoid
52 // aliasing when copying the coords
53 // of a curve to the end of the array.
54 // This is also serendipitously equal
55 // to the size of a full quad segment
56 // and 2 relative quad segments.
57
58 double curx, cury; // The ending x,y of the last segment
59
60 double movx, movy; // The x,y of the last move segment
61
62 int holdType; // The type of the curve being held
63 // for interpolation
64
65 int holdEnd; // The index of the last curve segment
66 // being held for interpolation
67
68 int holdIndex; // The index of the curve segment
69 // that was last interpolated. This
70 // is the curve segment ready to be
71 // returned in the next call to
72 // currentSegment().
73
74 int[] levels; // The recursion level at which
75 // each curve being held in storage
76 // was generated.
77
78 int levelIndex; // The index of the entry in the
79 // levels array of the curve segment
80 // at the holdIndex
81
82 boolean done; // True when iteration is done
83
84 /**
85 * Constructs a new {@code FlatteningPathIterator} object that
86 * flattens a path as it iterates over it. The iterator does not
87 * subdivide any curve read from the source iterator to more than
88 * 10 levels of subdivision which yields a maximum of 1024 line
89 * segments per curve.
90 * @param src the original unflattened path being iterated over
91 * @param flatness the maximum allowable distance between the
92 * control points and the flattened curve
93 */
94 public FlatteningPathIterator(PathIterator src, double flatness) {
158 return src.getWindingRule();
159 }
160
161 /**
162 * Tests if the iteration is complete.
163 * @return {@code true} if all the segments have
164 * been read; {@code false} otherwise.
165 */
166 public boolean isDone() {
167 return done;
168 }
169
170 /*
171 * Ensures that the hold array can hold up to (want) more values.
172 * It is currently holding (hold.length - holdIndex) values.
173 */
174 void ensureHoldCapacity(int want) {
175 if (holdIndex - want < 0) {
176 int have = hold.length - holdIndex;
177 int newsize = hold.length + GROW_SIZE;
178 double[] newhold = new double[newsize];
179 System.arraycopy(hold, holdIndex,
180 newhold, holdIndex + GROW_SIZE,
181 have);
182 hold = newhold;
183 holdIndex += GROW_SIZE;
184 holdEnd += GROW_SIZE;
185 }
186 }
187
188 /**
189 * Moves the iterator to the next segment of the path forwards
190 * along the primary direction of traversal as long as there are
191 * more points in that direction.
192 */
193 public void next() {
194 next(true);
195 }
196
197 private void next(boolean doNext) {
198 int level;
|