Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022-2026 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.PathIterator;
25 import java.util.ArrayList;
26
27 import org.jetbrains.annotations.NotNull;
28
29 import com.github.weisj.jsvg.geometry.size.Length;
30
31 public final class SegmentIteratorWithLookBehind {
32
33 private final @NotNull PathIterator pathIterator;
34 private float maxLookBehindLength;
35 private float currentLookBehindLength = 0;
36
37 private final ArrayList<Segment> lookBehind = new ArrayList<>();
38 private Segment currentSegment;
39
40 private final float[] cords = new float[2];
41 private float moveToX;
42 private float moveToY;
43
44 private int lookBehindCursor = -1;
45
46
47 public SegmentIteratorWithLookBehind(@NotNull PathIterator pathIterator, float maxLookBehindLength) {
48 this.pathIterator = pathIterator;
49 this.maxLookBehindLength = maxLookBehindLength;
50 prepareFirstSegment();
51 }
52
53 private void prepareFirstSegment() {
54 this.currentSegment = new Segment(
55 Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW,
56 Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW);
57 moveToNext();
58 if (Length.isUnspecified(currentSegment.xStart) || Length.isUnspecified(currentSegment.yStart)) {
59 throw new IllegalStateException("Path iterator did not establish starting position");
60 }
61 }
62
63 /*
64 * Do not modify while the iterator is still used.
65 */
66 public @NotNull PathIterator pathIterator() {
67 return pathIterator;
68 }
69
70 public void setMaxLookBehindLength(float maxLookBehindLength) {
71 this.maxLookBehindLength = maxLookBehindLength;
72 trimLookBehindIfNecessary();
73 }
74
75 public float maxLookBehindLength() {
76 return maxLookBehindLength;
77 }
78
79 public boolean hasNext() {
80 return lookBehindCursor >= 0 || !pathIterator.isDone();
81 }
82
83 public boolean isDone() {
84 return !hasNext();
85 }
86
87 public boolean hasPrevious() {
88 return lookBehindCursor < lookBehind.size() - 1;
89 }
90
91 public @NotNull Segment currentSegment() {
92 if (lookBehindCursor >= 0) {
93 return lookBehind.get(lookBehind.size() - 1 - lookBehindCursor);
94 } else {
95 return currentSegment;
96 }
97 }
98
99 public void moveToPrevious() {
100 if (!hasPrevious()) {
101 throw new IllegalStateException("Can't move back anymore. Maximum capacity is " + maxLookBehindLength);
102 }
103 lookBehindCursor++;
104 }
105
106 public void moveToNext() {
107 if (lookBehindCursor >= 0) {
108 lookBehindCursor--;
109 return;
110 }
111 Segment nextSegment = new Segment(
112 currentSegment.xEnd, currentSegment.yEnd,
113 Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW);
114
115 outer: while (!pathIterator.isDone()) {
116 switch (pathIterator.currentSegment(cords)) {
117 case PathIterator.SEG_MOVETO:
118 nextSegment.xStart = moveToX = cords[0];
119 nextSegment.yStart = moveToY = cords[1];
120 nextSegment.moveHappened = true;
121 pathIterator.next();
122 break;
123 case PathIterator.SEG_CLOSE:
124 nextSegment.xEnd = moveToX;
125 nextSegment.yEnd = moveToY;
126 pathIterator.next();
127 break outer;
128 case PathIterator.SEG_LINETO:
129 nextSegment.xEnd = cords[0];
130 nextSegment.yEnd = cords[1];
131 pathIterator.next();
132 break outer;
133 default:
134 throw new IllegalStateException("Unsupported segment type");
135 }
136 }
137 if (Float.isNaN(nextSegment.xEnd) || Float.isNaN(nextSegment.yEnd)) {
138 nextSegment.xEnd = nextSegment.xStart;
139 nextSegment.yEnd = nextSegment.yStart;
140 }
141
142 if (maxLookBehindLength > 0) {
143 lookBehind.add(currentSegment);
144 currentLookBehindLength += (float) currentSegment.length();
145
146 trimLookBehindIfNecessary();
147 }
148 currentSegment = nextSegment;
149 }
150
151 private void trimLookBehindIfNecessary() {
152 while (GeometryUtil.notablyGreater(currentLookBehindLength, maxLookBehindLength)) {
153 Segment segment = lookBehind.get(0);
154 double segmentLength = segment.length();
155 if (this.currentLookBehindLength - segmentLength < maxLookBehindLength) break;
156 this.currentLookBehindLength -= (float) segmentLength;
157 lookBehind.remove(0);
158 }
159 }
160
161 public static final class Segment {
162 public float xStart;
163 public float yStart;
164 public float xEnd;
165 public float yEnd;
166
167 public boolean moveHappened;
168
169 private Segment(float xStart, float yStart, float xEnd, float yEnd) {
170 this.xStart = xStart;
171 this.yStart = yStart;
172 this.xEnd = xEnd;
173 this.yEnd = yEnd;
174 }
175
176 public double length() {
177 return GeometryUtil.lineLength(xStart, yStart, xEnd, yEnd);
178 }
179
180 @Override
181 public String toString() {
182 return String.format("[%.2f,%.2f] -> [%.2f,%.2f] (moved: %b)", xStart, yStart, xEnd, yEnd, moveHappened);
183 }
184 }
185 }