svg

package
v0.1.107 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package svg provides an Evy runtime to generate SVG output for evy programs that contain graphics function calls. The SVG elements modeled in this package, such as <svg>, <circle> or <g>, and their attributes, "fill" or "stroke-width", are a small subset of all SVG elements and attributes. They are minimum necessary to output evy drawings as SVG.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Fill            string   `xml:"fill,attr,omitempty"`
	Stroke          string   `xml:"stroke,attr,omitempty"`
	StrokeWidth     *float64 `xml:"stroke-width,attr,omitempty"`
	StrokeLinecap   string   `xml:"stroke-linecap,attr,omitempty"`
	StorkeDashArray string   `xml:"stroke-dasharray,attr,omitempty"`
}

Attr represents the attributes of text and non-text SVG elements. It is embedded in other types representing SVG elements, such as Group or Circle.

type Circle

type Circle struct {
	XMLName struct{} `xml:"circle"`
	Attr
	CX float64 `xml:"cx,attr"`
	CY float64 `xml:"cy,attr"`
	R  float64 `xml:"r,attr"`
}

Circle represents an SVG circle element <circle>.

type Ellipse

type Ellipse struct {
	XMLName struct{} `xml:"ellipse"`
	Attr
	CX        float64 `xml:"cx,attr"`
	CY        float64 `xml:"cy,attr"`
	RX        float64 `xml:"rx,attr"`
	RY        float64 `xml:"ry,attr"`
	Transform string  `xml:"transform,attr,omitempty"`
}

Ellipse represents an SVG ellipse element <ellipse>.

type GraphicsRuntime

type GraphicsRuntime struct {
	SVG SVG
	// contains filtered or unexported fields
}

GraphicsRuntime implements evaluator.GraphcisRuntime for SVG output.

func NewGraphicsRuntime

func NewGraphicsRuntime() *GraphicsRuntime

NewGraphicsRuntime returns a new GraphicsRuntime with default attributes set suitable for Evy drawing output.

func (*GraphicsRuntime) Circle

func (rt *GraphicsRuntime) Circle(radius float64)

Circle draws a circle at the current cursor position with the given radius.

func (*GraphicsRuntime) Clear

func (rt *GraphicsRuntime) Clear(color string)

Clear sets the background color of the SVG canvas. We cannot simply remove all previous SVG elements as the background color could be semi-transparent overlaying previous elements.

func (*GraphicsRuntime) Color

func (rt *GraphicsRuntime) Color(color string)

Color sets the stroke and fill color.

func (*GraphicsRuntime) Dash

func (rt *GraphicsRuntime) Dash(segments []float64)

Dash sets the stroke dash array.

func (*GraphicsRuntime) Ellipse

func (rt *GraphicsRuntime) Ellipse(x, y, radiusX, radiusY, rotation, _, _ float64)

Ellipse draws an ellipse at the given x, y with the given radii. Note: startAngle, endAngle are not implemented.

func (*GraphicsRuntime) Fill

func (rt *GraphicsRuntime) Fill(str string)

Fill sets the fill color only.

func (*GraphicsRuntime) Font

func (rt *GraphicsRuntime) Font(props map[string]any)

Font sets the text font properties. The following property keys with sample values are supported:

"family": "Georgia, serif",
"size": 3, // relative to canvas, numbers only no "12px" etc.
"weight": 100, //| 200| 300 | 400 == "normal" | 500 | 600 | 700 == "bold" | 800 | 900
"style": "italic", | "oblique 35deg" | "normal"
"baseline": "top", // | "middle" | "bottom" | "alphabetic"
"align": "left", // | "center" | "right"
"letterspacing": 1 // number, see size. extra inter-character space. negative allowed.

func (*GraphicsRuntime) Gridn

func (rt *GraphicsRuntime) Gridn(unit float64, color string)

Gridn draws a grid with the given unit and color.

func (*GraphicsRuntime) Line

func (rt *GraphicsRuntime) Line(x, y float64)

Line draws a line from the current cursor position to the given x, y.

func (*GraphicsRuntime) Linecap

func (rt *GraphicsRuntime) Linecap(str string)

Linecap sets the stroke linecap style.

func (*GraphicsRuntime) Move

func (rt *GraphicsRuntime) Move(x, y float64)

Move sets the current cursor position.

func (*GraphicsRuntime) Poly

func (rt *GraphicsRuntime) Poly(vertices [][]float64)

Poly draws a polygon or polyline with the given vertices.

func (*GraphicsRuntime) Push

func (rt *GraphicsRuntime) Push()

Push combines all previously collected SVG elements and adds them to the top-level SVG element. It is typically called when styling attributes change (like fill or stroke color) or when the Evy program execution ends.

If there are multiple collected elements, they are wrapped in a group and the styles are applied to the group element. If there is only one element, the styles are applied directly to that element.

func (*GraphicsRuntime) Rect

func (rt *GraphicsRuntime) Rect(width, height float64)

Rect draws a rectangle from the current cursor position for given width and height. Negative values are permitted.

func (*GraphicsRuntime) Stroke

func (rt *GraphicsRuntime) Stroke(str string)

Stroke sets the stroke color only.

func (*GraphicsRuntime) Text

func (rt *GraphicsRuntime) Text(str string)

Text draws a text at the current cursor position.

func (*GraphicsRuntime) Width

func (rt *GraphicsRuntime) Width(w float64)

Width sets the stroke width.

func (*GraphicsRuntime) WriteSVG

func (rt *GraphicsRuntime) WriteSVG(w io.Writer) error

WriteSVG writes the SVG output to the given writer.

type Group

type Group struct {
	XMLName struct{} `xml:"g"`
	Attr
	TextAttr
	Elements []any `xml:""` // circle, rect, ...
}

Group represents a group of SVG elements <g>.

type Line

type Line struct {
	XMLName struct{} `xml:"line"`
	Attr
	X1 float64 `xml:"x1,attr"`
	Y1 float64 `xml:"y1,attr"`
	X2 float64 `xml:"x2,attr"`
	Y2 float64 `xml:"y2,attr"`
}

Line represents an SVG line element <line>.

type Polyline

type Polyline struct {
	XMLName struct{} `xml:"polyline"`
	Attr
	Points string `xml:"points,attr"`
}

Polyline represents an SVG polyline element <polyline>.

type Rect

type Rect struct {
	XMLName struct{} `xml:"rect"`
	Attr
	X      float64 `xml:"x,attr"`
	Y      float64 `xml:"y,attr"`
	Width  string  `xml:"width,attr"` // we need to use "100%" for `clear` command, so keep string type
	Height string  `xml:"height,attr"`
}

Rect represents an SVG rectangle element <rect>.

type SVG

type SVG struct {
	XMLName xml.Name `xml:"svg"`
	Attr
	TextAttr
	Width    string `xml:"width,attr,omitempty"`
	Height   string `xml:"height,attr,omitempty"`
	ViewBox  string `xml:"viewBox,attr,omitempty"`
	Style    string `xml:"style,attr,omitempty"`
	XMLNS    string `xml:"xmlns,attr,omitempty"`
	Elements []any  `xml:""` // group, circle, rect, ...
}

SVG represents a top-level SVG element <svg>.

type Text

type Text struct {
	XMLName struct{} `xml:"text"`
	Attr
	TextAttr
	X     float64 `xml:"x,attr"`
	Y     float64 `xml:"y,attr"`
	Value string  `xml:",chardata"`
}

Text represents an SVG text element <text>.

type TextAttr

type TextAttr struct {
	TextAnchor    string   `xml:"text-anchor,attr,omitempty"`
	Baseline      string   `xml:"dominant-baseline,attr,omitempty"`
	FontSize      *float64 `xml:"font-size,attr,omitempty"`
	FontWeight    *float64 `xml:"font-weight,attr,omitempty"`
	FontStyle     string   `xml:"font-style,attr,omitempty"` // italic, normal
	FontFamily    string   `xml:"font-family,attr,omitempty"`
	LetterSpacing string   `xml:"letter-spacing,attr,omitempty"`
}

TextAttr represents the attributes of text or group SVG elements and is embedded in Group and Text types.

Jump to

Keyboard shortcuts

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