NAME

PDF::Make::Font - Font handling for PDF generation

SYNOPSIS

use PDF::Make::Font;

# Create a Standard 14 font
my $font = PDF::Make::Font->standard14('Helvetica');

# Or load a TrueType font
my $font = PDF::Make::Font->from_file('/path/to/font.ttf');

# Get text width
my $width = $font->string_width("Hello World", 12);

# Get individual glyph advance
my $advance = $font->advance(ord('A'), 12);

# Get font metrics
my $metrics = $font->metrics;
print "Ascent: $metrics->{ascent}\n";

DESCRIPTION

PDF::Make::Font provides font handling for PDF generation, supporting:

  • Standard 14 fonts (built into all PDF readers)

  • TrueType font embedding with subsetting

  • Font metrics and glyph widths

  • UTF-8 text encoding

CONSTRUCTORS

standard14($base_font, $arena?)

Create a Standard 14 font by name. Valid names are:

Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique
Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic
Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique
Symbol, ZapfDingbats

from_file($path, $arena?)

Load a TrueType font from a file.

from_bytes($bytes, $arena?)

Load a TrueType font from bytes in memory.

new(%opts)

General constructor. Options:

file      => '/path/to/font.ttf'
bytes     => $ttf_bytes
standard14 => 'Helvetica'  # or std14
arena     => $arena  # optional

METHODS

base_font()

Returns the PostScript font name (e.g., "Helvetica", "ArialMT").

type()

Returns the font type constant (TYPE_TYPE1, TYPE_TRUETYPE, TYPE_CID_TRUETYPE).

is_standard14()

Returns true if this is a Standard 14 font.

is_truetype()

Returns true if this is a TrueType font.

std14_id()

For Standard 14 fonts, returns the font ID constant.

advance($codepoint, $font_size)

Returns the advance width for a single Unicode codepoint at the given font size.

string_width($utf8_string, $font_size)

Returns the total width of a UTF-8 string at the given font size.

metrics()

Returns a hashref of font metrics:

{
    ascent       => 718,    # Ascender height (units/1000 em)
    descent      => -207,   # Descender (negative)
    cap_height   => 718,    # Capital letter height
    x_height     => 523,    # Lowercase x height
    stem_v       => 88,     # Vertical stem width
    stem_h       => 76,     # Horizontal stem width
    italic_angle => 0,      # Italic angle (degrees)
    flags        => 32,     # Font flags
    bbox         => [-166, -225, 1000, 931],  # Bounding box
}

encode_utf8($utf8_string)

Encodes a UTF-8 string to PDF string bytes. For Standard 14 fonts, this produces WinAnsi encoding. For TrueType fonts, this produces CID encoding. Also marks used glyphs for later subsetting.

write_to_doc($doc)

Writes the font to a PDF document and returns the object number.

A Standard 14 font becomes a simple /Type1 dictionary. A TrueType font is subsetted to the glyphs marked used - which is what encode_utf8 marks as it encodes - and written as a /Type0 font with Identity-H encoding over a /CIDFontType2 descendant, with the font program in /FontFile2, a /CIDToGIDMap stream and a /ToUnicode CMap.

Call it after the content streams that use the font are built, or the subset will not contain their glyphs. Writing the same font twice returns the same object number rather than embedding it again.

The object number is not attached to any page by itself: pass it to PDF::Make::Page->add_font_ref to make it usable from a content stream.

SEE ALSO

PDF::Make, PDF::Make::Font::Std14