Skip to content

Support weight prefixed local fonts #35

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

Closed
jbebe opened this issue Dec 1, 2022 · 2 comments
Closed

Support weight prefixed local fonts #35

jbebe opened this issue Dec 1, 2022 · 2 comments

Comments

@jbebe
Copy link

jbebe commented Dec 1, 2022

Right now a very quirky-looking method checks the filename and extracts the weight value.
Instead, I suggest the much simpler weight number prefix at the end of the filename, like "Arial-200.ttf" where 200 is extracted as the weight. I think it's more common than the "Arial-extralight.ttf" variant.

But the easiest approach would be to just simply delegate the weight extraction to the user by adding a callback to the config like this:

VitePluginFonts({
      custom: {
        getWeight: (x) => {....},
        families: [ ... ]

We accept PRs 🙂

I'm aware of that, thanks for mentioning.

@stafyniaksacha
Copy link
Member

This will be available on next release #39

Unfonts({
  custom: {
    display: 'swap',
    families: {
      'Dancing Script': {
        src: './assets/fonts/DancingScript*',
        transform(font) {
          if (font.basename === 'DancingScript-Bold') {
            font.weight = 700
          }

          return font
        },
      },
    },
  },
})

where font has a type like:

export interface CustomFontFace {
  source: string
  name: string
  basename: string
  weight: number
  style: string
  display: string
  local?: string | string[]
  files: {
    src: string
    ext: string
    path: string
    format: string
  }[]
}

you can override any values or return null to exclude the font!

@stafyniaksacha
Copy link
Member

Hello @jbebe !

This feature has been included in the v1.0.0

You can use the transform option from custom-fonts provider

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

No branches or pull requests

2 participants