render

package
v0.0.0-...-a626a07 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package render produces 1-bit-friendly grayscale image.Image values suitable for the printer.PrintImage path. Every renderer takes a target width in pixels and returns an image whose width matches.

Index

Constants

This section is empty.

Variables

View Source
var AllFonts = []struct {
	Name string
	Face font.Face
}{
	{"Spleen 8", fonts.MiscSpleenMedium8},
	{"Spleen 12", fonts.MiscSpleenMedium12},
	{"Spleen 16", fonts.MiscSpleenMedium16},
	{"Spleen 24", fonts.MiscSpleenMedium24},
	{"Apple Helvetica 9", fonts.AppleHelveticaMedium9},
	{"Apple Helvetica 10", fonts.AppleHelveticaMedium10},
	{"Apple Helvetica 12", fonts.AppleHelveticaMedium12},
	{"Apple Helvetica 14", fonts.AppleHelveticaMedium14},
	{"Apple Helvetica 18", fonts.AppleHelveticaMedium18},
	{"Apple Helvetica 24", fonts.AppleHelveticaMedium24},
	{"Apple Times 9", fonts.AppleTimesMedium9},
	{"Apple Times 10", fonts.AppleTimesMedium10},
	{"Apple Times 12", fonts.AppleTimesMedium12},
	{"Apple Times 14", fonts.AppleTimesMedium14},
	{"Apple Times 18", fonts.AppleTimesMedium18},
	{"Apple Times 24", fonts.AppleTimesMedium24},
	{"Apple Geneva 9", fonts.AppleGenevaMedium9},
	{"Apple Geneva 10", fonts.AppleGenevaMedium10},
	{"Apple Geneva 12", fonts.AppleGenevaMedium12},
	{"Apple Geneva 14", fonts.AppleGenevaMedium14},
	{"Apple Geneva 18", fonts.AppleGenevaMedium18},
	{"Apple Geneva 20", fonts.AppleGenevaMedium20},
	{"Apple Geneva 24", fonts.AppleGenevaMedium24},
	{"Apple Monaco 9", fonts.AppleMonacoMedium9},
	{"Apple Monaco 12", fonts.AppleMonacoMedium12},
	{"Apple Chicago 12", fonts.AppleChicagoMedium12},
	{"Apple Courier 9", fonts.AppleCourierMedium9},
	{"Apple Courier 10", fonts.AppleCourierMedium10},
	{"Apple Courier 12", fonts.AppleCourierMedium12},
	{"Apple Courier 14", fonts.AppleCourierMedium14},
	{"Apple Courier 18", fonts.AppleCourierMedium18},
	{"Apple Courier 24", fonts.AppleCourierMedium24},
	{"Sun Gallant 19", fonts.SunGallantDemi19},
}

AllFonts is the catalog of vendored bitmap fonts. Order = display order in the font sampler.

View Source
var DefaultFamily = "helvetica"

DefaultFamily is consulted by pickFace for non-monospace text. Set via SetDefaultFamily.

View Source
var MonospaceFamily = "monaco"

MonospaceFamily is the fixed-width family used by callers that want code-style text (markdown code blocks). Set via SetMonospaceFamily.

Functions

func DecodeImage

func DecodeImage(r io.Reader) (image.Image, error)

DecodeImage decodes from any registered format.

func Dither

func Dither(src image.Image) image.Image

Dither converts a grayscale-ish image to 1-bit using Floyd–Steinberg error diffusion. Returned image is "1 = black" for direct packing.

Operates on a fresh grayscale buffer so the caller's image isn't mutated. Transparent source pixels become white, not black — many PNG sources (e.g. clip art) use a transparent background.

func Dragon

func Dragon(iter, targetWidth int) image.Image

Dragon returns a Heighway dragon curve at iter folding iterations, scaled to fit targetWidth. iter=12 gives 4096 segments in a ~64x32 bounding grid — the densest stage where folds are still legible.

func Face

func Face(family string, size float64) font.Face

Face returns the bitmap face nearest the requested size in the named family, rounding up. Unknown families fall back to helvetica.

func FamilyNames

func FamilyNames() string

FamilyNames returns the comma-separated list of known family names, useful for error messages.

func FitWidth

func FitWidth(src image.Image, targetW int) image.Image

FitWidth resizes src to exactly targetW pixels wide, preserving aspect ratio. Catmull-Rom — good for both photos and clean diagrams.

func FontSampler

func FontSampler(sample string, width int) (image.Image, error)

FontSampler renders one labeled line per vendored font with the given sample text.

func Gosper

func Gosper(order, targetWidth int) image.Image

Gosper returns the order-N flowsnake (Gosper) curve scaled to fit targetWidth. Order 4 = 2401 segments; the curve fills a hexagonal region with a self-similar boundary.

func Gradient

func Gradient(width, h int) image.Image

Gradient draws a left-to-right black-to-white gradient. Pre-Dither for printing.

func Hilbert

func Hilbert(order, targetWidth int) image.Image

Hilbert returns an order-N Hilbert space-filling curve drawn at the largest integer pixels-per-cell that fits within targetWidth. The canvas is square. Order 7 is the visually densest that still resolves individual line segments at 384 px wide.

func LoadImage

func LoadImage(path string) (image.Image, error)

LoadImage decodes any registered format (PNG/JPEG/GIF) from a file path.

func MonoText

func MonoText(s string, width int, ptSize float64) (image.Image, error)

MonoText renders with the active MonospaceFamily — used for code blocks and anything else that wants column-aligned text.

func PDF417

func PDF417(data string, width, errLevel int) (image.Image, error)

PDF417 builds a PDF417 barcode that fills `width` pixels and auto-sizes height based on payload length. iPhone Camera reads it.

errLevel: 0..8 per ISO/IEC 15438 §5.5 — higher = more redundant. Default 2 is plenty for clean thermal prints.

func QR

func QR(data string, width int) (image.Image, error)

QR builds a QR-code bitmap that fills `width` pixels at a pixel-perfect scale (no resampling). High error correction so the code survives a thumbprint or fading thermal paper.

func Rotate

func Rotate(src image.Image, deg int) image.Image

Rotate rotates by a multiple of 90 degrees (0, 90, 180, 270). Anything else returns src unchanged.

func Rotate90

func Rotate90(src image.Image) image.Image

Rotate90 rotates the source image 90° counter-clockwise. Useful for printing landscape photos on a portrait-format thermal head.

func Rotate180

func Rotate180(src image.Image) image.Image

Rotate180 rotates 180°. Same dimensions as input.

func Rotate270

func Rotate270(src image.Image) image.Image

Rotate270 rotates 270° counter-clockwise (= 90° clockwise).

func Rule

func Rule(width, h int) image.Image

Rule is a solid horizontal line, `h` pixels tall.

func RuleAutomaton

func RuleAutomaton(rule byte, width, rows int) image.Image

RuleAutomaton renders Wolfram's elementary 1-D cellular automaton for the given rule (0..255) as a width-by-rows strip, starting from a single black cell in the center of the top row.

func SetDefaultFamily

func SetDefaultFamily(name string) error

SetDefaultFamily switches the family used by Text(). Returns an error if the name isn't known.

func SetMonospaceFamily

func SetMonospaceFamily(name string) error

SetMonospaceFamily switches the monospace family. Returns an error if the name isn't known.

func Solid

func Solid(width, h int) image.Image

Solid is a fully black rectangle of the given dimensions.

func Sparkline

func Sparkline(values []float64, w, h int) image.Image

Sparkline draws a tiny line chart of values into a w×h grayscale image. Black on white. Each adjacent pair of values is connected by a 1-pixel straight line; values are normalized to fit between rows 1 and h-2 with a 1-pixel margin top and bottom.

If fewer than 2 values, returns a blank image. If all values are equal the line draws across the vertical center.

func Text

func Text(s string, width int, ptSize float64) (image.Image, error)

Text renders s into a bitmap of the given width with word wrapping at the actual face's advance widths. Output is pure 1-bit grayscale so it passes through Dither unchanged. Uses DefaultFamily.

func TextWithFace

func TextWithFace(s string, width int, face font.Face) (image.Image, error)

TextWithFace is Text with an explicit font.Face — useful when you want a specific bitmap (e.g. Apple Chicago for a system-7 vibe).

func Uniform

func Uniform(width, h int, level uint8) image.Image

Uniform fills a rectangle with one gray value (0=black, 255=white).

func VStack

func VStack(gap int, parts ...image.Image) image.Image

VStack composes equal-width images vertically with `gap` blank rows between each. Caller-supplied widths must match the first image's width.

Types

type Family

type Family struct {
	Name  string
	Sizes []float64
}

Family describes a vendored font family and its available point sizes.

func Families

func Families() []Family

Families returns the catalog of vendored families in alphabetical order, each with its available point sizes (also sorted ascending).

Jump to

Keyboard shortcuts

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