Debian Codegraph @

src/com/github/weisj/jsvg/nodes/Pattern.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.Point2D;
26 import java.awt.geom.Rectangle2D;
27
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30
31 import com.github.weisj.jsvg.attributes.Overflow;
32 import com.github.weisj.jsvg.attributes.UnitType;
33 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
34 import com.github.weisj.jsvg.attributes.value.TransformValue;
35 import com.github.weisj.jsvg.geometry.size.Length;
36 import com.github.weisj.jsvg.nodes.container.BaseInnerViewContainer;
37 import com.github.weisj.jsvg.nodes.filter.Filter;
38 import com.github.weisj.jsvg.nodes.prototype.Instantiator;
39 import com.github.weisj.jsvg.nodes.prototype.ShapedContainer;
40 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
41 import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories;
42 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
43 import com.github.weisj.jsvg.nodes.text.Text;
44 import com.github.weisj.jsvg.paint.SVGPaint;
45 import com.github.weisj.jsvg.paint.impl.TransformedPaint;
46 import com.github.weisj.jsvg.parser.PaintParser;
47 import com.github.weisj.jsvg.parser.impl.AttributeNode;
48 import com.github.weisj.jsvg.parser.impl.AttributeNode.ElementRelation;
49 import com.github.weisj.jsvg.renderer.MeasureContext;
50 import com.github.weisj.jsvg.renderer.RenderContext;
51 import com.github.weisj.jsvg.renderer.output.Output;
52 import com.github.weisj.jsvg.util.BlittableImage;
53 import com.github.weisj.jsvg.util.ImageUtil;
54 import com.github.weisj.jsvg.view.FloatSize;
55
56 @ElementCategories(Category.Container)
57 @PermittedContent(
58 categories = {Category.Animation, Category.Descriptive, Category.Shape, Category.Structural, Category.Gradient},
59 /*
60 * <altGlyphDef>, <color-profile>, <cursor>, <font>, <font-face>, <foreignObject>, <script>,
61 * <switch>
62 */
63 anyOf = {Anchor.class, ClipPath.class, Filter.class, Image.class, Mask.class, Marker.class, Pattern.class,
64 Style.class, Text.class, View.class}
65 )
66 public final class Pattern extends BaseInnerViewContainer implements SVGPaint, ShapedContainer<SVGNode>, Instantiator {
67 public static final String TAG = "pattern";
68
69 private Length x;
70 private Length y;
71 private Length width;
72 private Length height;
73
74 private UnitType patternUnits;
75 private UnitType patternContentUnits;
76 private TransformValue patternTransform;
77
78 @Override
79 public @NotNull String tagName() {
80 return TAG;
81 }
82
83 @Override
84 protected @NotNull Point2D outerLocation(@NotNull MeasureContext context) {
85 return new Point2D.Float(0, 0);
86 }
87
88 @Override
89 protected @Nullable Point2D anchorLocation(@NotNull MeasureContext context) {
90 return null;
91 }
92
93 @Override
94 protected @NotNull Overflow defaultOverflow() {
95 return Overflow.Hidden;
96 }
97
98 @Override
99 public @NotNull FloatSize size(@NotNull RenderContext context) {
100 return new FloatSize(
101 width.resolve(context.measureContext()),
102 height.resolve(context.measureContext()));
103 }
104
105 @Override
106 public void build(@NotNull AttributeNode attributeNode) {
107 super.build(attributeNode);
108 Pattern template = parseTemplate(attributeNode);
109
110 if (viewBox == null && template != null) viewBox = template.viewBox;
111 preserveAspectRatio = template != null ? template.preserveAspectRatio : preserveAspectRatio;
112
113 patternUnits = attributeNode.getEnum("patternUnits",
114 template != null ? template.patternUnits : UnitType.ObjectBoundingBox);
115 patternContentUnits = attributeNode.getEnum("patternContentUnits",
116 template != null ? template.patternContentUnits : UnitType.UserSpaceOnUse);
117
118 x = attributeNode
119 .getLength("x", PercentageDimension.WIDTH, template != null ? template.x : Length.ZERO)
120 .coercePercentageToCorrectUnit(patternUnits, PercentageDimension.WIDTH);
121 y = attributeNode
122 .getLength("y", PercentageDimension.HEIGHT, template != null ? template.y : Length.ZERO)
123 .coercePercentageToCorrectUnit(patternUnits, PercentageDimension.HEIGHT);
124 // Note: width == 0 || height == 0 implies nothing should be painted.
125 width = attributeNode
126 .getLength("width", PercentageDimension.WIDTH, template != null ? template.width : Length.ZERO)
127 .coerceNonNegative()
128 .coercePercentageToCorrectUnit(patternUnits, PercentageDimension.WIDTH);
129 height = attributeNode
130 .getLength("height", PercentageDimension.HEIGHT, template != null ? template.height : Length.ZERO)
131 .coerceNonNegative()
132 .coercePercentageToCorrectUnit(patternUnits, PercentageDimension.HEIGHT);
133
134 patternTransform = attributeNode.parseTransform("patternTransform");
135 if (patternTransform == null && template != null) patternTransform = template.patternTransform;
136 }
137
138 private @Nullable Pattern parseTemplate(@NotNull AttributeNode attributeNode) {
139 Pattern template =
140 attributeNode.getElementByHref(Pattern.class, attributeNode.getHref(), ElementRelation.TEMPLATE);
141 return template != this ? template : null;
142 }
143
144 @Override
145 public boolean isVisible(@NotNull RenderContext context) {
146 return !width.isZero() && !height.isZero() && SVGPaint.super.isVisible(context);
147 }
148
149 @Override
150 public void fillShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape,
151 @Nullable Rectangle2D bounds) {
152 Rectangle2D b = bounds != null ? bounds : shape.getBounds2D();
153 // TODO: Might not fully want to avoid this here if shape computation should become more fine
154 // grained.
155 output.setPaint(() -> paintForBounds(output, context, b));
156 output.fillShape(shape);
157 }
158
159 @Override
160 public void drawShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape,
161 @Nullable Rectangle2D bounds) {
162 Rectangle2D b = bounds != null ? bounds : shape.getBounds2D();
163 output.setPaint(() -> paintForBounds(output, context, b));
164 output.drawShape(shape);
165 }
166
167 private @NotNull Paint paintForBounds(@NotNull Output output, @NotNull RenderContext context,
168 @NotNull Rectangle2D bounds) {
169 MeasureContext measure = context.measureContext();
170 Rectangle2D.Double patternBounds = patternUnits.computeViewBounds(measure, bounds, x, y, width, height);
171
172 // TODO: With overflow = visible this does not result in the correct behaviour
173 BlittableImage blittableImage = BlittableImage.create(
174 ImageUtil::createCompatibleTransparentImage, context, null,
175 patternBounds, bounds, patternContentUnits);
176
177 if (blittableImage == null) return PaintParser.DEFAULT_COLOR;
178
179 blittableImage.render(output, (out, ctx) -> {
180 if (patternContentUnits == UnitType.UserSpaceOnUse) {
181 ctx.translate(out, patternBounds.getX(), patternBounds.getY());
182 }
183 renderWithSize(new FloatSize(patternBounds), viewBox, ctx, out);
184 });
185
186 // Fixme: When patternTransform != null antialiasing is broken
187 return patternTransform != null
188 ? new TransformedPaint(new TexturePaint(blittableImage.image(), patternBounds),
189 patternTransform.get(measure))
190 : new TexturePaint(blittableImage.image(), patternBounds);
191 }
192
193 @Override
194 public boolean requiresInstantiation() {
195 return true;
196 }
197
198 @Override
199 public boolean canInstantiate(@NotNull SVGNode node) {
200 return node == this;
201 }
202 }