diff options
| author | Lars Magne Ingebrigtsen | 2016-06-27 22:25:10 +0200 |
|---|---|---|
| committer | Lars Magne Ingebrigtsen | 2016-06-27 22:25:10 +0200 |
| commit | f7ea7aa11f6211b5142bbcfc41c580d75485ca56 (patch) | |
| tree | 465777c047077cf9a45b4e2220fd32e1de45f114 | |
| parent | 54fe3b6ec0557941c5759523b36bfdec21003f77 (diff) | |
| download | emacs-f7ea7aa11f6211b5142bbcfc41c580d75485ca56.tar.gz emacs-f7ea7aa11f6211b5142bbcfc41c580d75485ca56.zip | |
New functions svg-text and svg-remove
* doc/lispref/display.texi (SVG Images): Document svg-remove.
* doc/lispref/display.texi (SVG Images): Document svg-text.
* lisp/svg.el (svg-remove): New function.
(svg-text): New function.
| -rw-r--r-- | doc/lispref/display.texi | 22 | ||||
| -rw-r--r-- | lisp/svg.el | 42 |
2 files changed, 53 insertions, 11 deletions
diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index 575cad89f83..b7a6b570eb0 100644 --- a/doc/lispref/display.texi +++ b/doc/lispref/display.texi | |||
| @@ -5350,6 +5350,24 @@ 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-text svg text &rest args | ||
| 5354 | Add a text to @var{svg}. | ||
| 5355 | |||
| 5356 | @lisp | ||
| 5357 | (svg-text | ||
| 5358 | svg "This is a text" | ||
| 5359 | :font-size "40" | ||
| 5360 | :font-weight "bold" | ||
| 5361 | :stroke "black" | ||
| 5362 | :fill "white" | ||
| 5363 | :font-family "impact" | ||
| 5364 | :letter-spacing "4pt" | ||
| 5365 | :x 300 | ||
| 5366 | :y 400 | ||
| 5367 | :stroke-width 1) | ||
| 5368 | @end lisp | ||
| 5369 | @end defun | ||
| 5370 | |||
| 5353 | @defun svg-embed svg image image-type datap &rest args | 5371 | @defun svg-embed svg image image-type datap &rest args |
| 5354 | Add an embedded (raster) image to @var{svg}. If @var{datap} is | 5372 | 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 | 5373 | @code{nil}, @var{IMAGE} should be a file name; if not, it should be a |
| @@ -5363,6 +5381,10 @@ binary string containing the image data. @var{image-type} should be a | |||
| 5363 | @end lisp | 5381 | @end lisp |
| 5364 | @end defun | 5382 | @end defun |
| 5365 | 5383 | ||
| 5384 | @defun svg-remove svg id | ||
| 5385 | Remove the element with identifier @code{id} from the @code{svg}. | ||
| 5386 | @end defun | ||
| 5387 | |||
| 5366 | Finally, the @code{svg-image} takes an SVG object as its parameter and | 5388 | Finally, the @code{svg-image} takes an SVG object as its parameter and |
| 5367 | returns an image object suitable for use in functions like | 5389 | returns an image object suitable for use in functions like |
| 5368 | @code{insert-image}. Here's a complete example that creates and | 5390 | @code{insert-image}. Here's a complete example that creates and |
diff --git a/lisp/svg.el b/lisp/svg.el index c33b0923c26..a92c6dfb610 100644 --- a/lisp/svg.el +++ b/lisp/svg.el | |||
| @@ -27,6 +27,7 @@ | |||
| 27 | (require 'cl-lib) | 27 | (require 'cl-lib) |
| 28 | (require 'xml) | 28 | (require 'xml) |
| 29 | (require 'dom) | 29 | (require 'dom) |
| 30 | (require 'subr-x) | ||
| 30 | 31 | ||
| 31 | (defun svg-create (width height &rest args) | 32 | (defun svg-create (width height &rest args) |
| 32 | "Create a new, empty SVG image with dimensions WIDTHxHEIGHT. | 33 | "Create a new, empty SVG image with dimensions WIDTHxHEIGHT. |
| @@ -149,13 +150,22 @@ otherwise. IMAGE-TYPE should be a MIME image type, like | |||
| 149 | `((xlink:href . ,(svg--image-data image image-type datap)) | 150 | `((xlink:href . ,(svg--image-data image image-type datap)) |
| 150 | ,@(svg--arguments svg args))))) | 151 | ,@(svg--arguments svg args))))) |
| 151 | 152 | ||
| 153 | (defun svg-text (svg text &rest args) | ||
| 154 | "Add TEXT to SVG." | ||
| 155 | (svg--append | ||
| 156 | svg | ||
| 157 | (dom-node | ||
| 158 | 'text | ||
| 159 | `(,@(svg--arguments svg args)) | ||
| 160 | text))) | ||
| 161 | |||
| 152 | (defun svg--append (svg node) | 162 | (defun svg--append (svg node) |
| 153 | (let ((old (and (dom-attr node 'id) | 163 | (let ((old (and (dom-attr node 'id) |
| 154 | (dom-by-id svg | 164 | (dom-by-id svg |
| 155 | (concat "\\`" (regexp-quote (dom-attr node 'id)) | 165 | (concat "\\`" (regexp-quote (dom-attr node 'id)) |
| 156 | "\\'"))))) | 166 | "\\'"))))) |
| 157 | (if old | 167 | (if old |
| 158 | (dom-set-attributes old (dom-attributes node)) | 168 | (setcdr (car old) (cdr node)) |
| 159 | (dom-append-child svg node))) | 169 | (dom-append-child svg node))) |
| 160 | (svg-possibly-update-image svg)) | 170 | (svg-possibly-update-image svg)) |
| 161 | 171 | ||
| @@ -237,16 +247,26 @@ If the SVG is later changed, the image will also be updated." | |||
| 237 | 247 | ||
| 238 | (defun svg-print (dom) | 248 | (defun svg-print (dom) |
| 239 | "Convert DOM into a string containing the xml representation." | 249 | "Convert DOM into a string containing the xml representation." |
| 240 | (insert (format "<%s" (car dom))) | 250 | (if (stringp dom) |
| 241 | (dolist (attr (nth 1 dom)) | 251 | (insert dom) |
| 242 | ;; Ignore attributes that start with a colon. | 252 | (insert (format "<%s" (car dom))) |
| 243 | (unless (= (aref (format "%s" (car attr)) 0) ?:) | 253 | (dolist (attr (nth 1 dom)) |
| 244 | (insert (format " %s=\"%s\"" (car attr) (cdr attr))))) | 254 | ;; Ignore attributes that start with a colon. |
| 245 | (insert ">") | 255 | (unless (= (aref (format "%s" (car attr)) 0) ?:) |
| 246 | (dolist (elem (nthcdr 2 dom)) | 256 | (insert (format " %s=\"%s\"" (car attr) (cdr attr))))) |
| 247 | (insert " ") | 257 | (insert ">") |
| 248 | (svg-print elem)) | 258 | (dolist (elem (nthcdr 2 dom)) |
| 249 | (insert (format "</%s>" (car dom)))) | 259 | (insert " ") |
| 260 | (svg-print elem)) | ||
| 261 | (insert (format "</%s>" (car dom))))) | ||
| 262 | |||
| 263 | (defun svg-remove (svg id) | ||
| 264 | "Remove the element identified by ID from SVG." | ||
| 265 | (when-let ((node (car (dom-by-id | ||
| 266 | svg | ||
| 267 | (concat "\\`" (regexp-quote id) | ||
| 268 | "\\'"))))) | ||
| 269 | (dom-remove-node svg node))) | ||
| 250 | 270 | ||
| 251 | (provide 'svg) | 271 | (provide 'svg) |
| 252 | 272 | ||