An advanced font engine for Node and the browser
npm install @pdf-lib/fontkitListed below are changes that have been made in this fork:
* Store binary data as compressed base64 JSON so the fs module isn't needed to read it back:
* 968e35c
* 99a35c7
* 2f1445d
* f674bf2-R24, f674bf2-R13
* Rewrote Makefile to Makefile.js using shelljs:
* a246e7f
* Update to Babel 7:
* 70049f8
* 8d5b29b
* Build UMD modules:
* cce995c
* 08cacef
* Build ES modules:
* dbe8e9d
* 9363d1f
* Bundle Node dependencies (stream, util, Buffer) into UMD and ES modules so consumers of this lib don't have to deal with them:
* 9363d1f
* Accept Uint8Array objects for font data instead of Buffer objects, so consumers can stick to plain JS regardless of their environment:
* 9363d1f-R12
* Add TypeScript declaration file:
* 387ebc4
* 3bafdbc
* b0241e7
* Remove calls to new Function() to allow usage on CSP sites:
* e3dcc8a
* Released to NPM as @pdf-lib/fontkit
* 873b05d
Also see
* https://github.com/Hopding/unicode-properties
* https://github.com/Hopding/brotli.js
* https://github.com/Hopding/restructure
* https://github.com/Hopding/png-ts
Fontkit is an advanced font engine for Node and the browser, used by PDFKit and pdf-lib. It supports many font formats, advanced glyph substitution and layout features, glyph path extraction, color emoji glyphs, font subsetting, and more.
* Suports TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collection (.ttc), and Datafork TrueType (.dfont) font files
* Supports mapping characters to glyphs, including support for ligatures and other advanced substitutions (see below)
* Supports reading glyph metrics and laying out glyphs, including support for kerning and other advanced layout features (see below)
* Advanced OpenType features including glyph substitution (GSUB) and positioning (GPOS)
* Apple Advanced Typography (AAT) glyph substitution features (morx table)
* Support for getting glyph vector paths and converting them to SVG paths, or rendering them to a graphics context
* Supports TrueType (glyf) and PostScript (CFF) outlines
* Support for color glyphs (e.g. emoji), including Apple’s SBIX table, and Microsoft’s COLR table
* Support for AAT variation glyphs, allowing for nearly infinite design control over weight, width, and other axes.
* Font subsetting support - create a new font including only the specified glyphs
``js
import fontkit from '@pdf-lib/fontkit';
import fs from 'fs';
// open a font synchronously
const fontData = fs.readFileSync('font.ttf');
const font = fontkit.create(fontData);
// layout a string, using default shaping features.
// returns a GlyphRun, describing glyphs and positions.
const run = font.layout('hello world!');
// get an SVG path for a glyph
const svg = run.glyphs[0].path.toSVG();
// create a font subset
const subset = font.createSubset();
run.glyphs.forEach(function(glyph) {
subset.includeGlyph(glyph);
});
subset.encodeStream()
.pipe(fs.createWriteStream('subset.ttf'));
`
bash
With npm
npm install --save @pdf-lib/fontkitWith yarn
yarn add @pdf-lib/fontkit
`
This assumes you're using npm or yarn as your package manager.$3
You can also download @pdf-lib/fontkit as a UMD module from unpkg. The UMD builds have been compiled to ES5, so they should work in any modern browser. UMD builds are useful if you aren't using a package manager or module bundler. For example, you can use them directly in the