giosvg

package module
v0.0.0-...-c8ae55d Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: BSD-3-Clause Imports: 8 Imported by: 5

README

GIOSVG

Give to your app some SVG icons. (:


Example:

// Embed your SVG into Golang:
//go:embed your_icon.svg
var iconFile []byte

// Give your SVG/XML to the Vector:
vector, err := giosvg.NewVector(iconFile)
if err != nil {
	panic(err)
}

// Create the Icon:
icon := giosvg.NewIcon(vector)

func someWidget(gtx layout.Context) layout.Dimensions {
	// Render your icon anywhere:
	return icon.Layout(gtx)
}

You can use embed to include your icon. The Vector can be reused to avoid parse the SVG multiple times.

If your icon use currentColor, you can use paint.ColorOp:

func someWidget(gtx layout.Context) layout.Dimensions {
    	// Render your icon anywhere, with custom color:
	paint.ColorOp{Color: color.NRGBA{B: 255, A: 255}}.Add(gtx.Ops)
	return icon.Layout(gtx)
}

It's possible to generate Gio functions from SVG, without need to parse XML at runtime, you can use:

go run github.com/inkeliz/giosvg/cmd/svggen -i .\path\to\assets -o .\path\to\pkg\vectors.go

It will compile all .SVG into one single file, that will create Gio functions (here you can see one example of generated file). You can render the SVG using icon := giosvg.NewIcon(pkg.IconName) then icon.Layout(gtx) as mentioned above (consider that pkg.IconName is the generated Golang code).


Icons in the example are from Freepik and from Flaticon Licensed by Creative Commons 3.0. This package is based on OKSVG.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constraints

type Constraints struct {
	Max f32.Point
	Min f32.Point
}

Constraints is the layout.Constraints with f32.Pt instead of image.Point. This is used to keep aspect ratio, and to keep the size of the icon within the constraints.

type Icon

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

Icon keeps a cache from the last frame and re-uses it if the size didn't change.

func NewIcon

func NewIcon(vector Vector) *Icon

NewIcon creates the layout.Widget from the iconOp. Similar to widget.List, the Icon keeps the state from the last draw, and the drawing is used if the size remains unchanged. You should reuse the same Icon across multiples frames.

Make sure to not reuse the Icon with different sizes in the same frame, if the same Icon is used twice in the same frame you MUST create two Icon, for each one.

func (*Icon) Layout

func (icon *Icon) Layout(gtx layout.Context) layout.Dimensions

Layout implements widget.Layout. It will render the icon based on the given layout.Constraints.Max. If the SVG uses `currentColor` you can set the color using paint.ColorOp.

type Vector

type Vector func(ops *op.Ops, constraints Constraints) layout.Dimensions

Vector hold the information from the XML/SVG file, in order to avoid decoding of the XML.

func NewVector

func NewVector(data []byte) (Vector, error)

NewVector creates an IconOp from the given data. The data is expected to be an SVG/XML

func NewVectorReader

func NewVectorReader(reader io.Reader) (Vector, error)

NewVectorReader creates an IconOp from the given io.Reader. The data is expected to be an SVG/XML

func (Vector) Layout

func (v Vector) Layout(gtx layout.Context) layout.Dimensions

Layout implements layout.Widget, that renders the current vector without any cache. Consider using NewIcon instead.

You should avoid it, that functions only exists to simplify integration to custom cache implementations.

Directories

Path Synopsis
cmd
example module
internal
svgparser/simplexml
Package simplexml is used to parse the SVG xml file, but it avoids `encoding/xml`for JS, and it's compatible with TinyGo.
Package simplexml is used to parse the SVG xml file, but it avoids `encoding/xml`for JS, and it's compatible with TinyGo.

Jump to

Keyboard shortcuts

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