Next: , Previous: ImageMagick Images, Up: Images

39.17.6 SVG Images

SVG (Scalable Vector Graphics) is an XML format for specifying images. If your Emacs build has SVG support, you can create and manipulate these images with the following functions from the svg.el library.

— Function: svg-create width height &rest args

Create a new, empty SVG image with the specified dimensions. args is an argument plist with you can specify following:

:stroke-width
The default width (in pixels) of any lines created.
:stroke
The default stroke color on any lines created.

This function returns an SVG object, a Lisp data structure that specifies an SVG image, and all the following functions work on that structure. The argument svg in the following functions specifies such an SVG object.

— Function: svg-gradient svg id type stops

Create a gradient in svg with identifier id. type specifies the gradient type, and can be either linear or radial. stops is a list of percentage/color pairs.

The following will create a linear gradient that goes from red at the start, to green 25% of the way, to blue at the end:

          (svg-gradient svg "gradient1" 'linear
                        '((0 . "red") (25 . "green") (100 . "blue")))

The gradient created (and inserted into the SVG object) can later be used by all functions that create shapes.

All the following functions take an optional list of keyword parameters that alter the various attributes from their default values. Valid attributes include:

:stroke-width
The width (in pixels) of lines drawn, and outlines around solid shapes.
:stroke-color
The color of lines drawn, and outlines around solid shapes.
:fill-color
The color used for solid shapes.
:id
The identified of the shape.
:gradient
If given, this should be the identifier of a previously defined gradient object.
— Function: svg-rectangle svg x y width height &rest args

Add to svg a rectangle whose upper left corner is at position x/y and whose size is width/height.

          (svg-rectangle svg 100 100 500 500 :gradient "gradient1")
— Function: svg-circle svg x y radius &rest args

Add to svg a circle whose center is at x/y and whose radius is radius.

— Function: svg-ellipse svg x y x-radius y-radius &rest args

Add to svg an ellipse whose center is at x/y, and whose horizontal radius is x-radius and the vertical radius is y-radius.

— Function: svg-line svg x1 y1 x2 y2 &rest args

Add to svg a line that starts at x1/y1 and extends to x2/y2.

— Function: svg-polyline svg points &rest args

Add to svg a multiple-segment line (a.k.a. “polyline”) that goes through points, which is a list of X/Y position pairs.

          (svg-polyline svg '((200 . 100) (500 . 450) (80 . 100))
                        :stroke-color "green")
— Function: svg-polygon svg points &rest args

Add a polygon to svg where points is a list of X/Y pairs that describe the outer circumference of the polygon.

          (svg-polygon svg '((100 . 100) (200 . 150) (150 . 90))
                       :stroke-color "blue" :fill-color "red")
— Function: svg-text svg text &rest args

Add the specified text to svg.

          (svg-text
           svg "This is a text"
           :font-size "40"
           :font-weight "bold"
           :stroke "black"
           :fill "white"
           :font-family "impact"
           :letter-spacing "4pt"
           :x 300
           :y 400
           :stroke-width 1)
— Function: svg-embed svg image image-type datap &rest args

Add an embedded (raster) image to svg. If datap is nil, image should be a file name; otherwise it should be a string containing the image data as raw bytes. image-type should be a MIME image type, for instance "image/jpeg".

          (svg-embed svg "~/rms.jpg" "image/jpeg" nil
                     :width "100px" :height "100px"
                     :x "50px" :y "75px")
— Function: svg-remove svg id

Remove the element with identifier id from the svg.

— Function: svg-image svg

Finally, the svg-image takes an SVG object as its argument and returns an image object suitable for use in functions like insert-image.

Here's a complete example that creates and inserts an image with a circle:

     (let ((svg (svg-create 400 400 :stroke-width 10)))
       (svg-gradient svg "gradient1" 'linear '((0 . "red") (100 . "blue")))
       (svg-circle svg 200 200 100 :gradient "gradient1"
                       :stroke-color "green")
       (insert-image (svg-image svg)))