woff2

package module
v0.0.0-...-957792c Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2018 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Overview

Package woff2 implements a WOFF2 font decoder.

The WOFF2 font packaging format is specified at https://www.w3.org/TR/WOFF2/.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CollectionDirectory

type CollectionDirectory struct {
	Header  CollectionHeader
	Entries []CollectionFontEntry
}

CollectionDirectory is an optional table containing the font fragment descriptions of font collection entries.

type CollectionFontEntry

type CollectionFontEntry struct {
	NumTables    uint16   // The number of tables in this font.
	Flavor       uint32   // The "sfnt version" of the font.
	TableIndices []uint16 // The indicies identifying an entry in the Table Directory for each table in this font.
}

CollectionFontEntry represents a CollectionFontEntry record.

type CollectionHeader

type CollectionHeader struct {
	Version  uint32
	NumFonts uint16
}

CollectionHeader is a part of CollectionDirectory.

type ExtendedMetadata

type ExtendedMetadata struct{}

ExtendedMetadata is an optional block of extended metadata, represented in XML format and compressed for storage in the WOFF2 file.

type File

type File struct {
	Header         Header
	TableDirectory TableDirectory
	// CollectionDirectory is present only if the font is a collection,
	// as reported by Header.IsCollection.
	CollectionDirectory *CollectionDirectory

	// FontData is the concatenation of data for each table in the font.
	// During storage, it's compressed using Brotli.
	FontData []byte

	ExtendedMetadata *ExtendedMetadata

	// PrivateData is an optional block of private data for the font designer,
	// foundry, or vendor to use.
	PrivateData []byte
}

File represents a parsed WOFF2 file.

func Parse

func Parse(r io.Reader) (File, error)

Parse parses the WOFF2 data from r.

Example
package main

import (
	"fmt"
	"log"

	"dmitri.shuralyov.com/font/woff2"
	"github.com/shurcooL/gofontwoff"
)

func main() {
	f, err := gofontwoff.Assets.Open("/Go-Regular.woff2")
	if err != nil {
		log.Fatalln(err)
	}
	defer f.Close()

	font, err := woff2.Parse(f)
	if err != nil {
		log.Fatalln(err)
	}
	Dump(font)

}

func Dump(f woff2.File) {
	dumpHeader(f.Header)
	fmt.Println()
	dumpTableDirectory(f.TableDirectory)
	fmt.Println()
	fmt.Println("CollectionDirectory:", f.CollectionDirectory)
	fmt.Println("CompressedFontData:", len(f.FontData), "bytes (uncompressed size)")
	fmt.Println("ExtendedMetadata:", f.ExtendedMetadata)
	fmt.Println("PrivateData:", f.PrivateData)
}

func dumpHeader(hdr woff2.Header) {
	fmt.Printf("Signature:           %#08x\n", hdr.Signature)
	fmt.Printf("Flavor:              %#08x\n", hdr.Flavor)
	fmt.Printf("Length:              %d\n", hdr.Length)
	fmt.Printf("NumTables:           %d\n", hdr.NumTables)
	fmt.Printf("Reserved:            %d\n", hdr.Reserved)
	fmt.Printf("TotalSfntSize:       %d\n", hdr.TotalSfntSize)
	fmt.Printf("TotalCompressedSize: %d\n", hdr.TotalCompressedSize)
	fmt.Printf("MajorVersion:        %d\n", hdr.MajorVersion)
	fmt.Printf("MinorVersion:        %d\n", hdr.MinorVersion)
	fmt.Printf("MetaOffset:          %d\n", hdr.MetaOffset)
	fmt.Printf("MetaLength:          %d\n", hdr.MetaLength)
	fmt.Printf("MetaOrigLength:      %d\n", hdr.MetaOrigLength)
	fmt.Printf("PrivOffset:          %d\n", hdr.PrivOffset)
	fmt.Printf("PrivLength:          %d\n", hdr.PrivLength)
}

func dumpTableDirectory(td woff2.TableDirectory) {
	fmt.Println("TableDirectory:", len(td), "entries")
	for _, t := range td {
		fmt.Printf("\t{")
		fmt.Printf("Flags: %#02x, ", t.Flags)
		if t.Tag != nil {
			fmt.Printf("Tag: %v, ", *t.Tag)
		} else {
			fmt.Printf("Tag: <nil>, ")
		}
		fmt.Printf("OrigLength: %v, ", t.OrigLength)
		if t.TransformLength != nil {
			fmt.Printf("TransformLength: %v", *t.TransformLength)
		} else {
			fmt.Printf("TransformLength: <nil>")
		}
		fmt.Printf("}\n")
	}
}
Output:


Signature:           0x774f4632
Flavor:              0x00010000
Length:              46132
NumTables:           14
Reserved:            0
TotalSfntSize:       140308
TotalCompressedSize: 46040
MajorVersion:        1
MinorVersion:        0
MetaOffset:          0
MetaLength:          0
MetaOrigLength:      0
PrivOffset:          0
PrivLength:          0

TableDirectory: 14 entries
	{Flags: 0x06, Tag: <nil>, OrigLength: 96, TransformLength: <nil>}
	{Flags: 0x00, Tag: <nil>, OrigLength: 1318, TransformLength: <nil>}
	{Flags: 0x08, Tag: <nil>, OrigLength: 176, TransformLength: <nil>}
	{Flags: 0x09, Tag: <nil>, OrigLength: 3437, TransformLength: <nil>}
	{Flags: 0x11, Tag: <nil>, OrigLength: 8, TransformLength: <nil>}
	{Flags: 0x0a, Tag: <nil>, OrigLength: 118912, TransformLength: 105020}
	{Flags: 0x0b, Tag: <nil>, OrigLength: 1334, TransformLength: 0}
	{Flags: 0x01, Tag: <nil>, OrigLength: 54, TransformLength: <nil>}
	{Flags: 0x02, Tag: <nil>, OrigLength: 36, TransformLength: <nil>}
	{Flags: 0x03, Tag: <nil>, OrigLength: 2662, TransformLength: <nil>}
	{Flags: 0x04, Tag: <nil>, OrigLength: 32, TransformLength: <nil>}
	{Flags: 0x05, Tag: <nil>, OrigLength: 6967, TransformLength: <nil>}
	{Flags: 0x07, Tag: <nil>, OrigLength: 4838, TransformLength: <nil>}
	{Flags: 0x0c, Tag: <nil>, OrigLength: 188, TransformLength: <nil>}

CollectionDirectory: <nil>
CompressedFontData: 124832 bytes (uncompressed size)
ExtendedMetadata: <nil>
PrivateData: []
type Header struct {
	Signature           uint32 // The identifying signature; must be 0x774F4632 ('wOF2').
	Flavor              uint32 // The "sfnt version" of the input font.
	Length              uint32 // Total size of the WOFF file.
	NumTables           uint16 // Number of entries in directory of font tables.
	Reserved            uint16 // Reserved; set to 0.
	TotalSfntSize       uint32 // Total size needed for the uncompressed font data, including the sfnt header, directory, and font tables (including padding).
	TotalCompressedSize uint32 // Total length of the compressed data block.
	MajorVersion        uint16 // Major version of the WOFF file.
	MinorVersion        uint16 // Minor version of the WOFF file.
	MetaOffset          uint32 // Offset to metadata block, from beginning of WOFF file.
	MetaLength          uint32 // Length of compressed metadata block.
	MetaOrigLength      uint32 // Uncompressed size of metadata block.
	PrivOffset          uint32 // Offset to private data block, from beginning of WOFF file.
	PrivLength          uint32 // Length of private data block.
}

Header is the file header with basic font type and version, along with offsets to metadata and private data blocks.

func (Header) IsCollection

func (hdr Header) IsCollection() bool

IsCollection reports whether this is a font collection, i.e., if the value of Flavor field is set to the TrueType Collection flavor 'ttcf'.

type Table

type Table struct {
	Tag    uint32
	Offset int
	Length int
}

Table is a high-level representation of a table.

type TableDirectory

type TableDirectory []TableDirectoryEntry

TableDirectory is the directory of font tables, containing size and other info.

func (TableDirectory) Tables

func (td TableDirectory) Tables() []Table

Tables returns the derived high-level information about the tables in the table directory.

type TableDirectoryEntry

type TableDirectoryEntry struct {
	Flags           uint8   // Table type and flags.
	Tag             *uint32 // 4-byte tag (optional).
	OrigLength      uint32  // Length of original table.
	TransformLength *uint32 // Transformed length (optional).
}

TableDirectoryEntry is a table directory entry.

Jump to

Keyboard shortcuts

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