Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 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.Path2D;
26 import java.awt.geom.Point2D;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32
33 import com.github.weisj.jsvg.attributes.text.TextAnchor;
34 import com.github.weisj.jsvg.geometry.size.Length;
35 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
36 import com.github.weisj.jsvg.renderer.RenderContext;
37 import com.github.weisj.jsvg.renderer.output.Output;
38 import com.github.weisj.jsvg.renderer.output.TextOutput;
39
40 final class LinearTextLayoutGroup implements TextLayoutGroup {
41 private final LinearTextContainer<?> parent;
42 private final @NotNull List<@NotNull TextSegment> segments;
43 private final TextSegment.RenderableSegment asSegment;
44
45 LinearTextLayoutGroup(@NotNull LinearTextContainer<?> parent) {
46 this(parent, new ArrayList<>());
47 }
48
49 LinearTextLayoutGroup(@NotNull LinearTextContainer<?> parent, @NotNull List<@NotNull TextSegment> segments) {
50 this.parent = parent;
51 this.segments = segments;
52 this.asSegment = createSegment(parent, this);
53 }
54
55 private static <E> TextSegment.RenderableSegment createSegment(
56 @NotNull LinearTextContainer<E> parent, @NotNull TextLayoutGroup group) {
57 return new LayoutGroupSegment<>(parent, group);
58 }
59
60 public @NotNull TextSegment.RenderableSegment asSegment() {
61 return asSegment;
62 }
63
64 @Override
65 public @NotNull List<TextSegment> segments() {
66 return segments;
67 }
68
69 @Override
70 public @Nullable Length fixedLength() {
71 if (parent.textLength.isSpecified()) return parent.textLength;
72 return null;
73 }
74
75 private @NotNull GlyphCursor createCursor(@Nullable Point2D start) {
76 GlyphCursor cursor = parent.createLocalCursor(start == null, new GlyphCursor(0, 0, new AffineTransform()));
77 if (start != null) {
78 cursor.x = (float) start.getX();
79 cursor.y = (float) start.getY();
80 }
81 return cursor;
82 }
83
84 @Override
85 public @NotNull Point2D renderText(@Nullable Point2D start, @NotNull RenderContext context,
86 @NotNull Output output) {
87 GlyphCursor cursor = createCursor(start);
88 TextOutput textOutput = output.textOutput();
89 textOutput.beginText();
90 asSegment().prepareSegmentForRendering(cursor, context, textOutput);
91
92 // Disable text anchor if there is an intermediate <textPath>
93 double offset = start == null
94 ? textAnchorOffset(parent.textAnchor(context), cursor.completeGlyphRunMetrics)
95 : 0;
96 context.translate(output, -offset, 0);
97 asSegment().renderSegmentWithoutLayout(cursor, context, output);
98 context.translate(output, offset, 0);
99
100 textOutput.endText();
101 return cursor.currentLocation(context.measureContext());
102 }
103
104 @Override
105 public @NotNull Point2D appendGlyphShape(@Nullable Point2D start, @NotNull RenderContext context,
106 @NotNull Path2D shape) {
107 MutableGlyphRun glyphRun = new MutableGlyphRun();
108 GlyphCursor cursor = createCursor(start);
109 asSegment().appendTextShape(cursor, glyphRun, context);
110 double offset = textAnchorOffset(parent.textAnchor(context), glyphRun.metrics());
111 if (GeometryUtil.approximatelyEqual(offset, 0)) {
112 shape.append(glyphRun.shape(), false);
113 } else {
114 shape.append(glyphRun.shape().createTransformedShape(AffineTransform.getTranslateInstance(-offset, 0)),
115 false);
116 }
117 return cursor.currentLocation(context.measureContext());
118 }
119
120 private double textAnchorOffset(@NotNull TextAnchor textAnchor, @NotNull AbstractGlyphRun.Metrics metrics) {
121 switch (textAnchor) {
122 case Start:
123 return 0;
124 case Middle:
125 return metrics.layoutBounds.getWidth() / 2f;
126 case End:
127 return metrics.layoutBounds.getWidth();
128 default:
129 throw new IllegalStateException("Unexpected value: " + textAnchor);
130 }
131 }
132
133 }