Skip to content

A Go tool that parses font files (TrueType/OpenType) and reports comprehensive typography metrics. Designed for design systems and typography research. Analyzes typography metrics from font files.

License

Notifications You must be signed in to change notification settings

BaseMax/go-type-metrics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-type-metrics

A Go tool that parses font files (TrueType/OpenType) and reports comprehensive typography metrics. Designed for design systems and typography research.

Features

  • 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

Installation

go install github.com/BaseMax/go-type-metrics/cmd/go-type-metrics@latest

Or 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-metrics

Usage

Analyze a Single Font

go-type-metrics font.ttf

Example 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)
  ...

Compare Multiple Fonts

go-type-metrics -compare font1.ttf font2.ttf font3.ttf

Example 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

JSON Output

go-type-metrics -json font.ttf

Example 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,
    ...
  }
}

Command Line Options

  • -compare: Compare multiple fonts in a table format
  • -json: Output single font metrics in JSON format
  • -help: Show help message

Metrics Explained

Vertical Metrics

  • 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

Typography Metrics

  • X-Height: The height of lowercase letters (measured using 'x')
  • Cap Height: The height of capital letters (measured using 'H')

Spacing Ratios

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.

Kerning Information

The tool estimates the number of kerning pairs in the font by sampling common character combinations and extrapolating.

Character Widths

Advance widths for common characters show how much horizontal space each character occupies, useful for calculating text layout and spacing.

Supported Formats

  • TrueType fonts (.ttf)
  • OpenType fonts (.otf)

Use Cases

  • 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

Library Usage

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)

Architecture

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

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

Built using the excellent golang.org/x/image/font/sfnt package for font parsing.

About

A Go tool that parses font files (TrueType/OpenType) and reports comprehensive typography metrics. Designed for design systems and typography research. Analyzes typography metrics from font files.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages