@@ -254,14 +254,15 @@ export async function wrapJsonText(jsonText: TextComponent, maxLineWidth = 200)
254254
255255 const words = await parseWords ( jsonText . toJSON ( ) )
256256 const lines : Line [ ] = [ ]
257- // FIXME - This will not work for custom fonts
258- const font = await MinecraftFont . getById ( 'minecraft:default' )
257+ // The `getWordWidth` function handles getting the width of words with different / custom fonts
258+ const font = ( await MinecraftFont . getById ( 'minecraft:default' ) ) !
259259
260260 let backgroundWidth = 0
261261 let currentLine : Line = { words : [ ] , width : 0 }
262262 for ( const word of words ) {
263263 const wordWidth = await font . getWordWidth ( word )
264264 const wordStyles = [ ...word . styles ]
265+
265266 // If the word is longer than than the max line width, split it into multiple lines
266267 if ( wordWidth - 1 > maxLineWidth ) {
267268 if ( currentLine . words . length ) {
@@ -344,6 +345,15 @@ export async function wrapJsonText(jsonText: TextComponent, maxLineWidth = 200)
344345 currentLine . words . pop ( )
345346 currentLine . width -= lastWord . width
346347 }
348+ if ( word . text . at ( 0 ) === ' ' ) {
349+ // If the word starts with a space, remove it.
350+ word . text = word . text . slice ( 1 )
351+ word . styles . forEach ( span => {
352+ span . start = Math . max ( 0 , span . start - 1 )
353+ span . end = Math . max ( 0 , span . end - 1 )
354+ } )
355+ // word.width = await font.getWordWidth(word)
356+ }
347357 lines . push ( currentLine )
348358 backgroundWidth = Math . max ( backgroundWidth , currentLine . width )
349359 currentLine = { words : [ ] , width : 0 }
0 commit comments