Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/Info.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-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.*;
25 import java.awt.geom.Rectangle2D;
26 import java.awt.image.BufferedImage;
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.nodes.ClipPath;
33 import com.github.weisj.jsvg.nodes.Mask;
34 import com.github.weisj.jsvg.nodes.filter.Filter;
35 import com.github.weisj.jsvg.nodes.prototype.Renderable;
36 import com.github.weisj.jsvg.renderer.RenderContext;
37 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
38 import com.github.weisj.jsvg.renderer.output.Output;
39 import com.github.weisj.jsvg.renderer.output.impl.Graphics2DOutput;
40 import com.github.weisj.jsvg.util.BlittableImage;
41 import com.github.weisj.jsvg.util.ImageUtil;
42
43 class Info implements AutoCloseable {
44 protected final @NotNull RenderContext context;
45 protected final @NotNull Output output;
46 private final @NotNull Renderable renderable;
47
48 Info(@NotNull Renderable renderable, @NotNull RenderContext context, @NotNull Output output) {
49 this.renderable = renderable;
50 this.context = context;
51 this.output = output;
52 }
53
54 public @NotNull Renderable renderable() {
55 return renderable;
56 }
57
58 public @NotNull Output output() {
59 return output;
60 }
61
62 public @NotNull RenderContext context() {
63 return context;
64 }
65
66 @Override
67 public void close() {
68 output.dispose();
69 }
70
71 static final class InfoWithIsolation extends Info {
72
73 private final @NotNull BlittableImage blittableImage;
74 private final @NotNull Output imageOutput;
75 private final @NotNull ElementBounds elementBounds;
76 private final @NotNull IsolationEffects isolationEffects;
77 private final @Nullable Filter.FilterInfo filterInfo;
78
79 static @Nullable InfoWithIsolation create(@NotNull Renderable renderable,
80 @NotNull RenderContext context, @NotNull Output output,
81 @NotNull ElementBounds elementBounds, @NotNull IsolationEffects effects) {
82
83 Rectangle2D clipBounds = null;
84 Rectangle2D bounds = null;
85 Filter.FilterBounds filterBounds = null;
86 Filter.FilterInfo filterInfo = null;
87
88 Filter filter = effects.filter;
89 Mask mask = effects.mask;
90 ClipPath clipPath = effects.clipPath;
91
92 if (filter != null) {
93 filterBounds = filter.createFilterBounds(output, context, elementBounds);
94 if (filterBounds != null) {
95 bounds = filterBounds.filterRegion();
96 clipBounds = filterBounds.effectiveFilterArea();
97 }
98 }
99 if (mask != null || clipPath != null) {
100 bounds = elementBounds.geometryBox();
101 }
102
103 if (bounds == null) {
104 return null;
105 }
106
107 RenderContext imageContext = RenderContextAccessor.instance().deriveForSurface(context);
108
109 BlittableImage blitImage = BlittableImage.create(
110 ImageUtil::createCompatibleTransparentImage, context, clipBounds,
111 bounds, elementBounds.boundingBox(), UnitType.UserSpaceOnUse, imageContext);
112 if (blitImage == null) return null;
113
114 Graphics2D g = blitImage.createGraphics();
115 g.setRenderingHints(output.renderingHints());
116 Output imageOutput = new Graphics2DOutput(g);
117
118 if (filter != null && filterBounds != null) {
119 filterInfo = new Filter.FilterInfo(blitImage, imageOutput, filterBounds);
120 }
121
122 return new InfoWithIsolation(
123 renderable, context, output, imageOutput, blitImage, elementBounds, effects, filterInfo);
124 }
125
126 private InfoWithIsolation(@NotNull Renderable renderable, @NotNull RenderContext context,
127 @NotNull Output output, @NotNull Output imageOutput,
128 @NotNull BlittableImage blittableImage,
129 @NotNull ElementBounds elementBounds,
130 @NotNull IsolationEffects isolationEffects, @Nullable Filter.FilterInfo filterInfo) {
131 super(renderable, context, output);
132 this.blittableImage = blittableImage;
133 this.imageOutput = imageOutput;
134 this.elementBounds = elementBounds;
135 this.isolationEffects = isolationEffects;
136 this.filterInfo = filterInfo;
137 }
138
139 @Override
140 public @NotNull Output output() {
141 return imageOutput;
142 }
143
144 @Override
145 public @NotNull RenderContext context() {
146 return blittableImage.context();
147 }
148
149 @Override
150 public void close() {
151 Output previousOutput = this.output;
152 BufferedImage result = this.blittableImage.image();
153
154 if (isolationEffects.filter != null) {
155 assert filterInfo != null;
156 result = isolationEffects.filter.applyFilter(previousOutput, context, filterInfo);
157 previousOutput.applyClip(filterInfo.filterRegion());
158 }
159 if (isolationEffects.mask != null) {
160 previousOutput.setPaint(() -> isolationEffects.mask.createMaskPaint(
161 previousOutput, context, elementBounds));
162 }
163 if (isolationEffects.clipPath != null) {
164 isolationEffects.clipPath.applyClip(previousOutput, context, elementBounds);
165 }
166
167 blittableImage.prepareForBlitting(previousOutput);
168 previousOutput.drawImage(result, context.platformSupport().imageObserver());
169 imageOutput.dispose();
170 super.close();
171 }
172 }
173 }