svgicon

package
v0.0.0-...-1380579 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: BSD-3-Clause Imports: 14 Imported by: 1

Documentation

Overview

Provides parsing and rendering of SVG images. SVG files are parsed into an abstract representation, which can then be consumed by painting drivers. See for example oksvg/svgraster or oksvg/svgpdf .

Index

Constants

View Source
const (
	Px unite = iota
	Cm
	Mm
	Pt
	In
	Q
	Pc
	Perc // Special case : percentage (%) relative to the viewbox
)

Absolute units supported.

Variables

View Source
var DefaultStyle = PathStyle{
	FillOpacity:       1.0,
	LineOpacity:       1.0,
	LineWidth:         2.0,
	UseNonZeroWinding: true,
	Join: JoinOptions{
		MiterLimit:   fToFixed(4.),
		LineJoin:     Bevel,
		TrailLineCap: ButtCap,
	},
	FillerColor: NewPlainColor(0x00, 0x00, 0x00, 0xff),
	// contains filtered or unexported fields
}

DefaultStyle sets the default PathStyle to fill black, winding rule, full opacity, no stroke, ButtCap line end and Bevel line connect.

View Source
var Identity = Matrix2D{1, 0, 0, 1, 0, 0}

Identity is the identity matrix

Functions

This section is empty.

Types

type Bounds

type Bounds struct{ X, Y, W, H float64 }

Bounds defines a bounding box, such as a viewport or a path extent.

type CapMode

type CapMode uint8

CapMode defines how to draw caps on the ends of lines

const (
	NilCap CapMode = iota // default value
	ButtCap
	SquareCap
	RoundCap
	CubicCap     // Not part of the SVG2.0 standard.
	QuadraticCap // Not part of the SVG2.0 standard.
)

func (CapMode) String

func (c CapMode) String() string

type DashOptions

type DashOptions struct {
	Dash       []float64 // values for the dash pattern (nil or an empty slice for no dashes)
	DashOffset float64   // starting offset into the dash array
}

type Drawer

type Drawer interface {
	// Clear must reset the internal state, before starting a new path painting
	Clear()

	// Start starts a new path at the given point.
	Start(a fixed.Point26_6)

	// Line Adds a line for the current point to `b`
	Line(b fixed.Point26_6)

	// QuadBezier adds a quadratic bezier curve to the path
	QuadBezier(b, c fixed.Point26_6)

	// CubeBezier adds a cubic bezier curve to the path
	CubeBezier(b, c, d fixed.Point26_6)

	// Closes the path to the start point if `closeLoop` is true
	Stop(closeLoop bool)

	// Draw fills or strokes the accumulated path using the given color
	Draw(color Pattern, opacity float64)
}

Drawer knows how to do the actual draw operations but doesn't need any SVG kwowledge In particular, tranformations matrix are already applied to the points before sending them to the Drawer.

type Driver

type Driver interface {
	// SetupDrawers returns the backend painters, and
	// will be called at the begining of every path.
	// If the `willXXX` boolean is false, the returned drawer should be nil
	// to avoid useless operations.
	// When both booleans are true, one can assume that the exact same draw operations
	// will be performed on the Filler first and then on the Stroker.
	// This promise may enable the implementation to avoid duplicating filled and stroked paths.
	SetupDrawers(willFill, willStroke bool) (Filler, Stroker)
}

type ErrorMode

type ErrorMode uint8

ErrorMode is the for setting how the parser reacts to unparsed elements

const (
	// IgnoreErrorMode skips unparsed SVG elements
	IgnoreErrorMode ErrorMode = iota
	// WarnErrorMode outputs a warning when an unparsed SVG element is found
	WarnErrorMode
	// StrictErrorMode causes a error when an unparsed SVG element is found
	StrictErrorMode
)

type Filler

type Filler interface {
	Drawer

	// Decide to use or not the "non-zero winding" rule for the current path
	SetWinding(useNonZeroWinding bool)
}

type GapMode

type GapMode uint8

GapMode defines how to bridge gaps when the miter limit is exceeded, and is not part of the SVG2.0 standard.

const (
	NilGap GapMode = iota
	FlatGap
	RoundGap
	CubicGap
	QuadraticGap
)

func (GapMode) String

func (g GapMode) String() string

type GradStop

type GradStop struct {
	StopColor color.Color
	Offset    float64
	Opacity   float64
}

GradStop represents a stop in the SVG 2.0 gradient specification

type Gradient

type Gradient struct {
	Direction gradientDirecter
	Stops     []GradStop
	Bounds    Bounds
	Matrix    Matrix2D
	Spread    SpreadMethod
	Units     GradientUnits
}

Gradient holds a description of an SVG 2.0 gradient

func (*Gradient) ApplyPathExtent

func (g *Gradient) ApplyPathExtent(extent fixed.Rectangle26_6) Matrix2D

ApplyPathExtent use the given path extent to adjust the bounding box, if required by `Units`. The `Direction` field is not modified, but a matrix accounting for both the bouding box and the gradient matrix is returned

type GradientUnits

type GradientUnits byte

GradientUnits is the type for gradient units

const (
	ObjectBoundingBox GradientUnits = iota
	UserSpaceOnUse
)

SVG bounds paremater constants

type JoinMode

type JoinMode uint8

JoinMode type to specify how segments join.

const (
	Arc JoinMode = iota // New in SVG2
	Round
	Bevel
	Miter
	MiterClip // New in SVG2
	ArcClip   // Like MiterClip applied to arcs, and is not part of the SVG2.0 standard.
)

JoinMode constants determine how stroke segments bridge the gap at a join ArcClip mode is like MiterClip applied to arcs, and is not part of the SVG2.0 standard.

func (JoinMode) String

func (s JoinMode) String() string

type JoinOptions

type JoinOptions struct {
	MiterLimit   fixed.Int26_6 // he miter cutoff value for miter, arc, miterclip and arcClip joinModes
	LineJoin     JoinMode      // JoinMode for curve segments
	TrailLineCap CapMode       // capping functions for leading and trailing line ends. If one is nil, the other function is used at both ends.

	LeadLineCap CapMode // not part of the standard specification
	LineGap     GapMode // not part of the standard specification. determines how a gap on the convex side of two lines joining is filled
}

type Linear

type Linear [4]float64

x1, y1, x2, y2

type Matrix2D

type Matrix2D struct {
	A, B, C, D, E, F float64
}

Matrix2D represents an SVG style matrix

func (Matrix2D) Invert

func (a Matrix2D) Invert() Matrix2D

Invert returns the inverse matrix

func (Matrix2D) Mult

func (a Matrix2D) Mult(b Matrix2D) Matrix2D

Mult returns a*b

func (Matrix2D) Rotate

func (a Matrix2D) Rotate(theta float64) Matrix2D

Rotate rotate the matrix by theta

func (Matrix2D) Scale

func (a Matrix2D) Scale(x, y float64) Matrix2D

Scale matrix in x and y dimensions

func (Matrix2D) SkewX

func (a Matrix2D) SkewX(theta float64) Matrix2D

SkewX skews the matrix in the X dimension

func (Matrix2D) SkewY

func (a Matrix2D) SkewY(theta float64) Matrix2D

SkewY skews the matrix in the Y dimension

func (Matrix2D) TFixed

func (a Matrix2D) TFixed(x fixed.Point26_6) (y fixed.Point26_6)

TFixed transforms a fixed.Point26_6 by the matrix

func (Matrix2D) Transform

func (a Matrix2D) Transform(x1, y1 float64) (x2, y2 float64)

Transform multiples the input vector by matrix m and outputs the results vector components.

func (Matrix2D) TransformVector

func (a Matrix2D) TransformVector(x1, y1 float64) (x2, y2 float64)

TransformVector is a modidifed version of Transform that ignores the translation components.

func (Matrix2D) Translate

func (a Matrix2D) Translate(x, y float64) Matrix2D

Translate translates the matrix to the x , y point

type OpClose

type OpClose struct{}

OpClose close the current path.

func (OpClose) String

func (op OpClose) String() string

type OpCubicTo

type OpCubicTo [3]fixed.Point26_6

OpCubicTo draws a cubic Bezier curve from the current point, and updates it.

func (OpCubicTo) String

func (op OpCubicTo) String() string

type OpLineTo

type OpLineTo fixed.Point26_6

OpLineTo draws a line from the current point, and updates it.

func (OpLineTo) String

func (op OpLineTo) String() string

type OpMoveTo

type OpMoveTo fixed.Point26_6

OpMoveTo moves the current point.

func (OpMoveTo) String

func (op OpMoveTo) String() string

type OpQuadTo

type OpQuadTo [2]fixed.Point26_6

OpQuadTo draws a quadratic Bezier curve from the current point, and updates it.

func (OpQuadTo) String

func (op OpQuadTo) String() string

type Operation

type Operation interface {
	// SVG text representation of the command
	fmt.Stringer
	// contains filtered or unexported methods
}

Operation groups the different SVG commands

type Path

type Path []Operation

Path describes a sequence of basic SVG operations, which should not be nil Higher-level shapes may be reduced to a path.

func (*Path) Clear

func (p *Path) Clear()

Clear zeros the path slice

func (*Path) CubeBezier

func (p *Path) CubeBezier(b, c, d fixed.Point26_6)

CubeBezier adds a cubic segment to the current curve.

func (*Path) Line

func (p *Path) Line(b fixed.Point26_6)

Line adds a linear segment to the current curve.

func (*Path) QuadBezier

func (p *Path) QuadBezier(b, c fixed.Point26_6)

QuadBezier adds a quadratic segment to the current curve.

func (*Path) Start

func (p *Path) Start(a fixed.Point26_6)

Start starts a new curve at the given point.

func (*Path) Stop

func (p *Path) Stop(closeLoop bool)

Stop joins the ends of the path

func (Path) String

func (p Path) String() string

String returns a readable representation of a Path.

func (Path) ToSVGPath

func (p Path) ToSVGPath() string

ToSVGPath returns a string representation of the path

type PathStyle

type PathStyle struct {
	FillOpacity, LineOpacity float64
	LineWidth                float64
	UseNonZeroWinding        bool

	Join                    JoinOptions
	Dash                    DashOptions
	FillerColor, LinerColor Pattern // either PlainColor or Gradient
	// contains filtered or unexported fields
}

PathStyle holds the state of the SVG style

type Pattern

type Pattern interface {
	// contains filtered or unexported methods
}

Pattern groups a basic color and a gradient pattern A nil value may by used to indicated that the function (fill or stroke) is off

type PlainColor

type PlainColor struct {
	color.NRGBA
}

func NewPlainColor

func NewPlainColor(r, g, b, a uint8) PlainColor

type Radial

type Radial [6]float64

cx, cy, fx, fy, r, fr

type SpreadMethod

type SpreadMethod byte

SpreadMethod is the type for spread parameters

const (
	PadSpread SpreadMethod = iota
	ReflectSpread
	RepeatSpread
)

SVG spread parameter constants

type StrokeOptions

type StrokeOptions struct {
	LineWidth fixed.Int26_6 // width of the line
	Join      JoinOptions
	Dash      DashOptions
}

type Stroker

type Stroker interface {
	Drawer

	// Parametrize the stroking style for the current path
	SetStrokeOptions(options StrokeOptions)
}

type SvgIcon

type SvgIcon struct {
	ViewBox      Bounds
	Titles       []string // Title elements collect here
	Descriptions []string // Description elements collect here
	SVGPaths     []SvgPath
	Transform    Matrix2D

	Width, Height string // top level width and height attributes
	// contains filtered or unexported fields
}

SvgIcon holds data from parsed SVGs. See the `Draw` methods to use it.

func ReadIcon

func ReadIcon(iconFile string, errMode ErrorMode) (*SvgIcon, error)

ReadIcon reads the Icon from the named file This only supports a sub-set of SVG, but is enough to draw many icons. errMode determines if the icon ignores, errors out, or logs a warning if it does not handle an element found in the icon file.

func ReadIconStream

func ReadIconStream(stream io.Reader, errMode ErrorMode) (*SvgIcon, error)

ReadIconStream reads the Icon from the given io.Reader This only supports a sub-set of SVG, but is enough to draw many icons. errMode determines if the icon ignores, errors out, or logs a warning if it does not handle an element found in the icon file.

func (*SvgIcon) Draw

func (s *SvgIcon) Draw(d Driver, opacity float64)

Draw the compiled SVG icon into the driver `d`. `opacity` is composed (mutliplied) with the eventual <stroke-opacity> and <fill-opacity> style attributes. All elements should be contained by the Bounds rectangle of the SvgIcon: see `SetTarget` method.

func (*SvgIcon) SetTarget

func (s *SvgIcon) SetTarget(x, y, w, h float64)

SetTarget sets the Transform matrix to draw within the bounds of the rectangle arguments

type SvgPath

type SvgPath struct {
	Path  Path
	Style PathStyle
}

SvgPath binds a style to a path

Jump to

Keyboard shortcuts

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