Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/ElementBounds.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.geom.Rectangle2D;
25
26 import org.jetbrains.annotations.NotNull;
27
28 import com.github.weisj.jsvg.nodes.SVGNode;
29 import com.github.weisj.jsvg.nodes.filter.Filter;
30 import com.github.weisj.jsvg.nodes.prototype.HasFilter;
31 import com.github.weisj.jsvg.nodes.prototype.HasShape;
32 import com.github.weisj.jsvg.renderer.MeasureContext;
33 import com.github.weisj.jsvg.renderer.RenderContext;
34 import com.github.weisj.jsvg.view.ViewBox;
35
36 public class ElementBounds {
37
38 private final @NotNull SVGNode node;
39 private final RenderContext context;
40
41 private Rectangle2D boundingBox;
42 private Rectangle2D strokeBox;
43 private Rectangle2D geometryBox;
44
45 public ElementBounds(@NotNull SVGNode node, RenderContext context) {
46 this.node = node;
47 this.context = context;
48 }
49
50 public static @NotNull ElementBounds fromUntransformedBounds(@NotNull SVGNode node, @NotNull RenderContext context,
51 @NotNull Rectangle2D bounds, HasShape.Box box) {
52 ElementBounds elementBounds = new ElementBounds(node, context);
53 switch (box) {
54 case BoundingBox:
55 elementBounds.boundingBox = bounds;
56 break;
57 case StrokeBox:
58 elementBounds.strokeBox = bounds;
59 break;
60 default:
61 throw new IllegalStateException("Unexpected value: " + box);
62 }
63 return elementBounds;
64 }
65
66 public @NotNull Rectangle2D boundingBox() {
67 if (boundingBox == null) {
68 boundingBox = elementBounds(node, context, HasShape.Box.BoundingBox);
69 }
70 return boundingBox;
71 }
72
73 public @NotNull Rectangle2D geometryBox() {
74 if (geometryBox == null) {
75 geometryBox = strokeBox();
76 if (node instanceof HasFilter) {
77 geometryBox = filterBounds((HasFilter) node, context, geometryBox);
78 }
79 }
80 return geometryBox;
81 }
82
83 public @NotNull Rectangle2D strokeBox() {
84 if (strokeBox == null) {
85 strokeBox = elementBounds(node, context, HasShape.Box.StrokeBox);
86 }
87 return strokeBox;
88 }
89
90 public @NotNull Rectangle2D fillBox() {
91 return boundingBox();
92 }
93
94 private static @NotNull Rectangle2D elementBounds(@NotNull SVGNode node, @NotNull RenderContext context,
95 HasShape.Box box) {
96 Rectangle2D elementBounds;
97 if (node instanceof HasShape) {
98 elementBounds = ((HasShape) node).untransformedElementBounds(context, box);
99 } else {
100 MeasureContext measureContext = context.measureContext();
101 elementBounds = new ViewBox(measureContext.viewWidth(), measureContext.viewHeight());
102 }
103 return elementBounds;
104 }
105
106 private @NotNull Rectangle2D filterBounds(@NotNull HasFilter node, @NotNull RenderContext context,
107 @NotNull Rectangle2D elementBounds) {
108 Filter filter = node.filter();
109 if (filter == null) return elementBounds;
110 Filter.FilterBounds filterBounds = filter.createFilterBounds(null, context, this);
111 if (filterBounds == null) return elementBounds;
112 return elementBounds.createUnion(filterBounds.effectiveFilterArea());
113 }
114
115 }