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 ¶
- type Renderer
- type Writer
- func (w *Writer) Attr(name, value string)
- func (w *Writer) AttrFloat(name string, v float64)
- func (w *Writer) Bytes() []byte
- func (w *Writer) CloseAttr()
- func (w *Writer) CloseTagOpen()
- func (w *Writer) EndTag(name string)
- func (w *Writer) Indent(n int)
- func (w *Writer) JoinAttr(name string, parts []string)
- func (w *Writer) Newline()
- func (w *Writer) OpenAttr(name string)
- func (w *Writer) OpenTag(name string)
- func (w *Writer) Raw(s string)
- func (w *Writer) SelfClose()
- func (w *Writer) String() string
- func (w *Writer) Text(s string)
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 (*Renderer) Render ¶
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 ¶
Attr writes ` name="value"` with the value XML-escaped for attribute context (handles quotes, ampersands, etc.).
func (*Writer) AttrFloat ¶
AttrFloat writes ` name="3.142"` with the value pinned to RenderPrecision via FormatFloat.
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) JoinAttr ¶
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 ¶
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 ¶
OpenTag writes `<name`. Caller follows with Attr / AttrFloat then CloseTag or SelfClose.
func (*Writer) Raw ¶
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.