fontlab.FontMetrics
FontMetrics
Vertical metric values for one master.
What is FontMetrics?
Every master in a font has a set of vertical metrics: how tall is the tallest letter, how far does the descender drop below the baseline, how tall are the lowercase letters, and so on. FontMetrics is a simple container that holds these six values for one master.
These are the values that determine the overall proportions of your design: - UPM — the grid size. All other values are relative to this. - Ascender — the top of tall lowercase letters like 'd', 'h', 'l'. Typically 70–80% of UPM. - Descender — the bottom of descending letters like 'g', 'p', 'y'. Typically -20% to -30% of UPM (negative). - x-height — the top of short lowercase letters like 'x', 'n', 'o'. Typically 45–55% of UPM. - Cap height — the top of uppercase letters like 'H', 'I'. Typically 60–75% of UPM. - Line gap — extra space between lines. Usually 0 for most text fonts.
You will use FontMetrics mainly to read or verify master-specific metrics in scripts. For the values exported into the OpenType tables, use FontInfo instead.
Tutorial
import fontlab as fl
from fontlab import FontMetrics
font = fl.CurrentFont()
# ============================================================
# Read the vertical metrics from the first master.
# ============================================================
master = font.masters[0]
fm = FontMetrics(font, master)
print(f"Metrics for master '{master}':")
print(f" UPM: {fm.upm:.0f}")
print(f" Ascender: {fm.ascender:.0f}")
print(f" Descender: {fm.descender:.0f}")
print(f" x-Height: {fm.xHeight:.0f}")
print(f" Cap Height: {fm.capsHeight:.0f}")
print(f" Line Gap: {fm.lineGap:.0f}")
# ============================================================
# Compute some useful ratios from the metrics.
# These ratios describe the proportions of the typeface.
# ============================================================
total_height = fm.ascender - fm.descender
x_ratio = fm.xHeight / fm.upm * 100
cap_ratio = fm.capsHeight / fm.upm * 100
print(f"\nProportions:")
print(f" Total height (asc-dsc): {total_height:.0f} units")
print(f" x-height / UPM: {x_ratio:.1f}%")
print(f" Cap height / UPM: {cap_ratio:.1f}%")
# ============================================================
# Check all masters have consistent x-height values.
# Inconsistent x-heights across masters cause problems in
# variable fonts.
# ============================================================
print("\nChecking x-height consistency across masters:")
x_heights = {}
for master_name in font.masters:
fm = FontMetrics(font, master_name)
x_heights[master_name] = fm.xHeight
print(f" [{master_name}] x-height = {fm.xHeight:.0f}")
values = list(x_heights.values())
if len(set(values)) == 1:
print("✓ All masters have the same x-height.")
else:
print(f"⚠ x-heights differ across masters!")
for name, val in x_heights.items():
print(f" {name}: {val:.0f}")
# ============================================================
# Create a FontMetrics struct and set values manually.
# Useful when you want to compare or document intended values.
# ============================================================
target = FontMetrics(1000.0) # 1000 UPM
target.ascender = 730
target.descender = -270
target.xHeight = 540
target.capsHeight = 710
target.lineGap = 0
print(f"\nTarget metrics: "
f"asc={target.ascender} dsc={target.descender} "
f"xh={target.xHeight} cap={target.capsHeight}")
Common Scripting Patterns
Print a metrics summary for every master
import fontlab as fl
from fontlab import FontMetrics
font = fl.CurrentFont()
print(f"{'Master':<20} {'UPM':>6} {'Asc':>6} {'Dsc':>6} {'xH':>6} {'Cap':>6}")
for master in font.masters:
fm = FontMetrics(font, master)
print(f"{master:<20} {fm.upm:>6.0f} {fm.ascender:>6.0f} "
f"{fm.descender:>6.0f} {fm.xHeight:>6.0f} {fm.capsHeight:>6.0f}")
Check that ascender + |descender| ≤ UPM
import fontlab as fl
from fontlab import FontMetrics
font = fl.CurrentFont()
for master in font.masters:
fm = FontMetrics(font, master)
total = fm.ascender + abs(fm.descender)
if total > fm.upm:
print(f"⚠ [{master}] asc+|dsc| = {total:.0f} > UPM {fm.upm:.0f}")
else:
print(f"✓ [{master}] asc+|dsc| = {total:.0f} (fits within UPM)")
Constructors: FontMetrics() - default FontMetrics(FontMetrics) FontMetrics(double upm) FontMetrics(fg::Package *p) FontMetrics(flPackage p, string layer_name)
Native bases: object
Captured native MRO: FontMetrics, object
Native new allocation evidence (not an init overload): (args, *kwargs) Create and return a new object. See help(type) for accurate signature.
Supplemental documentation:
Vertical metrics record for a font. The captured declaration exposes upm, ascender, descender, lineGap, capsHeight and xHeight as numeric (double) attributes, and init for initialisation. Consult the retained constructor documentation for the supported creation forms, which take a units-per-em value, an existing record, or a package with a layer name.
Source: FontLab Proteus API documentation
Source: generated/fontlab.py: FontMetrics captured declaration
Complete member index
Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.
| Member | Kind | Signature | Summary |
|---|---|---|---|
__init__ |
function | (self, /, *args: Any, **kwargs: Any) -> None |
Initialize self. (shared captured text) |
ascender |
attribute | Any |
The ascender line position above the baseline (positive value). |
capsHeight |
attribute | Any |
The cap height — the top of uppercase letters like 'H', 'I'. |
descender |
attribute | Any |
The descender line position below the baseline (negative value). |
init |
function | (self) -> Any |
Resets all values to their defaults (0 for all except UPM which becomes 1000). |
lineGap |
attribute | Any |
Extra vertical space added between lines when text is laid out at 100% leading. |
upm |
attribute | Any |
Units per em — the fundamental scale of the font grid. |
xHeight |
attribute | Any |
The x-height — the top of the lowercase letters like 'x', 'n', 'o'. |