svgparser

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 6 Imported by: 0

README

svgparser

Library for parsing and manipulating SVG files. We've made some changes to original repo and soon we will update this, sorry in advance.

Installation
go get github.com/garaekz/svgparser
Features
Validation

Checks if the SVG input is valid according to the W3C Recommendation.

Find functionality

Provides capability to search for SVG elements by id or element name.

Path Parser

Parsing the 'd' attribute of a path element into a structure containing all subpaths with their commands and parameters.

Style Parser

Parsing the value of a style element.

Example
func ExampleParse() {
	svg := `
		<svg width="100" height="100">
			<circle cx="50" cy="50" r="40" fill="red" />
		</svg>
	`
	reader := strings.NewReader(svg)

	element, _ := svgparser.Parse(reader, false)

	fmt.Printf("SVG width: %s", element.GetAttribute("width"))
	fmt.Printf("Circle fill: %s", element.Children[0].GetAttribute("fill"))

	// Output:
	// SVG width: 100
	// Circle fill: red
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	Name       xml.Name
	Attributes []xml.Attr
	Children   []*Element
	Content    string
}

Element is a representation of an SVG element.

func DecodeFirst

func DecodeFirst(decoder *xml.Decoder) (*Element, error)

DecodeFirst creates the first element from the decoder.

func NewElement

func NewElement(token xml.StartElement) *Element

NewElement creates element from decoder token.

func Parse

func Parse(source io.Reader, validate bool) (*Element, error)

Parse creates an Element instance from an SVG input.

func (*Element) Compare

func (e *Element) Compare(o *Element) bool

Compare compares two elements.

func (*Element) Decode

func (e *Element) Decode(decoder *xml.Decoder) error

Decode decodes the child elements of element.

func (*Element) FindAll

func (e *Element) FindAll(name string) []*Element

FindAll finds all children with the given name.

func (*Element) FindAllBySpaceAndLocalName

func (e *Element) FindAllBySpaceAndLocalName(space, localName string) []*Element

FindAllBySpaceAndLocalName finds all children with the specific space and local name.

func (*Element) FindID

func (e *Element) FindID(id string) *Element

FindID finds the first child with the specified ID.

func (*Element) GetAttribute added in v1.0.1

func (e *Element) GetAttribute(name string) string

GetAttribute returns the value of the attribute with the given name.

type ValidationError

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

ValidationError contains errors which have occured when parsing svg input.

func (ValidationError) Error

func (err ValidationError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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