Next: Other Image Types, Previous: ImageMagick Images, Up: 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.
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.
Create a gradient in svg with identifier id. type specifies the gradient type, and can be either
linear
orradial
. 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
:stroke-color
:fill-color
:id
:gradient
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")
Add to svg a circle whose center is at x/y and whose radius is radius.
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.
Add to svg a line that starts at x1/y1 and extends to x2/y2.
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")
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")
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)
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")
Finally, the
svg-image
takes an SVG object as its argument and returns an image object suitable for use in functions likeinsert-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)))