FontMetrics#
Import |
|
Defined in |
|
Install |
|
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:
objectAdvance 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-textand 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
nametable.
- style#
The style within the family:
Regular,Italic,Bold Italic.
Pairs come from the GPOS
kernfeature, or from a legacykerntable 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.pathis 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 aFileNotFoundError, 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
hheaascender 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:
- advance(character)[source]#
Advance width of one character, in em units, before any kerning.
- Return type:
- 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:
- 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.
- measure(text, font_size)[source]#
Width of a string set at a given size, kerned.
- Parameters:
- Return type:
- 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
- glyph_outlines(text)[source]#
Each character’s outline as SVG path data, with its
stepin em units.The step is the advance plus the kerning to the next character, so the outlines advance by exactly what
measuresummed.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-textexists – in the print PDF with the face embedded, so exactly.
- glyph_id(character)[source]#
The id of the glyph a character is drawn with, or
Nonefor 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.
- 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:
- Returns:
m,l,candhoperators, or""for a character with no outline.