seek-oss / capsize

Flipping how we define typography in CSS.
https://seek-oss.github.io/capsize/
MIT License
1.48k stars 37 forks source link

Pass optional `postscriptName` parameter to fontkit #156

Open tsung-ju opened 6 months ago

tsung-ju commented 6 months ago

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');
changeset-bot[bot] commented 6 months ago

⚠️ No Changeset found

Latest commit: 3f6b63b27b94a4d0633e7344f18f7578fc1f0dcb

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

michaeltaranto commented 5 months ago

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.

paschun commented 1 month ago

Would love to see this!

michaeltaranto commented 1 month ago

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?