Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/NodeRenderer.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.renderer.impl;
23
24 import java.awt.geom.Rectangle2D;
25
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import com.github.weisj.jsvg.attributes.font.MeasurableFontSpec;
30 import com.github.weisj.jsvg.nodes.ClipPath;
31 import com.github.weisj.jsvg.nodes.Mask;
32 import com.github.weisj.jsvg.nodes.SVG;
33 import com.github.weisj.jsvg.nodes.SVGNode;
34 import com.github.weisj.jsvg.nodes.container.BaseInnerViewContainer;
35 import com.github.weisj.jsvg.nodes.filter.Filter;
36 import com.github.weisj.jsvg.nodes.prototype.*;
37 import com.github.weisj.jsvg.renderer.RenderContext;
38 import com.github.weisj.jsvg.renderer.SVGRenderingHints;
39 import com.github.weisj.jsvg.renderer.impl.context.ContextElementAttributes;
40 import com.github.weisj.jsvg.renderer.impl.context.FontRenderContext;
41 import com.github.weisj.jsvg.renderer.impl.context.PaintContext;
42 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
43 import com.github.weisj.jsvg.renderer.output.Output;
44 import com.github.weisj.jsvg.view.FloatSize;
45 import com.github.weisj.jsvg.view.ViewBox;
46
47 public final class NodeRenderer {
48 private NodeRenderer() {}
49
50 public static void renderRootSVG(@NotNull SVG svgRoot, @NotNull RenderContext context, @NotNull Output output) {
51 RenderContext viewContext = svgRoot.createInnerContextForViewBox(
52 svgRoot.size(context), svgRoot.viewBox(context), context, output);
53 try (Info info = createRenderInfo(svgRoot, viewContext, output, null)) {
54 if (info != null) ((SVG) info.renderable()).renderWithEstablishedViewBox(info.context(), info.output());
55 }
56 }
57
58 public static void renderNode(@NotNull SVGNode node, @NotNull RenderContext context, @NotNull Output output) {
59 renderNode(node, context, output, null);
60 }
61
62 public static void renderNode(@NotNull SVGNode node, @NotNull RenderContext context, @NotNull Output output,
63 @Nullable Instantiator instantiator) {
64 try (Info info = createRenderInfo(node, context, output, instantiator)) {
65 if (info != null) info.renderable().render(info.context(), info.output());
66 }
67 }
68
69 public static void renderWithSize(@NotNull BaseInnerViewContainer node, @NotNull FloatSize size,
70 @NotNull RenderContext context, @NotNull Output output,
71 @Nullable Instantiator instantiator) {
72 try (Info info = createRenderInfo(node, context, output, instantiator)) {
73 if (info != null) node.renderWithSize(size, node.viewBox(info.context()), info.context(), info.output());
74 }
75 }
76
77 public static @NotNull RenderContext createChildContext(@NotNull Renderable node, @NotNull RenderContext context,
78 @Nullable Instantiator instantiator) {
79 return setupRenderContext(instantiator, node, context);
80 }
81
82 private static @Nullable Info createRenderInfo(@NotNull SVGNode node, @NotNull RenderContext context,
83 @NotNull Output output, @Nullable Instantiator instantiator) {
84 if (!(node instanceof Renderable)) return null;
85
86 Renderable renderable = (Renderable) node;
87
88 if (!checkInstantiation(node, instantiator, renderable)) return null;
89 if (!renderable.isVisible(context)) return null;
90
91 RenderContext childContext = createChildContext(renderable, context, instantiator);
92 Output childOutput = output.createChild();
93 ElementBounds elementBounds = new ElementBounds(node, childContext);
94
95 applyTransform(renderable, childOutput, childContext, elementBounds);
96
97 Mask maskForIsolation = null;
98 ClipPath clipPathForIsolation = null;
99 if (renderable instanceof HasClip) {
100 maskForIsolation = setupMask((HasClip) renderable, elementBounds, childOutput, childContext);
101
102 ClipPath clipPath = setupClip((HasClip) renderable, elementBounds, childContext, childOutput);
103 // Elements with an invalid clip shouldn't be painted
104 if (clipPath != null && !clipPath.isValid()) return null;
105
106 if (useAccurateMasking(childOutput)) {
107 clipPathForIsolation = clipPath;
108 }
109 }
110
111 Filter filter = null;
112 if (renderable instanceof HasFilter) {
113 filter = setupFilter((HasFilter) renderable, childOutput);
114 }
115
116 Info info = Info.InfoWithIsolation.create(renderable, childContext, childOutput, elementBounds,
117 new IsolationEffects(filter, maskForIsolation, clipPathForIsolation));
118 if (info != null) return info;
119
120 return new Info(renderable, childContext, childOutput);
121 }
122
123 private static void applyTransform(@NotNull Renderable renderable, @NotNull Output childOutput,
124 @NotNull RenderContext childContext, @NotNull ElementBounds elementBounds) {
125 if (renderable instanceof Transformable && ((Transformable) renderable).shouldTransform()) {
126 ((Transformable) renderable).applyTransform(childOutput, childContext, elementBounds);
127 }
128 }
129
130 private static boolean checkInstantiation(@NotNull SVGNode node, @Nullable Instantiator instantiator,
131 @NotNull Renderable renderable) {
132 boolean instantiated = renderable.requiresInstantiation();
133 return !instantiated || (instantiator != null && instantiator.canInstantiate(node));
134 }
135
136 private static @Nullable ClipPath setupClip(@NotNull HasClip renderable, @NotNull ElementBounds elementBounds,
137 @NotNull RenderContext childContext, @NotNull Output childOutput) {
138 ClipPath childClip = renderable.clipPath();
139 if (childClip == null) return null;
140 if (!childClip.isValid()) return childClip;
141
142 childClip.applyClip(childOutput, childContext, elementBounds);
143
144 return childClip;
145 }
146
147 private static @Nullable Mask setupMask(HasClip renderable, ElementBounds elementBounds, Output childOutput,
148 RenderContext childContext) {
149 Mask mask = renderable.mask();
150 if (mask == null) return null;
151
152 Rectangle2D bounds = elementBounds.geometryBox();
153 if (bounds.isEmpty()) return null;
154
155 if (useAccurateMasking(childOutput)) return mask;
156
157 childOutput.setPaint(() -> mask.createMaskPaint(childOutput, childContext, elementBounds));
158 return null;
159 }
160
161 private static @Nullable Filter setupFilter(@NotNull HasFilter hasFilter, @NotNull Output childOutput) {
162 Filter filter = hasFilter.filter();
163
164 if (filter != null && !(filter.hasEffect() && childOutput.supportsFilters())) {
165 filter = null;
166 }
167 return filter;
168 }
169
170 private static boolean useAccurateMasking(@NotNull Output output) {
171 return output.renderingHint(
172 SVGRenderingHints.KEY_MASK_CLIP_RENDERING) == SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY;
173 }
174
175 public static @NotNull RenderContext setupRenderContext(@NotNull Object node, @NotNull RenderContext context) {
176 return setupRenderContext(null, node, context);
177 }
178
179 private static @NotNull RenderContext setupRenderContext(@Nullable Instantiator instantiator, @NotNull Object node,
180 @NotNull RenderContext context) {
181 @Nullable Mutator<PaintContext> paintContext = null;
182 @Nullable Mutator<MeasurableFontSpec> fontSpec = null;
183 @Nullable FontRenderContext fontRenderContext = null;
184
185 if (node instanceof HasPaintContext) paintContext = ((HasPaintContext) node).paintContext();
186 if (node instanceof HasFontContext) fontSpec = ((HasFontContext) node).fontSpec();
187 if (node instanceof HasFontRenderContext) fontRenderContext = ((HasFontRenderContext) node).fontRenderContext();
188
189 @Nullable ContextElementAttributes contextElementAttributes = null;
190 if (instantiator != null) contextElementAttributes = instantiator.createContextAttributes(context);
191
192 return RenderContextAccessor.instance().deriveForNode(
193 context, paintContext, fontSpec, fontRenderContext, contextElementAttributes, node);
194 }
195
196 public static @NotNull RenderContext setupInnerViewRenderContext(@NotNull ViewBox viewBox,
197 @NotNull RenderContext context, boolean inheritAttributes) {
198 return RenderContextAccessor.instance().setupInnerViewRenderContext(viewBox, context, inheritAttributes);
199 }
200 }