Debian Codegraph @

src/com/github/weisj/jsvg/nodes/ShapeNode.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.Area;
26 import java.awt.geom.Rectangle2D;
27 import java.util.Set;
28
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import com.github.weisj.jsvg.attributes.VectorEffect;
33 import com.github.weisj.jsvg.attributes.font.FontParser;
34 import com.github.weisj.jsvg.attributes.font.FontSize;
35 import com.github.weisj.jsvg.attributes.font.MeasurableFontSpec;
36 import com.github.weisj.jsvg.attributes.value.LengthValue;
37 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
38 import com.github.weisj.jsvg.geometry.SVGShape;
39 import com.github.weisj.jsvg.geometry.size.Length;
40 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
41 import com.github.weisj.jsvg.nodes.prototype.*;
42 import com.github.weisj.jsvg.parser.impl.AttributeNode;
43 import com.github.weisj.jsvg.parser.impl.AttributeNode.ElementRelation;
44 import com.github.weisj.jsvg.renderer.MeasureContext;
45 import com.github.weisj.jsvg.renderer.RenderContext;
46 import com.github.weisj.jsvg.renderer.impl.ShapeRenderer;
47 import com.github.weisj.jsvg.renderer.impl.context.PaintContext;
48 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
49 import com.github.weisj.jsvg.renderer.output.Output;
50
51 public abstract class ShapeNode extends RenderableSVGNode
52 implements HasShape, HasPaintContext, HasFontContext, HasVectorEffects, Instantiator {
53
54 private PaintContext paintContext;
55 private FontSize fontSize;
56 private Length fontSizeAdjust;
57
58 private Length pathLength;
59 private SVGShape shape;
60
61 private Marker markerStart;
62 private Marker markerMid;
63 private Marker markerEnd;
64
65 private Set<VectorEffect> vectorEffects;
66
67 @Override
68 public @NotNull PaintContext paintContext() {
69 return paintContext;
70 }
71
72 @Override
73 public @NotNull Mutator<MeasurableFontSpec> fontSpec() {
74 return s -> s.withFontSize(fontSize, fontSizeAdjust);
75 }
76
77 public @NotNull SVGShape shape() {
78 return shape;
79 }
80
81 @Override
82 public @NotNull Set<VectorEffect> vectorEffects() {
83 return vectorEffects;
84 }
85
86 @Override
87 public final void build(@NotNull AttributeNode attributeNode) {
88 super.build(attributeNode);
89 paintContext = PaintContext.parse(attributeNode);
90
91 fontSize = FontParser.parseFontSize(attributeNode);
92 fontSizeAdjust = FontParser.parseSizeAdjust(attributeNode);
93
94 shape = buildShape(attributeNode);
95 pathLength = attributeNode.getLength("pathLength", PercentageDimension.NONE, Length.UNSPECIFIED);
96
97 // Todo: These are actually inheritable and hence have to go into the RenderContext
98 // Todo: The marker shorthand is a bit more complicated than just being a template.
99 // https://www.w3.org/TR/svg-markers/#MarkerShorthand
100 Marker template = attributeNode.getElementByHref(Marker.class, attributeNode.getValue("marker"),
101 ElementRelation.TEMPLATE);
102 markerStart = attributeNode.getElementByHref(Marker.class, attributeNode.getValue("marker-start"),
103 ElementRelation.TEMPLATE);
104 if (markerStart == null) markerStart = template;
105
106 markerMid = attributeNode.getElementByHref(Marker.class, attributeNode.getValue("marker-mid"),
107 ElementRelation.TEMPLATE);
108 if (markerMid == null) markerMid = template;
109
110 markerEnd = attributeNode.getElementByHref(Marker.class, attributeNode.getValue("marker-end"),
111 ElementRelation.TEMPLATE);
112 if (markerEnd == null) markerEnd = template;
113
114 vectorEffects = VectorEffect.parse(attributeNode);
115 }
116
117 protected abstract @NotNull SVGShape buildShape(@NotNull AttributeNode attributeNode);
118
119 @Override
120 public @NotNull Shape untransformedElementShape(@NotNull RenderContext context, Box box) {
121 Shape realShape = shape.shape(context);
122 switch (box) {
123 case BoundingBox:
124 return realShape;
125 case StrokeBox:
126 Area area = new Area(realShape);
127 area.add(new Area(computeEffectiveStroke(context).createStrokedShape(realShape)));
128 return area;
129 default:
130 throw new IllegalStateException("Unexpected value: " + box);
131 }
132 }
133
134 @Override
135 public @NotNull Rectangle2D untransformedElementBounds(@NotNull RenderContext context, Box box) {
136 Rectangle2D bounds = shape.bounds(context, true);
137 switch (box) {
138 case BoundingBox:
139 return bounds;
140 case StrokeBox: {
141 LengthValue strokeWidth = RenderContextAccessor.instance().strokeContext(context).strokeWidth;
142 if (strokeWidth != null) {
143 float stroke = strokeWidth.resolve(context.measureContext());
144 if (stroke > 0) bounds = GeometryUtil.grow(bounds, stroke);
145 }
146 return bounds;
147 }
148 default:
149 throw new IllegalStateException("Unexpected value: " + box);
150 }
151 }
152
153 @Override
154 public boolean canInstantiate(@NotNull SVGNode node) {
155 return node instanceof Marker;
156 }
157
158 private @NotNull Stroke computeEffectiveStroke(@NotNull RenderContext context) {
159 MeasureContext measureContext = context.measureContext();
160 float pathLengthFactor = 1f;
161 if (pathLength.isSpecified()) {
162 double effectiveLength = pathLength.resolve(measureContext);
163 double actualLength = shape.pathLength(context);
164 pathLengthFactor = (float) (actualLength / effectiveLength);
165 }
166 return context.stroke(pathLengthFactor);
167 }
168
169 @Override
170 public final void render(@NotNull RenderContext context, @NotNull Output output) {
171 Shape paintShape = shape.shape(context);
172 @Nullable Rectangle2D bounds = shape.usesOptimizedBoundsCalculation()
173 ? shape.bounds(context, false)
174 : null;
175
176 Stroke effectiveStroke = computeEffectiveStroke(context);
177 ShapeRenderer.renderWithPaintOrder(output, shape.canBeFilled(),
178 RenderContextAccessor.instance().paintOrder(context),
179 new ShapeRenderer.ShapePaintContext(context, vectorEffects(), effectiveStroke,
180 GeometryUtil.toAwtTransform(context, transform())),
181 new ShapeRenderer.PaintShape(paintShape, bounds),
182 new ShapeRenderer.ShapeMarkerInfo(this, markerStart, markerMid, markerEnd,
183 shouldPaintStartEndMarkersInMiddle()));
184 }
185
186 protected boolean shouldPaintStartEndMarkersInMiddle() {
187 return true;
188 }
189 }