@lvcabral/opentype.js
v1.3.9
Published
OpenType.JS (font parser only)
Maintainers
Readme
opentype.js (parse only)
This fork contains a light-weight version of opentype.js that only parses OpenType/TrueType/WOFF font buffers and returns metadata. There are no drawing helpers, glyph constructors, or layout utilities—parse() is the entire public API.
Installation
npm install @lvcabral/opentype.jsUsage
import { readFileSync } from 'fs';
import { parse } from '@lvcabral/opentype.js';
const data = readFileSync('./test/fonts/Roboto-Black.ttf');
const font = parse(data);
console.log(font.unitsPerEm); // 2048
console.log(font.ascender, font.descender);
console.log(font.tables.head.macStyle);
console.log(font.names.fontFamily.en);The returned Font object exposes:
unitsPerEm,ascender,descendertables.headandtables.hheaobjects exactly as they appear in the font tablesnames, keyed by name ID (e.g.font.names.fullName) with locale entries (BCP-47 tags when available, otherwise the raw0x####language ID).
Unsupported table data is ignored. If a required table (head or hhea) is missing the parser throws.
Formats
- TrueType and OpenType fonts (
\x00\x01\x00\x00,true,typ1,OTTO) - WOFF containers (tables are decompressed automatically)
- Format 0/1
nametables encoded in UTF-16
Development
npm run build– bundlessrc/opentype.mjsintodist/opentype.module.js(ESM) anddist/opentype.jswith sourcemaps for local debuggingnpm run dist– produces the minified distributablesdist/opentype.module.min.jsanddist/opentype.min.js(with sourcemaps)npm test– runs the minimal Mocha suite that loads sample fonts and asserts metadatanpm run lint– ESLint over thesrc/directory
The parser lives in src/opentype.mjs and only depends on:
src/parse.mjsfor DataView helperssrc/tables/{head,hhea,name}.mjssrc/[email protected]for WOFF decompression
Test fonts are stored in test/fonts/. Published consumers load from the generated dist/ artifacts (main/module point there).
Versioning
We use SemVer for versioning.
License
MIT
Thanks
We would like to acknowledge the work of others without which opentype.js wouldn't be possible:
- pdf.js: for an awesome implementation of font parsing in the browser.
- FreeType: for the nitty-gritty details and filling in the gaps when the spec was incomplete.
- ttf.js: for hints about the TrueType parsing code.
- CFF-glyphlet-fonts: for a great explanation/implementation of CFF font writing.
- fontkit: for a great implementation of CFF2 parsing and variable font features
- tiny-inflate: for WOFF decompression.
- Microsoft Typography: the go-to reference for all things OpenType.
- Adobe Compact Font Format spec and the Adobe Type 2 Charstring spec: explains the data structures and commands for the CFF glyph format.
- All contributors.
