okage draws 2D graphics in pure Go. It fills and strokes paths, renders
SVG files, draws Lottie animations, lays out text and math, and writes PDF
files. It needs the Go toolchain and nothing else: no cgo, no C library and
no WebAssembly runtime.
okage is a port of tiny-skia, the small 2D renderer of the Rust ecosystem, and of resvg, which renders SVG files with tiny-skia. The renderer gives the same bytes as tiny-skia. okage also ports the projects around them, such as velato for Lottie and krilla for PDF. Packages lists each one.
Add okage to a Go module with this command:
go get github.com/xo/okageA program that imports okage builds with CGO_ENABLED=0 for each platform
that Go supports.
Each example below is a part of a test in test/readme. The test also
draws each image of this section. The example of Lottie draws one frame,
and the test draws four frames for its image.
The root package fills and strokes paths onto a Pixmap, with a solid
color, a gradient or a pattern. This example fills a circle with a radial
gradient, and strokes a curve with round dashes:
pixmap, _ := okage.NewPixmap(480, 240)
pixmap.Fill(okage.ColorFromRGBA8(255, 255, 255, 255))
// Fill a circle with a radial gradient.
circle, _ := okage.PathFromCircle(120, 120, 96)
paint := okage.DefaultPaint()
paint.AntiAlias = true
paint.Shader, _ = okage.NewRadialGradient(
okage.PointFromXY(84, 84), 0,
okage.PointFromXY(120, 120), 96,
[]okage.GradientStop{
okage.NewGradientStop(0, okage.ColorFromRGBA8(255, 214, 102, 255)),
okage.NewGradientStop(1, okage.ColorFromRGBA8(232, 89, 60, 255)),
},
okage.SpreadModePad,
okage.IdentityTransform(),
)
pixmap.FillPath(circle, &paint, okage.FillRuleWinding, okage.IdentityTransform(), nil)
// Stroke a curve with round dashes.
pb := okage.NewPathBuilder()
pb.MoveTo(250, 200)
pb.CubicTo(290, 20, 400, 20, 450, 200)
curve, _ := pb.Finish()
paint.SetColorRGBA8(59, 113, 96, 255)
stroke := okage.DefaultStroke()
stroke.Width = 10
stroke.LineCap = okage.LineCapRound
stroke.Dash, _ = okage.NewStrokeDash([]float32{18, 18}, 0)
pixmap.StrokePath(curve, &paint, &stroke, okage.IdentityTransform(), nil)
if err := pixmap.SavePNG("shapes.png"); err != nil {
return err
}svg.New makes a Renderer, which renders SVG data into an *image.RGBA
in one call:
data, err := os.ReadFile("docs/okage.svg")
if err != nil {
return err
}
r := svg.New(svg.WithWidth(480))
img, err := r.Render(data)
if err != nil {
return err
}A Renderer loads the fonts of the system on its first call. The package
also registers the format svg, so image.Decode reads SVG data.
svg shapes text with typesetting, which handles each script, the
direction of the text and the fallback from one font to the next. For an
image that is the same on each machine, give the fonts and turn off the
fonts of the system:
data := []byte(`<svg xmlns="http://www.w3.org/2000/svg" width="480" height="80">
<rect width="100%" height="100%" fill="white"/>
<text x="240" y="52" font-family="Noto Sans" font-size="32"
text-anchor="middle" fill="#3B7160">Hello, مرحبا, こんにちは</text>
</svg>`)
r := svg.New(
svg.WithLoadSystemFonts(false),
svg.WithFontFile(latin),
svg.WithFontFile(arabic),
svg.WithFontFile(japanese),
)
img, err := r.Render(data)
if err != nil {
return err
}svg.ShapeText gives the glyphs and the outlines of a run of text without
SVG data. The package typesetting/shaping gives the line breaks and the
shaping of whole paragraphs.
texmath lays out TeX math with the OpenType MATH table of a font, by the
rules of TeX. The package texmath/stixtwomath holds the font STIX Two
Math:
f, err := stixtwomath.Font()
if err != nil {
return err
}
tex := `x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`
box, err := texmath.LayoutTeX(tex, font.NewFace(f), 40, texmath.Display)
if err != nil {
return err
}
w := uint32(math.Ceil(box.Width)) + 32
h := uint32(math.Ceil(box.Height+box.Depth)) + 32
pixmap, _ := okage.NewPixmap(w, h)
pixmap.Fill(okage.ColorFromRGBA8(255, 255, 255, 255))
paint := okage.DefaultPaint()
paint.AntiAlias = true
box.Draw(pixmap, okage.TransformFromTranslate(16, 16+float32(box.Height)), &paint)Box.Path gives the outline of the formula as one path, for a writer of
SVG or PDF.
lottie reads Lottie files and dotLottie archives, and draws each frame:
c, err := lottie.Load(data)
if err != nil {
return nil, err
}
// Draw the middle frame at 256 by 256 pixels.
img, err := c.Image(c.FrameCount()/2, 256, 256)
if err != nil {
return nil, err
}The animation is the fire emoji of the Noto animated emoji, under the CC BY 4.0 license.
pdf writes PDF files, with paths, gradients, images and text with
subsets of the fonts. pdfsvg draws a tree of svg onto a page, so this
example converts an SVG file to PDF:
tree, err := svg.FromData(data)
if err != nil {
return nil, err
}
d := pdf.NewDocument(pdf.WithSVGGlyphs(pdfsvg.RenderGlyph))
ps, _ := pdf.PageSettingsFromWH(tree.Size().Width(), tree.Size().Height())
page := d.StartPage(ps)
pdfsvg.Draw(page.Surface(), tree, tree.Size())
page.Finish()
if err := d.Finish(&w); err != nil {
return nil, err
}| Package | What it does | Port of |
|---|---|---|
okage |
Fills and strokes paths, with gradients, patterns, masks and blend modes | tiny-skia |
path |
Builds, transforms, strokes and dashes paths | tiny-skia-path |
svg |
Parses SVG files into a tree, and renders the tree | resvg and usvg |
lottie |
Reads Lottie files and dotLottie archives, and draws their frames | velato |
filter |
Applies the filter effects of SVG and CSS to pixmaps, blurs masks and pixmaps, and changes the colors of pixmaps and images with a color matrix or transfer functions | filtra |
pdf |
Writes PDF files | krilla |
pdfsvg |
Draws a tree of svg onto a page of pdf |
krilla-svg |
texmath |
Parses TeX math and lays it out with the MATH table of a font | go-tex/math |
typesetting |
Reads fonts, shapes text, breaks lines and runs the bidi algorithm | a copy of go-text/typesetting |
html |
Parses HTML documents, and lays out the HTML-like labels of Graphviz | tinyhtml5 |
tinycss2 |
Parses CSS | tinycss2 |
cssselect2 |
Matches CSS selectors | cssselect2 |
webfont |
Converts WOFF, WOFF2 and EOT fonts to TrueType or OpenType data | none |
roxmltree |
Parses XML into a read-only tree | roxmltree |
kurbo |
Curves, shapes and their geometry | a subset of kurbo |
The root package imports only the Go standard library. svg also uses
golang.org/x/image/webp for WebP files.
okage differs from its upstreams only where a decision says so. docs/DIFFERENCES.md lists each difference, and each API that okage adds. docs/STATUS.md gives the state of each package, and the upstream commit that it follows.
okage uses SIMD where Go can, and its scalar code is the fallback. SIMD (single instruction, multiple data) means one instruction that works on several values at once. Go 1.27 has SIMD only as an experiment, so okage has two builds:
- The scalar build is the default.
- The SIMD build uses the package
simd/archsimdof Go on amd64, arm64 and WebAssembly. A program gets it when it builds withGOEXPERIMENT=simd.
GOEXPERIMENT=simd go build ./...Both builds give the same bytes for each image. On an amd64 CPU without AVX2, the SIMD build uses the scalar code. When Go makes SIMD a standard feature, the SIMD build becomes the default. docs/SIMD.md holds the details.
okage also builds with TinyGo, which always uses the scalar code. A build for WebAssembly is a goal that each package keeps in mind.
| Document | What it holds |
|---|---|
| docs/STATUS.md | the state of each package, and the upstream commits that okage follows |
| docs/DIFFERENCES.md | each difference from upstream, and each API that okage adds |
| docs/PLAN.md | the plan, the testing plan, the phases and the open questions |
| docs/UPSTREAM.md | the rules for porting each upstream repository and each change that it makes |
| docs/SIMD.md | the rules for the vector code, the two builds and the SIMD packages of Go |
| docs/BACKLOG.md | the work that is known and not done |
| docs/FORMATS.md | a survey of the work to support other vector formats, such as PDF, EPS and DWG |
| docs/decisions/ | each decision, one file each, with an index |
| CONTRIBUTING.md | how to change okage |
| AGENTS.md | the rules for a coding agent, which also hold for a person |
okage keeps the license of each upstream project. Each folder that holds
code from an upstream project holds the license of that project, in a file
LICENSE, NOTICE or OFL.txt. LICENSE in the root is the
BSD-3-Clause license of tiny-skia, and it covers the root package and
path.




