Skip to content

Commit 1241837

Browse files
authored
Merge pull request #8391 from processing/fix/text-special-chars
Fix font measurement with font families containing special characters
2 parents e21ac5a + 930f56a commit 1241837

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

src/type/textCore.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function textCore(p5, fn) {
2424
const LinebreakRe = /\r?\n/g;
2525
const CommaDelimRe = /,\s+/;
2626
const QuotedRe = /^".*"$/;
27+
const SpecialCharRe = /[^\x00-\x7F]/; // Non-ascii
2728
const TabsRe = /\t/g;
2829

2930
const FontVariationSettings = 'fontVariationSettings';
@@ -2425,7 +2426,7 @@ function textCore(p5, fn) {
24252426
let parts = familyStr.split(CommaDelimRe);
24262427
let family = parts.map(part => {
24272428
part = part.trim();
2428-
if (part.indexOf(' ') > -1 && !QuotedRe.test(part)) {
2429+
if ((part.indexOf(' ') > -1 || SpecialCharRe.test(part)) && !QuotedRe.test(part)) {
24292430
part = `"${part}"`; // quote font names with spaces
24302431
}
24312432
return part;

test/unit/visual/cases/typography.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ visualSuite('Typography', function () {
4343
screenshot();
4444
});
4545

46+
visualTest('with a font file and special chars', async function (p5, screenshot) {
47+
p5.createCanvas(100, 100, p5.WEBGL);
48+
const font = await p5.loadFont(
49+
'/unit/assets/Inconsolata-Bold.ttf',
50+
'Incönsolata'
51+
);
52+
p5.textFont(font);
53+
p5.textAlign(p5.CENTER, p5.CENTER);
54+
p5.textSize(35);
55+
p5.text('p5*js', 0, 0);
56+
p5.noFill();
57+
p5.rectMode(p5.CENTER);
58+
p5.rect(0, 0, p5.fontWidth('p5*js'), p5.textLeading());
59+
screenshot();
60+
});
61+
4662
visualTest('with a woff font file', async function (p5, screenshot) {
4763
p5.createCanvas(100, 100);
4864
const font = await p5.loadFont(
2.29 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)