svg

package
v0.43.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package svg provides an SVG renderer built on top of the gg 2D graphics library.

It parses a subset of SVG XML sufficient for rendering icon-style SVGs (as used by JetBrains IntelliJ, Material Design, etc.) and renders them to RGBA images using github.com/gogpu/gg.Context.

Supported SVG Elements

  • <svg> with viewBox, width, height
  • <path> with d attribute (full SVG path command set)
  • <circle> cx, cy, r
  • <rect> x, y, width, height, rx, ry
  • <ellipse> cx, cy, rx, ry
  • <line> x1, y1, x2, y2
  • <polygon> points
  • <polyline> points
  • <g> grouping with inherited attributes and transform

Supported Attributes

  • fill, fill-rule, fill-opacity
  • stroke, stroke-width, stroke-linecap, stroke-linejoin, stroke-opacity
  • transform (translate, rotate, scale, matrix)
  • opacity

Usage

img, err := svg.Render(svgBytes, 64, 64)

// With color override (theming):
img, err := svg.RenderWithColor(svgBytes, 64, 64, color.White)

// Parse once, render many times:
doc, err := svg.Parse(svgBytes)
img1 := doc.Render(16, 16)
img2 := doc.Render(32, 32)
img3 := doc.RenderWithColor(24, 24, themeColor)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(data []byte, width, height int) (*image.RGBA, error)

Render parses SVG XML data and renders it to an RGBA image at the specified size. The SVG viewBox is scaled to fit the target width and height.

func RenderWithColor

func RenderWithColor(data []byte, width, height int, c color.Color) (*image.RGBA, error)

RenderWithColor parses SVG XML data and renders it with all fill and stroke colors replaced by the given color. This is useful for theming icon SVGs. Colors set to "none" are preserved (transparent areas stay transparent).

Types

type Attrs

type Attrs struct {
	Fill          string  // "#hex", "none", "rgb(...)", named color, ""
	FillRule      string  // "evenodd", "nonzero", ""
	FillOpacity   float64 // 0-1, default 1
	Stroke        string  // "#hex", "none", "rgb(...)", named color, ""
	StrokeWidth   float64 // default 1
	StrokeCap     string  // "round", "square", "butt", ""
	StrokeJoin    string  // "round", "bevel", "miter", ""
	StrokeOpacity float64 // 0-1, default 1
	Opacity       float64 // element-level opacity, default 1
	Transform     string  // raw transform string
	ClipRule      string  // "evenodd", "nonzero", ""
}

Attrs holds common SVG presentation attributes shared by all elements.

type CircleElement

type CircleElement struct {
	Attrs  Attrs
	CX, CY float64
	R      float64
}

CircleElement represents an SVG <circle> element.

type Document

type Document struct {
	// ViewBox defines the SVG coordinate system (minX, minY, width, height).
	ViewBox ViewBox

	// Width and Height are the explicit width/height from the SVG root element.
	// If zero, ViewBox dimensions are used.
	Width, Height float64

	// RootFill is the fill attribute from the <svg> root element.
	// In SVG, presentation attributes on <svg> are inherited by children.
	// Common in expui icons: fill="none" on root.
	RootFill string

	// Elements contains the top-level SVG elements.
	Elements []Element
}

Document represents a parsed SVG document. It can be rendered multiple times at different sizes, making it suitable for caching parsed SVG icons.

func Parse

func Parse(data []byte) (*Document, error)

Parse parses SVG XML data into a reusable Document. The Document can be rendered multiple times at different sizes.

Only the SVG subset needed for icon rendering is supported. Unsupported elements are silently skipped.

func (*Document) Render

func (d *Document) Render(width, height int) *image.RGBA

Render renders the document to an RGBA image at the specified size. The SVG viewBox is scaled to fit the target dimensions.

func (*Document) RenderTo

func (d *Document) RenderTo(dc *gg.Context, x, y, width, height float64)

RenderTo renders the document into an existing gg.Context at the specified position and size. The SVG viewBox is scaled to fit (x, y, width, height).

func (*Document) RenderToWithColor

func (d *Document) RenderToWithColor(dc *gg.Context, x, y, width, height float64, c color.Color)

RenderToWithColor renders the document into an existing gg.Context with all non-"none" colors replaced by the given override color.

func (*Document) RenderWithColor

func (d *Document) RenderWithColor(width, height int, c color.Color) *image.RGBA

RenderWithColor renders the document with all fill and stroke colors replaced by the given color. Colors set to "none" are preserved.

func (*Document) String

func (d *Document) String() string

String returns a short description of the document for debugging.

type Element

type Element interface {
	// contains filtered or unexported methods
}

Element is the interface implemented by all SVG element types.

type EllipseElement

type EllipseElement struct {
	Attrs  Attrs
	CX, CY float64
	RX, RY float64
}

EllipseElement represents an SVG <ellipse> element.

type GroupElement

type GroupElement struct {
	Attrs    Attrs
	Children []Element
}

GroupElement represents an SVG <g> element with children.

type LineElement

type LineElement struct {
	Attrs          Attrs
	X1, Y1, X2, Y2 float64
}

LineElement represents an SVG <line> element.

type PathElement

type PathElement struct {
	Attrs Attrs
	D     string // SVG path data string
}

PathElement represents an SVG <path> element.

type PolygonElement

type PolygonElement struct {
	Attrs  Attrs
	Points []float64 // alternating x, y values
}

PolygonElement represents an SVG <polygon> element.

type PolylineElement

type PolylineElement struct {
	Attrs  Attrs
	Points []float64 // alternating x, y values
}

PolylineElement represents an SVG <polyline> element.

type RectElement

type RectElement struct {
	Attrs      Attrs
	X, Y, W, H float64
	RX, RY     float64 // corner radii
}

RectElement represents an SVG <rect> element.

type ViewBox

type ViewBox struct {
	MinX, MinY, Width, Height float64
}

ViewBox represents the SVG viewBox attribute.

Jump to

Keyboard shortcuts

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