tinysvg

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: BSD-3-Clause Imports: 7 Imported by: 9

README

tinysvg Build Go Report Card GoDoc

Construct SVG documents and images using Go.

This package mainly uses []byte slices instead of strings, and does not indent the generated SVG data, for performance and compactness.

General info

Documentation

Overview

Package tinysvg has structs and functions for creating and rendering TinySVG images

Package tinysvg supports generating and writing TinySVG 1.2 images

Some function names are suffixed with "2" if they take structs instead of ints/floats, "i" if they take ints and "f" if they take floats. Using generics might be an option.

Index

Constants

View Source
const (
	TRANSPARENT = 0.0
	OPAQUE      = 1.0

	NO = iota
	YES
	AUTO
)

Variables

View Source
var (
	ErrPair = errors.New("position pairs must be exactly two comma separated numbers")
)

Functions

func ColorBytes

func ColorBytes(r, g, b int) []byte

ColorBytes converts r, g and b (integers in the range 0..255) to a color string on the form "#nnnnnn".

func ColorBytesAlpha

func ColorBytesAlpha(r, g, b int, a float64) []byte

ColorBytesAlpha converts integers r, g and b (the color) and also a given alpha (opacity) to a color-string on the form "rgba(255, 255, 255, 1.0)".

func NewTinySVG

func NewTinySVG(w, h int) (*Document, *Tag)

NewTinySVG will create a new TinySVG document, where the width and height is defined in pixels, using the "px" suffix.

func NewTinySVG2

func NewTinySVG2(p *Pos, s *Size) (*Document, *Tag)

NewTinySVG2 creates new TinySVG 1.2 image. Pos and Size defines the viewbox

func NewTinySVGf

func NewTinySVGf(x, y, w, h float64) (*Document, *Tag)

func NewTinySVGi

func NewTinySVGi(x, y, w, h int) (*Document, *Tag)

func RGBABytes

func RGBABytes(r, g, b int, a float64) []byte

RGBABytes converts integers r, g and b (the color) and also a given alpha (opacity) to a color-string on the form "rgba(255, 255, 255, 1.0)".

func RGBBytes

func RGBBytes(r, g, b int) []byte

RGBBytes converts r, g and b (integers in the range 0..255) to a color string on the form "#nnnnnn", returned as a byte slice. May also return colors strings on the form "#nnn".

Types

type Color

type Color struct {
	R, G, B int     // red, green, blue (0..255)
	A       float64 // alpha, 0.0..1.0
	N       string  // name (optional, will override the above values)
}

func ColorByName

func ColorByName(name string) *Color

ColorByName creates a new Color with a given name, like "blue"

func NewColor

func NewColor(name string) *Color

NewColor is the same as ColorByName

func RGB

func RGB(r, g, b int) *Color

RGB creates a new Color with the given red, green and blue values. The colors are in the range 0..255.

func RGBA

func RGBA(r, g, b int, a float64) *Color

RGBA creates a new Color with the given red, green, blue and alpha values. Alpha is between 0 and 1, the rest are 0..255. For the alpha value, 0 is transparent and 1 is opaque.

func (*Color) Bytes

func (c *Color) Bytes() []byte

Bytes returns the color as an RGB (#1234FF) byte slice, or as an RGBA (rgba(0, 1, 2 ,3)) byte slice.

type Document

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

Document is an XML document, with a title and a root tag

func NewDocument

func NewDocument(title, rootTagName []byte) *Document

NewDocument creates a new XML/HTML/SVG image, with a root tag. If rootTagName contains "<" or ">", it can be used for preceding declarations, like <!DOCTYPE html> or <?xml version=\"1.0\"?>. Returns a pointer to a Document.

func (*Document) AddContent

func (image *Document) AddContent(content []byte) (*Tag, error)

AddContent adds content to the body tag. Returns the body tag and nil if successful. Returns and an error if no body tag is found, else nil.

func (*Document) Bytes

func (image *Document) Bytes() []byte

Bytes renders the image as an XML document

func (*Document) GetRoot

func (image *Document) GetRoot() *Tag

GetRoot returns the root tag of the image

func (*Document) GetTag

func (image *Document) GetTag(name []byte) (*Tag, error)

GetTag searches all tags for the given name

func (*Document) SaveSVG

func (image *Document) SaveSVG(filename string) error

SaveSVG will save the current image as an SVG file

func (*Document) String

func (image *Document) String() string

String renders the image as an XML document

func (*Document) WriteTo added in v1.1.0

func (image *Document) WriteTo(w io.Writer) (int64, error)

WriteTo will write the current image to the given io.Writer. Returns bytes written and possibly an error. This also fullfills the io.WriterTo interface.

type Font

type Font struct {
	Family string
	Size   int
}

type Pos

type Pos Vec2

func NewPos

func NewPos(xString, yString string) (*Pos, error)

func NewPosf

func NewPosf(x, y float64) *Pos

func PointsFromString

func PointsFromString(pointString string) ([]*Pos, error)

type Radius

type Radius Vec2

type Size

type Size struct {
	W, H float64
}

type Tag

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

Tag represents an XML tag, as part of a larger XML document

func NewTag

func NewTag(name []byte) *Tag

NewTag creates a new tag based on the given name. "name" is what will appear right after "<" when rendering as XML/HTML/SVG.

func (*Tag) AddAttrib

func (tag *Tag) AddAttrib(attrName string, attrValue []byte)

AddAttrib adds an attribute to a tag, for instance "size" and "20"

func (*Tag) AddAttribMap added in v1.0.2

func (tag *Tag) AddAttribMap(attrMap map[string][]byte)

AddAttribMap adds attributes based on a given map

func (*Tag) AddChild

func (tag *Tag) AddChild(child *Tag)

AddChild adds a tag as a child to another tag

func (*Tag) AddCircle

func (svg *Tag) AddCircle(x, y, radius int) *Tag

AddCircle adds a circle Add a circle, given a position (x, y) and a radius. No color is being set.

func (*Tag) AddCirclef

func (svg *Tag) AddCirclef(x, y, radius float64) *Tag

AddCirclef adds a circle Add a circle, given a position (x, y) and a radius. No color is being set.

func (*Tag) AddContent

func (tag *Tag) AddContent(content []byte)

AddContent adds text to a tag. This is what will appear between two tag markers, for example: <tag>content</tag> If the tag contains child tags, they will be rendered after this content.

func (*Tag) AddEllipse

func (svg *Tag) AddEllipse(x, y, rx, ry int) *Tag

AddEllipse adds an ellipse with a given position (x,y) and radius (rx, ry). No color is being set.

func (*Tag) AddEllipsef

func (svg *Tag) AddEllipsef(x, y, rx, ry float64) *Tag

AddEllipsef adds an ellipse with a given position (x,y) and radius (rx, ry). No color is being set.

func (*Tag) AddLastContent

func (tag *Tag) AddLastContent(content []byte)

AddLastContent appends content to the end of the existing content of a tag. Deprecated.

func (*Tag) AddNewTag

func (tag *Tag) AddNewTag(name []byte) *Tag

AddNewTag adds a new tag to another tag. This will place it one step lower in the hierarchy of tags. You can for example add a body tag to an html tag.

func (*Tag) AddRect

func (svg *Tag) AddRect(x, y, w, h int) *Tag

AddRect adds a rectangle, given x and y position, width and height. No color is being set.

func (*Tag) AddRectf

func (svg *Tag) AddRectf(x, y, w, h float64) *Tag

AddRectf adds a rectangle, given x and y position, width and height. No color is being set.

func (*Tag) AddRoundedRect

func (svg *Tag) AddRoundedRect(x, y, rx, ry, w, h int) *Tag

AddRoundedRect adds a rectangle, given x and y position, radius x, radius y, width and height. No color is being set.

func (*Tag) AddRoundedRectf

func (svg *Tag) AddRoundedRectf(x, y, rx, ry, w, h float64) *Tag

AddRoundedRectf adds a rectangle, given x and y position, radius x, radius y, width and height. No color is being set.

func (*Tag) AddSingularAttrib

func (tag *Tag) AddSingularAttrib(attrName string)

AddSingularAttrib adds attribute without a value

func (*Tag) AddTag

func (tag *Tag) AddTag(child *Tag)

AddTag adds a tag to another tag

func (*Tag) AddText

func (svg *Tag) AddText(x, y, fontSize int, fontFamily, text string) *Tag

AddText adds text. No color is being set

func (*Tag) AlphaDot

func (svg *Tag) AlphaDot(x, y, r, g, b int, a float32) *Tag

AlphaDot creates a small circle that can be transparent. Takes a position (x, y) and a color (r, g, b, a).

func (*Tag) AppendContent

func (tag *Tag) AppendContent(content []byte)

AppendContent appends content to the end of the existing content of a tag

func (*Tag) Box

func (svg *Tag) Box(x, y, w, h int, color string) *Tag

Box adds a rectangle, given x and y position, width, height and color

func (*Tag) Bytes

func (tag *Tag) Bytes() []byte

Bytes (previously getXMLRecursively) renders XML for a tag, recursively. The generated XML is returned as a []byte.

func (*Tag) Circle

func (svg *Tag) Circle(x, y, radius int, color string) *Tag

Circle adds a circle, given x and y position, radius and color

func (*Tag) Circle2

func (svg *Tag) Circle2(p *Pos, radius int, c *Color) *Tag

Circle2 adds a circle, given a position, radius and color

func (*Tag) Circlef

func (svg *Tag) Circlef(p *Pos, radius float64, c *Color) *Tag

Circlef adds a circle, given a position, radius and color

func (*Tag) CountChildren

func (tag *Tag) CountChildren() int

CountChildren returns the number of children a tag has

func (*Tag) CountSiblings

func (tag *Tag) CountSiblings() int

CountSiblings returns the number of siblings a tag has

func (*Tag) Describe

func (svg *Tag) Describe(description string)

Describe can be used for adding a description to the SVG header

func (*Tag) Dot

func (svg *Tag) Dot(x, y, r, g, b int) *Tag

Dot adds a small colored circle. Takes a position (x, y) and a color (r, g, b).

func (*Tag) Ellipse

func (svg *Tag) Ellipse(x, y, xr, yr int, color string) *Tag

Ellipse adds an ellipse, given x and y position, radiuses and color

func (*Tag) Ellipse2

func (svg *Tag) Ellipse2(p *Pos, r *Radius, c *Color) *Tag

Ellipse2 adds an ellipse with a given position (x,y) and radius (rx, ry).

func (*Tag) Fill

func (svg *Tag) Fill(color string)

Fill selects the fill color that will be used when drawing

func (*Tag) Fill2

func (svg *Tag) Fill2(c *Color)

Fill2 selects the fill color that will be used when drawing

func (*Tag) Focusable

func (svg *Tag) Focusable(yes bool, auto bool)

Focusable sets the "focusable" attribute to either true, false or auto If "auto" is true, it overrides the value of "yes".

func (*Tag) GetAttrString

func (tag *Tag) GetAttrString() []byte

GetAttrString returns a []byte that represents all the attribute keys and values of a tag. This can be used when generating XML, SVG or HTML.

func (*Tag) GetChildren

func (tag *Tag) GetChildren() []*Tag

GetChildren returns all children for a given tag. Returns a slice of pointers to tags.

func (*Tag) GetTag

func (tag *Tag) GetTag(name []byte) (*Tag, error)

GetTag finds a tag by name and returns an error if not found. Returns the first tag that matches.

func (*Tag) LastChild

func (tag *Tag) LastChild() *Tag

LastChild returns the last child of a tag

func (*Tag) Line

func (svg *Tag) Line(x1, y1, x2, y2, thickness int, color string) *Tag

Line adds a line from (x1, y1) to (x2, y2) with a given stroke width and color

func (*Tag) Line2

func (svg *Tag) Line2(p1, p2 *Pos, thickness int, c *Color) *Tag

Line2 adds a line from (x1, y1) to (x2, y2) with a given stroke width and color

func (*Tag) Pixel

func (svg *Tag) Pixel(x, y, r, g, b int) *Tag

Pixel creates a rectangle that is 1 wide with the given color. Note that the size of the "pixel" depends on how large the viewBox is.

func (*Tag) Poly2

func (svg *Tag) Poly2(p1, p2, p3, p4 *Pos, c *Color) *Tag

Poly2 adds a colored path with 4 points

func (*Tag) Poly4

func (svg *Tag) Poly4(x1, y1, x2, y2, x3, y3, x4, y4 int, color string) *Tag

Poly4 adds a colored path with 4 points

func (*Tag) Polygon

func (svg *Tag) Polygon(points []*Pos, c *Color) *Tag

Polygon adds a set of connected straight lines, a closed shape

func (*Tag) Polyline

func (svg *Tag) Polyline(points []*Pos, c *Color) *Tag

Polyline adds a set of connected straight lines, an open shape

func (*Tag) Rect2

func (svg *Tag) Rect2(p *Pos, s *Size, c *Color) *Tag

Rect2 creates a rectangle, given x and y position, width and height. No color is being set.

func (*Tag) RoundedRect2

func (svg *Tag) RoundedRect2(p *Pos, r *Radius, s *Size, c *Color) *Tag

RoundedRect2 a rectangle, given x and y position, width and height. No color is being set.

func (*Tag) ShallowCopy added in v1.1.0

func (tag *Tag) ShallowCopy() *Tag

ShallowCopy creates a copy of a tag, but uses the same attribute map!

func (*Tag) String

func (tag *Tag) String() string

String returns the XML contents as a string

func (*Tag) Stroke2

func (svg *Tag) Stroke2(c *Color)

Stroke2 selects the stroke color that will be used when drawing

func (*Tag) Text

func (svg *Tag) Text(x, y, fontSize int, fontFamily, text, color string) *Tag

Text adds text, with a color

func (*Tag) Text2

func (svg *Tag) Text2(p *Pos, f *Font, message string, c *Color) *Tag

Text2 adds text. No color is being set

func (*Tag) Thickness

func (svg *Tag) Thickness(thickness int)

Thickness sets the stroke-width attribute

func (*Tag) Triangle

func (svg *Tag) Triangle(x1, y1, x2, y2, x3, y3 int, color string) *Tag

Triangle adds a colored triangle

func (*Tag) Triangle2

func (svg *Tag) Triangle2(p1, p2, p3 *Pos, c *Color) *Tag

Triangle2 adds a colored triangle

func (*Tag) WriteTo added in v1.1.0

func (tag *Tag) WriteTo(w io.Writer) (n int64, err error)

WriteTo renders XML for a tag, recursively. The generated XML is written to the given io.Writer. This also fullfills the io.WriterTo interface.

type Vec2

type Vec2 struct {
	X, Y float64
}

type YesNoAuto

type YesNoAuto int

func NewYesNoAuto

func NewYesNoAuto(yes bool, auto bool) YesNoAuto

NewYesNoAuto will create a new Yes/No/Auto struct. If auto is true, it overrides the yes value.

func (YesNoAuto) Bytes

func (yna YesNoAuto) Bytes() []byte

Jump to

Keyboard shortcuts

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