Debian Codegraph @

src/com/github/weisj/jsvg/nodes/Marker.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;
23
24 import java.awt.geom.Point2D;
25
26 import org.jetbrains.annotations.NotNull;
27
28 import com.github.weisj.jsvg.attributes.MarkerOrientation;
29 import com.github.weisj.jsvg.attributes.MarkerUnitType;
30 import com.github.weisj.jsvg.attributes.Overflow;
31 import com.github.weisj.jsvg.attributes.value.LengthValue;
32 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
33 import com.github.weisj.jsvg.geometry.size.Length;
34 import com.github.weisj.jsvg.nodes.container.BaseInnerViewContainer;
35 import com.github.weisj.jsvg.nodes.filter.Filter;
36 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
37 import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories;
38 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
39 import com.github.weisj.jsvg.nodes.text.Text;
40 import com.github.weisj.jsvg.parser.impl.AttributeNode;
41 import com.github.weisj.jsvg.renderer.MeasureContext;
42 import com.github.weisj.jsvg.renderer.RenderContext;
43 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
44 import com.github.weisj.jsvg.view.FloatSize;
45
46 @ElementCategories(Category.Container)
47 @PermittedContent(
48 categories = {Category.Animation, Category.Descriptive, Category.Shape, Category.Structural, Category.Gradient},
49 /*
50 * <altGlyphDef>, <color-profile>, <cursor>, <font>, <font-face>, <foreignObject>, <script>,
51 * <switch>
52 */
53 anyOf = {Anchor.class, ClipPath.class, Filter.class, Image.class, Mask.class, Marker.class, Pattern.class,
54 Style.class, Text.class, View.class}
55 )
56 public final class Marker extends BaseInnerViewContainer {
57 public static final String TAG = "marker";
58
59 private Length refX;
60 private Length refY;
61
62 private MarkerOrientation orientation;
63
64 private MarkerUnitType markerUnits;
65 private Length markerHeight;
66 private Length markerWidth;
67
68 @Override
69 public @NotNull String tagName() {
70 return TAG;
71 }
72
73 public @NotNull MarkerOrientation orientation() {
74 return orientation;
75 }
76
77 @Override
78 protected @NotNull Point2D outerLocation(@NotNull MeasureContext context) {
79 return new Point2D.Float(0, 0);
80 }
81
82 @Override
83 protected @NotNull Point2D anchorLocation(@NotNull MeasureContext context) {
84 return new Point2D.Float(-refX.resolve(context), -refY.resolve(context));
85 }
86
87 @Override
88 protected @NotNull Overflow defaultOverflow() {
89 return Overflow.Hidden;
90 }
91
92 @Override
93 public @NotNull FloatSize size(@NotNull RenderContext context) {
94 MeasureContext measure = context.measureContext();
95 if (markerUnits == MarkerUnitType.StrokeWidth) {
96 LengthValue strokeWidthLength = RenderContextAccessor.instance().strokeContext(context).strokeWidth;
97 assert strokeWidthLength != null;
98 float strokeWidth = strokeWidthLength.resolve(measure);
99 return new FloatSize(markerWidth.raw() * strokeWidth, markerHeight.raw() * strokeWidth);
100 } else {
101 return new FloatSize(markerWidth.resolve(measure), markerHeight.resolve(measure));
102 }
103 }
104
105 @Override
106 public void build(@NotNull AttributeNode attributeNode) {
107 super.build(attributeNode);
108 refX = attributeNode.getHorizontalReferenceLengthFromKey("refX");
109 refY = attributeNode.getVerticalReferenceLengthFromKey("refY");
110
111 orientation = MarkerOrientation.parse(attributeNode.getValue("orient"), attributeNode.parser());
112
113 markerUnits = attributeNode.getEnum("markerUnits", MarkerUnitType.StrokeWidth);
114 markerWidth = attributeNode.getLength("markerWidth", PercentageDimension.WIDTH, 3);
115 markerHeight = attributeNode.getLength("markerHeight", PercentageDimension.HEIGHT, 3);
116 }
117
118 @Override
119 public boolean requiresInstantiation() {
120 return true;
121 }
122
123 @Override
124 protected boolean inheritAttributes() {
125 // Markers do not inherit properties from the element they are referenced by.
126 return false;
127 }
128 }