webfonts

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 24 Imported by: 1

README

webfonts Go Package

Package webfonts provides a client for retrieving Google Webfonts.

Example

Please see the comprehensive example.

Documentation

Overview

Package webfonts provides client for the google webfonts helper and a way to easily retrieve and serve webfonts.

Index

Constants

View Source
const (
	UserAgentEOT   = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)"
	UserAgentSVG   = "" /* 127-byte string literal not displayed */
	UserAgentTTF   = "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) Safari/538.1 Daum/4.1"
	UserAgentWOFF2 = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"
	UserAgentWOFF  = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"
)

User agents.

Variables

View Source
var DefaultTransport = http.DefaultTransport

DefaultTransport is the default http transport.

Functions

func Available

func Available(ctx context.Context, opts ...ClientOption) ([]*gfonts.Webfont, error)

Available retrieves all available webfonts.

func BuildRoutes

func BuildRoutes(prefix string, fonts []Font, h func(string, []byte, []Route) error) error

BuildRoutes builds routes for the provided font faces.

Types

type Client

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

Client is a webfonts client.

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient creates a new webfonts client.

func (*Client) All added in v0.2.0

func (cl *Client) All(ctx context.Context, family string, opts ...QueryOption) ([]Font, error)

All retrieves all common font faces for the specified family by using multiple user agents (EOT, SVG, TTF, WOFF2, WOFF).

func (*Client) Available

func (cl *Client) Available(ctx context.Context) ([]*gfonts.Webfont, error)

Available retrieves all available webfonts from the google webfonts service.

func (*Client) EOT added in v0.2.0

func (cl *Client) EOT(ctx context.Context, family string, opts ...QueryOption) (Font, error)

EOT retrieves the eot font face for the specified family.

func (*Client) Faces added in v0.2.0

func (cl *Client) Faces(ctx context.Context, family string, opts ...QueryOption) ([]Font, error)

Faces retrieves the font faces for the specified family, building a query using the client's user agent and passed options.

func (*Client) Format added in v0.2.0

func (cl *Client) Format(ctx context.Context, family, format string, opts ...QueryOption) (Font, error)

Format retrieves a font face with the specified format and family.

func (*Client) SVG added in v0.2.0

func (cl *Client) SVG(ctx context.Context, family string, opts ...QueryOption) (Font, error)

SVG retrieves the svg font face for the specified family.

func (*Client) TTF added in v0.2.0

func (cl *Client) TTF(ctx context.Context, family string, opts ...QueryOption) (Font, error)

TTF retrieves the ttf font face for the specified family.

func (*Client) WOFF added in v0.2.0

func (cl *Client) WOFF(ctx context.Context, family string, opts ...QueryOption) (Font, error)

WOFF retrieves the woff font face for the specified family.

func (*Client) WOFF2 added in v0.2.0

func (cl *Client) WOFF2(ctx context.Context, family string, opts ...QueryOption) (Font, error)

WOFF2 retrieves the woff2 font face for the specified family.

type ClientOption

type ClientOption func(*Client)

ClientOption is a webfonts client option.

func WithAppCacheDir

func WithAppCacheDir(appCacheDir string) ClientOption

WithAppCacheDir is a webfonts client option to set the app cache dir.

func WithClientOption

func WithClientOption(opt option.ClientOption) ClientOption

WithClientOption is a webfonts client option to set underlying client options.

func WithKey

func WithKey(key string) ClientOption

WithKey is a webfonts client option to set the google webfonts api key.

func WithLogf

func WithLogf(logf interface{}, opts ...httplog.Option) ClientOption

WithLogf is a webfonts client option to set a log handler for http requests and responses.

func WithTokenSource

func WithTokenSource(source oauth2.TokenSource) ClientOption

WithTokenSource is a webfonts client option to set the token source.

func WithTransport

func WithTransport(transport http.RoundTripper) ClientOption

WithTransport is a webfonts client option to set the http transport.

type Error added in v0.2.0

type Error string

Error is a client error.

const (
	ErrServiceUninitialized Error = "service uninitialized"
	ErrClientUninitialized  Error = "client uninitialized"
	ErrStatusNotOK          Error = "status not ok"
	ErrFormatNotAvailable   Error = "format not available"
)

Errors.

func (Error) Error added in v0.2.0

func (err Error) Error() string

Error satisfies the error interface.

type Font added in v0.2.0

type Font struct {
	Subset  string   `json:"subset,omitempty"`
	Family  string   `json:"font-family,omitempty"`
	Style   string   `json:"font-style,omitempty"`
	Weight  string   `json:"font-weight,omitempty"`
	Display string   `json:"font-display,omitempty"`
	Stretch string   `json:"font-stretch,omitempty"`
	Src     string   `json:"src,omitempty"`
	Format  string   `json:"format,omitempty"`
	Range   []string `json:"unicode-range,omitempty"`
}

Font describes a font face.

func All added in v0.2.0

func All(ctx context.Context, family string, opts ...ClientOption) ([]Font, error)

All retrieves all font faces for the specified family by using multiple user agents.

func EOT added in v0.2.0

func EOT(ctx context.Context, family string, opts ...ClientOption) (Font, error)

EOT retrieves the eot font face for the specified family.

func Faces added in v0.2.0

func Faces(ctx context.Context, family string, opts ...ClientOption) ([]Font, error)

Faces retrieves the font faces for the specified family.

func FontsFromStylesheetReader added in v0.2.0

func FontsFromStylesheetReader(r io.Reader) ([]Font, error)

FontsFromStylesheetReader parses stylesheet from the passed reader, returning any parsed font face.

func Format added in v0.2.0

func Format(ctx context.Context, family, format string, opts ...ClientOption) (Font, error)

Format retrieves a font face with the specified format and family.

func SVG added in v0.2.0

func SVG(ctx context.Context, family string, opts ...ClientOption) (Font, error)

SVG retrieves the svg font face for the specified family.

func TTF added in v0.2.0

func TTF(ctx context.Context, family string, opts ...ClientOption) (Font, error)

TTF retrieves the ttf font face for the specified family.

func WOFF added in v0.2.0

func WOFF(ctx context.Context, family string, opts ...ClientOption) (Font, error)

WOFF retrieves the woff font face for the specified family.

func WOFF2 added in v0.2.0

func WOFF2(ctx context.Context, family string, opts ...ClientOption) (Font, error)

WOFF2 retrieves the woff2 font face for the specified family.

type Query

type Query struct {
	Family    string
	UserAgent string
	Variants  []string
	Subsets   []string
	Styles    []string
	Effects   []string
	Directory string
	Display   string
	Text      string
}

Query wraps a font request.

func NewQuery

func NewQuery(family string, opts ...QueryOption) *Query

NewQuery builds a new webfont query.

func (*Query) String

func (q *Query) String() string

String satisfies the fmt.Stringer interface.

Returns the URL for the request.

func (*Query) Values

func (q *Query) Values() url.Values

Values returns the url values for the request.

type QueryOption

type QueryOption func(*Query)

QueryOption is a webfonts query option.

func WithDirectory added in v0.2.0

func WithDirectory(directory string) QueryOption

WithDirectory is a query option to set directory.

func WithDisplay added in v0.2.0

func WithDisplay(display string) QueryOption

WithDisplay is a query option to set display.

func WithEffects

func WithEffects(effects ...string) QueryOption

WithEffects is a query option to set effects.

func WithStyles

func WithStyles(styles ...string) QueryOption

WithStyles is a query option to set styles.

func WithSubsets

func WithSubsets(subsets ...string) QueryOption

WithSubsets is a query option to set subsets.

func WithText added in v0.2.0

func WithText(text string) QueryOption

WithText is a query option to set text.

func WithUserAgent

func WithUserAgent(userAgent string) QueryOption

WithUserAgent is a query option to set the user agent.

func WithVariants

func WithVariants(variants ...string) QueryOption

WithVariants is a query option to set variants.

type Route

type Route struct {
	Path string
	URL  string
}

Route wraps information about a route. Used for callbacks passed to BuildRoutes.

Directories

Path Synopsis
_example/example.go
_example/example.go

Jump to

Keyboard shortcuts

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