Debian Codegraph @

src/com/github/weisj/jsvg/renderer/output/impl/ShapeOutput.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.output.impl;
23
24 import java.awt.*;
25 import java.awt.geom.AffineTransform;
26 import java.awt.geom.Area;
27 import java.awt.geom.Rectangle2D;
28 import java.awt.image.BufferedImage;
29 import java.awt.image.ImageObserver;
30 import java.util.Optional;
31 import java.util.function.Consumer;
32 import java.util.function.Supplier;
33
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36
37 import com.github.weisj.jsvg.renderer.output.Output;
38 import com.github.weisj.jsvg.util.ShapeUtil;
39
40
41 public class ShapeOutput implements Output {
42
43 private final @NotNull Area accumulatorShape;
44 private @NotNull AffineTransform currentTransform;
45 private @NotNull Stroke currentStroke;
46 private @Nullable Shape currentClip;
47
48 public ShapeOutput(@NotNull Area area) {
49 accumulatorShape = area;
50 currentStroke = new BasicStroke();
51 currentTransform = new AffineTransform();
52 currentClip = null;
53 }
54
55 private ShapeOutput(@NotNull ShapeOutput parent) {
56 accumulatorShape = parent.accumulatorShape;
57 currentStroke = parent.currentStroke;
58 currentTransform = new AffineTransform(parent.currentTransform);
59 currentClip = parent.currentClip != null ? new Area(parent.currentClip) : null;
60 }
61
62 private void addShape(@NotNull Shape shape) {
63 // NOTE: ShapeUtil.transformShape always returns a new shape hence we can safely modify shape.
64 Shape s = currentClip != null
65 ? ShapeUtil.intersect(currentClip, shape, true, false)
66 : shape;
67 accumulatorShape.add(new Area(s));
68 }
69
70 private void append(@NotNull Shape shape, @NotNull AffineTransform transform) {
71 AffineTransform at = new AffineTransform(currentTransform);
72 at.concatenate(transform);
73 addShape(ShapeUtil.transformShape(shape, at));
74 }
75
76 private void append(@NotNull Shape shape) {
77 addShape(ShapeUtil.transformShape(shape, currentTransform));
78 }
79
80 @Override
81 public void fillShape(@NotNull Shape shape) {
82 append(shape);
83 }
84
85
86 @Override
87 public void drawShape(@NotNull Shape shape) {
88 append(currentStroke.createStrokedShape(shape));
89 }
90
91 @Override
92 public void drawImage(@NotNull BufferedImage image) {
93 append(new Rectangle2D.Float(0, 0, image.getWidth(), image.getHeight()));
94 }
95
96 @Override
97 public void drawImage(@NotNull Image image, @Nullable ImageObserver observer) {
98 append(new Rectangle2D.Float(0, 0, image.getWidth(null), image.getHeight(null)));
99 }
100
101 @Override
102 public void drawImage(@NotNull Image image, @NotNull AffineTransform at, @Nullable ImageObserver observer) {
103 append(new Rectangle2D.Float(0, 0, image.getWidth(null), image.getHeight(null)), at);
104 }
105
106 @Override
107 public void setPaint(@NotNull Paint paint) {
108 // Not supported. Do nothing
109 }
110
111 @Override
112 public void setPaint(@NotNull Supplier<Paint> paintProvider) {
113 // Not supported. Do nothing
114 }
115
116 @Override
117 public void setStroke(@NotNull Stroke stroke) {
118 currentStroke = stroke;
119 }
120
121 @Override
122 public @NotNull Stroke stroke() {
123 return currentStroke;
124 }
125
126 @Override
127 public void applyClip(@NotNull Shape clipShape) {
128 Shape transformedShape = ShapeUtil.transformShape(clipShape, currentTransform);
129 if (currentClip != null) {
130 currentClip = ShapeUtil.intersect(currentClip, transformedShape, true, false);
131 } else {
132 currentClip = transformedShape;
133 }
134 }
135
136 @Override
137 public void setClip(@Nullable Shape shape) {
138 currentClip = shape != null
139 ? ShapeUtil.transformShape(shape, currentTransform)
140 : null;
141 }
142
143 @Override
144 public Optional<Float> contextFontSize() {
145 return Optional.empty();
146 }
147
148 @Override
149 public @NotNull Output createChild() {
150 return new ShapeOutput(this);
151 }
152
153 @Override
154 public void dispose() {
155 // No action needed
156 }
157
158 @Override
159 public void debugPaint(@NotNull Consumer<Graphics2D> painter) {
160 // Not supported. Do nothing
161 }
162
163 @Override
164 public @NotNull Rectangle2D clipBounds() {
165 float veryLargeNumber = Float.MAX_VALUE / 4;
166 return currentClip != null ? currentClip.getBounds2D()
167 : new Rectangle2D.Float(-veryLargeNumber, -veryLargeNumber, 2 * veryLargeNumber, 2 * veryLargeNumber);
168 }
169
170 @Override
171 public @Nullable RenderingHints renderingHints() {
172 return null;
173 }
174
175 @Override
176 public @Nullable Object renderingHint(RenderingHints.@NotNull Key key) {
177 return null;
178 }
179
180 @Override
181 public void setRenderingHint(RenderingHints.@NotNull Key key, @Nullable Object value) {
182 // Not supported. Do nothing
183 }
184
185 @Override
186 public @NotNull AffineTransform transform() {
187 return new AffineTransform(currentTransform);
188 }
189
190 @Override
191 public void setTransform(@NotNull AffineTransform affineTransform) {
192 currentTransform = new AffineTransform(affineTransform);
193 }
194
195 @Override
196 public void applyTransform(@NotNull AffineTransform transform) {
197 currentTransform.concatenate(transform);
198 }
199
200 @Override
201 public void rotate(double angle) {
202 currentTransform.rotate(angle);
203 }
204
205 @Override
206 public void scale(double sx, double sy) {
207 currentTransform.scale(sx, sy);
208 }
209
210 @Override
211 public void translate(double dx, double dy) {
212 currentTransform.translate(dx, dy);
213 }
214
215 @Override
216 public float currentOpacity() {
217 return 1;
218 }
219
220 @Override
221 public void applyOpacity(float opacity) {
222 // Not supported. Do nothing
223 }
224
225 @Override
226 public @NotNull SafeState safeState() {
227 return new ShapeOutputSafeState(this);
228 }
229
230 @Override
231 public boolean supportsFilters() {
232 return false;
233 }
234
235 @Override
236 public boolean supportsColors() {
237 return false;
238 }
239
240 @Override
241 public boolean isSoftClippingEnabled() {
242 // Not needed here. Always return false
243 return false;
244 }
245
246 private static class ShapeOutputSafeState implements SafeState {
247 private final @NotNull ShapeOutput shapeOutput;
248 private final @NotNull Stroke oldStroke;
249 private final @NotNull AffineTransform oldTransform;
250 private final @Nullable Area oldClip;
251
252 private ShapeOutputSafeState(@NotNull ShapeOutput shapeOutput) {
253 this.shapeOutput = shapeOutput;
254 this.oldStroke = shapeOutput.stroke();
255 this.oldTransform = shapeOutput.transform();
256 this.oldClip = shapeOutput.currentClip != null ? new Area(shapeOutput.currentClip) : null;
257 }
258
259 @Override
260 public void restore() {
261 shapeOutput.currentStroke = oldStroke;
262 shapeOutput.currentTransform = oldTransform;
263 shapeOutput.currentClip = oldClip;
264 }
265 }
266 }