Independent research workbench

Can optical kerning work in Typst?

This project tests whether a deterministic algorithm can improve display-text spacing when a font's built-in kerning is missing, sparse, or visually weak, without throwing away good font spacing.

Typst is an open-source, markup-based typesetting system for documents and presentations. It currently exposes kerning as an on/off text option; this workbench investigates a possible opt-in optical mode.

Current answer

The direction works well on the targeted Latin display-text corpus, and a smaller extracted kernel now runs inside a separate Typst compiler prototype. A broad font audit exposed a preservation failure, and the current prototype now includes a bounded response.

The research candidate's combined mean difference across 61 cases is 0.0146em. The smaller compiler-facing candidate scores 0.0222em mean across the current 30-case no-ligature suite and 0.0156em across the 31-case ligature suite after metric preservation. This is evidence for a technical direction, not proof of universal typographic quality.

A 15-font audit initially found 659 cases where the compact candidate changed the sign of effective font kerning. The bounded-prior version changes none and limits its maximum movement from existing positioning to 0.0300em.

EB Garamond WAVY rendered with Typst Metric, InDesign Optical, and the Typst optical kerning candidate.
The core comparison: current Typst metric spacing, InDesign's optical result as an external reference, and the Typst-side candidate.

What optical kerning is

Kerning changes the horizontal space between neighboring shaped glyphs. In metric kerning, the layout engine follows spacing and positioning data supplied by the font and shaping system. In optical kerning, the actual glyph shapes are also examined so visually uneven gaps can be corrected.

Metric

The type designer's spacing remains authoritative. This should stay the default and is usually the right choice for a well-made font used as intended.

Optical

A computed alternative for visible spacing problems, especially large titles, acronyms, display type, weak kern data, mixed shapes, and fonts used outside their intended size.

Large poster and presentation titles expose these problems quickly: a gap such as W|A, A|V, o|T, or 1|0 can look too open or too tight even when the numeric advances are valid. Manual tracking fixes one line, but it does not provide a reusable document-level behavior.

Why InDesign is foregrounded

InDesign Optical is the external publishing reference in this workbench because designers already know it and use it in production. The goal is not to reverse-engineer Adobe or declare its output to be ground truth. It gives reviewers a real, inspectable answer to “compared with what?”

Every optical comparison first passes a metric-parity gate: Typst and InDesign must use the same static font instance, text, size, ligature setting, and closely matching metric output. Only then is the InDesign Optical result compared with the Typst candidate.

Table of four words across several fonts comparing Typst Metric, InDesign Optical, the Typst candidate, and an optical overlay.
A sample-first cross-font excerpt: the same word is stacked across fonts before the next sample begins. The full suites also cover script and other display styles. Every row is rendered by the benchmark, not recreated as browser text.
Sample-first comparison of InDesign Optical and the compact Typst candidate in Libertinus Serif, STIX Two Text, and Latin Modern at 100 points.
An excerpt from the academic display evidence at 100 pt. The same samples are stacked across Libertinus, STIX Two Text, and Latin Modern. Latin Modern OpenType remains a visible outlier.

What the algorithm does

The result of this work is a guarded hybrid algorithm, not one spacing constant. The benchmark first tested nearest-contour distance, whitespace profiles, area balance, metric-prior blending, and conservative fallback behavior. Their failures led to the full guarded-profile-hybrid research reference and then to the smaller compact-guarded runtime candidate.

  1. Shape first. Work with the real glyph run and active OpenType features.
  2. Keep a metric prior. Start from shaped font positioning, prevent sign changes, and bound optical movement around nonzero values.
  3. Read outlines. Build font-local whitespace profiles from glyph contours.
  4. Guard corrections. Reject moves that risk collisions, broken apertures, punctuation errors, or poor word rhythm.
  5. Emit small deltas. Return deterministic, cacheable adjustments in em.
Libre Baskerville final with an fi ligature compared between InDesign Optical, the Typst candidate, and an overlay.
Ligatures are handled after shaping. If fi becomes one glyph, the algorithm spaces that glyph against its neighbors; it does not insert kerning inside the ligature.

Measured result

The first table records the full research reference. The smaller compiler-facing candidate and its preservation trade-off follow directly below it.

Research reference suite Measured cases Mean difference Worst difference
No ligatures 30 / 30 0.0170em 0.0304em
Ligatures enabled 31 / 31 0.0123em 0.0240em
Combined 61 / 61 0.0146em 0.0304em

These scores measure rendered position and width differences against InDesign Optical. They make regressions visible; they do not replace visual review or prove that InDesign is always preferable.

The extracted compact-guarded kernel was compared with two deliberately simpler controls on the same 30 no-ligature cases: nearest contour scored 0.2914em mean, fallback only 0.1061em, and the current bounded compact candidate 0.0222em. On the 31-case ligature suite, bounded compact scores 0.0156em mean and 0.0360em worst. The separate compiler prototype matches the workbench's expected width correction in all 61 current cases within 0.000049em.

What changed after the font-metric audit

A second audit compared the candidate with effective kerning already present in 15 Google Fonts. It retained 15,271 pairs and initially found 659 sign changes and a 0.2720em maximum difference. The generic bounded-prior rule reduces the mean difference from 0.0206em to 0.0095em, caps the maximum at 0.0300em, and removes all sign changes. No font or glyph exceptions were added.

InDesign Optical and the bounded compact candidate compared across six samples and five fonts.
The bounded candidate accepts a small loss of InDesign agreement in exchange for a measurable guarantee around existing font kerning.

From workbench to a possible Typst PR

There is an active upstream discussion in Typst issue #8514, but no assigned implementation or accepted API shape. The current repository is not a merge-ready patch. Its job is to make the algorithmic direction and its tradeoffs concrete.

1 · Complete

Evaluation workbench

Reproducible fonts, shaping, algorithms, InDesign baselines, visual sheets, metrics, and regression cases.

2 · Complete

Small compiler prototype

Extracted dependency-free runtime kernel, post-shaping Typst integration, font/glyph caches, parity checks, and compiler measurements.

3 · Current

Upstream design review

Decide whether the behavior and complexity are worth pursuing, then agree on API, scope, and font-positioning semantics.

4 · Current

Upstream-grade validation

Review the bounded metric policy, expand scripts and font classes, lock compatibility tests, and only then propose a focused PR.

Typst Metric and the opt-in Typst Optical compiler prototype across six samples and five fonts.
Output from the separate Typst compiler prototype. Each sample is stacked across five fonts before the next sample begins.

The exact API remains open. A deliberately small sketch is to preserve the current boolean behavior and add an optical mode rather than prematurely renaming every metric source:

#set text(kerning: true)      // current behavior
#set text(kerning: false)     // current disabled behavior

// Possible future direction, subject to upstream design:
#set text(kerning: "optical")

Repository map

crates/optikern-core

Font loading, shaping, outline profiles, algorithm candidates, guards, and tests.

crates/optikern-runtime

Dependency-free compact pair/run decision kernel used by the compiler experiment.

scripts/

Typst and InDesign runs, parity gates, comparison suites, and reproducible figure generation.

corpus/ and baselines/

Pinned font definitions, sample matrices, compact current evidence, and regression inputs.

docs/

Algorithm rationale, rendering contracts, current findings, research sources, and the upstream path.