resvg is a wrapper around rust's resvg c-api crate, used by
usql to render SVGs. Useful for rendering SVGs.
Overview | Building Artifacts | TODO | About
Install in the usual Go fashion:
$ go get github.com/xo/resvg@latestThen use like the following:
package resvg_test
import (
"bytes"
"fmt"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func Example() {
img, err := resvg.Render(svgData)
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d", 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)
}
// Output:
// width: 400 height: 180
}
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>`)When using this library with Windows, the Go binary must be built statically:
$ go build -ldflags '-extldflags "-static"'Otherwise, the resvg.dll (not included) will need to be located next to the
built Go binary, or in the Windows system path.
Notes for building the libresvg artifacts:
Build Darwin images:
# get cross
$ git clone https://github.com/cross-rs/cross.git
$ git -C cross submodule update --init --remote
# grab sdk
$ export SDK='https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz'
$ curl -L -o cross/docker/$(basename "$SDK") "$SDK"
$ md5sum cross/docker/MacOSX11.3.sdk.tar.xz
22c0ed904531f6d7dee4f87c15f7cf47 cross/docker/MacOSX11.3.sdk.tar.xz
# build containers (from cross directory, make sure the sdk file is in the cross/docker directory)
$ cd cross
# -----------------------------------------------------------------------------------
# 1. edit docker/common.sh to comment out sed -i for ubuntu sources
#
# 2. update:
#
# docker/cross-toolchains/docker/Dockerfile.{x86_64,aarch64}-apple-darwin-cross
#
# to use the latest ubuntu:24.04 image, or use the base image the other base cross-rs images are using
#
# 3. change the llvm version used in the darwin.sh script:
# perl -pi -e 's/install_llvm 16/install_llvm 22/' cross/docker/cross-toolchains/docker/darwin.sh
# -----------------------------------------------------------------------------------
$ CROSS_CONTAINER_ENGINE_NO_BUILDKIT=1 cargo build-docker-image x86_64-apple-darwin-cross --build-arg 'MACOS_SDK_FILE=MacOSX11.3.sdk.tar.xz'
$ CROSS_CONTAINER_ENGINE_NO_BUILDKIT=1 cargo build-docker-image aarch64-apple-darwin-cross --build-arg 'MACOS_SDK_FILE=MacOSX11.3.sdk.tar.xz'
# add rust toolchains
$ rustup target add x86_64-apple-darwin
$ rustup target add aarch64-apple-darwinThen use:
$ ./gen.sh$ mkdir -p libresvg/$(go env GOOS)_$(go env GOARCH)
$ git clone https://github.com/linebender/resvg.git && cd resvg/crates/c-api
$ cargo build --release
$ cp ../../target/release/libresvg.a ../../../libresvg/$(go env GOOS)_$(go env GOARCH)
$ cd ../../../ && rm -rf resvgTo get the needed static dependencies (for Windows or other platforms):
$ cd resvg/crates/c-api
$ RUSTFLAGS="--print=native-static-libs" cargo build --releaseUse
- Rewrite as pure Go
resvg was written primarily to support these projects: