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

Expand Playfair Display's character map to include super/subscripts & fix PNG exports #2791

Merged
merged 17 commits into from
Oct 25, 2023

Conversation

samizdatco
Copy link
Contributor

@samizdatco samizdatco commented Oct 18, 2023

The stock version of Playfair Display includes support for super- and subscript numerals through the opentype 'sups' and 'subs' features, but does not actually provide direct access to those glyphs through their standard unicode codepoints. This PR adds a new script (devTools/fonts/fix-numerals.py) to the recipe that builds our custom fonts. It uses ttx from fonttools to merge in the missing character→glyph mappings before generating our webfonts and latin subsets.

PNG exports now use Playfair & Lato instead of falling back to Georgia and Arial as they did previously on any system that didn't have the fonts installed locally. PNG rendering has been moved into a new helper class, StaticChartRasterizer, which handles font-embedding, client-side rendering, and a workaround for a (truly infernal) race condition between font-loading and image rendering.

Copy link
Member

@ikesau ikesau left a comment

Choose a reason for hiding this comment

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

I can confirm this fixes the rendering of sups 'n' subs 👍

before

image

after

image

Similarly for content on the site, one of these is the literal and one is in sub tags:
Screenshot 2023-10-18 at 11 25 19 AM

Unless @marcelgerber notices something in the fiddly details of this change, it's all good on my end.

@samizdatco samizdatco changed the title Expand Playfair Display's character map to include super/subscripts Expand Playfair Display's character map to include super/subscripts & fix PNG exports Oct 19, 2023
@samizdatco samizdatco force-pushed the fix-fonts-in-exports-v2 branch 2 times, most recently from 3de0704 to 77ed4a3 Compare October 19, 2023 21:10
@samizdatco samizdatco force-pushed the fix-fonts-in-exports-v2 branch from 565f78f to 2ad133a Compare October 20, 2023 15:29
@@ -200,23 +213,14 @@ export class IRSuperscript implements IRToken {
return <sup key={key}>{this.text}</sup>
}
toSVG(key?: React.Key): JSX.Element {
// replace numerals with literals, for everything else let the font-feature handle it
const style = { fontFeatureSettings: '"sups"' }
const text = this.text.replace(/./g, (c) =>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const text = this.text.replace(/./g, (c) =>
const text = this.text.replace(/[0-9]/g, (c) =>

Copy link
Contributor Author

@samizdatco samizdatco Oct 23, 2023

Choose a reason for hiding this comment

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

<extremely-theoretical>Matching . and using get to do the default-if-undefined replacement means we could conceivably add non-digit ordinal characters (like ª and º) to the mapping someday.</extremely-theoretical>

Copy link
Member

Choose a reason for hiding this comment

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

You're right; when I first looked at that code I thought these would be every IRText node that we would run the replace on.
But since IRSuperscript nodes are rare (and usually short) this is totally fine.

@samizdatco samizdatco force-pushed the fix-fonts-in-exports-v2 branch from 97b41de to 696c1f9 Compare October 23, 2023 21:30
@samizdatco samizdatco marked this pull request as ready for review October 23, 2023 22:09
Copy link
Member

@marcelgerber marcelgerber left a comment

Choose a reason for hiding this comment

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

Wow this actually works! Thank you for your hard work, I can see the madness you went through when working on this!

@@ -200,23 +213,14 @@ export class IRSuperscript implements IRToken {
return <sup key={key}>{this.text}</sup>
}
toSVG(key?: React.Key): JSX.Element {
// replace numerals with literals, for everything else let the font-feature handle it
const style = { fontFeatureSettings: '"sups"' }
const text = this.text.replace(/./g, (c) =>
Copy link
Member

Choose a reason for hiding this comment

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

You're right; when I first looked at that code I thought these would be every IRText node that we would run the replace on.
But since IRSuperscript nodes are rare (and usually short) this is totally fine.

else rej(new Error("Failed to generate bitmap blob"))
})
} catch (err) {
rej(err)
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if in the case where the png generation fails for whatever reason, we could still display the svg version.

But only do this if it's easy to do, no need to go down another massive rabbit hole.

@samizdatco samizdatco merged commit 5222918 into master Oct 25, 2023
12 of 13 checks passed
@samizdatco samizdatco deleted the fix-fonts-in-exports-v2 branch October 25, 2023 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants