svg

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package svg parses a document from an SVG subset and rasterises it to a straight-alpha RGBA bitmap, built on the sibling github.com/go-gfx/gfx/vector and github.com/go-gfx/gfx/raster packages.

It is not a general-purpose SVG renderer: it understands a practical subset — a root <svg> with a viewBox (or width/height), nested <g> groups carrying transforms and fills, <path> outlines (commands M/L/H/V/C/S/Q/T/Z, absolute and relative; arcs are unsupported and cause the path to be skipped), <rect>, <circle>, nested <svg> islands with their own viewBox, and embedded raster <image> data URIs.

Fills and STROKES resolve black/white/currentColor/none, #rgb and #rrggbb literals, and url(#id) references to a <linearGradient> or <radialGradient> — in objectBoundingBox units (the SVG default, placed across the filled shape's own box) or userSpaceOnUse. A stroke honours stroke-width, carried through the transform as the length it is; caps and joins are round. <rect> honours rx/ry. An ink recolour and a transparent-or-opaque paper background are available.

A value this subset does not implement leaves the shape UNPAINTED rather than inheriting: silently flooding it with the ancestor's colour is what rendered every gradient-filled document as a flat square. Malformed shapes are skipped rather than failing the page.

Text is NOT rendered: <text> needs a font, which this package deliberately does not depend on.

Every <g> element is reported in Result.Groups together with the device-pixel bounding box of everything it drew. That is the generic seam a consumer uses to map a group attribute back to a screen region: filter Result.Groups by whatever attribute you care about — id, class, aria-*, a custom data-* marker — and read its Group.Bounds. The package itself assigns no meaning to any particular attribute.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	Attrs  map[string]string
	Bounds image.Rectangle
}

Group records one <g> element: its attributes (namespace-insensitive local names) and the device-pixel bounding box of everything drawn within it, including nested descendants. A group that drew nothing has an empty Bounds (see image.Rectangle.Empty). Groups are reported in document order.

type Options

type Options struct {
	Scale float64    // device pixels per SVG user-unit (pt). <=0 defaults to 2.0
	Ink   color.RGBA // colour used for the default black / currentColor fills
	Paper color.RGBA // page background fill (the document's fill="white" rect). If Paper.A==0, no background is painted (transparent).
}

Options controls rasterization.

type Result

type Result struct {
	Image  *raster.Image // straight-alpha RGBA surface, W*H pixels
	Groups []Group       // one entry per <g> element, in document order
}

Result is a rasterized SVG document.

func Rasterize

func Rasterize(doc string, opt Options) (*Result, error)

Rasterize parses ONE SVG document and renders it to a Result. It returns an error only for a document with no parseable <svg> root.

Jump to

Keyboard shortcuts

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