fontnik
v0.7.7
Published
A library that delivers a range of glyphs rendered as SDFs (signed distance fields) in a protobuf.
Maintainers
Readme
node-fontnik
A library that delivers a range of glyphs rendered as SDFs (signed distance fields) in a protocol buffer. We use these encoded glyphs as the basic blocks of font rendering in Mapbox GL. SDF encoding is superior to traditional fonts for our usecase in terms of scaling, rotation, and quickly deriving halos - WebGL doesn't have built-in font rendering, so the decision is between vectorization, which tends to be slow, and SDF generation.
The approach this library takes is to parse and rasterize the font with Freetype (hence the C++ requirement), and then generate a distance field from that rasterized image.
See also TinySDF, which is a faster but less precise approach to generating SDFs for fonts.
API
Installing
Prebuilt binaries are included in the npm package for:
- macOS arm64 (
darwin-arm64) - macOS x64 (
darwin-x64) - Linux x64 (
linux-x64) - Linux arm64 (
linux-arm64)
npm installOn other platforms, or when no matching prebuild exists, npm install compiles from source via CMake (see below). Node.js 20 or later is required.
Building from source
Install system dependencies:
macOS
brew install cmake ninja boost zlibLinux (Debian/Ubuntu)
sudo apt-get install cmake ninja-build zlib1g-dev libboost-devThen build:
npm run rebuildN-API headers (node-addon-api, node-api-headers), protozero, and FreeType are fetched automatically by CMake at configure time.
Local testing
npm testAfter changing C++ sources in src/, rebuild the native module:
makeSee the Makefile for coverage, sanitize, tidy, format, and Linux Docker testing (make test-linux).
Linux builds via Docker
To mirror CI on a Mac (or any host with Docker), build and test inside Ubuntu 22.04 containers matching GHA:
./scripts/docker-linux-test.sh # linux-x64 + linux-arm64, Release
./scripts/docker-linux-test.sh x64 Debug # single arch / build typeRelease builds write prebuilds/linux-<arch>/fontnik.node directly (same as local npm run rebuild).
Or make test-linux / make test-linux-x64. Use --rebuild-image after changing scripts/docker/Dockerfile.
Release
See DEV.md.
