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 GradientStop
- type Group
- type Line
- type LinearGradient
- type Matrix
- type Paint
- type PaintKind
- type Path
- type PathCommand
- type Point
- type Polygon
- type Polyline
- type Rect
- type RenderOptions
- type ShadingRegistrar
- type ShadingRequest
- 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
Defs map[string]*LinearGradient // Gradient definitions keyed by id
}
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 GradientStop ¶ added in v0.0.4
GradientStop represents a single <stop> inside a gradient.
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 LinearGradient ¶ added in v0.0.4
type LinearGradient struct {
ID string
X1, Y1, X2, Y2 float64
GradientUnits string // "userSpaceOnUse" (default for this implementation)
GradientTransform Matrix // Identity if absent
Stops []GradientStop
}
LinearGradient is the parsed form of an SVG <linearGradient> element. Only the subset needed for PDF Shading Type 2 (axial shading) is captured; spreadMethod, xlink:href, and percentage coordinates against objectBoundingBox are out of scope for the first iteration.
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 Paint ¶ added in v0.0.4
type Paint struct {
Kind PaintKind
Color Color
GradientRef string // id of the gradient when Kind == PaintGradient
}
Paint is a resolved SVG paint value: either none, a uniform color, or a reference to a gradient in the document's Defs map.
type PaintKind ¶ added in v0.0.4
type PaintKind int
PaintKind discriminates a paint between "no fill", a flat color, and a gradient reference. Distinguishing the three at parse time keeps the renderer's mergeStyle free of url(#…) string inspection.
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
// ShadingRegistrar, if non-nil, receives gradient fills and returns the
// page-resource name the renderer should use in the content stream.
// When nil, gradient fills degrade to the first stop's flat color (or
// to "no fill" when the gradient has no stops).
ShadingRegistrar ShadingRegistrar
}
RenderOptions controls PDF rendering.
type ShadingRegistrar ¶ added in v0.0.4
type ShadingRegistrar interface {
RegisterShading(req ShadingRequest) string
}
ShadingRegistrar is implemented by callers (e.g. boxesandglue) to make a gradient available as a PDF Pattern resource. RegisterShading is invoked once per gradient fill encountered during rendering; it returns the resource name (without leading slash) that the renderer then writes as "/<name> scn" into the content stream. The same gradient may be registered multiple times — implementations may dedupe, but the renderer does not require it.
type ShadingRequest ¶ added in v0.0.4
type ShadingRequest struct {
Gradient *LinearGradient
CTM Matrix
}
ShadingRequest is the data the registrar needs to materialise a PDF shading-pattern object. CTM is the renderer's current transformation matrix at the point of fill; gradients use it together with their own GradientTransform to build the pattern-to-page mapping.
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.