font

package
v0.14.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 22 Imported by: 63

Documentation

Overview

Package font provides support for TrueType fonts.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingFontDir signals a missing destination font directory.
	ErrMissingFontDir = errors.New("missing font directory")

	// ErrMissingFontName signals a missing font name.
	ErrMissingFontName = errors.New("missing font name")

	// ErrMissingFontData signals missing font data.
	ErrMissingFontData = errors.New("missing font data")

	// ErrInvalidFontData signals malformed or inconsistent font data.
	ErrInvalidFontData = errors.New("invalid font data")

	// ErrUnsupportedFontFormat signals a valid but unsupported font format.
	ErrUnsupportedFontFormat = errors.New("unsupported font format")

	// ErrUnknownFont signals an unavailable font.
	ErrUnknownFont = errors.New("unknown font")

	// ErrDuplicatePostScriptName signals conflicting PostScript output names.
	ErrDuplicatePostScriptName = errors.New("duplicate PostScript name")
)
View Source
var UserFontDir string

UserFontDir is the location for installed TTF or OTF font files.

Functions

func Ascent added in v0.3.3

func Ascent(fontName string, fontSize int) (float64, error)

Ascent returns fontName's ascent in user-space units for fontSize.

func BoundingBox

func BoundingBox(fontName string) (*types.Rectangle, error)

BoundingBox returns the font bounding box for a given font.

func CharWidth

func CharWidth(fontName string, r rune) (int, error)

CharWidth returns the character width for a char and font in glyph space units.

func CoreFontNames

func CoreFontNames() []string

CoreFontNames returns a list of the 14 PDF standard Type 1 fonts.

func Descent added in v0.3.3

func Descent(fontName string, fontSize int) (float64, error)

Descent returns fontName's descent in user-space units for fontSize.

func GlyphSpaceUnits added in v0.3.3

func GlyphSpaceUnits(userSpaceUnits float64, fontScalingFactor int) float64

GlyphSpaceUnits transforms userSpaceUnits into glyphspace Units.

func GlyphSpaceUnitsFloat added in v0.14.0

func GlyphSpaceUnitsFloat(userSpaceUnits, fontScalingFactor float64) float64

GlyphSpaceUnitsFloat transforms userSpaceUnits into glyphspace units using a fractional font scaling factor.

func InstallFontFromBytes added in v0.7.0

func InstallFontFromBytes(fontDir, fontName string, bb []byte) error

InstallFontFromBytes saves an internal representation of TrueType font fontName to the pdfcpu config dir.

func InstallFontFromBytesQuiet added in v0.14.0

func InstallFontFromBytesQuiet(fontDir, fontName string, bb []byte) error

InstallFontFromBytesQuiet saves an internal representation of TrueType font fontName without logging.

func IsCoreFont

func IsCoreFont(fontName string) bool

IsCoreFont returns true for the 14 PDF standard Type 1 fonts.

func IsUserFont

func IsUserFont(fontName string) (bool, error)

IsUserFont returns true for installed TrueType fonts.

func LineHeight added in v0.3.3

func LineHeight(fontName string, fontSize int) (float64, error)

LineHeight returns fontName's line height in user-space units for fontSize.

func LoadUserFonts added in v0.3.6

func LoadUserFonts() error

LoadUserFonts loads any installed TTF or OTF font files. This function is idempotent - it can be called multiple times safely. The actual loading happens exactly once, protected by sync.Once.

func Read

func Read(fileName string) (bb []byte, err error)

Read reads the embedded font bytes from an installed font representation.

func ReloadUserFonts added in v0.14.0

func ReloadUserFonts() error

ReloadUserFonts reloads installed user fonts after the font directory has changed.

func Size

func Size(text, fontName string, width float64) (int, error)

Size returns the font size needed to fit text into width.

func SizeForLineHeight added in v0.7.0

func SizeForLineHeight(fontName string, lh float64) (int, error)

SizeForLineHeight returns the needed font size in points for rendering using a given font name fitting into given line height lh.

func Subset added in v0.3.7

func Subset(fontName string, usedGIDs map[uint16]bool) ([]byte, error)

Subset creates a new font file based on usedGIDs.

func SupportedFont

func SupportedFont(fontName string) (bool, error)

SupportedFont returns true for core fonts or installed user fonts.

func TextWidth

func TextWidth(text, fontName string, fontSize int) (float64, error)

TextWidth returns the width in user-space units for text.

func TextWidthFloat added in v0.14.0

func TextWidthFloat(text, fontName string, fontSize float64) (float64, error)

TextWidthFloat returns the width in user-space units for text using a fractional font size.

func UserFontNames

func UserFontNames() ([]string, error)

UserFontNames returns a list of all installed TrueType fonts.

func UserFontNamesVerbose added in v0.3.7

func UserFontNamesVerbose() ([]string, error)

UserFontNamesVerbose returns installed TrueType fonts with glyph counts.

func UserSpaceFontBBox

func UserSpaceFontBBox(fontName string, fontSize int) (*types.Rectangle, error)

UserSpaceFontBBox returns the font box for given font name and font size in user space coordinates.

func UserSpaceUnits added in v0.3.3

func UserSpaceUnits(glyphSpaceUnits float64, fontScalingFactor int) float64

UserSpaceUnits transforms glyphSpaceUnits into userspace units.

func UserSpaceUnitsFloat added in v0.14.0

func UserSpaceUnitsFloat(glyphSpaceUnits, fontScalingFactor float64) float64

UserSpaceUnitsFloat transforms glyphSpaceUnits into userspace units using a fractional font scaling factor.

func ValidateTTFLight added in v0.14.0

func ValidateTTFLight(fd TTFLight) error

ValidateTTFLight validates the semantic invariants required for font embedding.

Types

type InstallReport added in v0.14.0

type InstallReport struct {
	Fonts    []InstallResult
	Warnings []error
}

InstallReport describes successfully installed fonts and non-fatal cleanup failures. A non-nil warning means the fonts were published successfully but temporary or backup artifacts may remain.

func InstallTrueTypeCollection added in v0.3.7

func InstallTrueTypeCollection(fontDir, fn string) (InstallReport, error)

InstallTrueTypeCollection transactionally installs all fonts contained in a TrueType collection. No collection member remains installed when parsing, staging, or commit fails. Cleanup failures after publication are returned in InstallReport.Warnings.

func InstallTrueTypeCollectionResults added in v0.14.0

func InstallTrueTypeCollectionResults(fontDir, fn string) (InstallReport, error)

InstallTrueTypeCollectionResults transactionally installs a TrueType collection and reports every member. No collection member remains installed when parsing, staging, or commit fails. Cleanup failures after publication are returned in InstallReport.Warnings.

func InstallTrueTypeFont

func InstallTrueTypeFont(fontDir, fontName string) (InstallReport, error)

InstallTrueTypeFont saves an internal representation of TrueType font fontName to the pdfcpu config dir.

func InstallTrueTypeFontResult added in v0.14.0

func InstallTrueTypeFontResult(fontDir, fontName string) (InstallReport, error)

InstallTrueTypeFontResult saves a TrueType font and reports its installed PostScript name.

type InstallResult added in v0.14.0

type InstallResult struct {
	PostScriptName string
	Member         int
}

InstallResult identifies an installed PostScript font name and optional collection member.

type TTFLight

type TTFLight struct {
	PostscriptName     string            // name: NameID 6
	Protected          bool              // OS/2: fsType
	UnitsPerEm         int               // head: unitsPerEm
	Ascent             int               // OS/2: sTypoAscender
	Descent            int               // OS/2: sTypoDescender
	CapHeight          int               // OS/2: sCapHeight
	FirstChar          uint16            // OS/2: fsFirstCharIndex
	LastChar           uint16            // OS/2: fsLastCharIndex
	UnicodeRange       [4]uint32         // OS/2: Unicode Character Range
	LLx, LLy, URx, URy float64           // head: xMin, yMin, xMax, yMax (fontbox)
	ItalicAngle        float64           // post: italicAngle
	FixedPitch         bool              // post: isFixedPitch
	Bold               bool              // OS/2: usWeightClass == 7
	HorMetricsCount    int               // hhea: numOfLongHorMetrics
	GlyphCount         int               // maxp: numGlyphs
	GlyphWidths        []int             // hmtx: fd.HorMetricsCount.advanceWidth
	Chars              map[uint32]uint16 // cmap: Unicode character to glyph index
	ToUnicode          map[uint16]uint32 // map glyph index to unicode character
	Planes             map[int]bool      // used Unicode planes
}

TTFLight represents a TrueType font w/o font file.

func UserFont added in v0.14.0

func UserFont(fontName string) (TTFLight, bool, error)

UserFont returns a detached copy of the metrics for an installed TrueType font.

func (TTFLight) Gids added in v0.4.0

func (fd TTFLight) Gids() []int

Gids returns glyph ids for s using fontName.

func (TTFLight) String

func (fd TTFLight) String() string

String returns the string value of fd.

func (TTFLight) SupportsScript added in v0.4.0

func (fd TTFLight) SupportsScript(id string) (bool, error)

SupportsScript returns true if ttf supports the unicodeblocks identified by iso15924 id.

Jump to

Keyboard shortcuts

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