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
- Variables
- func FillBox(mbb MBB, xScale, yScale float64, message, fontName string, opts *TextOpts) (x, y, pts float64, err error)
- func InitFromFontData(font *Font, fontData string)
- func SavePNG(filename string, inWidth, inHeight int, renders ...*Render) error
- type Font
- type Glyph
- type GlyphInfo
- type MBB
- type PathStep
- type Polygon
- type Pt
- type Render
- func (r *Render) GetPolygonsForGlyph(index int) ([]*Polygon, error)
- func (r *Render) MoveGlyph(index int, dx, dy float64) error
- func (r *Render) RenderToDC(dc *gg.Context, dx, dy, scale float64, height int)
- func (r *Render) SaveDXF(filename string, scale float64) error
- func (r *Render) SavePNG(filename string, width, height int) error
- func (t *Render) SaveSVG(fontName, filename string, scale float64) error
- func (r *Render) ToContext(width, height int) *gg.Context
- func (r *Render) ToImage(width, height int) image.Image
- type TextOpts
Constants ¶
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 ¶
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} )
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 ¶
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.
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.
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 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.
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 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) GetPolygonsForGlyph ¶ added in v0.0.13
func (*Render) RenderToDC ¶
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.