From 7fb55a44e4e52631e8bef7d38de5972c959e2d7c Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Thu, 14 Mar 2024 13:07:33 -0400 Subject: [PATCH] Fix usage of default fonts --- src/core/p5.Renderer2D.js | 7 +++++- test/unit/spec.js | 3 ++- test/unit/visual/cases/typography.js | 20 ++++++++++++++++++ .../With the default font/000.png | Bin 0 -> 848 bytes .../With the default font/metadata.json | 3 +++ .../With the default monospace font/000.png | Bin 0 -> 694 bytes .../metadata.json | 3 +++ test/visual/visualTestList.js | 2 +- 8 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/unit/visual/cases/typography.js create mode 100644 test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default font/000.png create mode 100644 test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default font/metadata.json create mode 100644 test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default monospace font/000.png create mode 100644 test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default monospace font/metadata.json diff --git a/src/core/p5.Renderer2D.js b/src/core/p5.Renderer2D.js index a9be0a6211..e9e2195d30 100644 --- a/src/core/p5.Renderer2D.js +++ b/src/core/p5.Renderer2D.js @@ -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) { diff --git a/test/unit/spec.js b/test/unit/spec.js index 64c8cc8f91..c6a16946c0 100644 --- a/test/unit/spec.js +++ b/test/unit/spec.js @@ -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( diff --git a/test/unit/visual/cases/typography.js b/test/unit/visual/cases/typography.js new file mode 100644 index 0000000000..f7eb3092a6 --- /dev/null +++ b/test/unit/visual/cases/typography.js @@ -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 }); diff --git a/test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default font/000.png b/test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default font/000.png new file mode 100644 index 0000000000000000000000000000000000000000..2df2de71259f776212ffee2a1de6b7a60ee09a40 GIT binary patch literal 848 zcmV-W1F!svP)Px&3Q0skRA@u(*GZ@yRS*W?FX9wIL=Y2$f*3ctaYzh;Mlm>mvr!R5vq=cV32|5i z#6;a>;RKEg1Bwel#9-723c7HfL2;v^7@~*^M^KSyN}7(Yz_+y2-Kzt7fPBOpiNQM`&xvlV=bIA`E7T#dJ8>#h-y8*v}j zPXNePT!2#sAiI$7Ua5CP<+jsz9mi^IKgX6IuKE~ttq!sT#ti^IH!q;W{Fn&eu zx)+z>aBRUhxEcSg(2khKU z!uw3I|DUi;_eK`ri%R@@JW|YERBYy=#rO(m7LcfPF1{_+pT$YI1@}~D6MrLj;C6h3 zB`tbVF6*j6+=Nwl0xw}D{w(_wa2nplJ97mjY+i!rk>AaADpr>YXXD6v6R}ANJy)R8 z&VOn7PF#`CaO{{uP5mEHHY@NH4sH27iRZ4luv-%-9m62&cmlwdZ*W0*oF@qwi$d;AnO49(YGR*B@b3-Nr%b0 zuxdT6G&3aKX_Qa^yso?R)qxO3_ zo-XqwlTVd5@j@}5qx`xdB;tuU$i8yYI*IP7QMT-mUVn(wP12^+##%kn__WzNY5;JH11CTkR zewteSzva{^pP{rm(bl8Wph%D8b_zWx(uuYnod!jEB)3!OL6J_h_2@Jx(j&Q@LJx{` zqOC`#L6IKG?G$=Yq!VpDIt_~SNN%UlgCd=1>(OaYq(^c)g&q{?L|czegCaeW+bQ%O aqsYIIgP}9nQB33j00006~)ya#81+Loq&w*=@80ZuZZX4T$QxI-|#bN2BeI`8JBZy>pOf7M65Z=9izjsvuk^6`f)?{+XdXUf==a%S-)cv{}jpi zb-L||sAl{2kR{Bl=l!;MtCntMS#HSI&)sC6pzrpm_VDf%-{(aHOYG-dEcm%A{B89U zoqdVk?A_mw*{;zJnAz)nqw{vfEUPE}U!3mzuT zo;?2V@q|aOe6z2+Dl z9J(f=eawBibywnya0RjVr(b1G_o$z9FGWy!YfZU#zdJ)MqB2n1>M{=OD#=_ eI-Ju2KY5-1_8L!nW0nU@hzy>telF{r5}E+EgfSfe literal 0 HcmV?d00001 diff --git a/test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default monospace font/metadata.json b/test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default monospace font/metadata.json new file mode 100644 index 0000000000..2d4bfe30da --- /dev/null +++ b/test/unit/visual/screenshots/Typography/textFont() with default fonts/With the default monospace font/metadata.json @@ -0,0 +1,3 @@ +{ + "numScreenshots": 1 +} \ No newline at end of file diff --git a/test/visual/visualTestList.js b/test/visual/visualTestList.js index 7a008a20ef..f52f971180 100644 --- a/test/visual/visualTestList.js +++ b/test/visual/visualTestList.js @@ -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(