svg

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package svg is refract's built-in, zero-dependency SVG backend.

It emits SVG using nothing but the standard library. That is its whole reason to exist: the leanest use case — "give me a chart as SVG on a server" — should pull in no rendering engine, no font stack, and nothing young. Importing refract and this backend yields a stdlib-only dependency graph and a static binary.

Text

Text is emitted as <text> elements, so the viewer or browser shapes it. For layout, this backend measures with internal/fontmetrics: exact advances when a font file is supplied via WithFont, and a built-in generic-sans table otherwise. Geometry is therefore identical to any other backend given identical metrics, but the metrics themselves differ slightly between backends, so the layout does too — the accepted imprecision in refract's "one model, identical output" promise. The bounds are measured in backend/gg/parity_test.go and explained in docs/adr/0003.

Determinism

Attribute order and number formatting are fixed, and nothing here iterates a map or reads a clock, so the same chart produces byte-identical SVG on every run on a given machine.

Across architectures it does not, and no amount of care here would change that: Go may contract a*b+c into a fused multiply-add, arm64 does and amd64 does not, and a float32 coordinate can consequently differ in its last bit. Golden-file tests therefore compare with internal/svgdiff — everything but the numbers exactly, coordinates to within a hundredth of a pixel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func File

func File(path string, opts ...Option) ir.Target

File returns a Target that writes an SVG document to the named file. The file is created on Open and closed on Close.

func Writer

func Writer(w io.Writer, opts ...Option) ir.Target

Writer returns a Target that writes an SVG document to w.

Types

type Option

type Option func(*options)

Option configures an SVG target.

func Pretty

func Pretty() Option

Pretty writes one element per line. Off by default: the compact form is smaller, and golden files are diffed by tooling rather than read.

func WithFont

func WithFont(ttf []byte) Option

WithFont supplies a TrueType or OpenType file whose metrics are used for layout. Without it the backend measures with a built-in generic-sans table, which is close but not exact.

The font is not embedded in the output; only its metrics are read. Pair it with WithFontFamily so the viewer picks the same face.

func WithFontFamily

func WithFontFamily(family string) Option

WithFontFamily sets the font-family attribute written into the SVG. The default is "sans-serif".

Jump to

Keyboard shortcuts

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