Working with SVG in Scheme

Kurt Nørmark, Department of Computer Science, Aalborg University, normark@cs.aau.dk

This work describes an embedding of SVG in the functional programming language Scheme. Using our approach, an author of an SVG material is supposed to write an SVG document as a Scheme program. Overall, this allows for a development process in which both SVG and the functional programming language is available at any time, and at any location in the source document.

An accurate mirror of the SVG markup language has been created via use of the LAML system. The SVG mirror in Scheme represents a systematic, syntactic mapping of the SVG language to Scheme forms. As an example, the XML fragment <el a1="v1" a2="v2">cont</e1> is equivalent to the Scheme expression (el 'a1 "v1" 'a2 "v2" "cont") , where el is the name of a mirror function. When an SVG Scheme expression is evaluated by the Scheme processor, an internal AST representation is created. In addition, and as part of the evaluation, the SVG expression is validated according the SVG DTD. Thus, any deviation from the SVG DTD is discovered when the Scheme program generates the SVG code. Many efforts have been spent on a smooth integration between list structures and XML forms in Scheme. The LAML tool used to create the validating mirror of SVG in Scheme has been used the last five years on a number of different XML languages, including several versions of HTML. A recent paper, published in Journal of Functional programming, describes the approach in more details.

The work with SVG in Scheme has a number of advantages compared with authoring of plain SVG markup. Scheme expressions and SVG mirror expressions can be mixed arbitrarily. Hereby it is possible to use the power of the Scheme programming language to deal with the complexities in an SVG document. First and foremost, it is possible to create functional abstractions in an SVG document. This implies that any well-formed SVG fragment, and any part of an SVG attribute list, can be encapsulated in a parameterized functional abstraction. As a particular aspect, it is possible to create and implement new, domain-specific SVG elements. Repetitive document aspects can be handled by recursive or iterative processing in Scheme. Conditional document parts can be handled by the conditional language constructs in Scheme. Details in the SVG document that appear several times can be factored out, and they can be bound to local names via Scheme name binding forms. Those parts of SVG which are represented textually in attributes can be dealt with by Scheme data abstractions. In that way it is possible to provide for a structural representation of the d attribute of SVG paths, and other similar attributes.

In the paper we will describe the general idea of working with SVG in a functional programming language. As a concrete and significant case, the paper will focus on a Scheme library (still in active development) that supports the drawing of animated graphs in SVG. The library relies on the mirror of SVG in Scheme, and it provides a number of new SVG elements (such as svg-graph, svg-node, svg-composite-node, and svg-edge ) for the graph domain. The new elements are created as XML abstractions in LAML and Scheme. Different kinds of predefined animations make it easy to highlight nodes and edges in some given order, and to manage the animations by some simple means of control.

Selected links with additional information:

  1. The LAML Home Page
  2. The LAML Software
  3. Web Programming in Scheme with LAML, Journal of Functional Programming, 15 (1): 53-65, January 2005.
  4. Reference Manual of the SVG 1.1 Mirror Library
  5. The SVG Graph Extension Library
  6. Sample Animated Graph Abstractions and the underlying Scheme/LAML source