diff --git a/.vscode/settings.json b/.vscode/settings.json index 88d67b9835..fdf39476cc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,4 +10,4 @@ "editor.formatOnSave": false, "editor.codeActionsOnSave": {}, "javascript.format.enable": false -} \ No newline at end of file +} diff --git a/lib/empty-example/sketch.js b/lib/empty-example/sketch.js index 33ce6b4450..336fa0777f 100644 --- a/lib/empty-example/sketch.js +++ b/lib/empty-example/sketch.js @@ -4,4 +4,4 @@ function setup() { function draw() { // put drawing code here - } \ No newline at end of file + } diff --git a/src/type/textCore.js b/src/type/textCore.js index 5e0efbe8fe..28e3e8888a 100644 --- a/src/type/textCore.js +++ b/src/type/textCore.js @@ -1905,12 +1905,13 @@ function textCore(p5, fn) { // adjust the bounding boxes based on horiz. text alignment if (lines.length > 1) { - // Call the 2D mode version: the WebGL mode version does additional - // alignment adjustments to account for how WebGL renders text. - boxes.forEach(bb => - bb.x += p5.Renderer2D.prototype._xAlignOffset - .call(this, textAlign, width) - ); + // When width is not provided (e.g., fontBounds path), fall back to the widest line. + const maxWidth = boxes.reduce((m, b) => Math.max(m, b.w || 0), 0); + + boxes.forEach((bb) => { + const w = (width ?? maxWidth); + bb.x += p5.Renderer2D.prototype._xAlignOffset.call(this, textAlign, w); + }); } // adjust the bounding boxes based on vert. text alignment diff --git a/test/unit/type/p5.Font.js b/test/unit/type/p5.Font.js index 4bb10e193e..d3592215c1 100644 --- a/test/unit/type/p5.Font.js +++ b/test/unit/type/p5.Font.js @@ -40,6 +40,16 @@ suite('p5.Font', function () { assert.property(bbox, 'h'); }); + test('fontBounds no NaN (multiline + CENTER)', async () => { + const pFont = await myp5.loadFont(fontFile); + myp5.textAlign(myp5.CENTER, myp5.CENTER); + const b = pFont.fontBounds('Hello,\nWorld!', 50, 50, 24); + expect(b.x).not.toBeNaN(); + expect(b.y).not.toBeNaN(); + expect(b.w).not.toBeNaN(); + expect(b.h).not.toBeNaN(); + }); + suite('textToPoints', () => { test('contains no NaNs', async () => { const pFont = await myp5.loadFont(fontFile);