| •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.geom.AffineTransform; |
| 25 |
import java.awt.geom.Path2D; |
| 26 |
import java.awt.geom.Point2D; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
import org.jetbrains.annotations.NotNull; |
| 31 |
import org.jetbrains.annotations.Nullable; |
| 32 |
|
| 33 |
import com.github.weisj.jsvg.attributes.text.TextAnchor; |
| 34 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 35 |
import com.github.weisj.jsvg.geometry.util.GeometryUtil; |
| 36 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 37 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 38 |
import com.github.weisj.jsvg.renderer.output.TextOutput; |
| 39 |
|
| 40 |
final class LinearTextLayoutGroup implements TextLayoutGroup { |
| 41 |
private final LinearTextContainer<?> parent; |
| 42 |
private final @NotNull List<@NotNull TextSegment> segments; |
| 43 |
private final TextSegment.RenderableSegment asSegment; |
| 44 |
|
| 45 |
LinearTextLayoutGroup(@NotNull LinearTextContainer<?> parent) { |
| 46 |
this(parent, new ArrayList<>()); |
| 47 |
} |
| 48 |
|
| 49 |
LinearTextLayoutGroup(@NotNull LinearTextContainer<?> parent, @NotNull List<@NotNull TextSegment> segments) { |
| 50 |
this.parent = parent; |
| 51 |
this.segments = segments; |
| 52 |
this.asSegment = createSegment(parent, this); |
| 53 |
} |
| 54 |
|
| 55 |
private static <E> TextSegment.RenderableSegment createSegment( |
| 56 |
@NotNull LinearTextContainer<E> parent, @NotNull TextLayoutGroup group) { |
| 57 |
return new LayoutGroupSegment<>(parent, group); |
| 58 |
} |
| 59 |
|
| 60 |
public @NotNull TextSegment.RenderableSegment asSegment() { |
| 61 |
return asSegment; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public @NotNull List<TextSegment> segments() { |
| 66 |
return segments; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public @Nullable Length fixedLength() { |
| 71 |
if (parent.textLength.isSpecified()) return parent.textLength; |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
private @NotNull GlyphCursor createCursor(@Nullable Point2D start) { |
| 76 |
GlyphCursor cursor = parent.createLocalCursor(start == null, new GlyphCursor(0, 0, new AffineTransform())); |
| 77 |
if (start != null) { |
| 78 |
cursor.x = (float) start.getX(); |
| 79 |
cursor.y = (float) start.getY(); |
| 80 |
} |
| 81 |
return cursor; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public @NotNull Point2D renderText(@Nullable Point2D start, @NotNull RenderContext context, |
| 86 |
@NotNull Output output) { |
| 87 |
GlyphCursor cursor = createCursor(start); |
| 88 |
TextOutput textOutput = output.textOutput(); |
| 89 |
textOutput.beginText(); |
| 90 |
asSegment().prepareSegmentForRendering(cursor, context, textOutput); |
| 91 |
|
| 92 |
|
| 93 |
double offset = start == null |
| 94 |
? textAnchorOffset(parent.textAnchor(context), cursor.completeGlyphRunMetrics) |
| 95 |
: 0; |
| 96 |
context.translate(output, -offset, 0); |
| 97 |
asSegment().renderSegmentWithoutLayout(cursor, context, output); |
| 98 |
context.translate(output, offset, 0); |
| 99 |
|
| 100 |
textOutput.endText(); |
| 101 |
return cursor.currentLocation(context.measureContext()); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public @NotNull Point2D appendGlyphShape(@Nullable Point2D start, @NotNull RenderContext context, |
| 106 |
@NotNull Path2D shape) { |
| 107 |
MutableGlyphRun glyphRun = new MutableGlyphRun(); |
| 108 |
GlyphCursor cursor = createCursor(start); |
| 109 |
asSegment().appendTextShape(cursor, glyphRun, context); |
| 110 |
double offset = textAnchorOffset(parent.textAnchor(context), glyphRun.metrics()); |
| 111 |
if (GeometryUtil.approximatelyEqual(offset, 0)) { |
| 112 |
shape.append(glyphRun.shape(), false); |
| 113 |
} else { |
| 114 |
shape.append(glyphRun.shape().createTransformedShape(AffineTransform.getTranslateInstance(-offset, 0)), |
| 115 |
false); |
| 116 |
} |
| 117 |
return cursor.currentLocation(context.measureContext()); |
| 118 |
} |
| 119 |
|
| 120 |
private double textAnchorOffset(@NotNull TextAnchor textAnchor, @NotNull AbstractGlyphRun.Metrics metrics) { |
| 121 |
switch (textAnchor) { |
| 122 |
case Start: |
| 123 |
return 0; |
| 124 |
case Middle: |
| 125 |
return metrics.layoutBounds.getWidth() / 2f; |
| 126 |
case End: |
| 127 |
return metrics.layoutBounds.getWidth(); |
| 128 |
default: |
| 129 |
throw new IllegalStateException("Unexpected value: " + textAnchor); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
} |