papandreou / subset-font

Create a subset of a TrueType/OpenType/WOFF/WOFF2 font using the wasm build of harfbuzz/hb-subset
BSD 3-Clause "New" or "Revised" License
95 stars 6 forks source link

subset-font

Create a subset font from an existing font in SFNT (TrueType/OpenType), WOFF, or WOFF2 format. When subsetting a variable font, you can also reduce the variation space at the individual axis level.

These operations are implemented using harfbuzzjs, which is a WebAssembly build of HarfBuzz.

Basic example

const subsetFont = require('subset-font');

const mySfntFontBuffer = Buffer.from(/*...*/);

// Create a new font with only the characters required to render "Hello, world!" in WOFF2 format:
const subsetBuffer = await subsetFont(mySfntFontBuffer, 'Hello, world!', {
  targetFormat: 'woff2',
});

Reducing the variation space

const subsetFont = require('subset-font');

const mySfntFontBuffer = Buffer.from(/*...*/);

// Create a new font with only the characters required to render "Hello, world!" in WOFF2 format:
const subsetBuffer = await subsetFont(mySfntFontBuffer, 'Hello, world!', {
  targetFormat: 'woff2',
  variationAxes: {
    // Pin the axis to 200:
    wght: 200,
    // Reduce the variation space, explicitly setting a new default value:
    GRAD: { min: -50, max: 50, default: 25 },
    // Reduce the variation space. A new default value will be inferred by clamping the old default to the new range:
    slnt: { min: -9, max: 0 },
    // The remaining axes will be kept as-is
  },
});

API

subsetFont(buffer, text, options): Promise<Buffer>

Asynchronously create a subset font as a Buffer instance, optionally converting it to another format.

Returns a promise that gets fulfilled with the subset font as a Buffer instance, or rejected with an error.

Options:

For backwards compatibility reasons, 'truetype' is supported as an alias for 'sfnt'.

Why not use harfbuzzjs directly?

This middle-man module only really exists for convenience.

Releases

Changelog

License

3-clause BSD license -- see the LICENSE file for details.