Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/StringTextSegment.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 }