Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/PathGlyphCursor.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-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.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 // Note: Only use when cursor is not used anymore.
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 // We only ever transition one into a PathGlyphCursor (from a linear one)
75 // hence we can share all our state without the problem of overwriting it.
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 // We don't want to reduce the lookbehind for future segments hence coercion to the bigger value is
101 // needed.
102 segmentIterator.setMaxLookBehindLength(
103 Math.max(advancement.maxLookBehind(), segmentIterator.maxLookBehindLength()));
104 }
105
106 @Override
107 @NotNull
108 Point2D.Float currentLocation(@NotNull MeasureContext measure) {
109 // Note: This will be valid before advance is called due to setupInitialData.
110 return new Point2D.Float(x, y);
111 }
112
113 @Override
114 @Nullable
115 AffineTransform advance(@NotNull MeasureContext measure, @NotNull Glyph glyph) {
116 // Todo: Absolute x positions require arbitrary moves along the path
117 // dx can be done by using the look behind iterator.
118 // Absolute x can use a look up table for the segment/state.
119 if (segmentIterator.isDone() && GeometryUtil.approximatelyNegative(remainingSegmentLength)) return null;
120
121 float deltaX = nextDeltaX(measure);
122 if (deltaX != 0) advance(deltaX);
123
124 // Move the advance of the glyph
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 // The glyph midpoint is outside the path and should not be made visible. Abort
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 // Adjust the location along the paths normal vector.
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 // We cannot move the distance backwards. Retain the given amount of text from being displayed.
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 }