Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
}
}
15 changes: 9 additions & 6 deletions src/type/textCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1905,12 +1905,15 @@ 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.length
? boxes.reduce((m, b) => Math.max(m, b.w || 0), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super minor, but I think since you provide the initial value of 0 in the reduce, we don't additionally need to have a ternary on boxes.length; the reduce will already return 0 here.

: 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the body of this test might be indented one time too many?

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