icon/svg.go
Functions
func FromSVG
func FromSVG(name string, viewBox float32, svgPathData string) IconData {
ops, err := parseSVGToOps(svgPathData)
if err != nil {
panic("icon.FromSVG: " + name + ": " + err.Error())
}
return IconData{
Name: name,
ViewBox: viewBox,
Ops: ops,
SVGData: svgPathData, // fill rendering for complex shapes
StrokeWidth: 0.8, // fallback stroke
}
}
func FromSVGStroke
FromSVGStroke creates an IconData for stroke-based SVG icons.
These are rendered via stroke (DrawLine) not fill, matching expui outline style.
func FromSVGStroke(name string, viewBox float32, svgPathData string) IconData {
ops, err := parseSVGToOps(svgPathData)
if err != nil {
panic("icon.FromSVGStroke: " + name + ": " + err.Error())
}
return IconData{
Name: name,
ViewBox: viewBox,
Ops: ops,
StrokeWidth: 0.8, // expui stroke icons
}
}
func FromSVGXML
FromSVGXML creates an IconData from full SVG XML data.
The SVG is rendered via gg/svg.RenderWithColor into a bitmap at the
target size, matching JetBrains icon rendering pipeline.
This handles all SVG elements: path, circle, rect, with proper
fill, stroke, fill-rule, stroke-linecap, etc.
func FromSVGXML(name string, svgXML []byte) IconData {
return IconData{
Name: name,
ViewBox: 16, // default; actual viewBox from SVG XML
SVGXML: svgXML,
}
}
func TryFromSVG
TryFromSVG creates an IconData from SVG path data, returning an error
instead of panicking if the data is invalid. This is useful for
user-supplied or runtime-loaded icon data.
func TryFromSVG(name string, viewBox float32, svgPathData string) (IconData, error) {
ops, err := parseSVGToOps(svgPathData)
if err != nil {
return IconData{}, err
}
return IconData{
Name: name,
ViewBox: viewBox,
Ops: ops,
}, nil
}
FromSVG creates an IconData from an SVG path data string.
The viewBox specifies the SVG coordinate space side length (e.g., 16 for
a 16x16 viewBox, 24 for Material Design's 24x24). SVG path commands
are parsed using [gg.ParseSVGPath] and converted to [PathOp] operations.
FromSVG panics if the SVG path data is invalid. This is intentional because
icon definitions are typically package-level variables initialized at startup;
a malformed path indicates a programming error that should fail fast.
Example:
var MyIcon = icon.FromSVG("my_icon", 16, "M8 2L14 8L8 14L2 8Z")