Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visual tests for textToPoints, addTextToContours #7427

Merged
merged 2 commits into from
Dec 15, 2024
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
19 changes: 16 additions & 3 deletions src/type/p5.Font.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,23 @@ function font(p5, fn) {
({ width, height, options } = this._parseArgs(width, height, options));

// lineate and get the glyphs for each line
let glyphs = this.textToPaths(str, x, y, width, height, options);
let commands = this.textToPaths(str, x, y, width, height, options);

// convert glyphs to points array with {sampleFactor, simplifyThreshold}
return pathToPoints(glyphs, options);
return pathToPoints(commands, options);
}

textToContours(str, x, y, width, height, options) {
const cmds = this.textToPaths(str, x, y, width, height, options);
const cmdContours = [];
for (const cmd of cmds) {
if (cmd[0] === 'M') {
cmdContours.push([]);
}
cmdContours[cmdContours.length - 1].push(cmd);
}

return cmdContours.map((commands) => pathToPoints(commands, options));
}

static async list(log = false) { // tmp
Expand Down Expand Up @@ -1122,7 +1135,7 @@ function font(p5, fn) {
}

let opts = parseOpts(options, {
sampleFactor: 0.05,
sampleFactor: 0.25,
simplifyThreshold: 0
});

Expand Down
83 changes: 83 additions & 0 deletions test/unit/visual/cases/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,87 @@ visualSuite("Typography", function () {
screenshot();
});
});

visualSuite('textToPoints', function() {
visualTest('Fonts can be converted to points', async function(p5, screenshot) {
p5.createCanvas(100, 100);
const font = await p5.loadFont(
'/unit/assets/Inconsolata-Bold.ttf'
);
p5.background(255);
p5.strokeWeight(2);
p5.textSize(50);
const pts = font.textToPoints('p5*js', 0, 50);
p5.beginShape(p5.POINTS);
for (const { x, y } of pts) p5.vertex(x, y);
p5.endShape();
screenshot();
});

visualTest('Sampling density can be changed', async function(p5, screenshot) {
p5.createCanvas(100, 100);
const font = await p5.loadFont(
'/unit/assets/Inconsolata-Bold.ttf'
);
p5.background(255);
p5.strokeWeight(2);
p5.textSize(50);
const pts = font.textToPoints('p5*js', 0, 50, { sampleFactor: 0.5 });
p5.beginShape(p5.POINTS);
for (const { x, y } of pts) p5.vertex(x, y);
p5.endShape();
screenshot();
});
});

visualSuite('textToContours', function() {
visualTest('Fonts can be converted to points grouped by contour', async function(p5, screenshot) {
p5.createCanvas(100, 100);
const font = await p5.loadFont(
'/unit/assets/Inconsolata-Bold.ttf'
);
p5.background(200);
p5.strokeWeight(2);
p5.textSize(50);
const contours = font.textToContours('p5*js', 0, 50, { samplingDensity: 0.5 })
p5.beginShape();
for (const pts of contours) {
p5.beginContour();
for (const { x, y } of pts) p5.vertex(x, y);
p5.endContour(p5.CLOSE);
}
p5.endShape();
screenshot();
});
});

visualSuite('textToPaths', function() {
visualTest('Fonts can be converted to drawing context commands', async function(p5, screenshot) {
p5.createCanvas(100, 100);
const font = await p5.loadFont(
'/unit/assets/Inconsolata-Bold.ttf'
);
p5.background(200);
p5.strokeWeight(2);
p5.textSize(50);
const cmds = font.textToPaths('p5*js', 0, 50)
p5.drawingContext.beginPath();
for (const [type, ...args] of cmds) {
if (type === 'M') {
p5.drawingContext.moveTo(...args);
} else if (type === 'L') {
p5.drawingContext.lineTo(...args);
} else if (type === 'C') {
p5.drawingContext.bezierCurveTo(...args);
} else if (type === 'Q') {
p5.drawingContext.quadraticCurveTo(...args);
} else if (type === 'Z') {
p5.drawingContext.closePath();
}
}
p5.drawingContext.fill();
p5.drawingContext.stroke();
screenshot();
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading