Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support font-family names with numbers in (e.g. Font Awesome 5) #1948

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/render/canvas/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ export class CanvasRenderer {
const fontVariant = styles.fontVariant
.filter(variant => variant === 'normal' || variant === 'small-caps')
.join('');
const fontFamily = styles.fontFamily.join(', ');
const fontFamily = styles.fontFamily.map(fontName => {
return fontName.indexOf(' ') === -1 ? fontName : `"${fontName}"`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grahamkane Is there a need to perform the indexOf test, or could we simplify this code by applying quoting to each map entry?

const fontFamily = styles.fontFamily.map(fontName => `'${fontName}'`).join(', ');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW - the W3 has some good reference docs for the font-family property - they list a number of invalid property values, and note that quoting is required for any font names that may also be keywords.

Given that, it may be easiest to apply quoting to all values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: the suggested code snippet should be:

const fontFamily = styles.fontFamily.map(fontName => `'${fontName}'`).join(', ');

(the join operation was previously missing)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From CSS Fonts Module Level 3 (September, 2018):

Font family names other than generic families must either be given quoted as strings, or unquoted as a sequence of one or more identifiers. This means most punctuation characters and digits at the start of each token must be escaped in unquoted font family names.

So generic families do not require quoting; my mistake earlier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use the isIdentToken function to identify built-in font names instead?

It's possible this fix should be pushed down to the font-family CSS property parser.

I'm seeing slightly different behaviour between Chrome and Firefox with the fix proposed here in #1948 ; in particular I find that performing styles.fontFamily.join(' ').split(',') ... helps a little, since this joins all existing font-family tokens and then splits them into individual font names. I don't yet have a perfect solution though...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long story; in the end this train of thought led me to investigate the font-family CSS property parser more thoroughly, and the bug is due to the fact that Font Awesome 5 Free (four words) parses to the family list ["Font", "Awesome", "5", "Free"] (i.e. four non-existent font names).

A parser-level fix is provided in #2219.

}).join(', ');
const fontSize = isDimensionToken(styles.fontSize)
? `${styles.fontSize.number}${styles.fontSize.unit}`
: `${styles.fontSize.number}px`;
Expand Down