2 Quick Start🔗ℹ

The four functions you’ll likely reach for first are svg-string->bitmap, svg-file->bitmap, svg-string->pict, and svg-file->pict read an SVG document from a string or a file, and get back either a bitmap% or a pict.

(require svg/svg racket/class)
 
(define bm
  (svg-string->bitmap
   #<<SVG
<svg width="160" height="120" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="gold"/>
      <stop offset="100%" stop-color="crimson"/>
    </linearGradient>
  </defs>
  <rect x="10" y="10" width="140" height="60" rx="10" fill="url(#g)"/>
  <circle cx="40" cy="95" r="18" fill="steelblue" stroke="black" stroke-width="2"/>
  <circle cx="80" cy="95" r="18" fill="seagreen" stroke="black" stroke-width="2"/>
  <circle cx="120" cy="95" r="18" fill="orange" stroke="black" stroke-width="2"/>
</svg>
SVG
   ))
 
(send bm save-file "out.png" 'png)

gradient rounded rect above a row of three colored circles

Swap svg-string->bitmap for svg-string->pict and you get a pict instead of a bitmap% one that composes with ordinary picts like disk, text, frame, and hc-append/vc-append, since it’s built on pict’s own dc constructor:

(require svg/svg pict)
 
(define badge (svg-string->pict "...")) ; same SVG string as above
 
(hc-append 16 (text "A pict:" '(bold) 16) (frame badge))

the same gradient badge, framed, next to a bold text label

svg-file->bitmap and svg-file->pict are the file-path equivalents; use them (rather than reading a file yourself and passing the string to svg-string->bitmap/svg-string->pict) whenever the SVG has relative <image> references, since only the file-path versions know what directory to resolve those against.

Filters, masks, and group opacity all compose the way you’d expect from a browser — a <filter> with feDropShadow, or a <g mask="..."> layered with a semi-transparent group, render correctly rather than as a rough approximation:

a purple circle and a coral rounded square, both with soft drop shadows a teal rectangle faded left-to-right by a mask, with two overlapping semi-transparent circles composited correctly as one unit above it

If all you need is one of those four top-level functions, you can stop reading here. The rest of this document covers the lower-level pieces (path-data parsing, paint resolution, transform matrices, and so on) that svg/svg also exports, in case you want to reuse a piece of the pipeline directly — say, to parse path data for your own purposes, or to resolve an SVG color string without rendering anything.