fontmetrics

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package fontmetrics answers "how wide is this string" using nothing but the standard library.

It exists for the built-in SVG backend. That backend emits <text> elements and lets the viewer shape them, but refract's layout still has to size margins, space ticks and detect label collisions before the SVG is written — and that needs metrics. So this package reads the advance widths out of a TrueType/OpenType file (head, hhea, hmtx, cmap) and nothing else: no shaping, no GSUB/GPOS, no rasterization.

Accuracy

Advances are per-glyph sums with no kerning and no substitution. For Latin chart labels — which is what ticks, axis titles and legends are — that is within a fraction of a pixel of a shaped result. For scripts that require shaping it is an approximation, and refract accepts that on the SVG path: the viewer shapes the text anyway, so exact agreement was never available. The gg backend measures with the same shaper it draws with and is exact.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Face

type Face interface {
	// Advance returns the total advance width of s, in device units.
	Advance(s string) float64
	// Ascent returns the distance from the baseline to the top of the font
	// box, positive, in device units.
	Ascent() float64
	// Descent returns the distance from the baseline down to the bottom of the
	// font box, positive, in device units.
	Descent() float64
}

Face measures text at a specific size.

func Builtin

func Builtin(size float64, weight int, italic bool) Face

Builtin returns a Face over the built-in metrics at the given size.

weight and italic are accepted so callers do not have to special-case the fallback, but only weight is honoured, and crudely: bold text is treated as 4% wider, which is about right for the sans faces this table approximates. Synthetic obliquing does not change advances at all.

type Font

type Font struct {
	// contains filtered or unexported fields
}

Font is a parsed font's size-independent metrics.

func Parse

func Parse(b []byte) (*Font, error)

Parse reads the metric tables out of a TrueType or OpenType font.

It reads exactly four tables — head for unitsPerEm, hhea for the vertical metrics and the hmtx entry count, hmtx for advances, cmap for the rune-to-glyph mapping — and ignores everything else in the file, including the outlines. Parsing is bounds-checked throughout: font files arrive from users and must not be able to panic a render.

func (*Font) Face

func (f *Font) Face(size float64) Face

Face returns a Face for f at the given size in device units.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL