| •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.text.BreakIterator; |
| 25 |
import java.text.StringCharacterIterator; |
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Objects; |
| 29 |
import java.util.function.Supplier; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.NotNull; |
| 32 |
import org.jetbrains.annotations.Nullable; |
| 33 |
|
| 34 |
import com.github.weisj.jsvg.parser.TextContent; |
| 35 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 36 |
import com.github.weisj.jsvg.util.supplier.ConstantSupplier; |
| 37 |
|
| 38 |
final class StringTextSegment implements TextSegment { |
| 39 |
private final Supplier<List<String>> codepoints; |
| 40 |
private final @NotNull TextContainer<?> parent; |
| 41 |
private final @NotNull TextLayoutGroup layoutGroup; |
| 42 |
private final int index; |
| 43 |
|
| 44 |
@Nullable |
| 45 |
GlyphRun currentGlyphRun = null; |
| 46 |
@Nullable |
| 47 |
RenderContext currentRenderContext = null; |
| 48 |
|
| 49 |
public StringTextSegment(@NotNull TextContainer<?> parent, |
| 50 |
@NotNull TextLayoutGroup layoutGroup, int index, @NotNull TextContent.Segment content) { |
| 51 |
this.parent = parent; |
| 52 |
this.layoutGroup = layoutGroup; |
| 53 |
this.index = index; |
| 54 |
|
| 55 |
if (content.isConstant()) { |
| 56 |
codepoints = new ConstantSupplier<>(segmentCodepoints(content.text())); |
| 57 |
} else { |
| 58 |
codepoints = new CachedCodepoints(content); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public boolean isSegmentVisible(@NotNull RenderContext currentContext) { |
| 64 |
return parent.isVisible(currentContext); |
| 65 |
} |
| 66 |
|
| 67 |
public @NotNull List<@NotNull String> codepoints() { |
| 68 |
return codepoints.get(); |
| 69 |
} |
| 70 |
|
| 71 |
public boolean isLastSegmentInParent() { |
| 72 |
return index == layoutGroup.segments().size() - 1; |
| 73 |
} |
| 74 |
|
| 75 |
private static @NotNull List<@NotNull String> segmentCodepoints(String text) { |
| 76 |
BreakIterator it = BreakIterator.getCharacterInstance(); |
| 77 |
it.setText(new StringCharacterIterator(text)); |
| 78 |
int start = it.first(); |
| 79 |
List<String> characters = new ArrayList<>(); |
| 80 |
for (int end = it.next(); end != BreakIterator.DONE; start = end, end = it.next()) { |
| 81 |
characters.add(text.substring(start, end)); |
| 82 |
} |
| 83 |
return characters; |
| 84 |
} |
| 85 |
|
| 86 |
private static class CachedCodepoints implements Supplier<List<String>> { |
| 87 |
private final @NotNull TextContent.Segment segment; |
| 88 |
private List<@NotNull String> codepoints; |
| 89 |
private @Nullable String lastText; |
| 90 |
|
| 91 |
private CachedCodepoints(@NotNull TextContent.Segment segment) { |
| 92 |
this.segment = segment; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public List<String> get() { |
| 97 |
String text = segment.text(); |
| 98 |
if (Objects.equals(text, lastText)) { |
| 99 |
return codepoints; |
| 100 |
} |
| 101 |
lastText = text; |
| 102 |
codepoints = segmentCodepoints(text); |
| 103 |
return codepoints; |
| 104 |
} |
| 105 |
} |
| 106 |
} |