render

package
v0.0.0-...-52c3ae2 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package render implements the rendering engine for the browser. It converts layout boxes into visual output (PNG images).

Spec references: - CSS 2.1 §14 Colors and backgrounds: https://www.w3.org/TR/CSS21/colors.html - CSS 2.1 §15 Fonts: https://www.w3.org/TR/CSS21/fonts.html - CSS 2.1 §16 Text: https://www.w3.org/TR/CSS21/text.html - CSS 2.1 §8.5 Border properties: https://www.w3.org/TR/CSS21/box.html#border-properties - HTML5 §4.8.2 The img element: https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element

Implemented features: - Canvas-based raster rendering - Background colors (CSS 2.1 §14.2) - Border rendering with solid style (CSS 2.1 §8.5) - Text rendering with TrueType fonts (Go fonts) - Font styling: size, weight (bold), style (italic) per CSS 2.1 §15 - Text decoration: underline (CSS 2.1 §16.3.1) - Color parsing: named colors and hex colors (CSS 2.1 §4.3.6) - Image rendering: PNG, JPEG, GIF formats (HTML5 §4.8.2) - SVG rendering via custom parser (SVG 1.1 subset) - Data URL support for inline resources (RFC 2397) - Background images (CSS 2.1 §14.2.1) - PNG output via image/png

Not yet implemented (would require additional work): - Font family selection (CSS 2.1 §15.3) - uses Go fonts only - Text alignment within boxes (CSS 2.1 §16.2) - handled at layout level - Line height control (CSS 2.1 §10.8.1) - uses font metrics - Text decoration: overline, line-through (CSS 2.1 §16.3.1) - Border styles: dashed, dotted, etc. (CSS 2.1 §8.5.3) - solid only - Border radius (CSS3) - Box shadows (CSS3) - Gradients (CSS3) - Transforms and transitions (CSS3)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Canvas

type Canvas struct {
	Width      int
	Height     int
	Pixels     []color.RGBA
	ImageCache map[string]image.Image // Cache for loaded images
}

Canvas represents the rendering surface.

func NewCanvas

func NewCanvas(width, height int) *Canvas

NewCanvas creates a new canvas with the given dimensions.

func Render

func Render(root *layout.LayoutBox, width, height int) *Canvas

Render renders a layout tree to a canvas.

func (*Canvas) Clear

func (c *Canvas) Clear(bg color.RGBA)

Clear fills the canvas with a background color.

func (*Canvas) DrawImage

func (c *Canvas) DrawImage(img image.Image, x, y, width, height int)

DrawImage draws an image onto the canvas at the specified position. HTML5 §4.8.2 The img element

func (*Canvas) DrawRect

func (c *Canvas) DrawRect(x, y, width, height int, col color.RGBA, thickness int)

DrawRect draws a rectangle outline with the given color and thickness. CSS 2.1 §8.5 Border properties

func (*Canvas) DrawSVG

func (c *Canvas) DrawSVG(svgData []byte, x, y, width, height int) error

DrawSVG renders an SVG image onto the canvas at the specified position. Uses the svg package for parsing and rasterization per SVG 1.1 spec.

func (*Canvas) DrawStyledText

func (c *Canvas) DrawStyledText(text string, x, y int, col color.RGBA, style FontStyle)

DrawStyledText draws text with font styling at the given position. CSS 2.1 §15 Fonts and §16 Text CSS 2.1 §15.6 Font boldness: font-weight CSS 2.1 §15.7 Font style: font-style CSS 2.1 §16.3.1 Underlining, overlining, striking: text-decoration

Text is rendered directly at 1x resolution using TrueType fonts with built-in antialiasing.

func (*Canvas) DrawText

func (c *Canvas) DrawText(text string, x, y int, col color.RGBA)

DrawText draws text at the given position with the given color. CSS 2.1 §16 Text Uses default font style for backward compatibility with code that doesn't specify font properties.

func (*Canvas) FillRect

func (c *Canvas) FillRect(x, y, width, height int, col color.RGBA)

FillRect fills a rectangle with the given color. CSS 2.1 §14.2 The background

func (*Canvas) LoadImage

func (c *Canvas) LoadImage(path string) (image.Image, error)

LoadImage loads an image from an absolute file path or URL. Supports PNG, JPEG, and GIF formats. The path should be already resolved (absolute) before calling this method.

Uses dom.ResourceLoader for consistent resource fetching across the browser.

func (*Canvas) SavePNG

func (c *Canvas) SavePNG(filename string) error

SavePNG saves the canvas as a PNG file.

func (*Canvas) SetPixel

func (c *Canvas) SetPixel(x, y int, col color.RGBA)

SetPixel sets a pixel at the given coordinates.

func (*Canvas) ToImage

func (c *Canvas) ToImage() *image.RGBA

ToImage converts the canvas to an image.Image.

type FontStyle

type FontStyle = browserfont.Style

FontStyle represents text rendering options. CSS 2.1 §16 Text and §15 Fonts This is a local alias for browserfont.Style to maintain backward compatibility with existing render code.

Jump to

Keyboard shortcuts

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