From 5bc792574f85e2cbf64c3eb4a031758b2370d82a Mon Sep 17 00:00:00 2001 From: Jay Hopkins Date: Sun, 1 May 2022 22:33:36 -0400 Subject: [PATCH] Fix text_width issues, add the rest of the ascii.png chars (#2331) * Fix text_width issues, add the rest of the ascii.png chars * Use array for text_width ascii chars --- .../denizen/utilities/TextWidthHelper.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/utilities/TextWidthHelper.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/TextWidthHelper.java index 0e67ddfc00..3a820081c7 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/TextWidthHelper.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/TextWidthHelper.java @@ -3,30 +3,44 @@ import com.denizenscript.denizencore.utilities.AsciiMatcher; import org.bukkit.ChatColor; +import java.util.HashMap; + public class TextWidthHelper { - public static int[] characterWidthMap = new int[128]; + public static int[] asciiWidthMap = new int[128]; + public static HashMap characterWidthMap = new HashMap<>(); public static void setWidth(int width, String chars) { for (char c : chars.toCharArray()) { - characterWidthMap[c] = width; + if (c < 128) { + asciiWidthMap[c] = width; + } + else { + characterWidthMap.put(c, width); + } } } static { for (int i = 0; i < 128; i++) { - characterWidthMap[i] = 6; + asciiWidthMap[i] = 6; } - setWidth(2, "!,.:;|i`"); - setWidth(3, "'l"); - setWidth(4, " []tI"); - setWidth(5, "\"()*<>fk{}"); - // all other ASCII characters are length=6 - setWidth(7, "@~"); + // Covers all symbols in the default ascii.png texture file + setWidth(2, "!,.:;|i'"); + setWidth(3, "l`"); + setWidth(4, " (){}[]tI\"*"); + setWidth(5, "<>fkªº▌⌡°ⁿ²"); + // all other characters are length=6 + setWidth(7, "@~«»≡≈√"); + setWidth(8, "░╢╖╣║╗╝╜∅⌠"); + setWidth(9, "▒▓└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┌█▄▐▀"); } public static int getWidth(char c) { - return c > 127 ? 6 : characterWidthMap[c]; + if (c < 128) { + return asciiWidthMap[c]; + } + return characterWidthMap.getOrDefault(c, 6); } public static AsciiMatcher formatCharCodeMatcher = new AsciiMatcher("klmnoKLMNO"); @@ -59,7 +73,7 @@ else if (!formatCharCodeMatcher.isMatch(c2)) { i++; continue; } - total += getWidth(c) + (bold ? 2 : 0); + total += getWidth(c) + (bold ? 1 : 0); if (c == '\n') { if (total > maxWidth) { maxWidth = total;