| •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.util.List; |
| 25 |
import java.util.function.BiConsumer; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
|
| 29 |
import com.github.weisj.jsvg.attributes.font.SVGFont; |
| 30 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 31 |
import com.github.weisj.jsvg.nodes.prototype.Renderable; |
| 32 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 33 |
import com.github.weisj.jsvg.renderer.impl.NodeRenderer; |
| 34 |
import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor; |
| 35 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 36 |
import com.github.weisj.jsvg.renderer.output.TextOutput; |
| 37 |
|
| 38 |
class LayoutGroupSegment<E, T extends TextContainer<E> & CursorContext> implements TextSegment.RenderableSegment { |
| 39 |
private final @NotNull T parent; |
| 40 |
private final @NotNull TextLayoutGroup group; |
| 41 |
|
| 42 |
LayoutGroupSegment(@NotNull T parent, @NotNull TextLayoutGroup group) { |
| 43 |
this.parent = parent; |
| 44 |
this.group = group; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public boolean isSegmentVisible(@NotNull RenderContext currentContext) { |
| 49 |
return parent.isVisible(currentContext); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void prepareSegmentForRendering(@NotNull GlyphCursor cursor, @NotNull RenderContext context, |
| 54 |
@NotNull TextOutput textOutput) { |
| 55 |
SVGFont font = RenderContextAccessor.instance().font(context); |
| 56 |
|
| 57 |
GlyphCursor localCursor = parent.createLocalCursor(false, cursor); |
| 58 |
localCursor.setAdvancement(localGlyphAdvancement(context, cursor)); |
| 59 |
|
| 60 |
forEachSegment( |
| 61 |
group.segments(), |
| 62 |
context, |
| 63 |
(seg, ctx) -> GlyphRenderer.prepareGlyphRun(seg, localCursor, font, ctx, textOutput), |
| 64 |
(seg, ctx) -> seg.prepareSegmentForRendering(localCursor, ctx, textOutput)); |
| 65 |
|
| 66 |
parent.cleanUpLocalCursor(cursor, localCursor); |
| 67 |
} |
| 68 |
|
| 69 |
private @NotNull GlyphAdvancement localGlyphAdvancement(@NotNull RenderContext context, |
| 70 |
@NotNull GlyphCursor cursor) { |
| 71 |
Length length = group.fixedLength(); |
| 72 |
if (length != null) { |
| 73 |
return new GlyphAdvancement( |
| 74 |
computeTextMetrics(context, UseTextLengthForCalculation.NO), |
| 75 |
length.resolve(context.measureContext()), parent.lengthAdjust); |
| 76 |
} |
| 77 |
return cursor.advancement(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void renderSegmentWithoutLayout(@NotNull GlyphCursor cursor, @NotNull RenderContext context, |
| 82 |
@NotNull Output output) { |
| 83 |
forEachSegment( |
| 84 |
group.segments(), |
| 85 |
context, |
| 86 |
(seg, ctx) -> { |
| 87 |
if ((parent instanceof Renderable) && !((Renderable) parent).isVisible(ctx)) { |
| 88 |
return; |
| 89 |
} |
| 90 |
GlyphRenderer.renderGlyphRun( |
| 91 |
output, RenderContextAccessor.instance().paintOrder(context), |
| 92 |
parent.vectorEffects(), seg); |
| 93 |
}, |
| 94 |
(seg, ctx) -> seg.renderSegmentWithoutLayout(cursor, ctx, output)); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public @NotNull TextMetrics computeTextMetrics(@NotNull RenderContext context, |
| 99 |
@NotNull UseTextLengthForCalculation flag) { |
| 100 |
return TextMetrics.computeTextMetrics(group, context, flag); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void appendTextShape(@NotNull GlyphCursor cursor, @NotNull MutableGlyphRun glyphRun, |
| 105 |
@NotNull RenderContext context) { |
| 106 |
SVGFont font = RenderContextAccessor.instance().font(context); |
| 107 |
|
| 108 |
GlyphCursor localCursor = parent.createLocalCursor(false, cursor); |
| 109 |
localCursor.setAdvancement(localGlyphAdvancement(context, cursor)); |
| 110 |
|
| 111 |
forEachSegment( |
| 112 |
group.segments(), |
| 113 |
context, |
| 114 |
(seg, ctx) -> glyphRun.append( |
| 115 |
GlyphRenderer.layoutGlyphRun(seg, localCursor, font, ctx, NullTextOutput.INSTANCE)), |
| 116 |
(seg, ctx) -> seg.appendTextShape(localCursor, glyphRun, ctx)); |
| 117 |
|
| 118 |
parent.cleanUpLocalCursor(cursor, localCursor); |
| 119 |
} |
| 120 |
|
| 121 |
static void forEachSegment(@NotNull List<? extends @NotNull TextSegment> segments, |
| 122 |
@NotNull RenderContext context, |
| 123 |
@NotNull BiConsumer<StringTextSegment, RenderContext> onStringTextSegment, |
| 124 |
@NotNull BiConsumer<TextSegment.RenderableSegment, RenderContext> onRenderableSegment) { |
| 125 |
for (TextSegment segment : segments) { |
| 126 |
RenderContext currentContext = context; |
| 127 |
if (segment instanceof TextContainer) { |
| 128 |
currentContext = NodeRenderer.setupRenderContext(segment, context); |
| 129 |
} |
| 130 |
if (segment instanceof StringTextSegment) { |
| 131 |
onStringTextSegment.accept((StringTextSegment) segment, currentContext); |
| 132 |
} else if (segment instanceof TextSegment.RenderableSegment) { |
| 133 |
onRenderableSegment.accept((TextSegment.RenderableSegment) segment, currentContext); |
| 134 |
} else { |
| 135 |
throw new IllegalStateException("Unexpected segment " + segment); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |