sfnt

package
Version: v0.0.0-...-ce8d41c Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2021 License: MIT Imports: 14 Imported by: 5

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

Constants

This section is empty.

Variables

View Source
var (
	PlatformUnicode   = PlatformID(0)
	PlatformMac       = PlatformID(1)
	PlatformMicrosoft = PlatformID(3)
)
View Source
var (
	PlatformEncodingMacRoman         = PlatformEncodingID(0)
	PlatformEncodingUnicodeDefault   = PlatformEncodingID(0)
	PlatformEncodingMicrosoftUnicode = PlatformEncodingID(1)
)
View Source
var (
	PlatformLanguageMacEnglish       = PlatformLanguageID(0)
	PlatformLanguageUnicodeDefault   = PlatformLanguageID(0)
	PlatformLanguageMicrosoftEnglish = PlatformLanguageID(0x0409)
)
View Source
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)
)
View Source
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")
)
View Source
var ErrInvalidChecksum = errors.New("invalid checksum")

ErrInvalidChecksum is returned by ParseOTF if the font's checksum is wrong

View Source
var ErrMissingHead = errors.New("missing head table in font")

ErrMissingHead is returned by ParseOTF when the font has no head section.

View Source
var ErrMissingTable = errors.New("missing table")

ErrMissingTable is returned from *Table if the table does not exist in the font.

View Source
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.

func (*Feature) String

func (f *Feature) String() string

Script returns the name for this feature.

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 New

func New(scalerType Tag) *Font

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

func Parse

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

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

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

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

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

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

func (*Font) HeadTable

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

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

func (*Font) HheaTable

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

func (*Font) NameTable

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

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

func (*Font) OS2Table

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

func (*Font) RemoveTable

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

func (font *Font) String() string

String provides a debugging representation of a font.

func (*Font) Table

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

func (*Font) TableLayout

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

func (*Font) Tags

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

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

func (*Font) Type

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

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

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

func (l *LangSys) String() string

String returns the name for this language.

type Lookup

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

Lookup represents a feature lookup table.

func (Lookup) GSubString

func (l Lookup) GSubString() string

GSubString returns the Type as a readable entry.

type NameEntry

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

func (*NameEntry) Label

func (nameEntry *NameEntry) Label() string

func (*NameEntry) Platform

func (nameEntry *NameEntry) Platform() string

func (*NameEntry) String

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

type NameID uint16

NameID is the ID for entries in the font table.

func (NameID) String

func (nameId NameID) String() string

String returns an identifying

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

func (*Script) String

func (s *Script) String() string

Script returns the name for this script.

type Table

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

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

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

Bytes returns the byte representation of this header.

func (*TableHead) ClearExpectedChecksum

func (table *TableHead) ClearExpectedChecksum()

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

func (*TableHead) ExpectedChecksum

func (table *TableHead) ExpectedChecksum() uint32

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

func (table *TableHead) SetExpectedChecksum(checksum uint32)

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

type TableHhea

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

func (*TableHhea) Bytes

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

Bytes returns the byte representation of this header.

func (TableHhea) Name

func (b TableHhea) Name() string

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

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.

func (TableLayout) Name

func (b TableLayout) Name() string

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

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 NewTableName

func NewTableName() *TableName

NewTableName returns an empty NAME table.

func (*TableName) Add

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

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

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

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

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

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

func (*TableName) List

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

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

func (TableName) Name

func (b TableName) Name() string

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

type TableOS2

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

func (*TableOS2) Bytes

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

func (TableOS2) Name

func (b TableOS2) Name() string

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

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

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

func NamedTag(str string) (Tag, error)

NamedTag gives you the Tag corresponding to the acronym.

func NewTag

func NewTag(bytes []byte) Tag

func ReadTag

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

func (Tag) String

func (tag Tag) String() string

String returns the ASCII representation of the tag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL