Skip to content

Commit

Permalink
Merge pull request #6861 from davepagurek/fix/default-fonts
Browse files Browse the repository at this point in the history
Fix usage of default fonts
  • Loading branch information
Qianqianye authored Mar 14, 2024
2 parents 2037155 + 7fb55a4 commit e1ca2e0
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -1318,8 +1318,13 @@ class Renderer2D extends p5.Renderer {
this._setProperty('_textStyle', this._textFont.font.styleName);
}

let fontNameString = font || 'sans-serif';
if (/\s/.exec(fontNameString)) {
// If the name includes spaces, surround in quotes
fontNameString = `"${fontNameString}"`;
}
this.drawingContext.font = `${this._textStyle || 'normal'} ${this._textSize ||
12}px "${font || 'sans-serif'}"`;
12}px ${fontNameString}`;

this.drawingContext.textAlign = this._textAlign;
if (this._textBaseline === constants.CENTER) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ var spec = {
'visual/cases': [
// Add the visual tests that you want run as part of CI here. Feel free
// to omit some for speed if they should only be run manually.
'webgl'
'webgl',
'typography'
]
};
document.write(
Expand Down
20 changes: 20 additions & 0 deletions test/unit/visual/cases/typography.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
visualSuite('Typography', function() {
visualSuite('textFont() with default fonts', function() {
visualTest('With the default font', function (p5, screenshot) {
p5.createCanvas(50, 50);
p5.textSize(20);
p5.textAlign(p5.LEFT, p5.TOP);
p5.text('test', 0, 0);
screenshot();
});

visualTest('With the default monospace font', function (p5, screenshot) {
p5.createCanvas(50, 50);
p5.textSize(20);
p5.textFont('monospace');
p5.textAlign(p5.LEFT, p5.TOP);
p5.text('test', 0, 0);
screenshot();
});
});
}, { focus: true });
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
}
2 changes: 1 addition & 1 deletion test/visual/visualTestList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// List all visual test files here that should be manually run
const visualTestList = ['webgl'];
const visualTestList = ['webgl', 'typography'];

for (const file of visualTestList) {
document.write(
Expand Down

0 comments on commit e1ca2e0

Please sign in to comment.