subset

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 9 Imported by: 0

README

Package subset provides functions for subsetting SFNT type fonts. It also provides implementations of the FontSubsetter interface defined in the github.com/cdillond/gdf package. However, it may be used independently of that package.

The TTFSubset function is written entirely in Go, but it may not work with all TrueType/OpenType fonts, including variable fonts and fonts that use CFF outlines. To overcome these limitations, this package also includes functions that depend on HarfBuzz. The HBSubset and HBSubsetPath functions invoke the hb-subset tool via os/exec. The HBSubsetC function, which must be built using the build tag hbsubsetc, uses CGo to call functions in libharfbuzz and libharfbuzz-subset versions 2.9.0 and later.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HBSubset

func HBSubset(src []byte, cutset map[rune]struct{}) ([]byte, error)

HBSubset returns the source bytes of a font containing only the characters included in the cutset. For this function to work, the HarfBuzz hb-subsettool must be installed. The HBSubset func may handle edge cases that the TTFSubset func does not. hb-subset has a mature, well-tested API and is capable of handling more font formats than TTFSubset. However, this approach requires os/exec, so it might not be suitable for all environments.

func HBSubsetC

func HBSubsetC(src []byte, charset map[rune]struct{}) ([]byte, error)

HBSubsetC calls functions in libharfbuzz and libharfbuzz-subset via CGo and returns the source bytes of a font containing only the characters included in the cutset. In order for this function to work, CGo must be enabled, HarfBuzz v>=2.9.0 must be installed on your system, and `hbsubsetc` must be passed to the Go compiler as a build tag.

func HBSubsetPath

func HBSubsetPath(path string, cutset map[rune]struct{}) ([]byte, error)

HBSubset returns the source bytes of a font containing only the characters included in the cutset. The path parameter represents the path of the source font. This function has the same limitations as HBSubset.

func TTFSubset

func TTFSubset(f *sfnt.Font, src []byte, cutset map[rune]struct{}) ([]byte, error)

TTFSubset is something of a poor man's subsetting function. It works - for TrueType fonts with 'glyf' tables only - by zeroing out the outlines of all glyphs not corresponding to or directly referenced by f's glyphs for the runes in cutset, truncating f's glyf and loca tables, and then writing only the required tables to the returned byte slice. The final subset font contains cmap, glyf, head, hhea, hmtx, loca, and maxp tables. The glyph indices are not affected. src should be a copy of the source bytes for f.

Types

type BasicSubsetter

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

func (*BasicSubsetter) Init

func (b *BasicSubsetter) Init(SFNT *sfnt.Font, src []byte, _ string)

func (*BasicSubsetter) Subset

func (b *BasicSubsetter) Subset(cutset map[rune]struct{}) ([]byte, error)

type HarfBuzzCGoSubsetter

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

func (*HarfBuzzCGoSubsetter) Init

func (h *HarfBuzzCGoSubsetter) Init(_ *sfnt.Font, src []byte, _ string)

func (*HarfBuzzCGoSubsetter) Subset

func (h *HarfBuzzCGoSubsetter) Subset(cutset map[rune]struct{}) ([]byte, error)

type HarfBuzzSubsetter

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

func (*HarfBuzzSubsetter) Init

func (h *HarfBuzzSubsetter) Init(_ *sfnt.Font, src []byte, path string)

func (*HarfBuzzSubsetter) Subset

func (h *HarfBuzzSubsetter) Subset(cutset map[rune]struct{}) ([]byte, error)

type TableTag

type TableTag uint32
const (
	Cmap TableTag = 'c'<<24 | 'm'<<16 | 'a'<<8 | 'p'
	Glyf TableTag = 'g'<<24 | 'l'<<16 | 'y'<<8 | 'f'
	Head TableTag = 'h'<<24 | 'e'<<16 | 'a'<<8 | 'd'
	Hhea TableTag = 'h'<<24 | 'h'<<16 | 'e'<<8 | 'a'
	Hmtx TableTag = 'h'<<24 | 'm'<<16 | 't'<<8 | 'x'
	Loca TableTag = 'l'<<24 | 'o'<<16 | 'c'<<8 | 'a'
	Maxp TableTag = 'm'<<24 | 'a'<<16 | 'x'<<8 | 'p'
)

Jump to

Keyboard shortcuts

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