2 Home
dxtr edited this page 2026-10-18 02:22:12 +02:00

cl-svgread

cl-svgread reads the part of SVG that icons and simple vector art use: paths and the basic shapes, filled and stroked with solid colours, with opacity and transforms. It resolves styles, inheritance, units, the view box and transforms, as resvg's parser does, and gives each shape its curves as plain arrays in its own coordinates, the transform to the drawing's, its fill and its stroke. It strokes and rasterizes nothing itself. Its optional system cl-svgread/ft2 rasterizes drawings into RGBA images with FreeType, through cl-ft2.

cl-svgread is one of the libraries of the Pyssel game engine, whose editor draws its icons with it, but it does not depend on the engine and is usable on its own.

A first look

(asdf:load-system :cl-svgread)

(let* ((drawing (cl-svgread:read-svg
                 "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'>
                    <rect x='2' y='2' width='12' height='12' rx='2' fill='#4a90d9'/>
                    <path d='M5 8 L7 10 L11 6' fill='none' stroke='currentColor'
                          stroke-width='1.5' stroke-linecap='round'/>
                  </svg>"))
       (check (aref (cl-svgread:drawing-shapes drawing) 1)))
  (list (cl-svgread:drawing-width drawing)
        (cl-svgread:drawing-height drawing)
        (length (cl-svgread:drawing-shapes drawing))
        (cl-svgread:shape-fill check)
        (cl-svgread:shape-stroke check)
        (cl-svgread:shape-stroke-width check)
        (cl-svgread:shape-stroke-line-cap check)
        (cl-svgread:path-contours (cl-svgread:shape-path check))))
;; => (16.0 16.0 2 NIL :CURRENT-COLOR 1.5 :ROUND
;;     (((:LINE 5.0 8.0 7.0 10.0) (:LINE 7.0 10.0 11.0 6.0))))

The tick is stroked with :current-color, SVG's currentColor, which no element of the icon sets: whoever draws it chooses the colour, as a theme colours its icons. To turn the drawing into pixels, see Rasterizing.

Pages

Using cl-svgread

  • Getting Started: the systems, requirements, installing with stift, running the tests.
  • Reading SVG: what read-svg takes, which parts of SVG it reads and which it leaves out.
  • Drawings and Shapes: a drawing's size and shapes; paints, colours, currentColor and opacity; strokes; transforms and the view box.
  • Paths: a shape's curves as plain arrays, the contours as lists, and handing them to FreeType, cl-sdf or cl-textrender.
  • Rasterizing: cl-svgread/ft2's rasterize, into RGBA images.

Reference

At a glance

  • Dependencies: xmls. cl-svgread/ft2 also needs cl-ft2 and FreeType 2.13.1 or newer.
  • Lisp: SBCL.
  • Threads: read-svg keeps no state between calls, and drawings never change once read, so threads may read and share them freely.
  • License: MIT.
  • Source: https://fakehacker.cc/dxtr/cl-svgread