gfont

package module
v0.0.0-...-6a8a844 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 8 Imported by: 0

README

gfont

Get information about any Google Font. No API key required.

Quick start

Google serves different CSS based on your useragent string. WOFF2, WOFF and TTF have Apple variants. WOFF2 and WOFF also have variants that support unicode range.

Use gfont.DownloadCSS to get the CSS for any variant:

cssBytes, _ := gfont.DownloadCSS(gfont.AppleWOFF2, "Domine", "wght@400;500;600;700", nil)
fmt.Printf("%s\n", string(cssBytes))

You can get all the styles by going to Google Fonts website, and select all the styles. Note the embed URL.

Next, parse the CSS into a collection of font objects:

var typefaces gfont.Typefaces
_ := gfont.UnmarshalCSS(cssBytes, &typefaces)

Each font object has its properties all parsed out. It can also be converted back to CSS:

fmt.Printf("format: %s\n", typefaces.Fonts[0].Format)
fmt.Printf("%s\n", typefaces.Fonts[0].PrettyCSS())

You can select fonts from a collection:

ts := typefaces.Select("", "Domine", "normal", 400)
for _, v := range ts {
    fmt.Printf("%s: %s\n", v.Format, v.URL.String())
}

Generate CSS from a collection of fonts:

fmt.Printf("%s\n", typefaces.PrettyCSS())

CLI utility

If you need to use the above functionality on the commandline, check out the gfontc subfolder.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadCSS

func DownloadCSS(ua FontProfile, fontFamily, fontStyle string, mirror *url.URL) ([]byte, error)

DownloadCSS downloads font-face CSS from Google API

func GetURL

func GetURL(ua FontProfile, fontFamily, fontStyle string, mirror *url.URL) string

GetURL returns the URL for font-face CSS from Google API

func UnmarshalCSS

func UnmarshalCSS(cssBytes []byte, typefaces *Typefaces) error

UnmarshalCSS unmarshals a byte slice to Typefaces

Types

type FontProfile

type FontProfile int

FontProfile is a font profile for specific devices and format

const (
	// WOFF2 font format supported by most modern browsers
	WOFF2 FontProfile = iota
	// AppleWOFF2 is WOFF2 font format optimized for Apple devices
	AppleWOFF2
	// LegacyWOFF2 is WOFF2 font format without support for Unicode range feature
	LegacyWOFF2
	// AppleLegacyWOFF2 is WOFF2 font format optimized for Apple devices, without support for Unicode range feature
	AppleLegacyWOFF2
	// WOFF font format supported by most modern browsers
	WOFF
	// AppleWOFF is WOFF font format optimized for Apple devices
	AppleWOFF
	// LegacyWOFF is WOFF font format without support for Unicode range feature
	LegacyWOFF
	// AppleLegacyWOFF is WOFF font format optimized for Apple devices, without support for Unicode range feature
	AppleLegacyWOFF
	// TTF is TTF font format
	TTF
	// AppleTTF is TTF font format optimized for Apple devices
	AppleTTF
	// SVG is SVG font format
	SVG
	// EOT is EOT font format
	EOT
)

type Typeface

type Typeface struct {
	Format       string   `json:"format"`
	Weight       int      `json:"weight"`
	Family       string   `json:"family"`
	Style        string   `json:"style"`
	URL          *url.URL `json:"url"`
	UnicodeRange []string `json:"unicodeRange,omitempty"`
}

Typeface represents a font face

func (*Typeface) CSS

func (t *Typeface) CSS() string

CSS returns the CSS representation of a TypeFace

func (*Typeface) FileName

func (t *Typeface) FileName() string

FileName returns the font filename by parsing the URL

func (*Typeface) MarshalJSON

func (t *Typeface) MarshalJSON() ([]byte, error)

MarshalJSON returns a JSON representation of Typeface

func (*Typeface) PrettyCSS

func (t *Typeface) PrettyCSS() string

PrettyCSS is the human readable version of method CSS

func (*Typeface) String

func (t *Typeface) String() string

func (*Typeface) UnmarshalJSON

func (t *Typeface) UnmarshalJSON(data []byte) error

UnmarshalJSON converts a JSON representation of Typeface to Typeface

func (*Typeface) Version

func (t *Typeface) Version() string

Version returns the font version by parsing the URL

type Typefaces

type Typefaces struct {
	Fonts []Typeface `json:"fonts"`
}

Typefaces represents a collection of Typeface

func (*Typefaces) CSS

func (ts *Typefaces) CSS() string

CSS returns the CSS for all fonts in the collection, in the most legacy compatible manner

func (*Typefaces) Family

func (ts *Typefaces) Family() []string

Family returns a unique list of font families

func (*Typefaces) Format

func (ts *Typefaces) Format() []string

Format returns a unique list of font formats

func (*Typefaces) PrettyCSS

func (ts *Typefaces) PrettyCSS() string

PrettyCSS is the human readable version of method CSS

func (*Typefaces) Select

func (ts *Typefaces) Select(format, family, style string, weight int) []Typeface

Select returns a slice of Typeface based on the selection criteria

func (*Typefaces) Style

func (ts *Typefaces) Style() []string

Style returns a unique list of font style

func (*Typefaces) URL

func (ts *Typefaces) URL() []*url.URL

URL returns a unique list of font URLs

func (*Typefaces) Weight

func (ts *Typefaces) Weight() []int

Weight returns a unique list of font weights

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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