svg

package module
v0.0.0-...-5415eb8 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 10 Imported by: 8

README

svg

Go library for parsing svg files.
Includes Bezier Curve rasterization.

Currently used for svg2kicad

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PathStringFromDrawingInstructions

func PathStringFromDrawingInstructions(dis []*DrawingInstruction) string

PathStringFromDrawingInstructions converts drawing instructions obtained from svg <path/> element back into <path/> form

Types

type Circle

type Circle struct {
	ID        string  `xml:"id,attr"`
	Transform string  `xml:"transform,attr"`
	Style     string  `xml:"style,attr"`
	Cx        float64 `xml:"cx,attr"`
	Cy        float64 `xml:"cy,attr"`
	Radius    float64 `xml:"r,attr"`
	Fill      string  `xml:"fill,attr"`
	// contains filtered or unexported fields
}

Circle is an SVG circle element

func (*Circle) ParseDrawingInstructions

func (c *Circle) ParseDrawingInstructions() (chan *DrawingInstruction, chan error)

ParseDrawingInstructions implements the DrawingInstructionParser interface

type CurvePoints

type CurvePoints struct {
	C1 *Tuple
	C2 *Tuple
	T  *Tuple
}

CurvePoints are the points needed by a bezier curve.

type DrawingInstruction

type DrawingInstruction struct {
	Kind           InstructionType
	M              *Tuple
	CurvePoints    *CurvePoints
	Radius         *float64
	StrokeWidth    *float64
	Opacity        *float64
	Fill           *string
	Stroke         *string
	StrokeLineCap  *string
	StrokeLineJoin *string
}

DrawingInstruction contains enough information that a simple drawing library can draw the shapes contained in an SVG file.

The struct contains all necessary fields but only the ones needed (as indicated byt the InstructionType) will be non-nil.

func (*DrawingInstruction) String

func (di *DrawingInstruction) String() string

type DrawingInstructionParser

type DrawingInstructionParser interface {
	ParseDrawingInstructions() (chan *DrawingInstruction, chan error)
}

DrawingInstructionParser allow getting segments and drawing instructions from them. All SVG elements should implement this interface.

type Ellipse

type Ellipse struct {
	ID        string `xml:"id,attr"`
	Transform string `xml:"transform,attr"`
	Style     string `xml:"style,attr"`
	Cx        string `xml:"cx,attr"`
	Cy        string `xml:"cy,attr"`
	Rx        string `xml:"rx,attr"`
	Ry        string `xml:"ry,attr"`
	// contains filtered or unexported fields
}

Ellipse is an SVG ellipse XML element

type Group

type Group struct {
	ID              string
	Stroke          string
	StrokeWidth     float64
	Fill            string
	Opacity         float64
	FillRule        string
	Elements        []DrawingInstructionParser
	TransformString string
	Transform       *mt.Transform // row, column
	Parent          *Group
	Owner           *Svg
	// contains filtered or unexported fields
}

Group represents an SVG group (usually located in a 'g' XML element)

func (*Group) ParseDrawingInstructions

func (g *Group) ParseDrawingInstructions() (chan *DrawingInstruction, chan error)

ParseDrawingInstructions implements the DrawingInstructionParser interface

This method makes it easier to get all the drawing instructions.

func (*Group) SetOwner

func (g *Group) SetOwner(svg *Svg)

SetOwner sets the owner of a SVG Group

func (*Group) UnmarshalXML

func (g *Group) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the encoding.xml.Unmarshaler interface

type InstructionType

type InstructionType int

InstructionType tells our path drawing library which function it has to call

const (
	MoveInstruction InstructionType = iota
	CircleInstruction
	CurveInstruction
	LineInstruction
	CloseInstruction
	PaintInstruction
)

These are instruction types that we use with our path drawing library

type Line

type Line struct {
	ID        string `xml:"id,attr"`
	Transform string `xml:"transform,attr"`
	Style     string `xml:"style,attr"`
	X1        string `xml:"x1,attr"`
	X2        string `xml:"x2,attr"`
	Y1        string `xml:"y1,attr"`
	Y2        string `xml:"y2,attr"`
	// contains filtered or unexported fields
}

Line is an SVG XML line element

type Path

type Path struct {
	ID              string `xml:"id,attr"`
	D               string `xml:"d,attr"`
	Style           string `xml:"style,attr"`
	TransformString string `xml:"transform,attr"`

	StrokeWidth    float64  `xml:"stroke-width,attr"`
	Fill           *string  `xml:"fill,attr"`
	Opacity        *float64 `xml:"opacity,attr"`
	Stroke         *string  `xml:"stroke,attr"`
	StrokeLineCap  *string  `xml:"stroke-linecap,attr"`
	StrokeLineJoin *string  `xml:"stroke-linejoin,attr"`
	Segments       chan Segment
	// contains filtered or unexported fields
}

Path is an SVG XML path element

func (*Path) Parse

func (p *Path) Parse() chan Segment

Parse interprets path description, transform and style atttributes to create a channel of segments.

func (*Path) ParseDrawingInstructions

func (p *Path) ParseDrawingInstructions() (chan *DrawingInstruction, chan error)

ParseDrawingInstructions returns two channels. One is a channel of Segments identical to the one returned by Parse() and the other one is a channel of DrawingInstruction. The latter should be used to pass to a path drawing library (like Cairo or something comparable)

type PolyLine

type PolyLine struct {
	ID        string `xml:"id,attr"`
	Transform string `xml:"transform,attr"`
	Style     string `xml:"style,attr"`
	Points    string `xml:"points,attr"`
	// contains filtered or unexported fields
}

PolyLine is a set of connected line segments that typically form a closed shape

type Polygon

type Polygon struct {
	ID        string `xml:"id,attr"`
	Transform string `xml:"transform,attr"`
	Style     string `xml:"style,attr"`
	Points    string `xml:"points,attr"`
	// contains filtered or unexported fields
}

Polygon is a closed shape of straight line segments

type Rect

type Rect struct {
	ID        string `xml:"id,attr"`
	Width     string `xml:"width,attr"`
	Height    string `xml:"height,attr"`
	Transform string `xml:"transform,attr"`
	Style     string `xml:"style,attr"`
	Rx        string `xml:"rx,attr"`
	Ry        string `xml:"ry,attr"`
	// contains filtered or unexported fields
}

Rect is an SVG XML rect element

func (*Rect) ParseDrawingInstructions

func (r *Rect) ParseDrawingInstructions() (chan *DrawingInstruction, chan error)

ParseDrawingInstructions implements the DrawingInstructionParser interface

type Segment

type Segment struct {
	Width  float64
	Closed bool
	Points [][2]float64
}

A Segment of a path that contains a list of connected points, its stroke Width and if the segment forms a closed loop. Points are defined in world space after any matrix transformation is applied.

type Svg

type Svg struct {
	Title     string  `xml:"title"`
	Groups    []Group `xml:"g"`
	Width     string  `xml:"width,attr"`
	Height    string  `xml:"height,attr"`
	ViewBox   string  `xml:"viewBox,attr"`
	Elements  []DrawingInstructionParser
	Name      string
	Transform *mt.Transform
	// contains filtered or unexported fields
}

Svg represents an SVG file containing at least a top level group or a number of Paths

func ParseSvg

func ParseSvg(str string, name string, scale float64) (*Svg, error)

ParseSvg parses an SVG string into an SVG struct

func ParseSvgFromReader

func ParseSvgFromReader(r io.Reader, name string, scale float64) (*Svg, error)

ParseSvgFromReader parses an SVG struct from an io.Reader

func (*Svg) ParseDrawingInstructions

func (s *Svg) ParseDrawingInstructions() (chan *DrawingInstruction, chan error)

ParseDrawingInstructions implements the DrawingInstructionParser interface

This method makes it easier to get all the drawing instructions.

func (*Svg) UnmarshalXML

func (s *Svg) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the encoding.xml.Unmarshaler interface

func (*Svg) ViewBoxValues

func (s *Svg) ViewBoxValues() ([]float64, error)

ViewBoxValues returns all the numerical values in the viewBox attribute.

type Tuple

type Tuple [2]float64

Tuple is an X,Y coordinate

Jump to

Keyboard shortcuts

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