diff options
| -rw-r--r-- | doc/lispref/display.texi | 13 | ||||
| -rw-r--r-- | lisp/svg.el | 23 |
2 files changed, 36 insertions, 0 deletions
diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index a7c1d0992de..575cad89f83 100644 --- a/doc/lispref/display.texi +++ b/doc/lispref/display.texi | |||
| @@ -5350,6 +5350,19 @@ that describe the outer circumference of the polygon. | |||
| 5350 | @end lisp | 5350 | @end lisp |
| 5351 | @end defun | 5351 | @end defun |
| 5352 | 5352 | ||
| 5353 | @defun svg-embed svg image image-type datap &rest args | ||
| 5354 | Add an embedded (raster) image to @var{svg}. If @var{datap} is | ||
| 5355 | @code{nil}, @var{IMAGE} should be a file name; if not, it should be a | ||
| 5356 | binary string containing the image data. @var{image-type} should be a | ||
| 5357 | @acronym{MIME} image type, for instance @samp{"image/jpeg"}. | ||
| 5358 | |||
| 5359 | @lisp | ||
| 5360 | (svg-embed svg "~/rms.jpg" "image/jpeg" nil | ||
| 5361 | :width "100px" :height "100px" | ||
| 5362 | :x "50px" :y "75px") | ||
| 5363 | @end lisp | ||
| 5364 | @end defun | ||
| 5365 | |||
| 5353 | Finally, the @code{svg-image} takes an SVG object as its parameter and | 5366 | Finally, the @code{svg-image} takes an SVG object as its parameter and |
| 5354 | returns an image object suitable for use in functions like | 5367 | returns an image object suitable for use in functions like |
| 5355 | @code{insert-image}. Here's a complete example that creates and | 5368 | @code{insert-image}. Here's a complete example that creates and |
diff --git a/lisp/svg.el b/lisp/svg.el index c4f3270ea8a..c33b0923c26 100644 --- a/lisp/svg.el +++ b/lisp/svg.el | |||
| @@ -137,6 +137,18 @@ POINTS is a list of x/y pairs." | |||
| 137 | ", ")) | 137 | ", ")) |
| 138 | ,@(svg--arguments svg args))))) | 138 | ,@(svg--arguments svg args))))) |
| 139 | 139 | ||
| 140 | (defun svg-embed (svg image image-type datap &rest args) | ||
| 141 | "Insert IMAGE into the SVG structure. | ||
| 142 | IMAGE should be a file name if DATAP is nil, and a binary string | ||
| 143 | otherwise. IMAGE-TYPE should be a MIME image type, like | ||
| 144 | \"image/jpeg\" or the like." | ||
| 145 | (svg--append | ||
| 146 | svg | ||
| 147 | (dom-node | ||
| 148 | 'image | ||
| 149 | `((xlink:href . ,(svg--image-data image image-type datap)) | ||
| 150 | ,@(svg--arguments svg args))))) | ||
| 151 | |||
| 140 | (defun svg--append (svg node) | 152 | (defun svg--append (svg node) |
| 141 | (let ((old (and (dom-attr node 'id) | 153 | (let ((old (and (dom-attr node 'id) |
| 142 | (dom-by-id svg | 154 | (dom-by-id svg |
| @@ -147,6 +159,17 @@ POINTS is a list of x/y pairs." | |||
| 147 | (dom-append-child svg node))) | 159 | (dom-append-child svg node))) |
| 148 | (svg-possibly-update-image svg)) | 160 | (svg-possibly-update-image svg)) |
| 149 | 161 | ||
| 162 | (defun svg--image-data (image image-type datap) | ||
| 163 | (with-temp-buffer | ||
| 164 | (set-buffer-multibyte nil) | ||
| 165 | (if datap | ||
| 166 | (insert image) | ||
| 167 | (insert-file-contents image)) | ||
| 168 | (base64-encode-region (point-min) (point-max) t) | ||
| 169 | (goto-char (point-min)) | ||
| 170 | (insert "data:" image-type ";base64,") | ||
| 171 | (buffer-string))) | ||
| 172 | |||
| 150 | (defun svg--arguments (svg args) | 173 | (defun svg--arguments (svg args) |
| 151 | (let ((stroke-width (or (plist-get args :stroke-width) | 174 | (let ((stroke-width (or (plist-get args :stroke-width) |
| 152 | (dom-attr svg 'stroke-width))) | 175 | (dom-attr svg 'stroke-width))) |