raster

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package raster handles text and image rendering to monochrome bitmaps suitable for the P-Touch raster protocol.

Index

Constants

View Source
const (
	PrefixTabler    = "ti"
	PrefixBootstrap = "bi"
)

Icon providers with their SVG base URLs.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alignment

type Alignment int

Alignment controls horizontal text alignment.

const (
	AlignLeft Alignment = iota
	AlignCenter
	AlignRight
)

type Bitmap

type Bitmap struct {
	Width  int
	Height int
	Stride int    // bytes per row = (Width+7)/8
	Data   []byte // row-major packed pixel data
}

Bitmap is a 1-bit monochrome image. Pixels are packed MSB-first: bit 7 of byte 0 is the leftmost pixel. 1 = black, 0 = white.

func FromImage

func FromImage(img image.Image, threshold uint8) *Bitmap

FromImage converts any image.Image to a 1-bit Bitmap using the given luminance threshold (0–255). Pixels darker than threshold become black. Transparent pixels are composited onto a white background.

func NewBitmap

func NewBitmap(w, h int) *Bitmap

NewBitmap creates a white (all zeros) bitmap of the given dimensions.

func (*Bitmap) GetPixel

func (b *Bitmap) GetPixel(x, y int) bool

GetPixel returns true if the pixel at (x, y) is black.

func (*Bitmap) PadCenter

func (b *Bitmap) PadCenter(targetWidth int) *Bitmap

PadCenter returns a new bitmap of targetWidth, with the receiver's content centered horizontally. Used after transpose to align tape content on the printhead — narrower tapes don't start at pixel 0 of the printhead.

func (*Bitmap) SetPixel

func (b *Bitmap) SetPixel(x, y int, black bool)

SetPixel sets the pixel at (x, y) to black (true) or white (false).

func (*Bitmap) ToPNG

func (b *Bitmap) ToPNG(w io.Writer) error

ToPNG writes the bitmap as a black-and-white PNG.

func (*Bitmap) ToRasterRows

func (b *Bitmap) ToRasterRows(maxPixels int) [][]byte

ToRasterRows splits the bitmap into byte rows suitable for SendRasterLine. Each row is padded to maxPixels/8 bytes. Returns one []byte per bitmap row.

func (*Bitmap) Transpose

func (b *Bitmap) Transpose() *Bitmap

Transpose returns a transposed bitmap (W×H becomes H×W). Maps (x, y) → (y, x) so that:

  • each column of the original becomes a raster row (tape feed direction)
  • top of the original (y=0) maps to MSB of byte 0 (top edge of tape)

This produces correctly oriented, non-mirrored output on P-Touch printers.

type RenderResult

type RenderResult struct {
	Bitmap     *Bitmap  // transposed bitmap, ready for printing
	Preview    *Bitmap  // human-readable bitmap (before transpose), for preview
	RasterRows [][]byte // pre-split rows for SendRasterLine
	WidthPx    int      // label width in pixels (before transpose)
	HeightPx   int      // label height in pixels (before transpose = tape pixels)
}

RenderResult holds the output of a rendering operation.

func LoadImage

func LoadImage(path string, tapePixels, maxPixels, marginPx int) (*RenderResult, error)

LoadImage loads an image file, scales it to fit the tape height, converts to monochrome, rotates for tape orientation, and returns raster rows ready for printing.

Supported formats: PNG, JPEG, GIF. tapePixels is the printable height (from Tape.Pixels). maxPixels is the printhead width for row padding (from Model.MaxPixels). marginPx is the margin in pixels on each side (0 = edge-to-edge).

func RenderImage

func RenderImage(img image.Image, tapePixels, maxPixels, marginPx int) *RenderResult

RenderImage converts an already-decoded image to a print-ready RenderResult. The image is scaled to fit tapePixels height (maintaining aspect ratio), converted to 1-bit monochrome, and transposed for tape orientation. marginPx adds padding on all sides (0 = no margin).

func RenderText

func RenderText(cfg TextConfig, tapePixels, maxPixels int) (*RenderResult, error)

RenderText renders text lines to a 1-bit bitmap suitable for printing. tapePixels is the printable pixel height (from Tape.Pixels). maxPixels is the printhead width for row padding (from Model.MaxPixels).

type TextConfig

type TextConfig struct {
	Lines      []string  // text lines to render
	FontData   []byte    // TTF/OTF data (nil = use embedded Go font)
	FontSize   float64   // size in points (0 = auto-fit)
	Bold       bool      // use bold variant of embedded font
	Align      Alignment // horizontal alignment (default: center)
	DPI        int       // dots per inch (0 = 180)
	MaxWidthPx int       // fixed label width in pixels (0 = auto from text width)
}

TextConfig configures text rendering.

Jump to

Keyboard shortcuts

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