fig

package
v0.0.0-alpha.16 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 28 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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

func ParseControl(data []byte) *Control

ParseControl reads a control file. As in figlet, what it does not understand is skipped, so any data is a control file.

func (*Control) Map

func (c *Control) Map(r rune) rune

Map returns r as the control file maps it.

type Direction

type Direction uint8

Direction is which way text is printed.

const (
	// FontDirection is the font's own direction.
	FontDirection Direction = iota
	// LeftToRight prints text left to right.
	LeftToRight
	// RightToLeft prints text right to left.
	RightToLeft
)

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) At

func (f *Figure) At(x, y int) Cell

At returns the cell at x, y, which is blank outside the figure.

func (*Figure) Glyphs

func (f *Figure) Glyphs() []Glyph

Glyphs returns the figure's glyphs, in the order of the text. It must not be changed.

func (*Figure) Render

func (f *Figure) Render(p Paint) string

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.

func (*Figure) Seams

func (f *Figure) Seams() []Seam

Seams returns the cells where glyphs met. It must not be changed.

func (*Figure) Size

func (f *Figure) Size() (w, h int)

Size returns the figure's width and height, in cells.

func (*Figure) String

func (f *Figure) String() string

String returns the figure as plain text, with hardblanks as spaces, and each row's trailing spaces removed.

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

func Lazy(name, data string) *Font

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

func Parse(data []byte) (*Font, error)

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

func ParseFS(fsys fs.FS, name string) (*Font, error)

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) Err

func (f *Font) Err() error

Err reports why a font made by Lazy could not be parsed, or nil.

func (*Font) Figure

func (f *Font) Figure(text string, opts Options) *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.

func (*Font) Has

func (f *Font) Has(r rune) bool

Has reports whether the font has a glyph for r. Characters it lacks are shown as its glyph for code 0, if it has one, and otherwise not at all.

func (*Font) Info

func (f *Font) Info() Info

Info returns what the font's header says about it.

func (*Font) Name

func (f *Font) Name() string

Name returns the font's name: the one given to Lazy, the file name for ParseFS, and empty for Parse.

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

By sets what the gradient runs along.

func (GradientPaint) Shade

func (g GradientPaint) Shade(f *Figure, c Cell) uv.Style

Shade implements Paint.

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 Justify

type Justify uint8

Justify is where lines go within Options.Width.

const (
	// Auto is the left for left-to-right text and the right for
	// right-to-left text.
	Auto Justify = iota
	// Left puts lines at the left.
	Left
	// Center puts lines in the middle.
	Center
	// Right puts lines at the right.
	Right
)

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

func NewMorph(text string, opts ...anim.Option) Morph

NewMorph returns a Morph showing text, animating changes with opts, which default to those of anim.NewValue.

func (*Morph) Animating

func (m *Morph) Animating() bool

Animating reports whether a change is still animating.

func (*Morph) Current

func (m *Morph) Current() string

Current returns the text.

func (*Morph) Set

func (m *Morph) Set(text string) tea.Cmd

Set changes the text, starting the animation from the text before.

func (*Morph) Text

func (m *Morph) Text() TextNode

Text returns a node showing the text, with the glyphs of a change still animating.

func (*Morph) Update

func (m *Morph) Update(msg tea.Msg) tea.Cmd

Update advances the animation. It implements anim.Animation.

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 Order

type Order uint64

Order is the order glyphs start in, for TextNode.Spread.

const (
	// Forward starts glyphs from the first to the last.
	Forward Order = iota
	// Backward starts glyphs from the last to the first.
	Backward
	// Outward starts glyphs from the middle out to both ends.
	Outward
	// Inward starts glyphs from both ends in to the middle.
	Inward
)

func Random

func Random(seed uint32) Order

Random starts glyphs in an order that is the same for the same seed.

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

func Fill(inside color.Color, p Paint) Paint

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.

func Rainbow

func Rainbow() Paint

Rainbow returns TOIlet's rainbow filter: bright colors in diagonal bands.

func Solid

func Solid(c color.Color) Paint

Solid draws every character in one color.

type PaintFunc

type PaintFunc func(f *Figure, c Cell) uv.Style

PaintFunc adapts a function to a Paint.

func (PaintFunc) Shade

func (p PaintFunc) Shade(f *Figure, c Cell) uv.Style

Shade implements Paint.

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.

func (PalettePaint) Shade

func (p PalettePaint) Shade(f *Figure, c Cell) uv.Style

Shade implements Paint.

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 Side

type Side uint8

Side is whose character a Seam shows.

const (
	// Earlier is the character of the glyph earlier in the text.
	Earlier Side = iota + 1
	// Later is the character of the glyph later in the text.
	Later
	// Both is a character both glyphs had.
	Both
	// Neither is a new character the two made, such as "|" from "[]".
	Neither
)

type Stacking

type Stacking uint8

Stacking is which glyph is drawn on top where glyphs overlap.

const (
	// LaterOnTop draws the glyph later in the text on top, as figlet does.
	LaterOnTop Stacking = iota
	// EarlierOnTop draws the glyph earlier in the text on top.
	EarlierOnTop
)

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

func Text(s string) TextNode

Text returns a node showing s, in figlet's standard font unless given others with TextNode.Font.

func (TextNode) Animate

func (n TextNode) Animate(opts ...anim.Option) TextNode

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

func (n TextNode) Each(t float64, effects ...fx.Effect) TextNode

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

func (n TextNode) Enter(effects ...fx.Effect) TextNode

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

func (n TextNode) Fallback(c bloom.Child) TextNode

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

func (n TextNode) Font(chain ...*Font) TextNode

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

func (n TextNode) ID(id bloom.ID) TextNode

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

func (n TextNode) Key(k Keying) TextNode

Key sets how glyphs of the text before and after a change are matched, for TextNode.Animate and Morph.

func (TextNode) Layout

func (n TextNode) Layout(ctx bloom.Context) bloom.Child

Layout implements bloom.Child.

func (TextNode) Leave

func (n TextNode) Leave(effects ...fx.Effect) TextNode

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

func (n TextNode) Measure(avail bloom.Size) bloom.Size

Measure implements bloom.Measurer.

func (TextNode) MoveWith

func (n TextNode) MoveWith(effects ...fx.Effect) TextNode

MoveWith has TextNode.Animate move glyphs with effects, such as fx.Fly, rather than by gliding, as bloom.SlotNode.MoveWith does.

func (TextNode) On

func (n TextNode) On(handlers ...on.Handler) TextNode

On adds mouse handlers to the node, made by package on.

func (TextNode) OnGlyph

func (n TextNode) OnGlyph(fn func(g Glyph) []on.Handler) TextNode

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

func (n TextNode) Options(o Options) TextNode

Options sets how the text is laid out. With TextNode.Wrap, its Width is the node's width.

func (TextNode) Paint

func (n TextNode) Paint(p Paint) TextNode

Paint sets how the text is colored.

func (TextNode) Spread

func (n TextNode) Spread(s float64, order Order) TextNode

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

func (n TextNode) Wrap() TextNode

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.

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.

Jump to

Keyboard shortcuts

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