svg

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: BSD-3-Clause Imports: 3 Imported by: 0

README

svgicon

SVG parser and renderer written in Go.

This code is based on github.com/benoitkugler/oksvg, which itself is a heavily modified fork of github.com/srwiley/oksvg.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidColor = Color{}

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.

func (Bounds) AspectMeet

func (viewBox Bounds) AspectMeet(dx, dy, ax, ay float64) (X, Y, W, H float64)

AspectMeet positions and sizes the ViewBox inside the rectangle defined by the arguments, while maintaining the aspect ratio. The arguments dx,dy are the width and height of a rectangle (with X and Y both 0.0) and the alignment of the rectangle. Where {ax:0.0,ay:0.0} is left/top and {ax:1.0,ay:1.0} is right/bottom.

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 Color

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

Color is enables you to differentiate between a valid and invalid (none) color.

func ValidColor

func ValidColor(r, g, b, a uint8) Color

func (Color) AsColor

func (o Color) AsColor() color.Color

func (Color) AsPattern

func (o Color) AsPattern() Pattern

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 DrawerNG

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

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

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

	// 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
	Close()

	// Fill fills the accumulated path using the given color
	Fill(color Pattern, opacity float64)

	// Stroke will stroke the accumulated path using the given color
	Stroke(color Pattern, opacity float64)
}

DrawerNG is the interface for specifying paths and then filling and/or stroking them. This is a simpler interface to use instead of the Driver, Drawer, Filler and Stroker interfaces.

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)
}

Driver will given a parsed SVG document, implements how to draw it on screen. This requires a driver implementation to perform the actual draw operations, such as a rasterizer to output .png images or a pdf writer.

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 GradDirection

type GradDirection interface {
	Type() GradType
	Params() []float64
}

GradDirection is either Radial or Linear

type GradStop

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

GradStop represents a stop in the SVG 2.0 gradient specification

type GradType

type GradType byte
const (
	Linear GradType = iota
	Radial
)

type GradUnits

type GradUnits byte

GradUnits is the type for gradient units

const (
	ObjectBoundingBox GradUnits = iota
	UserSpaceOnUse
)

SVG bounds paremater constants

type Gradient

type Gradient struct {
	Direction GradDirection
	Stops     []GradStop
	Bounds    Bounds
	Matrix    matrix.Matrix2D
	Spread    SpreadMethod
	Units     GradUnits
}

Gradient holds a description of an SVG 2.0 gradient

func (*Gradient) ApplyPathExtent

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

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

func (Gradient) IsPattern

func (Gradient) IsPattern()

type Icon

type Icon interface {
	ViewBox() Bounds
	SetTarget(x, y, w, h float64)
	Draw(d Driver, opacity float64)
}

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 LinearParams

type LinearParams [4]float64

LinearParams contains the linear gradient parameters x1, y1, x2, y2

func (LinearParams) Params

func (p LinearParams) Params() []float64

func (LinearParams) Type

func (LinearParams) Type() GradType

type Pattern

type Pattern interface {
	IsPattern()
}

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 color.NRGBA

func (PlainColor) IsPattern

func (PlainColor) IsPattern()

func (PlainColor) RGBA

func (c PlainColor) RGBA() (r, g, b, a uint32)

type RadialParams

type RadialParams [6]float64

RadialParams contains the radial gradiant parameters cx, cy, fx, fy, r, fr

func (RadialParams) Params

func (p RadialParams) Params() []float64

func (RadialParams) Type

func (RadialParams) Type() GradType

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
	Dash      DashOptions
	Join      JoinOptions
}

type Stroker

type Stroker interface {
	Drawer

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

Directories

Path Synopsis
driver
Provides parsing and rendering of SVG images.
Provides parsing and rendering of SVG images.

Jump to

Keyboard shortcuts

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