sfnt

package
v0.1.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: 6 Imported by: 0

Documentation

Overview

Package sfnt reads the font tables an embedded PDF font needs and builds a subset containing only the glyphs a document uses.

It reads tables and moves bytes. It does not map characters to glyphs: that mapping is the engine's, through the metrics it measured with, and a second implementation of it in the renderer is how a renderer comes to draw glyphs the printout never measured.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseNames

func ParseNames(raw []byte) map[int]string

ParseNames returns the best string for each name ID in a name table.

Windows records in English win, then any Windows record, then Macintosh, then Unicode. A face carrying several localised strings for one ID must not resolve to whichever happens to come last in the file.

Types

type Font

type Font struct {
	// Tables holds the raw table data, by four-character tag.
	Tables map[string][]byte

	// TrueType reports whether the outlines are in a glyf table.
	// A font with PostScript outlines carries CFF instead and cannot
	// be embedded as the TrueType font program this package writes.
	TrueType bool

	Upem      float64
	NumGlyphs int

	XMin, YMin, XMax, YMax int16
	// Ascender is positive and Descender negative,
	// whichever way the face wrote them. See readHhea.
	Ascender, Descender int16
	CapHeight           int16
	ItalicAngle         float64
	FixedPitch          bool
	WeightClass         uint16

	// PostScriptName is name ID 6, which is the name a PDF
	// font dictionary carries, falling back to the family
	// with its spaces removed the way a PostScript name is formed.
	PostScriptName string
	// contains filtered or unexported fields
}

Font is a parsed font file: its tables, and the values a PDF font descriptor asks for.

func Parse

func Parse(raw []byte, index int) (*Font, error)

Parse reads a font file, or one face of a collection.

func (*Font) Advance

func (font *Font) Advance(gid uint16) uint16

Advance returns a glyph's advance width in font units, read from hmtx.

The last entry of hmtx covers every glyph after it, which is how a font with a run of equal-width glyphs at the end saves space.

func (*Font) GlyphData

func (font *Font) GlyphData(gid uint16) []byte

GlyphData returns one glyph's outline data, empty for a blank glyph.

func (*Font) SideBearing

func (font *Font) SideBearing(gid uint16) int16

SideBearing returns a glyph's left side bearing in font units.

func (*Font) Subset

func (font *Font) Subset(glyphs []uint16) ([]byte, error)

Subset builds a font program holding only the given glyphs.

A glyph's position in glyphs is its index in the result, which is what lets a PDF address it by that index through an identity CID to glyph map. Composite glyphs pull their components in and are rewritten to point at the components' new indices; those extra glyphs land after the requested ones, so no requested glyph moves.

Jump to

Keyboard shortcuts

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