Adding font weight support
See original GitHub issueHi there,
I’d like to add font weight support to react-pdf and I was hoping a contributor could recommend an efficient strategy or inform me of any existing work before I dive in.
Cheers Luke
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (10 by maintainers)
 Top Results From Across the Web
Top Results From Across the Web
font-weight - CSS: Cascading Style Sheets - MDN Web Docs
The font-weight CSS property sets the weight (or boldness) of the font. The weights available depend on the font-family that is currently ...
Read more >How To Set Weights And Styles With The @font-face ...
Another way to set weights and styles is to use the same font-family name multiple times, setting the weights and styles in each...
Read more >How To Load and Use Custom Fonts with CSS
You learned how each font weight and style is a different file loaded from the service, and that the number of loaded variations...
Read more >CSS font-weight property
Definition and Usage. The font-weight property sets how thick or thin characters in text should be displayed.
Read more >font-weight
The font-weight property sets the weight, or thickness, of a font and is dependent either on available font faces within a font family...
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free Top Related Reddit Thread
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

Hey. Font weight is not as simple as it might look at first glance. Currently the way to support different font weights it’s by loading different font files per each font-weight needed, and styling the text based on your needs.
Or you can also choose one of the default fonts shipped with the lib:
The API could change, but you still have to provide different files and change the font-family for each chunk you want styled differently. I know it’s not ideal, but it’s the only way
Thanks for pushing this feature forward. Here’s what I think:
I always tried to be as close to the web APIs as possible. It’s great to take advantage of these, since most of people already know them. So implementing the
font-faceAPI as @DuncanMacWeb described it’s aligned to that. However, I’m not sure if adding theunicode-rangeoption. Didn’t estimated how hard would be to do it (I refactored the entire textkit solution, in order to be more easy to just add stuff), but the font-substitution engine implemented inreact-pdfalready fallbacks into another font if the given one does not have a glyph for a particular character. So struggling with unicode ranges seems making the api more complex without any visible enhance.I think @agarant solution really makes a change though. Because would enable to handle the styling of a text as you would do it in the web (using the
font-weightattribute), instead of having to set different font-families. However, I don’t think that adding theX-Light|Medium|Bold|Whateveris even necessary. It creates an arbitrary pattern that font families names should follow. And also with justfamilyandweightwe have all information to switch between font files to each particular case. It’s also necessary to fallback to the closest font in case the providedfont-weightis no explicitly defined.In conclusion, I think we can use some kind of mixture of both solutions:
Internally we can also map values such as
bold,light, etc to the corresponding number representation.What do you guys think?