Get the width of the given text as it would be rendered using this font. @param text The text. @return The width in pixels.
(@NotNull String text)
| 52 | * @return The width in pixels. |
| 53 | */ |
| 54 | public int getWidth(@NotNull String text) { |
| 55 | if (!isValid(text)) { |
| 56 | throw new IllegalArgumentException("text contains invalid characters"); |
| 57 | } |
| 58 | |
| 59 | if (text.length() == 0) { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int result = 0; |
| 64 | for (int i = 0; i < text.length(); ++i) { |
| 65 | char ch = text.charAt(i); |
| 66 | if (ch == ChatColor.COLOR_CHAR) continue; |
| 67 | result += chars.get(ch).getWidth(); |
| 68 | } |
| 69 | result += text.length() - 1; // Account for 1px spacing between characters |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the height of this font. |