| •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.Path2D; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.nodes.Anchor; |
| 31 |
import com.github.weisj.jsvg.nodes.SVGNode; |
| 32 |
import com.github.weisj.jsvg.nodes.animation.Animate; |
| 33 |
import com.github.weisj.jsvg.nodes.animation.AnimateTransform; |
| 34 |
import com.github.weisj.jsvg.nodes.animation.Set; |
| 35 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 36 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 38 |
import com.github.weisj.jsvg.parser.TextContent; |
| 39 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 40 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 41 |
import com.github.weisj.jsvg.renderer.output.TextOutput; |
| 42 |
import com.github.weisj.jsvg.util.AttributeUtil; |
| 43 |
|
| 44 |
@ElementCategories({Category.TextContent, Category.TextContentChild}) |
| 45 |
@PermittedContent( |
| 46 |
categories = Category.Descriptive, |
| 47 |
anyOf = {Anchor.class, TextSpan.class, Animate.class, AnimateTransform.class, Set.class }, |
| 48 |
charData = true |
| 49 |
) |
| 50 |
public final class TextSpan extends LinearTextContainer<TextSegment> implements TextSegment.RenderableSegment { |
| 51 |
public static final String TAG = "tspan"; |
| 52 |
|
| 53 |
private final @NotNull LinearTextLayoutGroup layoutGroup = new LinearTextLayoutGroup(this, children); |
| 54 |
|
| 55 |
@Override |
| 56 |
public @NotNull String tagName() { |
| 57 |
return TAG; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected void doAdd(@NotNull SVGNode node) { |
| 62 |
children.add((TextSegment) node); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void addContent(@NotNull TextContent.Segment content) { |
| 67 |
if (content.isConstant() && content.text().isEmpty()) return; |
| 68 |
if (children.isEmpty() && content.isConstant() && AttributeUtil.isBlank(content.text())) return; |
| 69 |
children.add(new StringTextSegment(this, layoutGroup, children.size(), content)); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
protected boolean acceptChild(@Nullable String id, @NotNull SVGNode node) { |
| 74 |
return node instanceof TextSegment; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void prepareSegmentForRendering(@NotNull GlyphCursor cursor, @NotNull RenderContext context, |
| 79 |
@NotNull TextOutput textOutput) { |
| 80 |
layoutGroup.asSegment().prepareSegmentForRendering(cursor, context, textOutput); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void renderSegmentWithoutLayout(@NotNull GlyphCursor cursor, @NotNull RenderContext context, |
| 85 |
@NotNull Output output) { |
| 86 |
layoutGroup.asSegment().renderSegmentWithoutLayout(cursor, context, output); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public @NotNull TextMetrics computeTextMetrics(@NotNull RenderContext context, |
| 91 |
@NotNull UseTextLengthForCalculation flag) { |
| 92 |
return layoutGroup.asSegment().computeTextMetrics(context, flag); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void appendTextShape(@NotNull GlyphCursor cursor, @NotNull MutableGlyphRun glyphRun, |
| 97 |
@NotNull RenderContext context) { |
| 98 |
layoutGroup.asSegment().appendTextShape(cursor, glyphRun, context); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
@NotNull |
| 103 |
Shape glyphShape(@NotNull RenderContext context) { |
| 104 |
Path2D shape = new Path2D.Float(); |
| 105 |
layoutGroup.appendGlyphShape(null, context, shape); |
| 106 |
return shape; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public boolean isSegmentVisible(@NotNull RenderContext currentContext) { |
| 111 |
return layoutGroup.asSegment().isSegmentVisible(currentContext); |
| 112 |
} |
| 113 |
} |