Skip to content

Commit 20f7f07

Browse files
SnaveSutitgitbutler-client
authored andcommitted
🐛 Fix custom fonts breaking Text Display components
1 parent 707068f commit 20f7f07

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/systems/jsonText/wrapping.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 }

src/systems/minecraft/fontManager.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class BitmapFontProvider extends FontProvider {
274274
}
275275

276276
export class MinecraftFont {
277-
static all: MinecraftFont[] = []
277+
static all = new Map<string, MinecraftFont>()
278278

279279
id: string
280280
providers: FontProvider[] = []
@@ -291,18 +291,23 @@ export class MinecraftFont {
291291
this.assetPath = assetPath
292292
this.fallback = fallback
293293

294-
MinecraftFont.all.push(this)
294+
MinecraftFont.all.set(this.id, this)
295295
}
296296

297297
static async getById(id: string) {
298-
let font = MinecraftFont.all.find(font => font.id === id)
298+
let font = MinecraftFont.all.get(id)
299299

300300
if (!font) {
301301
const path = getPathFromResourceLocation(id, 'font') + '.json'
302302
font = new MinecraftFont(id, path)
303303
}
304304

305-
await font.load()
305+
try {
306+
await font.load()
307+
} catch (error) {
308+
console.error(`Failed to load font ${font.id} from ${font.assetPath}:`, error)
309+
return undefined
310+
}
306311

307312
return font
308313
}

0 commit comments

Comments
 (0)