Skip to content

Commit 57d8b3a

Browse files
authored
Merge pull request #8084 from perminder-17/comma-included-in-loadFont
Comma included in load font
2 parents 165a9ed + 433fabc commit 57d8b3a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/type/p5.Font.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,10 +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 (name.includes(' ')) name = "'" + name + "'"; // NOTE: must be single-quotes
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) {
987993

994+
name = sanitizeFontName(name);
988995
let fontArg = rawFont?._compressedData ?? rawFont?._data;
989996
if (!fontArg) {
990997
if (!validFontTypesRe.test(path)) {
@@ -1530,6 +1537,7 @@ export const arrayCommandsToObjects = commands => commands.map(command => {
15301537
}
15311538
});
15321539

1540+
export { sanitizeFontName as _sanitizeFontName };
15331541
export default font;
15341542

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

test/unit/type/p5.Font.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import p5 from '../../../src/app.js';
2+
import {_sanitizeFontName} from '../../../src/type/p5.Font.js';
23

34
suite('p5.Font', function () {
45
var myp5;
@@ -61,3 +62,23 @@ suite('p5.Font', function () {
6162
});
6263
});
6364
});
65+
66+
suite('sanitizeFontName', function () {
67+
test('fully alphabetic or alpha-leading alnum do not need quotes', function () {
68+
assert.equal(_sanitizeFontName('Arial'), 'Arial');
69+
assert.equal(_sanitizeFontName('Family900'), 'Family900');
70+
assert.equal(_sanitizeFontName('A_b-c'), 'A_b-c');
71+
});
72+
73+
test('names starting with a digit need quotes', function () {
74+
assert.equal(_sanitizeFontName('9lives'), "'9lives'");
75+
});
76+
77+
test('names with spaces need quotes', function () {
78+
assert.equal(_sanitizeFontName('My Font'), "'My Font'");
79+
});
80+
81+
test('names with commas need quotes', function () {
82+
assert.equal(_sanitizeFontName('Foo,Bar'), "'Foo,Bar'");
83+
});
84+
});

0 commit comments

Comments
 (0)