font

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: BSD-3-Clause Imports: 5 Imported by: 3,651

Documentation

Overview

Package font defines an interface for font faces, for drawing text on an image.

Other packages provide font face implementations. For example, a truetype package would provide one based on .ttf font files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoundBytes

func BoundBytes(f Face, s []byte) (bounds fixed.Rectangle26_6, advance fixed.Int26_6)

BoundBytes returns the bounding box of s with f, drawn at a dot equal to the origin, as well as the advance.

It is equivalent to BoundString(string(s)) but may be more efficient.

func BoundString

func BoundString(f Face, s string) (bounds fixed.Rectangle26_6, advance fixed.Int26_6)

BoundString returns the bounding box of s with f, drawn at a dot equal to the origin, as well as the advance.

func MeasureBytes

func MeasureBytes(f Face, s []byte) (advance fixed.Int26_6)

MeasureBytes returns how far dot would advance by drawing s with f.

It is equivalent to MeasureString(string(s)) but may be more efficient.

func MeasureString

func MeasureString(f Face, s string) (advance fixed.Int26_6)

MeasureString returns how far dot would advance by drawing s with f.

Types

type Drawer

type Drawer struct {
	// Dst is the destination image.
	Dst draw.Image
	// Src is the source image.
	Src image.Image
	// Face provides the glyph mask images.
	Face Face
	// Dot is the baseline location to draw the next glyph. The majority of the
	// affected pixels will be above and to the right of the dot, but some may
	// be below or to the left. For example, drawing a 'j' in an italic face
	// may affect pixels below and to the left of the dot.
	Dot fixed.Point26_6
}

Drawer draws text on a destination image.

A Drawer is not safe for concurrent use by multiple goroutines, since its Face is not.

func (*Drawer) BoundBytes

func (d *Drawer) BoundBytes(s []byte) (bounds fixed.Rectangle26_6, advance fixed.Int26_6)

BoundBytes returns the bounding box of s, drawn at the drawer dot, as well as the advance.

It is equivalent to BoundBytes(string(s)) but may be more efficient.

func (*Drawer) BoundString

func (d *Drawer) BoundString(s string) (bounds fixed.Rectangle26_6, advance fixed.Int26_6)

BoundString returns the bounding box of s, drawn at the drawer dot, as well as the advance.

func (*Drawer) DrawBytes

func (d *Drawer) DrawBytes(s []byte)

DrawBytes draws s at the dot and advances the dot's location.

It is equivalent to DrawString(string(s)) but may be more efficient.

func (*Drawer) DrawString

func (d *Drawer) DrawString(s string)

DrawString draws s at the dot and advances the dot's location.

func (*Drawer) MeasureBytes

func (d *Drawer) MeasureBytes(s []byte) (advance fixed.Int26_6)

MeasureBytes returns how far dot would advance by drawing s.

It is equivalent to MeasureString(string(s)) but may be more efficient.

func (*Drawer) MeasureString

func (d *Drawer) MeasureString(s string) (advance fixed.Int26_6)

MeasureString returns how far dot would advance by drawing s.

type Face

type Face interface {
	io.Closer

	// Glyph returns the draw.DrawMask parameters (dr, mask, maskp) to draw r's
	// glyph at the sub-pixel destination location dot, and that glyph's
	// advance width.
	//
	// It returns !ok if the face does not contain a glyph for r. This includes
	// returning !ok for a fallback glyph (such as substituting a U+FFFD glyph
	// or OpenType's .notdef glyph), in which case the other return values may
	// still be non-zero.
	//
	// The contents of the mask image returned by one Glyph call may change
	// after the next Glyph call. Callers that want to cache the mask must make
	// a copy.
	Glyph(dot fixed.Point26_6, r rune) (
		dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool)

	// GlyphBounds returns the bounding box of r's glyph, drawn at a dot equal
	// to the origin, and that glyph's advance width.
	//
	// It returns !ok if the face does not contain a glyph for r. This includes
	// returning !ok for a fallback glyph (such as substituting a U+FFFD glyph
	// or OpenType's .notdef glyph), in which case the other return values may
	// still be non-zero.
	//
	// The glyph's ascent and descent are equal to -bounds.Min.Y and
	// +bounds.Max.Y. The glyph's left-side and right-side bearings are equal
	// to bounds.Min.X and advance-bounds.Max.X. A visual depiction of what
	// these metrics are is at
	// https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyphterms_2x.png
	GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool)

	// GlyphAdvance returns the advance width of r's glyph.
	//
	// It returns !ok if the face does not contain a glyph for r. This includes
	// returning !ok for a fallback glyph (such as substituting a U+FFFD glyph
	// or OpenType's .notdef glyph), in which case the other return values may
	// still be non-zero.
	GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool)

	// Kern returns the horizontal adjustment for the kerning pair (r0, r1). A
	// positive kern means to move the glyphs further apart.
	Kern(r0, r1 rune) fixed.Int26_6

	// Metrics returns the metrics for this Face.
	Metrics() Metrics
}

Face is a font face. Its glyphs are often derived from a font file, such as "Comic_Sans_MS.ttf", but a face has a specific size, style, weight and hinting. For example, the 12pt and 18pt versions of Comic Sans are two different faces, even if derived from the same font file.

A Face is not safe for concurrent use by multiple goroutines, as its methods may re-use implementation-specific caches and mask image buffers.

To create a Face, look to other packages that implement specific font file formats.

type Hinting

type Hinting int

Hinting selects how to quantize a vector font's glyph nodes.

Not all fonts support hinting.

const (
	HintingNone Hinting = iota
	HintingVertical
	HintingFull
)

type Metrics

type Metrics struct {
	// Height is the recommended amount of vertical space between two lines of
	// text.
	Height fixed.Int26_6

	// Ascent is the distance from the top of a line to its baseline.
	Ascent fixed.Int26_6

	// Descent is the distance from the bottom of a line to its baseline. The
	// value is typically positive, even though a descender goes below the
	// baseline.
	Descent fixed.Int26_6

	// XHeight is the distance from the top of non-ascending lowercase letters
	// to the baseline.
	XHeight fixed.Int26_6

	// CapHeight is the distance from the top of uppercase letters to the
	// baseline.
	CapHeight fixed.Int26_6

	// CaretSlope is the slope of a caret as a vector with the Y axis pointing up.
	// The slope {0, 1} is the vertical caret.
	CaretSlope image.Point
}

Metrics holds the metrics for a Face. A visual depiction is at https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png

type Stretch

type Stretch int

Stretch selects a normal, condensed, or expanded face.

Not all fonts support stretches.

const (
	StretchUltraCondensed Stretch = -4
	StretchExtraCondensed Stretch = -3
	StretchCondensed      Stretch = -2
	StretchSemiCondensed  Stretch = -1
	StretchNormal         Stretch = +0
	StretchSemiExpanded   Stretch = +1
	StretchExpanded       Stretch = +2
	StretchExtraExpanded  Stretch = +3
	StretchUltraExpanded  Stretch = +4
)

type Style

type Style int

Style selects a normal, italic, or oblique face.

Not all fonts support styles.

const (
	StyleNormal Style = iota
	StyleItalic
	StyleOblique
)

type Weight

type Weight int

Weight selects a normal, light or bold face.

Not all fonts support weights.

The named Weight constants (e.g. WeightBold) correspond to CSS' common weight names (e.g. "Bold"), but the numerical values differ, so that in Go, the zero value means to use a normal weight. For the CSS names and values, see https://developer.mozilla.org/en/docs/Web/CSS/font-weight

const (
	WeightThin       Weight = -3 // CSS font-weight value 100.
	WeightExtraLight Weight = -2 // CSS font-weight value 200.
	WeightLight      Weight = -1 // CSS font-weight value 300.
	WeightNormal     Weight = +0 // CSS font-weight value 400.
	WeightMedium     Weight = +1 // CSS font-weight value 500.
	WeightSemiBold   Weight = +2 // CSS font-weight value 600.
	WeightBold       Weight = +3 // CSS font-weight value 700.
	WeightExtraBold  Weight = +4 // CSS font-weight value 800.
	WeightBlack      Weight = +5 // CSS font-weight value 900.
)

Directories

Path Synopsis
Package basicfont provides fixed-size font faces.
Package basicfont provides fixed-size font faces.
gofont
gobold
Package gobold provides the "Go Bold" TrueType font from the Go font family.
Package gobold provides the "Go Bold" TrueType font from the Go font family.
gobolditalic
Package gobolditalic provides the "Go Bold Italic" TrueType font from the Go font family.
Package gobolditalic provides the "Go Bold Italic" TrueType font from the Go font family.
goitalic
Package goitalic provides the "Go Italic" TrueType font from the Go font family.
Package goitalic provides the "Go Italic" TrueType font from the Go font family.
gomedium
Package gomedium provides the "Go Medium" TrueType font from the Go font family.
Package gomedium provides the "Go Medium" TrueType font from the Go font family.
gomediumitalic
Package gomediumitalic provides the "Go Medium Italic" TrueType font from the Go font family.
Package gomediumitalic provides the "Go Medium Italic" TrueType font from the Go font family.
gomono
Package gomono provides the "Go Mono" TrueType font from the Go font family.
Package gomono provides the "Go Mono" TrueType font from the Go font family.
gomonobold
Package gomonobold provides the "Go Mono Bold" TrueType font from the Go font family.
Package gomonobold provides the "Go Mono Bold" TrueType font from the Go font family.
gomonobolditalic
Package gomonobolditalic provides the "Go Mono Bold Italic" TrueType font from the Go font family.
Package gomonobolditalic provides the "Go Mono Bold Italic" TrueType font from the Go font family.
gomonoitalic
Package gomonoitalic provides the "Go Mono Italic" TrueType font from the Go font family.
Package gomonoitalic provides the "Go Mono Italic" TrueType font from the Go font family.
goregular
Package goregular provides the "Go Regular" TrueType font from the Go font family.
Package goregular provides the "Go Regular" TrueType font from the Go font family.
gosmallcaps
Package gosmallcaps provides the "Go Smallcaps" TrueType font from the Go font family.
Package gosmallcaps provides the "Go Smallcaps" TrueType font from the Go font family.
gosmallcapsitalic
Package gosmallcapsitalic provides the "Go Smallcaps Italic" TrueType font from the Go font family.
Package gosmallcapsitalic provides the "Go Smallcaps Italic" TrueType font from the Go font family.
Package inconsolata provides pre-rendered bitmap versions of the Inconsolata font family.
Package inconsolata provides pre-rendered bitmap versions of the Inconsolata font family.
Package opentype implements a glyph rasterizer for TTF (TrueType Fonts) and OTF (OpenType Fonts).
Package opentype implements a glyph rasterizer for TTF (TrueType Fonts) and OTF (OpenType Fonts).
Package plan9font implements font faces for the Plan 9 font and subfont file formats.
Package plan9font implements font faces for the Plan 9 font and subfont file formats.
Package sfnt implements a decoder for TTF (TrueType Fonts) and OTF (OpenType Fonts).
Package sfnt implements a decoder for TTF (TrueType Fonts) and OTF (OpenType Fonts).

Jump to

Keyboard shortcuts

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