Documentation ¶
Overview ¶
Package sfnt provides support for sfnt based font formats.
This includes OpenType, TrueType, WOFF, WOFF2, and EOT (though EOT is currently unimplemented).
Usually you will want to parse a font, make modifications, and then output the modified font. If you're really brave, you can build a new font from scratch.
Index ¶
- Variables
- type Feature
- type File
- type Font
- func (font *Font) AddTable(tag Tag, table Table)
- func (font *Font) GposTable() (*TableLayout, error)
- func (font *Font) GsubTable() (*TableLayout, error)
- func (font *Font) HasTable(tag Tag) bool
- func (font *Font) HeadTable() (*TableHead, error)
- func (font *Font) HheaTable() (*TableHhea, error)
- func (font *Font) NameTable() (*TableName, error)
- func (font *Font) OS2Table() (*TableOS2, error)
- func (font *Font) RemoveTable(tag Tag)
- func (font *Font) String() string
- func (font *Font) Table(tag Tag) (Table, error)
- func (font *Font) TableLayout(tag Tag) (*TableLayout, error)
- func (font *Font) Tags() []Tag
- func (font *Font) Type() Tag
- func (font *Font) WriteOTF(w io.Writer) (n int, err error)
- type LangSys
- type Lookup
- type NameEntry
- type NameID
- type PlatformEncodingID
- type PlatformID
- type PlatformLanguageID
- type Script
- type Table
- type TableHead
- type TableHhea
- type TableLayout
- type TableName
- func (table *TableName) Add(entry *NameEntry)
- func (table *TableName) AddMacEnglishEntry(nameId NameID, value string) error
- func (table *TableName) AddMicrosoftEnglishEntry(nameId NameID, value string) error
- func (table *TableName) AddUnicodeEntry(nameId NameID, value string) error
- func (table *TableName) Bytes() []byte
- func (table *TableName) List() []*NameEntry
- func (b TableName) Name() string
- type TableOS2
- type Tag
Constants ¶
This section is empty.
Variables ¶
var ( PlatformUnicode = PlatformID(0) PlatformMac = PlatformID(1) PlatformMicrosoft = PlatformID(3) )
var ( PlatformEncodingMacRoman = PlatformEncodingID(0) PlatformEncodingUnicodeDefault = PlatformEncodingID(0) PlatformEncodingMicrosoftUnicode = PlatformEncodingID(1) )
var ( PlatformLanguageMacEnglish = PlatformLanguageID(0) PlatformLanguageUnicodeDefault = PlatformLanguageID(0) PlatformLanguageMicrosoftEnglish = PlatformLanguageID(0x0409) )
var ( NameCopyrightNotice = NameID(0) NameFontFamily = NameID(1) NameFontSubfamily = NameID(2) NameUniqueIdentifier = NameID(3) NameFull = NameID(4) NameVersion = NameID(5) NamePostscript = NameID(6) NameTrademark = NameID(7) NameManufacturer = NameID(8) NameDesigner = NameID(9) NameDescription = NameID(10) NameVendorURL = NameID(11) NameDesignerURL = NameID(12) NameLicenseDescription = NameID(13) NameLicenseURL = NameID(14) NamePreferredFamily = NameID(16) NamePreferredSubfamily = NameID(17) NameCompatibleFull = NameID(18) NameSampleText = NameID(19) NamePostscriptCID = NameID(20) NameWWSFamily = NameID(21) NameWWSSubfamily = NameID(22) NameLightBackgroundPalette = NameID(23) NameDarkBackgroundPalette = NameID(24) )
var ( // TagHead represents the 'head' table, which contains the font header TagHead = MustNamedTag("head") // TagMaxp represents the 'maxp' table, which contains the maximum profile TagMaxp = MustNamedTag("maxp") // TagHmtx represents the 'hmtx' table, which contains the horizontal metrics TagHmtx = MustNamedTag("hmtx") // TagHhea represents the 'hhea' table, which contains the horizonal header TagHhea = MustNamedTag("hhea") // TagOS2 represents the 'OS/2' table, which contains windows-specific metadata TagOS2 = MustNamedTag("OS/2") // TagName represents the 'name' table, which contains font name information TagName = MustNamedTag("name") // TagGpos represents the 'GPOS' table, which contains Glyph Positioning features TagGpos = MustNamedTag("GPOS") // TagGsub represents the 'GSUB' table, which contains Glyph Substitution features TagGsub = MustNamedTag("GSUB") // TypeTrueType is the first four bytes of an OpenType file containing a TrueType font TypeTrueType = Tag{0x00010000} // TypeAppleTrueType is the first four bytes of an OpenType file containing a TrueType font // (specifically one designed for Apple products, it's recommended to use TypeTrueType instead) TypeAppleTrueType = MustNamedTag("true") // TypePostScript1 is the first four bytes of an OpenType file containing a PostScript Type 1 font TypePostScript1 = MustNamedTag("typ1") // TypeOpenType is the first four bytes of an OpenType file containing a PostScript Type 2 font // as specified by OpenType TypeOpenType = MustNamedTag("OTTO") // SignatureWOFF is the magic number at the start of a WOFF file. SignatureWOFF = MustNamedTag("wOFF") // SignatureWOFF2 is the magic number at the start of a WOFF2 file. SignatureWOFF2 = MustNamedTag("wOF2") )
var ErrInvalidChecksum = errors.New("invalid checksum")
ErrInvalidChecksum is returned by ParseOTF if the font's checksum is wrong
var ErrMissingHead = errors.New("missing head table in font")
ErrMissingHead is returned by ParseOTF when the font has no head section.
var ErrMissingTable = errors.New("missing table")
ErrMissingTable is returned from *Table if the table does not exist in the font.
var ErrUnsupportedFormat = errors.New("unsupported font format")
ErrUnsupportedFormat is returned from Parse if parsing failed
Functions ¶
This section is empty.
Types ¶
type Feature ¶
type Feature struct {
Tag Tag // Tag for this feature
}
Feature represents a glyph substitution or glyph positioning features.
type File ¶
type File interface { Read([]byte) (int, error) ReadAt([]byte, int64) (int, error) Seek(int64, int) (int64, error) }
File is a combination of io.Reader, io.Seeker and io.ReaderAt. This interface is satisfied by most things that you'd want to parse, for example os.File, or io.SectionReader.
type Font ¶
type Font struct {
// contains filtered or unexported fields
}
Font represents a SFNT font, which is the underlying representation found in .otf and .ttf files (and .woff, .woff2, .eot files) SFNT is a container format, which contains a number of tables identified by Tags. Depending on the type of glyphs embedded in the file which tables will exist. In particular, there's a big different between TrueType glyphs (usually .ttf) and CFF/PostScript Type 2 glyphs (usually .otf)
func Parse ¶
Parse parses an OpenType, TrueType, WOFF, or WOFF2 file and returns a Font. If parsing fails, an error is returned and *Font will be nil.
func StrictParse ¶
StrictParse parses an OpenType, TrueType, WOFF or WOFF2 file and returns a Font. Each table will be fully parsed and an error is returned if any fail.
func (*Font) AddTable ¶
AddTable adds a table to the font. If a table with the given tag is already present, it will be overwritten.
func (*Font) GposTable ¶
func (font *Font) GposTable() (*TableLayout, error)
GposTable returns the Glyph Positioning table identified with the 'GPOS' tag.
func (*Font) GsubTable ¶
func (font *Font) GsubTable() (*TableLayout, error)
GsubTable returns the Glyph Substitution table identified with the 'GSUB' tag.
func (*Font) RemoveTable ¶
RemoveTable removes a table from the font. If the table doesn't exist, this method will do nothing.
func (*Font) TableLayout ¶
func (font *Font) TableLayout(tag Tag) (*TableLayout, error)
func (*Font) Tags ¶
Tags is the list of tags that are defined in this font, sorted by numeric value.
type LangSys ¶
type LangSys struct { Tag Tag // Tag for this language. Features []*Feature // Features contains the features for this language. }
LangSys represents the language system for a script.
type Lookup ¶
type Lookup struct { Type uint16 // Different enumerations for GSUB and GPOS. Flag uint16 // Lookup qualifiers. }
Lookup represents a feature lookup table.
type NameEntry ¶
type NameEntry struct { PlatformID PlatformID EncodingID PlatformEncodingID LanguageID PlatformLanguageID NameID NameID Value []byte }
type PlatformEncodingID ¶
type PlatformEncodingID uint16
PlatformEncodingID represents the platform specific id for entries in the name table. the three most common values are provided as constants.
type PlatformID ¶
type PlatformID uint16
PlatformID represents the platform id for entries in the name table.
func (PlatformID) String ¶
func (p PlatformID) String() string
String returns an idenfying string for each platform or "Platform X" for unknown values.
type PlatformLanguageID ¶
type PlatformLanguageID uint16
PlatformLanguageID represents the language used by an entry in the name table, the three most common values are provided as constants.
type Script ¶
type Script struct { Tag Tag // Tag for this script. DefaultLanguage *LangSys // DefaultLanguage used by this script. Languages []*LangSys // Languages within this script. }
Script represents a single script (i.e "latn" (Latin), "cyrl" (Cyrillic), etc).
type TableHead ¶
type TableHead struct {
// contains filtered or unexported fields
}
TableHead contains critical information about the rest of the font. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6head.html
func (*TableHead) ClearExpectedChecksum ¶
func (table *TableHead) ClearExpectedChecksum()
ClearExpectedChecksum updates the table so that the checksum can be calculated.
func (*TableHead) ExpectedChecksum ¶
ExpectedChecksum is the checksum that the file should have had.
func (TableHead) Name ¶
func (b TableHead) Name() string
Name returns the name of the table represented by this tag.
func (*TableHead) SetExpectedChecksum ¶
SetExpectedChecksum updates the table so it can be output with the correct checksum.
type TableHhea ¶
type TableHhea struct {
// contains filtered or unexported fields
}
type TableLayout ¶
type TableLayout struct { Scripts []*Script // Scripts contains all the scripts in this layout. Features []*Feature // Features contains all the features in this layout. Lookups []*Lookup // Lookups contains all the lookups in this layout. // contains filtered or unexported fields }
TableLayout represents the common layout table used by GPOS and GSUB. The Features field contains all the features for this layout. However, the script and language determines which feature is used.
See https://www.microsoft.com/typography/otspec/chapter2.htm#organization See https://www.microsoft.com/typography/otspec/GPOS.htm See https://www.microsoft.com/typography/otspec/GSUB.htm
func (*TableLayout) Bytes ¶
func (t *TableLayout) Bytes() []byte
Bytes returns the bytes for this table. The TableLayout is read only, so the bytes will always be the same as what is read in.
type TableName ¶
type TableName struct {
// contains filtered or unexported fields
}
TableName represents the OpenType 'name' table. This contains human-readable meta-data about the font, for example the Author and Copyright. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
func (*TableName) Add ¶
Add an entry to the table. This is a relatively low-level method, most of what you need can be accomplished using AddUnicodeEntry,AddMacEnglishEntry, and AddMicrosoftEnglishEntry.
func (*TableName) AddMacEnglishEntry ¶
AddMacEnglishEntry adds an entry to the name table for the 'Mac' platform, with Default Encoding (Mac Roman) and the Language set to English. It returns an error if the value cannot be represented in Mac Roman.
func (*TableName) AddMicrosoftEnglishEntry ¶
AddMicrosoftEnglishEntry adds an entry to the name table for the 'Microsoft' platform, with Unicode Encoding (UCS-2) and the language set to English. It returns an error if the string cannot be represented in UCS-2.
func (*TableName) AddUnicodeEntry ¶
AddUnicodeEntry adds an entry to the name table for the 'Unicode' platform, with Default Encoding (UTF-16). It returns an error if the value cannot be represented in UTF-16.
type Tag ¶
type Tag struct {
Number uint32
}
Tag represents an open-type table name. These are technically uint32's, but are usually displayed in ASCII as they are all acronyms. see https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Overview
func MustNamedTag ¶
MustNamedTag gives you the Tag corresponding to the acronym. This function will panic if the string passed in is not 4 bytes long.