FontMetrics#

Import

from scenet import FontMetrics

Defined in

scenet.solve.text (source)

Install

pip install scenet

Since

0.13

Stability

Public: follows semantic versioning. Below 1.0, a minor release may change it, and the CHANGELOG says how.

class scenet.solve.text.FontMetrics[source]#

Bases: object

Advance widths and pair kerning, read straight from the font’s own tables.

Kerning is applied, in measurement and in the outlines alike. It used to be ignored, because SVG renderers do not agree on whether to kern <text>, and a measurement the renderer will not reproduce is worse than a slightly generous one. That holds for the SVG’s --live-text and not for the default: outlines are positioned here, glyph by glyph, so no renderer has a say and every pair adjustment is reproduced exactly. So is the print PDF’s live text, whose glyphs are placed here too.

path#

The file it was read from, or the path a document wrote for bytes it was handed. Used to open it and nothing else: no path reaches any output.

sha256#

The digest of the file’s bytes, which is what Panel Core names a face by.

family#

The family name from the font’s name table.

style#

The style within the family: Regular, Italic, Bold Italic.

Pairs come from the GPOS kern feature, or from a legacy kern table in a font that has no GPOS kerning – read with fontTools rather than shaped with HarfBuzz, which Pyodide does not ship. HarfBuzz is the test oracle instead. Only pair adjustments are applied: no ligatures, no contextual alternates, no mark positioning, which is all a Latin line of lettering needs and all that measurement can promise to reproduce.

Example

>>> from scenet import FontMetrics, compile_source, render
>>> from scenet.solve.text import DEFAULT_FONT_PATH
>>> font = FontMetrics(DEFAULT_FONT_PATH)
>>> font.family
'Source Sans Pro'
>>> result = compile_source("cast: {a: {reference: alice}}", metrics=font)
>>> svg = render(result.core, metrics=font)

See also

FontSpec, a font a document declares instead.

__init__(path=PosixPath('/home/runner/work/scenet/scenet/.venv/lib/python3.14/site-packages/font_source_sans_pro/files/SourceSansPro-Regular.ttf'), *, data=None)[source]#

Open a font and read the tables needed for measurement.

Parameters:
  • path (Path) – A TrueType or OpenType file. Defaults to the font that ships as an ordinary dependency of this package – never a system font lookup, because determinism requires the same metrics everywhere.

  • data (bytes | None) – The file’s bytes, already read – from beside a document, or sent with one. path is then only the name it goes by: a family name falls back to its stem, so one font names itself the same from a file or from memory.

Raises:
  • FileReadError – The file cannot be read: a MissingFileError, also a FileNotFoundError, when it is not there.

  • MalformedAssetError – The file is not a TrueType or OpenType font, or has no usable Unicode character map, so no text could be measured against it at all. Also a ValueError.

property extent: tuple[float, float]#

How far the face reaches above and below the baseline, in em units.

The hhea ascender and descender, both as positive distances: what a line of this face can occupy, which is what a sound effect’s footprint is built from.

static digest(data)[source]#

What a font file is known by: the SHA-256 of its bytes, in hex.

Return type:

str

property data: bytes#

The font file’s bytes, as they were read: what print embeds for live text.

property units_per_em: float#

The font’s design grid size, from its head table.

advance(character)[source]#

Advance width of one character, in em units, before any kerning.

Return type:

float

kerning(left, right)[source]#

The adjustment between two neighbouring characters, in em units.

Negative tightens. Zero for a pair the font does not kern, and for a character the font has no glyph for.

Example

>>> from scenet.solve.text import load_metrics
>>> load_metrics().kerning("A", "V") < 0
True
Return type:

float

step(character, following=None)[source]#

How far one character moves the pen, in em units.

Its advance, plus the kerning between it and the character after it, summed in font units before dividing – so a pair the font does not kern steps exactly as far as its advance.

Parameters:
  • character (str) – The character being set.

  • following (str | None) – The one after it in the same face, or None at the end of a run.

Return type:

float

Returns:

The distance to the next character’s origin.

measure(text, font_size)[source]#

Width of a string set at a given size, kerned.

Parameters:
  • text (str) – The string to measure. Not wrapped; measured as one run.

  • font_size (float) – Type size in panel units.

Return type:

float

Returns:

Width in panel units – the sum of every character’s step, which is exactly how far the outlines advance when they are drawn.

Example

>>> from scenet.solve.text import load_metrics
>>> metrics = load_metrics()
>>> metrics.measure("mm", 100) > metrics.measure("ii", 100)
True
line_height(font_size)[source]#

Baseline-to-baseline distance for a given type size.

Parameters:

font_size (float) – Type size in panel units.

Return type:

float

Returns:

font_size * LINE_HEIGHT_FACTOR. A fixed multiple rather than the font’s own ascent-plus-descent, because comics lettering is set to a chosen leading rather than to whatever the typeface suggests.

glyph_outlines(text)[source]#

Each character’s outline as SVG path data, with its step in em units.

The step is the advance plus the kerning to the next character, so the outlines advance by exactly what measure summed.

Converting lettering to outlines rather than emitting <text> is what makes the output genuinely self-contained: no font to embed, no font to be missing, and the rendered shapes are by construction the ones that were measured. The cost is that the text is no longer selectable, which is why --live-text exists – in the print PDF with the face embedded, so exactly.

Return type:

list[tuple[str, float]]

glyph_id(character)[source]#

The id of the glyph a character is drawn with, or None for one the face lacks.

What print letters live text by: a face goes into the PDF with its glyphs at their own ids, so the code for a glyph is its id.

Return type:

int | None

glyph_pdf_path(character)[source]#

A character’s outline as PDF path operators, in font units, y up.

The same outline glyph_outlines() writes for SVG, drawn from the same glyph set – so a composite glyph, an accented letter, is decomposed the same way – for the print emitter. PDF has only cubic curves, so a TrueType outline’s quadratics are written as the cubics that draw exactly the same curves.

Return type:

str

Returns:

m, l, c and h operators, or "" for a character with no outline.