render

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package render is the drawing seam between the figure model and its output formats. A figure is laid out once, in pixels with y pointing down, and drawn through the Renderer interface; Raster and SVG implement it.

Index

Constants

This section is empty.

Variables

View Source
var Identity = Affine{A: 1, D: 1}

Identity is the identity transform.

Functions

This section is empty.

Types

type Affine

type Affine struct{ A, B, C, D, E, F float64 }

Affine is the transform x' = A*x + C*y + E, y' = B*x + D*y + F.

func Rotate

func Rotate(deg float64) Affine

Rotate returns a rotation by deg degrees counter-clockwise on screen (y pointing down), the sense in which matplotlib rotates text.

func Scale

func Scale(sx, sy float64) Affine

Scale returns a scaling about the origin.

func Translate

func Translate(dx, dy float64) Affine

Translate returns a translation.

func (Affine) Apply

func (m Affine) Apply(p Pt) Pt

Apply transforms a point.

func (Affine) Then

func (m Affine) Then(n Affine) Affine

Then returns the transform that applies m, then n.

type Cap

type Cap uint8

Cap is how an open stroke ends.

const (
	CapButt       Cap = iota // stops at the end point
	CapRound                 // a half disc
	CapProjecting            // carries on half a width
)

Caps, as matplotlib names them.

type Image

type Image struct {
	X, Y, W, H float64
	Pix        *image.RGBA
}

Image is a raster placed in the figure: Pix is drawn into the rectangle (X, Y, W, H) in pixels, with nearest-neighbour sampling.

type Join

type Join uint8

Join is how two segments of a stroke meet.

const (
	JoinMiter Join = iota
	JoinRound
	JoinBevel
)

Joins, as matplotlib names them.

type Op

type Op uint8

Op is a path operation.

const (
	MoveTo Op = iota
	LineTo
	QuadTo
	CubicTo
	Close
)

Path operations. A Seg's P holds 1 point for MoveTo and LineTo, 2 for QuadTo (control, end), 3 for CubicTo and none for Close.

type Path

type Path struct{ Segs []Seg }

Path is an outline in pixel coordinates.

func (*Path) Append

func (p *Path) Append(q *Path, m Affine)

Append adds every segment of q, transformed by m.

func (*Path) Bounds

func (p *Path) Bounds() (x0, y0, x1, y1 float64, ok bool)

Bounds returns the bounding box of the path's points (control points included), or ok false for an empty path.

func (*Path) Circle

func (p *Path) Circle(cx, cy, r float64)

Circle adds a closed circle of radius r about (cx, cy), as four cubics.

func (*Path) Close

func (p *Path) Close()

Close closes the current subpath.

func (*Path) CubicTo

func (p *Path) CubicTo(c1x, c1y, c2x, c2y, x, y float64)

CubicTo adds a cubic Bézier segment.

func (*Path) LineTo

func (p *Path) LineTo(x, y float64)

LineTo adds a straight segment.

func (*Path) MoveTo

func (p *Path) MoveTo(x, y float64)

MoveTo starts a new subpath.

func (*Path) QuadTo

func (p *Path) QuadTo(cx, cy, x, y float64)

QuadTo adds a quadratic Bézier segment.

func (*Path) Rect

func (p *Path) Rect(x, y, w, h float64)

Rect adds a closed rectangle.

type Pt

type Pt struct{ X, Y float64 }

Pt is a point in pixels.

type Raster

type Raster struct {
	Img *image.RGBA
	// contains filtered or unexported fields
}

Raster renders into an RGBA image. Shapes are rasterised to anti-aliased coverage by go-gfx/gfx/vector (which draws every cap, join and dash a matplotlib line can have) and composited through go-widgets/painter's PixelPainter as a coverage mask under its clip stack; images go through the painter's DrawImage.

func NewRaster

func NewRaster(w, h int) *Raster

NewRaster returns a Raster of w x h pixels filled with nothing (transparent); the figure paints its own background.

func (*Raster) BeginGroup

func (r *Raster) BeginGroup(string)

BeginGroup implements Renderer; a raster keeps no structure.

func (*Raster) DrawImage

func (r *Raster) DrawImage(im Image)

DrawImage implements Renderer: the rectangle is rounded to whole pixels and the painter scales the image into it, nearest neighbour.

func (*Raster) DrawPath

func (r *Raster) DrawPath(p *Path, s Style)

DrawPath implements Renderer.

func (*Raster) EndGroup

func (r *Raster) EndGroup()

EndGroup implements Renderer.

func (*Raster) PopClip

func (r *Raster) PopClip()

PopClip implements Renderer.

func (*Raster) PushClip

func (r *Raster) PushClip(x, y, w, h float64)

PushClip implements Renderer. The rectangle is rounded to whole pixels, as Agg's clip box is.

func (*Raster) Size

func (r *Raster) Size() (int, int)

Size implements Renderer.

type Renderer

type Renderer interface {
	// Size is the figure size in pixels.
	Size() (w, h int)
	// DrawPath paints a path.
	DrawPath(p *Path, s Style)
	// DrawImage paints a raster.
	DrawImage(im Image)
	// PushClip restricts drawing to a rectangle (intersected with any
	// enclosing clip) until the matching PopClip.
	PushClip(x, y, w, h float64)
	PopClip()
	// Group brackets a logical object (an artist) for formats that keep
	// structure; id is a stable identifier such as "line2d_3". Raster
	// formats ignore it.
	BeginGroup(id string)
	EndGroup()
}

A Renderer draws a figure. Coordinates are pixels, y down, origin at the top-left corner of the figure.

type SVG

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

SVG renders a figure as an SVG document. Coordinates are written in pixels at the figure's dpi, in a viewBox whose physical size is given in points, as matplotlib does; text arrives here already turned into glyph outlines, so the document needs no font.

func NewSVG

func NewSVG(w, h int, dpi float64) *SVG

NewSVG returns an SVG renderer for a w x h pixel figure at dpi.

func (*SVG) BeginGroup

func (s *SVG) BeginGroup(id string)

BeginGroup implements Renderer.

func (*SVG) DrawImage

func (s *SVG) DrawImage(im Image)

DrawImage implements Renderer. The image is embedded as a PNG at its own resolution and scaled by the viewer without smoothing.

func (*SVG) DrawPath

func (s *SVG) DrawPath(p *Path, st Style)

DrawPath implements Renderer.

func (*SVG) EndGroup

func (s *SVG) EndGroup()

EndGroup implements Renderer.

func (*SVG) PopClip

func (s *SVG) PopClip()

PopClip implements Renderer.

func (*SVG) PushClip

func (s *SVG) PushClip(x, y, w, h float64)

PushClip implements Renderer.

func (*SVG) Size

func (s *SVG) Size() (int, int)

Size implements Renderer.

func (*SVG) WriteTo

func (s *SVG) WriteTo(w io.Writer) (int64, error)

WriteTo writes the finished document.

type Seg

type Seg struct {
	Op Op
	P  [3]Pt
}

Seg is one path segment.

type Style

type Style struct {
	Fill    colors.RGBA
	EvenOdd bool // fill rule; nonzero otherwise

	Stroke colors.RGBA
	Width  float64   // pixels
	Cap    Cap       // ignored for closed subpaths
	Join   Join      //
	Dash   []float64 // on/off lengths in pixels; nil is solid
	Offset float64   // dash offset in pixels

	// Snap rounds the vertices of a path made only of horizontal and vertical
	// segments to the pixel grid so that its strokes are crisp, as
	// matplotlib's path snapping does for spines, ticks and grid lines.
	Snap bool
}

Style says how a path is painted. A zero Fill.A paints no fill; a zero Width or Stroke.A paints no stroke. The fill is painted first.

Jump to

Keyboard shortcuts

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