| •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.nodes.container; |
| 23 |
|
| 24 |
import java.awt.geom.AffineTransform; |
| 25 |
import java.awt.geom.Point2D; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.MustBeInvokedByOverriders; |
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
import org.jetbrains.annotations.Nullable; |
| 30 |
|
| 31 |
import com.github.weisj.jsvg.attributes.Overflow; |
| 32 |
import com.github.weisj.jsvg.attributes.PreserveAspectRatio; |
| 33 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 34 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 35 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 36 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 37 |
import com.github.weisj.jsvg.renderer.impl.NodeRenderer; |
| 38 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 39 |
import com.github.weisj.jsvg.view.FloatSize; |
| 40 |
import com.github.weisj.jsvg.view.ViewBox; |
| 41 |
|
| 42 |
public abstract class BaseInnerViewContainer extends CommonRenderableContainerNode { |
| 43 |
|
| 44 |
protected ViewBox viewBox; |
| 45 |
protected PreserveAspectRatio preserveAspectRatio; |
| 46 |
private Overflow overflow; |
| 47 |
|
| 48 |
protected abstract @NotNull Point2D outerLocation(@NotNull MeasureContext context); |
| 49 |
|
| 50 |
protected abstract @Nullable Point2D anchorLocation(@NotNull MeasureContext context); |
| 51 |
|
| 52 |
public abstract @NotNull FloatSize size(@NotNull RenderContext context); |
| 53 |
|
| 54 |
protected abstract @NotNull Overflow defaultOverflow(); |
| 55 |
|
| 56 |
public @Nullable ViewBox viewBox(@NotNull RenderContext context) { |
| 57 |
return viewBox != null ? viewBox : new ViewBox(size(context)); |
| 58 |
} |
| 59 |
|
| 60 |
public @NotNull ViewBox staticViewBox(@NotNull FloatSize fallbackSize) { |
| 61 |
return viewBox != null ? viewBox : new ViewBox(fallbackSize); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
@MustBeInvokedByOverriders |
| 66 |
public void build(@NotNull AttributeNode attributeNode) { |
| 67 |
super.build(attributeNode); |
| 68 |
viewBox = attributeNode.getViewBox(); |
| 69 |
preserveAspectRatio = PreserveAspectRatio.parse( |
| 70 |
attributeNode.getValue("preserveAspectRatio"), attributeNode.parser()); |
| 71 |
overflow = attributeNode.getEnum("overflow", defaultOverflow()); |
| 72 |
} |
| 73 |
|
| 74 |
public void renderWithEstablishedViewBox(@NotNull RenderContext context, @NotNull Output output) { |
| 75 |
super.render(context, output); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void render(@NotNull RenderContext context, @NotNull Output output) { |
| 80 |
renderWithSize(size(context), viewBox(context), context, output); |
| 81 |
} |
| 82 |
|
| 83 |
public final void renderWithSize(@NotNull FloatSize useSiteSize, @Nullable ViewBox view, |
| 84 |
@NotNull RenderContext context, @NotNull Output output) { |
| 85 |
RenderContext innerContext = createInnerContextForViewBox(useSiteSize, view, context, output); |
| 86 |
renderWithEstablishedViewBox(innerContext, output); |
| 87 |
} |
| 88 |
|
| 89 |
protected boolean inheritAttributes() { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
private @NotNull RenderContext createInnerContext(@NotNull RenderContext context, |
| 94 |
@NotNull ViewBox viewBox) { |
| 95 |
return NodeRenderer.setupInnerViewRenderContext(viewBox, context, inheritAttributes()); |
| 96 |
} |
| 97 |
|
| 98 |
private @NotNull ViewBox computeOuterViewBox(@NotNull RenderContext context, @NotNull FloatSize useSiteSize) { |
| 99 |
MeasureContext measureContext = context.measureContext(); |
| 100 |
Point2D outerPos = outerLocation(measureContext); |
| 101 |
ViewBox vb = new ViewBox(outerPos, useSiteSize); |
| 102 |
|
| 103 |
if (Length.isUnspecified(vb.width) || Length.isUnspecified(vb.height)) { |
| 104 |
FloatSize size = size(context); |
| 105 |
if (Length.isUnspecified(vb.width)) vb.width = size.width; |
| 106 |
if (Length.isUnspecified(vb.height)) vb.height = size.height; |
| 107 |
} |
| 108 |
|
| 109 |
return vb; |
| 110 |
} |
| 111 |
|
| 112 |
public final @NotNull RenderContext createInnerContextForViewBox(@NotNull FloatSize useSiteSize, |
| 113 |
@Nullable ViewBox view, @NotNull RenderContext context, @NotNull Output output) { |
| 114 |
ViewBox outerViewBox = computeOuterViewBox(context, useSiteSize); |
| 115 |
ViewBox innerViewBox = view; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
AffineTransform viewTransform = innerViewBox != null |
| 120 |
? preserveAspectRatio.computeViewportTransform(outerViewBox.size(), innerViewBox) |
| 121 |
: null; |
| 122 |
|
| 123 |
if (innerViewBox == null) { |
| 124 |
innerViewBox = new ViewBox(outerViewBox.size()); |
| 125 |
} |
| 126 |
|
| 127 |
RenderContext innerContext = createInnerContext(context, innerViewBox); |
| 128 |
MeasureContext innerMeasure = innerContext.measureContext(); |
| 129 |
Point2D anchorPos = anchorLocation(innerMeasure); |
| 130 |
|
| 131 |
|
| 132 |
if (overflow.establishesClip()) { |
| 133 |
ViewBox clipViewBox = new ViewBox(outerViewBox); |
| 134 |
if (anchorPos != null) { |
| 135 |
Point2D clipAnchor = anchorPos; |
| 136 |
if (viewTransform != null) { |
| 137 |
clipAnchor = new Point2D.Double(); |
| 138 |
viewTransform.transform(anchorPos, clipAnchor); |
| 139 |
} |
| 140 |
clipViewBox.x += (float) clipAnchor.getX(); |
| 141 |
clipViewBox.y += (float) clipAnchor.getY(); |
| 142 |
} |
| 143 |
output.applyClip(clipViewBox); |
| 144 |
} |
| 145 |
|
| 146 |
innerContext.translate(output, outerViewBox.location()); |
| 147 |
if (viewTransform != null) { |
| 148 |
|
| 149 |
innerContext.transform(output, viewTransform); |
| 150 |
} |
| 151 |
|
| 152 |
if (anchorPos != null) { |
| 153 |
innerContext.translate(output, anchorPos); |
| 154 |
} |
| 155 |
|
| 156 |
return innerContext; |
| 157 |
} |
| 158 |
} |