1 ;; Common Lisp bindings for Cairo
2 ;; Copyright 2005 Espen S. Johnsen <espen@users.sf.net>
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:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
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.
23 ;; $Id: cairo.lisp,v 1.13 2007/06/04 10:42:53 espen Exp $
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28 (define-enum-type surface-format :argb32 :rgb24 :a8 :a1)
29 (define-enum-type content :color :alpha :color-alpha)
30 (define-enum-type svg-version :svg-1.1 :svg-1.2)
32 (define-enum-type status
33 :success :no-memory :invalid-restore :invalid-pop-group
34 :no-current-point :invalid-matrix :invalid-status :null-pointer
35 :invalid-string :invalid-path-data :read-error :write-error
36 :surface-finished :surface-type-mismatch :pattern-type-mismatch
37 :invalid-content :invalid-format :invalid-visual :file-not-found
40 (define-enum-type fill-rule :winding :even-odd)
41 (define-enum-type line-cap :butt :round :square)
42 (define-enum-type line-join :miter :round :bevel)
43 (define-enum-type font-slant :normal :itaic :oblique)
44 (define-enum-type font-weight :normal :bold)
46 (define-enum-type operator
47 :clear :source :over :in :out :atop :dest :dest-over
48 :dest-in :dest-out :dest-atop :xor :add :saturate)
50 (define-enum-type antialias :default :none :gray :subpixel)
51 (define-enum-type extend :none :repeat :reflect)
52 (define-enum-type filter :fast :good :best :nearest :bilinear :gaussian)
53 (define-enum-type subpixel-order :default :rgb :bgr :vrgb :vbgr)
54 (define-enum-type hint-style :default :none :slight :medium :full)
55 (define-enum-type hint-metrics :default :off :on)
58 (define-enum-type surface-type
59 image-surface pdf-surface ps-surface xlib-surface xcb-surface
60 glitz-surface quartz-surface win32-surface beos-surface
61 directfb-surface svg-surface os2-surface)
63 (defclass surface-class (proxy-class)
66 (defmethod validate-superclass ((class surface-class) (super standard-class))
67 (subtypep (class-name super) 'surface))
69 (defclass glyph (struct)
85 (:metaclass struct-class))
87 (defclass font-face (ref-counted-object)
89 (:metaclass proxy-class)
90 (:ref %font-face-reference)
91 (:unref %font-face-destroy))
93 (defclass font-options (ref-counted-object)
96 :getter "font_options_get_antialias"
97 :setter "font_options_set_antialias"
98 :accessor font-options-antialias
102 :getter "font_options_get_subpixel_order"
103 :setter "font_options_set_subpixel_order"
104 :accessor font-options-subpixel-order
105 :type subpixel-order)
108 :getter "font_options_get_hint_style"
109 :setter "font_options_set_hint_style"
110 :accessor font-options-hint-style
114 :getter "font_options_get_hint_metrics"
115 :setter "font_options_set_hint_metrics"
116 :accessor font-options-hint-metrics
118 (:metaclass proxy-class)
119 (:ref %font-options-reference)
120 (:unref %font-options-destroy))
122 (defclass scaled-font (ref-counted-object)
124 (:metaclass proxy-class)
125 (:ref %scaled-font-reference)
126 (:unref %scaled-font-destroy))
128 (defclass matrix (struct)
129 ((xx :allocation :alien :initarg :xx :initform 1.0
130 :accessor matrix-xx :type double-float)
131 (yx :allocation :alien :initarg :yx :initform 0.0
132 :accessor matrix-yx :type double-float)
133 (xy :allocation :alien :initarg :xy :initform 1.0
134 :accessor matrix-xy :type double-float)
135 (yy :allocation :alien :initarg :yy :initform 0.0
136 :accessor matrix-yy :type double-float)
137 (x0 :allocation :alien :initarg :x0 :initform 0.0
138 :accessor matrix-x0 :type double-float)
139 (y0 :allocation :alien :initarg :y0 :initform 0.0
140 :accessor matrix-y0 :type double-float))
141 (:metaclass struct-class))
144 (defclass text-extents (struct)
145 ((x-bearing :allocation :alien :reader text-extents-x-bearing :type double-float)
146 (y-bearing :allocation :alien :reader text-extents-y-bearing :type double-float)
147 (width :allocation :alien :reader text-extents-width :type double-float)
148 (height :allocation :alien :reader text-extents-height :type double-float)
149 (x-advance :allocation :alien :reader text-extents-x-advance :type double-float)
150 (y-advance :allocation :alien :reader text-extents-y-advance :type double-float))
151 (:metaclass struct-class))
153 (defclass pattern (ref-counted-object)
156 :getter "cairo_pattern_get_extend"
157 :setter "cairo_pattern_set_extend"
158 :accessor pattern-extend
162 :getter "cairo_pattern_get_filter"
163 :setter "cairo_pattern_set_filter"
164 :accessor pattern-filter
168 :getter "cairo_pattern_get_matrix"
169 :setter "cairo_pattern_set_matrix"
170 :accessor pattern-matrix
172 (:metaclass proxy-class)
173 (:ref %pattern-reference)
174 (:unref %pattern-destroy))
177 (defclass surface (ref-counted-object)
180 :getter "cairo_surface_get_content"
181 :reader surface-content
183 (:metaclass surface-class))
185 (defclass image-surface (surface)
188 :getter "cairo_image_surface_get_data"
193 :getter "cairo_image_surface_get_format"
194 :reader surface-format
195 :type surface-format)
198 :getter "cairo_image_surface_get_width"
199 :reader surface-width
203 :getter "cairo_image_surface_get_height"
204 :reader surface-height
208 :getter "cairo_image_surface_get_stride"
209 :reader surface-height
211 (:metaclass surface-class))
213 (defclass xlib-surface (surface)
216 :getter "cairo_xlib_surface_get_width"
217 :reader surface-width
221 :getter "cairo_xlib_surface_get_height"
222 :reader surface-height
224 (:metaclass surface-class))
226 (defclass pdf-surface (surface)
228 (:metaclass surface-class))
230 (defclass ps-surface (surface)
232 (:metaclass surface-class))
234 (defclass svg-surface (surface)
236 (:metaclass surface-class))
239 (defclass context (ref-counted-object)
242 :getter "cairo_get_target"
247 :getter "cairo_get_source"
248 :setter "cairo_set_source"
253 :getter "cairo_get_antialias"
254 :setter "cairo_set_antialias"
259 :getter "cairo_get_tolerance"
260 :setter "cairo_set_tolerance"
265 :getter "cairo_get_fill_rule"
266 :setter "cairo_set_fill_rule"
271 :getter "cairo_get_line_width"
272 :setter "cairo_set_line_width"
277 :getter "cairo_get_line_cap"
278 :setter "cairo_set_line_cap"
283 :getter "cairo_get_line_join"
284 :setter "cairo_set_line_join"
289 :getter "cairo_get_miter_limit"
290 :setter "cairo_set_miter_limit"
291 :accessor miter-limit
295 :getter "cairo_get_font_matrix"
296 :setter "cairo_set_font_matrix"
297 :accessor font-matrix
301 :getter "cairo_get_font_options"
302 :setter "cairo_set_font_options"
303 :accessor font-options
307 :getter "cairo_get_font_face"
308 :setter "cairo_set_font_face"
313 :getter "cairo_get_operator"
314 :setter "cairo_set_operator"
320 :setter "cairo_set_matrix"
321 :writer (setf matrix)
324 (:metaclass proxy-class)
329 ;; (defclass path (proxy)
331 ;; (:metaclass proxy-class))
338 (defmethod allocate-foreign ((context context) &key target)
339 (%create-context target))
341 (defbinding (%create-context "cairo_create") () pointer
344 (defbinding %reference () pointer
347 (defbinding %destroy () nil
350 (defbinding (save-context "cairo_save") () nil
353 (defbinding (restore-context "cairo_restore") () nil
356 (defmacro with-context ((cr &optional var) &body body)
357 (let ((context (or var (make-symbol "CONTEXT"))))
358 `(let ((,context ,cr))
359 (save-context ,context)
362 (restore-context ,context)))))
364 (defbinding status () status
367 (defun ensure-color-component (component)
370 (integer (/ component 256.0))))
372 (defbinding (set-source-color "cairo_set_source_rgba") (cr red green blue &optional (alpha 1.0)) nil
374 ((ensure-color-component red) double-float)
375 ((ensure-color-component green) double-float)
376 ((ensure-color-component blue) double-float)
377 ((ensure-color-component alpha) double-float))
379 (defbinding set-source-surface (cr surface &optional (x 0.0) (y 0.0)) nil
385 (defun set-source (cr source)
387 (pattern (setf (source cr) source))
388 (surface (set-source-surface cr source))
389 (null (set-source-color cr 0.0 0.0 0.0))
390 (list (apply #'set-source-color cr source))
391 (vector (apply #'set-source-color cr (coerce source 'list)))))
393 (defbinding set-dash (cr dashes &optional (offset 0.0)) nil
395 (dashes (vector double-float))
396 ((length dashes) int)
397 (offset double-float))
399 (defbinding (paint "cairo_paint_with_alpha") (cr &optional (alpha 1.0)) nil
401 (alpha double-float))
403 (defbinding mask () nil
407 (defbinding mask-surface () nil
410 (surface-x double-float)
411 (surface-y double-float))
413 (defmacro defoperator (name &optional clip-p)
414 (let ((iname (intern (format nil "%~A" name)))
415 (pname (intern (format nil "%~A-PRESERVE" name))))
417 (defbinding ,iname () nil
419 (defbinding ,pname () nil
421 (defun ,name (cr &optional preserve)
426 (let ((tname (intern (format nil "IN-~A-P" name)))
427 (ename (intern (format nil "~A-EXTENTS" name))))
429 (defbinding ,tname () boolean
433 (defbinding ,ename () nil
435 (x1 double-float :out)
436 (y1 double-float :out)
437 (x2 double-float :out)
438 (y2 double-float :out))))))))
444 (defbinding reset-clip () nil
447 (defbinding copy-page () nil
450 (defbinding show-page () nil
456 (defbinding get-current-point () nil
458 (x double-float :out)
459 (y double-float :out))
461 (defbinding new-path () nil
464 #?(pkg-exists-p "cairo" :atleast-version "1.2")
465 (defbinding new-sub-path () nil
468 (defbinding close-path () nil
471 (defmacro defpath (name args &optional relative-p)
472 (flet ((def (name type)
474 ,(when (eq type 'optimized-double-float)
475 `(declaim (ftype (function (context ,@(loop repeat (length args) collect 'double-float))) ,(first name))))
476 (defbinding ,name () nil
478 ,@(mapcar #'(lambda (arg) (list arg type)) args)))))
481 ,(def name 'double-float)
482 ,(let ((name (intern (format nil "FAST-~A" name)))
483 (cname (gffi::default-alien-fname name)))
484 (def (list name cname) 'optimized-double-float))
486 (let* ((rel-name (intern (format nil "REL-~A" name)))
487 (fast-rel-name (intern (format nil "FAST-REL-~A" name)))
488 (cname (gffi::default-alien-fname rel-name)))
490 (def rel-name 'double-float)
491 (def (list fast-rel-name cname) 'optimized-double-float)))))))
494 (defpath arc (xc yc radius angle1 angle2))
495 (defpath arc-negative (xc yc radius angle1 angle2))
496 (defpath curve-to (x1 y1 x2 y2 x3 y3) t)
497 (defpath line-to (x y) t)
498 (defpath move-to (x y) t)
499 (defpath rectangle (x y width height))
501 (defun circle (cr x y radius &optional negative-p)
502 (move-to cr radius 0.0d0)
504 (arc-negative cr x y radius (* pi 2) 0.0d0)
505 (arc cr x y radius 0.0d0 (* pi 2)))
509 (defbinding glyph-path (cr glyphs) nil
511 (glyphs (vector glyph))
512 ((length glyphs) int))
514 (defbinding text-path () nil
522 (defbinding (pattern-add-color-stop "cairo_pattern_add_color_stop_rgba")
523 (pattern offset red green blue &optional (alpha 1.0)) nil
525 (offset double-float)
526 ((ensure-color-component red) double-float)
527 ((ensure-color-component green) double-float)
528 ((ensure-color-component blue) double-float)
529 ((ensure-color-component alpha) double-float))
531 (defbinding (pattern-create "cairo_pattern_create_rgba")
532 (red green blue &optional (alpha 1.0)) pattern
533 ((ensure-color-component red) double-float)
534 ((ensure-color-component green) double-float)
535 ((ensure-color-component blue) double-float)
536 ((ensure-color-component alpha) double-float))
538 (defbinding pattern-create-for-surface () pattern
541 (defbinding pattern-create-linear () pattern
547 (defbinding pattern-create-radial () pattern
550 (radius0 double-float)
553 (radius1 double-float))
555 (defbinding %pattern-reference () pointer
558 (defbinding %pattern-destroy () nil
561 (defbinding pattern-status () status
568 (defbinding translate () nil
573 (defbinding scale (cr sx &optional (sy sx)) nil
578 (defun scale-to-device (cr &optional keep-rotation-p)
580 (multiple-value-call #'scale cr (device-to-user-distance cr 1.0))
581 (multiple-value-bind (x y)
582 (multiple-value-call #'user-to-device cr (get-current-point cr))
583 ; (identity-matrix cr)
584 (setf (matrix cr) (matrix-init-identity))
585 (translate cr x y))))
587 (defbinding rotate () nil
589 (angle double-float))
591 (defbinding transform () nil
595 (defbinding (matrix "cairo_get_matrix") () nil
597 ((make-instance 'matrix) matrix :in/return))
599 (defbinding identity-matrix () nil
602 (defbinding user-to-device () nil
604 (x double-float :in/out)
605 (y double-float :in/out))
607 (defbinding user-to-device-distance (cr dx &optional (dy dx)) nil
609 (dx double-float :in/out)
610 (dy double-float :in/out))
612 (defbinding device-to-user () nil
614 (x double-float :in/out)
615 (y double-float :in/out))
617 (defbinding device-to-user-distance (cr dx &optional (dy dx)) nil
619 (dx double-float :in/out)
620 (dy double-float :in/out))
625 (defbinding select-font-face () nil
629 (weight font-weight))
631 (defbinding set-font-size () nil
635 (defbinding show-text () nil
639 (defbinding show-glyphs () nil
641 (glyphs (vector glyph))
642 ((length glyphs) int))
644 (defbinding font-extents () boolean
647 (defbinding text-extents (cr text &optional (extents (make-instance 'text-extents))) nil
650 (extents text-extents :in/return))
652 (defbinding glyph-extents (cr glyphs &optional (extents (make-instance 'text-extents))) nil
654 (glyphs (vector glyph))
655 ((length glyphs) int)
656 (extents text-extents :in/return))
661 (defbinding %font-face-reference () pointer
664 (defbinding %font-face-destroy () nil
667 (defbinding font-face-status () status
668 (font-face font-face))
674 (defbinding %scaled-font-reference () pointer
677 (defbinding %scaled-font-destroy () nil
680 (defbinding scaled-font-status () status
681 (scaled-font scaled-font))
683 (defbinding scaled-font-extents (scaled-font &optional (extents (make-instance 'text-extents))) nil
684 (scaled-font scaled-font)
685 (extents text-extents :in/return))
687 (defbinding scaled-font-glyph-extents (scaled-font glyphs &optional (extents (make-instance 'text-extents))) nil
688 (scaled-font scaled-font)
689 (glyphs (vector glyph))
690 ((length glyphs) int)
691 (extents text-extents :in/return))
693 (defbinding %scaled-font-create () pointer
694 (font-face font-face)
697 (options font-options))
699 (defmethod allocate-foreign ((scaled-font scaled-font) &key font-face font-matrix cmt options)
700 (%scaled-font-create font-face font-matrix cmt options))
707 (defbinding %font-options-copy () nil
710 (defbinding %font-options-destroy () nil
713 (defbinding font-options-status () status
714 (font-options font-options))
716 (defbinding %font-options-create () pointer)
718 (defmethod allocate-foreign ((font-options font-options) &rest initargs)
719 (declare (ignore initargs))
720 (%font-options-create))
722 (defbinding font-options-merge () nil
723 (options1 font-options :in/return)
724 (options2 font-options))
726 (defbinding font-options-hash () unsigned-int
727 (options font-options))
729 (defbinding font-options-equal-p () boolean
730 (options1 font-options)
731 (options2 font-options))
737 (defmethod make-proxy-instance :around ((class surface-class) location
739 (let ((class (find-class (%surface-get-type location))))
740 (apply #'call-next-method class location initargs)))
742 (defbinding %surface-get-type () surface-type
745 (defbinding %surface-reference () pointer
748 (defbinding %surface-destroy () nil
751 (defmethod reference-function ((class surface-class))
752 (declare (ignore class))
753 #'%surface-reference)
755 (defmethod unreference-function ((class surface-class))
756 (declare (ignore class))
759 (defbinding %surface-set-user-data (surface key data-id) status
761 ((quark-intern key) pointer-data)
762 (data-id pointer-data)
763 (user-data-destroy-callback callback))
765 (defmethod (setf user-data) (data (surface surface) key)
766 (%surface-set-user-data (foreign-location surface) key (register-user-data data))
769 (defbinding %surface-get-user-data () pointer-data
773 (defmethod user-data ((surface surface) key)
774 (find-user-data (%surface-get-user-data surface (quark-intern key))))
776 (defbinding surface-create-similar () surface
778 (format surface-format )
782 (defbinding surface-finish () nil
785 (defbinding surface-flush () nil
788 (defbinding surface-get-font-options () nil
790 ((make-instance 'font-options) font-options :in/return))
792 (defbinding surface-set-device-offset () nil
794 (x-offset double-float)
795 (y-offset double-float))
797 (defbinding surface-status () status
800 (defbinding %surface-mark-dirty () nil
803 (defbinding %surface-mark-dirty-rectangle () nil
810 (defun surface-mark-dirty (surface &optional x y width height)
812 (%surface-mark-dirty-rectangle surface x y width height)
813 (%surface-mark-dirty surface)))
815 (defbinding surface-set-fallback-resolution () nil
817 (x-pixels-per-inch double-float)
818 (y-pixels-per-inch double-float))
820 (define-callback stream-write-func status
821 ((stream-id pointer-data) (data pointer) (length unsigned-int))
822 (let ((stream (find-user-data stream-id)))
825 (map-c-vector 'nil #'(lambda (octet) (write-byte octet stream))
826 data '(unsigned-byte 8) length))
827 ((or symbol function)
829 (map-c-vector 'vector #'identity data '(unsigned-byte 8) length)))))
835 ;; Should data be automatically freed when the surface is GCed?
836 (defmethod allocate-foreign ((surface image-surface)
837 &key filename width height stride format data)
839 (filename (%image-surface-create-from-png filename))
840 ((not data) (%image-surface-create format width height))
842 (%image-surface-create-for-data data format width height
845 (let ((element-size (cdr (assoc format '((:argb32 . 4) (:rgb24 . 4) (:a8 . 1) (:a1 1/8))))))
846 (ceiling (* width element-size))))))))
849 (defbinding %image-surface-create () pointer
850 (format surface-format)
854 (defbinding %image-surface-create-for-data () pointer
856 (format surface-format)
861 (defbinding %image-surface-create-from-png () pointer
864 (defbinding surface-write-to-png () status
871 (defmethod allocate-foreign ((surface pdf-surface)
872 &key filename stream width height)
874 ((and filename stream)
875 (error "Only one of the arguments :filename and :stream may be specified"))
876 (filename (%pdf-surface-create filename width height))
878 (let* ((stream-id (register-user-data stream))
879 (location (%pdf-surface-create-for-stream stream-id width height)))
880 (%surface-set-user-data location 'stream stream-id)
884 (defbinding %pdf-surface-create () pointer
887 (height double-float))
889 (defbinding %pdf-surface-create-for-stream (stream width height) pointer
890 (stream-write-func callback)
891 (stream pointer-data)
893 (height double-float))
895 (defbinding pdf-surface-set-size () nil
896 (surface pdf-surface)
898 (height double-float))
903 (defmethod allocate-foreign ((surface ps-surface)
904 &key filename stream width height)
906 ((and filename stream)
907 (error "Only one of the arguments :filename and :stream may be specified"))
908 (filename (%ps-surface-create filename width height))
910 (let* ((stream-id (register-user-data stream))
911 (location (%ps-surface-create-for-stream stream-id width height)))
912 (%surface-set-user-data location 'stream stream-id)
915 (defbinding %ps-surface-create () pointer
918 (height double-float))
920 (defbinding %ps-surface-create-for-stream (stream width height) pointer
921 (stream-write-func callback)
922 (stream pointer-data)
924 (height double-float))
926 (defbinding ps-surface-set-size () nil
929 (height double-float))
931 (defbinding ps-surface-dsc-begin-setup () nil
932 (surface ps-surface))
934 (defbinding ps-surface-dsc-begin-page-setup () nil
935 (surface ps-surface))
937 (defbinding ps-surface-dsc-comment () nil
944 (defmethod allocate-foreign ((surface svg-surface)
945 &key filename stream width height)
947 ((and filename stream)
948 (error "Only one of the arguments :filename and :stream may be specified"))
949 (filename (%svg-surface-create filename width height))
951 (let* ((stream-id (register-user-data stream))
952 (location (%svg-surface-create-for-stream stream-id width height)))
953 (%surface-set-user-data location 'stream stream-id)
956 (defbinding %svg-surface-create () pointer
959 (height double-float))
961 (defbinding %svg-surface-create-for-stream (stream width height) pointer
962 (stream-write-func callback)
963 (stream pointer-data)
965 (height double-float))
967 (defbinding svg-surface-restrict-to-version () nil
968 (surface svg-surface)
969 (version svg-version))
975 (defbinding matrix-init () nil
976 (matrix matrix :in/return)
977 (xx double-float) (yx double-float)
978 (xy double-float) (yy double-float)
979 (x0 double-float) (y0 double-float))
981 (defbinding matrix-init-identity (&optional (matrix (make-instance 'matrix))) nil
982 (matrix matrix :in/return))
984 (defbinding matrix-init-translate () nil
985 (matrix matrix :in/return)
989 (defbinding matrix-init-scale () nil
990 (matrix matrix :in/return)
994 (defbinding matrix-init-rotate () nil
995 (matrix matrix :in/return)
996 (radians double-float))
998 (defbinding matrix-translate () nil
999 (matrix matrix :in/return)
1003 (defbinding matrix-scale () nil
1004 (matrix matrix :in/return)
1008 (defbinding matrix-rotate () nil
1009 (matrix matrix :in/return)
1010 (radians double-float))
1012 (defbinding matrix-invert () nil
1013 (matrix matrix :in/return))
1015 (defbinding matrix-multiply () nil
1016 (result matrix :out)
1020 (defbinding matrix-transform-distance () nil
1021 (matrix matrix :in/return)
1025 (defbinding matrix-transform-point () nil
1026 (matrix matrix :in/return)