Articles and news about Common Lisp

Menu

Generating SVG from Lisp is easy!

There are libraries dedicated to the task of generating SVG (Simple Vector Graphics) from Lisp. Namely the cl-svg. But they might offer a limited subset of the SVG standard.

Since SVG is an application of XML and XML is closely related to both SGML and HTML/XHTML one could suggest that it might be possible to use standard full-featured HTML-generators. Indeed, it is, and here is how.

CL-WHO of Edi Weitz actually transforms a tree of S-exp into a sequence of commands that ouput the corresponding XML/SGML into the given stream. And this is exactly what is needed to produce SVG from Lisp!

Let’s define a stream where the SVG data will be serialized:

(defparameter *svg* *standard-output*)

Now let’s define a macro that will tune CL-WHO to do the job we want it to do: