d2svgimport

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MPL-2.0 Imports: 25 Imported by: 4 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package d2svgimport imports the bounded, network-free SVG subset used by D2 assets and MathJax into d2scene.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParsePath

func ParsePath(ctx context.Context, source, data string, limits PathLimits) ([]d2scene.PathCommand, error)

ParsePath converts the complete common SVG path grammar to absolute typed d2scene commands. source must already be safe to include in an error (asset resolvers should pass their redacted display name, never a credentialed URL).

Types

type ImportOptions

type ImportOptions struct {
	CurrentColor *color.NRGBA
}

ImportOptions selects caller-owned presentation context that is outside the imported SVG subtree. A nil CurrentColor preserves SVG's opaque-black initial color. ImportNodeWithOptions copies the pointed-to value before parsing and never retains the pointer.

type Limits

type Limits struct {
	MaxBytes int
	// MaxDecodedRasterBytes caps decoded pixels separately from SVG source bytes.
	// Zero uses MaxBytes for callers that supply only the original limits.
	MaxDecodedRasterBytes int64
	MaxDepth              int
	MaxElements           int
	MaxAttributes         int
	MaxAttributeBytes     int
	MaxPathCommands       int
	MaxTransformFunctions int
	MaxUseDepth           int
	MaxResources          int
}

Limits bound one SVG import. MaxElements and MaxPathCommands each cap both source and emitted scene totals. MaxResources bounds declared IDs plus embedded assets, and separately limits expanded use instances. All limits except MaxDecodedRasterBytes must be positive.

type Metrics

type Metrics struct {
	SourceBytes          int
	ParsedElements       int
	ParsedAttributes     int
	ParsedAttributeBytes int
	ParsedPathCommands   int
	ParsedTransformFuncs int
	DeclaredResources    int
	ExpandedUseInstances int
	EmittedElements      int
	EmittedPathCommands  int
	EmbeddedRasterAssets int
	EmbeddedRasterBytes  int
	DecodedRasterBytes   int64
}

Metrics reports bounded source and emitted-scene work for one successful import. Callers retaining multiple imported subscenes use these counters to enforce a shared document budget in addition to Limits' per-import ceilings.

type PathLimits

type PathLimits struct {
	MaxBytes    int
	MaxCommands int
}

PathLimits are caller-selected hard limits for one SVG path data string. MaxCommands counts parsed source command groups, including groups such as an identical-endpoint arc that SVG defines to emit no segment. Both values must be positive.

type Result

type Result struct {
	Root              *d2scene.Node
	ViewBox           d2scene.Box
	Width             float64
	Height            float64
	Aspect            d2scene.AspectRatio
	ViewportTransform d2scene.Matrix
	// Assets owns every immutable raster resource referenced by Image
	// primitives below Root. Keys are stable content IDs; callers embedding the
	// subtree must merge these assets into their document without mutation.
	Assets  map[d2scene.AssetID]d2scene.Asset
	Metrics Metrics
}

Result is an owned, network-free SVG subtree plus its viewport metadata. Root remains in SVG user coordinates. ViewportTransform maps ViewBox into the intrinsic Width x Height viewport using Aspect. A caller embedding the subtree must clip final painting to [0,Width] x [0,Height]; in particular, AspectSlice intentionally maps content outside that viewport.

func ImportNode

func ImportNode(ctx context.Context, source string, data []byte, limits Limits) (*Result, error)

ImportNode imports a strict SVG subset. It never reads files, resolves a URL, or performs network I/O. On every error it returns a nil result. Supported icon content includes vector geometry, positioned text in bundled fonts, local linear and radial gradients, clips, masks, Gaussian blur, and embedded static PNG, JPEG, GIF and WebP images. Nested viewports require a viewBox and finite absolute dimensions. The importer validates simple class stylesheets and non-painting editor metadata before emitting the scene. Frozen MathJax uses its own text outline and ex measurement rules.

Example (LocalClipPath)
result, err := ImportNode(context.Background(), "clip.svg", []byte(`<svg width="2" height="1"><clipPath id="c"><rect width="1" height="1"/></clipPath><rect width="2" height="1" clip-path="url(#c)"/></svg>`), generousImportLimits())
fmt.Println(err, result.Root.Children[0].Clip != nil)
Output:
<nil> true

func ImportNodeWithOptions

func ImportNodeWithOptions(ctx context.Context, source string, data []byte, limits Limits, options ImportOptions) (*Result, error)

ImportNodeWithOptions imports the same strict subset as ImportNode with an explicit initial currentColor. An SVG color declaration still takes normal cascade precedence over this caller-supplied value.

Jump to

Keyboard shortcuts

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