Package detail

woff2-encoder

itskyedo2.4kMIT2.0.0

A TypeScript library for handling WOFF2 encoding using WebAssembly

woff2, ttf, otf, sfnt

readme

woff2-encoder

A TypeScript library for handling WOFF2 encoding using WebAssembly.


🚀 Getting Started

Prerequisites

  • If using Node, >= 18.x

Installation

npm install woff2-encoder

Notes

If you only need to decompress WOFF2 files, it's recommended that you import from woff2-encoder/decompress (see the Decompress only example below). This will net your end users a significant decrease in bundle size as it uses its own separate WASM file with a much smaller footprint.

📚 API Reference

compress

Compresses SFNT (TrueType/OpenType) font data to WOFF2 font data.

Returns: Promise<Uint8Array> A promise resolving to the WOFF2 font data.

Parameter Type Description
buffer `ArrayBuffer \ Uint8Array` The SFNT font data.

decompress

Decompresses WOFF2 font data back to SFNT (TrueType/OpenType) font data.

Returns: Promise<Uint8Array> A promise resolving to the SFNT font data.

Parameter Type Description
buffer `ArrayBuffer \ Uint8Array` The WOFF2 font data.

💡 Examples

Compress a TTF font using Node.js

import fs from 'node:fs';
import { compress } from 'woff2-encoder';

async function example() {
  const fontFile = fs.readFileSync('./my-font.ttf');
  const output = await compress(fontFile);
}

Decompress a WOFF2 font from a URL

import { decompress } from 'woff2-encoder';

async function example() {
  const fontBuffer = await fetch('https://example.com/my-font.woff2').then(
    (res) => res.arrayBuffer()
  );

  const output = await decompress(fontBuffer);
}

Parse a WOFF2 font with opentype.js

import fs from 'node:fs';
import opentype from 'opentype.js';
import { decompress } from 'woff2-encoder';

async function example() {
  const fontFile = fs.readFileSync('./my-font.woff2');
  const output = await decompress(fontFile);

  // Since opentype.js requires a buffer, we pass
  // in the buffer and not the byte array itself
  const fontData = opentype.parse(output.buffer);
}

Decompress only

import fs from 'node:fs';
import opentype from 'opentype.js';
import decompress from 'woff2-encoder/decompress';

async function example() {
  const fontBuffer = await fetch('https://example.com/my-font.woff2').then(
    (res) => res.arrayBuffer()
  );

  const output = await decompress(fontBuffer);
}

⭐ Acknowledgements

  • google/woff2 - For the C++ implemention for encoding WOFF2 files.
  • fontello/wawoff2 - For the initial WebAssembly port of Google's WOFF2 encoder.

📃 License

MIT License. See LICENSE for details.

changelog

Changelog

2.0.0 (2025-01-29)

⚠ BREAKING CHANGES

  • require node >= 18.x (#27)

Features

1.1.0 (2024-07-25)

Features

  • change error message punctuation (fd89002)
  • create separate decompress wasm function (40a272b)

1.0.3 (2023-11-23)

Bug Fixes

  • load module on onRuntimeInitialized event (#6) (91ef55f)

1.0.2 (2023-11-04)

Bug Fixes

  • update license header and readme (#2) (1f2aa8c)

1.0.1 (2023-10-18)

Features