Documentation ¶
Overview ¶
Package svg provides SVG rendering classes, I/O parsing: full SVG rendering
SVG currently supports most of SVG, but not:
- Flow
- Filter Effects
- 3D Perspective transforms
See gi/examples/svg for a basic SVG viewer app, using the svg.Editor, which will ultimately be expanded to support more advanced editing. Also in that directory are a number of test files that stress different aspects of rendering.
svg.NodeBase is the base type for all SVG elements -- unlike Widget nodes, SVG nodes do not use layout logic, and just draw directly into a parent SVG viewport, with cumulative transforms determining drawing position, etc. The BBox values are only valid after rendering for these nodes.
It uses srwiley/rasterx for SVG-compatible rasterization, and the core.Paint interface for drawing.
The Path element uses a compiled bytecode version of the Data path for increased speed.
Index ¶
- Constants
- Variables
- func AggCSS(agg *map[string]any, css map[string]any)
- func BBoxFromChildren(n Node) image.Rectangle
- func EscapeText(w io.Writer, s []byte, escapeNewline bool) error
- func GradientReadPts(gr gradient.Gradient, dat []float32)
- func GradientWritePts(gr gradient.Gradient, dat *[]float32)
- func IncRefCount(k tree.Node)
- func MarshalXML(n tree.Node, enc *XMLEncoder, setName string) string
- func MarshalXMLGradient(n *Gradient, name string, enc *XMLEncoder)
- func MarshalXMLTree(n Node, enc *XMLEncoder, setName string) (string, error)
- func NameFromURL(url string) string
- func NameID(nm string, id int) string
- func NameToURL(nm string) string
- func NodePropURL(n Node, prop string) string
- func PathCmdIsRel(pc PathCmds) bool
- func PathDataBBox(data []PathData) math32.Box2
- func PathDataEnd(data []PathData) (vec math32.Vector2, ang float32)
- func PathDataIterFunc(data []PathData, ...)
- func PathDataNext(data []PathData, i *int) float32
- func PathDataNextRel(data []PathData, i *int, cp math32.Vector2) math32.Vector2
- func PathDataNextVector(data []PathData, i *int) math32.Vector2
- func PathDataRender(data []PathData, pc *paint.Context)
- func PathDataStart(data []PathData) (vec math32.Vector2, ang float32)
- func PathDataString(data []PathData) string
- func PathDataTransformAbs(data []PathData, i *int, xf math32.Matrix2, lpt math32.Vector2) math32.Vector2
- func PathDataTransformRel(data []PathData, i *int, xf math32.Matrix2, cp math32.Vector2) math32.Vector2
- func PathDataValidate(data *[]PathData, errstr string) error
- func SVGWalkDown(n Node, fun func(sn Node, snb *NodeBase) bool)
- func SVGWalkDownNoDefs(n Node, fun func(sn Node, snb *NodeBase) bool)
- func SetStandardXMLAttr(ni Node, name, val string) bool
- func SplitNameID(elnm, nm string) (bool, int)
- func SplitNameIDDig(nm string) (string, int)
- func XMLAddAttr(attr *[]xml.Attr, name, val string)
- type Circle
- func (g *Circle) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Circle) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Circle) Init()
- func (g *Circle) LocalBBox() math32.Box2
- func (g *Circle) ReadGeom(sv *SVG, dat []float32)
- func (g *Circle) Render(sv *SVG)
- func (g *Circle) SVGName() string
- func (g *Circle) SetNodePos(pos math32.Vector2)
- func (g *Circle) SetNodeSize(sz math32.Vector2)
- func (t *Circle) SetPos(v math32.Vector2) *Circle
- func (t *Circle) SetRadius(v float32) *Circle
- func (g *Circle) WriteGeom(sv *SVG, dat *[]float32)
- type ClipPath
- type Ellipse
- func (g *Ellipse) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Ellipse) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Ellipse) Init()
- func (g *Ellipse) LocalBBox() math32.Box2
- func (g *Ellipse) ReadGeom(sv *SVG, dat []float32)
- func (g *Ellipse) Render(sv *SVG)
- func (g *Ellipse) SVGName() string
- func (g *Ellipse) SetNodePos(pos math32.Vector2)
- func (g *Ellipse) SetNodeSize(sz math32.Vector2)
- func (t *Ellipse) SetPos(v math32.Vector2) *Ellipse
- func (t *Ellipse) SetRadii(v math32.Vector2) *Ellipse
- func (g *Ellipse) WriteGeom(sv *SVG, dat *[]float32)
- type Filter
- type Flow
- type Gradient
- type Group
- func (g *Group) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Group) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Group) EnforceSVGName() bool
- func (g *Group) NodeBBox(sv *SVG) image.Rectangle
- func (g *Group) ReadGeom(sv *SVG, dat []float32)
- func (g *Group) Render(sv *SVG)
- func (g *Group) SVGName() string
- func (g *Group) WriteGeom(sv *SVG, dat *[]float32)
- type Image
- func (g *Image) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Image) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Image) DrawImage(sv *SVG)
- func (g *Image) LocalBBox() math32.Box2
- func (g *Image) NodeBBox(sv *SVG) image.Rectangle
- func (g *Image) OpenImage(filename string, width, height float32) error
- func (g *Image) ReadGeom(sv *SVG, dat []float32)
- func (g *Image) Render(sv *SVG)
- func (g *Image) SVGName() string
- func (g *Image) SaveImage(filename string) error
- func (t *Image) SetFilename(v string) *Image
- func (g *Image) SetImage(img image.Image, width, height float32)
- func (g *Image) SetImageSize(nwsz image.Point)
- func (g *Image) SetNodePos(pos math32.Vector2)
- func (g *Image) SetNodeSize(sz math32.Vector2)
- func (t *Image) SetPixels(v *image.RGBA) *Image
- func (t *Image) SetPos(v math32.Vector2) *Image
- func (t *Image) SetSize(v math32.Vector2) *Image
- func (t *Image) SetViewBox(v ViewBox) *Image
- func (g *Image) WriteGeom(sv *SVG, dat *[]float32)
- type Line
- func (g *Line) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Line) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Line) Init()
- func (g *Line) LocalBBox() math32.Box2
- func (g *Line) ReadGeom(sv *SVG, dat []float32)
- func (g *Line) Render(sv *SVG)
- func (g *Line) SVGName() string
- func (t *Line) SetEnd(v math32.Vector2) *Line
- func (g *Line) SetPos(pos math32.Vector2)
- func (g *Line) SetSize(sz math32.Vector2)
- func (t *Line) SetStart(v math32.Vector2) *Line
- func (g *Line) WriteGeom(sv *SVG, dat *[]float32)
- type Marker
- func (g *Marker) BBoxes(sv *SVG)
- func (g *Marker) EnforceSVGName() bool
- func (g *Marker) Render(sv *SVG)
- func (mrk *Marker) RenderMarker(sv *SVG, vertexPos math32.Vector2, vertexAng, strokeWidth float32)
- func (g *Marker) SVGName() string
- func (t *Marker) SetEffSize(v math32.Vector2) *Marker
- func (t *Marker) SetOrient(v string) *Marker
- func (t *Marker) SetRefPos(v math32.Vector2) *Marker
- func (t *Marker) SetSize(v math32.Vector2) *Marker
- func (t *Marker) SetStrokeWidth(v float32) *Marker
- func (t *Marker) SetTransform(v math32.Matrix2) *Marker
- func (t *Marker) SetUnits(v MarkerUnits) *Marker
- func (t *Marker) SetVertexAngle(v float32) *Marker
- func (t *Marker) SetVertexPos(v math32.Vector2) *Marker
- func (t *Marker) SetViewBox(v ViewBox) *Marker
- type MarkerUnits
- type MetaData
- type Node
- type NodeBase
- func (g *NodeBase) ApplyCSS(sv *SVG, key string, css map[string]any) bool
- func (g *NodeBase) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *NodeBase) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *NodeBase) AsNodeBase() *NodeBase
- func (g *NodeBase) BBoxes(sv *SVG)
- func (n *NodeBase) BaseInterface() reflect.Type
- func (g *NodeBase) DeltaTransform(trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2, ...) (math32.Matrix2, math32.Vector2)
- func (g *NodeBase) EnforceSVGName() bool
- func (g *NodeBase) GradientApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *NodeBase) GradientApplyTransformPt(sv *SVG, xf math32.Matrix2, pt math32.Vector2)
- func (g *NodeBase) GradientReadPts(sv *SVG, dat []float32)
- func (g *NodeBase) GradientWritePts(sv *SVG, dat *[]float32)
- func (g *NodeBase) Init()
- func (g *NodeBase) LocalBBox() math32.Box2
- func (g *NodeBase) LocalBBoxToWin(bb math32.Box2) image.Rectangle
- func (g *NodeBase) LocalLineWidth() float32
- func (g *NodeBase) NodeBBox(sv *SVG) image.Rectangle
- func (g *NodeBase) PaintStyle() *styles.Paint
- func (g *NodeBase) ParTransform(self bool) math32.Matrix2
- func (g *NodeBase) PushTransform(sv *SVG) (bool, *paint.Context)
- func (g *NodeBase) ReadGeom(sv *SVG, dat []float32)
- func (g *NodeBase) ReadTransform(dat []float32, idx int)
- func (g *NodeBase) Render(sv *SVG)
- func (g *NodeBase) RenderChildren(sv *SVG)
- func (g *NodeBase) SVGName() string
- func (t *NodeBase) SetClass(v string) *NodeBase
- func (g *NodeBase) SetColorProperties(prop, color string)
- func (g *NodeBase) SetNodePos(pos math32.Vector2)
- func (g *NodeBase) SetNodeSize(sz math32.Vector2)
- func (g *NodeBase) SetPos(pos math32.Vector2)
- func (g *NodeBase) SetSize(sz math32.Vector2)
- func (g *NodeBase) Style(sv *SVG)
- func (g *NodeBase) StyleCSS(sv *SVG, css map[string]any)
- func (g *NodeBase) WriteGeom(sv *SVG, dat *[]float32)
- func (g *NodeBase) WriteTransform(dat []float32, idx int)
- type Path
- func (g *Path) AddPath(cmd PathCmds, args ...float32)
- func (g *Path) AddPathArc(r, angle1, angle2 float32)
- func (g *Path) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Path) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Path) ApplyTransformImpl(xf math32.Matrix2, lpt math32.Vector2)
- func (g *Path) LocalBBox() math32.Box2
- func (g *Path) ReadGeom(sv *SVG, dat []float32)
- func (g *Path) Render(sv *SVG)
- func (g *Path) SVGName() string
- func (g *Path) SetData(data string) error
- func (t *Path) SetDataStr(v string) *Path
- func (g *Path) SetPos(pos math32.Vector2)
- func (g *Path) SetSize(sz math32.Vector2)
- func (g *Path) UpdatePathString()
- func (g *Path) WriteGeom(sv *SVG, dat *[]float32)
- type PathCmds
- type PathData
- type Polygon
- type Polyline
- func (g *Polyline) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Polyline) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Polyline) LocalBBox() math32.Box2
- func (g *Polyline) ReadGeom(sv *SVG, dat []float32)
- func (g *Polyline) Render(sv *SVG)
- func (g *Polyline) SVGName() string
- func (t *Polyline) SetPoints(v ...math32.Vector2) *Polyline
- func (g *Polyline) SetPos(pos math32.Vector2)
- func (g *Polyline) SetSize(sz math32.Vector2)
- func (g *Polyline) WriteGeom(sv *SVG, dat *[]float32)
- type Rect
- func (g *Rect) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Rect) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Rect) Init()
- func (g *Rect) LocalBBox() math32.Box2
- func (g *Rect) ReadGeom(sv *SVG, dat []float32)
- func (g *Rect) Render(sv *SVG)
- func (g *Rect) SVGName() string
- func (g *Rect) SetNodePos(pos math32.Vector2)
- func (g *Rect) SetNodeSize(sz math32.Vector2)
- func (t *Rect) SetPos(v math32.Vector2) *Rect
- func (t *Rect) SetRadius(v math32.Vector2) *Rect
- func (t *Rect) SetSize(v math32.Vector2) *Rect
- func (g *Rect) WriteGeom(sv *SVG, dat *[]float32)
- type Root
- type SVG
- func (sv *SVG) Base() color.RGBA
- func (sv *SVG) Config(width, height int)
- func (sv *SVG) DeleteAll()
- func (sv *SVG) FillViewport()
- func (sv *SVG) FindDefByName(defnm string) Node
- func (sv *SVG) FindNamedElement(name string) Node
- func (sv *SVG) GatherIDs()
- func (sv *SVG) GradientByName(n Node, grnm string) *Gradient
- func (sv *SVG) GradientCloneNodeProp(n Node, prop string) *Gradient
- func (sv *SVG) GradientDeleteForNode(n Node, grnm string) bool
- func (sv *SVG) GradientDeleteNodeProp(n Node, prop string) bool
- func (sv *SVG) GradientNew(radial bool) (*Gradient, string)
- func (sv *SVG) GradientNewForNode(n Node, radial bool, stops string) (*Gradient, string)
- func (sv *SVG) GradientUpdateAllStops()
- func (sv *SVG) GradientUpdateNodePoints(n Node, prop string)
- func (sv *SVG) GradientUpdateNodeProp(n Node, prop string, radial bool, stops string) (*Gradient, string)
- func (sv *SVG) GradientUpdateStops(gr *Gradient)
- func (sv *SVG) ImageByURL(url string) image.Image
- func (sv *SVG) MarkerByName(n Node, marker string) *Marker
- func (sv *SVG) MarshalXMLx(enc *XMLEncoder, se xml.StartElement) error
- func (sv *SVG) NewUniqueID() int
- func (sv *SVG) NodeEnsureUniqueID(n Node)
- func (sv *SVG) NodeFindURL(n Node, url string) Node
- func (sv *SVG) OpenFS(fsys fs.FS, fname string) error
- func (sv *SVG) OpenXML(fname string) error
- func (sv *SVG) ReadXML(reader io.Reader) error
- func (sv *SVG) RemoveOrphanedDefs() bool
- func (sv *SVG) Render()
- func (sv *SVG) Resize(nwsz image.Point)
- func (sv *SVG) SavePNG(fname string) error
- func (sv *SVG) SaveXML(fname string) error
- func (sv *SVG) SetDPITransform(logicalDPI float32)
- func (sv *SVG) SetRootTransform()
- func (sv *SVG) SetUnitContext(pc *styles.Paint, el, parent math32.Vector2)
- func (sv *SVG) Style()
- func (sv *SVG) UnmarshalXML(decoder *xml.Decoder, se xml.StartElement) error
- func (sv *SVG) WriteXML(wr io.Writer, indent bool) error
- type StyleSheet
- type Text
- func (g *Text) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, ...)
- func (g *Text) ApplyTransform(sv *SVG, xf math32.Matrix2)
- func (g *Text) IsParText() bool
- func (g *Text) LayoutText()
- func (g *Text) LocalBBox() math32.Box2
- func (g *Text) NodeBBox(sv *SVG) image.Rectangle
- func (g *Text) ReadGeom(sv *SVG, dat []float32)
- func (g *Text) Render(sv *SVG)
- func (g *Text) RenderText(sv *SVG)
- func (g *Text) SVGName() string
- func (t *Text) SetAdjustGlyphs(v bool) *Text
- func (t *Text) SetCharPosDX(v ...float32) *Text
- func (t *Text) SetCharPosDY(v ...float32) *Text
- func (t *Text) SetCharPosX(v ...float32) *Text
- func (t *Text) SetCharPosY(v ...float32) *Text
- func (t *Text) SetCharRots(v ...float32) *Text
- func (t *Text) SetLastBBox(v math32.Box2) *Text
- func (t *Text) SetLastPos(v math32.Vector2) *Text
- func (g *Text) SetNodePos(pos math32.Vector2)
- func (g *Text) SetNodeSize(sz math32.Vector2)
- func (t *Text) SetPos(v math32.Vector2) *Text
- func (t *Text) SetText(v string) *Text
- func (t *Text) SetTextLength(v float32) *Text
- func (t *Text) SetTextRender(v paint.Text) *Text
- func (t *Text) SetWidth(v float32) *Text
- func (g *Text) TextBBox() math32.Box2
- func (g *Text) WriteGeom(sv *SVG, dat *[]float32)
- type ViewBox
- type ViewBoxAligns
- func (va ViewBoxAligns) AlignFactor() float32
- func (va ViewBoxAligns) Aligns() styles.Aligns
- func (i ViewBoxAligns) Desc() string
- func (i ViewBoxAligns) Int64() int64
- func (i ViewBoxAligns) MarshalText() ([]byte, error)
- func (va *ViewBoxAligns) SetFromAligns(a styles.Aligns)
- func (i *ViewBoxAligns) SetInt64(in int64)
- func (i *ViewBoxAligns) SetString(s string) error
- func (i ViewBoxAligns) String() string
- func (i *ViewBoxAligns) UnmarshalText(text []byte) error
- func (i ViewBoxAligns) Values() []enums.Enum
- type ViewBoxMeetOrSlice
- func (i ViewBoxMeetOrSlice) Desc() string
- func (i ViewBoxMeetOrSlice) Int64() int64
- func (i ViewBoxMeetOrSlice) MarshalText() ([]byte, error)
- func (i *ViewBoxMeetOrSlice) SetInt64(in int64)
- func (i *ViewBoxMeetOrSlice) SetString(s string) error
- func (i ViewBoxMeetOrSlice) String() string
- func (i *ViewBoxMeetOrSlice) UnmarshalText(text []byte) error
- func (i ViewBoxMeetOrSlice) Values() []enums.Enum
- type ViewBoxPreserveAspectRatio
- type XMLEncoder
- func (xe *XMLEncoder) EncodeToken(t xml.Token) error
- func (xe *XMLEncoder) EscapeString(s string, escapeNewline bool)
- func (xe *XMLEncoder) Flush()
- func (xe *XMLEncoder) Indent(prefix, indent string)
- func (xe *XMLEncoder) WriteEOL()
- func (xe *XMLEncoder) WriteEnd(name string) error
- func (xe *XMLEncoder) WriteIndent()
- func (xe *XMLEncoder) WriteStart(start *xml.StartElement) error
- func (xe *XMLEncoder) WriteString(str string)
Constants ¶
const SVGRefCountKey = "SVGRefCount"
Variables ¶
var InkscapeProperties = map[string]bool{ "isstock": true, "stockid": true, }
InkscapeProperties are property keys that should be prefixed with "inkscape:"
var PathCmdNMap = map[PathCmds]int{ PcM: 2, Pcm: 2, PcL: 2, Pcl: 2, PcH: 1, Pch: 1, PcV: 1, Pcv: 1, PcC: 6, Pcc: 6, PcS: 4, Pcs: 4, PcQ: 4, Pcq: 4, PcT: 2, Pct: 2, PcA: 7, Pca: 7, PcZ: 0, Pcz: 0, }
PathCmdNMap gives the number of points per each command
var PathCmdToRune = map[PathCmds]rune{}
PathCmdToRune maps command to rune
var PathRuneToCmd = map[rune]PathCmds{ 'M': PcM, 'm': Pcm, 'L': PcL, 'l': Pcl, 'H': PcH, 'h': Pch, 'V': PcV, 'v': Pcv, 'C': PcC, 'c': Pcc, 'S': PcS, 's': Pcs, 'Q': PcQ, 'q': Pcq, 'T': PcT, 't': Pct, 'A': PcA, 'a': Pca, 'Z': PcZ, 'z': Pcz, }
PathRuneToCmd maps rune to path command
Functions ¶
func BBoxFromChildren ¶
BBoxFromChildren sets the Group BBox from children
func EscapeText ¶
XMLEscapeText writes to w the properly escaped XML equivalent of the plain text data s. If escapeNewline is true, newline XMLcharacters will be escaped.
func GradientReadPts ¶
GradientReadPoints reads the gradient points from a slice of floating point numbers, reading from the end.
func GradientWritePts ¶
GradientWritePoints writes the gradient points to a slice of floating point numbers, appending to end of slice.
func IncRefCount ¶
func MarshalXML ¶ added in v0.2.0
func MarshalXML(n tree.Node, enc *XMLEncoder, setName string) string
MarshalXML encodes just the given node under SVG to XML. It returns the name of node, for end tag; if empty, then children will not be output.
func MarshalXMLGradient ¶ added in v0.2.0
func MarshalXMLGradient(n *Gradient, name string, enc *XMLEncoder)
MarshalXMLGradient adds the XML for the given gradient to the given encoder. This is not in cogentcore.org/core/colors/gradient because it uses a lot of SVG and XML infrastructure defined here.
func MarshalXMLTree ¶ added in v0.2.0
func MarshalXMLTree(n Node, enc *XMLEncoder, setName string) (string, error)
MarshalXMLTree encodes the given node and any children to XML. It returns any error, and name of element that enc.WriteEnd() should be called with; allows for extra elements to be added at end of list.
func NameFromURL ¶
NameFromURL returns just the name referred to in a url(#name) if it is not a url(#) format then returns empty string.
func NameID ¶ added in v0.0.7
NameID returns the name with given unique id. returns plain name if id == 0
func NodePropURL ¶
NodePropURL returns a url(#name) url from given prop name on node, or empty string if none. Returned value is just the 'name' part of the url, not the full string.
func PathCmdIsRel ¶
PathCmdIsRel returns true if the path command is relative, false for absolute
func PathDataBBox ¶
PathDataBBox traverses the path data and extracts the local bounding box
func PathDataEnd ¶
PathDataEnd gets the ending coords and angle from the path
func PathDataIterFunc ¶
func PathDataIterFunc(data []PathData, fun func(idx int, cmd PathCmds, ptIndex int, cp math32.Vector2, ctrls []math32.Vector2) bool)
PathDataIterFunc traverses the path data and calls given function on each coordinate point, passing overall starting index of coords in data stream, command, index of the points within that command, and coord values (absolute, not relative, regardless of the command type), including special control points for path commands that have them (else nil). If function returns false (use tree.Break vs. tree.Continue) then traversal is aborted. For Control points, order is in same order as in standard path stream when multiple, e.g., C,S. For A: order is: nc, prv, rad, math32.Vector2{X: ang}, math32.Vec2(laf, sf)}
func PathDataNext ¶
PathDataNext gets the next path data point, incrementing the index
func PathDataNextRel ¶
PathDataNextRel gets the next 2 path data points as a relative vector and returns that relative vector added to current point
func PathDataNextVector ¶ added in v0.0.10
PathDataNextVector gets the next 2 path data points as a vector
func PathDataRender ¶
PathDataRender traverses the path data and renders it using paint. We assume all the data has been validated and that n's are sufficient, etc
func PathDataStart ¶
PathDataStart gets the starting coords and angle from the path
func PathDataString ¶
PathDataString returns the string representation of the path data
func PathDataTransformAbs ¶
func PathDataTransformAbs(data []PathData, i *int, xf math32.Matrix2, lpt math32.Vector2) math32.Vector2
PathDataTransformAbs does the transform of next two data points as absolute coords
func PathDataTransformRel ¶
func PathDataTransformRel(data []PathData, i *int, xf math32.Matrix2, cp math32.Vector2) math32.Vector2
PathDataTransformRel does the transform of next two data points as relative coords compared to given cp coordinate. returns new *absolute* coordinate
func PathDataValidate ¶
PathDataValidate validates the path data and emits error messages on log
func SVGWalkDown ¶ added in v0.2.0
SVGWalkDown does tree.NodeBase.WalkDown on given node using given walk function with SVG Node parameters.
func SVGWalkDownNoDefs ¶ added in v0.2.0
SVGWalkDownNoDefs does tree.Node.WalkDown on given node using given walk function with SVG Node parameters. Automatically filters Defs nodes (IsDef) and MetaData, i.e., it only processes concrete graphical nodes.
func SetStandardXMLAttr ¶ added in v0.2.0
SetStandardXMLAttr sets standard attributes of node given XML-style name / attribute values (e.g., from parsing XML / SVG files); returns true if handled.
func SplitNameID ¶ added in v0.0.7
SplitNameID splits name after the element name (e.g., 'rect') returning true if it starts with element name, and numerical id part after that element. if numerical id part is 0, then it didn't parse. SVG object names are element names + numerical id
func SplitNameIDDig ¶ added in v0.0.7
SplitNameIDDig splits name into numerical end part and preceding name, based on string of digits from end of name. If Id == 0 then it was not specified or didn't parse. SVG object names are element names + numerical id
func XMLAddAttr ¶
Types ¶
type Circle ¶
type Circle struct { NodeBase // position of the center of the circle Pos math32.Vector2 `xml:"{cx,cy}"` // radius of the circle Radius float32 `xml:"r"` }
Circle is a SVG circle
func NewCircle ¶
NewCircle returns a new Circle with the given optional parent: Circle is a SVG circle
func (*Circle) ApplyDeltaTransform ¶
func (g *Circle) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Circle) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Circle) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*Circle) SetNodePos ¶ added in v0.0.7
func (*Circle) SetNodeSize ¶ added in v0.0.7
type ClipPath ¶
type ClipPath struct {
NodeBase
}
ClipPath is used for holding a path that renders as a clip path
func NewClipPath ¶
NewClipPath returns a new ClipPath with the given optional parent: ClipPath is used for holding a path that renders as a clip path
type Ellipse ¶
type Ellipse struct { NodeBase // position of the center of the ellipse Pos math32.Vector2 `xml:"{cx,cy}"` // radii of the ellipse in the horizontal, vertical axes Radii math32.Vector2 `xml:"{rx,ry}"` }
Ellipse is a SVG ellipse
func NewEllipse ¶
NewEllipse returns a new Ellipse with the given optional parent: Ellipse is a SVG ellipse
func (*Ellipse) ApplyDeltaTransform ¶
func (g *Ellipse) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Ellipse) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Ellipse) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*Ellipse) SetNodePos ¶ added in v0.0.7
func (*Ellipse) SetNodeSize ¶ added in v0.0.7
type Filter ¶
Filter represents SVG filter* elements
func NewFilter ¶
NewFilter returns a new Filter with the given optional parent: Filter represents SVG filter* elements
func (*Filter) SetFilterType ¶
SetFilterType sets the [Filter.FilterType]
type Flow ¶
Flow represents SVG flow* elements
func NewFlow ¶
NewFlow returns a new Flow with the given optional parent: Flow represents SVG flow* elements
func (*Flow) SetFlowType ¶
SetFlowType sets the [Flow.FlowType]
type Gradient ¶
type Gradient struct { NodeBase // the color gradient Grad gradient.Gradient // name of another gradient to get stops from StopsName string }
Gradient is used for holding a specified color gradient. The name is the id for lookup in url
func NewGradient ¶
NewGradient returns a new Gradient with the given optional parent: Gradient is used for holding a specified color gradient. The name is the id for lookup in url
func (*Gradient) GradientTypeName ¶
GradientTypeName returns the SVG-style type name of gradient: linearGradient or radialGradient
func (*Gradient) SetStopsName ¶
SetStopsName sets the [Gradient.StopsName]: name of another gradient to get stops from
type Group ¶
type Group struct {
NodeBase
}
Group groups together SVG elements. Provides a common transform for all group elements and shared style properties.
func NewGroup ¶
NewGroup returns a new Group with the given optional parent: Group groups together SVG elements. Provides a common transform for all group elements and shared style properties.
func (*Group) ApplyDeltaTransform ¶
func (g *Group) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Group) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Group) EnforceSVGName ¶
type Image ¶
type Image struct { NodeBase // position of the top-left of the image Pos math32.Vector2 `xml:"{x,y}"` // rendered size of the image (imposes a scaling on image when it is rendered) Size math32.Vector2 `xml:"{width,height}"` // file name of image loaded -- set by OpenImage Filename string // how to scale and align the image ViewBox ViewBox `xml:"viewbox"` // the image pixels Pixels *image.RGBA `xml:"-" json:"-" display:"-"` }
Image is an SVG image (bitmap)
func NewImage ¶
NewImage returns a new Image with the given optional parent: Image is an SVG image (bitmap)
func (*Image) ApplyDeltaTransform ¶
func (g *Image) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Image) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Image) OpenImage ¶
OpenImage opens an image for the bitmap, and resizes to the size of the image or the specified size -- pass 0 for width and/or height to use the actual image size for that dimension
func (*Image) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*Image) SetFilename ¶
SetFilename sets the [Image.Filename]: file name of image loaded -- set by OpenImage
func (*Image) SetImage ¶
SetImage sets an image for the bitmap, and resizes to the size of the image or the specified size -- pass 0 for width and/or height to use the actual image size for that dimension. Copies from given image into internal image for this bitmap.
func (*Image) SetImageSize ¶
SetImageSize sets size of the bitmap image. This does not resize any existing image, just makes a new image if the size is different
func (*Image) SetNodePos ¶ added in v0.0.7
func (*Image) SetNodeSize ¶ added in v0.0.7
func (*Image) SetSize ¶
SetSize sets the [Image.Size]: rendered size of the image (imposes a scaling on image when it is rendered)
func (*Image) SetViewBox ¶ added in v0.0.3
SetViewBox sets the [Image.ViewBox]: how to scale and align the image
type Line ¶
type Line struct { NodeBase // position of the start of the line Start math32.Vector2 `xml:"{x1,y1}"` // position of the end of the line End math32.Vector2 `xml:"{x2,y2}"` }
Line is a SVG line
func (*Line) ApplyDeltaTransform ¶
func (g *Line) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Line) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Line) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
type Marker ¶
type Marker struct { NodeBase // reference position to align the vertex position with, specified in ViewBox coordinates RefPos math32.Vector2 `xml:"{refX,refY}"` // size of marker to render, in Units units Size math32.Vector2 `xml:"{markerWidth,markerHeight}"` // units to use Units MarkerUnits `xml:"markerUnits"` // viewbox defines the internal coordinate system for the drawing elements within the marker ViewBox ViewBox // orientation of the marker -- either 'auto' or an angle Orient string `xml:"orient"` // current vertex position VertexPos math32.Vector2 // current vertex angle in radians VertexAngle float32 // current stroke width StrokeWidth float32 // net transform computed from settings and current values -- applied prior to rendering Transform math32.Matrix2 // effective size for actual rendering EffSize math32.Vector2 }
Marker represents marker elements that can be drawn along paths (arrow heads, etc)
func NewMarker ¶
NewMarker returns a new Marker with the given optional parent: Marker represents marker elements that can be drawn along paths (arrow heads, etc)
func (*Marker) EnforceSVGName ¶
func (*Marker) RenderMarker ¶
RenderMarker renders the marker using given vertex position, angle (in radians), and stroke width
func (*Marker) SetEffSize ¶
SetEffSize sets the [Marker.EffSize]: effective size for actual rendering
func (*Marker) SetOrient ¶
SetOrient sets the [Marker.Orient]: orientation of the marker -- either 'auto' or an angle
func (*Marker) SetRefPos ¶
SetRefPos sets the [Marker.RefPos]: reference position to align the vertex position with, specified in ViewBox coordinates
func (*Marker) SetStrokeWidth ¶
SetStrokeWidth sets the [Marker.StrokeWidth]: current stroke width
func (*Marker) SetTransform ¶
SetTransform sets the [Marker.Transform]: net transform computed from settings and current values -- applied prior to rendering
func (*Marker) SetUnits ¶
func (t *Marker) SetUnits(v MarkerUnits) *Marker
SetUnits sets the [Marker.Units]: units to use
func (*Marker) SetVertexAngle ¶
SetVertexAngle sets the [Marker.VertexAngle]: current vertex angle in radians
func (*Marker) SetVertexPos ¶
SetVertexPos sets the [Marker.VertexPos]: current vertex position
func (*Marker) SetViewBox ¶
SetViewBox sets the [Marker.ViewBox]: viewbox defines the internal coordinate system for the drawing elements within the marker
type MarkerUnits ¶
type MarkerUnits int32 //enum: enum
MarkerUnits specifies units to use for svg marker elements
const ( StrokeWidth MarkerUnits = iota UserSpaceOnUse MarkerUnitsN )
type MetaData ¶
MetaData is used for holding meta data info
func NewMetaData ¶
NewMetaData returns a new MetaData with the given optional parent: MetaData is used for holding meta data info
func (*MetaData) SetMetaData ¶
SetMetaData sets the [MetaData.MetaData]
type Node ¶
type Node interface { tree.Node // AsNodeBase returns the [NodeBase] for our node, which gives // access to all the base-level data structures and methods // without requiring interface methods. AsNodeBase() *NodeBase // Render draws the node to the svg image. Render(sv *SVG) // BBoxes computes BBox and VisBBox during Render. BBoxes(sv *SVG) // LocalBBox returns the bounding box of node in local dimensions. LocalBBox() math32.Box2 // NodeBBox returns the bounding box in image coordinates for this node. NodeBBox(sv *SVG) image.Rectangle // SetNodePos sets the upper left effective position of this element, in local dimensions. SetNodePos(pos math32.Vector2) // SetNodeSize sets the overall effective size of this element, in local dimensions. SetNodeSize(sz math32.Vector2) // ApplyTransform applies the given 2D transform to the geometry of this node // this just does a direct transform multiplication on coordinates. ApplyTransform(sv *SVG, xf math32.Matrix2) // ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node // relative to given point. Trans translation and point are in top-level coordinates, // so must be transformed into local coords first. // Point is upper left corner of selection box that anchors the translation and scaling, // and for rotation it is the center point around which to rotate. ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2) // WriteGeom writes the geometry of the node to a slice of floating point numbers // the length and ordering of which is specific to each node type. // Slice must be passed and will be resized if not the correct length. WriteGeom(sv *SVG, dat *[]float32) // ReadGeom reads the geometry of the node from a slice of floating point numbers // the length and ordering of which is specific to each node type. ReadGeom(sv *SVG, dat []float32) // SVGName returns the SVG element name (e.g., "rect", "path" etc). SVGName() string // EnforceSVGName returns true if in general this element should // be named with its SVGName plus a unique id. // Groups and Markers are false. EnforceSVGName() bool }
Node is the interface for all SVG nodes.
func FirstNonGroupNode ¶
FirstNonGroupNode returns the first item that is not a group recursing into groups until a non-group item is found.
type NodeBase ¶
type NodeBase struct { tree.NodeBase // Class contains user-defined class name(s) used primarily for attaching // CSS styles to different display elements. // Multiple class names can be used to combine properties; // use spaces to separate per css standard. Class string // CSS is the cascading style sheet at this level. // These styles apply here and to everything below, until superceded. // Use .class and #name Properties elements to apply entire styles // to given elements, and type for element type. CSS map[string]any `xml:"css" set:"-"` // CSSAgg is the aggregated css properties from all higher nodes down to this node. CSSAgg map[string]any `copier:"-" json:"-" xml:"-" set:"-" display:"no-inline"` // BBox is the bounding box for the node within the SVG Pixels image. // This one can be outside the visible range of the SVG image. // VisBBox is intersected and only shows visible portion. BBox image.Rectangle `copier:"-" json:"-" xml:"-" set:"-"` // VisBBox is the visible bounding box for the node intersected with the SVG image geometry. VisBBox image.Rectangle `copier:"-" json:"-" xml:"-" set:"-"` // Paint is the paint style information for this node. Paint styles.Paint `json:"-" xml:"-" set:"-"` // contains filtered or unexported fields }
NodeBase is the base type for all elements within an SVG tree. It implements the Node interface and contains the core functionality.
func NewNodeBase ¶
NewNodeBase returns a new NodeBase with the given optional parent: NodeBase is the base type for all elements within an SVG tree. It implements the Node interface and contains the core functionality.
func (*NodeBase) ApplyCSS ¶
ApplyCSS applies css styles to given node, using key to select sub-properties from overall properties list
func (*NodeBase) ApplyDeltaTransform ¶
func (g *NodeBase) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*NodeBase) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node this just does a direct transform multiplication on coordinates.
func (*NodeBase) AsNodeBase ¶
func (*NodeBase) BBoxes ¶
ComputeBBox is called by default in render to compute bounding boxes for gui interaction -- can only be done in rendering because that is when all the proper transforms are all in place -- VpBBox is intersected with parent SVG
func (*NodeBase) BaseInterface ¶ added in v0.1.4
func (*NodeBase) DeltaTransform ¶
func (g *NodeBase) DeltaTransform(trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2, self bool) (math32.Matrix2, math32.Vector2)
DeltaTransform computes the net transform matrix for given delta transform parameters and the transformed version of the reference point. If self is true, then include the current node self transform, otherwise don't. Groups do not but regular rendering nodes do.
func (*NodeBase) EnforceSVGName ¶
func (*NodeBase) GradientApplyTransform ¶
GradientApplyTransform applies the given transform to any gradients for this node, that are using specific coordinates (not bounding box which is automatic)
func (*NodeBase) GradientApplyTransformPt ¶
GradientApplyTransformPt applies the given transform with ctr point to any gradients for this node, that are using specific coordinates (not bounding box which is automatic)
func (*NodeBase) GradientReadPts ¶
GradientReadPts reads the geometry of the gradients for this node from a slice of floating point numbers, reading from the end.
func (*NodeBase) GradientWritePts ¶
GradientWritePts writes the geometry of the gradients for this node to a slice of floating point numbers, appending to end of slice.
func (*NodeBase) LocalBBoxToWin ¶
LocalBBoxToWin converts a local bounding box to SVG coordinates
func (*NodeBase) LocalLineWidth ¶
LocalLineWidth returns the line width in local coordinates
func (*NodeBase) PaintStyle ¶
func (*NodeBase) ParTransform ¶
ParTransform returns the full compounded 2D transform matrix for all of the parents of this node. If self is true, then include our own transform too.
func (*NodeBase) PushTransform ¶
PushTransform checks our bounding box and visibility, returning false if out of bounds. If visible, pushes our transform. Must be called as first step in Render.
func (*NodeBase) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*NodeBase) ReadTransform ¶
ReadTransform reads the node transform from slice at starting index.
func (*NodeBase) RenderChildren ¶
func (*NodeBase) SetClass ¶
SetClass sets the [NodeBase.Class]: Class contains user-defined class name(s) used primarily for attaching CSS styles to different display elements. Multiple class names can be used to combine properties; use spaces to separate per css standard.
func (*NodeBase) SetColorProperties ¶ added in v0.0.10
SetColorProperties sets color property from a string representation. It breaks color alpha out as opacity. prop is either "stroke" or "fill"
func (*NodeBase) SetNodePos ¶ added in v0.0.7
func (*NodeBase) SetNodeSize ¶ added in v0.0.7
func (*NodeBase) StyleCSS ¶
StyleCSS applies css style properties to given SVG node parsing out type, .class, and #name selectors
func (*NodeBase) WriteGeom ¶
WriteGeom writes the geometry of the node to a slice of floating point numbers the length and ordering of which is specific to each node type. Slice must be passed and will be resized if not the correct length.
func (*NodeBase) WriteTransform ¶
WriteTransform writes the node transform to slice at starting index. slice must already be allocated sufficiently.
type Path ¶
type Path struct { NodeBase // the path data to render -- path commands and numbers are serialized, with each command specifying the number of floating-point coord data points that follow Data []PathData `xml:"-" set:"-"` // string version of the path data DataStr string `xml:"d"` }
Path renders SVG data sequences that can render just about anything
func NewPath ¶
NewPath returns a new Path with the given optional parent: Path renders SVG data sequences that can render just about anything
func (*Path) AddPathArc ¶ added in v0.1.1
AddPathArc adds an arc command using the simpler Paint.DrawArc parameters with center at the current position, and the given radius and angles in degrees. Because the y axis points down, angles are clockwise, and the rendering draws segments progressing from angle1 to angle2.
func (*Path) ApplyDeltaTransform ¶
func (g *Path) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Path) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Path) ApplyTransformImpl ¶
ApplyTransformImpl does the implementation of applying a transform to all points
func (*Path) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*Path) SetData ¶
SetData sets the path data to given string, parsing it into an optimized form used for rendering
func (*Path) SetDataStr ¶
SetDataStr sets the [Path.DataStr]: string version of the path data
func (*Path) UpdatePathString ¶ added in v0.1.1
func (g *Path) UpdatePathString()
UpdatePathString sets the path string from the Data
type PathCmds ¶
type PathCmds byte //enum: enum
PathCmds are the commands within the path SVG drawing data type
const ( // move pen, abs coords PcM PathCmds = iota // move pen, rel coords Pcm // lineto, abs PcL // lineto, rel Pcl // horizontal lineto, abs PcH // relative lineto, rel Pch // vertical lineto, abs PcV // vertical lineto, rel Pcv // Bezier curveto, abs PcC // Bezier curveto, rel Pcc // smooth Bezier curveto, abs PcS // smooth Bezier curveto, rel Pcs // quadratic Bezier curveto, abs PcQ // quadratic Bezier curveto, rel Pcq // smooth quadratic Bezier curveto, abs PcT // smooth quadratic Bezier curveto, rel Pct // elliptical arc, abs PcA // elliptical arc, rel Pca // close path PcZ // close path Pcz // error -- invalid command PcErr )
func PathDataNextCmd ¶
PathDataNextCmd gets the next path data command, incrementing the index -- ++ not an expression so its clunky
func PathDecodeCmd ¶
PathDecodeCmd decodes rune into corresponding command
type PathData ¶
type PathData float32
PathData encodes the svg path data, using 32-bit floats which are converted into uint32 for path commands, and contain the command as the first 5 bits, and the remaining 27 bits are the number of data points following the path command to interpret as numbers.
func PathDataParse ¶
PathDataParse parses a string representation of the path data into compiled path data
type Polygon ¶
type Polygon struct {
Polyline
}
Polygon is a SVG polygon
func NewPolygon ¶
NewPolygon returns a new Polygon with the given optional parent: Polygon is a SVG polygon
type Polyline ¶
type Polyline struct { NodeBase // the coordinates to draw -- does a moveto on the first, then lineto for all the rest Points []math32.Vector2 `xml:"points"` }
Polyline is a SVG multi-line shape
func NewPolyline ¶
NewPolyline returns a new Polyline with the given optional parent: Polyline is a SVG multi-line shape
func (*Polyline) ApplyDeltaTransform ¶
func (g *Polyline) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Polyline) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Polyline) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
type Rect ¶
type Rect struct { NodeBase // position of the top-left of the rectangle Pos math32.Vector2 `xml:"{x,y}"` // size of the rectangle Size math32.Vector2 `xml:"{width,height}"` // radii for curved corners, as a proportion of width, height Radius math32.Vector2 `xml:"{rx,ry}"` }
Rect is a SVG rectangle, optionally with rounded corners
func NewRect ¶
NewRect returns a new Rect with the given optional parent: Rect is a SVG rectangle, optionally with rounded corners
func (*Rect) ApplyDeltaTransform ¶
func (g *Rect) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Rect) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Rect) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*Rect) SetNodePos ¶ added in v0.0.7
func (*Rect) SetNodeSize ¶ added in v0.0.7
func (*Rect) SetRadius ¶
SetRadius sets the [Rect.Radius]: radii for curved corners, as a proportion of width, height
type Root ¶ added in v0.2.0
type Root struct { Group // ViewBox defines the coordinate system for the drawing. // These units are mapped into the screen space allocated // for the SVG during rendering. ViewBox ViewBox }
Root represents the root of an SVG tree.
func NewRoot ¶ added in v0.2.0
NewRoot returns a new Root with the given optional parent: Root represents the root of an SVG tree.
func (*Root) EnforceSVGName ¶ added in v0.2.0
func (*Root) SetViewBox ¶ added in v0.2.0
SetViewBox sets the [Root.ViewBox]: ViewBox defines the coordinate system for the drawing. These units are mapped into the screen space allocated for the SVG during rendering.
type SVG ¶
type SVG struct { // Name is the name of the SVG -- e.g., the filename if loaded Name string // the title of the svg Title string `xml:"title"` // the description of the svg Desc string `xml:"desc"` // Background is the image/color to fill the background with, // if any. Background image.Image // Color can be set to provide a default Fill and Stroke Color value Color image.Image // Size is size of image, Pos is offset within any parent viewport. // Node bounding boxes are based on 0 Pos offset within Pixels image Geom math32.Geom2DInt // physical width of the drawing, e.g., when printed. // Does not affect rendering, metadata. PhysicalWidth units.Value // physical height of the drawing, e.g., when printed. // Does not affect rendering, metadata. PhysicalHeight units.Value // InvertY, when applying the ViewBox transform, also flip the Y axis so that // the smallest Y value is at the bottom of the SVG box, // instead of being at the top as it is by default. InvertY bool // Translate specifies a translation to apply beyond what is specified in the SVG, // and its ViewBox transform. Translate math32.Vector2 // Scale specifies a zoom scale factor to apply beyond what is specified in the SVG, // and its ViewBox transform. Scale float32 // render state for rendering RenderState paint.State `copier:"-" json:"-" xml:"-" edit:"-"` // live pixels that we render into Pixels *image.RGBA `copier:"-" json:"-" xml:"-" edit:"-"` // all defs defined elements go here (gradients, symbols, etc) Defs *Group // Root is the root of the svg tree, which has the top-level viewbox and styles. Root *Root // map of def names to index. uses starting index to find element. // always updated after each search. DefIndexes map[string]int `display:"-" json:"-" xml:"-"` // map of unique numeric ids for all elements. // Used for allocating new unique id numbers, appended to end of elements. // See NewUniqueID, GatherIDs UniqueIDs map[int]struct{} `display:"-" json:"-" xml:"-"` // flag is set when the SVG is rendering IsRendering bool // mutex for protecting rendering RenderMu sync.Mutex `display:"-" json:"-" xml:"-"` }
SVG is an SVG object.
func (*SVG) Base ¶
Base returns the current Color activated in the context. Color has support for special color names that are relative to this current color.
func (*SVG) Config ¶
Config configures the SVG, setting image to given size and initializing all relevant fields.
func (*SVG) DeleteAll ¶
func (sv *SVG) DeleteAll()
DeleteAll deletes any existing elements in this svg
func (*SVG) FillViewport ¶
func (sv *SVG) FillViewport()
func (*SVG) FindDefByName ¶
FindDefByName finds Defs item by name, using cached indexes for speed
func (*SVG) FindNamedElement ¶
func (*SVG) GatherIDs ¶ added in v0.0.7
func (sv *SVG) GatherIDs()
GatherIDs gathers all the numeric id suffixes currently in use. It automatically renames any that are not unique or empty.
func (*SVG) GradientByName ¶
GradientByName returns the gradient of given name, stored on SVG node
func (*SVG) GradientCloneNodeProp ¶
GradientCloneNodeProp creates a new clone of the existing gradient for node if set for given property key ("fill" or "stroke"). returns new gradient.
func (*SVG) GradientDeleteForNode ¶
GradientDeleteForNode deletes the node-specific gradient on given node of given name, which can be a full url(# name or just the bare name. Returns true if deleted.
func (*SVG) GradientDeleteNodeProp ¶
GradientDeleteNodeProp deletes any existing gradient for node if set for given property key ("fill" or "stroke"). Returns true if deleted.
func (*SVG) GradientNew ¶
GradientNew adds a new gradient, either linear or radial, with a new unique id
func (*SVG) GradientNewForNode ¶
GradientNewForNode adds a new gradient specific to given node that points to given stops name. returns the new gradient and the url that points to it (nil if parent svg cannot be found). Initializes gradient to use bounding box of object, but using userSpaceOnUse setting
func (*SVG) GradientUpdateAllStops ¶
func (sv *SVG) GradientUpdateAllStops()
GradientUpdateAllStops removes any items from Defs that are not actually referred to by anything in the current SVG tree. Returns true if items were removed. Does not remove gradients with StopsName = "" with extant stops -- these should be removed manually, as they are not automatically generated.
func (*SVG) GradientUpdateNodePoints ¶
GradientUpdateNodePoints updates the points for node based on current bbox
func (*SVG) GradientUpdateNodeProp ¶
func (sv *SVG) GradientUpdateNodeProp(n Node, prop string, radial bool, stops string) (*Gradient, string)
GradientUpdateNodeProp ensures that node has a gradient property of given type
func (*SVG) GradientUpdateStops ¶
GradientUpdateStops copies stops from StopsName gradient if it is set
func (*SVG) ImageByURL ¶
ImageByURL finds a Node by an element name (URL-like path), and attempts to convert it to an image.Image. Used for color styling based on url() value.
func (*SVG) MarkerByName ¶
MarkerByName finds marker property of given name, or generic "marker" type, and if set, attempts to find that marker and return it
func (*SVG) MarshalXMLx ¶
func (sv *SVG) MarshalXMLx(enc *XMLEncoder, se xml.StartElement) error
MarshalXMLx marshals the svg using XMLEncoder
func (*SVG) NewUniqueID ¶ added in v0.0.7
NewUniqueID returns a new unique numerical id number, for naming an object
func (*SVG) NodeEnsureUniqueID ¶ added in v0.2.0
NodeEnsureUniqueID ensures that the given node has a unique ID. Call this on any newly created nodes.
func (*SVG) NodeFindURL ¶
NodeFindURL finds a url element in the parent SVG of given node. Returns nil if not found. Works with full 'url(#Name)' string or plain name or "none"
func (*SVG) ReadXML ¶
ReadXML reads XML-formatted SVG input from io.Reader, and uses xml.Decoder to create the SVG scenegraph for corresponding SVG drawing. Removes any existing content in SVG first. To process a byte slice, pass: bytes.NewReader([]byte(str)) -- all errors are logged and also returned.
func (*SVG) RemoveOrphanedDefs ¶
RemoveOrphanedDefs removes any items from Defs that are not actually referred to by anything in the current SVG tree. Returns true if items were removed. Does not remove gradients with StopsName = "" with extant stops -- these should be removed manually, as they are not automatically generated.
func (*SVG) SetDPITransform ¶
SetDPITransform sets a scaling transform to compensate for a given LogicalDPI factor. svg rendering is done within a 96 DPI context.
func (*SVG) SetRootTransform ¶
func (sv *SVG) SetRootTransform()
SetRootTransform sets the Root node transform based on ViewBox, Translate, Scale parameters set on the SVG object.
func (*SVG) SetUnitContext ¶
SetUnitContext sets the unit context based on size of viewport, element, and parent element (from bbox) and then caches everything out in terms of raw pixel dots for rendering -- call at start of render
func (*SVG) UnmarshalXML ¶
UnmarshalXML unmarshals the svg using xml.Decoder
type StyleSheet ¶
type StyleSheet struct { NodeBase Sheet *css.Stylesheet `copier:"-"` }
StyleSheet is a Node2D node that contains a stylesheet -- property values contained in this sheet can be transformed into tree.Properties and set in CSS field of appropriate node
func NewStyleSheet ¶
func NewStyleSheet(parent ...tree.Node) *StyleSheet
NewStyleSheet returns a new StyleSheet with the given optional parent: StyleSheet is a Node2D node that contains a stylesheet -- property values contained in this sheet can be transformed into tree.Properties and set in CSS field of appropriate node
func (*StyleSheet) CSSProperties ¶ added in v0.0.10
func (ss *StyleSheet) CSSProperties() map[string]any
CSSProperties returns the properties for each of the rules in this style sheet, suitable for setting the CSS value of a node -- returns nil if empty sheet
func (*StyleSheet) ParseString ¶
func (ss *StyleSheet) ParseString(str string) error
ParseString parses the string into a StyleSheet of rules, which can then be used for extracting properties
func (*StyleSheet) SetSheet ¶
func (t *StyleSheet) SetSheet(v *css.Stylesheet) *StyleSheet
SetSheet sets the [StyleSheet.Sheet]
type Text ¶
type Text struct { NodeBase // position of the left, baseline of the text Pos math32.Vector2 `xml:"{x,y}"` // width of text to render if using word-wrapping Width float32 `xml:"width"` // text string to render Text string `xml:"text"` // render version of text TextRender paint.Text `xml:"-" json:"-" copier:"-"` // character positions along X axis, if specified CharPosX []float32 // character positions along Y axis, if specified CharPosY []float32 // character delta-positions along X axis, if specified CharPosDX []float32 // character delta-positions along Y axis, if specified CharPosDY []float32 // character rotations, if specified CharRots []float32 // author's computed text length, if specified -- we attempt to match TextLength float32 // in attempting to match TextLength, should we adjust glyphs in addition to spacing? AdjustGlyphs bool // last text render position -- lower-left baseline of start LastPos math32.Vector2 `xml:"-" json:"-" copier:"-"` // last actual bounding box in display units (dots) LastBBox math32.Box2 `xml:"-" json:"-" copier:"-"` }
Text renders SVG text, handling both text and tspan elements. tspan is nested under a parent text -- text has empty Text string.
func NewText ¶
NewText returns a new Text with the given optional parent: Text renders SVG text, handling both text and tspan elements. tspan is nested under a parent text -- text has empty Text string.
func (*Text) ApplyDeltaTransform ¶
func (g *Text) ApplyDeltaTransform(sv *SVG, trans math32.Vector2, scale math32.Vector2, rot float32, pt math32.Vector2)
ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node relative to given point. Trans translation and point are in top-level coordinates, so must be transformed into local coords first. Point is upper left corner of selection box that anchors the translation and scaling, and for rotation it is the center point around which to rotate
func (*Text) ApplyTransform ¶
ApplyTransform applies the given 2D transform to the geometry of this node each node must define this for itself
func (*Text) IsParText ¶
IsParText returns true if this element serves as a parent text element to tspan elements within it. This is true if NumChildren() > 0 and Text == ""
func (*Text) LayoutText ¶ added in v0.1.1
func (g *Text) LayoutText()
LayoutText does the full text layout without any transformations, including computing the text bounding box. This is called in TextBBox because that is called first prior to render.
func (*Text) ReadGeom ¶
ReadGeom reads the geometry of the node from a slice of floating point numbers the length and ordering of which is specific to each node type.
func (*Text) RenderText ¶
func (*Text) SetAdjustGlyphs ¶
SetAdjustGlyphs sets the [Text.AdjustGlyphs]: in attempting to match TextLength, should we adjust glyphs in addition to spacing?
func (*Text) SetCharPosDX ¶ added in v0.0.5
SetCharPosDX sets the [Text.CharPosDX]: character delta-positions along X axis, if specified
func (*Text) SetCharPosDY ¶ added in v0.0.5
SetCharPosDY sets the [Text.CharPosDY]: character delta-positions along Y axis, if specified
func (*Text) SetCharPosX ¶
SetCharPosX sets the [Text.CharPosX]: character positions along X axis, if specified
func (*Text) SetCharPosY ¶
SetCharPosY sets the [Text.CharPosY]: character positions along Y axis, if specified
func (*Text) SetCharRots ¶
SetCharRots sets the [Text.CharRots]: character rotations, if specified
func (*Text) SetLastBBox ¶ added in v0.0.5
SetLastBBox sets the [Text.LastBBox]: last actual bounding box in display units (dots)
func (*Text) SetLastPos ¶
SetLastPos sets the [Text.LastPos]: last text render position -- lower-left baseline of start
func (*Text) SetNodePos ¶ added in v0.0.7
func (*Text) SetNodeSize ¶ added in v0.0.7
func (*Text) SetTextLength ¶
SetTextLength sets the [Text.TextLength]: author's computed text length, if specified -- we attempt to match
func (*Text) SetTextRender ¶
SetTextRender sets the [Text.TextRender]: render version of text
func (*Text) SetWidth ¶
SetWidth sets the [Text.Width]: width of text to render if using word-wrapping
type ViewBox ¶
type ViewBox struct { // offset or starting point in parent Viewport2D Min math32.Vector2 // size of viewbox within parent Viewport2D Size math32.Vector2 // how to scale the view box within parent PreserveAspectRatio ViewBoxPreserveAspectRatio }
ViewBox is used in SVG to define the coordinate system
type ViewBoxAligns ¶ added in v0.0.3
type ViewBoxAligns int32 //enums:enum -trim-prefix Align -transform lower
ViewBoxAlign defines values for the PreserveAspectRatio alignment factor
const ( // align ViewBox.Min with midpoint of Viewport (default) AlignMid ViewBoxAligns = iota // do not preserve uniform scaling (if either X or Y is None, both are treated as such). // In this case, the Meet / Slice value is ignored. // This is the same as FitFill from styles.ObjectFits AlignNone // align ViewBox.Min with top / left of Viewport AlignMin // align ViewBox.Min+Size with bottom / right of Viewport AlignMax )
const ViewBoxAlignsN ViewBoxAligns = 4
ViewBoxAlignsN is the highest valid value for type ViewBoxAligns, plus one.
func ViewBoxAlignsValues ¶ added in v0.0.3
func ViewBoxAlignsValues() []ViewBoxAligns
ViewBoxAlignsValues returns all possible values for the type ViewBoxAligns.
func (ViewBoxAligns) AlignFactor ¶ added in v0.0.3
func (va ViewBoxAligns) AlignFactor() float32
AlignFactor returns the alignment factor for proportion offset
func (ViewBoxAligns) Aligns ¶ added in v0.0.3
func (va ViewBoxAligns) Aligns() styles.Aligns
Aligns returns the styles.Aligns version of ViewBoxAligns
func (ViewBoxAligns) Desc ¶ added in v0.0.3
func (i ViewBoxAligns) Desc() string
Desc returns the description of the ViewBoxAligns value.
func (ViewBoxAligns) Int64 ¶ added in v0.0.3
func (i ViewBoxAligns) Int64() int64
Int64 returns the ViewBoxAligns value as an int64.
func (ViewBoxAligns) MarshalText ¶ added in v0.0.3
func (i ViewBoxAligns) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (*ViewBoxAligns) SetFromAligns ¶ added in v0.0.3
func (va *ViewBoxAligns) SetFromAligns(a styles.Aligns)
SetFromAligns sets alignment from the styles.Aligns version of ViewBoxAligns
func (*ViewBoxAligns) SetInt64 ¶ added in v0.0.3
func (i *ViewBoxAligns) SetInt64(in int64)
SetInt64 sets the ViewBoxAligns value from an int64.
func (*ViewBoxAligns) SetString ¶ added in v0.0.3
func (i *ViewBoxAligns) SetString(s string) error
SetString sets the ViewBoxAligns value from its string representation, and returns an error if the string is invalid.
func (ViewBoxAligns) String ¶ added in v0.0.3
func (i ViewBoxAligns) String() string
String returns the string representation of this ViewBoxAligns value.
func (*ViewBoxAligns) UnmarshalText ¶ added in v0.0.3
func (i *ViewBoxAligns) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.
func (ViewBoxAligns) Values ¶ added in v0.0.3
func (i ViewBoxAligns) Values() []enums.Enum
Values returns all possible values for the type ViewBoxAligns.
type ViewBoxMeetOrSlice ¶
type ViewBoxMeetOrSlice int32 //enums:enum -transform lower
ViewBoxMeetOrSlice defines values for the PreserveAspectRatio meet or slice factor
const ( // Meet only applies if Align != None (i.e., only for uniform scaling), // and means the entire ViewBox is visible within Viewport, // and it is scaled up as much as possible to meet the align constraints. // This is the same as FitContain from styles.ObjectFits Meet ViewBoxMeetOrSlice = iota // Slice only applies if Align != None (i.e., only for uniform scaling), // and means the entire ViewBox is covered by the ViewBox, and the // ViewBox is scaled down as much as possible, while still meeting the // align constraints. // This is the same as FitCover from styles.ObjectFits Slice )
const ViewBoxMeetOrSliceN ViewBoxMeetOrSlice = 2
ViewBoxMeetOrSliceN is the highest valid value for type ViewBoxMeetOrSlice, plus one.
func ViewBoxMeetOrSliceValues ¶
func ViewBoxMeetOrSliceValues() []ViewBoxMeetOrSlice
ViewBoxMeetOrSliceValues returns all possible values for the type ViewBoxMeetOrSlice.
func (ViewBoxMeetOrSlice) Desc ¶
func (i ViewBoxMeetOrSlice) Desc() string
Desc returns the description of the ViewBoxMeetOrSlice value.
func (ViewBoxMeetOrSlice) Int64 ¶
func (i ViewBoxMeetOrSlice) Int64() int64
Int64 returns the ViewBoxMeetOrSlice value as an int64.
func (ViewBoxMeetOrSlice) MarshalText ¶
func (i ViewBoxMeetOrSlice) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (*ViewBoxMeetOrSlice) SetInt64 ¶
func (i *ViewBoxMeetOrSlice) SetInt64(in int64)
SetInt64 sets the ViewBoxMeetOrSlice value from an int64.
func (*ViewBoxMeetOrSlice) SetString ¶
func (i *ViewBoxMeetOrSlice) SetString(s string) error
SetString sets the ViewBoxMeetOrSlice value from its string representation, and returns an error if the string is invalid.
func (ViewBoxMeetOrSlice) String ¶
func (i ViewBoxMeetOrSlice) String() string
String returns the string representation of this ViewBoxMeetOrSlice value.
func (*ViewBoxMeetOrSlice) UnmarshalText ¶
func (i *ViewBoxMeetOrSlice) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.
func (ViewBoxMeetOrSlice) Values ¶
func (i ViewBoxMeetOrSlice) Values() []enums.Enum
Values returns all possible values for the type ViewBoxMeetOrSlice.
type ViewBoxPreserveAspectRatio ¶
type ViewBoxPreserveAspectRatio struct { // how to align X, Y coordinates within viewbox Align styles.XY[ViewBoxAligns] `xml:"align"` // how to scale the view box relative to the viewport MeetOrSlice ViewBoxMeetOrSlice `xml:"meetOrSlice"` }
ViewBoxPreserveAspectRatio determines how to scale the view box within parent Viewport2D
func (*ViewBoxPreserveAspectRatio) SetFromStyle ¶ added in v0.0.3
func (pa *ViewBoxPreserveAspectRatio) SetFromStyle(s *styles.Style)
SetFromStyle sets from ObjectFit and Justify (X) and Align (Y) Content in given style.
func (*ViewBoxPreserveAspectRatio) SetString ¶ added in v0.0.3
func (pa *ViewBoxPreserveAspectRatio) SetString(s string) error
SetString sets from a standard svg-formatted string, consisting of: none | x[Min, Mid, Max]Y[Min, Mid, Max] [ meet | slice] e.g., "xMidYMid meet" (default) It does not make sense to specify "meet | slice" for "none" as they do not apply in that case.
func (*ViewBoxPreserveAspectRatio) String ¶ added in v0.0.3
func (pa *ViewBoxPreserveAspectRatio) String() string
type XMLEncoder ¶
type XMLEncoder struct { Writer io.Writer DoIndent bool IndBytes []byte PreBytes []byte CurIndent int CurStart string NoEndIndent bool }
XMLEncoder is a minimal XML encoder that formats output with Attr each on a new line, using same API as xml.Encoder
func NewXMLEncoder ¶
func NewXMLEncoder(wr io.Writer) *XMLEncoder
func (*XMLEncoder) EncodeToken ¶
func (xe *XMLEncoder) EncodeToken(t xml.Token) error
func (*XMLEncoder) EscapeString ¶
func (xe *XMLEncoder) EscapeString(s string, escapeNewline bool)
EscapeString writes to p the properly escaped XML equivalent of the plain text data s.
func (*XMLEncoder) Flush ¶
func (xe *XMLEncoder) Flush()
func (*XMLEncoder) Indent ¶
func (xe *XMLEncoder) Indent(prefix, indent string)
func (*XMLEncoder) WriteEOL ¶
func (xe *XMLEncoder) WriteEOL()
func (*XMLEncoder) WriteEnd ¶
func (xe *XMLEncoder) WriteEnd(name string) error
func (*XMLEncoder) WriteIndent ¶
func (xe *XMLEncoder) WriteIndent()
func (*XMLEncoder) WriteStart ¶
func (xe *XMLEncoder) WriteStart(start *xml.StartElement) error
func (*XMLEncoder) WriteString ¶
func (xe *XMLEncoder) WriteString(str string)