Skip to content

Commit 3f910f6

Browse files
committed
Restructure to be more pluggable
1 parent c39ad5c commit 3f910f6

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/type/p5.Font.js

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,32 @@ function font(p5, fn) {
816816
*/
817817
p5.Font = Font;
818818

819+
/**
820+
* @private
821+
*/
822+
fn.parseFontData = async function(pathOrData) {
823+
// load the raw font bytes
824+
let result = pathOrData instanceof Uint8Array
825+
? pathOrData
826+
: await fn.loadBytes(path);
827+
//console.log('result:', result);
828+
829+
if (!result) {
830+
throw Error('Failed to load font data');
831+
}
832+
833+
// parse the font data
834+
let fonts = Typr.parse(result);
835+
836+
// TODO: generate descriptors from font in the future
837+
838+
if (fonts.length === 0 || fonts[0].cmap === undefined) {
839+
throw Error('parsing font data');
840+
}
841+
842+
return fonts[0];
843+
};
844+
819845
/**
820846
* Loads a font and creates a <a href="#/p5.Font">p5.Font</a> object.
821847
* `loadFont()` can load fonts in either .otf or .ttf format. Loaded fonts can
@@ -996,37 +1022,39 @@ function font(p5, fn) {
9961022
.join('');
9971023
fontDescriptors[camelCaseKey] = style.getPropertyValue(key);
9981024
}
999-
fontPromises.push(create(this, name, src, fontDescriptors));
1025+
fontPromises.push((async () => {
1026+
let fontData;
1027+
try {
1028+
const urlMatch = /url\(([^\)]+)\)/.exec(src);
1029+
if (urlMatch) {
1030+
let url = urlMatch[1];
1031+
if (/^['"]/.exec(url) && url.at(0) === url.at(-1)) {
1032+
url = url.slice(1, -1)
1033+
}
1034+
fontData = await fn.parseFontData(url);
1035+
}
1036+
} catch (_e) {
1037+
console.log(
1038+
'This font can only be drawn and will not work with textToPoints/Contours/Model.'
1039+
);
1040+
}
1041+
return create(this, name, src, fontDescriptors, fontData)
1042+
})());
10001043
}
10011044
}
10021045
const fonts = await Promise.all(fontPromises);
1003-
return fonts[0]; // TODO: handle multiple faces?
1046+
return fonts.find(f => f.data) || fonts[0]; // TODO: handle multiple faces?
10041047
}
10051048

10061049
let pfont;
10071050
try {
1008-
// load the raw font bytes
1009-
let result = await fn.loadBytes(path);
1010-
//console.log('result:', result);
1011-
1012-
if (!result) {
1013-
throw Error('Failed to load font data');
1014-
}
1015-
1016-
// parse the font data
1017-
let fonts = Typr.parse(result);
1018-
1019-
// TODO: generate descriptors from font in the future
1020-
1021-
if (fonts.length === 0 || fonts[0].cmap === undefined) {
1022-
throw Error('parsing font data');
1023-
}
1051+
const fontData = await fn.parseFontData(path);
10241052

10251053
// make sure we have a valid name
1026-
name = name || extractFontName(fonts[0], path);
1054+
name = name || extractFontName(font, path);
10271055

10281056
// create a FontFace object and pass it to the p5.Font constructor
1029-
pfont = await create(this, name, path, descriptors, fonts[0]);
1057+
pfont = await create(this, name, path, descriptors, fontData);
10301058

10311059
} catch (err) {
10321060
// failed to parse the font, load it as a simple FontFace

0 commit comments

Comments
 (0)