Documentation
¶
Overview ¶
Package svgreader parses SVG documents and renders them as PDF content streams.
The package supports a practical subset of SVG: paths, basic shapes (rect, circle, ellipse, line, polyline, polygon), groups with transforms, and fill/stroke styling. Text elements are parsed and can be rendered using an external text shaper (e.g. textshape).
Usage:
doc, err := svgreader.Parse(reader) pdfStream := doc.RenderPDF(widthPt, heightPt)
Index ¶
- func PathCommandsToString(cmds []PathCommand) string
- func QuadToCubic(qx0, qy0, qx1, qy1, qx2, qy2 float64) (cx1, cy1, cx2, cy2 float64)
- type Circle
- type Color
- type Document
- type Element
- type Ellipse
- type Group
- type Line
- type Matrix
- type Path
- type PathCommand
- type Point
- type Polygon
- type Polyline
- type Rect
- type RenderOptions
- type StyleAttrs
- type Text
- type TextRenderer
- type ViewBox
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PathCommandsToString ¶
func PathCommandsToString(cmds []PathCommand) string
String returns the SVG path data string representation of the commands.
func QuadToCubic ¶
QuadToCubic converts a quadratic Bézier (Q) to a cubic Bézier (C). PDF only supports cubic Bézier curves.
Types ¶
type Circle ¶
type Circle struct {
Cx, Cy float64
R float64
Style StyleAttrs
Transform string
}
Circle represents an SVG <circle> element.
type Color ¶
Color represents an SVG color with RGB components (0–1 range).
func ParseColor ¶
ParseColor parses an SVG color value. Supported formats: "none", "#rgb", "#rrggbb", "rgb(r,g,b)", named colors.
func (Color) PDFNonstroking ¶
PDFNonstroking returns the PDF operator to set this color as the fill color.
func (Color) PDFStroking ¶
PDFStroking returns the PDF operator to set this color as the stroking color.
type Document ¶
type Document struct {
Width float64 // Document width in SVG user units
Height float64 // Document height in SVG user units
ViewBox ViewBox // ViewBox specification
Elements []Element // Child elements in document order
FontFamily string // Default font-family from the <svg> element
}
Document represents a parsed SVG document.
func (*Document) RenderPDF ¶
func (doc *Document) RenderPDF(opts RenderOptions) string
RenderPDF renders the SVG document as a PDF content stream. The width and height specify the desired output size in PDF points. If both are zero, the SVG dimensions are used as-is.
type Element ¶
type Element interface {
// contains filtered or unexported methods
}
Element is the interface implemented by all SVG elements.
type Ellipse ¶
type Ellipse struct {
Cx, Cy float64
Rx, Ry float64
Style StyleAttrs
Transform string
}
Ellipse represents an SVG <ellipse> element.
type Group ¶
type Group struct {
Style StyleAttrs
Transform string
Children []Element
}
Group represents an SVG <g> element.
type Line ¶
type Line struct {
X1, Y1 float64
X2, Y2 float64
Style StyleAttrs
Transform string
}
Line represents an SVG <line> element.
type Matrix ¶
type Matrix [6]float64
Matrix is a 2D affine transformation matrix in the form:
[a c e] [b d f] [0 0 1]
This matches both SVG's matrix(a,b,c,d,e,f) and PDF's a b c d e f cm.
func ParseTransform ¶
ParseTransform parses an SVG transform attribute value into a combined matrix. Supported: matrix(), translate(), scale(), rotate(), skewX(), skewY(). Multiple transforms are applied left to right (as per SVG spec).
func RotateAround ¶
RotateAround returns a rotation matrix around point (cx, cy).
func (Matrix) PDFOperator ¶
PDFOperator returns the PDF content stream operator for this matrix.
type Path ¶
type Path struct {
D string
Style StyleAttrs
Transform string
}
Path represents an SVG <path> element.
type PathCommand ¶
type PathCommand struct {
Cmd byte // Command letter: M, L, H, V, C, S, Q, T, A, Z (upper=absolute, lower=relative)
Args []float64 // Coordinate arguments
}
PathCommand represents a single SVG path command with its arguments.
func ExpandShorthands ¶
func ExpandShorthands(cmds []PathCommand) []PathCommand
ExpandShorthands expands S/T shorthand commands into full C/Q commands by computing the reflected control point from the previous command.
func ParsePathData ¶
func ParsePathData(d string) ([]PathCommand, error)
ParsePathData parses an SVG path data string (the "d" attribute) into a sequence of path commands.
Supported commands: M, L, H, V, C, S, Q, T, A, Z and their relative (lowercase) variants. Implicit command repetition is handled: extra coordinate pairs after M are treated as L, and similarly for other commands.
func ResolveToAbsolute ¶
func ResolveToAbsolute(cmds []PathCommand) []PathCommand
ResolveToAbsolute converts all relative commands to absolute coordinates. This simplifies rendering since PDF only uses absolute coordinates.
type Polygon ¶
type Polygon struct {
Points []Point
Style StyleAttrs
Transform string
}
Polygon represents an SVG <polygon> element.
type Polyline ¶
type Polyline struct {
Points []Point
Style StyleAttrs
Transform string
}
Polyline represents an SVG <polyline> element.
type Rect ¶
type Rect struct {
X, Y float64
Width, Height float64
Rx, Ry float64
Style StyleAttrs
Transform string
}
Rect represents an SVG <rect> element.
type RenderOptions ¶
type RenderOptions struct {
// Width and Height specify the output size in PDF points.
// If zero, the SVG's own dimensions are used (1 SVG user unit = 1 PDF point).
Width, Height float64
// TextRenderer handles <text> elements. If nil, text is skipped.
TextRenderer TextRenderer
}
RenderOptions controls PDF rendering.
type StyleAttrs ¶
type StyleAttrs struct {
Fill string
FillOpacity string
Stroke string
StrokeWidth string
StrokeOpacity string
StrokeLinecap string
StrokeLinejoin string
StrokeDasharray string
StrokeDashoffset string
Opacity string
FillRule string
ClipPath string
}
StyleAttrs holds SVG presentation attributes.
type Text ¶
type Text struct {
X, Y float64
Content string
FontFamily string
FontSize float64
FontWeight string
FontStyle string
TextAnchor string // "start", "middle", "end"
Style StyleAttrs
Transform string
}
Text represents an SVG <text> element. Text rendering requires an external shaper (e.g. textshape).
type TextRenderer ¶
type TextRenderer interface {
// RenderText returns PDF operators to render text at the given position.
// fontSize is in PDF points. textAnchor is "start", "middle", or "end".
// The returned string is inserted into the content stream as-is.
RenderText(text string, x, y, fontSize float64, fontFamily, fontWeight, fontStyle, textAnchor string, fill Color) string
}
TextRenderer is implemented by external text shapers (e.g. textshape) to render SVG <text> elements into PDF operators. If nil, text elements are skipped.