Debian Codegraph @

src/com/github/weisj/jsvg/nodes/mesh/MeshGradient.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.mesh;
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.UnitType;
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.SVGNode;
35 import com.github.weisj.jsvg.nodes.animation.Animate;
36 import com.github.weisj.jsvg.nodes.animation.AnimateTransform;
37 import com.github.weisj.jsvg.nodes.animation.Set;
38 import com.github.weisj.jsvg.nodes.container.ContainerNode;
39 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
40 import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories;
41 import com.github.weisj.jsvg.nodes.prototype.spec.NotImplemented;
42 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
43 import com.github.weisj.jsvg.paint.SVGPaint;
44 import com.github.weisj.jsvg.parser.impl.AttributeNode;
45 import com.github.weisj.jsvg.renderer.MeasureContext;
46 import com.github.weisj.jsvg.renderer.RenderContext;
47 import com.github.weisj.jsvg.renderer.output.Output;
48
49 @ElementCategories(Category.Gradient)
50 @PermittedContent(
51 categories = Category.Descriptive,
52 anyOf = {MeshRow.class, Animate.class, AnimateTransform.class, Set.class /* <script> */ }
53 )
54 public final class MeshGradient extends ContainerNode implements SVGPaint {
55 public static final String TAG = "meshgradient";
56
57 private Length x;
58 private Length y;
59
60 @SuppressWarnings("UnusedVariable")
61 private @NotImplemented UnitType gradientUnits;
62
63 @Override
64 public @NotNull String tagName() {
65 return TAG;
66 }
67
68 @Override
69 public void build(@NotNull AttributeNode attributeNode) {
70 super.build(attributeNode);
71 x = attributeNode.getLength("x", PercentageDimension.WIDTH, 0);
72 y = attributeNode.getLength("y", PercentageDimension.HEIGHT, 0);
73 gradientUnits = attributeNode.getEnum("gradientUnits", UnitType.ObjectBoundingBox);
74 MeshBuilder.buildMesh(this, new Point2D.Float(x.raw(), y.raw()));
75 // Todo: type bilinear|bicubic
76 // Todo: href template
77 // Todo: transform
78 }
79
80 public void renderMesh(@NotNull MeasureContext measure, @NotNull Output output) {
81 Output meshOutput = output.createChild();
82 // meshGraphics.translate(x.resolve(measure), y.resolve(measure));
83
84 meshOutput.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
85 for (SVGNode child : children()) {
86 MeshRow row = (MeshRow) child;
87 for (SVGNode node : row.children()) {
88 MeshPatch patch = (MeshPatch) node;
89 patch.renderPath(meshOutput);
90 }
91 }
92 meshOutput.dispose();
93 }
94
95 @Override
96 public void fillShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape,
97 @Nullable Rectangle2D bounds) {
98 Output.SafeState safeState = output.safeState();
99 Rectangle2D b = bounds != null ? bounds : shape.getBounds2D();
100 output.applyClip(shape);
101 output.translate(b.getX(), b.getY());
102 renderMesh(context.measureContext(), output);
103 safeState.restore();
104 }
105
106 @Override
107 public void drawShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape,
108 @Nullable Rectangle2D bounds) {
109 Output.SafeState safeState = output.safeState();
110 Rectangle2D b = bounds != null ? bounds : shape.getBounds2D();
111 output.applyClip(output.stroke().createStrokedShape(shape));
112 output.translate(b.getX(), b.getY());
113 renderMesh(context.measureContext(), output);
114 safeState.restore();
115 }
116 }