svg

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package svg parses SVG markup and adapts it to Paper components.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSVGHasZeroDimensions is returned when SVG rasterization cannot infer dimensions.
	ErrSVGHasZeroDimensions = svgraster.ErrSVGHasZeroDimensions
	// ErrSVGTooLarge is returned when rasterization would exceed the pixel limit.
	ErrSVGTooLarge = svgraster.ErrSVGTooLarge
	// ErrNilDocument is returned when a method requiring an SVG document is called on nil.
	ErrNilDocument = errors.New("svg: nil document")
	// ErrNoSVGElement is returned when parsed XML does not contain an <svg> element.
	ErrNoSVGElement = errors.New("svg: no <svg> element found")
	// ErrEmptyDocument is returned when the SVG input has no XML element.
	ErrEmptyDocument = errors.New("svg: empty document")
)

Functions

func Rasterize

func Rasterize(data []byte, widthMM, heightMM float64) ([]byte, int, int, error)

Rasterize converts SVG bytes to PNG bytes at the requested millimetre dimensions.

func RasterizeWithLimit

func RasterizeWithLimit(data []byte, widthMM, heightMM float64, maxPixels int64) ([]byte, int, int, error)

RasterizeWithLimit converts SVG bytes to PNG bytes with a custom pixel cap.

Types

type AspectAlign

type AspectAlign int

AspectAlign controls how the viewBox is positioned within the viewport.

const (
	// AlignXMidYMid centers the viewBox on both axes.
	AlignXMidYMid AspectAlign = iota
	// AlignXMinYMin aligns to the minimum x and minimum y edge.
	AlignXMinYMin
	// AlignXMidYMin centers x and aligns to the minimum y edge.
	AlignXMidYMin
	// AlignXMaxYMin aligns to the maximum x and minimum y edge.
	AlignXMaxYMin
	// AlignXMinYMid aligns to the minimum x edge and centers y.
	AlignXMinYMid
	// AlignXMaxYMid aligns to the maximum x edge and centers y.
	AlignXMaxYMid
	// AlignXMinYMax aligns to the minimum x and maximum y edge.
	AlignXMinYMax
	// AlignXMidYMax centers x and aligns to the maximum y edge.
	AlignXMidYMax
	// AlignXMaxYMax aligns to the maximum x and maximum y edge.
	AlignXMaxYMax
)

type AspectMeetOrSlice

type AspectMeetOrSlice int

AspectMeetOrSlice selects the uniform-scaling strategy.

const (
	// ScaleMeet scales uniformly so the entire viewBox fits.
	ScaleMeet AspectMeetOrSlice = iota
	// ScaleSlice scales uniformly so the viewport is covered.
	ScaleSlice
)

type Node

type Node struct {
	Tag      string
	Attrs    map[string]string
	Children []*Node
	Text     string
}

Node represents a parsed SVG element.

type PreserveAspectRatio

type PreserveAspectRatio struct {
	Align       AspectAlign
	MeetOrSlice AspectMeetOrSlice
	None        bool
}

PreserveAspectRatio captures the parsed preserveAspectRatio attribute.

func DefaultPreserveAspectRatio

func DefaultPreserveAspectRatio() PreserveAspectRatio

DefaultPreserveAspectRatio returns the SVG default: xMidYMid meet.

type SVG

type SVG struct {
	// contains filtered or unexported fields
}

SVG is a parsed SVG document.

func Parse

func Parse(svgXML string) (*SVG, error)

Parse parses SVG markup from a string.

func ParseBytes

func ParseBytes(data []byte) (*SVG, error)

ParseBytes parses SVG markup from bytes.

func ParseReader

func ParseReader(r io.Reader) (*SVG, error)

ParseReader parses SVG markup from a reader.

func (*SVG) AspectRatio

func (s *SVG) AspectRatio() float64

AspectRatio returns width / height, or zero if dimensions are unavailable.

func (*SVG) AutoRow

func (s *SVG) AutoRow(rectProps ...props.Rect) core.Row

AutoRow returns an automatic-height Paper row containing this SVG as an image component.

func (*SVG) Bytes

func (s *SVG) Bytes() []byte

Bytes returns a copy of the original SVG bytes.

func (*SVG) Col

func (s *SVG) Col(size int, rectProps ...props.Rect) core.Col

Col returns a Paper column containing this SVG as an image component.

func (*SVG) Component

func (s *SVG) Component(rectProps ...props.Rect) core.Component

Component returns a Paper image component backed by the original SVG bytes.

func (*SVG) Defs

func (s *SVG) Defs() map[string]*Node

Defs returns reusable elements indexed by id.

func (*SVG) Height

func (s *SVG) Height() float64

Height returns the explicit SVG height, or the viewBox height when height is absent.

func (*SVG) PreserveAspectRatio

func (s *SVG) PreserveAspectRatio() PreserveAspectRatio

PreserveAspectRatio returns the parsed preserveAspectRatio attribute.

func (*SVG) Rasterize

func (s *SVG) Rasterize(widthMM, heightMM float64) ([]byte, int, int, error)

Rasterize converts this SVG to PNG bytes at the requested millimetre dimensions.

func (*SVG) Root

func (s *SVG) Root() *Node

Root returns the parsed root SVG node.

func (*SVG) Row

func (s *SVG) Row(height float64, rectProps ...props.Rect) core.Row

Row returns a fixed-height Paper row containing this SVG as an image component.

func (*SVG) ViewBox

func (s *SVG) ViewBox() ViewBox

ViewBox returns the parsed viewBox.

func (*SVG) Width

func (s *SVG) Width() float64

Width returns the explicit SVG width, or the viewBox width when width is absent.

type ViewBox

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

ViewBox defines the SVG coordinate system.

Jump to

Keyboard shortcuts

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