| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.ListIterator;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.BiConsumer;
|
| 8 | import java.util.stream.Collectors;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.network.chat.FormattedText;
|
| 12 | import net.minecraft.network.chat.Style;
|
| 13 | import net.minecraft.network.chat.FormattedText.ContentConsumer;
|
| 14 | import net.minecraft.network.chat.FormattedText.StyledContentConsumer;
|
| 15 | import net.minecraft.util.FormattedCharSequence;
|
| 16 | import net.minecraft.util.FormattedCharSink;
|
| 17 | import net.minecraft.util.StringDecomposer;
|
| 18 | import org.apache.commons.lang3.mutable.MutableFloat;
|
| 19 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class StringSplitter {
|
| 24 | private final StringSplitter.WidthProvider widthProvider;
|
| 25 |
|
| 26 | public StringSplitter(final StringSplitter.WidthProvider widthProvider) {
|
| 27 | this.widthProvider = widthProvider;
|
| 28 | }
|
| 29 |
|
| 30 | public float stringWidth(final @Nullable String str) {
|
| 31 | if (str == null) {
|
| 32 | return 0.0F;
|
| 33 | }
|
| 34 |
|
| 35 | MutableFloat result = new MutableFloat();
|
| 36 | StringDecomposer.iterateFormatted(str, Style.EMPTY, (position, style, codepoint) -> {
|
| 37 | result.add(this.widthProvider.getWidth(codepoint, style));
|
| 38 | return true;
|
| 39 | });
|
| 40 | return result.floatValue();
|
| 41 | }
|
| 42 |
|
| 43 | public float stringWidth(final FormattedText text) {
|
| 44 | MutableFloat result = new MutableFloat();
|
| 45 | StringDecomposer.iterateFormatted(text, Style.EMPTY, (position, style, codepoint) -> {
|
| 46 | result.add(this.widthProvider.getWidth(codepoint, style));
|
| 47 | return true;
|
| 48 | });
|
| 49 | return result.floatValue();
|
| 50 | }
|
| 51 |
|
| 52 | public float stringWidth(final FormattedCharSequence text) {
|
| 53 | MutableFloat result = new MutableFloat();
|
| 54 | text.accept((position, style, codepoint) -> {
|
| 55 | result.add(this.widthProvider.getWidth(codepoint, style));
|
| 56 | return true;
|
| 57 | });
|
| 58 | return result.floatValue();
|
| 59 | }
|
| 60 |
|
| 61 | public int plainIndexAtWidth(final String str, final int maxWidth, final Style style) {
|
| 62 | StringSplitter.WidthLimitedCharSink output = new StringSplitter.WidthLimitedCharSink(maxWidth);
|
| 63 | StringDecomposer.iterate(str, style, output);
|
| 64 | return output.getPosition();
|
| 65 | }
|
| 66 |
|
| 67 | public String plainHeadByWidth(final String str, final int maxWidth, final Style style) {
|
| 68 | return str.substring(0, this.plainIndexAtWidth(str, maxWidth, style));
|
| 69 | }
|
| 70 |
|
| 71 | public String plainTailByWidth(final String str, final int maxWidth, final Style style) {
|
| 72 | MutableFloat currentWidth = new MutableFloat();
|
| 73 | MutableInt result = new MutableInt(str.length());
|
| 74 | StringDecomposer.iterateBackwards(str, style, (position, s, codepoint) -> {
|
| 75 | float w = currentWidth.addAndGet(this.widthProvider.getWidth(codepoint, s));
|
| 76 | if (w > maxWidth) {
|
| 77 | return false;
|
| 78 | }
|
| 79 |
|
| 80 | result.setValue(position);
|
| 81 | return true;
|
| 82 | });
|
| 83 | return str.substring(result.intValue());
|
| 84 | }
|
| 85 |
|
| 86 | public FormattedText headByWidth(final FormattedText text, final int width, final Style initialStyle) {
|
| 87 | final StringSplitter.WidthLimitedCharSink output = new StringSplitter.WidthLimitedCharSink(width);
|
| 88 | return text.visit(new StyledContentConsumer<FormattedText>() {
|
| 89 | private final ComponentCollector collector = new ComponentCollector();
|
| 90 |
|
| 91 | @Override
|
| 92 | public Optional<FormattedText> accept(final Style style, final String contents) {
|
| 93 | output.resetPosition();
|
| 94 | if (!StringDecomposer.iterateFormatted(contents, style, output)) {
|
| 95 | String partial = contents.substring(0, output.getPosition());
|
| 96 | if (!partial.isEmpty()) {
|
| 97 | this.collector.append(FormattedText.of(partial, style));
|
| 98 | }
|
| 99 |
|
| 100 | return Optional.of(this.collector.getResultOrEmpty());
|
| 101 | } else {
|
| 102 | if (!contents.isEmpty()) {
|
| 103 | this.collector.append(FormattedText.of(contents, style));
|
| 104 | }
|
| 105 |
|
| 106 | return Optional.empty();
|
| 107 | }
|
| 108 | }
|
| 109 | }, initialStyle).orElse(text);
|
| 110 | }
|
| 111 |
|
| 112 | public int findLineBreak(final String input, final int max, final Style initialStyle) {
|
| 113 | StringSplitter.LineBreakFinder finder = new StringSplitter.LineBreakFinder(max);
|
| 114 | StringDecomposer.iterateFormatted(input, initialStyle, finder);
|
| 115 | return finder.getSplitPosition();
|
| 116 | }
|
| 117 |
|
| 118 | public static int getWordPosition(final String text, final int dir, final int from, final boolean stripSpaces) {
|
| 119 | int result = from;
|
| 120 | boolean reverse = dir < 0;
|
| 121 | int abs = Math.abs(dir);
|
| 122 |
|
| 123 | for (int i = 0; i < abs; i++) {
|
| 124 | if (reverse) {
|
| 125 | while (stripSpaces && result > 0 && (text.charAt(result - 1) == ' ' || text.charAt(result - 1) == '\n')) {
|
| 126 | result--;
|
| 127 | }
|
| 128 |
|
| 129 | while (result > 0 && text.charAt(result - 1) != ' ' && text.charAt(result - 1) != '\n') {
|
| 130 | result--;
|
| 131 | }
|
| 132 | } else {
|
| 133 | int length = text.length();
|
| 134 | int index1 = text.indexOf(32, result);
|
| 135 | int index2 = text.indexOf(10, result);
|
| 136 | if (index1 == -1 && index2 == -1) {
|
| 137 | result = -1;
|
| 138 | } else if (index1 != -1 && index2 != -1) {
|
| 139 | result = Math.min(index1, index2);
|
| 140 | } else if (index1 != -1) {
|
| 141 | result = index1;
|
| 142 | } else {
|
| 143 | result = index2;
|
| 144 | }
|
| 145 |
|
| 146 | if (result == -1) {
|
| 147 | result = length;
|
| 148 | } else {
|
| 149 | while (stripSpaces && result < length && (text.charAt(result) == ' ' || text.charAt(result) == '\n')) {
|
| 150 | result++;
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return result;
|
| 157 | }
|
| 158 |
|
| 159 | public void splitLines(final String input, final int maxWidth, final Style initialStyle, final boolean includeAll, final StringSplitter.LinePosConsumer output) {
|
| 160 | int start = 0;
|
| 161 | int size = input.length();
|
| 162 | Style workStyle = initialStyle;
|
| 163 |
|
| 164 | while (start < size) {
|
| 165 | StringSplitter.LineBreakFinder finder = new StringSplitter.LineBreakFinder(maxWidth);
|
| 166 | boolean endOfText = StringDecomposer.iterateFormatted(input, start, workStyle, initialStyle, finder);
|
| 167 | if (endOfText) {
|
| 168 | output.accept(workStyle, start, size);
|
| 169 | break;
|
| 170 | }
|
| 171 |
|
| 172 | int lineBreak = finder.getSplitPosition();
|
| 173 | char firstTailChar = input.charAt(lineBreak);
|
| 174 | int adjustedBreak = firstTailChar != '\n' && firstTailChar != ' ' ? lineBreak : lineBreak + 1;
|
| 175 | output.accept(workStyle, start, includeAll ? adjustedBreak : lineBreak);
|
| 176 | start = adjustedBreak;
|
| 177 | workStyle = finder.getSplitStyle();
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | public List<FormattedText> splitLines(final String input, final int maxWidth, final Style initialStyle) {
|
| 182 | List<FormattedText> result = Lists.newArrayList();
|
| 183 | this.splitLines(input, maxWidth, initialStyle, false, (style, start, end) -> result.add(FormattedText.of(input.substring(start, end), style)));
|
| 184 | return result;
|
| 185 | }
|
| 186 |
|
| 187 | public List<FormattedText> splitLines(final FormattedText input, final int maxWidth, final Style initialStyle) {
|
| 188 | List<FormattedText> result = Lists.newArrayList();
|
| 189 | this.splitLines(input, maxWidth, initialStyle, (text, wrapped) -> result.add(text));
|
| 190 | return result;
|
| 191 | }
|
| 192 |
|
| 193 | public void splitLines(final FormattedText input, final int maxWidth, final Style initialStyle, final BiConsumer<FormattedText, Boolean> output) {
|
| 194 | List<StringSplitter.LineComponent> partList = Lists.newArrayList();
|
| 195 | input.visit((style, contents) -> {
|
| 196 | if (!contents.isEmpty()) {
|
| 197 | partList.add(new StringSplitter.LineComponent(contents, style));
|
| 198 | }
|
| 199 |
|
| 200 | return Optional.empty();
|
| 201 | }, initialStyle);
|
| 202 | StringSplitter.FlatComponents parts = new StringSplitter.FlatComponents(partList);
|
| 203 | boolean shouldRestart = true;
|
| 204 | boolean forceNewLine = false;
|
| 205 | boolean isWrapped = false;
|
| 206 |
|
| 207 | while (shouldRestart) {
|
| 208 | shouldRestart = false;
|
| 209 | StringSplitter.LineBreakFinder finder = new StringSplitter.LineBreakFinder(maxWidth);
|
| 210 |
|
| 211 | for (StringSplitter.LineComponent part : parts.parts) {
|
| 212 | boolean endOfText = StringDecomposer.iterateFormatted(part.contents, 0, part.style, initialStyle, finder);
|
| 213 | if (!endOfText) {
|
| 214 | int lineBreak = finder.getSplitPosition();
|
| 215 | Style lineBreakStyle = finder.getSplitStyle();
|
| 216 | char firstTailChar = parts.charAt(lineBreak);
|
| 217 | boolean isNewLine = firstTailChar == '\n';
|
| 218 | boolean skipNextChar = isNewLine || firstTailChar == ' ';
|
| 219 | forceNewLine = isNewLine;
|
| 220 | FormattedText result = parts.splitAt(lineBreak, skipNextChar ? 1 : 0, lineBreakStyle);
|
| 221 | output.accept(result, isWrapped);
|
| 222 | isWrapped = !isNewLine;
|
| 223 | shouldRestart = true;
|
| 224 | break;
|
| 225 | }
|
| 226 |
|
| 227 | finder.addToOffset(part.contents.length());
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | FormattedText lastLine = parts.getRemainder();
|
| 232 | if (lastLine != null) {
|
| 233 | output.accept(lastLine, isWrapped);
|
| 234 | } else if (forceNewLine) {
|
| 235 | output.accept(FormattedText.EMPTY, false);
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | @Environment(EnvType.CLIENT)
|
| 240 | private static class FlatComponents {
|
| 241 | private final List<StringSplitter.LineComponent> parts;
|
| 242 | private String flatParts;
|
| 243 |
|
| 244 | public FlatComponents(final List<StringSplitter.LineComponent> parts) {
|
| 245 | this.parts = parts;
|
| 246 | this.flatParts = parts.stream().map(p -> p.contents).collect(Collectors.joining());
|
| 247 | }
|
| 248 |
|
| 249 | public char charAt(final int position) {
|
| 250 | return this.flatParts.charAt(position);
|
| 251 | }
|
| 252 |
|
| 253 | public FormattedText splitAt(final int skipPosition, final int skipSize, final Style splitStyle) {
|
| 254 | ComponentCollector result = new ComponentCollector();
|
| 255 | ListIterator<StringSplitter.LineComponent> it = this.parts.listIterator();
|
| 256 | int position = skipPosition;
|
| 257 | boolean inSkip = false;
|
| 258 |
|
| 259 | while (it.hasNext()) {
|
| 260 | StringSplitter.LineComponent element = it.next();
|
| 261 | String contents = element.contents;
|
| 262 | int contentsSize = contents.length();
|
| 263 | if (!inSkip) {
|
| 264 | if (position > contentsSize) {
|
| 265 | result.append(element);
|
| 266 | it.remove();
|
| 267 | position -= contentsSize;
|
| 268 | } else {
|
| 269 | String beforeSplit = contents.substring(0, position);
|
| 270 | if (!beforeSplit.isEmpty()) {
|
| 271 | result.append(FormattedText.of(beforeSplit, element.style));
|
| 272 | }
|
| 273 |
|
| 274 | position += skipSize;
|
| 275 | inSkip = true;
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | if (inSkip) {
|
| 280 | if (position <= contentsSize) {
|
| 281 | String afterSplit = contents.substring(position);
|
| 282 | if (afterSplit.isEmpty()) {
|
| 283 | it.remove();
|
| 284 | } else {
|
| 285 | it.set(new StringSplitter.LineComponent(afterSplit, splitStyle));
|
| 286 | }
|
| 287 | break;
|
| 288 | }
|
| 289 |
|
| 290 | it.remove();
|
| 291 | position -= contentsSize;
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | this.flatParts = this.flatParts.substring(skipPosition + skipSize);
|
| 296 | return result.getResultOrEmpty();
|
| 297 | }
|
| 298 |
|
| 299 | public @Nullable FormattedText getRemainder() {
|
| 300 | ComponentCollector result = new ComponentCollector();
|
| 301 | this.parts.forEach(result::append);
|
| 302 | this.parts.clear();
|
| 303 | return result.getResult();
|
| 304 | }
|
| 305 | }
|
| 306 |
|
| 307 | @Environment(EnvType.CLIENT)
|
| 308 | private class LineBreakFinder implements FormattedCharSink {
|
| 309 | private final float maxWidth;
|
| 310 | private int lineBreak = -1;
|
| 311 | private Style lineBreakStyle = Style.EMPTY;
|
| 312 | private boolean hadNonZeroWidthChar;
|
| 313 | private float width;
|
| 314 | private int lastSpace = -1;
|
| 315 | private Style lastSpaceStyle = Style.EMPTY;
|
| 316 | private int nextChar;
|
| 317 | private int offset;
|
| 318 |
|
| 319 | public LineBreakFinder(final float maxWidth) {
|
| 320 | this.maxWidth = Math.max(maxWidth, 1.0F);
|
| 321 | }
|
| 322 |
|
| 323 | @Override
|
| 324 | public boolean accept(final int position, final Style style, final int codepoint) {
|
| 325 | int adjustedPosition = position + this.offset;
|
| 326 | switch (codepoint) {
|
| 327 | case 10:
|
| 328 | return this.finishIteration(adjustedPosition, style);
|
| 329 | case 32:
|
| 330 | this.lastSpace = adjustedPosition;
|
| 331 | this.lastSpaceStyle = style;
|
| 332 | default:
|
| 333 | float charWidth = StringSplitter.this.widthProvider.getWidth(codepoint, style);
|
| 334 | this.width += charWidth;
|
| 335 | if (!this.hadNonZeroWidthChar || !(this.width > this.maxWidth)) {
|
| 336 | this.hadNonZeroWidthChar |= charWidth != 0.0F;
|
| 337 | this.nextChar = adjustedPosition + Character.charCount(codepoint);
|
| 338 | return true;
|
| 339 | } else {
|
| 340 | return this.lastSpace != -1 ? this.finishIteration(this.lastSpace, this.lastSpaceStyle) : this.finishIteration(adjustedPosition, style);
|
| 341 | }
|
| 342 | }
|
| 343 | }
|
| 344 |
|
| 345 | private boolean finishIteration(final int lineBreak, final Style style) {
|
| 346 | this.lineBreak = lineBreak;
|
| 347 | this.lineBreakStyle = style;
|
| 348 | return false;
|
| 349 | }
|
| 350 |
|
| 351 | private boolean lineBreakFound() {
|
| 352 | return this.lineBreak != -1;
|
| 353 | }
|
| 354 |
|
| 355 | public int getSplitPosition() {
|
| 356 | return this.lineBreakFound() ? this.lineBreak : this.nextChar;
|
| 357 | }
|
| 358 |
|
| 359 | public Style getSplitStyle() {
|
| 360 | return this.lineBreakStyle;
|
| 361 | }
|
| 362 |
|
| 363 | public void addToOffset(final int delta) {
|
| 364 | this.offset += delta;
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 | @Environment(EnvType.CLIENT)
|
| 369 | private static class LineComponent implements FormattedText {
|
| 370 | private final String contents;
|
| 371 | private final Style style;
|
| 372 |
|
| 373 | public LineComponent(final String contents, final Style style) {
|
| 374 | this.contents = contents;
|
| 375 | this.style = style;
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public <T> Optional<T> visit(final ContentConsumer<T> output) {
|
| 380 | return output.accept(this.contents);
|
| 381 | }
|
| 382 |
|
| 383 | @Override
|
| 384 | public <T> Optional<T> visit(final StyledContentConsumer<T> output, final Style parentStyle) {
|
| 385 | return output.accept(this.style.applyTo(parentStyle), this.contents);
|
| 386 | }
|
| 387 | }
|
| 388 |
|
| 389 | @FunctionalInterface
|
| 390 | @Environment(EnvType.CLIENT)
|
| 391 | public interface LinePosConsumer {
|
| 392 | void accept(final Style style, int start, int end);
|
| 393 | }
|
| 394 |
|
| 395 | @Environment(EnvType.CLIENT)
|
| 396 | private class WidthLimitedCharSink implements FormattedCharSink {
|
| 397 | private float maxWidth;
|
| 398 | private int position;
|
| 399 |
|
| 400 | public WidthLimitedCharSink(final float maxWidth) {
|
| 401 | this.maxWidth = maxWidth;
|
| 402 | }
|
| 403 |
|
| 404 | @Override
|
| 405 | public boolean accept(final int position, final Style style, final int codepoint) {
|
| 406 | this.maxWidth = this.maxWidth - StringSplitter.this.widthProvider.getWidth(codepoint, style);
|
| 407 | if (this.maxWidth >= 0.0F) {
|
| 408 | this.position = position + Character.charCount(codepoint);
|
| 409 | return true;
|
| 410 | } else {
|
| 411 | return false;
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | public int getPosition() {
|
| 416 | return this.position;
|
| 417 | }
|
| 418 |
|
| 419 | public void resetPosition() {
|
| 420 | this.position = 0;
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | @FunctionalInterface
|
| 425 | @Environment(EnvType.CLIENT)
|
| 426 | public interface WidthProvider {
|
| 427 | float getWidth(int codepoint, Style style);
|
| 428 | }
|
| 429 | }
|
| 430 |
|