svg

package
v1.26.4 Latest Latest
Warning

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

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

Documentation

Overview

Package svg converts SVG documents to TDTP packets and back.

Data model: each SVG element (and each text node inside mixed-content parents) becomes one row. Tree structure is encoded via parent_id + materialized path (zero-padded "/001/002") and per-parent order_idx.

Round-trip is lossy by design: byte-identical output is NOT a goal (attribute order, whitespace, namespace prefix choices may differ). Visual/DOM-level fidelity IS the goal. See pkg/svg/svg_test.go for round-trip coverage.

Index

Constants

View Source
const TableName = "svg_elements"

TableName is the TDTP table name used for SVG packets. Constant so that --from-svg / --to-svg / --inspect all agree on the name.

View Source
const TagTextNode = "_text_node"

TagTextNode is the framework-internal tag used for mixed-content text nodes (e.g. "Hello " in <text>Hello <tspan>world</tspan>!</text>). The leading underscore makes it visually distinct from any real SVG element name and unlikely to collide with namespaced elements.

Variables

View Source
var SVGDictionary = []packet.DictEntry{
	{Short: "@W3", Full: "http://www.w3.org/2000/svg"},
	{Short: "@xlink", Full: "http://www.w3.org/1999/xlink"},
	{Short: "@inks", Full: "http://www.inkscape.org/namespaces/inkscape"},
	{Short: "@sodi", Full: "http://sodipodi.sourceforge.net/DTD/sodipodi-0.0.dtd"},
	{Short: "@sketch", Full: "http://www.bohemiancoding.com/sketch/ns"},
}

SVGDictionary is the well-known set of XML namespace URI shortcuts that pkg/svg uses to compress the `namespace` column.

Tokens follow TDTP v1.4 dictionary semantics: whole-cell match only, `@` sigil, ASCII letters/digits/underscore after the sigil.

Keep this list short and stable — adding entries is a one-way commitment (existing TDTP-SVG files in the wild that use a token would re-parse with the old meaning if we ever changed the URI). Adding NEW tokens is safe; renaming or removing is not.

Functions

func BuildSchema

func BuildSchema() packet.Schema

BuildSchema returns the TDTP schema for SVG rows. ID is the key.

func ToPackets

func ToPackets(rows []SVGRow) ([]*packet.DataPacket, error)

ToPackets converts []SVGRow into TDTP DataPackets using the standard generator. Multiple packets are returned if the byte size exceeds the generator's max message size (default ~1.9 MB).

The SVG dictionary (well-known namespace URI shortcuts) is attached to the schema so that the resulting packet is self-describing — any v1.4 consumer can reverse the contraction without out-of-band knowledge.

func Write

func Write(w io.Writer, rows []SVGRow) error

Write serializes a slice of SVGRow back to an SVG document by building the tree from (ParentID, OrderIdx) and walking it depth-first. Rows may arrive in any order (e.g. reordered by storage or partitioned across multi-part TDTP packets).

Types

type SVGRow

type SVGRow struct {
	// --- Structural metadata (6 fields) ---
	// Tree is reconstructed from (ParentID, OrderIdx) alone.
	// Materialized path was dropped: a 50-byte derived string per row
	// is bloat — we already build the tree during parse/write anyway.
	ID          int64
	ParentID    int64  // 0 = root (the <svg> element itself)
	OrderIdx    int    // 1-based index among siblings
	Tag         string // "svg", "rect", "g", "_text_node", ...
	Namespace   string // XML namespace URI; "" = default SVG
	TextContent string // for _text_node, or text inside elements

	// --- Wide-sparse overflow (1 field) ---
	// JSON object with any attribute that does NOT have a dedicated
	// column below. Keys are the literal attribute names (including
	// namespace prefixes like "inkscape:label"). Empty if no overflow.
	AttrsJSON string

	// --- Wide attributes (17 most common in real SVG) ---
	IDAttr      string // <... id="...">
	Class       string
	Fill        string
	Stroke      string
	StrokeWidth string
	D           string // <path d="...">
	Transform   string
	Style       string
	Width       string
	Height      string
	X           string
	Y           string
	Cx          string
	Cy          string
	R           string
	ViewBox     string
	Href        string
}

SVGRow is the in-memory representation of one TDTP row. Field order in this struct DEFINES the column order in the schema — keep it stable, append-only.

func FromPacket

func FromPacket(pkt *packet.DataPacket) ([]SVGRow, error)

FromPacket reverses ToPackets for a single packet, returning the rows in the order they were stored. Caller is responsible for concatenating rows from multi-part packets in part-number order.

Handles both packet shapes: parser.Parse output (Data.Rows populated) and generator.GenerateReference output (rawRows fast-path, materialized on demand). This way the same code path works for both --to-svg reading from disk and unit tests staying in memory.

func Parse

func Parse(r io.Reader) ([]SVGRow, error)

Parse reads an SVG document and produces a flat slice of SVGRow. Tree structure is encoded via (parent_id, order_idx). Mixed-content text becomes synthetic rows with Tag=TagTextNode.

Streaming via xml.Decoder.Token(): memory use is O(tree depth), not O(file size), so this scales to large CAD-style SVGs.

Jump to

Keyboard shortcuts

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