@end lisp
@end defun
+@defun svg-embed svg image image-type datap &rest args
+Add an embedded (raster) image to @var{svg}. If @var{datap} is
+@code{nil}, @var{IMAGE} should be a file name; if not, it should be a
+binary string containing the image data. @var{image-type} should be a
+@acronym{MIME} image type, for instance @samp{"image/jpeg"}.
+
+@lisp
+(svg-embed svg "~/rms.jpg" "image/jpeg" nil
+ :width "100px" :height "100px"
+ :x "50px" :y "75px")
+@end lisp
+@end defun
+
Finally, the @code{svg-image} takes an SVG object as its parameter and
returns an image object suitable for use in functions like
@code{insert-image}. Here's a complete example that creates and
", "))
,@(svg--arguments svg args)))))
+(defun svg-embed (svg image image-type datap &rest args)
+ "Insert IMAGE into the SVG structure.
+IMAGE should be a file name if DATAP is nil, and a binary string
+otherwise. IMAGE-TYPE should be a MIME image type, like
+\"image/jpeg\" or the like."
+ (svg--append
+ svg
+ (dom-node
+ 'image
+ `((xlink:href . ,(svg--image-data image image-type datap))
+ ,@(svg--arguments svg args)))))
+
(defun svg--append (svg node)
(let ((old (and (dom-attr node 'id)
(dom-by-id svg
(dom-append-child svg node)))
(svg-possibly-update-image svg))
+(defun svg--image-data (image image-type datap)
+ (with-temp-buffer
+ (set-buffer-multibyte nil)
+ (if datap
+ (insert image)
+ (insert-file-contents image))
+ (base64-encode-region (point-min) (point-max) t)
+ (goto-char (point-min))
+ (insert "data:" image-type ";base64,")
+ (buffer-string)))
+
(defun svg--arguments (svg args)
(let ((stroke-width (or (plist-get args :stroke-width)
(dom-attr svg 'stroke-width)))