| 1 | package com.mojang.realmsclient.util;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.net.URI;
|
| 6 | import java.util.Arrays;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class TextRenderingUtils {
|
| 15 | private TextRenderingUtils() {
|
| 16 | }
|
| 17 |
|
| 18 | @VisibleForTesting
|
| 19 | protected static List<String> lineBreak(final String text) {
|
| 20 | return Arrays.asList(text.split("\\n"));
|
| 21 | }
|
| 22 |
|
| 23 | public static List<TextRenderingUtils.Line> decompose(final String text, final TextRenderingUtils.LineSegment... links) {
|
| 24 | return decompose(text, Arrays.asList(links));
|
| 25 | }
|
| 26 |
|
| 27 | private static List<TextRenderingUtils.Line> decompose(final String text, final List<TextRenderingUtils.LineSegment> links) {
|
| 28 | List<String> brokenLines = lineBreak(text);
|
| 29 | return insertLinks(brokenLines, links);
|
| 30 | }
|
| 31 |
|
| 32 | private static List<TextRenderingUtils.Line> insertLinks(final List<String> lines, final List<TextRenderingUtils.LineSegment> links) {
|
| 33 | int linkCount = 0;
|
| 34 | List<TextRenderingUtils.Line> processedLines = Lists.newArrayList();
|
| 35 |
|
| 36 | for (String line : lines) {
|
| 37 | List<TextRenderingUtils.LineSegment> segments = Lists.newArrayList();
|
| 38 |
|
| 39 | for (String part : split(line, "%link")) {
|
| 40 | if ("%link".equals(part)) {
|
| 41 | segments.add(links.get(linkCount++));
|
| 42 | } else {
|
| 43 | segments.add(TextRenderingUtils.LineSegment.text(part));
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | processedLines.add(new TextRenderingUtils.Line(segments));
|
| 48 | }
|
| 49 |
|
| 50 | return processedLines;
|
| 51 | }
|
| 52 |
|
| 53 | public static List<String> split(final String line, final String delimiter) {
|
| 54 | if (delimiter.isEmpty()) {
|
| 55 | throw new IllegalArgumentException("Delimiter cannot be the empty string");
|
| 56 | }
|
| 57 |
|
| 58 | List<String> parts = Lists.newArrayList();
|
| 59 | int searchStart = 0;
|
| 60 |
|
| 61 | int matchIndex;
|
| 62 | while ((matchIndex = line.indexOf(delimiter, searchStart)) != -1) {
|
| 63 | if (matchIndex > searchStart) {
|
| 64 | parts.add(line.substring(searchStart, matchIndex));
|
| 65 | }
|
| 66 |
|
| 67 | parts.add(delimiter);
|
| 68 | searchStart = matchIndex + delimiter.length();
|
| 69 | }
|
| 70 |
|
| 71 | if (searchStart < line.length()) {
|
| 72 | parts.add(line.substring(searchStart));
|
| 73 | }
|
| 74 |
|
| 75 | return parts;
|
| 76 | }
|
| 77 |
|
| 78 | @Environment(EnvType.CLIENT)
|
| 79 | public static class Line {
|
| 80 | public final List<TextRenderingUtils.LineSegment> segments;
|
| 81 |
|
| 82 | public Line(final TextRenderingUtils.LineSegment... segments) {
|
| 83 | this(Arrays.asList(segments));
|
| 84 | }
|
| 85 |
|
| 86 | public Line(final List<TextRenderingUtils.LineSegment> segments) {
|
| 87 | this.segments = segments;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public String toString() {
|
| 92 | return "Line{segments=" + this.segments + "}";
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public boolean equals(final Object o) {
|
| 97 | if (this == o) {
|
| 98 | return true;
|
| 99 | } else if (o != null && this.getClass() == o.getClass()) {
|
| 100 | TextRenderingUtils.Line line = (TextRenderingUtils.Line)o;
|
| 101 | return Objects.equals(this.segments, line.segments);
|
| 102 | } else {
|
| 103 | return false;
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public int hashCode() {
|
| 109 | return Objects.hash(this.segments);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Environment(EnvType.CLIENT)
|
| 114 | public static class LineSegment {
|
| 115 | private final String fullText;
|
| 116 | private final @Nullable String linkTitle;
|
| 117 | private final @Nullable URI linkUrl;
|
| 118 |
|
| 119 | private LineSegment(final String fullText) {
|
| 120 | this.fullText = fullText;
|
| 121 | this.linkTitle = null;
|
| 122 | this.linkUrl = null;
|
| 123 | }
|
| 124 |
|
| 125 | private LineSegment(final String fullText, final @Nullable String linkTitle, final @Nullable URI linkUrl) {
|
| 126 | this.fullText = fullText;
|
| 127 | this.linkTitle = linkTitle;
|
| 128 | this.linkUrl = linkUrl;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public boolean equals(final Object o) {
|
| 133 | if (this == o) {
|
| 134 | return true;
|
| 135 | } else if (o != null && this.getClass() == o.getClass()) {
|
| 136 | TextRenderingUtils.LineSegment segment = (TextRenderingUtils.LineSegment)o;
|
| 137 | return Objects.equals(this.fullText, segment.fullText)
|
| 138 | && Objects.equals(this.linkTitle, segment.linkTitle)
|
| 139 | && Objects.equals(this.linkUrl, segment.linkUrl);
|
| 140 | } else {
|
| 141 | return false;
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public int hashCode() {
|
| 147 | return Objects.hash(this.fullText, this.linkTitle, this.linkUrl);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public String toString() {
|
| 152 | return "Segment{fullText='" + this.fullText + "', linkTitle='" + this.linkTitle + "', linkUrl='" + this.linkUrl + "'}";
|
| 153 | }
|
| 154 |
|
| 155 | public String renderedText() {
|
| 156 | return this.isLink() ? this.linkTitle : this.fullText;
|
| 157 | }
|
| 158 |
|
| 159 | public boolean isLink() {
|
| 160 | return this.linkTitle != null;
|
| 161 | }
|
| 162 |
|
| 163 | public URI getLinkUrl() {
|
| 164 | if (!this.isLink()) {
|
| 165 | throw new IllegalStateException("Not a link: " + this);
|
| 166 | } else {
|
| 167 | return this.linkUrl;
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | public static TextRenderingUtils.LineSegment link(final String linkTitle, final URI linkUrl) {
|
| 172 | return new TextRenderingUtils.LineSegment(null, linkTitle, linkUrl);
|
| 173 | }
|
| 174 |
|
| 175 | @VisibleForTesting
|
| 176 | protected static TextRenderingUtils.LineSegment text(final String fullText) {
|
| 177 | return new TextRenderingUtils.LineSegment(fullText);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|