Skip to content
xoPublic

About

Go package wrapping rust's resvg, supports rendering SVGs

Topics

Resources

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

60 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

resvg

This project is no longer under active development. Use github.com/xo/okage/svg instead. It is a Go port of resvg and of the components that resvg needs, so it has no cgo and no prebuilt archives. This repository stays up so that existing programs keep building.

resvg is a wrapper around rust's resvg c-api crate, used by usql to render SVGs. Useful for rendering SVGs.

Overview | Building Artifacts | Documents | About

Unit Tests Go Reference Discord Discussion

Overview

Install in the usual Go fashion:

$ go get github.com/xo/resvg@latest

Then 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>`)

Using on Windows

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.

The windows/arm64 archive is built for the aarch64-pc-windows-gnullvm target. Build with a clang based C compiler, such as llvm-mingw, because the archive needs libunwind.

Using on FreeBSD

The freebsd/amd64 archive links the FreeBSD base libraries libexecinfo, libkvm, libmemstat, libprocstat, libdevstat and libgcc_s. All of them ship with FreeBSD. Nobody has run the package on FreeBSD yet. See docs/BACKLOG.md.

Using on Alpine (musl)

Pass -tags musl when building for linux/amd64 or linux/arm64 on a musl-based distribution such as Alpine, and have libunwind-dev (or your distribution's equivalent) installed at link time:

$ go build -tags musl

See libresvg/README.md for why this needs a separate target and can't be automatic.

Building Artifacts

Notes for building the libresvg artifacts:

Using gen.sh

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

# -----------------------------------------------------------------------------------
# from the ~/src/cross directory:
#
# 1. 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:
#
#      perl -pi -e 's/FROM ubuntu:.*/FROM ubuntu:24.04/' docker/cross-toolchains/docker/Dockerfile.{x86_64,aarch64}-apple-darwin-cross
#
# 2. change the llvm version used in the darwin.sh script:
#
#      perl -pi -e 's/install_llvm 16/install_llvm 22/' 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-darwin

Then use:

$ ./gen.sh

This also (re)writes the go.mod/doc.go/lib.go files under libresvg/<target>/ and the root link_GOOS_GOARCH.go files -- one Go module per platform, so a build only downloads the archive for the platform it targets. See libresvg/README.md for why, and for ./gen.sh -m to regenerate just that scaffolding without a rebuild.

Manually

$ 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 resvg
$ ./gen.sh -m -t $(go env GOOS)_$(go env GOARCH)

To get the needed static dependencies (for Windows or other platforms):

$ cd resvg/crates/c-api
$ RUSTFLAGS="--print=native-static-libs" cargo build --release

Use

Documents

  • AGENTS.md holds the rules for a coding agent: the layout, the conventions and the commands that test the code. CLAUDE.md imports it.
  • CONTRIBUTING.md is for a person who changes the project, and says how to install the agent skills.
  • docs/PLAN.md holds the plan, every decision, and the open questions.
  • docs/BACKLOG.md holds the work that is known and not done.
  • libresvg/README.md says why each platform archive is a module of its own, and how to release one.

About

resvg was written primarily to support these projects:

  • vegagoja - a package to render Vega visualizations
  • usql - a universal command-line interface for SQL databases

About

Go package wrapping rust's resvg, supports rendering SVGs

Topics

Resources

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages