Pass optional `postscriptName` parameter to fontkit
Currently passing .ttc files to fromFile, fromBlob or fromUrl causes an error, because fontkit returns a Collection instead of a Font. I added an optional postscriptName parameter which tells fontkit to return a specific font from the collection.
Here's an example usage:
const metrics = await fromFile('NotoSans.ttc', 'NotoSans');
⚠️ No Changeset found
Latest commit: db666daedbc1f244cdd6684553a1b9667891e430
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Thanks for looking at this. We have some work landing soon that introduces an options object as a second parameter to these APIs and that will clash with the approach of this PR currently.
Adding better support for type collections is definitely part of the plan, but want to consider this as a feature on top of the new work landing shortly if thats ok.
Would love to see this!
Sorry, returning to this now that we have completed both the unicode subset work and weight/variant support, and it looks like we didnt end up adding an options object.
I still prefer the extensibility of using an options object here, e.g.:
const metrics = await fromFile('<path>', {
postscriptName: '...'
});
Have just been playing around with how we can improve the DX if users get this wrong, e.g. providing feedback when opening a collection without providing a postscriptName and was able to extract and build this message which I think is helpful:
Error: Metrics cannot be unpacked for a font collection.
Either provide a single font file or specify a `postscriptName` in the options. For example:
------------------------------------------
const metrics = await fromFile('<path>', {
postscriptName: 'AvenirNext-Bold'
});
------------------------------------------
Available `postscriptNames` in this collection are:
- AvenirNext-Bold
- AvenirNext-BoldItalic
- AvenirNext-DemiBold
- AvenirNext-DemiBoldItalic
- AvenirNext-Italic
- AvenirNext-Medium
- AvenirNext-MediumItalic
- AvenirNext-Regular
- AvenirNext-Heavy
- AvenirNext-HeavyItalic
- AvenirNext-UltraLight
- AvenirNext-UltraLightItalic
Are you happy for me to push to this PR?
Closing due to inactivity, but have opened #208 that goes a bit further as described above. Thanks for the push.