svgparser

package module
v0.0.0-...-d4e593d Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2020 License: MIT Imports: 7 Imported by: 0

README

svgparser

Library for parsing and manipulating SVG files.

Installation
go get github.com/mfmayt/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.

Render functionality

Provides capability to render svgparser.Element to SVG format.

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.Attributes["width"])
	fmt.Printf("Circle fill: %s", element.Children[0].Attributes["fill"])
	
	// change children[0]'s fill attribute
	element.Children[0].Attributes["fill"]="green"
	
	// render image to writer
	buf := new(bytes.Buffer)
	err := svgparser.Render(element,buf)
	if err != nil{
	    panic(err)
	}
	
	fmt.Printf(buf.String())
	
	// Output:
	// SVG width: 100
	// Circle fill: red
	// <svg width="100" height="100">
	//	<circle cx="50" cy="50" r="40" fill="green"></circle>
	// </svg>
}
License

The MIT License (MIT)

Copyright (c) 2020 Fatih Maytalman

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

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

Render renders element to SVG

Types

type Element

type Element struct {
	Name       string
	Attributes map[string]string
	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.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/mfmayt/svgparser"
)

func main() {
	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\n", element.Attributes["width"])
	fmt.Printf("Circle fill: %s", element.Children[0].Attributes["fill"])

}
Output:

SVG width: 100
Circle fill: red

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) 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.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/mfmayt/svgparser"
)

func main() {
	svg := `
		<svg width="1000" height="600">
			<rect fill="#000" width="5" height="3"/>
			<rect fill="#444" width="5" height="2" y="1"/>
			<rect fill="#888" width="5" height="1" y="2"/>
		</svg>
	`
	reader := strings.NewReader(svg)
	element, _ := svgparser.Parse(reader, false)

	rectangles := element.FindAll("rect")

	fmt.Printf("First child fill: %s\n", rectangles[0].Attributes["fill"])
	fmt.Printf("Second child height: %s", rectangles[1].Attributes["height"])

}
Output:

First child fill: #000
Second child height: 2

func (*Element) FindID

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

FindID finds the first child with the specified ID.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/mfmayt/svgparser"
)

func main() {
	svg := `
		<svg width="1000" height="600">
			<rect id="black" fill="#000" width="5" height="3"/>
			<rect id="gray" fill="#888" width="5" height="2" y="1"/>
			<rect id="white" fill="#fff" width="5" height="1" y="2"/>
		</svg>
	`
	reader := strings.NewReader(svg)
	element, _ := svgparser.Parse(reader, false)

	white := element.FindID("white")

	fmt.Printf("White rect fill: %s", white.Attributes["fill"])

}
Output:

White rect fill: #fff

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