| •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.text; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.Area; |
| 26 |
import java.awt.geom.Rectangle2D; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.MustBeInvokedByOverriders; |
| 32 |
import org.jetbrains.annotations.NotNull; |
| 33 |
|
| 34 |
import com.github.weisj.jsvg.attributes.VectorEffect; |
| 35 |
import com.github.weisj.jsvg.attributes.font.AttributeFontSpec; |
| 36 |
import com.github.weisj.jsvg.attributes.font.FontParser; |
| 37 |
import com.github.weisj.jsvg.attributes.text.LengthAdjust; |
| 38 |
import com.github.weisj.jsvg.attributes.text.TextAnchor; |
| 39 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 40 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 41 |
import com.github.weisj.jsvg.nodes.container.BaseContainerNode; |
| 42 |
import com.github.weisj.jsvg.nodes.prototype.HasContext; |
| 43 |
import com.github.weisj.jsvg.nodes.prototype.HasShape; |
| 44 |
import com.github.weisj.jsvg.nodes.prototype.HasVectorEffects; |
| 45 |
import com.github.weisj.jsvg.nodes.prototype.Renderable; |
| 46 |
import com.github.weisj.jsvg.nodes.prototype.impl.HasContextImpl; |
| 47 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 48 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 49 |
import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor; |
| 50 |
|
| 51 |
abstract class TextContainer<T> extends BaseContainerNode<T> |
| 52 |
implements HasShape, HasContext.ByDelegate, HasVectorEffects { |
| 53 |
protected final List<@NotNull T> children = new ArrayList<>(); |
| 54 |
|
| 55 |
protected boolean isVisible; |
| 56 |
protected AttributeFontSpec fontSpec; |
| 57 |
protected LengthAdjust lengthAdjust; |
| 58 |
protected Length textLength; |
| 59 |
|
| 60 |
private HasContext context; |
| 61 |
|
| 62 |
private Set<VectorEffect> vectorEffects; |
| 63 |
|
| 64 |
@Override |
| 65 |
@MustBeInvokedByOverriders |
| 66 |
public void build(@NotNull AttributeNode attributeNode) { |
| 67 |
super.build(attributeNode); |
| 68 |
fontSpec = FontParser.parseFontSpec(attributeNode); |
| 69 |
lengthAdjust = attributeNode.getEnum("lengthAdjust", LengthAdjust.Spacing); |
| 70 |
textLength = attributeNode.getLength("textLength", PercentageDimension.NONE, Length.UNSPECIFIED); |
| 71 |
if (textLength.raw() < 0) textLength = Length.UNSPECIFIED; |
| 72 |
|
| 73 |
isVisible = Renderable.parseVisibility(attributeNode); |
| 74 |
context = HasContextImpl.parse(attributeNode); |
| 75 |
|
| 76 |
|
| 77 |
vectorEffects = VectorEffect.parse(attributeNode); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public @NotNull Set<VectorEffect> vectorEffects() { |
| 82 |
return vectorEffects; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public @NotNull HasContext contextDelegate() { |
| 87 |
return context; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public List<? extends @NotNull T> children() { |
| 92 |
return children; |
| 93 |
} |
| 94 |
|
| 95 |
public boolean isVisible(@NotNull RenderContext context) { |
| 96 |
return isVisible; |
| 97 |
} |
| 98 |
|
| 99 |
abstract @NotNull Shape glyphShape(@NotNull RenderContext context); |
| 100 |
|
| 101 |
public final TextAnchor textAnchor(@NotNull RenderContext context) { |
| 102 |
return RenderContextAccessor.instance().fontRenderContext(context).textAnchor(); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public final @NotNull Shape untransformedElementShape(@NotNull RenderContext context, @NotNull Box box) { |
| 107 |
Shape shape = glyphShape(context); |
| 108 |
switch (box) { |
| 109 |
case BoundingBox: |
| 110 |
return shape; |
| 111 |
case StrokeBox: |
| 112 |
Area area = new Area(shape); |
| 113 |
area.add(new Area(context.stroke(1).createStrokedShape(shape))); |
| 114 |
return area; |
| 115 |
default: |
| 116 |
throw new IllegalStateException("Unexpected value: " + box); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public @NotNull Rectangle2D untransformedElementBounds(@NotNull RenderContext context, Box box) { |
| 122 |
|
| 123 |
return untransformedElementShape(context, box).getBounds2D(); |
| 124 |
} |
| 125 |
} |