Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/TextSpan.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.*;
25 import java.awt.geom.Path2D;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.nodes.Anchor;
31 import com.github.weisj.jsvg.nodes.SVGNode;
32 import com.github.weisj.jsvg.nodes.animation.Animate;
33 import com.github.weisj.jsvg.nodes.animation.AnimateTransform;
34 import com.github.weisj.jsvg.nodes.animation.Set;
35 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
36 import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories;
37 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
38 import com.github.weisj.jsvg.parser.TextContent;
39 import com.github.weisj.jsvg.renderer.RenderContext;
40 import com.github.weisj.jsvg.renderer.output.Output;
41 import com.github.weisj.jsvg.renderer.output.TextOutput;
42 import com.github.weisj.jsvg.util.AttributeUtil;
43
44 @ElementCategories({Category.TextContent, Category.TextContentChild})
45 @PermittedContent(
46 categories = Category.Descriptive,
47 anyOf = {Anchor.class, TextSpan.class, Animate.class, AnimateTransform.class, Set.class /* <altGlyph>, <tref> */ },
48 charData = true
49 )
50 public final class TextSpan extends LinearTextContainer<TextSegment> implements TextSegment.RenderableSegment {
51 public static final String TAG = "tspan";
52
53 private final @NotNull LinearTextLayoutGroup layoutGroup = new LinearTextLayoutGroup(this, children);
54
55 @Override
56 public @NotNull String tagName() {
57 return TAG;
58 }
59
60 @Override
61 protected void doAdd(@NotNull SVGNode node) {
62 children.add((TextSegment) node);
63 }
64
65 @Override
66 public void addContent(@NotNull TextContent.Segment content) {
67 if (content.isConstant() && content.text().isEmpty()) return;
68 if (children.isEmpty() && content.isConstant() && AttributeUtil.isBlank(content.text())) return;
69 children.add(new StringTextSegment(this, layoutGroup, children.size(), content));
70 }
71
72 @Override
73 protected boolean acceptChild(@Nullable String id, @NotNull SVGNode node) {
74 return node instanceof TextSegment;
75 }
76
77 @Override
78 public void prepareSegmentForRendering(@NotNull GlyphCursor cursor, @NotNull RenderContext context,
79 @NotNull TextOutput textOutput) {
80 layoutGroup.asSegment().prepareSegmentForRendering(cursor, context, textOutput);
81 }
82
83 @Override
84 public void renderSegmentWithoutLayout(@NotNull GlyphCursor cursor, @NotNull RenderContext context,
85 @NotNull Output output) {
86 layoutGroup.asSegment().renderSegmentWithoutLayout(cursor, context, output);
87 }
88
89 @Override
90 public @NotNull TextMetrics computeTextMetrics(@NotNull RenderContext context,
91 @NotNull UseTextLengthForCalculation flag) {
92 return layoutGroup.asSegment().computeTextMetrics(context, flag);
93 }
94
95 @Override
96 public void appendTextShape(@NotNull GlyphCursor cursor, @NotNull MutableGlyphRun glyphRun,
97 @NotNull RenderContext context) {
98 layoutGroup.asSegment().appendTextShape(cursor, glyphRun, context);
99 }
100
101 @Override
102 @NotNull
103 Shape glyphShape(@NotNull RenderContext context) {
104 Path2D shape = new Path2D.Float();
105 layoutGroup.appendGlyphShape(null, context, shape);
106 return shape;
107 }
108
109 @Override
110 public boolean isSegmentVisible(@NotNull RenderContext currentContext) {
111 return layoutGroup.asSegment().isSegmentVisible(currentContext);
112 }
113 }