Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {},
"javascript.format.enable": false
}
}
2 changes: 1 addition & 1 deletion lib/empty-example/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ function setup() {

function draw() {
// put drawing code here
}
}
13 changes: 7 additions & 6 deletions src/type/textCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/unit/type/p5.Font.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading