Debian Codegraph @

src/com/github/weisj/jsvg/nodes/LinearGradient.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 import java.util.Arrays;
28
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
33 import com.github.weisj.jsvg.geometry.size.Length;
34 import com.github.weisj.jsvg.geometry.size.Percentage;
35 import com.github.weisj.jsvg.geometry.size.Unit;
36 import com.github.weisj.jsvg.nodes.animation.Animate;
37 import com.github.weisj.jsvg.nodes.animation.AnimateTransform;
38 import com.github.weisj.jsvg.nodes.animation.Set;
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.PermittedContent;
42 import com.github.weisj.jsvg.parser.impl.AttributeNode;
43 import com.github.weisj.jsvg.renderer.MeasureContext;
44
45 @ElementCategories(Category.Gradient)
46 @PermittedContent(
47 categories = Category.Descriptive,
48 anyOf = {Stop.class, Animate.class, AnimateTransform.class, Set.class}
49 )
50 public final class LinearGradient extends AbstractGradient<LinearGradient> {
51 public static final String TAG = "lineargradient";
52
53 private Length x1;
54 private Length x2;
55 private Length y1;
56 private Length y2;
57
58 @Override
59 public @NotNull String tagName() {
60 return TAG;
61 }
62
63 @Override
64 protected void buildGradient(@NotNull AttributeNode attributeNode, @Nullable LinearGradient template) {
65 x1 = attributeNode.getLength("x1", PercentageDimension.WIDTH,
66 template != null ? template.x1 : Unit.PERCENTAGE_WIDTH.valueOf(0));
67 x2 = attributeNode.getLength("x2", PercentageDimension.WIDTH,
68 template != null ? template.x2 : Unit.PERCENTAGE_WIDTH.valueOf(100));
69 y1 = attributeNode.getLength("y1", PercentageDimension.HEIGHT,
70 template != null ? template.y1 : Unit.PERCENTAGE_HEIGHT.valueOf(0));
71 y2 = attributeNode.getLength("y2", PercentageDimension.HEIGHT,
72 template != null ? template.y2 : Unit.PERCENTAGE_HEIGHT.valueOf(0));
73 }
74
75 @Override
76 protected @NotNull Paint gradientForBounds(@NotNull MeasureContext measure, @NotNull Rectangle2D bounds,
77 Percentage[] gradOffsets, @NotNull Color[] gradColors) {
78 Point2D.Float pt1 = new Point2D.Float(x1.resolve(measure), y1.resolve(measure));
79 Point2D.Float pt2 = new Point2D.Float(x2.resolve(measure), y2.resolve(measure));
80 if (pt1.equals(pt2)) return gradColors[0];
81
82 return new LinearGradientPaint(pt1, pt2, offsetsToFractions(gradOffsets), gradColors,
83 spreadMethod.cycleMethod(),
84 MultipleGradientPaint.ColorSpaceType.SRGB, computeViewTransform(measure, bounds));
85 }
86
87 @Override
88 public String toString() {
89 return "LinearGradient{" +
90 "spreadMethod=" + spreadMethod +
91 ", gradientTransform=" + gradientTransform +
92 ", x1=" + x1 +
93 ", x2=" + x2 +
94 ", y1=" + y1 +
95 ", y2=" + y2 +
96 ", colors=" + Arrays.toString(colors()) +
97 ", offsets=" + Arrays.toString(offsets()) +
98 '}';
99 }
100 }