| •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; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.Rectangle2D; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.attributes.FillRule; |
| 31 |
import com.github.weisj.jsvg.attributes.font.AttributeFontSpec; |
| 32 |
import com.github.weisj.jsvg.attributes.font.FontParser; |
| 33 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 34 |
import com.github.weisj.jsvg.geometry.AWTSVGShape; |
| 35 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 36 |
import com.github.weisj.jsvg.nodes.container.CommonInnerViewContainer; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.*; |
| 38 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 40 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 41 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 42 |
import com.github.weisj.jsvg.parser.impl.AttributeNode.ElementRelation; |
| 43 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 44 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 45 |
import com.github.weisj.jsvg.renderer.impl.NodeRenderer; |
| 46 |
import com.github.weisj.jsvg.renderer.impl.context.FontRenderContext; |
| 47 |
import com.github.weisj.jsvg.renderer.impl.context.PaintContext; |
| 48 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 49 |
import com.github.weisj.jsvg.view.FloatSize; |
| 50 |
|
| 51 |
@ElementCategories({Category.Graphic, Category.GraphicsReferencing, Category.Structural}) |
| 52 |
@PermittedContent(categories = {Category.Animation, Category.Descriptive}) |
| 53 |
public final class Use extends RenderableSVGNode implements HasContext, HasShape, Instantiator { |
| 54 |
public static final String TAG = "use"; |
| 55 |
|
| 56 |
private Length x; |
| 57 |
private Length y; |
| 58 |
private Length width; |
| 59 |
private Length height; |
| 60 |
|
| 61 |
private @Nullable SVGNode referencedNode; |
| 62 |
|
| 63 |
private PaintContext paintContext; |
| 64 |
private FontRenderContext fontRenderContext; |
| 65 |
private AttributeFontSpec fontSpec; |
| 66 |
private FillRule fillRule; |
| 67 |
|
| 68 |
@Override |
| 69 |
public @NotNull String tagName() { |
| 70 |
return TAG; |
| 71 |
} |
| 72 |
|
| 73 |
public @Nullable SVGNode referencedNode() { |
| 74 |
return referencedNode; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean isVisible(@NotNull RenderContext context) { |
| 79 |
return super.isVisible(context) && referencedNode instanceof Renderable; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void build(@NotNull AttributeNode attributeNode) { |
| 84 |
super.build(attributeNode); |
| 85 |
x = attributeNode.getLength("x", PercentageDimension.WIDTH, 0); |
| 86 |
y = attributeNode.getLength("y", PercentageDimension.HEIGHT, 0); |
| 87 |
width = attributeNode.getLength("width", PercentageDimension.WIDTH, Length.UNSPECIFIED); |
| 88 |
height = attributeNode.getLength("height", PercentageDimension.HEIGHT, Length.UNSPECIFIED); |
| 89 |
|
| 90 |
String href = attributeNode.getValue("href"); |
| 91 |
if (href == null) href = attributeNode.getValue("xlink:href"); |
| 92 |
referencedNode = attributeNode.getElementByHref(SVGNode.class, href, ElementRelation.PAINTED_CHILD); |
| 93 |
|
| 94 |
paintContext = PaintContext.parse(attributeNode); |
| 95 |
fontRenderContext = FontRenderContext.parse(attributeNode); |
| 96 |
fontSpec = FontParser.parseFontSpec(attributeNode); |
| 97 |
fillRule = FillRule.parse(attributeNode); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public @NotNull Shape untransformedElementShape(@NotNull RenderContext context, Box box) { |
| 102 |
|
| 103 |
return referencedNode instanceof HasShape |
| 104 |
? ((HasShape) referencedNode).elementShape( |
| 105 |
NodeRenderer.createChildContext((Renderable) referencedNode, context, this), |
| 106 |
box) |
| 107 |
: AWTSVGShape.EMPTY_SHAPE; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public @NotNull Rectangle2D untransformedElementBounds(@NotNull RenderContext context, Box box) { |
| 112 |
|
| 113 |
return referencedNode instanceof HasShape |
| 114 |
? ((HasShape) referencedNode).elementBounds( |
| 115 |
NodeRenderer.createChildContext((Renderable) referencedNode, context, this), box) |
| 116 |
: AWTSVGShape.EMPTY_SHAPE; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public @NotNull PaintContext paintContext() { |
| 121 |
return paintContext; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public @NotNull FontRenderContext fontRenderContext() { |
| 126 |
return fontRenderContext; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public @NotNull AttributeFontSpec fontSpec() { |
| 131 |
return fontSpec; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public boolean canInstantiate(@NotNull SVGNode node) { |
| 136 |
return node instanceof CommonInnerViewContainer; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void render(@NotNull RenderContext context, @NotNull Output output) { |
| 141 |
if (referencedNode == null) return; |
| 142 |
MeasureContext measureContext = context.measureContext(); |
| 143 |
context.translate(output, x.resolve(measureContext), y.resolve(measureContext)); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if (referencedNode instanceof CommonInnerViewContainer) { |
| 148 |
FloatSize targetViewBox = new FloatSize(Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW); |
| 149 |
if (width.isSpecified()) targetViewBox.width = width.resolve(measureContext); |
| 150 |
if (height.isSpecified()) targetViewBox.height = height.resolve(measureContext); |
| 151 |
CommonInnerViewContainer view = (CommonInnerViewContainer) referencedNode; |
| 152 |
NodeRenderer.renderWithSize(view, targetViewBox, context, output, this); |
| 153 |
} else { |
| 154 |
NodeRenderer.renderNode(referencedNode, context, output, this); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public String toString() { |
| 160 |
return "Use{" + |
| 161 |
"x=" + x + |
| 162 |
", y=" + y + |
| 163 |
", width=" + width + |
| 164 |
", height=" + height + |
| 165 |
", referencedNode=" + (referencedNode != null ? referencedNode.id() : null) + |
| 166 |
", styleContext=" + paintContext + |
| 167 |
", fillRule=" + fillRule + |
| 168 |
", fontRenderContext=" + fontRenderContext + |
| 169 |
'}'; |
| 170 |
} |
| 171 |
} |