| •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.geom.Point2D; |
| 25 |
|
| 26 |
import org.jetbrains.annotations.NotNull; |
| 27 |
|
| 28 |
import com.github.weisj.jsvg.attributes.MarkerOrientation; |
| 29 |
import com.github.weisj.jsvg.attributes.MarkerUnitType; |
| 30 |
import com.github.weisj.jsvg.attributes.Overflow; |
| 31 |
import com.github.weisj.jsvg.attributes.value.LengthValue; |
| 32 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 33 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 34 |
import com.github.weisj.jsvg.nodes.container.BaseInnerViewContainer; |
| 35 |
import com.github.weisj.jsvg.nodes.filter.Filter; |
| 36 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 38 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 39 |
import com.github.weisj.jsvg.nodes.text.Text; |
| 40 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 41 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 42 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 43 |
import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor; |
| 44 |
import com.github.weisj.jsvg.view.FloatSize; |
| 45 |
|
| 46 |
@ElementCategories(Category.Container) |
| 47 |
@PermittedContent( |
| 48 |
categories = {Category.Animation, Category.Descriptive, Category.Shape, Category.Structural, Category.Gradient}, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
anyOf = {Anchor.class, ClipPath.class, Filter.class, Image.class, Mask.class, Marker.class, Pattern.class, |
| 54 |
Style.class, Text.class, View.class} |
| 55 |
) |
| 56 |
public final class Marker extends BaseInnerViewContainer { |
| 57 |
public static final String TAG = "marker"; |
| 58 |
|
| 59 |
private Length refX; |
| 60 |
private Length refY; |
| 61 |
|
| 62 |
private MarkerOrientation orientation; |
| 63 |
|
| 64 |
private MarkerUnitType markerUnits; |
| 65 |
private Length markerHeight; |
| 66 |
private Length markerWidth; |
| 67 |
|
| 68 |
@Override |
| 69 |
public @NotNull String tagName() { |
| 70 |
return TAG; |
| 71 |
} |
| 72 |
|
| 73 |
public @NotNull MarkerOrientation orientation() { |
| 74 |
return orientation; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
protected @NotNull Point2D outerLocation(@NotNull MeasureContext context) { |
| 79 |
return new Point2D.Float(0, 0); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected @NotNull Point2D anchorLocation(@NotNull MeasureContext context) { |
| 84 |
return new Point2D.Float(-refX.resolve(context), -refY.resolve(context)); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
protected @NotNull Overflow defaultOverflow() { |
| 89 |
return Overflow.Hidden; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public @NotNull FloatSize size(@NotNull RenderContext context) { |
| 94 |
MeasureContext measure = context.measureContext(); |
| 95 |
if (markerUnits == MarkerUnitType.StrokeWidth) { |
| 96 |
LengthValue strokeWidthLength = RenderContextAccessor.instance().strokeContext(context).strokeWidth; |
| 97 |
assert strokeWidthLength != null; |
| 98 |
float strokeWidth = strokeWidthLength.resolve(measure); |
| 99 |
return new FloatSize(markerWidth.raw() * strokeWidth, markerHeight.raw() * strokeWidth); |
| 100 |
} else { |
| 101 |
return new FloatSize(markerWidth.resolve(measure), markerHeight.resolve(measure)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void build(@NotNull AttributeNode attributeNode) { |
| 107 |
super.build(attributeNode); |
| 108 |
refX = attributeNode.getHorizontalReferenceLengthFromKey("refX"); |
| 109 |
refY = attributeNode.getVerticalReferenceLengthFromKey("refY"); |
| 110 |
|
| 111 |
orientation = MarkerOrientation.parse(attributeNode.getValue("orient"), attributeNode.parser()); |
| 112 |
|
| 113 |
markerUnits = attributeNode.getEnum("markerUnits", MarkerUnitType.StrokeWidth); |
| 114 |
markerWidth = attributeNode.getLength("markerWidth", PercentageDimension.WIDTH, 3); |
| 115 |
markerHeight = attributeNode.getLength("markerHeight", PercentageDimension.HEIGHT, 3); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public boolean requiresInstantiation() { |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
protected boolean inheritAttributes() { |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
} |