Documentation
¶
Overview ¶
Package fig renders text in FIGlet and TOIlet fonts: large letters made of ordinary characters, as bloom nodes or as plain strings.
fig.Text("bloom").Font(fonts.Slant(), fonts.Small()).
Paint(fig.Gradient(rose, apricot))
A Font lays text out into a Figure the way C figlet does, byte for byte: full width, fitted, smushed by the font's rules or universally, with hardblanks, right-to-left fonts, word wrapping, and justification. Lines of text can also be smushed vertically, which figlet does not do. A Figure remembers, for every cell, which letter of the text drew it, and for every cell where two letters overlap, both letters and the rule that merged them. That is what lets TextNode color letters, animate them one at a time with bloom's effects, and pull overlapping letters apart without losing strokes.
Fonts are in package fonts, each behind a function, so a program only carries the fonts it uses. Parse loads any other.
Index ¶
- Variables
- type Axis
- type Cell
- type CellKind
- type Control
- type Direction
- type Figure
- type Font
- type Glyph
- type GradientPaint
- type Info
- type Justify
- type Keying
- type Merge
- type Mode
- type Morph
- type Options
- type Order
- type Paint
- type PaintFunc
- type PalettePaint
- type Rules
- type Seam
- type Side
- type Stacking
- type TextNode
- func (n TextNode) Animate(opts ...anim.Option) TextNode
- func (n TextNode) Each(t float64, effects ...fx.Effect) TextNode
- func (n TextNode) Enter(effects ...fx.Effect) TextNode
- func (n TextNode) Fallback(c bloom.Child) TextNode
- func (n TextNode) Font(chain ...*Font) TextNode
- func (n TextNode) ID(id bloom.ID) TextNode
- func (n TextNode) Key(k Keying) TextNode
- func (n TextNode) Layout(ctx bloom.Context) bloom.Child
- func (n TextNode) Leave(effects ...fx.Effect) TextNode
- func (n TextNode) Measure(avail bloom.Size) bloom.Size
- func (n TextNode) MoveWith(effects ...fx.Effect) TextNode
- func (n TextNode) On(handlers ...on.Handler) TextNode
- func (n TextNode) OnGlyph(fn func(g Glyph) []on.Handler) TextNode
- func (n TextNode) Options(o Options) TextNode
- func (n TextNode) Paint(p Paint) TextNode
- func (n TextNode) Spread(s float64, order Order) TextNode
- func (n TextNode) Wrap() TextNode
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFont is returned for data that is not a FIGlet or TOIlet font. ErrNotFont = errors.New("fig: not a FIGlet 2 or TOIlet font") // ErrTooLarge is returned for a font far larger than any real one: taller // or with wider glyphs than fig lays out, with longer characters in a // cell, or with more distinct sub-characters than it can hold. ErrTooLarge = errors.New("fig: font is too large") )
Errors returned by Parse.
Functions ¶
This section is empty.
Types ¶
type Axis ¶
type Axis uint8
Axis is what a GradientPaint runs along.
const ( // ByColumn runs from the left column to the right. It is the default. ByColumn Axis = iota // ByGlyph colors each glyph in one color, from the first glyph to the // last. ByGlyph // ByRow runs from the top row to the bottom. ByRow // ByDiagonal runs from the top left corner to the bottom right, at 45 // degrees as the terminal draws it, where a cell is twice as tall as it // is wide. ByDiagonal )
type Cell ¶
type Cell struct {
X, Y int
// Content is what the cell shows: its sub-character, or a space.
Content string
Kind CellKind
// Glyph is the index of the glyph drawn on top, or -1. For a blank
// inside letters, it is the top glyph whose bounds hold it.
Glyph int
}
Cell is a cell of a Figure.
type CellKind ¶
type CellKind uint8
CellKind is what a cell of a Figure holds.
const ( // KindBlank is an empty cell. KindBlank CellKind = iota // KindStroke is a sub-character of one glyph. KindStroke // KindHardblank is a glyph's hardblank, a blank that took part in // smushing as a character. KindHardblank // KindSeam is a cell where two or more glyphs' sub-characters met. KindSeam // KindInside is a blank enclosed by strokes, such as the middle of an // "O", which cannot be reached from the figure's edge through blanks. KindInside )
type Control ¶
type Control struct {
// contains filtered or unexported fields
}
Control is a FIGlet control file (.flc): a mapping applied to text before it is laid out, such as from a keyboard layout to the letters of a Hebrew font. Give one to a layout with Options.Control.
Its "t" (translate) commands, number pairs, and "f" (freeze) commands are followed as figlet follows them. The commands that choose how input bytes are decoded ("b", "u", "h", "j", and "g") are ignored: text is always UTF-8.
func ParseControl ¶
ParseControl reads a control file. As in figlet, what it does not understand is skipped, so any data is a control file.
type Figure ¶
type Figure struct {
// contains filtered or unexported fields
}
Figure is text laid out in a font: a grid of cells, each holding a sub-character and the glyph that drew it, and a record of every cell where two glyphs met. It is immutable and safe for concurrent use.
func (*Figure) Glyphs ¶
Glyphs returns the figure's glyphs, in the order of the text. It must not be changed.
func (*Figure) Render ¶
Render returns the figure with ANSI styles from p, for Lip Gloss or any other program that prints strings. A nil p is the same as Figure.String.
type Font ¶
type Font struct {
// contains filtered or unexported fields
}
Font is a FIGlet (.flf) or TOIlet (.tlf) font. It is safe for concurrent use, and lays out each text and Options once, remembering the result.
func Lazy ¶
Lazy returns a font named name that is parsed from data, as Parse does, the first time it is used. It is how package fonts holds its fonts. If data is not a font, the font has no glyphs and Font.Err reports why.
func Parse ¶
Parse reads a font: a FIGlet .flf or TOIlet .tlf file, either of which may be zip compressed, as figlet allows, or gzip compressed. Line endings may be CRLF.
func ParseFS ¶
ParseFS reads the font at name in fsys, as Parse does. The font is named after the file, without its extension.
func Standard ¶
func Standard() *Font
Standard returns figlet's standard font, which a TextNode uses when given none. It is the same font as fonts.Standard.
func (*Font) Figure ¶
Figure lays text out with opts. A font remembers the figures it lays out, so the same text and options are laid out once.
type Glyph ¶
type Glyph struct {
// Rune is the character, as written.
Rune rune
// Index is the glyph's place among the figure's glyphs, in the order the
// text was written.
Index int
// Src is the byte offset of the character in the text.
Src int
// Line is the line of the figure the glyph is on, after wrapping.
Line int
// Rect is the cells the glyph's sub-characters cover, which overlap its
// neighbors' where they were smushed. It is empty for a space.
Rect image.Rectangle
}
Glyph is a character of the text, as laid out in a Figure.
type GradientPaint ¶
type GradientPaint struct {
// contains filtered or unexported fields
}
GradientPaint draws characters in colors blended from one stop to the next. Build one with Gradient.
func Gradient ¶
func Gradient(stops ...color.Color) GradientPaint
Gradient returns a paint blending from the first of stops to the last, across the columns unless changed with GradientPaint.By. Up to 16 stops are used.
func (GradientPaint) Blend ¶
func (g GradientPaint) Blend() GradientPaint
Blend colors the cells where glyphs met with the glyphs' colors mixed, rather than in the color of the glyph whose character the cell shows. It only applies ByGlyph, as along the other axes, a cell's color follows from where it is.
func (GradientPaint) By ¶
func (g GradientPaint) By(a Axis) GradientPaint
By sets what the gradient runs along.
type Info ¶
type Info struct {
// Height is the height of every glyph, in rows.
Height int
// Baseline is how many rows from the top capital letters rest on.
Baseline int
// Horizontal and Vertical are the layouts the font asks for: [Full],
// [Fitted], [Smushed] by its Rules, or [Universal] when it smushes with
// none.
Horizontal, Vertical Mode
// Rules are the smushing rules the font asks for.
Rules Rules
// RightToLeft reports whether the font prints right to left.
RightToLeft bool
// Comments are the header's comment lines, which hold the font's
// author, history, and license, if it has one.
Comments []string
// Glyphs is how many characters the font has.
Glyphs int
}
Info describes a font, from its header.
type Keying ¶
type Keying uint8
Keying is how the glyphs of a text before and after a change are matched, so that a glyph in both is the same glyph, moving if its place changed.
const ( // ByOccurrence matches the nth of each character to the nth of the same // character: changing "listen" to "silent" moves every letter. It suits // words. ByOccurrence Keying = iota // ByPosition matches the characters at the same place in the text, if // they are the same character: changing "12:59" to "13:00" keeps the // "1" and the ":", and replaces the rest. It suits numbers and clocks. ByPosition )
type Merge ¶
type Merge uint8
Merge is how a Seam's characters were merged.
const ( // MergeEqual is [RuleEqual] or [RuleVEqual]. MergeEqual Merge = iota + 1 // MergeUnderscore is [RuleUnderscore] or [RuleVUnderscore]. MergeUnderscore // MergeHierarchy is [RuleHierarchy] or [RuleVHierarchy]. MergeHierarchy // MergePair is [RulePair]. MergePair // MergeBigX is [RuleBigX]. MergeBigX // MergeHardblank is [RuleHardblank]. MergeHardblank // MergeLine is [RuleVLine]. MergeLine // MergeSuper is [RuleVSuper]. MergeSuper // MergeUniversal is universal smushing. MergeUniversal // MergeOverlap is glyphs pushed together by a negative // [Options.Tracking], drawn by [Options.Stacking]. MergeOverlap )
type Mode ¶
type Mode uint8
Mode is a layout mode, horizontal or vertical.
const ( // Default is the font's own layout. Default Mode = iota // Full puts glyphs side by side, or lines one under another, at their // full width or height. Full // Fitted moves glyphs, or lines, together until they touch. Fitted // Smushed moves them one step further where the touching characters can // be merged by the font's smushing rules, or by [Options.Rules]. With no // rules, it is Universal. Smushed // Universal moves them one step further, the later character // replacing the earlier where they meet. Universal )
type Morph ¶
type Morph struct {
// contains filtered or unexported fields
}
Morph is text that animates its glyphs as it changes: glyphs that leave the text are drawn where they were, going out with TextNode.Leave's effects, glyphs that join it come in with TextNode.Enter's, and glyphs in both move to their new places, gliding with TextNode.Animate. Keep a Morph in the model, pass it messages, and draw Morph.Text:
m.clock = fig.NewMorph("12:59", anim.Duration(400*time.Millisecond))
cmd := m.clock.Set("13:00")
// in View:
m.clock.Text().Font(fonts.Big()).Key(fig.ByPosition).ID("clock").Animate()
Setting new text while a change is still animating starts over from the text shown last.
func NewMorph ¶
NewMorph returns a Morph showing text, animating changes with opts, which default to those of anim.NewValue.
type Options ¶
type Options struct {
// Horizontal is how glyphs are put side by side. The zero value is the
// font's own layout.
Horizontal Mode
// Vertical is how lines of text are stacked. The zero value stacks them
// at full height, as figlet does, unless Strict is set, which uses the
// font's own vertical layout.
Vertical Mode
// Rules replaces the font's smushing rules, as figlet's -m does, for
// smushed layouts: its horizontal rules horizontally, and its vertical
// rules vertically. Zero keeps the font's.
Rules Rules
// Direction is which way text is printed. The zero value is the font's
// own direction.
Direction Direction
// Width wraps lines of text that would be wider than this many columns,
// at spaces, as figlet does, or anywhere in a word too long for a line.
// Zero never wraps.
Width int
// Paragraph joins lines of text that are not separated by a blank line,
// as figlet's -p does, so that Width alone decides where lines break.
Paragraph bool
// Justify is where each line goes within Width. The zero value is the
// left, or the right for a right-to-left font, as in figlet. It does
// nothing without a Width.
Justify Justify
// Tracking adds columns between glyphs, after they are smushed. A
// negative Tracking pushes glyphs further into each other than the
// font's rules allow, overlapping their strokes, with Stacking deciding
// which is drawn on top.
Tracking int
// Stacking is which glyph is drawn on top where glyphs overlap.
Stacking Stacking
// Strict follows the FIGfont standard where C figlet does not: glyphs
// narrower than two columns may smush, and the zero value of Vertical
// is the font's own vertical layout.
Strict bool
// Control maps the text before it is laid out, as figlet's -C option
// does. Nil leaves it as it is.
Control *Control
}
Options is how a Font lays text out. The zero value lays it out as C figlet does with no options. Options is comparable, and a font remembers the Figure for each text and Options it has laid out.
type Paint ¶
type Paint interface {
// Shade returns the style of c, a cell of f.
Shade(f *Figure, c Cell) uv.Style
}
Paint chooses how each cell of a Figure is drawn. Paints made by this package are comparable, so a figure remembers what it rendered with each; one that is not, such as a PaintFunc, is rendered each time.
func Fill ¶
Fill returns p with the blanks inside letters, such as the middle of an "O", filled with color inside, which makes outlined fonts look solid. p may be nil to fill without coloring characters.
func Metal ¶
func Metal() Paint
Metal returns TOIlet's metal filter: shades of blue and gray in slanted bands.
type PalettePaint ¶
type PalettePaint struct {
// contains filtered or unexported fields
}
PalettePaint draws each glyph in the next of a list of colors. Build one with Palette.
func Palette ¶
func Palette(colors ...color.Color) PalettePaint
Palette returns a paint drawing each glyph that shows something in the next of colors, starting over after the last. Up to 16 colors are used.
type Rules ¶
type Rules uint16
Rules are the smushing rules of the FIGfont standard, as the bits of a font's Full_Layout.
const ( // RuleEqual merges two equal characters into one. RuleEqual Rules = 1 << iota // RuleUnderscore replaces "_" with any of "|/\[]{}()<>". RuleUnderscore // RuleHierarchy replaces a character with one of a later class, of // "|", "/\", "[]", "{}", "()", and "<>". RuleHierarchy // RulePair merges opposite brackets, "[]", "{}", and "()" either way // round, into "|". RulePair // RuleBigX merges "/\" into "|", "\/" into "Y", and "><" into "X". RuleBigX // RuleHardblank merges two hardblanks into one. RuleHardblank )
The horizontal smushing rules.
const ( // RuleVEqual merges two equal characters, one over the other. RuleVEqual Rules = 256 << iota // RuleVUnderscore replaces "_" with any of "|/\[]{}()<>" over or under it. RuleVUnderscore // RuleVHierarchy replaces a character with one of a later class over or // under it, as RuleHierarchy does. RuleVHierarchy // RuleVLine merges "-" and "_", one over the other, into "=". RuleVLine // RuleVSuper merges "|" over "|", and lets lines overlap further while // they keep meeting so. RuleVSuper )
The vertical smushing rules.
type Seam ¶
type Seam struct {
X, Y int
// Earlier and Later are the glyphs, in the order of the text.
Earlier, Later int
// EarlierContent and LaterContent are what each glyph had there.
EarlierContent, LaterContent string
// Content is what the cell shows.
Content string
// Merge is how the two were merged.
Merge Merge
// Winner is whose character the cell shows.
Winner Side
}
Seam is a cell where two glyphs' sub-characters met, as glyphs were put side by side, or as lines were stacked. A cell where three met has two.
type TextNode ¶
type TextNode struct {
// contains filtered or unexported fields
}
TextNode shows text in a FIGlet font, as a bloom node. Build one with Text. It implements bloom.Child and bloom.Measurer.
A node whose glyphs do not move is drawn as one string, which bloom parses once and does not draw again while it is unchanged. Once glyphs move, with TextNode.Each, TextNode.Animate, TextNode.OnGlyph, or a Morph, each glyph is drawn as a transparent bloom.LayerNode of its own, over the ones before it, so that bloom's effects and layout animation work on single glyphs. Where glyphs were smushed together, each glyph's layer holds its own strokes, and the merged characters are shown only while the glyphs that met there are at rest: glyphs moving apart never leave holes.
func Text ¶
Text returns a node showing s, in figlet's standard font unless given others with TextNode.Font.
func (TextNode) Animate ¶
Animate glides each glyph to its new place when the text changes, with bloom's layout animation, as bloom.SlotNode.Animate does a slot. It needs an ID, and a retained bloom.Root. Which glyph is which is decided by TextNode.Key.
func (TextNode) Each ¶
Each draws every glyph with effects applied for its own progress, as bloom.Transition does a node, the glyphs starting one after another as t runs from 0 to 1, as TextNode.Spread says. At t of 1, the text is drawn whole again.
func (TextNode) Enter ¶
Enter sets the effects glyphs entering the text of a Morph are drawn with, for their progress from 0 to 1. The default is fx.Fade.
func (TextNode) Fallback ¶
Fallback sets what is shown when the text fits in none of the fonts. The default is the text itself, on one line, cut off with "…" if it does not fit.
func (TextNode) Font ¶
Font sets the fonts the text may be shown in. The first whose figure fits the space the node is given is used, and TextNode.Fallback when none fits, so a header can step down to smaller fonts as space runs short.
func (TextNode) ID ¶
ID sets the ID that bloom.Frame.Rect refers to the node by. It is also what the IDs of its glyphs' layers are made from, which TextNode.Animate needs: they are parts of id, as bloom.ID.Sub makes, named by the glyph's character and its index from TextNode.Key, such as id.Sub("A0") for the first A by ByOccurrence, so give no other node a part of id.
func (TextNode) Key ¶
Key sets how glyphs of the text before and after a change are matched, for TextNode.Animate and Morph.
func (TextNode) Layout ¶
Layout implements bloom.Child.
func (TextNode) Leave ¶
Leave sets the effects glyphs leaving the text of a Morph are drawn with, for their progress from 1 down to 0, so an effect that brings a node in from the top, such as fx.Slide{From: bloom.Top}, takes it out the top. The default is fx.Fade.
func (TextNode) Measure ¶
Measure implements bloom.Measurer.
func (TextNode) MoveWith ¶
MoveWith has TextNode.Animate move glyphs with effects, such as fx.Fly, rather than by gliding, as bloom.SlotNode.MoveWith does.
func (TextNode) OnGlyph ¶
OnGlyph adds the mouse handlers fn returns to each glyph's layer, such as to shake a letter while the pointer is over it. Where glyphs overlap, the one on top gets the event.
func (TextNode) Options ¶
Options sets how the text is laid out. With TextNode.Wrap, its Width is the node's width.
func (TextNode) Spread ¶
Spread sets how much of TextNode.Each's progress is spent starting the glyphs, from 0, all at once, to 1, each in an instant, as fx.Stagger's Spread does, and the order they start in. Entering and leaving glyphs of a Morph start the same way. The default is 0.5, in Forward order.
func (TextNode) Wrap ¶
Wrap wraps the text onto more lines, at the node's width, as Options.Width does, before stepping down to the next font. A font is still skipped if the wrapped text is too tall.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package fonts holds FIGlet and TOIlet fonts for package fig: figlet's standard fonts, TOIlet's, and the fonts people use most, found by how often public code on GitHub asks for them.
|
Package fonts holds FIGlet and TOIlet fonts for package fig: figlet's standard fonts, TOIlet's, and the fonts people use most, found by how often public code on GitHub asks for them. |
|
internal/gen
command
Command gen builds package fonts from manifest.json: it downloads each font from the commit the manifest pins, checks it against the manifest's SHA-256, adds an SPDX-License-Identifier comment line to the fonts whose license is known, and writes the fonts gzipped, with fonts.gen.go and LICENSES.md.
|
Command gen builds package fonts from manifest.json: it downloads each font from the commit the manifest pins, checks it against the manifest's SHA-256, adds an SPDX-License-Identifier comment line to the fonts whose license is known, and writes the fonts gzipped, with fonts.gen.go and LICENSES.md. |