| •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.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 |
} |