Debian Codegraph @

src/com/github/weisj/jsvg/geometry/AWTSVGShape.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.geometry;
23
24 import java.awt.*;
25 import java.awt.geom.*;
26
27 import org.jetbrains.annotations.NotNull;
28
29 import com.github.weisj.jsvg.attributes.value.ConstantValue;
30 import com.github.weisj.jsvg.attributes.value.Value;
31 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
32 import com.github.weisj.jsvg.renderer.RenderContext;
33
34 public class AWTSVGShape<T extends Shape> implements SVGShape {
35 public static final Rectangle2D EMPTY_SHAPE = new Rectangle();
36 protected final @NotNull Value<@NotNull T> shapeValue;
37 private Rectangle2D boundsCache;
38 private T shapeCache;
39
40 private double pathLength;
41
42 public AWTSVGShape(@NotNull T shape) {
43 this(new ConstantValue<>(shape));
44 }
45
46 public AWTSVGShape(@NotNull Value<@NotNull T> shapeValue) {
47 this(shapeValue, Double.NaN);
48 }
49
50 private AWTSVGShape(@NotNull Value<@NotNull T> shapeValue, double pathLength) {
51 this.shapeValue = shapeValue;
52 this.pathLength = pathLength;
53 }
54
55 @Override
56 public @NotNull T shape(@NotNull RenderContext context, boolean validate) {
57 if (shapeCache == null || validate) {
58 shapeCache = shapeValue.get(context.measureContext());
59 }
60 return shapeCache;
61 }
62
63 @Override
64 public @NotNull Rectangle2D bounds(@NotNull RenderContext context, boolean validate) {
65 if (boundsCache == null || validate) {
66 Shape shape = shape(context, validate);
67 boundsCache = shape.getBounds2D();
68 }
69 return boundsCache;
70 }
71
72 @Override
73 public double pathLength(@NotNull RenderContext context) {
74 if (Double.isNaN(pathLength)) {
75 pathLength = computePathLength(context);
76 }
77 return pathLength;
78 }
79
80 private double computePathLength(@NotNull RenderContext context) {
81 // Optimize shapes for which the computation is simple.
82 Shape shape = shape(context, false);
83 if (shape instanceof Rectangle2D) {
84 Rectangle2D r = (Rectangle2D) shape;
85 return 2 * (r.getWidth() + r.getHeight());
86 } else if (shape instanceof Ellipse2D) {
87 Ellipse2D e = (Ellipse2D) shape;
88 double w = e.getWidth();
89 double h = e.getHeight();
90 if (w == h) return Math.PI * w;
91 // General ellipse
92 return SVGEllipse.ellipseCircumference(w / 2f, h / 2f);
93 } else {
94 return GeometryUtil.pathLength(shape);
95 }
96 }
97
98 @Override
99 public boolean isClosed(@NotNull RenderContext context) {
100 Shape shape = shape(context, false);
101 if (shape instanceof Rectangle2D) {
102 return true;
103 } else if (shape instanceof Ellipse2D) {
104 return true;
105 } else {
106 return GeometryUtil.isSingleClosedPath(shape);
107 }
108 }
109 }