glyph

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: BSD-3-Clause Imports: 8 Imported by: 2 modules, 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.

README

glyph

A bitmap font at several cell sizes, its strikes; the software raster that draws a cell grid with one; and what a drawer in another runtime (a Java view, a shader) paints from: a strike as one texture, and frames encoded for it. Every number below is little-endian.

The font file

Parse reads it where it lies and refuses any other.

offset  size             field
0       4                magic "TGF1"
4       2                n, the count of strikes, at least 1
6       2                l, the length of the name in bytes
8       4                g, the count of glyphs
12      l                the name, UTF-8, then zero bytes to the next multiple of 4
..      4*g              the runes, code points strictly ascending
then for each strike, strictly ascending by height:
        2                width in pixels, 1 to 32
        2                height in pixels, at least 1
        g*height*stride  the glyphs in rune order, a glyph's rows top first:
                         stride = ceil(width/8) bytes a row, bit 7 of the first
                         byte the leftmost column, the bits past the width zero

Every strike holds every rune; a glyph a strike does not draw is blank there.

The texture

NewTexture(strike).Bytes(), a strike as a cell drawer uploads it:

offset     size   field
0          4      w, a cell's width in pixels
4          4      h, its height
8          4      s, the count of slots
12         s*w*h  the mask, a slot below the last: a slot's rows top first, a byte
                  a pixel, 255 where the glyph is set and 0 elsewhere
12+s*w*h   4*s    each slot's rune

A glyph with a set bit takes the next slot, in rune order; a blank one has none and is painted as background. Each strike so has slots of its own.

Frames

A terminal.Frames encodes for the drawer with one of two encoders. Colors are terminal.CellColors: reverse swapped, dim halved, a palette index resolved.

Texture.Frame, the cells changed since the frame the drawer holds:

offset  size  field
0       4     cols
4       4     rows
8       4     c, the count of records
12      16*c  a record a cell, four words: its index, row-major; its glyph's
              slot, 0xffffffff for background only; its foreground and its
              background as 0xffRRGGBB

Raster.Frame, the picture itself, for a drawer that cannot paint cells:

offset  size             field
0       4                cols
4       4                rows
8       cols*w*rows*h*4  RGBA, rows top first, w by h the strike's cell

Picking a strike

Font.Cell(density, dp) is the pixels of a cell dp tall on a screen of density pixels a dp, whole ones at the face's aspect, and the strike that draws it: the largest no taller than the cell, so a glyph is stretched up and never down in a font whose strikes share one aspect. The largest strike is the face's own size, the dp of a host that keeps no zoom. Zoom.Scale steps a zoom and stops at the smallest strike. When a zoom crosses a strike, the host makes that strike's Texture or Raster and hands its encoder to Frames.SetEncoder: the next frame is whole, in the new slots.

Documentation

Overview

Package glyph is a bitmap font of several cell sizes, the software rasterizer that draws a cell grid with one of them (the reference drawer of any graphical host and the fallback of every one) and what a drawer in another runtime paints from: a cell size as one texture, and frames encoded for it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Atlas

type Atlas struct {
	Width, Height int
	// contains filtered or unexported fields
}

Atlas is one strike of a Font. A glyph's rows are whole bytes each, the leftmost column the first one's highest bit, so a cell is at most 32 pixels wide.

func (*Atlas) Aspect

func (a *Atlas) Aspect() float64

Aspect is the cell's width over its height: what a host drawing with this strike reports as its cell aspect

func (*Atlas) Has

func (a *Atlas) Has(r rune) bool

Has is whether the font holds a glyph for r, blank or not

type Font

type Font struct {
	Name string
	// contains filtered or unexported fields
}

Font is one face at several cell sizes, its strikes, read where a font file's bytes lie: the README lays the file out

func Parse

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

Parse reads a font file, as tool/font-atlas writes it. The font keeps data and reads its glyphs from it, so the caller leaves it as it is.

func (*Font) Cell

func (f *Font) Cell(density, dp float64) (strike *Atlas, width, height int)

Cell is the pixels a cell dp tall takes on a screen of density pixels to a dp, whole ones at the face's aspect and never under its smallest strike, and the strike that draws it: the largest no taller than the cell

func (*Font) Len

func (f *Font) Len() int

Len is how many glyphs the font holds, each of them in every strike

func (*Font) Runes

func (f *Font) Runes() []rune

Runes lists the glyphs the font holds in code point order

func (*Font) Strike

func (f *Font) Strike(height int) *Atlas

Strike is the largest strike no taller than height pixels, the smallest when none is

func (*Font) Strikes

func (f *Font) Strikes() []*Atlas

Strikes is the font's strikes, smallest first

type Probe

type Probe struct {
	// contains filtered or unexported fields
}

Probe is a frame of every glyph in the font under each color rule the raster applies, with the raster's pixels for it: the oracle a drawer is checked against, same frame, same pixels

func NewProbe

func NewProbe(tex *Texture, cols int) *Probe

NewProbe lays the probe out cols cells wide

func (*Probe) Cells

func (p *Probe) Cells() (cells []terminal.Cell, cols, rows int)

Cells is the probe's frame and its size, to Flush to a Surface

func (*Probe) Frame

func (p *Probe) Frame() []byte

Frame is the probe as a whole cell frame, as the texture's Frame encodes one

func (*Probe) Pixels

func (p *Probe) Pixels() []byte

Pixels is the frame as Raster draws it, RGBA rows top first

type Raster

type Raster struct {
	// contains filtered or unexported fields
}

Raster draws a cell grid into an RGBA framebuffer with a strike. It is a terminal.Surface: Flush takes a whole frame and draws the cells that changed since the last one.

func NewRaster

func NewRaster(a *Atlas) *Raster

func (*Raster) Bounds

func (r *Raster) Bounds() (width, height int)

Bounds is the framebuffer size in pixels

func (*Raster) ColorMode

func (r *Raster) ColorMode() terminal.ColorMode

ColorMode is truecolor: the raster paints RGB

func (*Raster) Flush

func (r *Raster) Flush(cells []terminal.Cell, width, height int)

Flush draws a frame of width by height cells, row-major; a frame short of its own size is dropped, as a terminal drops one

func (*Raster) Frame

func (r *Raster) Frame(dst []byte, cells []terminal.Cell, cols, rows int, changed []uint64) []byte

Frame is the terminal.FrameEncoder of a drawer that shows the raster's pixels: little-endian words cols and rows, then Pix. It draws the cells that differ from its own last frame, every one for a nil changed.

func (*Raster) Pix

func (r *Raster) Pix() []byte

Pix is the framebuffer: RGBA rows of Bounds' width, top row first

func (*Raster) Sync

func (r *Raster) Sync()

Sync forgets the frame drawn, so the next one is drawn whole

type Texture

type Texture struct {
	// contains filtered or unexported fields
}

Texture is a strike as a cell drawer takes it: every glyph with a set bit as one slot of a mask, one byte a pixel, and the slot of each rune. A blank glyph has no slot and is painted as background.

func NewTexture

func NewTexture(a *Atlas) *Texture

NewTexture encodes a as little-endian words and bytes: cell width, height and slot count, each slot's rows one below the last with 255 where the glyph is set, then each slot's rune as a word

func (*Texture) Atlas

func (t *Texture) Atlas() *Atlas

Atlas is the strike the texture encodes

func (*Texture) Bytes

func (t *Texture) Bytes() []byte

Bytes is the encoded texture, as NewTexture lays it out

func (*Texture) Frame

func (t *Texture) Frame(dst []byte, cells []terminal.Cell, cols, rows int, changed []uint64) []byte

Frame is the terminal.FrameEncoder of the texture's drawer: a terminal.CellFrame whose ids are slots

func (*Texture) Slot

func (t *Texture) Slot(r rune) int32

Slot is a rune's slot, -1 for a glyph painted as background only

type Zoom

type Zoom struct{ Least, Most, Step float64 }

Zoom is the range a host keeps a cell's height to as its player zooms, in dp: Least to Most in steps of Step

func (Zoom) Scale

func (z Zoom) Scale(f *Font, density, dp, by float64) float64

Scale is a cell dp tall scaled by a zoom's factor: a step of the range, within it and never under f's smallest strike at the density; a range of no step keeps the cell

Jump to

Keyboard shortcuts

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