Documentation
¶
Overview ¶
Package resvg is a wrapper around rust's resvg c-api crate.
The archives are prebuilt and come from a companion module per platform, under github.com/xo/resvg/libresvg/. Each is pulled in by a build-tagged blank import in link_GOOS_GOARCH.go, so `go build` downloads only the one for the platform being built and the #cgo LDFLAGS live next to the archive they describe, in that module's lib.go. See libresvg/README.md.
Example ¶
package main
import (
"bytes"
"fmt"
"image"
"image/png"
"log"
"os"
)
func main() {
img, _, err := image.Decode(bytes.NewReader(svgData))
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
}
var svgData = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="400" height="180" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="50" y="20" width="150" height="150" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
Sorry, your browser does not support inline SVG.
</svg>`)
Output: width: 400 height: 180
Example (Background) ¶
package main
import (
"bytes"
"fmt"
"image/color"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func main() {
img, err := resvg.Render(svgData, resvg.WithBackground(color.White))
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_white.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
}
var svgData = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="400" height="180" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="50" y="20" width="150" height="150" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
Sorry, your browser does not support inline SVG.
</svg>`)
Output: width: 400 height: 180
Example (Distort) ¶
package main
import (
"bytes"
"fmt"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func main() {
img, err := resvg.Render(svgData, resvg.WithWidth(200), resvg.WithHeight(700))
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_distort.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
}
var svgData = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="400" height="180" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="50" y="20" width="150" height="150" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
Sorry, your browser does not support inline SVG.
</svg>`)
Output: width: 200 height: 700
Example (Fonts) ¶
Example_fonts renders an SVG containing text using a specific, embedded font rather than whatever happens to be installed on the system. Load a font file (or use WithFonts for in-memory font data), disable system fonts so the loaded one is actually used, and map any generic CSS family name the SVG's text uses (here, "sans-serif") to it -- without that mapping, text set to a generic family won't resolve to a specific loaded font just because system fonts are off.
package main
import (
"bytes"
"fmt"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func main() {
img, err := resvg.Render(svgDataText,
resvg.WithLoadSystemFonts(false),
resvg.WithFontFiles("testdata/fonts/DejaVuSans.ttf"),
resvg.WithSansSerifFamily("DejaVu Sans"),
)
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("text.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
}
var svgDataText = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="300" height="60" xmlns="http://www.w3.org/2000/svg" version="1.1">
<text x="10" y="40" font-family="sans-serif" font-size="24">Hello, resvg!</text>
</svg>`)
Output: width: 300 height: 60
Example (Render) ¶
package main
import (
"bytes"
"fmt"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func main() {
img, err := resvg.Render(svgData)
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_render.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
}
var svgData = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="400" height="180" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="50" y="20" width="150" height="150" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
Sorry, your browser does not support inline SVG.
</svg>`)
Output: width: 400 height: 180
Example (ScaleBestFit) ¶
package main
import (
"bytes"
"fmt"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func main() {
a, err := resvg.Render(svgData, resvg.WithScaleMode(resvg.ScaleBestFit), resvg.WithWidth(300))
if err != nil {
log.Fatal(err)
}
ab := a.Bounds()
fmt.Printf("width: %d height: %d\n", ab.Max.X, ab.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, a); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_bestfit_a.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
b, err := resvg.Render(svgData, resvg.WithScaleMode(resvg.ScaleBestFit), resvg.WithHeight(135))
if err != nil {
log.Fatal(err)
}
bb := b.Bounds()
fmt.Printf("width: %d height: %d\n", bb.Max.X, bb.Max.Y)
buf.Reset()
if err := png.Encode(buf, b); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_bestfit_b.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
}
var svgData = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="400" height="180" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="50" y="20" width="150" height="150" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
Sorry, your browser does not support inline SVG.
</svg>`)
Output: width: 300 height: 135 width: 300 height: 135
Index ¶
- Variables
- func Decode(r io.Reader) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, error)
- func Render(data []byte, opts ...Option) (*image.RGBA, error)
- func Version() string
- type ErrNo
- type Error
- type ImageRendering
- type Option
- func WithBackground(background color.Color) Option
- func WithCursiveFamily(cursiveFamily string) Option
- func WithDPI(dpi float32) Option
- func WithFantasyFamily(fantasyFamily string) Option
- func WithFontFamily(fontFamily string) Option
- func WithFontFiles(fontFiles ...string) Option
- func WithFontSize(fontSize float32) Option
- func WithFonts(fonts ...[]byte) Option
- func WithHeight(height int) Option
- func WithImageRendering(imageRendering ImageRendering) Option
- func WithLanguages(languages ...string) Option
- func WithLoadSystemFonts(loadSystemFonts bool) Option
- func WithMonospaceFamily(monospaceFamily string) Option
- func WithResourcesDir(resourcesDir string) Option
- func WithSansSerifFamily(sansSerifFamily string) Option
- func WithScaleMode(scaleMode ScaleMode) Option
- func WithSerifFamily(serifFamily string) Option
- func WithShapeRendering(shapeRendering ShapeRendering) Option
- func WithStylesheet(stylesheet string) Option
- func WithTextRendering(textRendering TextRendering) Option
- func WithTransform(a, b, c, d, e, f float32) Option
- func WithWidth(width int) Option
- type Resvg
- type ScaleMode
- type ShapeRendering
- type TextRendering
- type Tree
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Default = New()
Default is the default renderer.
Functions ¶
func DecodeConfig ¶ added in v0.3.0
DecodeConfig decodes a svg config from the reader.
Types ¶
type Error ¶ added in v0.6.0
type Error string
Error is a package error.
type ImageRendering ¶ added in v0.2.0
type ImageRendering int
ImageRendering is the image rendering mode.
const ( ImageRenderingOptimizeQuality ImageRendering = C.RESVG_IMAGE_RENDERING_OPTIMIZE_QUALITY ImageRenderingOptimizeSpeed ImageRendering = C.RESVG_IMAGE_RENDERING_OPTIMIZE_SPEED )
Image rendering modes.
type Option ¶ added in v0.2.0
type Option func(*Resvg)
Option is a resvg rendering option.
func WithBackground ¶ added in v0.2.3
WithBackground is a resvg option to set the fill background color.
func WithCursiveFamily ¶ added in v0.2.0
WithCursiveFamily is a resvg option to set the cursive family.
func WithFantasyFamily ¶ added in v0.2.0
WithFantasyFamily is a resvg option to set the fantasy family.
func WithFontFamily ¶ added in v0.2.0
WithFontFamily is a resvg option to set the font family.
func WithFontFiles ¶ added in v0.2.0
WithFontFiles is a resvg option to set font files.
func WithFontSize ¶ added in v0.2.0
WithFontSize is a resvg option to set the font size.
func WithHeight ¶ added in v0.5.0
WithHeight is a resvg option to set the height.
func WithImageRendering ¶ added in v0.2.0
func WithImageRendering(imageRendering ImageRendering) Option
WithImageRendering is a resvg option to set the image rendering mode.
func WithLanguages ¶ added in v0.2.0
WithLanguages is a resvg option to set the languages.
func WithLoadSystemFonts ¶ added in v0.2.0
WithLoadSystemFonts is a resvg option to load system fonts.
func WithMonospaceFamily ¶ added in v0.2.0
WithMonospaceFamily is a resvg option to set the monospace family.
func WithResourcesDir ¶ added in v0.2.0
WithResourcesDir is a resvg option to set the resources dir.
func WithSansSerifFamily ¶ added in v0.10.0
WithSansSerifFamily is a resvg option to set the sans-serif family.
func WithScaleMode ¶ added in v0.6.0
WithScaleMode is a resvg option to set scale mode.
func WithSerifFamily ¶ added in v0.2.0
WithSerifFamily is a resvg option to set the serif family.
func WithShapeRendering ¶ added in v0.2.0
func WithShapeRendering(shapeRendering ShapeRendering) Option
WithShapeRendering is a resvg option to set the shape rendering mode.
func WithStylesheet ¶ added in v0.7.0
WithStylesheet is a resvg option to set the stylesheet.
func WithTextRendering ¶ added in v0.2.0
func WithTextRendering(textRendering TextRendering) Option
WithTextRendering is a resvg option to set the text rendering mode.
func WithTransform ¶ added in v0.5.0
WithTransform is a resvg option to set the transform used.
type Resvg ¶ added in v0.2.0
type Resvg struct {
// contains filtered or unexported fields
}
Resvg wraps the resvg c-api to render svgs as standard a image.RGBA.
A Resvg's configuration is fixed by the Option values passed to New and never changes afterward: there is no method to alter it once created. That makes a *Resvg, including the package-level Default, safe to share across goroutines -- concurrent calls to Parse, Render, and ParseConfig only read the underlying C options, never write them, and resvg's C API supports parsing and rendering concurrently against the same, unmodified resvg_options. Close is the only method that mutates a Resvg. It is safe to call concurrently with the others: it blocks until any in-flight call completes, releases the C options, and causes every call afterward to return ErrClosed.
Default must not be closed: it backs the package-level Render, Decode, and DecodeConfig, and the image.Format handlers registered for "svg". Closing it breaks every caller of those for the remainder of the process.
func New ¶ added in v0.2.0
New creates a new resvg, applying opts and building its underlying C options immediately. The returned Resvg's configuration cannot be changed afterward; construct a new one with different opts instead.
func (*Resvg) Close ¶ added in v0.9.0
Close releases the C resources associated with r. Safe to call more than once, and safe to call concurrently with Parse, Render, and ParseConfig: it blocks until any of those already in flight complete. Every call to r, and to any Tree already obtained from r, returns ErrClosed afterward.
func (*Resvg) Parse ¶ added in v0.9.0
Parse parses svg data into a Tree using r's options. Parsing is the expensive part of rendering an SVG; keeping the returned Tree around lets a caller call Config and Render more than once without reparsing data. The Tree must be released with Close once it is no longer needed.
func (*Resvg) ParseConfig ¶ added in v0.3.0
ParseConfig parses the svg, returning an image config.
type ScaleMode ¶ added in v0.6.0
type ScaleMode uint8
ScaleMode is a scale mode.
type ShapeRendering ¶ added in v0.2.0
type ShapeRendering int
ShapeRendering is the shape rendering mode.
const ( ShapeRenderingOptimizeSpeed ShapeRendering = C.RESVG_SHAPE_RENDERING_OPTIMIZE_SPEED ShapeRenderingCrispEdges ShapeRendering = C.RESVG_SHAPE_RENDERING_CRISP_EDGES ShapeRenderingGeometricPrecision ShapeRendering = C.RESVG_SHAPE_RENDERING_GEOMETRIC_PRECISION )
Shape rendering modes.
type TextRendering ¶ added in v0.2.0
type TextRendering int
TextRendering is the text rendering mode.
const ( TextRenderingOptimizeSpeed TextRendering = C.RESVG_TEXT_RENDERING_OPTIMIZE_SPEED TextRenderingOptimizeLegibility TextRendering = C.RESVG_TEXT_RENDERING_OPTIMIZE_LEGIBILITY TextRenderingGeometricPrecision TextRendering = C.RESVG_TEXT_RENDERING_GEOMETRIC_PRECISION )
Text rendering modes.
type Tree ¶ added in v0.9.0
type Tree struct {
// contains filtered or unexported fields
}
Tree is an SVG parsed by Resvg.Parse. Parsing is independent of the scaling, background, and transform used to render it, so the same Tree can be rendered more than once -- via repeated calls to Render or Config -- without reparsing the source data. A Tree must be released with Close once it is no longer needed; a finalizer releases it otherwise, but that is not guaranteed to run promptly.
A Tree is tied to the Resvg that parsed it and reads that Resvg's Width, Height, ScaleMode, Background, and Transform each time Config or Render is called, so changing which Resvg produced it is not possible, but the caller can rely on those values as of the current Resvg -- they cannot change after New. Concurrent calls to Config and Render on the same Tree from multiple goroutines are safe; Close is safe to call concurrently with them and blocks until any in-flight call completes.
func (*Tree) Close ¶ added in v0.9.0
Close releases the C resources associated with t. Safe to call more than once, and safe to call concurrently with Config and Render: it blocks until any of those already in flight complete. Every call to t returns ErrClosed afterward.
Directories
¶
| Path | Synopsis |
|---|---|
|
libresvg
|
|
|
darwin_amd64
module
|
|
|
darwin_arm64
module
|
|
|
freebsd_amd64
module
|
|
|
linux_amd64
module
|
|
|
linux_amd64_musl
module
|
|
|
linux_arm
module
|
|
|
linux_arm64
module
|
|
|
linux_arm64_musl
module
|
|
|
windows_amd64
module
|
|
|
windows_arm64
module
|