A Go tool that parses font files (TrueType/OpenType) and reports comprehensive typography metrics. Designed for design systems and typography research.
- Complete Font Metrics: Extract x-height, ascenders, descenders, cap height, and line gap
- Kerning Analysis: Estimate kerning pairs in fonts
- Spacing Ratios: Calculate all metrics as ratios of the Em square
- Character Widths: Report advance widths for common characters
- Multiple Output Formats: Text summaries, comparison tables, and JSON
- Font Comparison: Compare metrics across multiple fonts side-by-side
go install github.com/BaseMax/go-type-metrics/cmd/go-type-metrics@latestOr build from source:
git clone https://github.com/BaseMax/go-type-metrics.git
cd go-type-metrics
go build -o go-type-metrics ./cmd/go-type-metricsgo-type-metrics font.ttfExample output:
=== Font Metrics Summary ===
Font Name: Liberation Sans
Units Per Em: 2048
Glyph Count: 2620
=== Vertical Metrics ===
Ascender: 1854 (90.53% of em)
Descender: 434 (-21.19% of em)
Line Gap: 935 (45.65% of em)
X-Height: 1082.00 (52.83% of em)
Cap Height: 1409.00 (68.80% of em)
=== Spacing Ratios ===
Ascent/Em: 0.9053
Descent/Em: -0.2119
X-Height/Em: 0.5283
Cap-Height/Em: 0.6880
Line-Gap/Em: 0.4565
=== Kerning Information ===
Estimated Pairs: 600
=== Character Advance Widths ===
'[space]': 569.00 (27.78% of em)
'A': 1366.00 (66.70% of em)
'M': 1706.00 (83.30% of em)
...
go-type-metrics -compare font1.ttf font2.ttf font3.ttfExample output:
=== Font Comparison Table ===
Font Name | Units/Em | Ascender | Descender | X-Height | Cap-Height | Kerning
-----------------+------------+------------+------------+------------+------------+-----------
Liberation Sans | 2048 | 1854 | 434 | 1082.0 | 1409.0 | 600
Liberation Serif | 2048 | 1825 | 443 | 940.0 | 1341.0 | 600
Liberation Mono | 2048 | 1705 | 615 | 1082.0 | 1349.0 | 0
=== Spacing Ratios Comparison ===
Font Name | Ascent/Em | Descent/Em | X-Height/Em | Cap-Ht/Em
-----------------+--------------+--------------+--------------+-------------
Liberation Sans | 0.9053 | -0.2119 | 0.5283 | 0.6880
Liberation Serif | 0.8911 | -0.2163 | 0.4590 | 0.6548
Liberation Mono | 0.8325 | -0.3003 | 0.5283 | 0.6587
go-type-metrics -json font.ttfExample output:
{
"fontName": "Liberation Sans",
"unitsPerEm": 2048,
"glyphCount": 2620,
"ascender": 1854,
"descender": 434,
"lineGap": 935,
"xHeight": 1082.00,
"capHeight": 1409.00,
"kerningPairs": 600,
"spacingRatios": {
"ascentToEmRatio": 0.9053,
"descentToEmRatio": -0.2119,
"xHeightToEmRatio": 0.5283,
"capHeightToEmRatio": 0.6880,
"lineGapToEmRatio": 0.4565
},
"advanceWidths": {
"A": 1366.00,
"M": 1706.00,
...
}
}-compare: Compare multiple fonts in a table format-json: Output single font metrics in JSON format-help: Show help message
- Ascender: The distance from the baseline to the top of the tallest glyphs
- Descender: The distance from the baseline to the bottom of the lowest glyphs (typically negative)
- Line Gap: Additional spacing between lines of text
- X-Height: The height of lowercase letters (measured using 'x')
- Cap Height: The height of capital letters (measured using 'H')
All metrics are also expressed as ratios relative to the font's Em square (Units Per Em), making it easier to compare fonts of different sizes.
The tool estimates the number of kerning pairs in the font by sampling common character combinations and extrapolating.
Advance widths for common characters show how much horizontal space each character occupies, useful for calculating text layout and spacing.
- TrueType fonts (.ttf)
- OpenType fonts (.otf)
- Design Systems: Compare and document typography metrics across brand fonts
- Font Selection: Evaluate and compare fonts for readability and layout characteristics
- Typography Research: Analyze font characteristics for academic or design research
- Automated Testing: Validate font metrics in CI/CD pipelines (using JSON output)
- Font Documentation: Generate comprehensive metric reports for font libraries
You can also use go-type-metrics as a library in your Go projects:
import (
"github.com/BaseMax/go-type-metrics/parser"
"github.com/BaseMax/go-type-metrics/output"
)
// Parse a font file
metrics, err := parser.ParseFont("path/to/font.ttf")
if err != nil {
log.Fatal(err)
}
// Print summary to stdout
output.PrintSummary(os.Stdout, metrics)
// Or get JSON output
output.PrintJSON(os.Stdout, metrics)The project is organized into three main packages:
- parser: Font file parsing and metrics extraction using
golang.org/x/image/font/sfnt - output: Formatting and display of metrics in various formats
- cmd/go-type-metrics: Command-line interface
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Built using the excellent golang.org/x/image/font/sfnt package for font parsing.