ttf2woff2

repository·main·Indexed 43 days ago

https://github.com/nfroidure/ttf2woff2

A NodeJS wrapper for the Google WOFF2 project that converts TTF font files to WOFF2 format. Version 8.0.1 provides a CLI tool using stdin/stdout and a synchronous API for Buffer conversion. It utilizes a native C++ wrapper for performance with an Emscripten/WASM fallback, switchable via the TTF2WOFF2_VERSION environment variable.

Tokens
1K
Snippets
7
Records
7
Agent score
56%

What's inside ttf2woff2

  1. Build the ttf2woff2 binary from source

    main

    If you need to build the native C++ wrapper yourself, follow these steps. Note that the setup instructions are specific to Ubuntu/Linux and may vary on other operating systems.

    1. Install dependencies: Ensure make and g++ are installed via apt-get.
    2. Install npm packages: Run npm i.
    3. Configure: Run npm run configure.
    4. Compile: Run npm run make.

    If the C++ compilation fails, the package is designed to fall back to an Emscripten build.

    ## Setup (Ubuntu/Linux)
    apt-get install make g++
    
    ## Actual build
    npm i
    npm run configure
    npm run make
  2. Use the ttf2woff2 API

    main

    The ttf2woff2 package provides a synchronous function that accepts a Buffer containing TTF data and returns a Buffer containing the converted WOFF2 data. This is ideal for integration into Node.js applications using node:fs/promises for file operations.

    import { readFile, writeFile } from 'node:fs/promises';
    import ttf2woff2 from 'ttf2woff2';
    
    const input = await readFile('font.ttf');
    
    await writeFile('font.woff2', ttf2woff2(input));
  3. Switch between Native and WASM implementations using TTF2WOFF2_VERSION

    main

    You can control which engine the library uses by setting the TTF2WOFF2_VERSION environment variable. This is useful for environments where native bindings are unavailable or where WASM is preferred.

    • Native: Set TTF2WOFF2_VERSION=native (or leave unset/empty) to use the native C++ addon via bindings.
    • WASM: Set TTF2WOFF2_VERSION=wasm to use the WebAssembly version.

    If an invalid version is provided, the library throws an error with code E_UNABLE_TO_LOAD_TTF2WOFF2.

    # To use the WASM version
    TTF2WOFF2_VERSION=wasm node your-script.js
    
    # To use the Native version
    TTF2WOFF2_VERSION=native node your-script.js
  4. Use the ttf2woff2 CLI

    main

    You can use ttf2woff2 as a command-line tool to convert TTF files to WOFF2. The tool expects the TTF data via standard input and outputs the WOFF2 data to standard output.

    Linux/macOS

    Use cat to pipe the font file into the command:

    cat font.ttf | ttf2woff2 > font.woff2

    Windows (PowerShell)

    Since cat is not natively available in the same way, use Start-Process with input/output redirection:

    Start-Process -NoNewWindow -Wait ttf2woff2.cmd -RedirectStandardInput font.ttf -RedirectStandardOutput font.woff2
    # OR (shorthand)
    start-process -nnw -wait ttf2woff2.cmd -rsi font.ttf -rso font.woff2
    cat font.ttf | ttf2woff2 > font.woff2
  5. Use the ttf2woff2 function

    main

    The default export of this package is a function that converts a TTF font buffer into a WOFF2 font buffer. The implementation used (Native vs. WASM) is determined by the TTF2WOFF2_VERSION environment variable.

    To use the library, import the default export and pass the input buffer to it.

    import ttf2woff2 from 'ttf2woff2';
    
    // inputBuffer is a Buffer containing the TTF font data
    const woff2Buffer = ttf2woff2(inputBuffer);
  6. Use the ttf2woff2 CLI via stdin/stdout

    main

    The ttf2woff2 CLI tool allows you to convert TrueType Font (TTF) files to WOFF2 format by piping data through standard input (stdin) and receiving the result via standard output (stdout). This is useful for shell-based font processing pipelines.

    To use it, pipe the contents of a .ttf file into the command and redirect the output to a .woff2 file.

    cat font.ttf | ttf2woff2 > font.woff2