Skip to content

Commit 752a7fe

Browse files
authored
Merge pull request #8083 from perminder-17/fontBounds-NaN
textToContours is broken under textAlign(CENTER,CENTER) and use linebreak
2 parents a4fbdaa + 86ba30d commit 752a7fe

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
"editor.formatOnSave": false,
1111
"editor.codeActionsOnSave": {},
1212
"javascript.format.enable": false
13-
}
13+
}

lib/empty-example/sketch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ function setup() {
44

55
function draw() {
66
// put drawing code here
7-
}
7+
}

src/type/textCore.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,13 @@ function textCore(p5, fn) {
19051905

19061906
// adjust the bounding boxes based on horiz. text alignment
19071907
if (lines.length > 1) {
1908-
// Call the 2D mode version: the WebGL mode version does additional
1909-
// alignment adjustments to account for how WebGL renders text.
1910-
boxes.forEach(bb =>
1911-
bb.x += p5.Renderer2D.prototype._xAlignOffset
1912-
.call(this, textAlign, width)
1913-
);
1908+
// When width is not provided (e.g., fontBounds path), fall back to the widest line.
1909+
const maxWidth = boxes.reduce((m, b) => Math.max(m, b.w || 0), 0);
1910+
1911+
boxes.forEach((bb) => {
1912+
const w = (width ?? maxWidth);
1913+
bb.x += p5.Renderer2D.prototype._xAlignOffset.call(this, textAlign, w);
1914+
});
19141915
}
19151916

19161917
// adjust the bounding boxes based on vert. text alignment

test/unit/type/p5.Font.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ suite('p5.Font', function () {
4040
assert.property(bbox, 'h');
4141
});
4242

43+
test('fontBounds no NaN (multiline + CENTER)', async () => {
44+
const pFont = await myp5.loadFont(fontFile);
45+
myp5.textAlign(myp5.CENTER, myp5.CENTER);
46+
const b = pFont.fontBounds('Hello,\nWorld!', 50, 50, 24);
47+
expect(b.x).not.toBeNaN();
48+
expect(b.y).not.toBeNaN();
49+
expect(b.w).not.toBeNaN();
50+
expect(b.h).not.toBeNaN();
51+
});
52+
4353
suite('textToPoints', () => {
4454
test('contains no NaNs', async () => {
4555
const pFont = await myp5.loadFont(fontFile);

0 commit comments

Comments
 (0)