Documentation
¶
Overview ¶
Package font provides support for TrueType fonts.
Index ¶
- Variables
- func Ascent(c context.Context, fontName string, fontSize int) (float64, error)
- func BoundingBox(c context.Context, fontName string) (*types.Rectangle, error)
- func CharWidth(c context.Context, fontName string, r rune) (int, error)
- func CoreFontNames() []string
- func Descent(c context.Context, fontName string, fontSize int) (float64, error)
- func GlyphSpaceUnits(userSpaceUnits float64, fontScalingFactor int) float64
- func GlyphSpaceUnitsFloat(userSpaceUnits, fontScalingFactor float64) float64
- func InstallFontFromBytes(fontDir, fontName string, bb []byte) error
- func InstallFontFromBytesQuiet(fontDir, fontName string, bb []byte) error
- func IsCoreFont(fontName string) bool
- func IsUserFont(c context.Context, fontName string) (bool, error)
- func LineHeight(c context.Context, fontName string, fontSize int) (float64, error)
- func LoadUserFonts(c context.Context) error
- func Read(c context.Context, fileName string) ([]byte, error)
- func ReloadUserFonts(c context.Context) error
- func Size(c context.Context, text, fontName string, width float64) (int, error)
- func SizeForLineHeight(c context.Context, fontName string, lh float64) (int, error)
- func Subset(c context.Context, fontName string, usedGIDs map[uint16]bool) ([]byte, error)
- func SupportedFont(c context.Context, fontName string) (bool, error)
- func TextWidth(c context.Context, text, fontName string, fontSize int) (float64, error)
- func TextWidthFloat(c context.Context, text, fontName string, fontSize float64) (float64, error)
- func UserFontNames(c context.Context) ([]string, error)
- func UserFontNamesVerbose(c context.Context) ([]string, error)
- func UserSpaceFontBBox(c context.Context, fontName string, fontSize int) (*types.Rectangle, error)
- func UserSpaceUnits(glyphSpaceUnits float64, fontScalingFactor int) float64
- func UserSpaceUnitsFloat(glyphSpaceUnits, fontScalingFactor float64) float64
- func ValidateTTFLight(fd TTFLight) error
- type InstallReport
- func InstallTrueTypeCollection(fontDir, fn string) (InstallReport, error)
- func InstallTrueTypeCollectionResults(fontDir, fn string) (InstallReport, error)
- func InstallTrueTypeFont(fontDir, fontName string) (InstallReport, error)
- func InstallTrueTypeFontResult(fontDir, fontName string) (InstallReport, error)
- type InstallResult
- type Repository
- func (r *Repository) BoundingBox(c context.Context, fontName string) (*types.Rectangle, error)
- func (r *Repository) GlyphIDs(c context.Context, text, fontName string) ([]uint16, bool, error)
- func (r *Repository) IsUserFont(c context.Context, fontName string) (bool, error)
- func (r *Repository) Read(c context.Context, fontName string) ([]byte, error)
- func (r *Repository) Size(c context.Context, text, fontName string, width float64) (int, error)
- func (r *Repository) Subset(c context.Context, fontName string, usedGIDs map[uint16]bool) ([]byte, error)
- func (r *Repository) SupportedFont(c context.Context, fontName string) (bool, error)
- func (r *Repository) TextBoundingBox(c context.Context, text, fontName string, fontSize float64) (*types.Rectangle, error)
- func (r *Repository) TextWidth(c context.Context, text, fontName string, fontSize float64) (float64, error)
- func (r *Repository) UserFont(c context.Context, fontName string) (TTFLight, bool, error)
- type TTFLight
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingContext signals a missing required Go context. ErrMissingContext = contextutil.ErrMissingContext // 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") )
var UserFontDir string
UserFontDir is the location for installed TTF or OTF font files.
Functions ¶
func Ascent ¶ added in v0.3.3
Ascent returns fontName's ascent in user-space units for fontSize and supports cancellation.
func BoundingBox ¶
BoundingBox returns the font bounding box for a given font and supports cancellation.
func CharWidth ¶
CharWidth returns the character width for a char and font in glyph space units and supports cancellation.
func CoreFontNames ¶
func CoreFontNames() []string
CoreFontNames returns a list of the 14 PDF standard Type 1 fonts.
func Descent ¶ added in v0.3.3
Descent returns fontName's descent in user-space units for fontSize and supports cancellation.
func GlyphSpaceUnits ¶ added in v0.3.3
GlyphSpaceUnits transforms userSpaceUnits into glyphspace Units.
func GlyphSpaceUnitsFloat ¶ added in v0.14.0
GlyphSpaceUnitsFloat transforms userSpaceUnits into glyphspace units using a fractional font scaling factor.
func InstallFontFromBytes ¶ added in v0.7.0
InstallFontFromBytes saves an internal representation of TrueType font fontName to the pdfcpu config dir.
func InstallFontFromBytesQuiet ¶ added in v0.14.0
InstallFontFromBytesQuiet saves an internal representation of TrueType font fontName without logging.
func IsCoreFont ¶
IsCoreFont returns true for the 14 PDF standard Type 1 fonts.
func IsUserFont ¶
IsUserFont returns true for installed TrueType fonts and supports cancellation.
func LineHeight ¶ added in v0.3.3
LineHeight returns fontName's line height in user-space units for fontSize and supports cancellation.
func LoadUserFonts ¶ added in v0.3.6
LoadUserFonts loads installed font metrics once and supports cancellation. A canceled initial load may be retried.
func Read ¶
Read reads embedded font bytes from an installed font representation and supports cancellation.
func ReloadUserFonts ¶ added in v0.14.0
ReloadUserFonts reloads installed user fonts after the font directory has changed and supports cancellation.
func SizeForLineHeight ¶ added in v0.7.0
SizeForLineHeight returns the needed font size in points for rendering with fontName into line height lh and supports cancellation.
func Subset ¶ added in v0.3.7
Subset creates a new font file based on usedGIDs and supports cancellation.
func SupportedFont ¶
SupportedFont returns true for core fonts or installed user fonts and supports cancellation.
func TextWidth ¶
TextWidth returns the width in user-space units for text and supports cancellation.
func TextWidthFloat ¶ added in v0.14.0
TextWidthFloat returns the width in user-space units for text using a fractional font size and supports cancellation.
func UserFontNames ¶
UserFontNames returns a list of all installed TrueType fonts.
func UserFontNamesVerbose ¶ added in v0.3.7
UserFontNamesVerbose returns installed TrueType fonts with glyph counts.
func UserSpaceFontBBox ¶
UserSpaceFontBBox returns the font box for given font name and font size in user space coordinates and supports cancellation.
func UserSpaceUnits ¶ added in v0.3.3
UserSpaceUnits transforms glyphSpaceUnits into userspace units.
func UserSpaceUnitsFloat ¶ added in v0.14.0
UserSpaceUnitsFloat transforms glyphSpaceUnits into userspace units using a fractional font scaling factor.
func ValidateTTFLight ¶ added in v0.14.0
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
InstallResult identifies an installed PostScript font name and optional collection member.
type Repository ¶ added in v0.16.0
type Repository struct {
// contains filtered or unexported fields
}
Repository provides immutable, lazily loaded font metrics for one user-font directory.
func RepositoryForDir ¶ added in v0.16.0
func RepositoryForDir(dir string) *Repository
RepositoryForDir returns the shared repository for dir. An empty directory selects core fonts only and performs no filesystem access.
func (*Repository) BoundingBox ¶ added in v0.16.0
BoundingBox returns the font bounding box for fontName and supports cancellation.
func (*Repository) GlyphIDs ¶ added in v0.16.0
GlyphIDs returns the glyph IDs for the supported runes in text without exposing repository-owned font metrics. It supports cancellation.
func (*Repository) IsUserFont ¶ added in v0.16.0
IsUserFont reports whether fontName is available as a user font and supports cancellation.
func (*Repository) Read ¶ added in v0.16.0
Read reads embedded font bytes from this repository and supports cancellation.
func (*Repository) Size ¶ added in v0.16.0
Size returns the font size needed to fit text into width and supports cancellation.
func (*Repository) Subset ¶ added in v0.16.0
func (r *Repository) Subset(c context.Context, fontName string, usedGIDs map[uint16]bool) ([]byte, error)
Subset creates a subset font file from this repository and supports cancellation.
func (*Repository) SupportedFont ¶ added in v0.16.0
SupportedFont reports whether fontName is a core or available user font and supports cancellation.
func (*Repository) TextBoundingBox ¶ added in v0.16.0
func (r *Repository) TextBoundingBox(c context.Context, text, fontName string, fontSize float64) (*types.Rectangle, error)
TextBoundingBox returns the bounding box for text positioned at the origin and supports cancellation.
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
UserFont returns a detached copy of the metrics for an installed TrueType font.