font

package
v0.0.0-...-6d3a538 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 10 Imported by: 0

README

Font API reference

This library contains font parsers for WOFF, WOFF2, and EOT. It takes a byte-slice as input and converts it to SFNT formats (either TTF or OTF). As font formats for the web, WOFF, WOFF2, and EOT are really just containers for SFNT fonts (such as TTF and OTF) that have better compression.

The WOFF and WOFF2 converters have been testing using the validation tests from the W3C. Font collections (such as TTC and OTC) are not yet supported, but will be in the near future. Compression in EOT files are also not yet supported.

Usage

Import using:

import "github.com/tdewolff/canvas/font"

Then we can parse any byte-slice that is in the WOFF/WOFF2/EOT file format and extract its TTF/OTF content.

font, err := ioutil.ReadFile("DejaVuSerif.woff")
if err != nil {
    panic(err)
}

sfnt, err := font.ToSFNT(font)
if err != nil {
    panic(err)
}

or using an io.Reader

font, err := os.Open("DejaVuSerif.woff")
if err != nil {
    panic(err)
}

font, err = font.NewSFNTReader(font)
if err != nil {
    panic(err)
}
WOFF
woff, err := ioutil.ReadFile("DejaVuSerif.woff")
if err != nil {
    panic(err)
}

sfnt, err := font.ParseWOFF(woff)
if err != nil {
    panic(err)
}

ext := font.Extension(sfnt)
if err = ioutil.WriteFile("DejaVuSerif"+ext, sfnt, 0644); err != nil {
    panic(err)
}

Tested using https://github.com/w3c/woff/tree/master/woff1/tests.

WOFF2
woff2, err := ioutil.ReadFile("DejaVuSerif.woff2")
if err != nil {
    panic(err)
}

sfnt, err := font.ParseWOFF2(woff2)
if err != nil {
    panic(err)
}

ext := font.Extension(sfnt)
if err = ioutil.WriteFile("DejaVuSerif"+ext, sfnt, 0644); err != nil {
    panic(err)
}

Tested using https://github.com/w3c/woff2-tests.

EOT
eof, err := ioutil.ReadFile("DejaVuSerif.eot")
if err != nil {
    panic(err)
}

sfnt, err = font.ParseEOT(eot)
if err != nil {
    panic(err)
}

ext := font.Extension(sfnt)
if err = ioutil.WriteFile("DejaVuSerif"+ext, sfnt, 0644); err != nil {
    panic(err)
}

License

Released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExceedsMemory = fmt.Errorf("memory limit exceded")

ErrExceedsMemory is returned if the font is malformed.

View Source
var ErrInvalidFontData = fmt.Errorf("invalid font data")

ErrInvalidFontData is returned if the font is malformed.

View Source
var MaxMemory uint32 = 30 * 1024 * 1024

MaxMemory is the maximum memory that can be allocated by a font.

Functions

func Extension

func Extension(b []byte) string

Extension returns the file extension for a given font. An empty string is returned when the font is not recognized.

func MediaType

func MediaType(b []byte) (string, error)

MediaType returns the media type (MIME) for a given font.

func NewSFNTReader

func NewSFNTReader(r io.Reader) (*bytes.Reader, error)

NewReader takes an io.Reader and transforms it into an SFNT reader. That is, given TTF/OTF/WOFF/WOFF2/EOT input, it will return TTF/OTF output.

func ParseEOT

func ParseEOT(b []byte) ([]byte, error)

ParseEOT parses the EOT font format and returns its contained SFNT font format (TTF or OTF). See https://www.w3.org/Submission/EOT/

func ParseWOFF

func ParseWOFF(b []byte) ([]byte, error)

ParseWOFF parses the WOFF font format and returns its contained SFNT font format (TTF or OTF). See https://www.w3.org/TR/WOFF/

func ParseWOFF2

func ParseWOFF2(b []byte) ([]byte, error)

ParseWOFF2 parses the WOFF2 font format and returns its contained SFNT font format (TTF or OTF). See https://www.w3.org/TR/WOFF2/

func ToSFNT

func ToSFNT(b []byte) ([]byte, error)

ToSFNT takes a byte-slice and transforms it into an SFNT byte-slice. That is, given TTF/OTF/WOFF/WOFF2/EOT input, it will return TTF/OTF output.

Types

type Font

type Font sfnt.Font

Font currently uses golang.org/x/image/font/sfnt

func ParseFont

func ParseFont(b []byte) (*Font, error)

ParseFont parses a byte slice and recognized whether it is a TTF, OTF, WOFF, WOFF2, or EOT font format. It will return the parsed font and its mimetype. Currently returns instance of golang.org/x/image/font/sfnt.

func ParseSFNT

func ParseSFNT(b []byte) (*Font, error)

Jump to

Keyboard shortcuts

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