Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/context/PaintContext.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.renderer.impl.context;
23
24
25 import java.awt.*;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.animation.value.AnimatedColor;
31 import com.github.weisj.jsvg.attributes.Animatable;
32 import com.github.weisj.jsvg.attributes.FillRule;
33 import com.github.weisj.jsvg.attributes.Inherited;
34 import com.github.weisj.jsvg.attributes.PaintOrder;
35 import com.github.weisj.jsvg.attributes.value.PercentageValue;
36 import com.github.weisj.jsvg.geometry.size.Percentage;
37 import com.github.weisj.jsvg.nodes.prototype.Mutator;
38 import com.github.weisj.jsvg.paint.SVGPaint;
39 import com.github.weisj.jsvg.paint.impl.AwtSVGPaint;
40 import com.github.weisj.jsvg.paint.impl.PredefinedPaints;
41 import com.github.weisj.jsvg.paint.impl.RGBColor;
42 import com.github.weisj.jsvg.parser.impl.AttributeNode;
43
44 public final class PaintContext implements Mutator<PaintContext> {
45
46 public final @Nullable SVGPaint color;
47 public final @Nullable SVGPaint fillPaint;
48 public final @Nullable SVGPaint strokePaint;
49
50 public final @NotNull PercentageValue opacity;
51 public final @Nullable PercentageValue fillOpacity;
52 public final @Nullable PercentageValue strokeOpacity;
53
54 public final @Nullable PaintOrder paintOrder;
55
56 public final @Nullable StrokeContext strokeContext;
57 public final @Nullable FillRule fillRule;
58
59 public PaintContext(@Nullable SVGPaint color, @Nullable SVGPaint fillPaint, @Nullable PercentageValue fillOpacity,
60 @Nullable SVGPaint strokePaint, @Nullable PercentageValue strokeOpacity, @NotNull PercentageValue opacity,
61 @Nullable PaintOrder paintOrder,
62 @Nullable StrokeContext strokeContext, @Nullable FillRule fillRule) {
63 this.color = color;
64 this.fillPaint = fillPaint;
65 this.strokePaint = strokePaint;
66 this.fillOpacity = fillOpacity;
67 this.strokeOpacity = strokeOpacity;
68 this.opacity = opacity;
69 this.paintOrder = paintOrder;
70 // Avoid creating unnecessary intermediate contexts during painting.
71 this.strokeContext = strokeContext == null || strokeContext.isTrivial() ? null : strokeContext;
72 this.fillRule = fillRule;
73 }
74
75 public static @NotNull PaintContext createDefault(@NotNull SVGPaint currentColor) {
76 return new PaintContext(
77 currentColor,
78 PredefinedPaints.DEFAULT_PAINT, Percentage.ONE,
79 PredefinedPaints.NONE, Percentage.ONE, Percentage.ONE,
80 PaintOrder.NORMAL,
81 StrokeContext.createDefault(),
82 FillRule.Nonzero);
83 }
84
85 public static @NotNull PaintContext parse(@NotNull AttributeNode attributeNode) {
86 return new PaintContext(
87 parseColorAttribute(attributeNode),
88 attributeNode.getPaint("fill", Inherited.YES, Animatable.YES),
89 attributeNode.getPercentage("fill-opacity", Inherited.YES, Animatable.YES),
90 attributeNode.getPaint("stroke", Inherited.YES, Animatable.YES),
91 attributeNode.getPercentage("stroke-opacity", Inherited.YES, Animatable.YES),
92 attributeNode.getPercentage("opacity", Percentage.ONE, Inherited.NO, Animatable.YES),
93 PaintOrder.parse(attributeNode),
94 StrokeContext.parse(attributeNode),
95 FillRule.parse(attributeNode));
96 }
97
98 private static @Nullable SVGPaint parseColorAttribute(@NotNull AttributeNode attributeNode) {
99 Color c = attributeNode.getColor("color", null);
100 if (c == null) return null;
101 AnimatedColor animatedColor = attributeNode.getAnimatedColor("color", new RGBColor(c));
102 if (animatedColor != null) return animatedColor;
103 return new AwtSVGPaint(c);
104 }
105
106 public @NotNull PaintContext derive(@NotNull PaintContext context) {
107 return new PaintContext(
108 SVGPaint.derive(color, context.color),
109 SVGPaint.derive(fillPaint, context.fillPaint),
110 context.fillOpacity != null ? context.fillOpacity : fillOpacity,
111 SVGPaint.derive(strokePaint, context.strokePaint),
112 context.strokeOpacity != null ? context.strokeOpacity : strokeOpacity,
113 opacity.multiply(context.opacity),
114 context.paintOrder != null ? context.paintOrder : paintOrder,
115 strokeContext != null
116 ? strokeContext.derive(context.strokeContext)
117 : context.strokeContext,
118 context.fillRule != null && context.fillRule != FillRule.Inherit
119 ? context.fillRule
120 : this.fillRule);
121 }
122
123 @Override
124 public @NotNull PaintContext mutate(@NotNull PaintContext element) {
125 return element.derive(this);
126 }
127
128 @Override
129 public String toString() {
130 return "PaintContext{" +
131 "color=" + color +
132 ", fillPaint=" + fillPaint +
133 ", strokePaint=" + strokePaint +
134 ", opacity=" + opacity +
135 ", fillOpacity=" + fillOpacity +
136 ", strokeOpacity=" + strokeOpacity +
137 ", strokeContext=" + strokeContext +
138 ", paintOrder=" + paintOrder +
139 ", fillRule=" + fillRule +
140 '}';
141 }
142 }