render

package
v0.4.19 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package render ports pxpipe's text→PNG renderer: it wraps text onto a fixed-pitch glyph grid (Spleen 5x8 or JetBrains Mono atlases) and encodes dense grayscale/RGB PNG pages sized for Anthropic's vision tiling.

Index

Constants

View Source
const (
	DefaultRenderFont = "spleen-5x8"

	// MaxHeightPx keeps pages under the API downscale bounds (long edge ≤1568,
	// ~1.15MP): 1568×728 bills exactly ⌈w/28⌉×⌈h/28⌉ patches with no resample.
	MaxHeightPx               = 728
	ReadableCharsPerImage     = 28080
	DenseContentCharsPerImage = 28080
	DenseContentCols          = 312
	AnthropicSlabCols         = DenseContentCols

	PadX              = 4
	PadY              = 4
	DefaultCellWBonus = 0
	DefaultCellHBonus = 0
	CellW             = 5 + DefaultCellWBonus
	CellH             = 8 + DefaultCellHBonus
	LinesPerImage     = (MaxHeightPx - 2*PadY) / CellH

	NLSentinel = "↵"

	NLSentinelLiteral = "⏎"

	GlyphEscapeOpen  = "[U+"
	GlyphEscapeClose = "]"

	SlotMarkUser      = "\x01"
	SlotMarkAssistant = "\x02"
)

Variables

View Source
var DenseRenderStyle = RenderStyle{AA: true}
View Source
var RolePalette = [][3]int{
	{150, 20, 20},
	{20, 40, 160},
}

Functions

func CountTextPages added in v0.2.4

func CountTextPages(text string, cols int, style RenderStyle, maxHeightPx int) int

CountTextPages returns the exact number of pages RenderTextToPngs would produce without rasterizing or encoding them.

func Dereflow

func Dereflow(reflowed string) string

func EncodeGrayPNG

func EncodeGrayPNG(pixels []byte, width, height int) ([]byte, error)

EncodeGrayPNG encodes a row-major single-channel buffer (len = w×h) as an 8-bit grayscale PNG (sample-selected None/Average filter, single IDAT).

func EncodeRGBPNG

func EncodeRGBPNG(pixels []byte, width, height int) ([]byte, error)

EncodeRGBPNG encodes an interleaved RGB buffer (len = w×h×3) as an 8-bit truecolor PNG (filter Average, single IDAT).

func EscapeMissingGlyphs

func EscapeMissingGlyphs(line string) string

EscapeMissingGlyphs replaces atlas-missing codepoints with "[U+HEX]" (always ranked against the default Spleen atlas, matching TS).

func ExpandTabsInLine

func ExpandTabsInLine(line string) string

ExpandTabsInLine expands \t to a visible → marker padded to the next 4-column stop (wide CJK counts 2 cols).

func FitsReflowedTextPages added in v0.2.4

func FitsReflowedTextPages(text string, cols int, style RenderStyle, maxHeightPx, maxPages int) bool

FitsReflowedTextPages is FitsTextPages for output already produced by Reflow.

func FitsTextPages added in v0.2.4

func FitsTextPages(text string, cols int, style RenderStyle, maxHeightPx, maxPages int) bool

FitsTextPages reports whether the exact render fits within maxPages.

func MaxCharsPerImage added in v0.3.0

func MaxCharsPerImage(cols int) int

MaxCharsPerImage returns the real char capacity of one page at cols. DenseContentCharsPerImage is only correct at DenseContentCols.

func MeasureContentCols

func MeasureContentCols(text string, maxCols, markerScale int, font string) int

MeasureContentCols returns the display width in cols of the widest line, capped at maxCols.

func MeasureLineCols

func MeasureLineCols(line string, markerScale int, font string) int

func MinifyForRender

func MinifyForRender(text string) string

MinifyForRender strips trailing whitespace per line and collapses 4+ consecutive newlines to 3.

func NeutralizeSentinel

func NeutralizeSentinel(text string) string

func PNGBase64SHA256 added in v0.4.10

func PNGBase64SHA256(images []*RenderedImage) [sha256.Size]byte

PNGBase64SHA256 hashes the concatenated base64 payloads. Complete image sequences returned by the render cache share the exact digest.

func Reflow

func Reflow(text string) (string, bool)

Reflow minifies, expands tabs, then joins hard newlines with the ↵ sentinel. Returns ok=false when the text already contains ↵ (caller renders raw).

func RenderCellHeight

func RenderCellHeight(style RenderStyle) int

func RenderCellWidth

func RenderCellWidth(style RenderStyle) int

func RoleSlotSegment

func RoleSlotSegment(tag, body, mark, attr string) string

RoleSlotSegment builds the width-preserving slot-string segment for one role-wrapped turn `<tag attr>\nbody\n</tag>`; marker runs must cover the attr too since coloring is positional.

func ShrinkColsToContent

func ShrinkColsToContent(text string, cols, markerScale int, font string) int

func SlotCopyBody

func SlotCopyBody(body string) string

func WrapLines

func WrapLines(text string, cols, markerScale int, font string) []string

func WriteRoleSlotSegment added in v0.4.14

func WriteRoleSlotSegment(out *strings.Builder, tag, body, mark, attr string)

WriteRoleSlotSegment appends RoleSlotSegment directly to out.

Types

type RenderOptions

type RenderOptions struct {
	Cols             int          `json:"cols,omitempty"`
	Shrink           *bool        `json:"shrink,omitempty"`
	Reflow           bool         `json:"reflow,omitempty"`
	MaxCharsPerImage int          `json:"maxCharsPerImage,omitempty"`
	Style            *RenderStyle `json:"style,omitempty"`
	MaxHeightPx      int          `json:"maxHeightPx,omitempty"`
}

RenderOptions mirrors pxpipe's renderTextToImages options. Zero values mean "use the production defaults" except Shrink, which defaults to true and is only disabled when explicitly set to false.

type RenderResult

type RenderResult struct {
	Pages        []RenderedTextImage
	DroppedChars int
	Pixels       int
}

func RenderTextToImages

func RenderTextToImages(text string, opts RenderOptions) (*RenderResult, error)

RenderTextToImages renders arbitrary text to dense PNG pages — the public entry mirroring pxpipe's renderTextToImages (library.ts).

type RenderStyle

type RenderStyle struct {
	Font          string `json:"font,omitempty"`
	Grid          bool   `json:"grid,omitempty"`
	GridCols      int    `json:"gridCols,omitempty"`
	MarkerScale   int    `json:"markerScale,omitempty"`
	MarkerRed     bool   `json:"markerRed,omitempty"`
	CellHBonus    int    `json:"cellHBonus,omitempty"`
	CellWBonus    int    `json:"cellWBonus,omitempty"`
	AA            bool   `json:"aa,omitempty"`
	ColorCycle    bool   `json:"colorCycle,omitempty"`
	ColorByRole   bool   `json:"colorByRole,omitempty"`
	InkDilate     int    `json:"inkDilate,omitempty"`
	InkDilateAxis string `json:"inkDilateAxis,omitempty"`
	// Invert defaults to true (black ink on white); nil = default.
	Invert *bool `json:"invert,omitempty"`
	// PaperGray defaults to 255 (pure white); nil = default.
	PaperGray *int `json:"paperGray,omitempty"`
}

type RenderedImage

type RenderedImage struct {
	PNG               []byte
	Width             int
	Height            int
	CharsRendered     int
	DroppedChars      int
	DroppedCodepoints map[rune]int
	// contains filtered or unexported fields
}

func RenderChunkToPng

func RenderChunkToPng(text string, cols int, style RenderStyle, maxHeightPx int, slotText *string) (*RenderedImage, error)

RenderChunkToPng renders text to a single PNG page (≤ maxHeightPx tall).

func RenderReflowedTextToPngs added in v0.2.4

func RenderReflowedTextToPngs(text string, cols int, style RenderStyle, maxHeightPx int) ([]*RenderedImage, error)

RenderReflowedTextToPngs renders output already produced by Reflow.

func RenderTextToPngs

func RenderTextToPngs(text string, cols int, style RenderStyle, maxHeightPx int, slotText *string) ([]*RenderedImage, error)

func RenderTextToPngsReflow

func RenderTextToPngsReflow(text string, cols int, style RenderStyle) ([]*RenderedImage, error)

func RenderTextToPngsWithCharLimit

func RenderTextToPngsWithCharLimit(text string, cols, maxCharsPerImage int, style RenderStyle, maxHeightPx int, slotText *string) ([]*RenderedImage, error)

func (*RenderedImage) AppendPNGBase64 added in v0.4.9

func (image *RenderedImage) AppendPNGBase64(dst []byte) []byte

func (*RenderedImage) AppendPNGBase64Deferred added in v0.4.13

func (image *RenderedImage) AppendPNGBase64Deferred(dst []byte) []byte

AppendPNGBase64Deferred avoids retaining an encoded copy until an image is used more than twice, which keeps one-shot multi-pass transforms lean.

func (*RenderedImage) WritePNGBase64 added in v0.4.9

func (image *RenderedImage) WritePNGBase64(dst io.Writer) error

WritePNGBase64 writes the image's base64 payload to dst.

type RenderedTextImage

type RenderedTextImage struct {
	PNG    []byte
	Width  int
	Height int
}

Jump to

Keyboard shortcuts

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