Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/ShapeRenderer.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.*;
25 import java.awt.geom.*;
26 import java.util.Set;
27
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30
31 import com.github.weisj.jsvg.attributes.MarkerOrientation;
32 import com.github.weisj.jsvg.attributes.PaintOrder;
33 import com.github.weisj.jsvg.attributes.VectorEffect;
34 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
35 import com.github.weisj.jsvg.nodes.Marker;
36 import com.github.weisj.jsvg.nodes.ShapeNode;
37 import com.github.weisj.jsvg.paint.SVGPaint;
38 import com.github.weisj.jsvg.renderer.RenderContext;
39 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
40 import com.github.weisj.jsvg.renderer.output.Output;
41 import com.github.weisj.jsvg.view.FloatSize;
42
43 public final class ShapeRenderer {
44 private static final boolean DEBUG_MARKERS = false;
45
46 private ShapeRenderer() {}
47
48 private static final class PaintWithOpacity {
49 private final @NotNull SVGPaint paint;
50 private final float opacity;
51
52 private PaintWithOpacity(@NotNull SVGPaint paint, float opacity) {
53 this.paint = paint;
54 this.opacity = opacity;
55 }
56
57 boolean isVisible(@NotNull RenderContext context) {
58 return opacity > 0 && paint.isVisible(context);
59 }
60 }
61
62 public static final class PaintShape {
63 private final @NotNull Shape shape;
64 private final @Nullable Rectangle2D bounds;
65
66 public PaintShape(@NotNull Shape shape, @Nullable Rectangle2D bounds) {
67 this.shape = shape;
68 this.bounds = bounds;
69 }
70 }
71
72 public static final class ShapePaintContext {
73 private final @NotNull RenderContext context;
74 private final @NotNull Set<VectorEffect> vectorEffects;
75 private final @NotNull Stroke stroke;
76 private final @Nullable AffineTransform transform;
77
78 public ShapePaintContext(@NotNull RenderContext context, @NotNull Set<VectorEffect> vectorEffects,
79 @NotNull Stroke stroke, @Nullable AffineTransform transform) {
80 this.context = context;
81 this.vectorEffects = vectorEffects;
82 this.stroke = stroke;
83 this.transform = transform;
84 }
85 }
86
87 public static final class ShapeMarkerInfo {
88 private final @NotNull ShapeNode node;
89 private final @Nullable Marker markerStart;
90 private final @Nullable Marker markerMid;
91 private final @Nullable Marker markerEnd;
92 private final boolean shouldPaintStartEndMarkersInMiddle;
93
94 public ShapeMarkerInfo(@NotNull ShapeNode node, @Nullable Marker markerStart, @Nullable Marker markerMid,
95 @Nullable Marker markerEnd, boolean shouldPaintStartEndMarkersInMiddle) {
96 this.node = node;
97 this.markerStart = markerStart;
98 this.markerMid = markerMid;
99 this.markerEnd = markerEnd;
100 this.shouldPaintStartEndMarkersInMiddle = shouldPaintStartEndMarkersInMiddle;
101 }
102 }
103
104 public static void renderWithPaintOrder(@NotNull Output output, boolean canBeFilledHint,
105 @NotNull PaintOrder paintOrder, @NotNull ShapePaintContext shapePaintContext,
106 @NotNull PaintShape paintShape, @Nullable ShapeMarkerInfo markerInfo) {
107 Set<VectorEffect> vectorEffects = shapePaintContext.vectorEffects;
108 VectorEffect.applyEffects(shapePaintContext.vectorEffects, output,
109 shapePaintContext.context, shapePaintContext.transform);
110 Output.SafeState safeState = output.safeState();
111
112 // FIXME: Opacity should be uniform on fill and stroke. Fill shouldn't be visible beneath stroke.
113 boolean fillPainted = false;
114 for (PaintOrder.Phase phase : paintOrder.phases()) {
115 RenderContext phaseContext =
116 RenderContextAccessor.instance().deriveForChildGraphics(shapePaintContext.context);
117 switch (phase) {
118 case FILL:
119 if (canBeFilledHint) {
120 ShapeRenderer.renderShapeFill(phaseContext, output, paintShape);
121 }
122 fillPainted = true;
123 break;
124 case STROKE:
125 PaintShape strokeShape = null;
126 Shape shape = paintShape.shape;
127 if (vectorEffects.contains(VectorEffect.NonScalingStroke)
128 && !vectorEffects.contains(VectorEffect.NonScalingSize)) {
129 Shape stroked = VectorEffect.applyNonScalingStroke(
130 output, phaseContext, shapePaintContext.stroke, paintShape.shape);
131 strokeShape = new PaintShape(stroked, stroked.getBounds2D());
132 }
133 ShapeRenderer.renderShapeStroke(phaseContext, output,
134 new PaintShape(shape, paintShape.bounds),
135 strokeShape, shapePaintContext.stroke,
136 !fillPainted && canBeFilledHint);
137 break;
138 case MARKERS:
139 if (markerInfo != null) renderMarkers(output, phaseContext, paintShape, markerInfo);
140 break;
141 }
142 safeState.restore();
143 }
144 }
145
146 private static void renderMarkers(@NotNull Output output, @NotNull RenderContext context,
147 @NotNull PaintShape paintShape, @NotNull ShapeMarkerInfo markerInfo) {
148 if (markerInfo.markerStart == null && markerInfo.markerMid == null && markerInfo.markerEnd == null) return;
149 renderMarkersImpl(output, context, paintShape.shape.getPathIterator(null), markerInfo);
150 }
151
152 private static void renderShapeStroke(@NotNull RenderContext context, @NotNull Output output,
153 @NotNull PaintShape paintShape, @Nullable PaintShape strokeShape,
154 @Nullable Stroke stroke, boolean willBeFilledAfterwards) {
155 PaintWithOpacity paintWithOpacity = new PaintWithOpacity(context.strokePaint(), context.strokeOpacity());
156 if (!(stroke != null && paintWithOpacity.isVisible(context))) return;
157 output.applyOpacity(paintWithOpacity.opacity);
158 output.setStroke(stroke);
159
160 PaintShape strokeShapeToPaint =
161 computeEffectiveStrokeShape(context, output, paintShape, strokeShape, stroke, willBeFilledAfterwards);
162
163 if (strokeShapeToPaint != null) {
164 paintWithOpacity.paint.fillShape(output, context, strokeShapeToPaint.shape, strokeShapeToPaint.bounds);
165 } else {
166 paintWithOpacity.paint.drawShape(output, context, paintShape.shape, paintShape.bounds);
167 }
168
169 }
170
171 private static PaintShape computeEffectiveStrokeShape(@NotNull RenderContext context, @NotNull Output output,
172 @NotNull PaintShape paintShape, @Nullable PaintShape strokeShape, @NotNull Stroke stroke,
173 boolean willBeFilledAfterwards) {
174 PaintShape strokeShapeToPaint = strokeShape;
175
176 boolean removeFillArea = output.hasMaskedPaint()
177 && willBeFilledAfterwards
178 && context.fillPaint().isVisible(context)
179 && context.fillOpacity() == 1
180 && output.currentOpacity() == 1;
181 if (removeFillArea) {
182 Area s = new Area(strokeShape != null
183 ? strokeShape.shape
184 : stroke.createStrokedShape(paintShape.shape));
185 s.subtract(new Area(paintShape.shape));
186 Rectangle2D strokedBounds = paintShape.bounds;
187 if (strokedBounds != null) {
188 if (stroke instanceof BasicStroke) {
189 strokedBounds = GeometryUtil.grow(strokedBounds, ((BasicStroke) stroke).getLineWidth() / 2);
190 } else {
191 strokedBounds = s.getBounds2D();
192 }
193 }
194 strokeShapeToPaint = new PaintShape(s, strokedBounds);
195 }
196 return strokeShapeToPaint;
197 }
198
199 private static void renderShapeFill(@NotNull RenderContext context, @NotNull Output output,
200 @NotNull PaintShape paintShape) {
201 PaintWithOpacity paintWithOpacity = new PaintWithOpacity(context.fillPaint(), context.fillOpacity());
202 if (!paintWithOpacity.isVisible(context)) return;
203 output.applyOpacity(paintWithOpacity.opacity);
204 paintWithOpacity.paint.fillShape(output, context, paintShape.shape, paintShape.bounds);
205 }
206
207 private static void renderMarkersImpl(@NotNull Output output, @NotNull RenderContext context,
208 @NotNull PathIterator iterator, @NotNull ShapeMarkerInfo markerInfo) {
209 float[] args = new float[6];
210
211 float x = 0;
212 float y = 0;
213 float xStart = 0;
214 float yStart = 0;
215
216 float dxIn = 0;
217 float dyIn = 0;
218 float dxOut;
219 float dyOut;
220
221 Marker start = markerInfo.markerStart;
222 Marker mid = markerInfo.markerMid;
223 Marker end = markerInfo.markerEnd;
224
225 boolean onlyFirst = mid == null && end == null;
226
227 Marker markerToPaint = null;
228 MarkerOrientation.MarkerType markerToPaintType = null;
229
230 while (!iterator.isDone()) {
231 int type = iterator.currentSegment(args);
232 iterator.next();
233
234 Marker nextMarker = iterator.isDone()
235 ? end
236 : mid;
237 MarkerOrientation.MarkerType nextMarkerType = iterator.isDone()
238 ? MarkerOrientation.MarkerType.END
239 : MarkerOrientation.MarkerType.MID;
240
241 float xPaint = x;
242 float yPaint = y;
243 float dx = dxIn;
244 float dy = dyIn;
245
246 switch (type) {
247 case PathIterator.SEG_MOVETO:
248 dxIn = 0;
249 dyIn = 0;
250 x = xStart = args[0];
251 y = yStart = args[1];
252 if (markerInfo.shouldPaintStartEndMarkersInMiddle || markerToPaint == null) {
253 nextMarker = start;
254 nextMarkerType = MarkerOrientation.MarkerType.START;
255 }
256 if (markerToPaint != null) {
257 paintSingleMarker(markerInfo.node, context, output, markerToPaintType, markerToPaint,
258 xPaint, yPaint, 0, 0, dx, dy);
259 if (onlyFirst) return;
260 }
261 markerToPaint = nextMarker;
262 markerToPaintType = nextMarkerType;
263 continue;
264 case PathIterator.SEG_LINETO:
265 dxOut = dxIn = args[0] - x;
266 dyOut = dyIn = args[1] - y;
267 x = args[0];
268 y = args[1];
269 break;
270 case PathIterator.SEG_QUADTO:
271 dxOut = args[0] - x;
272 dyOut = args[1] - y;
273 dxIn = args[2] - args[0];
274 dyIn = args[3] - args[1];
275 x = args[2];
276 y = args[3];
277 break;
278 case PathIterator.SEG_CUBICTO:
279 dxOut = args[0] - x;
280 dyOut = args[1] - y;
281 dxIn = args[4] - args[2];
282 dyIn = args[5] - args[3];
283 x = args[4];
284 y = args[5];
285 break;
286 case PathIterator.SEG_CLOSE:
287 dxOut = dxIn = xStart - x;
288 dyOut = dyIn = yStart - y;
289 x = xStart;
290 y = yStart;
291 if (markerInfo.shouldPaintStartEndMarkersInMiddle) {
292 nextMarker = end;
293 nextMarkerType = MarkerOrientation.MarkerType.END;
294 }
295 break;
296 default:
297 throw new IllegalStateException();
298 }
299
300 paintSingleMarker(markerInfo.node, context, output, markerToPaintType, markerToPaint,
301 xPaint, yPaint, dx, dy, dxOut, dyOut);
302 if (onlyFirst) return;
303
304 markerToPaint = nextMarker;
305 markerToPaintType = nextMarkerType;
306 }
307 paintSingleMarker(markerInfo.node, context, output, markerToPaintType, markerToPaint,
308 x, y, dxIn, dyIn, 0, 0);
309 }
310
311 public static void paintSingleMarker(@NotNull ShapeNode shapeNode, @NotNull RenderContext context,
312 @NotNull Output output, @Nullable MarkerOrientation.MarkerType type, @Nullable Marker marker,
313 float x, float y, float dxIn, float dyIn, float dxOut, float dyOut) {
314 if (marker == null) return;
315 assert type != null;
316
317 MarkerOrientation orientation = marker.orientation();
318 float rotation = orientation.orientationFor(type, dxIn, dyIn, dxOut, dyOut);
319
320 Output markerOutput = output.createChild();
321 RenderContext markerContext = RenderContextAccessor.instance().deriveForChildGraphics(context);
322
323 markerContext.translate(markerOutput, x, y);
324
325 if (DEBUG_MARKERS) {
326 markerOutput.debugPaint(g -> paintDebugMarker(markerContext, g, marker, rotation));
327 }
328 markerContext.rotate(markerOutput, rotation);
329
330 NodeRenderer.renderNode(marker, markerContext, markerOutput, shapeNode);
331
332 markerOutput.dispose();
333 }
334
335 private static void paintDebugMarker(@NotNull RenderContext context, @NotNull Graphics2D g,
336 @NotNull Marker marker, float rotation) {
337 FloatSize size = marker.size(context);
338
339 Path2D p = new Path2D.Float();
340 p.moveTo(0, size.height / 2f);
341 p.lineTo(size.width, size.height / 2f);
342 p.moveTo(0.8 * size.width, 0.35f * size.height);
343 p.lineTo(size.width, size.height / 2f);
344 p.lineTo(0.8 * size.width, 0.65f * size.height);
345
346 g.setStroke(new BasicStroke(0.5f));
347
348 g.setColor(Color.MAGENTA.darker().darker());
349 g.draw(new Rectangle2D.Float(0, 0, size.width, size.height));
350 g.draw(p);
351
352 g.rotate(rotation);
353
354 g.setColor(Color.MAGENTA);
355 g.draw(new Rectangle2D.Float(0, 0, size.width, size.height));
356 g.draw(p);
357 }
358 }