NAME
Font::Metrics - advance widths, kerning and metrics for Standard 14 and TrueType fonts
VERSION
Version 0.01
SYNOPSIS
use Font::Metrics;
# Standard 14 font
my $fm = Font::Metrics->new(name => 'Helvetica');
printf "string width: %.2f pt\n", $fm->string_width('Hello World', 12);
printf "line height: %.2f pt\n", $fm->line_height(12);
printf "kern A+V: %.2f pt\n", $fm->kern_pair('A', 'V', 12);
# TrueType font
my $fm = Font::Metrics->new(file => '/path/to/font.ttf');
printf "%.2f\n", $fm->string_width('Hello', 14);
DESCRIPTION
Font::Metrics provides accurate per-glyph advance widths, kerning pair adjustments, and typographic metrics for the PDF Standard 14 fonts and for TrueType/OpenType fonts loaded from disk.
All values are returned in points at the requested size. The underlying data for Standard 14 fonts is sourced from the Adobe AFM files; TrueType metrics are read from the font's hhea, hmtx, cmap, and kern tables.
METHODS
new(%args)
my $fm = Font::Metrics->new(name => 'Helvetica');
my $fm = Font::Metrics->new(file => '/path/to/font.ttf');
Create a metrics object. Pass name for one of the Standard 14 PDF fonts or file for a TrueType/OpenType font path. Croaks on unrecognised names or unreadable files.
Standard 14 font names: Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol, ZapfDingbats.
char_width($char, $size)
Advance width of the first character in $char at $size points.
string_width($text, $size)
Sum of advance widths of all characters in $text at $size points. Does not apply kerning; add kern_pair calls between character pairs for precise measurement.
ascender($size)
Distance from baseline to top of capital letters at $size points (positive value).
descender($size)
Distance from baseline to bottom of descenders at $size points (negative value).
cap_height($size)
Height of capital letters at $size points.
x_height($size)
Height of lower-case letters (e.g. x) at $size points.
line_height($size)
ascender - descender at $size points — the natural leading for single-spaced text.
kern_pair($char1, $char2, $size)
Kerning adjustment between the first characters of $char1 and $char2 at $size points. Returns 0 if no kern pair is defined. Add this value to the advance width of $char1 when laying out the pair.
NOTES
TrueType / OpenType support
The loader reads head, hhea, OS/2, cmap (formats 4 and 12), hmtx, kern (format 0), and GPOS (PairPos formats 1 and 2) tables. Kerning from both tables is merged; GPOS takes precedence on conflicts. This covers OpenType CFF fonts that store kerning exclusively in GPOS with no kern table.
cap_height and x_height are taken from the OS/2 table version 2+ fields sCapHeight / sxHeight. For older fonts (OS/2 version 0 or 1), cap_height falls back to the sTypoAscender value and x_height returns 0.
Standard 14 kerning
Kerning data for the Standard 14 fonts covers the most visually significant Latin pairs (approximately 74 pairs for Helvetica variants and 83 pairs for Times variants) sourced from the Adobe AFM files. Courier, Symbol, and ZapfDingbats have no kern pairs.
AUTHOR
LNATION <email@lnation.org>
LICENSE AND COPYRIGHT
This software is Copyright (c) 2026 by LNATION.
This is free software, licensed under the Artistic License 2.0.