Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/TextContainer.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.Area;
26 import java.awt.geom.Rectangle2D;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.jetbrains.annotations.MustBeInvokedByOverriders;
32 import org.jetbrains.annotations.NotNull;
33
34 import com.github.weisj.jsvg.attributes.VectorEffect;
35 import com.github.weisj.jsvg.attributes.font.AttributeFontSpec;
36 import com.github.weisj.jsvg.attributes.font.FontParser;
37 import com.github.weisj.jsvg.attributes.text.LengthAdjust;
38 import com.github.weisj.jsvg.attributes.text.TextAnchor;
39 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
40 import com.github.weisj.jsvg.geometry.size.Length;
41 import com.github.weisj.jsvg.nodes.container.BaseContainerNode;
42 import com.github.weisj.jsvg.nodes.prototype.HasContext;
43 import com.github.weisj.jsvg.nodes.prototype.HasShape;
44 import com.github.weisj.jsvg.nodes.prototype.HasVectorEffects;
45 import com.github.weisj.jsvg.nodes.prototype.Renderable;
46 import com.github.weisj.jsvg.nodes.prototype.impl.HasContextImpl;
47 import com.github.weisj.jsvg.parser.impl.AttributeNode;
48 import com.github.weisj.jsvg.renderer.RenderContext;
49 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
50
51 abstract class TextContainer<T> extends BaseContainerNode<T>
52 implements HasShape, HasContext.ByDelegate, HasVectorEffects {
53 protected final List<@NotNull T> children = new ArrayList<>();
54
55 protected boolean isVisible;
56 protected AttributeFontSpec fontSpec;
57 protected LengthAdjust lengthAdjust;
58 protected Length textLength;
59
60 private HasContext context;
61
62 private Set<VectorEffect> vectorEffects;
63
64 @Override
65 @MustBeInvokedByOverriders
66 public void build(@NotNull AttributeNode attributeNode) {
67 super.build(attributeNode);
68 fontSpec = FontParser.parseFontSpec(attributeNode);
69 lengthAdjust = attributeNode.getEnum("lengthAdjust", LengthAdjust.Spacing);
70 textLength = attributeNode.getLength("textLength", PercentageDimension.NONE, Length.UNSPECIFIED);
71 if (textLength.raw() < 0) textLength = Length.UNSPECIFIED;
72
73 isVisible = Renderable.parseVisibility(attributeNode);
74 context = HasContextImpl.parse(attributeNode);
75
76 // Todo: Current vector effect pipeline doesn't allow them to be defined on 'tspan' and 'text-path'.
77 vectorEffects = VectorEffect.parse(attributeNode);
78 }
79
80 @Override
81 public @NotNull Set<VectorEffect> vectorEffects() {
82 return vectorEffects;
83 }
84
85 @Override
86 public @NotNull HasContext contextDelegate() {
87 return context;
88 }
89
90 @Override
91 public List<? extends @NotNull T> children() {
92 return children;
93 }
94
95 public boolean isVisible(@NotNull RenderContext context) {
96 return isVisible;
97 }
98
99 abstract @NotNull Shape glyphShape(@NotNull RenderContext context);
100
101 public final TextAnchor textAnchor(@NotNull RenderContext context) {
102 return RenderContextAccessor.instance().fontRenderContext(context).textAnchor();
103 }
104
105 @Override
106 public final @NotNull Shape untransformedElementShape(@NotNull RenderContext context, @NotNull Box box) {
107 Shape shape = glyphShape(context);
108 switch (box) {
109 case BoundingBox:
110 return shape;
111 case StrokeBox:
112 Area area = new Area(shape);
113 area.add(new Area(context.stroke(1).createStrokedShape(shape)));
114 return area;
115 default:
116 throw new IllegalStateException("Unexpected value: " + box);
117 }
118 }
119
120 @Override
121 public @NotNull Rectangle2D untransformedElementBounds(@NotNull RenderContext context, Box box) {
122 // TODO: Bounding-box is specified by the character box.
123 return untransformedElementShape(context, box).getBounds2D();
124 }
125 }