svg

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package svg renders an SVG drawing. It is a document the way the other formats of this module are: a file opens, it has one page, and the page runs onto the same gfx.Device a PDF page draws through.

What is drawn is the static picture: shapes and paths, transforms and viewports, the three paint servers, clips, masks, markers, filters, and text on a path or down the page, shaped and reordered. Scripting, animation and interaction are not a picture and are not read.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalid     = errors.New("svg: invalid")
	ErrUnsupported = errors.New("svg: unsupported")
)

Errors returned by this package. A file that is not SVG at all, or whose XML does not parse, is ErrInvalid; one this cannot draw is ErrUnsupported.

Functions

func Decode

func Decode(r io.Reader) (image.Image, error)

Decode reads a drawing and renders it at the size it asks to be.

It is not registered with image.RegisterFormat, because that decides for the whole program and is the importing program's call:

image.RegisterFormat("svg", "<svg", svg.Decode, svg.DecodeConfig)

func DecodeConfig

func DecodeConfig(r io.Reader) (image.Config, error)

DecodeConfig reads how big a drawing is without drawing it.

Types

type Device

type Device = gfx.Device

Device is the seam a page draws through, which is gfx.Device.

type Document

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

Document is one SVG file.

func Load

func Load(b []byte) (*Document, error)

Load reads an SVG drawing out of memory.

func LoadWith

func LoadWith(b []byte, o *LoadOptions) (*Document, error)

LoadWith reads an SVG drawing out of memory, resolving what it names through a loader and sizing it against the box a container gives it.

func NewReader added in v0.2.0

func NewReader(r io.ReaderAt, size int64) (*Document, error)

NewReader reads a drawing from a reader.

func NewStream added in v0.2.0

func NewStream(r io.Reader) (*Document, error)

NewStream reads a drawing from a stream that cannot be seeked, which means reading all of it into memory first. A caller with an untrusted source bounds it with io.LimitReader.

func NewStreamWith added in v0.2.0

func NewStreamWith(r io.Reader, o *LoadOptions) (*Document, error)

NewStreamWith is NewStream with the options LoadWith takes.

func Open

func Open(name string) (*Document, error)

Open reads an SVG file.

func (*Document) Close

func (d *Document) Close() error

Close releases what the document holds.

func (*Document) Metadata added in v0.2.0

func (d *Document) Metadata() Metadata

Metadata returns the drawing's title and description.

func (*Document) NumPages

func (d *Document) NumPages() int

NumPages is one: a drawing is a single page.

func (*Document) Page

func (d *Document) Page(i int) (*Page, error)

Page returns the drawing, which is page zero.

type Link struct {
	// Rect is the area the link covers, in the drawing's own coordinates.
	Rect raster.Rect
	// URI is where a link out of the drawing points, and "" for one inside.
	URI string
	// Fragment is the element a link inside the drawing names.
	Fragment string
}

Link is a link in a drawing: the area it covers, and where following it goes.

type LoadOptions

type LoadOptions struct {
	// Open resolves an address the drawing names, relative to the drawing
	// itself, which is how one inside a container reaches a picture beside
	// it. A nil Open resolves nothing but a data URL.
	Open func(name string) ([]byte, error)
	// Width and Height are the box the drawing is placed in, which is what a
	// percentage size on the root element measures against.
	Width, Height float32
}

LoadOptions are what a caller says about a drawing before it is read. A nil pointer, and a zero field, is the default.

type Metadata added in v0.2.0

type Metadata struct {
	// Title and Description are the title and desc elements of the root.
	Title       string
	Description string
}

Metadata is what a drawing says about itself.

type Options

type Options struct {
	// PixelLimit bounds the pixel area a drawing may allocate. Zero is the
	// default of 2^28 and a negative number is no limit at all.
	PixelLimit int
	// Alpha keeps the background transparent rather than filling it white,
	// which is what a drawing meant to sit on something else wants.
	Alpha bool
	// Flatness is how far a curve may sit from the lines that stand in for
	// it, in device pixels.
	Flatness float32
}

Options are what a caller may change about a render; nil is the defaults.

type Page

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

Page is the drawing.

func (*Page) Bounds

func (p *Page) Bounds() raster.Rect

Bounds is how big the drawing is, in CSS pixels, which is what a viewer draws it at when it is not told otherwise.

func (*Page) DeviceBounds

func (p *Page) DeviceBounds(dpi float64) raster.Rect

DeviceBounds is the area the page covers at a resolution.

func (*Page) HTML added in v0.2.0

func (p *Page) HTML() (string, error)

HTML returns the drawing's text as HTML.

func (*Page) Image added in v0.2.0

func (p *Page) Image() (*image.RGBA, error)

Image renders the drawing at the size it asks to be, a pixel per pixel.

func (*Page) ImageDPI

func (p *Page) ImageDPI(dpi float64) (*image.RGBA, error)

ImageDPI renders the drawing at a resolution, where 96 is the pixel its lengths are written in.

func (*Page) ImageSize

func (p *Page) ImageSize(w, h int) (*image.RGBA, error)

ImageSize renders the drawing into an image of exactly w by h pixels, scaling it to fit and keeping its aspect ratio, which is what an icon at the size a caller has room for wants. A zero side is taken from the other.

func (p *Page) Links() []Link

Links returns the links the drawing carries, one for every anchor element that covers something, in the order they are written.

func (*Page) Matrix

func (p *Page) Matrix(dpi float64) raster.Matrix

Matrix is the transform that puts the drawing on a device at a resolution, where 96 is the pixel an SVG length is written in.

func (*Page) Render

func (p *Page) Render(ctm raster.Matrix, o *Options) (*raster.Pixmap, error)

Render draws the page into a pixmap, with ctm mapping the drawing onto it.

func (*Page) RenderTo

func (p *Page) RenderTo(dst draw.Image, ctm raster.Matrix, o *Options) error

RenderTo draws the drawing into a destination the caller owns, with ctm mapping it onto that image's coordinates. This is how an icon is composited onto something already drawn rather than handed back on its own.

func (*Page) Run

func (p *Page) Run(dev Device, ctm raster.Matrix) error

Run draws the page onto a device under ctm.

func (*Page) SVG added in v0.2.0

func (p *Page) SVG() (string, error)

SVG returns the drawing as SVG.

func (*Page) StructuredText

func (p *Page) StructuredText() (*TextPage, error)

StructuredText returns the drawing's text with the box every character, line and block occupies, in the coordinates the drawing is written in.

func (*Page) StructuredTextOptions

func (p *Page) StructuredTextOptions(o *TextOptions) (*TextPage, error)

StructuredTextOptions is StructuredText with options.

func (*Page) Text

func (p *Page) Text() (string, error)

Text returns the drawing's text: a newline after every line and a blank line after every block.

func (*Page) WriteHTML added in v0.2.0

func (p *Page) WriteHTML(w io.Writer) error

WriteHTML writes the drawing's text as HTML, where it was drawn and in the face and the colour it was drawn with.

func (*Page) WriteSVG added in v0.2.0

func (p *Page) WriteSVG(w io.Writer) error

WriteSVG writes the drawing back out as SVG: the shapes it draws, resolved, with what it referred to drawn in place.

type Quad

type Quad = gfx.Quad

Quad is the four corners of what a character occupies.

type TextBlock

type TextBlock = gfx.TextBlock

TextBlock is a paragraph of text, or one image.

type TextChar

type TextChar = gfx.TextChar

TextChar is one character where it was drawn.

type TextLine

type TextLine = gfx.TextLine

TextLine is a run of characters along one baseline.

type TextOptions

type TextOptions = gfx.TextOptions

TextOptions configure what StructuredTextOptions collects.

type TextPage

type TextPage = gfx.TextPage

TextPage is a drawing's text: blocks of lines of characters.

Jump to

Keyboard shortcuts

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