JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1330
  • Score
    100M100P100Q116815F
  • License MIT

A TypeScript library for handling WOFF2 encoding using WebAssembly

Package Exports

  • woff2-encoder
  • woff2-encoder/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (woff2-encoder) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

woff2-encoder

A TypeScript library for handling WOFF2 encoding using WebAssembly.


🚀 Getting Started

Prerequisites

  • If using Node, >= 16.x

Installation

npm install woff2-encoder

📚 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('./myFont.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/myFont.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('./myFont.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);
}

⭐ 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.