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 ¶
- func Render(data []byte, width, height int) (*image.RGBA, error)
- func RenderWithColor(data []byte, width, height int, c color.Color) (*image.RGBA, error)
- type Attrs
- type CircleElement
- type Document
- func (d *Document) Render(width, height int) *image.RGBA
- func (d *Document) RenderTo(dc *gg.Context, x, y, width, height float64)
- func (d *Document) RenderToWithColor(dc *gg.Context, x, y, width, height float64, c color.Color)
- func (d *Document) RenderWithColor(width, height int, c color.Color) *image.RGBA
- func (d *Document) String() string
- type Element
- type EllipseElement
- type GroupElement
- type LineElement
- type PathElement
- type PolygonElement
- type PolylineElement
- type RectElement
- type ViewBox
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
RenderToWithColor renders the document into an existing gg.Context with all non-"none" colors replaced by the given override color.
func (*Document) RenderWithColor ¶
RenderWithColor renders the document with all fill and stroke colors replaced by the given color. Colors set to "none" are preserved.
type Element ¶
type Element interface {
// contains filtered or unexported methods
}
Element is the interface implemented by all SVG element types.
type EllipseElement ¶
EllipseElement represents an SVG <ellipse> element.
type GroupElement ¶
GroupElement represents an SVG <g> element with children.
type LineElement ¶
LineElement represents an SVG <line> element.
type PathElement ¶
PathElement represents an SVG <path> element.
type PolygonElement ¶
PolygonElement represents an SVG <polygon> element.
type PolylineElement ¶
PolylineElement represents an SVG <polyline> element.
type RectElement ¶
RectElement represents an SVG <rect> element.