TerminalSkills/skills

opentype-js

Parse and manipulate OpenType/TrueType fonts with opentype.js — read font metadata, access glyph outlines, measure text, generate font subsets, and render text to SVG paths.

View source

How to use

  1. Copy the installation command and review the source package before installing it in your own agent environment.
  2. Check the source document for required tools, credentials and supported hosts.
  3. Describe your input and desired deliverable; review the output against the source material.
npx skills add https://github.com/TerminalSkills/skills --skill opentype-js

Before you start

  • Installation does not include a model subscription, API allowance or permission to access your files.
  • Repository stars and installation counts describe activity, not an independent quality or security test.
Original skill document

Rendered from the source repository. Headings, examples, code, tables, links, and referenced images are preserved.

opentype.js

Overview

Read and write OpenType fonts. Access every glyph, measure text, convert to SVG paths.

opentype.js 2.0 (npm opentype.js, MIT) parses TTF, OTF and WOFF (not WOFF2), handles kerning and ligatures, and also supports variable fonts, COLR/CPAL colour glyphs and TrueType hinting. opentype.load() and loadSync() are deprecated: read the file yourself and call opentype.parse(). In ESM code use the default import (import opentype from "opentype.js"); named imports such as { Font } fail under Node because the package is CommonJS.

Instructions

Setup

bash
# Install opentype.js for font parsing and manipulation.
npm install opentype.js

Loading a Font

typescript
// src/fonts/load.ts — Load a font from file or URL and read metadata.
import opentype from "opentype.js";
import fs from "fs";

// From file (Node.js)
const file = fs.readFileSync("./fonts/LiberationSans-Regular.ttf");
// A Node Buffer may be a view into a shared pool: copy out exactly its bytes.
const font = opentype.parse(file.buffer.slice(file.byteOffset, file.byteOffset + file.byteLength));

// In 2.x names are grouped by platform, then field, then language.
console.log(font.names.windows?.fontFamily?.en);   // "Liberation Sans"
console.log(font.names.windows?.designer?.en);     // designer name, if the font has one
console.log(font.unitsPerEm);             // 2048 for this font
console.log(font.numGlyphs);              // total glyph count

Measuring Text

typescript
// src/fonts/measure.ts — Calculate text width and bounding box at a given size.
// Useful for layout engines and canvas text positioning.
import opentype from "opentype.js";

export function measureText(font: opentype.Font, text: string, fontSize: number) {
  const path = font.getPath(text, 0, 0, fontSize);
  const bb = path.getBoundingBox();
  const advance = font.getAdvanceWidth(text, fontSize); // includes kerning by default

  return {
    width: advance,
    height: bb.y2 - bb.y1,
    boundingBox: { x1: bb.x1, y1: bb.y1, x2: bb.x2, y2: bb.y2 },
  };
}

Rendering Text to SVG

typescript
// src/fonts/to-svg.ts — Convert a text string to an SVG path element.
// This produces resolution-independent text that doesn't require the font file.
import opentype from "opentype.js";

export function textToSvg(
  font: opentype.Font,
  text: string,
  fontSize: number,
  x: number,
  y: number
): string {
  const path = font.getPath(text, x, y, fontSize);
  const pathData = path.toPathData(2); // precision
  const bb = path.getBoundingBox();

  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${bb.x1} ${bb.y1} ${bb.x2 - bb.x1} ${bb.y2 - bb.y1}">
  <path d="${pathData}" fill="currentColor"/>
</svg>`;
}

Accessing Individual Glyphs

typescript
// src/fonts/glyphs.ts — Extract glyph outlines and metadata for specific characters.
import opentype from "opentype.js";

export function getGlyphInfo(font: opentype.Font, char: string) {
  const glyph = font.charToGlyph(char);
  const path = glyph.getPath(0, 0, 72);

  return {
    name: glyph.name,
    unicode: glyph.unicode,
    advanceWidth: glyph.advanceWidth,
    pathData: path.toPathData(2),
    commands: path.commands,
  };
}

// List all glyphs in a font (glyph.unicode is undefined for unmapped glyphs)
export function listGlyphs(font: opentype.Font) {
  const glyphs: { index: number; name: string; unicode: number | undefined }[] = [];
  for (let i = 0; i < font.numGlyphs; i++) {
    const g = font.glyphs.get(i);
    glyphs.push({ index: i, name: g.name, unicode: g.unicode });
  }
  return glyphs;
}

Font Subsetting

typescript
// src/fonts/subset.ts — Create a subset font containing only the glyphs needed
// for a specific string. Reduces font file size for web embedding.
import opentype from "opentype.js";
import fs from "fs";

export function subsetFont(font: opentype.Font, chars: string, outputPath: string) {
  const glyphs = [font.glyphs.get(0)]; // always include .notdef
  const seen = new Set<number>();

  for (const char of chars) {
    const glyph = font.charToGlyph(char);
    if (glyph.index !== 0 && !seen.has(glyph.index)) {
      glyphs.push(glyph);
      seen.add(glyph.index);
    }
  }

  const subset = new opentype.Font({
    familyName: "Subset",
    styleName: "Regular",
    unitsPerEm: font.unitsPerEm,
    ascender: font.ascender,
    descender: font.descender,
    glyphs,
  });

  // toArrayBuffer() works everywhere; download() only triggers a browser download.
  fs.writeFileSync(outputPath, Buffer.from(subset.toArrayBuffer()));
}

// subsetFont(font, "Hello, world", "./out/liberation-subset.ttf");
// Result: a font with the .notdef glyph plus one glyph per distinct character.

The subset keeps only outlines, advance widths and the character map: kerning, ligatures and hinting are dropped. For production web fonts prefer pyftsubset (fontTools) or glyphhanger, which keep layout tables.

Examples

"Turn the headline into an SVG path so it needs no font on the page"

typescript
import opentype from "opentype.js";
import fs from "fs";
import { textToSvg } from "./src/fonts/to-svg";

const file = fs.readFileSync("./fonts/LiberationSans-Bold.ttf");
const font = opentype.parse(file.buffer.slice(file.byteOffset, file.byteOffset + file.byteLength));
fs.writeFileSync("./out/headline.svg", textToSvg(font, "Spring Sale", 64, 0, 64));

Result: out/headline.svg holds one <path> with the outlines of "Spring Sale". The same width is available as font.getAdvanceWidth("Spring Sale", 64).

"Make a tiny font with only the digits and a dollar sign"

typescript
import { subsetFont } from "./src/fonts/subset";

subsetFont(font, "0123456789$.,", "./out/price-digits.ttf");

Result: a new TTF containing 14 glyphs (13 characters plus .notdef), a few KB instead of hundreds of KB. Re-parse it with opentype.parse to check numGlyphs.

Guidelines

  • Fonts with GSUB lookups that opentype.js 2.0.0 does not implement make getPath, getAdvanceWidth and stringToGlyphs throw (for example "lookupType: 6 - substFormat: 2 is not yet supported"; seen with DejaVu Sans and Inter). Catch it and fall back to per-character layout: sum font.charToGlyph(c).advanceWidth (font units, scale by fontSize / font.unitsPerEm) and add font.getKerningValue(a, b).
  • WOFF2 is not read directly; decompress it first (for example with wawoff2).
  • Coordinates are in font units until a fontSize is given; SVG y grows downward, and getPath already flips the outline.
  • Check the font's licence before converting text to outlines or redistributing a subset.
from this repository

More skills

All skills
TerminalSkills
Community

3dsmax-rendering

Scripts and configures production rendering in Autodesk 3ds Max with the V-Ray and Corona renderers: output size and files, render elements, denoising, light mix, batch and command-line rendering, and network rendering. Use when a user asks to set up a production render, render several cameras in one batch, render from the command line or on a render farm, add render passes for compositing, or cut render time for archviz and product shots.

installs
0
GitHub stars
180
Updated
Oct 25
TerminalSkills
Community

3dsmax-scripting

Covers scripting Autodesk 3ds Max, the 3D modeling and rendering application, with MAXScript and Python (pymxs): scene manipulation, object creation, material assignment, camera and light setup, batch operations, and file I/O. Use when tasks involve automating repetitive 3ds Max workflows, batch processing scenes, running scripts headless with 3dsmaxbatch, creating custom tools, or scripting scene setup for archviz, product visualization, or VFX.

installs
0
GitHub stars
180
Updated
Oct 25
TerminalSkills
Community

3proxy

3proxy is a small open-source proxy server that runs HTTP/HTTPS, SOCKS4/5, SNI and TCP/UDP port-mapping proxies from one config file. Use when a user asks to set up an HTTP or SOCKS5 proxy, add proxy users and passwords, write 3proxy access rules, chain or rotate upstream (parent) proxies, limit bandwidth, connections or monthly traffic per user, run 3proxy in Docker, or fix a 3proxy.cfg that will not start.

installs
0
GitHub stars
180
Updated
Oct 25
TerminalSkills
Community

a2a-protocol

Builds Agent2Agent (A2A) servers and clients, the open protocol (originally from Google, now under the Linux Foundation) that lets AI agents from different frameworks call each other. Use when the user wants to create an A2A-compliant agent, build an Agent Card, implement task management, connect agents across frameworks, set up agent discovery, handle streaming responses, implement push notifications, or orchestrate multi-agent workflows. Trigger words: a2a, agent to agent, agent2agent, a2a pro

installs
0
GitHub stars
180
Updated
Oct 25