Skip to content

Commit ff74e12

Browse files
authored
Merge pull request #7723 from processing/fix/texttopoints-nan
Fix division by 0 in textToPoints
2 parents ebd77b6 + 7e98bb4 commit ff74e12

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/type/p5.Font.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,13 +1016,17 @@ function pathToPoints(cmds, options, font) {
10161016
simplifyThreshold: 0
10171017
});
10181018

1019-
const totalPoints = Math.ceil(path.getTotalLength() * opts.sampleFactor);
1019+
const totalPoints = Math.max(1, Math.ceil(path.getTotalLength() * opts.sampleFactor));
10201020
let points = [];
10211021

10221022
const mode = font._pInst.angleMode();
10231023
const DEGREES = font._pInst.DEGREES;
10241024
for (let i = 0; i < totalPoints; i++) {
1025-
const length = path.getTotalLength() * (i / (totalPoints - 1));
1025+
const length = path.getTotalLength() * (
1026+
totalPoints === 1
1027+
? 0
1028+
: (i / (totalPoints - 1))
1029+
);
10261030
points.push({
10271031
...path.getPointAtLength(length),
10281032
get angle() {

test/unit/type/p5.Font.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ suite('p5.Font', function () {
3939
assert.property(bbox, 'w');
4040
assert.property(bbox, 'h');
4141
});
42+
43+
suite('textToPoints', () => {
44+
test('contains no NaNs', async () => {
45+
const pFont = await myp5.loadFont(fontFile);
46+
const pts = pFont.textToPoints('hello, world!', 0, 0);
47+
for (const pt of pts) {
48+
expect(pt.x).not.toBeNaN();
49+
expect(pt.y).not.toBeNaN();
50+
}
51+
});
52+
});
4253
});

0 commit comments

Comments
 (0)