| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
} |