| •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 |
|
| 25 |
import java.awt.*; |
| 26 |
import java.awt.geom.Path2D; |
| 27 |
import java.awt.geom.Point2D; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
import org.jetbrains.annotations.Nullable; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.nodes.Anchor; |
| 33 |
import com.github.weisj.jsvg.nodes.SVGNode; |
| 34 |
import com.github.weisj.jsvg.nodes.prototype.HasGeometryContext; |
| 35 |
import com.github.weisj.jsvg.nodes.prototype.Renderable; |
| 36 |
import com.github.weisj.jsvg.nodes.prototype.impl.HasGeometryContextImpl; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 38 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 40 |
import com.github.weisj.jsvg.parser.TextContent; |
| 41 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 42 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 43 |
import com.github.weisj.jsvg.renderer.impl.NodeRenderer; |
| 44 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 45 |
import com.github.weisj.jsvg.util.AttributeUtil; |
| 46 |
|
| 47 |
@ElementCategories({Category.Graphic, Category.TextContent}) |
| 48 |
@PermittedContent( |
| 49 |
categories = {Category.Animation, Category.Descriptive, Category.TextContentChild}, |
| 50 |
anyOf = Anchor.class, |
| 51 |
charData = true |
| 52 |
) |
| 53 |
public final class Text extends LinearTextContainer<TextLayoutGroup> implements HasGeometryContext.ByDelegate, |
| 54 |
Renderable { |
| 55 |
public static final String TAG = "text"; |
| 56 |
|
| 57 |
private HasGeometryContext geometryContext; |
| 58 |
|
| 59 |
@Override |
| 60 |
public @NotNull String tagName() { |
| 61 |
return TAG; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void build(@NotNull AttributeNode attributeNode) { |
| 66 |
super.build(attributeNode); |
| 67 |
geometryContext = HasGeometryContextImpl.parse(attributeNode); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public @NotNull HasGeometryContext geometryContextDelegate() { |
| 72 |
return geometryContext; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isVisible(@NotNull RenderContext context) { |
| 77 |
return isVisible; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
protected boolean acceptChild(@Nullable String id, @NotNull SVGNode node) { |
| 82 |
return node instanceof TextSegment || node instanceof TextLayoutGroup; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected void doAdd(@NotNull SVGNode node) { |
| 87 |
if (node instanceof TextSegment) { |
| 88 |
lastLinearLayoutGroup().segments().add((TextSegment) node); |
| 89 |
} |
| 90 |
if (node instanceof TextLayoutGroup) { |
| 91 |
children.add((TextLayoutGroup) node); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void addContent(@NotNull TextContent.Segment content) { |
| 97 |
if (content.isConstant() && content.text().isEmpty()) return; |
| 98 |
if (children.isEmpty() && content.isConstant() && AttributeUtil.isBlank(content.text())) return; |
| 99 |
|
| 100 |
LinearTextLayoutGroup linearTextLayoutGroup = lastLinearLayoutGroup(); |
| 101 |
linearTextLayoutGroup.segments().add( |
| 102 |
new StringTextSegment(this, linearTextLayoutGroup, linearTextLayoutGroup.segments().size(), content)); |
| 103 |
} |
| 104 |
|
| 105 |
private @NotNull LinearTextLayoutGroup lastLinearLayoutGroup() { |
| 106 |
TextLayoutGroup lastGroup = children.isEmpty() ? null : children.get(children.size() - 1); |
| 107 |
if (!(lastGroup instanceof LinearTextLayoutGroup)) { |
| 108 |
lastGroup = new LinearTextLayoutGroup(this); |
| 109 |
children.add(lastGroup); |
| 110 |
} |
| 111 |
return (LinearTextLayoutGroup) lastGroup; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void render(@NotNull RenderContext context, @NotNull Output output) { |
| 116 |
Point2D start = null; |
| 117 |
for (TextLayoutGroup layoutGroup : children()) { |
| 118 |
RenderContext currentContext = context; |
| 119 |
if (layoutGroup instanceof TextContainer) { |
| 120 |
currentContext = NodeRenderer.setupRenderContext(layoutGroup, context); |
| 121 |
} |
| 122 |
start = layoutGroup.renderText(start, currentContext, output); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
protected @NotNull Shape glyphShape(@NotNull RenderContext context) { |
| 128 |
Path2D shape = new Path2D.Float(); |
| 129 |
Point2D start = null; |
| 130 |
for (TextLayoutGroup layoutGroup : children()) { |
| 131 |
start = layoutGroup.appendGlyphShape(start, context, shape); |
| 132 |
} |
| 133 |
return shape; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public @NotNull GlyphCursor createLocalCursor(boolean isInitial, @NotNull GlyphCursor current) { |
| 138 |
if (!isInitial) return current; |
| 139 |
return super.createLocalCursor(isInitial, current); |
| 140 |
} |
| 141 |
} |