-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for font weight + fix Google Font loading
- Loading branch information
1 parent
6517307
commit 7fb18e8
Showing
21 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* API: | ||
* loadFont("https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,[email protected],200..800&display=swap") | ||
* loadFont("{ font-family: "Bricolage Grotesque", serif; font-optical-sizing: auto; font-weight: <weight> font-style: normal; font-variation-settings: "wdth" 100; }); | ||
* loadFont("@font-face { font-family: "Bricolage Grotesque", serif; font-optical-sizing: auto; font-weight: <weight> font-style: normal; font-variation-settings: "wdth" 100; }); | ||
* loadFont({ | ||
* fontFamily: '"Bricolage Grotesque", serif'; | ||
* fontOpticalSizing: 'auto'; | ||
|
@@ -31,6 +31,14 @@ | |
*/ | ||
import Typr from './lib/Typr.js'; | ||
|
||
function unquote(name) { | ||
// Unquote name from CSS | ||
if ((name.startsWith('"') || name.startsWith("'")) && name.at(0) === name.at(-1)) { | ||
return name.slice(1, -1).replace(/\/(['"])/g, '$1'); | ||
} | ||
return name; | ||
} | ||
|
||
function font(p5, fn) { | ||
|
||
const pathArgCounts = { M: 2, L: 2, C: 6, Q: 4 }; | ||
|
@@ -455,6 +463,43 @@ function font(p5, fn) { | |
|
||
let { path, name, success, error, descriptors } = parseCreateArgs(...args); | ||
|
||
let isCSS = path.includes('@font-face'); | ||
|
||
if (!isCSS) { | ||
const info = await fetch(path, { method: 'HEAD' }); | ||
const isCSSFile = info.headers.get('content-type')?.startsWith('text/css'); | ||
if (isCSSFile) { | ||
isCSS = true; | ||
path = await fetch(path).then((res) => res.text()); | ||
} | ||
} | ||
|
||
if (isCSS) { | ||
const stylesheet = new CSSStyleSheet(); | ||
await stylesheet.replace(path); | ||
const fontPromises = []; | ||
for (const rule of stylesheet.cssRules) { | ||
if (rule instanceof CSSFontFaceRule) { | ||
const style = rule.style; | ||
let name = unquote(style.getPropertyValue('font-family')); | ||
const src = style.getPropertyValue('src'); | ||
const fontDescriptors = { ...(descriptors || {}) }; | ||
for (const key of style) { | ||
if (key === 'font-family' || key === 'src') continue; | ||
const camelCaseKey = key | ||
.replace(/^font-/, '') | ||
.split('-') | ||
.map((v, i) => i === 0 ? v : `${v[0].toUpperCase()}${v.slice(1)}`) | ||
.join(''); | ||
fontDescriptors[camelCaseKey] = style.getPropertyValue(key); | ||
} | ||
fontPromises.push(create(this, name, src, fontDescriptors)); | ||
} | ||
} | ||
const fonts = await Promise.all(fontPromises); | ||
return fonts[0]; // TODO: handle multiple faces? | ||
} | ||
|
||
let pfont; | ||
try { | ||
// load the raw font bytes | ||
|
@@ -465,13 +510,20 @@ function font(p5, fn) { | |
|
||
// parse the font data | ||
let fonts = Typr.parse(result); | ||
console.log(fonts[0]) | ||
// TODO: generate descriptors from font in the future | ||
|
||
if (fonts.length !== 1 || fonts[0].cmap === undefined) { | ||
throw Error(23); | ||
} | ||
|
||
// make sure we have a valid name | ||
name = name || extractFontName(fonts[0], path); | ||
if (!name) { | ||
name = extractFontName(fonts[0], path); | ||
if (name.includes(' ')) { | ||
name = name.replace(/ /g, '_'); | ||
} | ||
} | ||
|
||
// create a FontFace object and pass it to the p5.Font constructor | ||
pfont = await create(this, name, path, descriptors, fonts[0]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,72 @@ visualSuite("Typography", function () { | |
p5.text("test", 0, 0); | ||
screenshot(); | ||
}); | ||
|
||
visualTest('with a Google Font URL', async function(p5, screenshot) { | ||
p5.createCanvas(100, 100); | ||
const font = await p5.loadFont( | ||
'https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,[email protected],200..800&display=swap' | ||
); | ||
p5.textFont(font); | ||
p5.textAlign(p5.LEFT, p5.TOP); | ||
p5.textSize(35); | ||
p5.text('p5*js', 0, 10, p5.width); | ||
screenshot(); | ||
}); | ||
}); | ||
|
||
visualSuite('textWeight', function() { | ||
visualTest('can control non-variable fonts', async function (p5, screenshot) { | ||
p5.createCanvas(100, 100); | ||
const font = await p5.loadFont( | ||
'https://fonts.googleapis.com/css2?family=Sniglet:wght@400;800&display=swap' | ||
); | ||
|
||
for (const weight of [400, 800]) { | ||
p5.background(255); | ||
p5.textFont(font); | ||
p5.textAlign(p5.LEFT, p5.TOP); | ||
p5.textSize(35); | ||
p5.textWeight(weight); | ||
p5.text('p5*js', 0, 10, p5.width); | ||
screenshot(); | ||
} | ||
}); | ||
|
||
visualTest('can control variable fonts', async function (p5, screenshot) { | ||
p5.createCanvas(100, 100); | ||
const font = await p5.loadFont( | ||
'https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap' | ||
); | ||
|
||
for (let weight = 400; weight <= 800; weight += 100) { | ||
p5.background(255); | ||
p5.textFont(font); | ||
p5.textAlign(p5.LEFT, p5.TOP); | ||
p5.textSize(35); | ||
p5.textWeight(weight); | ||
p5.text('p5*js', 0, 10, p5.width); | ||
screenshot(); | ||
} | ||
}); | ||
|
||
visualTest('can control variable fonts from files', async function (p5, screenshot) { | ||
p5.createCanvas(100, 100); | ||
const font = await p5.loadFont( | ||
'/unit/assets/BricolageGrotesque-Variable.ttf', | ||
{ weight: '200 800' } | ||
); | ||
|
||
for (let weight = 400; weight <= 800; weight += 100) { | ||
p5.background(255); | ||
p5.textFont(font); | ||
p5.textAlign(p5.LEFT, p5.TOP); | ||
p5.textSize(35); | ||
p5.textWeight(weight); | ||
p5.text('p5*js', 0, 10, p5.width); | ||
screenshot(); | ||
} | ||
}); | ||
}); | ||
|
||
visualSuite("textAlign", function () { // TEMPORARY SKIP | ||
|
Binary file added
BIN
+2.33 KB
test/unit/visual/screenshots/Typography/textFont/with a Google Font URL/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Typography/textFont/with a Google Font URL/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+2.7 KB
...visual/screenshots/Typography/textWeight/can control non-variable fonts/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.66 KB
...visual/screenshots/Typography/textWeight/can control non-variable fonts/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...nit/visual/screenshots/Typography/textWeight/can control non-variable fonts/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 2 | ||
} |
Binary file added
BIN
+2.7 KB
...screenshots/Typography/textWeight/can control variable fonts from files/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.7 KB
...screenshots/Typography/textWeight/can control variable fonts from files/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.67 KB
...screenshots/Typography/textWeight/can control variable fonts from files/002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.63 KB
...screenshots/Typography/textWeight/can control variable fonts from files/003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.61 KB
...screenshots/Typography/textWeight/can control variable fonts from files/004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...ual/screenshots/Typography/textWeight/can control variable fonts from files/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 5 | ||
} |
Binary file added
BIN
+2.59 KB
...nit/visual/screenshots/Typography/textWeight/can control variable fonts/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.59 KB
...nit/visual/screenshots/Typography/textWeight/can control variable fonts/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.59 KB
...nit/visual/screenshots/Typography/textWeight/can control variable fonts/002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.56 KB
...nit/visual/screenshots/Typography/textWeight/can control variable fonts/003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.56 KB
...nit/visual/screenshots/Typography/textWeight/can control variable fonts/004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Typography/textWeight/can control variable fonts/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 5 | ||
} |