diff --git a/devserver/src/main.ts b/devserver/src/main.ts index 8c8d2a9..0387ca5 100644 --- a/devserver/src/main.ts +++ b/devserver/src/main.ts @@ -416,8 +416,7 @@ function startRenderLoop() { if (state.centerX) { try { const glyphs = lib.parser.parse(state.textComponent); - const rawWidth = lib.layout.measureWidth(glyphs, fontResolver); - const textWidthGui = Math.ceil(rawWidth); + const textWidthGui = lib.layout.measureWidth(glyphs, fontResolver); const guiX = Math.floor(guiWidth / 2) - Math.floor(textWidthGui / 2); screenX = guiX * state.guiScale; } catch { diff --git a/devserver/src/test-runner.ts b/devserver/src/test-runner.ts index f2b4788..23d2778 100644 --- a/devserver/src/test-runner.ts +++ b/devserver/src/test-runner.ts @@ -127,8 +127,7 @@ async function runTest(config: TestConfig): Promise { if (position.centerX) { const glyphs = lib.parser.parse(text); - const rawWidth = lib.layout.measureWidth(glyphs, fontResolver); - const textWidthGui = Math.ceil(rawWidth); + const textWidthGui = lib.layout.measureWidth(glyphs, fontResolver); const guiX = Math.floor(guiWidth / 2) - Math.floor(textWidthGui / 2); screenX = guiX * viewport.guiScale; } else { diff --git a/package.json b/package.json index 9b2b53a..f03a625 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "libmojangles", "type": "module", - "version": "1.7.5", + "version": "1.7.6", "module": "dist/index.js", "types": "dist/index.d.ts", "description": "Pixel perfect Minecraft text rendering on the web, based on resourcepacks.", diff --git a/src/layout/engine.ts b/src/layout/engine.ts index a09b6f8..4d7a900 100644 --- a/src/layout/engine.ts +++ b/src/layout/engine.ts @@ -132,7 +132,8 @@ export class TextLayoutEngineImpl implements TextLayoutEngine { }; } - measureWidth(glyphs: StyledGlyph[], font: Font | FontResolver): number { + // Mirrors StringSplitter.stringWidth: the exact, unrounded advance sum. + measureWidthExact(glyphs: StyledGlyph[], font: Font | FontResolver): number { const resolveFont = toResolver(font); let width = 0; for (const glyph of glyphs) { @@ -145,6 +146,10 @@ export class TextLayoutEngineImpl implements TextLayoutEngine { return width; } + measureWidth(glyphs: StyledGlyph[], font: Font | FontResolver): number { + return Math.ceil(this.measureWidthExact(glyphs, font)); + } + measureHeight( glyphs: StyledGlyph[], font: Font | FontResolver, diff --git a/src/types/layout.ts b/src/types/layout.ts index 6fbcd26..ef3a585 100644 --- a/src/types/layout.ts +++ b/src/types/layout.ts @@ -40,6 +40,7 @@ export interface TextLayoutEngine { options?: LayoutOptions ): LayoutResult; measureWidth(glyphs: StyledGlyph[], font: Font | FontResolver): number; + measureWidthExact(glyphs: StyledGlyph[], font: Font | FontResolver): number; measureHeight( glyphs: StyledGlyph[], font: Font | FontResolver,