1ed0a2c5 |
1 | ;; Common Lisp bindings for librsvg |
2 | ;; Copyright 2005 Espen S. Johnsen <espen@users.sf.net> |
3 | ;; |
4 | ;; Permission is hereby granted, free of charge, to any person obtaining |
5 | ;; a copy of this software and associated documentation files (the |
6 | ;; "Software"), to deal in the Software without restriction, including |
7 | ;; without limitation the rights to use, copy, modify, merge, publish, |
8 | ;; distribute, sublicense, and/or sell copies of the Software, and to |
9 | ;; permit persons to whom the Software is furnished to do so, subject to |
10 | ;; the following conditions: |
11 | ;; |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
14 | ;; |
15 | ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
21 | ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 | |
bcf02931 |
23 | ;; $Id: rsvg.lisp,v 1.9 2008-10-08 18:24:01 espen Exp $ |
1ed0a2c5 |
24 | |
25 | (in-package "RSVG") |
26 | |
27 | |
28 | (eval-when (:compile-toplevel :load-toplevel :execute) |
2ff48596 |
29 | (init-types-in-library rsvg "librsvg-2" :prefix "rsvg_") |
e3f606ca |
30 | |
2ff48596 |
31 | (define-types-by-introspection "Rsvg" |
32 | ("RsvgError" :ignore t))) |
e3f606ca |
33 | |
1ed0a2c5 |
34 | |
35 | (defbinding init () nil) |
36 | (defbinding term () nil) |
37 | |
e3f606ca |
38 | (defbinding (set-default-dpi "rsvg_set_default_dpi_x_y") (dpi-x &optional (dpi-y dpi-x)) nil |
1ed0a2c5 |
39 | (dpi-x double-float) |
40 | (dpi-y double-float)) |
41 | |
1ed0a2c5 |
42 | |
e3f606ca |
43 | (defbinding handle-write () boolean |
1ed0a2c5 |
44 | (handle handle) |
39551e64 |
45 | (data (vector (integer 8))) |
f9921dfd |
46 | ((length data) int) |
e3f606ca |
47 | (nil gerror-signal :out)) |
1ed0a2c5 |
48 | |
49 | (defbinding handle-close () boolean |
50 | (handle handle) |
e3f606ca |
51 | (nil gerror-signal :out)) |
1ed0a2c5 |
52 | |
e3f606ca |
53 | (defbinding (handle-get-pixbuf "rsvg_handle_get_pixbuf_sub") (handle &optional id) boolean |
54 | (handle handle) |
55 | (id (or null string))) |
1ed0a2c5 |
56 | |
1ed0a2c5 |
57 | |
e3f606ca |
58 | (defbinding %handle-new-from-data () pointer |
59 | (data string) |
f9921dfd |
60 | ((1- (utf8-length data)) int) |
e3f606ca |
61 | (nil gerror-signal :out)) |
1ed0a2c5 |
62 | |
e3f606ca |
63 | (defbinding %handle-new-from-file () pointer |
64 | (filename pathname) |
65 | (nil gerror-signal :out)) |
1ed0a2c5 |
66 | |
e3f606ca |
67 | (defmethod allocate-foreign ((handle handle) &key data filename) |
68 | (cond |
69 | (filename (%handle-new-from-file filename)) |
70 | (data (%handle-new-from-data data)) |
71 | (t (call-next-method)))) |
1ed0a2c5 |
72 | |
bcf02931 |
73 | (defmacro with-handle ((handle &rest args) &body body) |
74 | `(let ((,handle (make-instance 'handle ,@args))) |
75 | (unwind-protect |
76 | (progn ,@body) |
77 | (handle-close ,handle)))) |
1ed0a2c5 |
78 | |
79 | ;;; Cairo interface |
80 | |
e3f606ca |
81 | (defbinding (render-cairo "rsvg_handle_render_cairo_sub") (handle cr &optional id) nil |
82 | (handle handle) |
1ed0a2c5 |
83 | (cr cairo:context) |
e3f606ca |
84 | (id (or null string))) |
bcf02931 |
85 | |
86 | (defun image-surface-create-from-svg (filename &key width height (format :argb32)id) |
87 | (with-handle (handle :filename filename) |
88 | (multiple-value-bind (width height) |
89 | (cond |
90 | ((and width height) (values width height)) |
91 | (width |
92 | (let ((ratio (/ (handle-height handle) (handle-width handle)))) |
93 | (values width (truncate (* width ratio))))) |
94 | (height |
95 | (let ((ratio (/ (handle-width handle) (handle-height handle)))) |
96 | (values (truncate (* height ratio)) height))) |
97 | (t (values (handle-width handle) (handle-height handle)))) |
98 | (let ((image (make-instance 'cairo:image-surface |
99 | :width width :height height :format format))) |
100 | (cairo:with-surface (image cr) |
101 | (cairo:scale cr (/ width (handle-width handle)) (/ height (handle-height handle))) |
102 | (render-cairo handle cr id)) |
103 | image)))) |