1.1.1 • Published 2 months ago

@web-alchemy/fonttools v1.1.1

Weekly downloads
-
License
-
Repository
github
Last release
2 months ago

Font tools

This is the Node.js adapter of python font tools via Pyodide without having to install python and its dependencies.

Font tools library usually used for optimizing fonts – subseting, format converting, deleting of unused variable font axes, etc.

Using as a CLI

You can install the library as a global dependency:

npm install --global @web-alchemy/fonttools

After installing font utilities will be available globally:

fonttools <params>
pyftsubset <params>
pyftmerge <params>
ttx <params>

Also you can use this tool via npx:

npx -p @web-alchemy/fonttools <params>
npx -p @web-alchemy/fonttools pyftsubset <params>
npx -p @web-alchemy/fonttools pyftmerge <params>
npx -p @web-alchemy/fonttools ttx <params>

Example of converting ttf to woff2:

npx -p @web-alchemy/fonttools pyftsubset \
  "./some/path/to/font.ttf" \
  "*" \  # keep all glyphs and just convert format
  --output-file="./some/path/to/font.woff2" \
  --flavor="woff2"

Example of converting ttf to woff2 and subseting with text and unicodes options:

npx -p @web-alchemy/fonttools pyftsubset \
  "./some/path/to/font.ttf" \
  --output-file="./some/path/to/font.woff2" \
  --flavor="woff2" \
  --text="The text whose characters will be included in the font file" \
  --unicodes="U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD" \
  --desubroutinize \
  --no-hinting \
  --layout-features="*"

Example of customizing variable font's axes:

npx @web-alchemy/fonttools varLib.instancer \
  "./src/font.woff2" \
  # decrease `wght` axis range
  wght=400:600 \
  # delete `wdth` axis
  wdth=drop \ 
  --output="./dist/font.woff2"

Using as a module

Library provides few JavaScript specific methods for fonts optimization:

const {
  subset,
  instantiateVariableFont
} = require('@web-alchemy/fonttools')

Example of converting ttf to woff2:

const fs = require('node:fs')
const { subset } = require('@web-alchemy/fonttools')

async function main() {
  const inputFileBuffer = await fs.promises.readFile('./font.ttf')

  const outputFileBuffer = await subset(inputFileBuffer, {
    '*': true, // keep all glyphs and just convert format
    'flavor': 'woff2',
  })

  await fs.promises.writeFile('./font.woff2', outputFileBuffer)
}

main()

Example of converting ttf to woff2 and subseting with text and unicodes options:

const fs = require('node:fs')
const { subset } = require('@web-alchemy/fonttools')

async function main() {
  const inputFileBuffer = await fs.promises.readFile('./font.ttf')

  const outputFileBuffer = await subset(inputFileBuffer, {
    'text': "The text whose characters will be included in the font file",
    'unicodes': "U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD",
    'flavor': 'woff2',
  })

  await fs.promises.writeFile('./font.woff2', outputFileBuffer)
}

main()

Method subset takes same params as original pyftsubset utility(without dashes --).

Example of customizing variable font's axes:

const fs = require('node:fs')
const { instantiateVariableFont } = require('@web-alchemy/fonttools')

async function main() {
  const inputFileBuffer = await fs.promises.readFile('./src/font.woff2')

  const outputFileBuffer = await instantiateVariableFont(inputFileBuffer, {
    wght: [300, 500], // decrease `wght` axis range
    wdth: null // delete `wdth` axis
  })

  await fs.promises.writeFile('dist/font.woff2', outputFileBuffer)
}

main()

This is port of method varLib.instancer.instantiateVariableFont

Limitations

  • Doesn't support zopfli package for better optimizing woff files.
  • In CLI all file paths should be relative to cwd (current working directory).

References

1.1.1

2 months ago

1.1.0

3 months ago

1.1.0-alpha1

3 months ago

1.0.3

3 months ago

1.0.2

3 months ago

1.0.1

7 months ago

1.0.0

11 months ago

0.1.0

12 months ago

0.0.1

12 months ago