font: github.com/ConradIrwin/font/sfnt Index | Files

package sfnt

import "github.com/ConradIrwin/font/sfnt"

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

Package Files

doc.go font.go names.go parse_otf.go parse_woff.go parse_woff2.go table.go table_feature.go table_head.go table_hhea.go table_name.go table_os2.go tag.go write_otf.go

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

type Feature Uses

type Feature struct {
    Tag Tag // Tag for this feature
}

Feature represents a glyph substitution or glyph positioning features.

func (*Feature) String Uses

func (f *Feature) String() string

Script returns the name for this feature.

type File Uses

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 Uses

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 New Uses

func New(scalerType Tag) *Font

New returns an empty Font. It has only an empty 'head' table.

func Parse Uses

func Parse(file File) (*Font, error)

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 Uses

func StrictParse(file File) (*Font, error)

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 Uses

func (font *Font) AddTable(tag Tag, table Table)

AddTable adds a table to the font. If a table with the given tag is already present, it will be overwritten.

func (*Font) GposTable Uses

func (font *Font) GposTable() (*TableLayout, error)

GposTable returns the Glyph Positioning table identified with the 'GPOS' tag.

func (*Font) GsubTable Uses

func (font *Font) GsubTable() (*TableLayout, error)

GsubTable returns the Glyph Substitution table identified with the 'GSUB' tag.

func (*Font) HasTable Uses

func (font *Font) HasTable(tag Tag) bool

HasTable returns true if this font has an entry for the given table.

func (*Font) HeadTable Uses

func (font *Font) HeadTable() (*TableHead, error)

HeadTable returns the table corresponding to the 'head' tag.

func (*Font) HheaTable Uses

func (font *Font) HheaTable() (*TableHhea, error)

func (*Font) NameTable Uses

func (font *Font) NameTable() (*TableName, error)

NameTable returns the table corresponding to the 'name' tag.

func (*Font) OS2Table Uses

func (font *Font) OS2Table() (*TableOS2, error)

func (*Font) RemoveTable Uses

func (font *Font) RemoveTable(tag Tag)

RemoveTable removes a table from the font. If the table doesn't exist, this method will do nothing.

func (*Font) String Uses

func (font *Font) String() string

String provides a debugging representation of a font.

func (*Font) Table Uses

func (font *Font) Table(tag Tag) (Table, error)

func (*Font) TableLayout Uses

func (font *Font) TableLayout(tag Tag) (*TableLayout, error)

func (*Font) Tags Uses

func (font *Font) Tags() []Tag

Tags is the list of tags that are defined in this font, sorted by numeric value.

func (*Font) Type Uses

func (font *Font) Type() Tag

Type represents the kind of glyphs in this font. It is one of TypeTrueType, TypeTrueTypeApple, TypePostScript1, TypeOpenType

func (*Font) WriteOTF Uses

func (font *Font) WriteOTF(w io.Writer) (n int, err error)

WriteOTF serializes a Font into OpenType format suitable for writing to a file such as *.otf. You can also use this to write to files called *.ttf if the font contains TrueType glyphs.

type LangSys Uses

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.

func (*LangSys) String Uses

func (l *LangSys) String() string

String returns the name for this language.

type Lookup Uses

type Lookup struct {
    Type uint16 // Different enumerations for GSUB and GPOS.
    Flag uint16 // Lookup qualifiers.
}

Lookup represents a feature lookup table.

type NameEntry Uses

type NameEntry struct {
    PlatformID PlatformID
    EncodingID PlatformEncodingID
    LanguageID PlatformLanguageID
    NameID     NameID
    Value      []byte
}

func (*NameEntry) Label Uses

func (nameEntry *NameEntry) Label() string

func (*NameEntry) Platform Uses

func (nameEntry *NameEntry) Platform() string

func (*NameEntry) String Uses

func (nameEntry *NameEntry) String() string

String is a best-effort attempt to get a UTF-8 encoded version of Value. Only MicrosoftUnicode (3,1 ,X), MacRomain (1,0,X) and Unicode platform strings are supported.

type NameID Uses

type NameID uint16

NameID is the ID for entries in the font table.

func (NameID) String Uses

func (nameId NameID) String() string

String returns an identifying

type PlatformEncodingID Uses

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 Uses

type PlatformID uint16

PlatformID represents the platform id for entries in the name table.

func (PlatformID) String Uses

func (p PlatformID) String() string

String returns an idenfying string for each platform or "Platform X" for unknown values.

type PlatformLanguageID Uses

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 Uses

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).

func (*Script) String Uses

func (s *Script) String() string

Script returns the name for this script.

type Table Uses

type Table interface {
    Bytes() []byte
    Name() string // Name returns the name of the table.
}

Table is an interface for each section of the font file.

type TableHead Uses

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) Bytes Uses

func (table *TableHead) Bytes() []byte

Bytes returns the byte representation of this header.

func (*TableHead) ClearExpectedChecksum Uses

func (table *TableHead) ClearExpectedChecksum()

ClearExpectedChecksum updates the table so that the checksum can be calculated.

func (*TableHead) ExpectedChecksum Uses

func (table *TableHead) ExpectedChecksum() uint32

ExpectedChecksum is the checksum that the file should have had.

func (TableHead) Name Uses

func (b TableHead) Name() string

Name returns the name of the table represented by this tag.

func (*TableHead) SetExpectedChecksum Uses

func (table *TableHead) SetExpectedChecksum(checksum uint32)

SetExpectedChecksum updates the table so it can be output with the correct checksum.

type TableHhea Uses

type TableHhea struct {
    // contains filtered or unexported fields
}

func (*TableHhea) Bytes Uses

func (table *TableHhea) Bytes() []byte

Bytes returns the byte representation of this header.

func (TableHhea) Name Uses

func (b TableHhea) Name() string

Name returns the name of the table represented by this tag.

type TableLayout Uses

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 Uses

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.

func (TableLayout) Name Uses

func (b TableLayout) Name() string

Name returns the name of the table represented by this tag.

type TableName Uses

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 NewTableName Uses

func NewTableName() *TableName

NewTableName returns an empty NAME table.

func (*TableName) Add Uses

func (table *TableName) Add(entry *NameEntry)

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 Uses

func (table *TableName) AddMacEnglishEntry(nameId NameID, value string) error

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 Uses

func (table *TableName) AddMicrosoftEnglishEntry(nameId NameID, value string) error

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 Uses

func (table *TableName) AddUnicodeEntry(nameId NameID, value string) error

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.

func (*TableName) Bytes Uses

func (table *TableName) Bytes() []byte

Bytes returns the representation of this table to be stored in a font.

func (*TableName) List Uses

func (table *TableName) List() []*NameEntry

List returns a list of all the strings defined in this table.

func (TableName) Name Uses

func (b TableName) Name() string

Name returns the name of the table represented by this tag.

type TableOS2 Uses

type TableOS2 struct {
    // contains filtered or unexported fields
}

func (*TableOS2) Bytes Uses

func (t *TableOS2) Bytes() []byte

func (TableOS2) Name Uses

func (b TableOS2) Name() string

Name returns the name of the table represented by this tag.

type Tag Uses

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 Uses

func MustNamedTag(str string) Tag

MustNamedTag gives you the Tag corresponding to the acronym. This function will panic if the string passed in is not 4 bytes long.

func NamedTag Uses

func NamedTag(str string) (Tag, error)

NamedTag gives you the Tag corresponding to the acronym.

func NewTag Uses

func NewTag(bytes []byte) Tag

func ReadTag Uses

func ReadTag(r io.Reader) (Tag, error)

func (Tag) String Uses

func (tag Tag) String() string

String returns the ASCII representation of the tag.

Package sfnt imports 14 packages (graph) and is imported by 2 packages. Updated 2018-12-17. Refresh now. Tools for package owners.