| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 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 |
} |