Skip to content

Commit 1459a39

Browse files
authored
Refactor font name to a separate function
1 parent a7bc586 commit 1459a39

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/type/p5.Font.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -981,12 +981,17 @@ async function create(pInst, name, path, descriptors, rawFont) {
981981
return new Font(pInst, face, name, path, rawFont);
982982
}
983983

984-
function createFontFace(name, path, descriptors, rawFont) {
985984

986-
if (!/^[A-Za-z_][\w-]*$/.test(name)) {
987-
name = "'" + name.replace(/'/g, "\\'") + "'";
988-
}
985+
function sanitizeFontName(name) {
986+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(name)) {
987+
name = "'" + String(name).replace(/'/g, "\\'") + "'";
988+
}
989+
return name;
990+
}
991+
992+
function createFontFace(name, path, descriptors, rawFont) {
989993

994+
name = sanitizeFontName(name);
990995
let fontArg = rawFont?._compressedData ?? rawFont?._data;
991996
if (!fontArg) {
992997
if (!validFontTypesRe.test(path)) {
@@ -1532,6 +1537,7 @@ export const arrayCommandsToObjects = commands => commands.map(command => {
15321537
}
15331538
});
15341539

1540+
export { sanitizeFontName as _sanitizeFontName };
15351541
export default font;
15361542

15371543
if (typeof p5 !== 'undefined') {

0 commit comments

Comments
 (0)