svgexport

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package svgexport implements a paint.Painter that records draw operations and serialises them as SVG XML. It fills the export- surface gap left after the opengl branch dropped Cairo — cairo_svg_surface used to give callers "draw with paint.Painter, save as .svg" for free; this package restores that capability in pure Go.

Usage:

p := svgexport.New(800, 600)
p.SetBrush1(paint.Color{R: 255, G: 0, B: 0, A: 255})
p.Rectangle(10, 10, 100, 50)
p.Fill()
p.WriteTo(file) // emits a complete <svg>…</svg> document

The painter implements every paint.Painter method but treats raster-only operations (DrawPixmap, DrawIcon, DrawGlyphs) as no-ops because SVG has no native equivalent for "paste an arbitrary bitmap from a glui glyph atlas". DrawText is rasterised into a single <text> element, which is the SVG-correct way to preserve text as selectable / accessible content rather than baked-in geometry.

The CTM (current transformation matrix) is folded into emitted coordinates at write time, so the output has no nested <g transform> wrappers and stays small.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SVGPainter

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

SVGPainter implements paint.Painter and emits SVG XML.

Concurrency: not safe for concurrent use. One painter per export.

func New

func New(width, height float64) *SVGPainter

New constructs a fresh SVGPainter with the given canvas size in SVG user units (points). The initial CTM is identity, brush is black, pen is 1pt black.

func (*SVGPainter) Arc

func (p *SVGPainter) Arc(xc, yc, radius, angle1, angle2 float64)

Arc emits an SVG elliptical-arc segment. SVG's "A" command takes (rx, ry, rotation, largeArcFlag, sweepFlag, x, y) so we translate from cairo-style (centre, radius, a0, a1) here. We always emit a preceding LineTo from the current point to the arc's start, which is what cairo also does implicitly.

func (*SVGPainter) ArcNegative

func (p *SVGPainter) ArcNegative(xc, yc, radius, angle1, angle2 float64)

func (*SVGPainter) Clip

func (p *SVGPainter) Clip()

Clip uses the current path as a clipPath and wraps subsequent emissions in a <g clip-path="url(#cN)"> element. The path buffer is reset; ClipPreserve keeps the path for follow-on Fill/Stroke.

func (*SVGPainter) ClipBounds

func (p *SVGPainter) ClipBounds() (float64, float64, float64, float64)

func (*SVGPainter) ClipBounds1

func (p *SVGPainter) ClipBounds1() geom.Rect

func (*SVGPainter) ClipPreserve

func (p *SVGPainter) ClipPreserve()

ClipPreserve installs the current path as a clip but keeps it on the path buffer so a subsequent Fill/Stroke renders the same geometry. Useful for "clip and stroke its border" patterns.

func (*SVGPainter) CurrentPoint

func (p *SVGPainter) CurrentPoint() (float64, float64)

func (*SVGPainter) CurrentState

func (p *SVGPainter) CurrentState() int

func (*SVGPainter) CurveTo

func (p *SVGPainter) CurveTo(x1, y1, x2, y2, x3, y3 float64)

func (*SVGPainter) DrawGlyph

func (p *SVGPainter) DrawGlyph(glyph *paint.Glyph)

func (*SVGPainter) DrawGlyphs

func (p *SVGPainter) DrawGlyphs(glyphs []paint.Glyph)

func (*SVGPainter) DrawIcon

func (p *SVGPainter) DrawIcon(ico paint.Icon, fSize float64, grayed bool)

func (*SVGPainter) DrawIcon1

func (p *SVGPainter) DrawIcon1(ico paint.Icon, x, y, fSize float64, grayed bool)

func (*SVGPainter) DrawPixmap

func (p *SVGPainter) DrawPixmap(pixmap paint.Pixmap)

func (*SVGPainter) DrawPixmap1

func (p *SVGPainter) DrawPixmap1(x, y float64, pixmap paint.Pixmap)

func (*SVGPainter) DrawPixmap2

func (p *SVGPainter) DrawPixmap2(x, y float64, pixmap paint.Pixmap, x0, y0 float64)

func (*SVGPainter) DrawPixmap5

func (p *SVGPainter) DrawPixmap5(x, y, w, h float64, pixmap paint.Pixmap)

func (*SVGPainter) DrawText

func (p *SVGPainter) DrawText(text string)

func (*SVGPainter) DrawText1

func (p *SVGPainter) DrawText1(x, y float64, text string)

func (*SVGPainter) Fill

func (p *SVGPainter) Fill()

func (*SVGPainter) FillPreserve

func (p *SVGPainter) FillPreserve()

func (*SVGPainter) Font

func (p *SVGPainter) Font() paint.Font

func (*SVGPainter) GetMatrix

func (p *SVGPainter) GetMatrix(m *geom.Mat3x2)

func (*SVGPainter) Line

func (p *SVGPainter) Line(x1, y1, x2, y2 float64)

func (*SVGPainter) LineTo

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

func (*SVGPainter) MoveTo

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

func (*SVGPainter) Paint

func (p *SVGPainter) Paint()

Paint fills the entire canvas with the current brush. SVG doesn't have a literal "paint everything" primitive; we emit a full-canvas rect that respects the active brush.

func (*SVGPainter) PaintWithAlpha

func (p *SVGPainter) PaintWithAlpha(alpha uint8)

func (*SVGPainter) Rectangle

func (p *SVGPainter) Rectangle(x, y, w, h float64)

func (*SVGPainter) Rectangle1

func (p *SVGPainter) Rectangle1(rect geom.Rect)

func (*SVGPainter) ResetClip

func (p *SVGPainter) ResetClip()

ResetClip can't be expressed in SVG without leaving the current element scope. Callers that need scoped clipping should bracket the clipped region in Save/Restore — Restore closes any clip <g> tags opened since the Save. This matches the PDF surface's approach and Cairo's "clip is graphics-state" model.

func (*SVGPainter) ResetMatrix

func (p *SVGPainter) ResetMatrix()

func (*SVGPainter) Restore

func (p *SVGPainter) Restore() int

func (*SVGPainter) RestoreTo

func (p *SVGPainter) RestoreTo(n int) bool

func (*SVGPainter) Rotate

func (p *SVGPainter) Rotate(radians float64)

func (*SVGPainter) Save

func (p *SVGPainter) Save() int

func (*SVGPainter) Scale

func (p *SVGPainter) Scale(sx, sy float64)

func (*SVGPainter) ScaledFont

func (p *SVGPainter) ScaledFont() paint.ScaledFont

func (*SVGPainter) SetBrush

func (p *SVGPainter) SetBrush(br paint.Brush)

func (*SVGPainter) SetBrush1

func (p *SVGPainter) SetBrush1(cr paint.Color)

func (*SVGPainter) SetFont

func (p *SVGPainter) SetFont(f paint.Font)

func (*SVGPainter) SetMatrix

func (p *SVGPainter) SetMatrix(m *geom.Mat3x2)

func (*SVGPainter) SetOperator

func (p *SVGPainter) SetOperator(op paint.Operator)

func (*SVGPainter) SetPen

func (p *SVGPainter) SetPen(pen paint.Pen)

func (*SVGPainter) SetPen1

func (p *SVGPainter) SetPen1(cr paint.Color, w float64)

func (*SVGPainter) String

func (p *SVGPainter) String() string

String returns the SVG document as a string. Convenience for tests.

func (*SVGPainter) Stroke

func (p *SVGPainter) Stroke()

func (*SVGPainter) StrokePreserve

func (p *SVGPainter) StrokePreserve()

func (*SVGPainter) Target

func (p *SVGPainter) Target() paint.Surface

func (*SVGPainter) Transform

func (p *SVGPainter) Transform(m *geom.Mat3x2)

func (*SVGPainter) Translate

func (p *SVGPainter) Translate(tx, ty float64)

func (*SVGPainter) WriteTo

func (p *SVGPainter) WriteTo(w io.Writer) (int64, error)

WriteTo serialises the recorded scene as a complete SVG document. The painter is not consumed — multiple calls produce identical output.

Any clip <g> tags still open at WriteTo time (i.e., the caller installed a clip but never Restored past it) are closed before </svg> so the output is always well-formed XML. Callers that want strict scope discipline should pair Save/Restore around clip regions.

Jump to

Keyboard shortcuts

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