raster

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package raster draws BDF documents into images without a browser: the pages of fixed and flow views, stretches of scroll views and regions of sheets, for thumbnails and other server-side previews.

It runs the same instructions as the viewer's Canvas 2D renderer (docs/spec.md §7) with a software rasterizer: anti-aliased paths with nonzero and even-odd fills, strokes with joins, caps and dashes, clips, gradients and patterns, images (PNG, JPEG, GIF, BMP, WebP), text in the embedded fonts (WOFF2, TrueType, OpenType) and in the fonts of the system for fonts referred to by name, groups with their alpha and blend mode, soft masks and shadows. The result is close to what a browser draws but not identical: text is not hinted, shaped (no kerning, ligatures or Arabic joining) or laid out by a full bidirectional algorithm; SVG and AVIF images and FILTER are not drawn. Warnings list what was left out.

A document decides what it asks of a renderer, so the drawing has limits: of the objects drawn again (maxReusedInstructions), of the groups, soft masks, states and clips that are open at a time (maxLayers, maxStates, maxLive), of the outline of a stroke (strokePoints, strokeDashes), of the blur of a shadow (maxShadowBlur), of the pictures kept decoded (maxPicturePixels), of SVG images (svgDepth, svgNodes, svgBudget, svgLayers and the constants beside them) and of the gridlines of a sheet (maxGridLines). What passes a limit is left out with a warning, and the rest is drawn. A panic of a drawing is the error of Page and Region.

Index

Constants

View Source
const MaxPixels = 64 << 20

MaxPixels is the largest image Page and Region draw (a canvas takes 16 bytes a pixel while it is drawn).

Variables

View Source
var ErrNoPage = errors.New("raster: no such page")

ErrNoPage is returned for a page a view does not have.

View Source
var ErrTooLarge = errors.New("raster: the image would be too large")

ErrTooLarge is returned for an image of more than MaxPixels pixels.

Functions

func SheetSize

func SheetSize(v *bdf.View) (w, h float64)

SheetSize returns the size of a sheet in units, from its rows and columns.

Types

type Options

type Options struct {
	// FontFS holds fonts searched before FontDirs, for the text of fonts
	// referred to by name (HTML, Markdown and EPUB documents, and others
	// converted with system fonts) and for characters an embedded font
	// lacks.
	FontFS fs.FS
	// FontDirs are searched for fonts before the system font directories.
	FontDirs []string
	// NoSystemFonts restricts font lookup to FontFS and FontDirs.
	NoSystemFonts bool
	// Background fills the page behind its content (default white). Use
	// color.Transparent for a transparent page.
	Background color.Color
}

Options configures a Renderer.

type Renderer

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

Renderer draws the views of a document. It keeps what it decodes (objects, paths, images, fonts) for the next drawing, the images up to maxPicturePixels, past which those of the drawings before make room; it is not safe for concurrent use.

func New

func New(doc *bdf.Document, opts *Options) *Renderer

New returns a renderer of a document.

func (*Renderer) Page

func (r *Renderer) Page(v *bdf.View, page int, scale float64) (img *image.RGBA, err error)

Page draws a whole page of a fixed or flow view (or a strip of a scroll view) at scale device pixels per unit.

func (*Renderer) Region

func (r *Renderer) Region(v *bdf.View, page int, region bdf.Rect, w, h int) (img *image.RGBA, err error)

Region draws a rectangle of a view into a w × h image: of the page with index page in a fixed or flow view (in page units), of the continuous layout of a scroll view (its strips stacked, in units from the top), or of a sheet (page is ignored in the last two).

func (*Renderer) Warnings

func (r *Renderer) Warnings() []string

Warnings returns what could not be drawn as the viewer would, once each.

Jump to

Keyboard shortcuts

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