svg Package
Core SVG types and utilities shared across all packages.
Types
BoundingBox
Represents a rectangular bounding box.
Methods
| Method | Description |
|---|---|
Width() float64 |
Returns the width of the bounding box |
Height() float64 |
Returns the height of the bounding box |
CenterX() float64 |
Returns the X coordinate of the center |
CenterY() float64 |
Returns the Y coordinate of the center |
IsValid() bool |
Returns true if the box has been expanded with at least one point |
Expand(x, y float64) |
Expands the box to include the given point |
Merge(other *BoundingBox) |
Merges another bounding box into this one |
Example
box := svg.NewBoundingBox()
box.Expand(10, 10)
box.Expand(90, 90)
fmt.Printf("Size: %.1f x %.1f\n", box.Width(), box.Height())
// Output: Size: 80.0 x 80.0
fmt.Printf("Center: (%.1f, %.1f)\n", box.CenterX(), box.CenterY())
// Output: Center: (50.0, 50.0)
ViewBox
Represents an SVG viewBox attribute.
Methods
| Method | Description |
|---|---|
CenterX() float64 |
Returns the X coordinate of the viewBox center |
CenterY() float64 |
Returns the Y coordinate of the viewBox center |
String() string |
Returns the viewBox as a string for SVG attribute |
Example
vb, err := svg.ParseViewBox("0 0 100 100")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Center: (%.1f, %.1f)\n", vb.CenterX(), vb.CenterY())
// Output: Center: (50.0, 50.0)
fmt.Println(vb.String())
// Output: 0.0 0.0 100.0 100.0
Functions
ParseViewBox
Parses a viewBox string.
Parameters:
s— ViewBox string (e.g., "0 0 100 100")
Returns:
ViewBox— Parsed viewBoxerror— Error if format is invalid
ParseFloat
Parses a float with a default value on error.
Parameters:
s— String to parse (handles "px" suffix)defaultVal— Default value if parsing fails
NewBoundingBox
Creates an empty bounding box.
GetElementBounds
Calculates bounds for an SVG element.
File Utilities
ListSVGFiles
Lists all SVG files in a directory (non-recursive).
ListSVGFilesRecursive
Lists all SVG files in a directory tree.
Example: