| •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.nodes.text; |
| 23 |
|
| 24 |
import java.awt.geom.AffineTransform; |
| 25 |
import java.awt.geom.PathIterator; |
| 26 |
import java.awt.geom.Point2D; |
| 27 |
|
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
import org.jetbrains.annotations.Nullable; |
| 30 |
|
| 31 |
import com.github.weisj.jsvg.geometry.util.GeometryUtil; |
| 32 |
import com.github.weisj.jsvg.geometry.util.SegmentIteratorWithLookBehind; |
| 33 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 34 |
|
| 35 |
final class PathGlyphCursor extends GlyphCursor { |
| 36 |
|
| 37 |
private float remainingSegmentLength; |
| 38 |
private float segmentLength; |
| 39 |
private float retainedLengthAtStart; |
| 40 |
private boolean shouldRenderCurrentGlyph; |
| 41 |
private SegmentIteratorWithLookBehind.Segment currentSegment; |
| 42 |
|
| 43 |
private final @NotNull SegmentIteratorWithLookBehind segmentIterator; |
| 44 |
|
| 45 |
PathGlyphCursor(@NotNull PathIterator pathIterator, float startOffset) { |
| 46 |
super(0, 0, new AffineTransform()); |
| 47 |
this.segmentIterator = new SegmentIteratorWithLookBehind(pathIterator, 0); |
| 48 |
setupInitialData(); |
| 49 |
advance(startOffset); |
| 50 |
} |
| 51 |
|
| 52 |
PathGlyphCursor(@NotNull GlyphCursor cursor, @NotNull PathIterator pathIterator, float startOffset) { |
| 53 |
super(cursor); |
| 54 |
this.segmentIterator = new SegmentIteratorWithLookBehind(pathIterator, 0); |
| 55 |
setupInitialData(); |
| 56 |
advance(startOffset); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
@NotNull |
| 61 |
PathIterator pathIterator() { |
| 62 |
return segmentIterator.pathIterator(); |
| 63 |
} |
| 64 |
|
| 65 |
private void setupInitialData() { |
| 66 |
this.currentSegment = segmentIterator.currentSegment(); |
| 67 |
this.segmentLength = this.remainingSegmentLength = (float) currentSegment.length(); |
| 68 |
this.x = currentSegment.xStart; |
| 69 |
this.y = currentSegment.yStart; |
| 70 |
} |
| 71 |
|
| 72 |
private PathGlyphCursor(@NotNull PathGlyphCursor pathCursor) { |
| 73 |
super(pathCursor); |
| 74 |
|
| 75 |
|
| 76 |
this.segmentIterator = pathCursor.segmentIterator; |
| 77 |
this.remainingSegmentLength = pathCursor.remainingSegmentLength; |
| 78 |
this.segmentLength = pathCursor.segmentLength; |
| 79 |
this.currentSegment = pathCursor.currentSegment; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
GlyphCursor derive() { |
| 84 |
return new PathGlyphCursor(this); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
void updateFrom(GlyphCursor local) { |
| 89 |
super.updateFrom(local); |
| 90 |
assert local instanceof PathGlyphCursor; |
| 91 |
PathGlyphCursor glyphCursor = (PathGlyphCursor) local; |
| 92 |
remainingSegmentLength = glyphCursor.remainingSegmentLength; |
| 93 |
segmentLength = glyphCursor.segmentLength; |
| 94 |
currentSegment = glyphCursor.currentSegment; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void setAdvancement(@NotNull GlyphAdvancement advancement) { |
| 99 |
super.setAdvancement(advancement); |
| 100 |
|
| 101 |
|
| 102 |
segmentIterator.setMaxLookBehindLength( |
| 103 |
Math.max(advancement.maxLookBehind(), segmentIterator.maxLookBehindLength())); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
@NotNull |
| 108 |
Point2D.Float currentLocation(@NotNull MeasureContext measure) { |
| 109 |
|
| 110 |
return new Point2D.Float(x, y); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
@Nullable |
| 115 |
AffineTransform advance(@NotNull MeasureContext measure, @NotNull Glyph glyph) { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
if (segmentIterator.isDone() && GeometryUtil.approximatelyNegative(remainingSegmentLength)) return null; |
| 120 |
|
| 121 |
float deltaX = nextDeltaX(measure); |
| 122 |
if (deltaX != 0) advance(deltaX); |
| 123 |
|
| 124 |
|
| 125 |
float advanceDist = advancement.glyphAdvancement(glyph); |
| 126 |
float halfAdvance = advanceDist / 2f; |
| 127 |
|
| 128 |
advance(halfAdvance); |
| 129 |
float walkedFraction = halfAdvance / segmentLength; |
| 130 |
float slopeX = walkedFraction * (currentSegment.xEnd - currentSegment.xStart); |
| 131 |
float slopeY = walkedFraction * (currentSegment.yEnd - currentSegment.yStart); |
| 132 |
float anchorX = x - slopeX; |
| 133 |
float anchorY = y - slopeY; |
| 134 |
|
| 135 |
|
| 136 |
shouldRenderCurrentGlyph = GeometryUtil.approximatelyNegative(retainedLengthAtStart); |
| 137 |
if (segmentIterator.isDone() && GeometryUtil.approximatelyNegative(remainingSegmentLength)) return null; |
| 138 |
advance(halfAdvance); |
| 139 |
|
| 140 |
transform.setToTranslation(anchorX, anchorY); |
| 141 |
float charRotation = calculateSegmentRotation(anchorX, anchorY, x + slopeX, y + slopeY); |
| 142 |
transform.rotate(charRotation, 0, 0); |
| 143 |
|
| 144 |
float deltaY = nextDeltaY(measure); |
| 145 |
if (deltaY != 0) { |
| 146 |
|
| 147 |
float nx = -(y - anchorX); |
| 148 |
float ny = (x - anchorY); |
| 149 |
float nn = deltaY / norm(nx, ny); |
| 150 |
transform.translate(nx * nn, ny * nn); |
| 151 |
} |
| 152 |
|
| 153 |
return advancement.glyphTransform(transform); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
void advanceSpacing(float letterSpacing) { |
| 158 |
advance(advancement.spacingAdvancement(letterSpacing)); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
boolean shouldRenderCurrentGlyph() { |
| 163 |
return shouldRenderCurrentGlyph; |
| 164 |
} |
| 165 |
|
| 166 |
private void advance(float distance) { |
| 167 |
if (distance >= 0) { |
| 168 |
advanceInsideSegment(advanceIntoSegment(adjustForRetainedLength(distance))); |
| 169 |
} else { |
| 170 |
advanceInsideSegment(-reverseIntoSegment(-distance)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
private float travelledSegmentLength() { |
| 175 |
return segmentLength - remainingSegmentLength; |
| 176 |
} |
| 177 |
|
| 178 |
private float adjustForRetainedLength(float distance) { |
| 179 |
if (distance > 0 && GeometryUtil.approximatelyPositive(retainedLengthAtStart)) { |
| 180 |
float delta = Math.min(distance, retainedLengthAtStart); |
| 181 |
retainedLengthAtStart -= delta; |
| 182 |
return distance - delta; |
| 183 |
} |
| 184 |
return distance; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
private float advanceIntoSegment(float distance) { |
| 189 |
if (GeometryUtil.approximatelyNegative(distance)) return 0; |
| 190 |
while (segmentIterator.hasNext() && remainingSegmentLength < distance) { |
| 191 |
distance -= remainingSegmentLength; |
| 192 |
segmentIterator.moveToNext(); |
| 193 |
currentSegment = segmentIterator.currentSegment(); |
| 194 |
x = currentSegment.xStart; |
| 195 |
y = currentSegment.yStart; |
| 196 |
segmentLength = (float) currentSegment.length(); |
| 197 |
remainingSegmentLength = segmentLength; |
| 198 |
} |
| 199 |
return distance; |
| 200 |
} |
| 201 |
|
| 202 |
private float reverseIntoSegment(float distance) { |
| 203 |
if (GeometryUtil.approximatelyNegative(distance)) return 0; |
| 204 |
while (segmentIterator.hasPrevious() && travelledSegmentLength() < distance) { |
| 205 |
distance -= travelledSegmentLength(); |
| 206 |
segmentIterator.moveToPrevious(); |
| 207 |
currentSegment = segmentIterator.currentSegment(); |
| 208 |
x = currentSegment.xEnd; |
| 209 |
y = currentSegment.yEnd; |
| 210 |
segmentLength = (float) currentSegment.length(); |
| 211 |
remainingSegmentLength = 0; |
| 212 |
} |
| 213 |
return distance; |
| 214 |
} |
| 215 |
|
| 216 |
private void advanceInsideSegment(float distance) { |
| 217 |
if (GeometryUtil.approximatelyZero(distance)) return; |
| 218 |
if (distance < 0 && -distance > travelledSegmentLength()) { |
| 219 |
|
| 220 |
retainedLengthAtStart += -1 * distance; |
| 221 |
return; |
| 222 |
} |
| 223 |
float fractionWalked = distance / segmentLength; |
| 224 |
x += (currentSegment.xEnd - currentSegment.xStart) * fractionWalked; |
| 225 |
y += (currentSegment.yEnd - currentSegment.yStart) * fractionWalked; |
| 226 |
remainingSegmentLength -= distance; |
| 227 |
} |
| 228 |
|
| 229 |
private float calculateSegmentRotation(float x1, float y1, float x2, float y2) { |
| 230 |
return (float) Math.atan2(y2 - y1, x2 - x1); |
| 231 |
} |
| 232 |
|
| 233 |
private float norm(float a, float b) { |
| 234 |
return (float) Math.sqrt(a * a + b * b); |
| 235 |
} |
| 236 |
} |