Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/LayoutGroupSegment.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.util.List;
25 import java.util.function.BiConsumer;
26
27 import org.jetbrains.annotations.NotNull;
28
29 import com.github.weisj.jsvg.attributes.font.SVGFont;
30 import com.github.weisj.jsvg.geometry.size.Length;
31 import com.github.weisj.jsvg.nodes.prototype.Renderable;
32 import com.github.weisj.jsvg.renderer.RenderContext;
33 import com.github.weisj.jsvg.renderer.impl.NodeRenderer;
34 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
35 import com.github.weisj.jsvg.renderer.output.Output;
36 import com.github.weisj.jsvg.renderer.output.TextOutput;
37
38 class LayoutGroupSegment<E, T extends TextContainer<E> & CursorContext> implements TextSegment.RenderableSegment {
39 private final @NotNull T parent;
40 private final @NotNull TextLayoutGroup group;
41
42 LayoutGroupSegment(@NotNull T parent, @NotNull TextLayoutGroup group) {
43 this.parent = parent;
44 this.group = group;
45 }
46
47 @Override
48 public boolean isSegmentVisible(@NotNull RenderContext currentContext) {
49 return parent.isVisible(currentContext);
50 }
51
52 @Override
53 public void prepareSegmentForRendering(@NotNull GlyphCursor cursor, @NotNull RenderContext context,
54 @NotNull TextOutput textOutput) {
55 SVGFont font = RenderContextAccessor.instance().font(context);
56
57 GlyphCursor localCursor = parent.createLocalCursor(false, cursor);
58 localCursor.setAdvancement(localGlyphAdvancement(context, cursor));
59
60 forEachSegment(
61 group.segments(),
62 context,
63 (seg, ctx) -> GlyphRenderer.prepareGlyphRun(seg, localCursor, font, ctx, textOutput),
64 (seg, ctx) -> seg.prepareSegmentForRendering(localCursor, ctx, textOutput));
65
66 parent.cleanUpLocalCursor(cursor, localCursor);
67 }
68
69 private @NotNull GlyphAdvancement localGlyphAdvancement(@NotNull RenderContext context,
70 @NotNull GlyphCursor cursor) {
71 Length length = group.fixedLength();
72 if (length != null) {
73 return new GlyphAdvancement(
74 computeTextMetrics(context, UseTextLengthForCalculation.NO),
75 length.resolve(context.measureContext()), parent.lengthAdjust);
76 }
77 return cursor.advancement();
78 }
79
80 @Override
81 public void renderSegmentWithoutLayout(@NotNull GlyphCursor cursor, @NotNull RenderContext context,
82 @NotNull Output output) {
83 forEachSegment(
84 group.segments(),
85 context,
86 (seg, ctx) -> {
87 if ((parent instanceof Renderable) && !((Renderable) parent).isVisible(ctx)) {
88 return;
89 }
90 GlyphRenderer.renderGlyphRun(
91 output, RenderContextAccessor.instance().paintOrder(context),
92 parent.vectorEffects(), seg);
93 },
94 (seg, ctx) -> seg.renderSegmentWithoutLayout(cursor, ctx, output));
95 }
96
97 @Override
98 public @NotNull TextMetrics computeTextMetrics(@NotNull RenderContext context,
99 @NotNull UseTextLengthForCalculation flag) {
100 return TextMetrics.computeTextMetrics(group, context, flag);
101 }
102
103 @Override
104 public void appendTextShape(@NotNull GlyphCursor cursor, @NotNull MutableGlyphRun glyphRun,
105 @NotNull RenderContext context) {
106 SVGFont font = RenderContextAccessor.instance().font(context);
107
108 GlyphCursor localCursor = parent.createLocalCursor(false, cursor);
109 localCursor.setAdvancement(localGlyphAdvancement(context, cursor));
110
111 forEachSegment(
112 group.segments(),
113 context,
114 (seg, ctx) -> glyphRun.append(
115 GlyphRenderer.layoutGlyphRun(seg, localCursor, font, ctx, NullTextOutput.INSTANCE)),
116 (seg, ctx) -> seg.appendTextShape(localCursor, glyphRun, ctx));
117
118 parent.cleanUpLocalCursor(cursor, localCursor);
119 }
120
121 static void forEachSegment(@NotNull List<? extends @NotNull TextSegment> segments,
122 @NotNull RenderContext context,
123 @NotNull BiConsumer<StringTextSegment, RenderContext> onStringTextSegment,
124 @NotNull BiConsumer<TextSegment.RenderableSegment, RenderContext> onRenderableSegment) {
125 for (TextSegment segment : segments) {
126 RenderContext currentContext = context;
127 if (segment instanceof TextContainer) {
128 currentContext = NodeRenderer.setupRenderContext(segment, context);
129 }
130 if (segment instanceof StringTextSegment) {
131 onStringTextSegment.accept((StringTextSegment) segment, currentContext);
132 } else if (segment instanceof TextSegment.RenderableSegment) {
133 onRenderableSegment.accept((TextSegment.RenderableSegment) segment, currentContext);
134 } else {
135 throw new IllegalStateException("Unexpected segment " + segment);
136 }
137 }
138 }
139 }