Skip to content
Jennifer Programming Language

font API reference

A pure-Jennifer TrueType / OpenType (SFNT) font parser: read a .ttf / .otf from bytes and expose its metrics, character map, and glyph outlines. No Go, no dependency - just the bytes type and the bitwise operators for the big-endian tables - so it runs on both binaries.

Both outline backends ship: the TrueType glyf backend (simple and composite glyphs, quadratic curves) and the CFF / PostScript backend for OpenType OTTO fonts (a Type2 charstring interpreter with global / local subroutines and CID-keyed FDArray / FDSelect, so CJK fonts outline too), detected on parse; a CFF glyph's cubic curves render as native C segments in glyphPath and are approximated as quadratics in the Glyph struct. It parses the core tables - head, cmap (formats 4 and 12), maxp / hhea / hmtx, OS/2 (vertical metrics), the legacy kern table, loca / glyf or CFF , and name.

Import with import "font.j" as font;. See the font guide for prose and examples.

Contents

NameWhat it is
advanceThe horizontal advance width of the glyph for a codepoint, in font units.
advanceGidThe horizontal advance width of a glyph id, in font units (the raw hmtx metric, for building a PDF W widt...
advancesBatch advance-width lookup: the advances (font units) for a list of glyph ids, in one call, the same values...
ascenderThe typographic ascender in font units (OS/2 sTypoAscender, else the hhea ascent).
bboxThe font bounding box in font units as [xMin, yMin, xMax, yMax] (the head table's global glyph bounds, fo...
capHeightThe capital height in font units (OS/2 sCapHeight): the height of a flat capital such as H.
dataThe raw font file bytes, exactly as given to parse (or read by open), for embedding the font program in...
descenderThe typographic descender in font units (OS/2 sTypoDescender, else hhea).
glyphThe full outline of the glyph for a codepoint: its contours (on / off-curve points), advance, and bounding ...
glyphIdThe glyph id (index) a codepoint maps to through the font's character map, or 0 (.notdef) when the font l...
glyphIdsBatch glyph-id lookup: map a list of codepoints to their glyph ids in one call.
glyphPathThe glyph outline for a codepoint as an SVG path d string, in font-unit coordinates (y-up, as fonts store...
isCffWhether the font uses CFF / PostScript outlines (an OpenType OTTO) rather than TrueType glyf outlines.
kernThe kerning adjustment between two codepoints in font units (negative pulls them closer).
lineGapThe typographic line gap in font units (OS/2 sTypoLineGap, else hhea): the recommended extra leading betw...
nameThe font family name.
numGlyphsThe number of glyphs in the font.
openLoad and parse a font from a file path.
parseParse a TrueType / OpenType (SFNT) font from its bytes - a glyf (TrueType) or CFF (OpenType/PostScript...
unitsPerEmThe font's units-per-em (the coordinate scale of every outline / metric).
xHeightThe x-height in font units (OS/2 sxHeight): the height of a lowercase x.
Contourstruct: One closed contour of a glyph: its ordered points.
Fontstruct: A parsed font.
Glyphstruct: A glyph outline: its contours, advance width, and bounding box, all in font units.
Pointstruct: A point in a glyph contour, in font-unit coordinates.

Functions

advance

jennifer
font.advance(f as Font, cp as int)

The horizontal advance width of the glyph for a codepoint, in font units.

f
the font
cp
the Unicode codepoint
Returns int
the advance width

advanceGid

jennifer
font.advanceGid(f as Font, gid as int)

The horizontal advance width of a glyph id, in font units (the raw hmtx metric, for building a PDF W widths array keyed by glyph / CID).

f
the font
gid
the glyph id
Returns int
the advance width

advances

jennifer
font.advances(f as Font, gids as list of int)

Batch advance-width lookup: the advances (font units) for a list of glyph ids, in one call, the same values advanceGid gives. Prefer this over advanceGid per glyph (see glyphIds).

f
the font
gids
the glyph ids
Returns list of int
the advance widths, in order

ascender

jennifer
font.ascender(f as Font)

The typographic ascender in font units (OS/2 sTypoAscender, else the hhea ascent). The distance from the baseline to the top of the em box.

f
the font
Returns int
the ascender

bbox

jennifer
font.bbox(f as Font)

The font bounding box in font units as [xMin, yMin, xMax, yMax] (the head table's global glyph bounds, for a PDF FontBBox).

f
the font
Returns list of int
[xMin, yMin, xMax, yMax]

capHeight

jennifer
font.capHeight(f as Font)

The capital height in font units (OS/2 sCapHeight): the height of a flat capital such as H. 0 when the font has no OS/2 v2+ table.

f
the font
Returns int
the cap height, or 0 when unavailable

data

jennifer
font.data(f as Font)

The raw font file bytes, exactly as given to parse (or read by open), for embedding the font program in a container such as a PDF FontFile2.

f
the font
Returns bytes
the font file contents

descender

jennifer
font.descender(f as Font)

The typographic descender in font units (OS/2 sTypoDescender, else hhea). Conventionally negative (below the baseline).

f
the font
Returns int
the descender

glyph

jennifer
font.glyph(f as Font, cp as int)

The full outline of the glyph for a codepoint: its contours (on / off-curve points), advance, and bounding box. A codepoint the font lacks maps to glyph 0 (.notdef).

f
the font
cp
the Unicode codepoint
Returns Glyph
the glyph outline

glyphId

jennifer
font.glyphId(f as Font, cp as int)

The glyph id (index) a codepoint maps to through the font's character map, or 0 (.notdef) when the font lacks the codepoint. The identifier a PDF embeds under Identity encoding.

f
the font
cp
the Unicode codepoint
Returns int
the glyph id

glyphIds

jennifer
font.glyphIds(f as Font, cps as list of int)

Batch glyph-id lookup: map a list of codepoints to their glyph ids in one call. Prefer this over calling glyphId per character: one call walks the whole list with the character-map lookup inlined, where a per-character loop pays a module call and a lookup setup per codepoint (about a third less time on a 20,000-character text).

f
the font
cps
the codepoints
Returns list of int
the glyph ids, in order

glyphPath

jennifer
font.glyphPath(f as Font, cp as int)

The glyph outline for a codepoint as an SVG path d string, in font-unit coordinates (y-up, as fonts store them - flip y for screen rendering). Quadratic segments render as Q commands. An empty glyph yields "".

f
the font
cp
the Unicode codepoint
Returns string
the SVG path data

isCff

jennifer
font.isCff(f as Font)

Whether the font uses CFF / PostScript outlines (an OpenType OTTO) rather than TrueType glyf outlines.

f
the font
Returns bool
true for a CFF font

kern

jennifer
font.kern(f as Font, left as int, right as int)

The kerning adjustment between two codepoints in font units (negative pulls them closer). Reads the legacy kern table (version 0, horizontal format-0 subtables); 0 when the font has no kern table or no pair entry. Modern fonts carry kerning in GPOS instead, which this does not read.

f
the font
left
the left codepoint
right
the right codepoint
Returns int
the kerning adjustment, or 0

lineGap

jennifer
font.lineGap(f as Font)

The typographic line gap in font units (OS/2 sTypoLineGap, else hhea): the recommended extra leading between lines. Line height = ascender - descender + lineGap.

f
the font
Returns int
the line gap

name

jennifer
font.name(f as Font)

The font family name.

f
the font
Returns string
the family name

numGlyphs

jennifer
font.numGlyphs(f as Font)

The number of glyphs in the font.

f
the font
Returns int
the glyph count

open

jennifer
font.open(path as string)

Load and parse a font from a file path.

path
the .ttf file path
Returns Font
the parsed font
Throws Error
on a read or parse error

parse

jennifer
font.parse(b as bytes)

Parse a TrueType / OpenType (SFNT) font from its bytes - a glyf (TrueType) or CFF (OpenType/PostScript) outline font.

b
the font file contents
Returns Font
the parsed font
Throws Error
kind "font" on a malformed font: an unrecognised container, a missing table, unitsPerEm outside 16..16384, or numHMetrics outside 1..numGlyphs

unitsPerEm

jennifer
font.unitsPerEm(f as Font)

The font's units-per-em (the coordinate scale of every outline / metric).

f
the font
Returns int
units per em

xHeight

jennifer
font.xHeight(f as Font)

The x-height in font units (OS/2 sxHeight): the height of a lowercase x. 0 when the font has no OS/2 v2+ table.

f
the font
Returns int
the x-height, or 0 when unavailable

Structs

Contour

One closed contour of a glyph: its ordered points.

jennifer
def struct Contour {
    points as list of Point
};
points
the contour points

Font

A parsed font. Holds the raw bytes plus the table offsets and header values needed to answer metric / cmap / outline queries; glyph outlines are decoded on demand.

jennifer
def struct Font {
    data as bytes,
    unitsPerEm as int,
    numGlyphs as int,
    longLoca as bool,
    numHMetrics as int,
    loca as int,
    glyf as int,
    hmtx as int,
    cmapSub as int,
    cmapFmt as int,
    family as string,
    ascender as int,
    descender as int,
    lineGap as int,
    capHeight as int,
    xHeight as int,
    kern as int,
    cff as int,
    xMin as int,
    yMin as int,
    xMax as int,
    yMax as int
};
data
the raw font file
unitsPerEm
the em square size (the coordinate scale)
numGlyphs
the number of glyphs
longLoca
whether the loca table uses 32-bit offsets
numHMetrics
the number of long horizontal metrics
loca
the loca table offset
glyf
the glyf table offset
hmtx
the hmtx table offset
cmapSub
the chosen cmap subtable offset
cmapFmt
the chosen cmap subtable format (4 or 12)
family
the font family name
ascender
the typographic ascender (OS/2 sTypoAscender, else hhea)
descender
the typographic descender (negative; OS/2 sTypoDescender, else hhea)
lineGap
the typographic line gap (OS/2 sTypoLineGap, else hhea)
capHeight
the capital height (OS/2 sCapHeight; 0 when unavailable)
xHeight
the x-height (OS/2 sxHeight; 0 when unavailable)
kern
the kern-table offset (0 when the font has no kern table)
cff
the CFF-table offset (0 for a TrueType/glyf font)
xMin
the font bounding-box minimum x (head xMin)
yMin
the font bounding-box minimum y (head yMin)
xMax
the font bounding-box maximum x (head xMax)
yMax
the font bounding-box maximum y (head yMax)

Glyph

A glyph outline: its contours, advance width, and bounding box, all in font units. An empty glyph (e.g. a space) has no contours.

jennifer
def struct Glyph {
    advance as int,
    xMin as int,
    yMin as int,
    xMax as int,
    yMax as int,
    contours as list of Contour
};
advance
the horizontal advance width
xMin
the bounding-box minimum x
yMin
the bounding-box minimum y
xMax
the bounding-box maximum x
yMax
the bounding-box maximum y
contours
the glyph contours

Point

A point in a glyph contour, in font-unit coordinates.

jennifer
def struct Point {
    x as int,
    y as int,
    onCurve as bool
};
x
the x coordinate
y
the y coordinate
onCurve
whether the point is on the curve (else a quadratic control point)