capsize icon indicating copy to clipboard operation
capsize copied to clipboard

Pass optional `postscriptName` parameter to fontkit

Open tsung-ju opened this issue 2 years ago • 4 comments

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');

tsung-ju avatar Dec 22 '23 03:12 tsung-ju

⚠️ 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

changeset-bot[bot] avatar Dec 22 '23 03:12 changeset-bot[bot]

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.

michaeltaranto avatar Jan 14 '24 22:01 michaeltaranto

Would love to see this!

paschun avatar May 23 '24 20:05 paschun

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?

michaeltaranto avatar May 24 '24 01:05 michaeltaranto

Closing due to inactivity, but have opened #208 that goes a bit further as described above. Thanks for the push.

michaeltaranto avatar Jul 24 '24 02:07 michaeltaranto