path

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package path provides SVG path data parsing and stringification, ported from SVGO's lib/path.js. Based on https://www.w3.org/TR/SVG11/paths.html#PathDataBNF.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMatrixToPathData

func ApplyMatrixToPathData(pathData []PathDataItem, matrix []float64)

ApplyMatrixToPathData applies a transformation matrix to path data in-place. Ported from SVGO's applyMatrixToPathData() in plugins/applyTransforms.js.

func ApplyTransformsVisitor

func ApplyTransformsVisitor(root *svgast.Root, params *ApplyTransformsParams, transformed map[*svgast.Element][]PathDataItem) *svgast.Visitor

ApplyTransformsVisitor returns a visitor that applies transforms to path data. Ported from SVGO's applyTransforms plugin in plugins/applyTransforms.js.

Transformed path data is recorded in transformed rather than serialized back into the d attribute, mirroring the pathJS array SVGO caches on the node. Callers must hand that data to convertPathData: a detour through the d attribute would collapse consecutive movetos before their coordinates are resolved and would lose any non-finite coordinate the transform produced.

func Intersects

func Intersects(path1, path2 []PathDataItem) bool

Intersects checks if two paths have an intersection by checking convex hulls collision using Gilbert-Johnson-Keerthi distance algorithm. Ported from SVGO's intersects() in plugins/_path.js.

func JS2Path

func JS2Path(elem *svgast.Element, data []PathDataItem, floatPrecision int, noSpaceAfterFlags bool)

JS2Path converts path data items back to a d attribute string and sets it on the element. Also removes consecutive moveto commands (last one wins). Ported from SVGO's js2path() in plugins/_path.js.

func StringifyPathData

func StringifyPathData(opts *StringifyPathDataOptions) string

StringifyPathData converts parsed path data back to an optimized SVG path string.

Types

type ApplyTransformsParams

type ApplyTransformsParams struct {
	TransformPrecision     int
	ApplyTransformsStroked bool
}

ApplyTransformsParams controls the applyTransforms behavior.

type ConvertPathDataParams

type ConvertPathDataParams struct {
	ApplyTransforms        bool
	ApplyTransformsStroked bool
	MakeArcs               *MakeArcsParams
	StraightCurves         bool
	ConvertToQ             bool
	LineShorthands         bool
	ConvertToZ             bool
	CurveSmoothShorthands  bool
	FloatPrecision         int  // use -1 for false
	FloatPrecisionEnabled  bool // true when floatPrecision is not false
	TransformPrecision     int
	SmartArcRounding       bool
	RemoveUseless          bool
	CollapseRepeated       bool
	UtilizeAbsolute        bool
	LeadingZero            bool
	NegativeExtraSpace     bool
	NoSpaceAfterFlags      bool
	ForceAbsolutePath      bool
}

ConvertPathDataParams holds all the parameters for convertPathData.

type ExtendedPathItem

type ExtendedPathItem struct {
	Command byte
	Args    []float64
	Base    []float64 // absolute coordinates of the previous point
	Coords  []float64 // absolute coordinates after this command
	Sdata   []float64 // saved curve data for arc conversion
}

ExtendedPathItem extends PathDataItem with absolute coordinate tracking used during optimization. Matches the JS pattern of attaching .base and .coords to path items.

func ConvertToMixed

func ConvertToMixed(path []ExtendedPathItem, params *ConvertPathDataParams) []ExtendedPathItem

ConvertToMixed writes data in shortest form using absolute or relative coordinates. Ported from SVGO's convertToMixed() in plugins/convertPathData.js.

func ConvertToRelative

func ConvertToRelative(pathData []ExtendedPathItem) []ExtendedPathItem

ConvertToRelative converts absolute path data coordinates to relative. Modifies items in place, attaching Base and Coords for later use. Ported from SVGO's convertToRelative() in plugins/convertPathData.js.

func Filters

func Filters(
	pathItems []ExtendedPathItem,
	params *ConvertPathDataParams,
	isSafeToUseZ bool,
	maybeHasStrokeAndLinecap bool,
	hasMarkerMid bool,
) []ExtendedPathItem

Filters is the main filter loop for path optimization. Ported from SVGO's filters() in plugins/convertPathData.js.

type MakeArcsParams

type MakeArcsParams struct {
	Threshold float64
	Tolerance float64
}

MakeArcsParams controls arc conversion thresholds.

type PathDataItem

type PathDataItem struct {
	Command byte
	Args    []float64
}

PathDataItem represents a single path command with its arguments.

func ParsePathData

func ParsePathData(s string) []PathDataItem

ParsePathData parses an SVG path data string into a slice of PathDataItems.

func Path2JS

func Path2JS(elem *svgast.Element) []PathDataItem

Path2JS converts a path element's d attribute to a slice of PathDataItems. Ported from SVGO's path2js() in plugins/_path.js. First moveto is always made absolute.

type StringifyPathDataOptions

type StringifyPathDataOptions struct {
	PathData               []PathDataItem
	Precision              int // -1 means no rounding
	DisableSpaceAfterFlags bool
}

StringifyPathDataOptions controls path data stringification.

Jump to

Keyboard shortcuts

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