Documentation
¶
Index ¶
- Variables
- func ConfigureTextFonts(options TextFontOptions) error
- func LoadSystemFonts()
- func ParseSVGColor(colorStr string) (color.Color, error)
- func ParseSVGColorNum(colorStr string) (r, g, b uint8, err error)
- func RegisterFont(family string, fontBytes []byte) error
- func RegisterFontAlias(alias, family string) error
- func RegisterFontCollection(family string, collectionBytes []byte) error
- func RegisterFontFamily(family string, source FaceSource) error
- type ErrorMode
- type FaceSource
- type IconCursor
- type ObjectBounds
- type PathCursor
- type PathStyle
- type Pattern
- type SvgIcon
- func ReadIcon(iconFile string, errMode ...ErrorMode) (*SvgIcon, error)
- func ReadIconStream(stream io.Reader, errMode ...ErrorMode) (*SvgIcon, error)
- func ReadIconStreamWithImages(stream io.Reader, images map[string]image.Image, errMode ...ErrorMode) (*SvgIcon, error)
- func ReadReplacingCurrentColor(stream io.Reader, currentColor string, errMode ...ErrorMode) (icon *SvgIcon, err error)
- type SvgImage
- type SvgPath
- func (svgp *SvgPath) Draw(r *rasterx.Dasher, opacity float64)
- func (svgp *SvgPath) DrawTransformed(r *rasterx.Dasher, opacity float64, t rasterx.Matrix2D)
- func (svgp *SvgPath) GetFillColor() color.Color
- func (svgp *SvgPath) GetLineColor() color.Color
- func (svgp *SvgPath) SetFillColor(clr color.Color)
- func (svgp *SvgPath) SetLineColor(clr color.Color)
- type TextFontOptions
- type TextMetrics
- type TextStyle
Constants ¶
This section is empty.
Variables ¶
var DefaultStyle = PathStyle{ FillOpacity: 1.0, LineOpacity: 1.0, LineWidth: 1.0, DashOffset: 0.0, MiterLimit: 4.0, UseNonZeroWinding: true, LineCap: rasterx.ButtCap, LineJoin: rasterx.Miter, FontFamily: "sans-serif", FontSize: 12.0, TextAnchor: "start", FontWeight: "normal", // contains filtered or unexported fields }
DefaultStyle sets the default PathStyle to fill black, winding rule, full opacity, no stroke, ButtCap line end and Miter line connect, with default font settings.
Functions ¶
func ConfigureTextFonts ¶ added in v0.1.5
func ConfigureTextFonts(options TextFontOptions) error
func LoadSystemFonts ¶
func LoadSystemFonts()
LoadSystemFonts loads the platform's system and emoji/symbol fonts into the font registry. It is safe to call concurrently and does the work at most once (subsequent calls are no-ops). oksvg calls it automatically the first time a <text> element is laid out, so the (large) emoji fonts are read from disk only when text is actually rendered rather than at package import. Callers that register fonts before rendering, or that want the load cost paid up front, may call it explicitly.
func ParseSVGColor ¶
ParseSVGColor parses an SVG color string in all forms including all SVG1.1 names, obtained from the image.colornames package
func ParseSVGColorNum ¶
ParseSVGColorNum reads the SVG color string e.g. #FBD9BD
func RegisterFont ¶
RegisterFont registers a font under the given family name.
func RegisterFontAlias ¶ added in v0.1.5
RegisterFontAlias maps an alias (including a CSS generic family) to an already registered family and its variants. It does not copy or parse font data.
func RegisterFontCollection ¶
RegisterFontCollection registers all fonts inside a TTC/collection.
func RegisterFontFamily ¶ added in v0.1.5
func RegisterFontFamily(family string, source FaceSource) error
RegisterFontFamily atomically registers all four faces under a CSS family. Source calls and parsing happen before taking the registry lock. It does not load system fonts or import a font bundle. Each face is limited to 32 MiB.
Types ¶
type ErrorMode ¶
type ErrorMode uint8
ErrorMode is the for setting how the parser reacts to unparsed elements
type FaceSource ¶ added in v0.1.5
FaceSource supplies immutable font bytes; it is compatible with ntfonts.Source. Font registration/configuration should finish before measuring/parsing documents.
type IconCursor ¶
type IconCursor struct {
PathCursor
StyleStack []PathStyle
// contains filtered or unexported fields
}
IconCursor is used while parsing SVG files.
func (*IconCursor) PushStyle ¶
func (c *IconCursor) PushStyle(attrs []xml.Attr) error
PushStyle parses the style attributes of an element and pushes the resulting style onto the style stack. Properties are applied in CSS precedence order: class selectors (lowest), then presentation attributes, then the inline style="" attribute (highest). Per-property parse errors are handled according to the error mode: StrictErrorMode aborts the parse; Warn logs and skips the property; Ignore skips silently. The style is always pushed (even when a property is skipped) so the stack stays balanced with EndElement pops.
func (*IconCursor) ReadGradAttr ¶
func (c *IconCursor) ReadGradAttr(attr xml.Attr) (err error)
ReadGradAttr reads an SVG gradient attribute
func (*IconCursor) ReadGradURL ¶
func (c *IconCursor) ReadGradURL(v string, defaultColor interface{}) (grad rasterx.Gradient, ok bool)
ReadGradURL reads an SVG format gradient url Since the context of the gradient can affect the colors the current fill or line color is passed in and used in the case of a nil stopClor value
func (*IconCursor) ReadPatternURL ¶
func (c *IconCursor) ReadPatternURL(v string) (pattern *Pattern, ok bool)
ReadPatternURL parses an SVG pattern url reference. It is a thin wrapper over readPatternURL that preserves the historical (pattern, ok) signature; any compile error is dropped (ok == false).
type ObjectBounds ¶
type ObjectBounds struct {
X, Y, W, H float64
}
ObjectBounds is the axis-aligned bounding box of the object being painted, expressed in user space (pre-transform). It positions objectBoundingBox gradients and patterns.
type PathCursor ¶
type PathCursor struct {
rasterx.Path
ErrorMode ErrorMode
// contains filtered or unexported fields
}
PathCursor is used to parse SVG format path strings into a rasterx Path
func (*PathCursor) AddArcFromA ¶
func (c *PathCursor) AddArcFromA(points []float64)
AddArcFromA adds a path of an arc element to the cursor path to the PathCursor
func (*PathCursor) CompilePath ¶
func (c *PathCursor) CompilePath(svgPath string) error
CompilePath translates the svgPath description string into a rasterx path. All valid SVG path elements are interpreted to rasterx equivalents. The resulting path element is stored in the PathCursor.
func (*PathCursor) EllipseAt ¶
func (c *PathCursor) EllipseAt(cx, cy, rx, ry float64)
EllipseAt adds a path of an elipse centered at cx, cy of radius rx and ry to the PathCursor
func (*PathCursor) GetPoints ¶
func (c *PathCursor) GetPoints(dataPoints string) error
GetPoints reads a set of floating point values from the SVG format number string, and add them to the cursor's points slice.
It tokenizes dataPoints as a run of SVG numbers: a number may start with a sign (+/-), contains digits and (via ReadFloat) one or more decimal points, and may end with an exponent marker ('e' or 'E') followed by an optional sign and digits. A '+' or '-' that is not part of an exponent starts a new number, matching the SVG grammar for lists of numbers packed together without whitespace (e.g. "1-2" is two numbers, "1e-2" is one).
func (*PathCursor) ReadFloat ¶
func (c *PathCursor) ReadFloat(numStr string) error
ReadFloat reads a floating point value and adds it to the cursor's points slice.
type PathStyle ¶
type PathStyle struct {
FillOpacity, LineOpacity float64
LineWidth, DashOffset, MiterLimit float64
Dash []float64
UseNonZeroWinding bool
LineGap rasterx.GapFunc
LeadLineCap rasterx.CapFunc // This is used if different than LineCap
LineCap rasterx.CapFunc
LineJoin rasterx.JoinMode
FontFamily string
FontSize float64
TextAnchor string
FontWeight string
FontStyle string
DominantBaseline string
// contains filtered or unexported fields
}
PathStyle holds the state of the SVG style.
type Pattern ¶
type Pattern struct {
ID string
X, Y, Width, Height float64
Units string // "userSpaceOnUse" or "objectBoundingBox"
ContentUnits string // "userSpaceOnUse" or "objectBoundingBox"
Transform rasterx.Matrix2D
Paths []SvgPath
}
Pattern holds the data of an SVG pattern element.
func (*Pattern) GetColorFunction ¶
func (p *Pattern) GetColorFunction(opacity float64, objBounds ObjectBounds, parentTransform rasterx.Matrix2D) rasterx.ColorFunc
GetColorFunction returns a ColorFunc that implements the tiling logic for this pattern. objBounds is the user-space bounding box of the painted object and parentTransform is the accumulated draw transform (user space -> device).
type SvgIcon ¶
type SvgIcon struct {
ViewBox struct{ X, Y, W, H float64 }
Titles []string // Title elements collect here
Descriptions []string // Description elements collect here
Grads map[string]*rasterx.Gradient
Patterns map[string]*Pattern
Defs map[string][]definition
SVGPaths []SvgPath
SVGImages []SvgImage
Transform rasterx.Matrix2D
DrawTarget draw.Image
MissingGlyphs int // Missing characters laid out with the selected face's .notdef glyph.
// contains filtered or unexported fields
}
SvgIcon holds data from parsed SVGs.
func ReadIcon ¶
ReadIcon reads the Icon from the named file. This only supports a sub-set of SVG, but is enough to draw many icons. If errMode is provided, the first value determines if the icon ignores, errors out, or logs a warning if it does not handle an element found in the icon file. Ignore warnings is the default if no ErrorMode value is provided.
func ReadIconStream ¶
ReadIconStream reads the Icon from the given io.Reader. This only supports a sub-set of SVG, but is enough to draw many icons. If errMode is provided, the first value determines if the icon ignores, errors out, or logs a warning if it does not handle an element found in the icon file. Ignore warnings is the default if no ErrorMode value is provided.
func ReadIconStreamWithImages ¶ added in v0.1.4
func ReadIconStreamWithImages(stream io.Reader, images map[string]image.Image, errMode ...ErrorMode) (*SvgIcon, error)
ReadIconStreamWithImages permits only already-decoded images in the supplied allowlist, keyed by href. It never reads files, fetches URLs, or decodes data URLs. The caller must keep images immutable while the icon is in use.
func ReadReplacingCurrentColor ¶
func ReadReplacingCurrentColor(stream io.Reader, currentColor string, errMode ...ErrorMode) (icon *SvgIcon, err error)
ReadReplacingCurrentColor replaces currentColor value with specified value and loads SvgIcon as ReadIconStream do. currentColor value should be valid hex, rgb or named color value.
func (*SvgIcon) Draw ¶
Draw rasterizes the icon. Vector shapes (SVGPaths) are painted into r, a rasterx.Dasher. Bitmap glyphs (SVGImages, e.g. color emoji) are composited into s.DrawTarget; if DrawTarget is nil they are skipped. Paths and images are interleaved by their draw-order stamp so document z-order is preserved. On equal or zero order, paths are drawn before images (legacy behavior).
type SvgImage ¶
type SvgImage struct {
Image image.Image
Transform rasterx.Matrix2D
Opacity float64
// contains filtered or unexported fields
}
SvgImage represents a parsed bitmap glyph/image and its transform/opacity properties.
func (*SvgImage) DrawTransformed ¶
DrawTransformed draws the SvgImage onto the destination image, applying the full affine transform (translation, scaling, rotation, and skew) and opacity in a single pass via golang.org/x/image/draw. There is no intermediate allocation: output is written directly into dst and is naturally clipped to dst's own bounds, regardless of how large the transform's scale factors are.
type SvgPath ¶
SvgPath binds a style to a path.
func (*SvgPath) DrawTransformed ¶
DrawTransformed draws the compiled SvgPath into the Dasher while applying transform t.
func (*SvgPath) GetFillColor ¶
GetFillColor returns the fill color of the SvgPath if one is defined and otherwise returns colornames.Black
func (*SvgPath) GetLineColor ¶
GetLineColor returns the stroke color of the SvgPath if one is defined and otherwise returns colornames.Black
func (*SvgPath) SetFillColor ¶
SetFillColor sets the fill color of the SvgPath
func (*SvgPath) SetLineColor ¶
SetLineColor sets the line color of the SvgPath
type TextFontOptions ¶ added in v0.1.5
TextFontOptions configures the existing process-wide registry. FallbackFamilies are tried after every CSS family and before Go fonts. Their order is preserved. Configure once at host startup, as with RegisterFont. Changing registrations between measurement and parsing can change metrics.
type TextMetrics ¶ added in v0.1.5
func MeasureText ¶ added in v0.1.5
func MeasureText(text string, style TextStyle) TextMetrics
MeasureText measures a single unnormalized text run with the same fallback, kerning and glyph advances as SVG layout. It does not shape complex scripts.




