Debian Codegraph @

src/com/github/weisj/jsvg/nodes/RadialGradient.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.paint.impl.jdk.SVGRadialGradientPaint;
43 import com.github.weisj.jsvg.parser.impl.AttributeNode;
44 import com.github.weisj.jsvg.renderer.MeasureContext;
45
46 @ElementCategories(Category.Gradient)
47 @PermittedContent(
48 categories = Category.Descriptive,
49 anyOf = {Stop.class, Animate.class, AnimateTransform.class, Set.class,}
50 )
51 public final class RadialGradient extends AbstractGradient<RadialGradient> {
52 public static final String TAG = "radialgradient";
53
54 private Length cx;
55 private Length cy;
56 private Length r;
57 private Length fr;
58 private Length fx;
59 private Length fy;
60
61 @Override
62 public @NotNull String tagName() {
63 return TAG;
64 }
65
66 @Override
67 protected void buildGradient(@NotNull AttributeNode attributeNode, @Nullable RadialGradient template) {
68 cx = attributeNode.getLength("cx", PercentageDimension.WIDTH,
69 template != null ? template.cx : Unit.PERCENTAGE_WIDTH.valueOf(50));
70 cy = attributeNode.getLength("cy", PercentageDimension.HEIGHT,
71 template != null ? template.cy : Unit.PERCENTAGE_HEIGHT.valueOf(50));
72 r = attributeNode.getLength("r", PercentageDimension.LENGTH,
73 template != null ? template.r : Unit.PERCENTAGE_LENGTH.valueOf(50));
74 fr = attributeNode.getLength("fr", PercentageDimension.LENGTH,
75 template != null ? template.fr : Unit.PERCENTAGE_LENGTH.valueOf(0));
76 fx = attributeNode.getLength("fx", PercentageDimension.WIDTH,
77 template != null ? template.fx : cx);
78 fy = attributeNode.getLength("fy", PercentageDimension.HEIGHT,
79 template != null ? template.fy : cy);
80 }
81
82 @Override
83 protected @NotNull Paint gradientForBounds(@NotNull MeasureContext measure, @NotNull Rectangle2D bounds,
84 Percentage[] gradOffsets, @NotNull Color[] gradColors) {
85 assert gradColors.length > 0;
86 Point2D.Float center = new Point2D.Float(cx.resolve(measure), cy.resolve(measure));
87 Point2D.Float focusCenter = new Point2D.Float(fx.resolve(measure), fy.resolve(measure));
88
89 float radius = r.resolve(measure);
90 float focusRadius = fr.resolve(measure);
91
92 if (radius <= 0) {
93 return gradColors[gradColors.length - 1];
94 }
95
96 if (focusRadius == 0) {
97 // If possible use built-in RadialGradientPaint as it profits from hardware acceleration
98 return new RadialGradientPaint(center, radius, focusCenter,
99 offsetsToFractions(gradOffsets), gradColors, spreadMethod.cycleMethod(),
100 MultipleGradientPaint.ColorSpaceType.SRGB, computeViewTransform(measure, bounds));
101 }
102
103 return new SVGRadialGradientPaint(center, radius, focusCenter, focusRadius,
104 offsetsToFractions(gradOffsets), gradColors, spreadMethod.cycleMethod(),
105 MultipleGradientPaint.ColorSpaceType.SRGB, computeViewTransform(measure, bounds));
106 }
107
108 @Override
109 public String toString() {
110 return "RadialGradient{" +
111 "spreadMethod=" + spreadMethod +
112 ", gradientTransform=" + gradientTransform +
113 ", cx=" + cx +
114 ", cy=" + cy +
115 ", r=" + r +
116 ", fr=" + fr +
117 ", fx=" + fx +
118 ", fy=" + fy +
119 ", colors=" + Arrays.toString(colors()) +
120 ", offsets=" + Arrays.toString(offsets()) +
121 '}';
122 }
123 }