Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 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.Point2D;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.geometry.size.Length;
31 import com.github.weisj.jsvg.renderer.MeasureContext;
32
33 class GlyphCursor {
34
35 public final @NotNull AbstractGlyphRun.Metrics completeGlyphRunMetrics;
36 protected @NotNull GlyphAdvancement advancement = GlyphAdvancement.defaultAdvancement();
37
38 protected float x;
39 protected float y;
40 protected int glyphOffset;
41 protected final AffineTransform transform;
42
43 protected Length[] xLocations;
44 protected int xOff;
45
46 protected Length[] xDeltas;
47 protected int dxOff;
48
49 protected Length[] yLocations;
50 protected int yOff;
51
52 protected Length[] yDeltas;
53 protected int dyOff;
54
55 protected float[] rotations;
56 protected int rotOff;
57
58 GlyphCursor(float x, float y, @NotNull AffineTransform transform) {
59 this(x, y, transform, AbstractGlyphRun.Metrics.createDefault());
60 }
61
62 private GlyphCursor(float x, float y, @NotNull AffineTransform transform,
63 @NotNull AbstractGlyphRun.Metrics metrics) {
64 this.x = x;
65 this.y = y;
66 this.transform = transform;
67 this.completeGlyphRunMetrics = metrics;
68 this.glyphOffset = 0;
69 }
70
71 GlyphCursor(@NotNull GlyphCursor c) {
72 this(c.x, c.y, c.transform, c.completeGlyphRunMetrics);
73 this.advancement = c.advancement;
74 this.glyphOffset = 0;
75 this.xLocations = c.xLocations;
76 this.xOff = c.xOff;
77 this.yLocations = c.yLocations;
78 this.yOff = c.yOff;
79 this.xDeltas = c.xDeltas;
80 this.dxOff = c.dxOff;
81 this.yDeltas = c.yDeltas;
82 this.dyOff = c.dyOff;
83 this.rotations = c.rotations;
84 this.rotOff = c.rotOff;
85 }
86
87 GlyphCursor derive() {
88 return new GlyphCursor(this);
89 }
90
91 void updateFrom(GlyphCursor local) {
92 x = local.x;
93 y = local.y;
94 }
95
96 public void setAdvancement(@NotNull GlyphAdvancement advancement) {
97 this.advancement = advancement;
98 }
99
100 public GlyphAdvancement advancement() {
101 return advancement;
102 }
103
104 @NotNull
105 Point2D.Float currentLocation(@NotNull MeasureContext measure) {
106 return new Point2D.Float(
107 currentX(measure) + currentXDelta(measure),
108 currentY(measure) + currentYDelta(measure));
109 }
110
111 /*
112 * Return a null value indicates that the iteration should stop.
113 */
114 @Nullable
115 AffineTransform advance(@NotNull MeasureContext measure, @NotNull Glyph glyph) {
116 x = nextX(measure);
117 x += nextDeltaX(measure);
118
119 y = nextY(measure);
120 y += nextDeltaY(measure);
121
122 transform.setToTranslation(x, y);
123
124 double rotation = nextRotation();
125 if (rotation != 0) {
126 transform.rotate(rotation);
127 }
128
129 glyphOffset++;
130
131 // Todo: Also handle non-horizontal and bidi text
132 // This assumes a horizontal baseline
133 x += advancement.glyphAdvancement(glyph);
134
135 return advancement.glyphTransform(transform);
136 }
137
138 void advanceSpacing(float letterSpacing) {
139 x += advancement.spacingAdvancement(letterSpacing);
140 }
141
142 private boolean currentGlyphHasXOverride() {
143 return xLocations != null && xOff < xLocations.length;
144 }
145
146 private float currentX(@NotNull MeasureContext measure) {
147 if (currentGlyphHasXOverride()) {
148 return xLocations[xOff].resolve(measure);
149 }
150 return x;
151 }
152
153 private float currentXDelta(@NotNull MeasureContext measure) {
154 if (currentGlyphHasDeltaXOverride()) {
155 return xDeltas[dxOff].resolve(measure);
156 }
157 return 0;
158 }
159
160 private float currentY(@NotNull MeasureContext measure) {
161 if (currentGlyphHasYOverride()) {
162 return yLocations[yOff].resolve(measure);
163 }
164 return y;
165 }
166
167 private float currentYDelta(@NotNull MeasureContext measure) {
168 if (currentGlyphHasDeltaYOverride()) {
169 return yDeltas[dyOff].resolve(measure);
170 }
171 return 0;
172 }
173
174 private float nextX(@NotNull MeasureContext measure) {
175 if (currentGlyphHasXOverride()) {
176 x = xLocations[xOff].resolve(measure);
177 xOff++;
178 }
179 return x;
180 }
181
182 private boolean currentGlyphHasDeltaXOverride() {
183 return xDeltas != null && dxOff < xDeltas.length;
184 }
185
186 protected final float nextDeltaX(@NotNull MeasureContext measure) {
187 if (currentGlyphHasDeltaXOverride()) {
188 return xDeltas[dxOff++].resolve(measure);
189 }
190 return 0;
191 }
192
193 private boolean currentGlyphHasYOverride() {
194 return yLocations != null && yOff < yLocations.length;
195 }
196
197 private float nextY(@NotNull MeasureContext measure) {
198 if (currentGlyphHasYOverride()) {
199 y = yLocations[yOff].resolve(measure);
200 yOff++;
201 }
202 return y;
203 }
204
205 private boolean currentGlyphHasDeltaYOverride() {
206 return yDeltas != null && dyOff < yDeltas.length;
207 }
208
209 protected final float nextDeltaY(@NotNull MeasureContext measure) {
210 if (currentGlyphHasDeltaYOverride()) {
211 return yDeltas[dyOff++].resolve(measure);
212 }
213 return 0;
214 }
215
216 private boolean currentGlyphHasRotationOverride() {
217 return rotations != null && rotations.length != 0;
218 }
219
220 private double nextRotation() {
221 if (currentGlyphHasRotationOverride()) {
222 float rotation = rotations[rotOff];
223 rotOff = Math.min(rotations.length - 1, rotOff + 1);
224 return Math.toRadians(rotation);
225 }
226 return 0;
227 }
228
229 boolean isCurrentGlyphAutoLayout() {
230 return !currentGlyphHasXOverride()
231 && !currentGlyphHasDeltaXOverride()
232 && !currentGlyphHasYOverride()
233 && !currentGlyphHasDeltaYOverride()
234 && !(currentGlyphHasRotationOverride() && rotations[rotOff] % 360 != 0);
235 }
236
237 boolean shouldRenderCurrentGlyph() {
238 return true;
239 }
240 }