Help us shape the future of Go! Take the 2026 Go Developer Survey

imagefont

package
v1.40.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package imagefont builds bitmap fonts for terminal image rendering. It does not install fonts, change terminal preferences, or write files.

Index

Constants

View Source
const (
	FirstCodepoint = '\ue000'
	LastCodepoint  = '\uf8ff'
	MaxGlyphs      = int(LastCodepoint-FirstCodepoint) + 1
	MaxFrameRows   = 32
)
View Source
const (
	FirstSupplementaryCodepoint = '\U000f0000'
	LastSupplementaryCodepoint  = FirstSupplementaryCodepoint + rune(MaxGlyphs) - 1
)

Variables

This section is empty.

Functions

func EncodePNG

func EncodePNG(ctx context.Context, encoder *png.Encoder, dst io.Writer, src image.Image) (err error)

EncodePNG checks cancellation while scanning pixels and writing compressed data, including highly compressible images that rarely flush their output. The PNG library still filters and compresses each scanline between callbacks; font bitmaps bound their width before calling this helper.

func PreservedGlyphCapacity added in v1.28.0

func PreservedGlyphCapacity(source PreserveOptions) (int, error)

PreservedGlyphCapacity reports the aggregate image-cell budget for this text face and geometry. Callers can reserve cells before adding optional previews. Encoding still validates individual frame dimensions and all source tables.

func ScaleBitmap

func ScaleBitmap(ctx context.Context, dst draw.Image, dr image.Rectangle, src image.Image, sr image.Rectangle) (err error)

ScaleBitmap scales pixels with Catmull-Rom interpolation, replacing dr in dst. Cancellation stops both filter passes before returning to the caller.

Types

type Font

type Font struct {
	Data     []byte
	Previews []Preview // In the same order as the input frames.
}

func Encode

func Encode(ctx context.Context, frames []Frame, options Options) (Font, error)

Encode creates a TrueType font containing lossless PNG glyphs at 32 and 64ppem. Tiles default to 16×32 pixels at 32ppem. The 750/-250 ascent/descent divide evenly at 16pt and 32pt. Custom tile geometry affects only image pixels; text advances and line metrics remain fixed to avoid changing the cell layout. Image fitting preserves aspect ratio and alpha, with transparent edge padding. An empty frame list produces the base monospace ASCII font for initial setup.

func EncodePreserving

func EncodePreserving(ctx context.Context, frames []Frame, options Options, source PreserveOptions) (Font, error)

EncodePreserving creates a private, uniquely named copy of an outline face, with extra bitmap glyphs. Source font files are never modified. Images occupy a supplementary private-use range, preserving existing BMP icons.

type Frame

type Frame struct {
	Image          image.Image
	Columns, Rows  int  // Zero selects 32 columns and an aspect-ratio-derived row count.
	CodepointStart rune // Zero allocates after the preceding frame, starting at U+E000.
}

Frame supplies one image and its immutable character range. Reusing its range for a different image would change the appearance of existing text.

type Options

type Options struct {
	Family, PostScript string
	// TileWidth and TileHeight are bitmap dimensions at 32ppem. Both zero selects
	// 16×32. Otherwise width must be 8..24 and height 16..48. These adapt image
	// tiles to terminal cell spacing without changing the font's text metrics.
	TileWidth, TileHeight int
}

Options identifies this font and its bitmap cell geometry. Callers must choose a unique identity whenever the contents change because platform font caches can retain older glyphs.

type PreserveOptions

type PreserveOptions struct {
	Tables           map[string][]byte
	SourcePostScript string
	// SourceName retains an exact bundled full face name when PostScript names
	// are shared by different installed files. Empty uses SourcePostScript.
	SourceName string
	Variations map[string]float64
	// FamilyClass is the native public typographic classification (0..15).
	FamilyClass           uint8
	PointSize             int
	CellWidth, CellHeight int
	Baseline              int
	// TextHeight is the original font's natural line height rounded up, before
	// profile spacing. Monaco needs this to preserve Terminal's special layout.
	TextHeight int
	// LegacyAliases restores old BMP gallery characters during an explicit
	// migration. Off by default so fallback-font private-use icons are retained.
	LegacyAliases bool
}

PreserveOptions supplies the installed text face to extend. Its original outlines, hinting, layout tables, style, character mappings and text metrics remain unchanged. Pixel geometry is measured from the user's Terminal tab.

type Preview

type Preview struct {
	Text                      string
	Columns, Rows             int
	WidthPixels, HeightPixels int // Dimensions of the 32ppem strike; the other strike doubles both.
	CodepointStart            rune
}

Jump to

Keyboard shortcuts

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