Correctly calculated new font width throughout script editor endpoints (for italics/bold)

This commit is contained in:
bigguy345 committed 2026-03-13 03:00:54 +02:00
1 parent 254655cb1d
commit f1f3b17402
6 files changed
+228 -86

No files matched your search

@@ -762,6 +762,43 @@ public class StringCache {
return (int) (entry.advance * renderScale());
}
/**
* Return the width of a string in pixels when rendered with the specified font style.
* This is needed when measuring text that will be rendered with bold/italic formatting,
* since bold glyphs are wider than plain glyphs.
*
* <p>The font style is applied by prepending the appropriate Minecraft formatting codes
* (§l for bold, §o for italic) to the string before measuring. This causes cacheString()
* to lay out the entire string using the specified font style.
*
* @param str compute the width of this string
* @param fontStyle combination of {@link java.awt.Font#BOLD} and {@link java.awt.Font#ITALIC}
* @return the width in pixels (in the scaled GUI coordinate system)
*/
public int getStringWidth(String str, int fontStyle) {
/* Check for invalid arguments */
if (str == null || str.isEmpty()) {
return 0;
}
/* If plain style, delegate to the normal method (no prefix needed) */
if (fontStyle == Font.PLAIN) {
return getStringWidth(str);
}
/* Build the style prefix: §l for bold, §o for italic */
StringBuilder sb = new StringBuilder();
if ((fontStyle & Font.BOLD) != 0) {
sb.append('\u00A7').append('l');
}
if ((fontStyle & Font.ITALIC) != 0) {
sb.append('\u00A7').append('o');
}
sb.append(str);
return getStringWidth(sb.toString());
}
/**
* Return the number of characters in a string that will completly fit inside the specified width when rendered, with
* or without prefering to break the line at whitespace instead of breaking in the middle of a word. This private provides