fonts

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: Apache-2.0 Imports: 17 Imported by: 3

Documentation

Overview

Package fonts provides a collection of open source fonts converted to Go.

To use the fonts in your own program, import the main package and import the font(s) that you want.

For example:

import (

"github.com/gmlewis/go-fonts/fonts"
_ "github.com/gmlewis/go-fonts/fonts/znikomitno24"

)

func main() {
  polys := fonts.Text(x, y, xs, ys, "znikomitno24")
  //...
}

Default units are in "em"s which typically represent the width of the character "M" in the font. Note that positive X is to the right and positive Y is up.

Each polygon is either "dark" or "clear". Dark polygons should be rendered before clear ones and should be returned in a natural drawing order.

Index

Constants

View Source
const (

	// If more resolution is needed in the rendered polygons,
	// MaxSteps could be increased.
	MaxSteps = 100

	// These are convenience constants for aligning text.
	XLeft   = 0
	XCenter = 0.5
	XRight  = 1
	YBottom = 0
	YCenter = 0.5
	YTop    = 1
)

Variables

View Source
var (
	// Convenience options for aligning the text.
	BottomLeft   = TextOpts{YAlign: YBottom, XAlign: XLeft}
	BottomCenter = TextOpts{YAlign: YBottom, XAlign: XCenter}
	BottomRight  = TextOpts{YAlign: YBottom, XAlign: XRight}
	CenterLeft   = TextOpts{YAlign: YCenter, XAlign: XLeft}
	Center       = TextOpts{YAlign: YCenter, XAlign: XCenter}
	CenterRight  = TextOpts{YAlign: YCenter, XAlign: XRight}
	TopLeft      = TextOpts{YAlign: YTop, XAlign: XLeft}
	TopCenter    = TextOpts{YAlign: YTop, XAlign: XCenter}
	TopRight     = TextOpts{YAlign: YTop, XAlign: XRight}
)
View Source
var Fonts = map[string]*Font{}

Fonts is a map of all the available fonts.

The map is initialized at runtime by `init` functions in order to reduce the overall initial compile time of the package.

Functions

func FillBox

func FillBox(mbb MBB, xScale, yScale float64, message, fontName string, opts *TextOpts) (x, y, pts float64, err error)

FillBox calculates the (x,y) offsets and pts values required (when using the Text method above) to fill the MBB and align the text according to TextOpts.

func InitFromFontData

func InitFromFontData(font *Font, fontData string)

InitFromFontData is a workaround for a Go compiler error when compiling large map literals. The data is marshaled to a string from a protobuf then base64 encoded into a font package source file.

This function decodes the base64 data, then unmarshals the protobuf and populates the glyphs into the font. Fortunately, this happens extremely quickly in the package's `init` function.

func SavePNG

func SavePNG(filename string, inWidth, inHeight int, renders ...*Render) error

Types

type Font

type Font struct {
	// ID is the name used to identify the font.
	ID string
	// HorizAdvX is the default font units to advance per glyph.
	HorizAdvX float64
	// UnitsPerEm is the number of font units per "em".
	UnitsPerEm float64
	// Ascent is the height of the font above the baseline.
	Ascent float64
	// Descent is the negative vertical distance below the baseline
	// of the font.
	Descent float64
	// MissingHorizAdvX is the amount of font units to advance
	// in the case of missing glyphs.
	MissingHorizAdvX float64
	// Glyphs is a map of the available glyphs, mapped by rune.
	Glyphs map[rune]*Glyph
}

Font represents a webfont.

Each font uses its own native units (typically not "ems") that are later scaled to "ems" when rendered.

type Glyph

type Glyph struct {
	// HorizAdvX is the number of font units to advance for this glyph.
	HorizAdvX float64
	// Unicode is the rune representing this glyph.
	Unicode rune
	// GerberLP is a string of "d" (for "dark") and "c" (for "clear")
	// representing the nature of each subsequent font curve subpath
	// contained within PathSteps. Its length matches the number of
	// subpaths in PathSteps (Starting from 'M' or 'm' path commands.)
	GerberLP string
	// PathSteps represents the SVG commands that define the glyph.
	PathSteps []*PathStep
	// MBB represents the minimum bounding box of the glyph in native units.
	MBB MBB
}

Glyph represents an individual character of the webfont data.

func (*Glyph) Render

func (g *Glyph) Render(x, y, xScale, yScale float64) (float64, *Render)

Render renders a glyph to polygons.

type GlyphInfo

type GlyphInfo struct {
	// Glyph is the rendered rune from the original text message.
	Glyph rune
	// X, Y represent the base position of the glyph.
	X, Y float64
	// Width represents the width of the glyph.
	Width float64
	// MBB represents the minimum bounding box (MBB) of the glyph.
	MBB MBB
	// N represents the number of polygons dedicated to rendering this
	// glyph.
	N int
}

GlyphInfo contains the MBB and base position of a glyph.

type MBB

type MBB = vec2.Rect

MBB represents a minimum bounding box.

func GetRenderMBB added in v0.0.10

func GetRenderMBB(inWidth, inHeight int, renders ...*Render) (mbb MBB, scale float64, width, height int, err error)

func TextMBB

func TextMBB(xPos, yPos, xScale, yScale float64, message, fontName string) (*MBB, error)

TextMBB calculates the minimum bounding box of a text message without the overhead of rendering it.

type PathStep

type PathStep struct {
	C byte      // C is the command.
	P []float64 // P are the parameters of the command.
}

PathStep represents a single subpath command.

There are 20 possible commands, broken up into 6 types, with each command having an "absolute" (upper case) and a "relative" (lower case) version.

Note that not all subpath types are currently supported. Only the ones needed for the provided fonts have been implemented.

See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d for more details.

MoveTo: M, m LineTo: L, l, H, h, V, v Cubic Bézier Curve: C, c, S, s Quadratic Bézier Curve: Q, q, T, t Elliptical Arc Curve: A, a ClosePath: Z, z

type Polygon

type Polygon struct {
	// RuneIndex is the index of the rune that this polygon belongs
	// to in the original rendered text message.
	RuneIndex int
	// Dark represents if this polygon is rendered dark (true) or clear (false).
	Dark bool
	// Pts is the collection of points making up the polygon.
	Pts []Pt
	// MBB represents the MBB of the polygon.
	MBB MBB
}

Polygon represents a dark or clear polygon.

func (*Polygon) Area

func (p *Polygon) Area() float64

Area calculates the area of the polygon.

type Pt

type Pt = vec2.T

Pt represents a 2D Point.

type Render

type Render struct {
	// MBB represents the minimum bounding box (MBB) of the render.
	MBB MBB
	// Polygons are the rendered polygons.
	Polygons []*Polygon
	// Info contains the MBB and base position of each glyph.
	// The length of info is identical to the number of runes in
	// the original text message.
	Info []*GlyphInfo
	// Background is the (optional) background color that the
	// "clear" polygons will use for rendering (default=white).
	Background color.Color
	// Foreground is the (optional) foreground color that the
	// "dark" polygons will use for rendering (default=black).
	Foreground color.Color
}

Render represents a collection of polygons and includes the minimum bounding box of their union.

func Merge

func Merge(renders ...*Render) *Render

func Text

func Text(xPos, yPos, xScale, yScale float64, message, fontName string, opts *TextOpts) (*Render, error)

Text returns a Render representing the rendered text. All dimensions are in "em"s, the width of the character "M" in the desired font.

xScale and yScale are provided to convert the font to any scale desired (or mirror the font with negative values).

func (*Render) RenderToDC

func (r *Render) RenderToDC(dc *gg.Context, dx, dy, scale float64, height int)

func (*Render) SaveDXF

func (r *Render) SaveDXF(filename string, scale float64) error

func (*Render) SavePNG

func (r *Render) SavePNG(filename string, width, height int) error

func (*Render) ToContext added in v0.0.11

func (r *Render) ToContext(width, height int) *gg.Context

func (*Render) ToImage added in v0.0.11

func (r *Render) ToImage(width, height int) image.Image

type TextOpts

type TextOpts struct {
	// XAlign represents the horizontal alignment of the text.
	// 0=x origin at left (the default), 1=x origin at right, 0.5=center.
	// XLeft, XCenter, and XRight are defined for convenience and
	// readability of the code.
	XAlign float64
	// YAlign represents the vertical alignment of the text.
	// 0=y origin at bottom (the default), 1=y origin at top, 0.5=center.
	// YBottom, YCenter, and YTop are defined for convenience and
	// readbility of the code.
	YAlign float64
	// Rotate rotates the entire message about its anchor point
	// by this number of radians.
	Rotate float64
}

TextOpts provides options for positioning (aligning) the text based on its minimum bounding box.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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