Documentation
¶
Overview ¶
Package svg provides an Evy platform 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 ¶
- type Attr
- type Circle
- type Ellipse
- type GraphicsPlatform
- func (rt *GraphicsPlatform) Circle(radius float64)
- func (rt *GraphicsPlatform) Clear(color string)
- func (rt *GraphicsPlatform) Color(color string)
- func (rt *GraphicsPlatform) Dash(segments []float64)
- func (rt *GraphicsPlatform) Ellipse(x, y, radiusX, radiusY, rotation, _, _ float64)
- func (rt *GraphicsPlatform) Fill(str string)
- func (rt *GraphicsPlatform) Font(props map[string]any)
- func (rt *GraphicsPlatform) Gridn(unit float64, color string)
- func (rt *GraphicsPlatform) Line(x, y float64)
- func (rt *GraphicsPlatform) Linecap(str string)
- func (rt *GraphicsPlatform) Move(x, y float64)
- func (rt *GraphicsPlatform) Poly(vertices [][]float64)
- func (rt *GraphicsPlatform) Push()
- func (rt *GraphicsPlatform) Rect(width, height float64)
- func (rt *GraphicsPlatform) Stroke(str string)
- func (rt *GraphicsPlatform) Text(str string)
- func (rt *GraphicsPlatform) Width(w float64)
- func (rt *GraphicsPlatform) WriteSVG(w io.Writer) error
- type Group
- type Line
- type Polyline
- type Rect
- type SVG
- type Text
- type TextAttr
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"` StrokeDashArray 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 GraphicsPlatform ¶ added in v0.1.206
type GraphicsPlatform struct { SVG SVG // contains filtered or unexported fields }
GraphicsPlatform implements evaluator.GraphicsPlatform for SVG output.
func NewGraphicsPlatform ¶ added in v0.1.206
func NewGraphicsPlatform() *GraphicsPlatform
NewGraphicsPlatform returns a new GraphicsPlatform with default attributes set suitable for Evy drawing output.
func (*GraphicsPlatform) Circle ¶ added in v0.1.206
func (rt *GraphicsPlatform) Circle(radius float64)
Circle draws a circle at the current cursor position with the given radius.
func (*GraphicsPlatform) Clear ¶ added in v0.1.206
func (rt *GraphicsPlatform) 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 (*GraphicsPlatform) Color ¶ added in v0.1.206
func (rt *GraphicsPlatform) Color(color string)
Color sets the stroke and fill color.
func (*GraphicsPlatform) Dash ¶ added in v0.1.206
func (rt *GraphicsPlatform) Dash(segments []float64)
Dash sets the stroke dash array.
func (*GraphicsPlatform) Ellipse ¶ added in v0.1.206
func (rt *GraphicsPlatform) 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 (*GraphicsPlatform) Fill ¶ added in v0.1.206
func (rt *GraphicsPlatform) Fill(str string)
Fill sets the fill color only.
func (*GraphicsPlatform) Font ¶ added in v0.1.206
func (rt *GraphicsPlatform) 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 (*GraphicsPlatform) Gridn ¶ added in v0.1.206
func (rt *GraphicsPlatform) Gridn(unit float64, color string)
Gridn draws a grid with the given unit and color.
func (*GraphicsPlatform) Line ¶ added in v0.1.206
func (rt *GraphicsPlatform) Line(x, y float64)
Line draws a line from the current cursor position to the given x, y.
func (*GraphicsPlatform) Linecap ¶ added in v0.1.206
func (rt *GraphicsPlatform) Linecap(str string)
Linecap sets the stroke linecap style.
func (*GraphicsPlatform) Move ¶ added in v0.1.206
func (rt *GraphicsPlatform) Move(x, y float64)
Move sets the current cursor position.
func (*GraphicsPlatform) Poly ¶ added in v0.1.206
func (rt *GraphicsPlatform) Poly(vertices [][]float64)
Poly draws a polygon or polyline with the given vertices.
func (*GraphicsPlatform) Push ¶ added in v0.1.206
func (rt *GraphicsPlatform) 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 (*GraphicsPlatform) Rect ¶ added in v0.1.206
func (rt *GraphicsPlatform) Rect(width, height float64)
Rect draws a rectangle from the current cursor position for given width and height. Negative values are permitted.
func (*GraphicsPlatform) Stroke ¶ added in v0.1.206
func (rt *GraphicsPlatform) Stroke(str string)
Stroke sets the stroke color only.
func (*GraphicsPlatform) Text ¶ added in v0.1.206
func (rt *GraphicsPlatform) Text(str string)
Text draws a text at the current cursor position.
func (*GraphicsPlatform) Width ¶ added in v0.1.206
func (rt *GraphicsPlatform) Width(w float64)
Width sets the stroke width.
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 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.