Skip to content

Commit

Permalink
Fix text_width issues, add the rest of the ascii.png chars (#2331)
Browse files Browse the repository at this point in the history
* Fix text_width issues, add the rest of the ascii.png chars

* Use array for text_width ascii chars
  • Loading branch information
mergu committed May 2, 2022
1 parent 9f4ced1 commit 5bc7925
Showing 1 changed file with 25 additions and 11 deletions.
Expand Up @@ -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<Character, Integer> 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");
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5bc7925

Please sign in to comment.