Debian Codegraph @

src/com/github/weisj/jsvg/nodes/Use.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;
23
24 import java.awt.*;
25 import java.awt.geom.Rectangle2D;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.attributes.FillRule;
31 import com.github.weisj.jsvg.attributes.font.AttributeFontSpec;
32 import com.github.weisj.jsvg.attributes.font.FontParser;
33 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
34 import com.github.weisj.jsvg.geometry.AWTSVGShape;
35 import com.github.weisj.jsvg.geometry.size.Length;
36 import com.github.weisj.jsvg.nodes.container.CommonInnerViewContainer;
37 import com.github.weisj.jsvg.nodes.prototype.*;
38 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
39 import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories;
40 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
41 import com.github.weisj.jsvg.parser.impl.AttributeNode;
42 import com.github.weisj.jsvg.parser.impl.AttributeNode.ElementRelation;
43 import com.github.weisj.jsvg.renderer.MeasureContext;
44 import com.github.weisj.jsvg.renderer.RenderContext;
45 import com.github.weisj.jsvg.renderer.impl.NodeRenderer;
46 import com.github.weisj.jsvg.renderer.impl.context.FontRenderContext;
47 import com.github.weisj.jsvg.renderer.impl.context.PaintContext;
48 import com.github.weisj.jsvg.renderer.output.Output;
49 import com.github.weisj.jsvg.view.FloatSize;
50
51 @ElementCategories({Category.Graphic, Category.GraphicsReferencing, Category.Structural})
52 @PermittedContent(categories = {Category.Animation, Category.Descriptive})
53 public final class Use extends RenderableSVGNode implements HasContext, HasShape, Instantiator {
54 public static final String TAG = "use";
55
56 private Length x;
57 private Length y;
58 private Length width;
59 private Length height;
60
61 private @Nullable SVGNode referencedNode;
62
63 private PaintContext paintContext;
64 private FontRenderContext fontRenderContext;
65 private AttributeFontSpec fontSpec;
66 private FillRule fillRule;
67
68 @Override
69 public @NotNull String tagName() {
70 return TAG;
71 }
72
73 public @Nullable SVGNode referencedNode() {
74 return referencedNode;
75 }
76
77 @Override
78 public boolean isVisible(@NotNull RenderContext context) {
79 return super.isVisible(context) && referencedNode instanceof Renderable;
80 }
81
82 @Override
83 public void build(@NotNull AttributeNode attributeNode) {
84 super.build(attributeNode);
85 x = attributeNode.getLength("x", PercentageDimension.WIDTH, 0);
86 y = attributeNode.getLength("y", PercentageDimension.HEIGHT, 0);
87 width = attributeNode.getLength("width", PercentageDimension.WIDTH, Length.UNSPECIFIED);
88 height = attributeNode.getLength("height", PercentageDimension.HEIGHT, Length.UNSPECIFIED);
89
90 String href = attributeNode.getValue("href");
91 if (href == null) href = attributeNode.getValue("xlink:href");
92 referencedNode = attributeNode.getElementByHref(SVGNode.class, href, ElementRelation.PAINTED_CHILD);
93
94 paintContext = PaintContext.parse(attributeNode);
95 fontRenderContext = FontRenderContext.parse(attributeNode);
96 fontSpec = FontParser.parseFontSpec(attributeNode);
97 fillRule = FillRule.parse(attributeNode);
98 }
99
100 @Override
101 public @NotNull Shape untransformedElementShape(@NotNull RenderContext context, Box box) {
102 // Todo: Inner views need to handle this differently
103 return referencedNode instanceof HasShape
104 ? ((HasShape) referencedNode).elementShape(
105 NodeRenderer.createChildContext((Renderable) referencedNode, context, this),
106 box)
107 : AWTSVGShape.EMPTY_SHAPE;
108 }
109
110 @Override
111 public @NotNull Rectangle2D untransformedElementBounds(@NotNull RenderContext context, Box box) {
112 // Todo: Inner views need to handle this differently
113 return referencedNode instanceof HasShape
114 ? ((HasShape) referencedNode).elementBounds(
115 NodeRenderer.createChildContext((Renderable) referencedNode, context, this), box)
116 : AWTSVGShape.EMPTY_SHAPE;
117 }
118
119 @Override
120 public @NotNull PaintContext paintContext() {
121 return paintContext;
122 }
123
124 @Override
125 public @NotNull FontRenderContext fontRenderContext() {
126 return fontRenderContext;
127 }
128
129 @Override
130 public @NotNull AttributeFontSpec fontSpec() {
131 return fontSpec;
132 }
133
134 @Override
135 public boolean canInstantiate(@NotNull SVGNode node) {
136 return node instanceof CommonInnerViewContainer;
137 }
138
139 @Override
140 public void render(@NotNull RenderContext context, @NotNull Output output) {
141 if (referencedNode == null) return;
142 MeasureContext measureContext = context.measureContext();
143 context.translate(output, x.resolve(measureContext), y.resolve(measureContext));
144
145 // Todo: Vector Effects
146
147 if (referencedNode instanceof CommonInnerViewContainer) {
148 FloatSize targetViewBox = new FloatSize(Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW);
149 if (width.isSpecified()) targetViewBox.width = width.resolve(measureContext);
150 if (height.isSpecified()) targetViewBox.height = height.resolve(measureContext);
151 CommonInnerViewContainer view = (CommonInnerViewContainer) referencedNode;
152 NodeRenderer.renderWithSize(view, targetViewBox, context, output, this);
153 } else {
154 NodeRenderer.renderNode(referencedNode, context, output, this);
155 }
156 }
157
158 @Override
159 public String toString() {
160 return "Use{" +
161 "x=" + x +
162 ", y=" + y +
163 ", width=" + width +
164 ", height=" + height +
165 ", referencedNode=" + (referencedNode != null ? referencedNode.id() : null) +
166 ", styleContext=" + paintContext +
167 ", fillRule=" + fillRule +
168 ", fontRenderContext=" + fontRenderContext +
169 '}';
170 }
171 }