66import java .util .HashMap ;
77import java .util .Map ;
88
9+ /**
10+ * Represents a resource pack font, containing its ID and character widths
11+ */
912public class Font {
1013
1114 // Static code
@@ -51,9 +54,19 @@ public class Font {
5154 register (DEFAULT );
5255 }
5356
57+ /**
58+ * Gets a registered font by its ID
59+ * @param id the id of the font
60+ * @return the font for the given ID, or null if was not found
61+ */
5462 @ Nullable
5563 public static Font getRegistered (String id ) { return registeredFonts .get (id ); }
5664
65+ /**
66+ * Registers a Font to be accessed statically later, through {@link Font#getRegistered(String)}
67+ * @param font the Font to be registered
68+ * @throws IllegalArgumentException if a font with the same ID already is registered
69+ */
5770 public static void register (Font font ) {
5871 String id = font .getID ();
5972 if (registeredFonts .containsKey (id )) {
@@ -62,6 +75,11 @@ public static void register(Font font) {
6275 registeredFonts .put (id , font );
6376 }
6477
78+ /**
79+ * Unregisters a Font from the static context
80+ * @param font the font to unregister
81+ * @return true if the font was unregistered, false if it was not previously registered
82+ */
6583 @ SuppressWarnings ("unused" )
6684 public static boolean unregister (Font font ) {
6785 boolean success = false ;
@@ -73,23 +91,56 @@ public static boolean unregister(Font font) {
7391
7492 private final String id ;
7593 private final int height ;
94+ private Font parent ;
7695 private Map <Character ,Integer > widths ;
7796
97+ /**
98+ * @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
99+ * @param height the default height of the characters in the font, as specified in the resource pack
100+ */
78101 public Font (@ NotNull String id , int height ) {
79102 this .id = id ;
80103 this .height = height ;
81104 }
82105
106+ /**
107+ * Creates a font that inherits its character widths from a parent font
108+ * Used for fonts that share the same textures with another one
109+ * @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
110+ * @param height the default height of the characters in the font, as specified in the resource pack
111+ * @param parent the font to inherit its character widths from
112+ */
113+ public Font (@ NotNull String id , int height , Font parent ) {
114+ this .id = id ;
115+ this .height = height ;
116+ this .parent = parent ;
117+ }
118+
119+ /**
120+ * Registers the width of a character for this font, if different from the default of 6
121+ * @param character the character to register the width for
122+ * @param width the width of the character
123+ */
83124 public void registerWidth (char character , int width ) {
84125 if (widths == null ) { widths = new HashMap <>(); }
85126 widths .put (character , width );
86127 }
87128
129+ /**
130+ * Gets the width of a given character for this font
131+ * @param character the character to get the width for
132+ * @param scale whether to scale the width according to the font's height
133+ * @return the width of the character
134+ */
88135 public int getWidth (char character , boolean scale ) {
89136 Integer result = null ;
90137 if (this == DEFAULT ) { result = widths .getOrDefault (character , 6 ); } else {
91138 try { result = widths .get (character ); } catch (NullPointerException ignored ) { }
92- if (result == null ) { result = DEFAULT .getWidth (character , false ); }
139+ if (result == null ) {
140+ result = parent == null ?
141+ DEFAULT .getWidth (character , false ) :
142+ parent .getWidth (character , false );
143+ }
93144 }
94145 if (scale && this != DEFAULT && character != ' ' ) {
95146 // Formula figured out experimentally (pain)
@@ -98,17 +149,32 @@ public int getWidth(char character, boolean scale) {
98149 return result ;
99150 }
100151
152+ /**
153+ * Calculates the width of a given {@link String} for this font
154+ * @param text the String to calculate the width for
155+ * @param scale whether to scale the width according to the font's height
156+ * @return the calculated width of the character
157+ */
101158 public int getWidth (String text , boolean scale ) {
102159 int total = 0 ;
103160 for (Character character : text .toCharArray ()) { total += getWidth (character , scale ); }
104161 return total ;
105162 }
106163
164+ /**
165+ * @return the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
166+ */
107167 @ Override
108168 public String toString () { return id ; }
109169
170+ /**
171+ * @return the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
172+ */
110173 public String getID () { return id ; }
111174
175+ /**
176+ * @return the default height of the characters in the font, as specified in the resource pack
177+ */
112178 @ SuppressWarnings ("unused" )
113179 public int getHeight () { return height ; }
114180}
0 commit comments