svg

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2021 License: MIT Imports: 12 Imported by: 1

README

SVG Parser

Forked from svgparser

Library for parsing and manipulating SVG files.

Installation
go get github.com/galihrivanto/svg
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, _ := svg.Parse(reader, false)

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

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

The MIT License (MIT)

Copyright (c) 2015 Ekaterina Goranova

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrElementNotFound = errors.New("element not found")
)

define library errors

Functions

func Render

func Render(e *Element, w io.Writer, vars ...bool) error

Render renders element to SVG

func Set64Image

func Set64Image(root *Element, id string, content string) error

Set64Image replace element embeded image by id

func SetContent

func SetContent(root *Element, id string, text string) error

SetContent replace element text by id

func SetImage

func SetImage(root *Element, id string, path string, embed bool) error

SetImage replace image by id

func SetSortAttributes

func SetSortAttributes(v bool)

SetSortAttributes override value whether we need to sort attribute during serialization

Types

type Element

type Element struct {
	Name       string
	Attributes map[string]string
	Children   []*Element
	Content    string

	// namespace dict extracted form root element attrributes
	// which prefixed with xmlns:
	Namespaces map[string]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(root *Element, 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(root *Element, decoder *xml.Decoder) error

Decode decodes the child elements of element.

func (*Element) Encode

func (e *Element) Encode(encoder *xml.Encoder) error

Encode encodes the element

func (*Element) FindAll

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

FindAll finds all children with the given name.

func (*Element) FindID

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

FindID finds the first child with the specified ID.

func (*Element) Serialize

func (e *Element) Serialize() xml.StartElement

Serialize serializes element

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