Skip to content

Commit fdd141e

Browse files
committed
fixing-issue
1 parent a4fbdaa commit fdd141e

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"editor.defaultFormatter": null,
1010
"editor.formatOnSave": false,
1111
"editor.codeActionsOnSave": {},
12-
"javascript.format.enable": false
12+
"javascript.format.enable": false,
13+
"liveServer.settings.port": 5501
1314
}

lib/empty-example/sketch.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
function setup() {
2-
// put setup code here
3-
}
4-
5-
function draw() {
6-
// put drawing code here
7-
}
2+
createCanvas(300, 200);
3+
background(240);
4+
5+
textSize(32);
6+
textAlign(CENTER,CENTER);
7+
textFont('Georgia');
8+
9+
let txt = "Hello, \nWorld!";
10+
// Compute the bounding box based on the font's intrinsic metrics
11+
let bounds = fontBounds(txt, 50, 50);
12+
13+
fill(0);
14+
text(txt, 50, 50);
15+
16+
noFill();
17+
stroke('green');
18+
rect(bounds.x, bounds.y, bounds.w, bounds.h);
19+
20+
noStroke();
21+
fill(50);
22+
textSize(15);
23+
text("Font Bounds: x=" + bounds.x.toFixed(1) + ", y=" + bounds.y.toFixed(1) +
24+
", w=" + bounds.w.toFixed(1) + ", h=" + bounds.h.toFixed(1), 78, 100);
25+
}

src/type/textCore.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,15 @@ 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.length
1910+
? boxes.reduce((m, b) => Math.max(m, b.w || 0), 0)
1911+
: 0;
1912+
1913+
boxes.forEach((bb) => {
1914+
const w = (width ?? maxWidth);
1915+
bb.x += p5.Renderer2D.prototype._xAlignOffset.call(this, textAlign, w);
1916+
});
19141917
}
19151918

19161919
// 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)