Debian Codegraph @

src/com/github/weisj/jsvg/util/ShapeUtil.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024 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.util;
23
24 import java.awt.*;
25 import java.awt.geom.*;
26
27 import org.jetbrains.annotations.NotNull;
28
29 public final class ShapeUtil {
30
31 private static final int NON_RECTILINEAR_TRANSFORM_MASK =
32 AffineTransform.TYPE_GENERAL_TRANSFORM | AffineTransform.TYPE_GENERAL_ROTATION;
33
34 private ShapeUtil() {}
35
36 public static boolean isInvalidArea(@NotNull Rectangle2D area) {
37 return area.isEmpty() || Double.isNaN(area.getWidth()) || Double.isNaN(area.getHeight());
38 }
39
40 /*
41 * Intersect two Shapes by the simplest method, attempting to produce a simplified result. The
42 * boolean arguments keep1 and keep2 specify whether or not the first or second shapes can be
43 * modified during the operation or whether that shape must be "kept" unmodified.
44 */
45 @NotNull
46 public static Shape intersect(@NotNull Shape s1, @NotNull Shape s2, boolean keep1, boolean keep2) {
47 if (s1 instanceof Rectangle && s2 instanceof Rectangle) {
48 return ((Rectangle) s1).intersection((Rectangle) s2);
49 }
50 if (s1 instanceof Rectangle2D) {
51 return intersectRectShape((Rectangle2D) s1, s2, keep1, keep2);
52 } else if (s2 instanceof Rectangle2D) {
53 return intersectRectShape((Rectangle2D) s2, s1, keep2, keep1);
54 }
55 return intersectByArea(s1, s2, keep1, keep2);
56 }
57
58 /*
59 * Intersect a Rectangle with a Shape by the simplest method, attempting to produce a simplified
60 * result. The boolean arguments keep1 and keep2 specify whether or not the first or second shapes
61 * can be modified during the operation or whether that shape must be "kept" unmodified.
62 */
63 @NotNull
64 private static Shape intersectRectShape(@NotNull Rectangle2D r, @NotNull Shape s,
65 boolean keep1, boolean keep2) {
66 if (s instanceof Rectangle2D) {
67 Rectangle2D r2 = (Rectangle2D) s;
68 Rectangle2D outputRect;
69 if (!keep1) {
70 outputRect = r;
71 } else if (!keep2) {
72 outputRect = r2;
73 } else {
74 outputRect = new Rectangle2D.Float();
75 }
76 double x1 = Math.max(r.getX(), r2.getX());
77 double x2 = Math.min(r.getX() + r.getWidth(), r2.getX() + r2.getWidth());
78 double y1 = Math.max(r.getY(), r2.getY());
79 double y2 = Math.min(r.getY() + r.getHeight(), r2.getY() + r2.getHeight());
80
81 if (((x2 - x1) < 0) || ((y2 - y1) < 0))
82 // Width or height is negative. No intersection.
83 outputRect.setFrameFromDiagonal(0, 0, 0, 0);
84 else
85 outputRect.setFrameFromDiagonal(x1, y1, x2, y2);
86 return outputRect;
87 }
88 if (r.contains(s.getBounds2D())) {
89 if (keep2) {
90 s = cloneShape(s);
91 }
92 return s;
93 }
94 return intersectByArea(r, s, keep1, keep2);
95 }
96
97 /*
98 * Intersect two Shapes using the Area class. Presumably other attempts at simpler intersection
99 * methods proved fruitless. The boolean arguments keep1 and keep2 specify whether or not the first
100 * or second shapes can be modified during the operation or whether that shape must be "kept"
101 * unmodified.
102 */
103 @NotNull
104 private static Shape intersectByArea(@NotNull Shape s1, @NotNull Shape s2, boolean keep1, boolean keep2) {
105 Area a1;
106 Area a2;
107
108 // First see if we can find an overwrite-able source shape
109 // to use as our destination area to avoid duplication.
110 if (!keep1 && (s1 instanceof Area)) {
111 a1 = (Area) s1;
112 } else if (!keep2 && (s2 instanceof Area)) {
113 a1 = (Area) s2;
114 s2 = s1;
115 } else {
116 a1 = new Area(s1);
117 }
118
119 if (s2 instanceof Area) {
120 a2 = (Area) s2;
121 } else {
122 a2 = new Area(s2);
123 }
124
125 a1.intersect(a2);
126 if (a1.isRectangular()) {
127 return a1.getBounds2D();
128 }
129
130 return a1;
131 }
132
133 public static @NotNull Shape transformShape(@NotNull Shape s, @NotNull AffineTransform transform) {
134 if (transform.getType() > AffineTransform.TYPE_TRANSLATION) {
135 return transformShape(transform, s);
136 } else {
137 return transformShape(transform.getTranslateX(), transform.getTranslateY(), s);
138 }
139 }
140
141 private static Shape transformShape(@NotNull AffineTransform tx, @NotNull Shape shape) {
142 if (shape instanceof Rectangle2D &&
143 (tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0) {
144 Rectangle2D rect = (Rectangle2D) shape;
145 double[] matrix = new double[4];
146 matrix[0] = rect.getX();
147 matrix[1] = rect.getY();
148 matrix[2] = matrix[0] + rect.getWidth();
149 matrix[3] = matrix[1] + rect.getHeight();
150 tx.transform(matrix, 0, matrix, 0, 2);
151 fixRectangleOrientation(matrix, rect);
152 return new Rectangle2D.Double(matrix[0], matrix[1],
153 matrix[2] - matrix[0],
154 matrix[3] - matrix[1]);
155 }
156
157 if (tx.isIdentity()) {
158 return cloneShape(shape);
159 }
160
161 return tx.createTransformedShape(shape);
162 }
163
164 private static void fixRectangleOrientation(double[] m, @NotNull Rectangle2D r) {
165 if (r.getWidth() > 0 != (m[2] - m[0] > 0)) {
166 double t = m[0];
167 m[0] = m[2];
168 m[2] = t;
169 }
170 if (r.getHeight() > 0 != (m[3] - m[1] > 0)) {
171 double t = m[1];
172 m[1] = m[3];
173 m[3] = t;
174 }
175 }
176
177 private static @NotNull Shape transformShape(double tx, double ty, @NotNull Shape s) {
178 if (s instanceof Rectangle2D) {
179 Rectangle2D rect = (Rectangle2D) s;
180 return new Rectangle2D.Double(
181 rect.getX() + tx,
182 rect.getY() + ty,
183 rect.getWidth(),
184 rect.getHeight());
185 }
186
187 if (tx == 0 && ty == 0) {
188 return ShapeUtil.cloneShape(s);
189 }
190
191 AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
192 return mat.createTransformedShape(s);
193 }
194
195 private static Shape cloneShape(Shape s) {
196 return new GeneralPath(s);
197 }
198 }