svg

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package svg implements render.Renderer for SVG output. The renderer walks a SceneDoc top-down, emitting per-mark elements (rect, line, area=path, circle, line=rule) inside the canonical <svg> skeleton from design/07-rendering.md.

Output goes through encoding/xml for attribute escaping; coordinates pin to RenderPrecision (3 decimals) via the writer's FormatFloat helper. Goldens compare byte-equal against committed testdata/svgs/*.svg files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Renderer

type Renderer struct{}

Renderer is the SVG implementation of render.Renderer. Stateless; safe to share across goroutines.

func New

func New() *Renderer

New returns the SVG renderer.

func (*Renderer) MimeType

func (r *Renderer) MimeType() string

MimeType implements render.Renderer.

func (*Renderer) Render

func (r *Renderer) Render(doc *scene.SceneDoc, opts render.RenderOpts) ([]byte, error)

Render implements render.Renderer. Walks the SceneDoc top-down:

<svg viewBox="0 0 W H" [width="..." height="..."]>
  <style>:root{ --prism-color-axis: ...; ... }
          .prism-axis-label { ... } ...</style>
  <defs>...</defs>
  <g class="prism-scene" data-scene-id="...">
    <text class="prism-title">...</text>
    <g class="prism-axes">...</g>
    <g class="prism-plot"><g class="prism-layer">...</g></g>
  </g>
</svg>

`width` and `height` attrs are emitted only when the caller asks for them via RenderOpts.Width / RenderOpts.Height. Default (zero-zero) output is viewBox-only so the SVG fills its container and CSS can resize it from outside — including inside shadow-DOM consumers like the <prism-chart> web component, which cannot otherwise override fixed attributes.

Coordinates are pinned to RenderPrecision; attributes are XML-escaped via the writer helpers; layer-by-layer dispatch hands each mark to the per-geom renderer in marks.go.

type Writer

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

Writer wraps bytes.Buffer with precision-pinned float formatting + escaped attribute writing. Used by every per-mark and per-axis helper so output stays byte-stable across Go versions.

We don't use xml.Encoder directly because its Token-based API is inconvenient for the heavily-nested SVG structure we emit; we reach for the underlying buf and use xml.EscapeText / our own FormatFloat to handle the safety bits.

func NewWriter

func NewWriter() *Writer

NewWriter returns a Writer backed by an internal bytes.Buffer. Call Bytes() at the end to get the final output.

func (*Writer) Attr

func (w *Writer) Attr(name, value string)

Attr writes ` name="value"` with the value XML-escaped for attribute context (handles quotes, ampersands, etc.).

func (*Writer) AttrFloat

func (w *Writer) AttrFloat(name string, v float64)

AttrFloat writes ` name="3.142"` with the value pinned to RenderPrecision via FormatFloat.

func (*Writer) Bytes

func (w *Writer) Bytes() []byte

Bytes returns the accumulated output.

func (*Writer) CloseAttr

func (w *Writer) CloseAttr()

CloseAttr writes `"` to terminate the OpenAttr value.

func (*Writer) CloseTagOpen

func (w *Writer) CloseTagOpen()

CloseTagOpen writes `>` (closes the opening tag; children follow).

func (*Writer) EndTag

func (w *Writer) EndTag(name string)

EndTag writes `</name>`.

func (*Writer) Indent

func (w *Writer) Indent(n int)

Indent writes n spaces. Used for indenting nested elements in goldens.

func (*Writer) JoinAttr

func (w *Writer) JoinAttr(name string, parts []string)

JoinAttr writes a space-separated list inside an attribute value. Convenience for `class="a b c"` and `stroke-dasharray="5 5"`.

func (*Writer) Newline

func (w *Writer) Newline()

Newline writes a single '\n'. Used between top-level sections for human-readable goldens.

func (*Writer) OpenAttr

func (w *Writer) OpenAttr(name string)

OpenAttr writes ` name="`. Caller follows with Raw calls (which must be pre-escaped) and a final CloseAttr. Used for SVG path d= and polyline points= attributes where building the value via repeated Attr calls would require an intermediate string buffer.

func (*Writer) OpenTag

func (w *Writer) OpenTag(name string)

OpenTag writes `<name`. Caller follows with Attr / AttrFloat then CloseTag or SelfClose.

func (*Writer) Raw

func (w *Writer) Raw(s string)

Raw writes raw bytes (no escaping). Use for control characters like `<`, `>`, `/` and structural skeletons.

func (*Writer) SelfClose

func (w *Writer) SelfClose()

SelfClose writes ` />` to end a void element.

func (*Writer) String

func (w *Writer) String() string

String returns the accumulated output as a string.

func (*Writer) Text

func (w *Writer) Text(s string)

Text writes XML-escaped text content (anything between tags).

Jump to

Keyboard shortcuts

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