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.8 2007-01-11 10:20:22 espen Exp $
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28 (define-enum-type surface-format :argb32 :rgb24 :a8 :a1)
29 #?(pkg-exists-p "cairo" :atleast-version "1.2")
30 (define-enum-type content :color :alpha :color-alpha)
31 #?(pkg-exists-p "cairo" :atleast-version "1.2")
32 (define-enum-type surface-type
33 :image :pdf :ps :xlib :xcb :glitz :quartz :win32 :beos :directfb
37 (define-enum-type status
38 :success :no-memory :invalid-restore :invalid-pop-group
39 :no-current-point :invalid-matrix :invalid-status :null-pointer
40 :invalid-string :invalid-path-data :read-error :write-error
41 :surface-finished :surface-type-mismatch :pattern-type-mismatch
42 :invalid-content :invalid-format :invalid-visual :file-not-found
45 (define-enum-type fill-rule :winding :even-odd)
46 (define-enum-type line-cap :butt :round :square)
47 (define-enum-type line-join :miter :round :bevel)
48 (define-enum-type font-slant :normal :itaic :oblique)
49 (define-enum-type font-weight :normal :bold)
51 (define-enum-type operator
52 :clear :source :over :in :out :atop :dest :dest-over
53 :dest-in :dest-out :dest-atop :xor :add :saturate)
55 (define-enum-type antialias :default :none :gray :subpixel)
56 (define-enum-type extend :none :repeat :reflect)
57 (define-enum-type filter :fast :good :best :nearest :bilinear :gaussian)
58 (define-enum-type subpixel-order :default :rgb :bgr :vrgb :vbgr)
59 (define-enum-type hint-style :default :none :slight :medium :full)
60 (define-enum-type hint-metrics :default :off :on)
62 (defclass glyph (struct)
78 (:metaclass struct-class))
80 (defclass font-face (proxy)
82 (:metaclass proxy-class)
83 (:ref %font-face-reference)
84 (:unref %font-face-destroy))
86 (defclass font-options (proxy)
89 :getter "font_options_get_antialias"
90 :setter "font_options_set_antialias"
91 :accessor font-options-antialias
95 :getter "font_options_get_subpixel_order"
96 :setter "font_options_set_subpixel_order"
97 :accessor font-options-subpixel-order
101 :getter "font_options_get_hint_style"
102 :setter "font_options_set_hint_style"
103 :accessor font-options-hint-style
107 :getter "font_options_get_hint_metrics"
108 :setter "font_options_set_hint_metrics"
109 :accessor font-options-hint-metrics
111 (:metaclass proxy-class)
112 (:ref %font-options-reference)
113 (:unref %font-options-destroy))
115 (defclass scaled-font (proxy)
117 (:metaclass proxy-class)
118 (:ref %scaled-font-reference)
119 (:unref %scaled-font-destroy))
121 (defclass matrix (struct)
122 ((xx :allocation :alien :initarg :xx :initform 1.0
123 :accessor matrix-xx :type double-float)
124 (yx :allocation :alien :initarg :yx :initform 0.0
125 :accessor matrix-yx :type double-float)
126 (xy :allocation :alien :initarg :xy :initform 1.0
127 :accessor matrix-xy :type double-float)
128 (yy :allocation :alien :initarg :yy :initform 0.0
129 :accessor matrix-yy :type double-float)
130 (x0 :allocation :alien :initarg :x0 :initform 0.0
131 :accessor matrix-x0 :type double-float)
132 (y0 :allocation :alien :initarg :y0 :initform 0.0
133 :accessor matrix-y0 :type double-float))
134 (:metaclass struct-class))
137 (defclass text-extents (struct)
138 ((x-bearing :allocation :alien :reader text-extents-x-bearing :type double-float)
139 (y-bearing :allocation :alien :reader text-extents-y-bearing :type double-float)
140 (width :allocation :alien :reader text-extents-width :type double-float)
141 (height :allocation :alien :reader text-extents-height :type double-float)
142 (x-advance :allocation :alien :reader text-extents-x-advance :type double-float)
143 (y-advance :allocation :alien :reader text-extents-y-advance :type double-float))
144 (:metaclass struct-class))
146 (defclass pattern (proxy)
149 :getter "cairo_pattern_get_extend"
150 :setter "cairo_pattern_set_extend"
151 :accessor pattern-extend
155 :getter "cairo_pattern_get_filter"
156 :setter "cairo_pattern_set_filter"
157 :accessor pattern-filter
161 :getter "cairo_pattern_get_matrix"
162 :setter "cairo_pattern_set_matrix"
163 :accessor pattern-matrix
165 (:metaclass proxy-class)
166 (:ref %pattern-reference)
167 (:unref %pattern-destroy))
170 (defclass surface (proxy)
171 (#?(pkg-exists-p "cairo" :atleast-version "1.2")
174 :getter "cairo_surface_get_tyoe"
177 #?(pkg-exists-p "cairo" :atleast-version "1.2")
180 :getter "cairo_surface_get_content"
181 :reader surface-content
183 (:metaclass proxy-class)
184 (:ref %surface-reference)
185 (:unref %surface-destroy))
187 (defclass context (proxy)
190 :getter "cairo_get_target"
195 :getter "cairo_get_source"
196 :setter "cairo_set_source"
201 :getter "cairo_get_antialias"
202 :setter "cairo_set_antialias"
207 :getter "cairo_get_tolerance"
208 :setter "cairo_set_tolerance"
213 :getter "cairo_get_fill_rule"
214 :setter "cairo_set_fill_rule"
219 :getter "cairo_get_line_width"
220 :setter "cairo_set_line_width"
225 :getter "cairo_get_line_cap"
226 :setter "cairo_set_line_cap"
231 :getter "cairo_get_line_join"
232 :setter "cairo_set_line_join"
237 :getter "cairo_get_miter_limit"
238 :setter "cairo_set_miter_limit"
239 :accessor miter-limit
243 :getter "cairo_get_font_matrix"
244 :setter "cairo_set_font_matrix"
245 :accessor font-matrix
249 :getter "cairo_get_font_options"
250 :setter "cairo_set_font_options"
251 :accessor font-options
255 :getter "cairo_get_font_face"
256 :setter "cairo_set_font_face"
261 :getter "cairo_get_operator"
262 :setter "cairo_set_operator"
268 :setter "cairo_set_matrix"
269 :writer (setf matrix)
272 (:metaclass proxy-class)
276 (defclass image-surface (surface)
279 :getter "cairo_image_surface_get_width"
280 :reader surface-width
284 :getter "cairo_image_surface_get_height"
285 :reader surface-height
287 (:metaclass proxy-class)
288 (:ref %surface-reference)
289 (:unref %surface-destroy))
292 ;; (defclass path (proxy)
294 ;; (:metaclass proxy-class))
301 (defbinding %reference () nil
304 (defbinding %destroy () nil
307 (defbinding (save-context "cairo_save") () nil
310 (defbinding (restore-context "cairo_restore") () nil
313 (defmacro with-context ((cr) &body body)
314 (let ((context (make-symbol "CONTEXT")))
315 `(let ((,context ,cr))
316 (save-context ,context)
319 (restore-context ,context)))))
321 (defbinding status () status
324 (defun ensure-color-component (component)
327 (integer (/ component 256.0))))
329 (defbinding (set-source-color "cairo_set_source_rgba") (cr red green blue &optional (alpha 1.0)) nil
331 ((ensure-color-component red) double-float)
332 ((ensure-color-component green) double-float)
333 ((ensure-color-component blue) double-float)
334 ((ensure-color-component alpha) double-float))
336 (defbinding set-source-surface (cr surface &optional (x 0.0) (y 0.0)) nil
342 (defun set-source (cr source)
344 (pattern (setf (source cr) source))
345 (surface (set-source-surface cr source))
346 (list (apply #'set-source-color cr source))
347 (vector (apply #'set-source-color cr (coerce source 'list)))
348 (null (set-source-color cr 0.0 0.0 0.0))))
350 (defbinding set-dash (cr dashes &optional (offset 0.0)) nil
352 (dashes (vector double-float))
353 ((length dashes) int)
354 (offset double-float))
356 (defbinding (paint "cairo_paint_with_alpha") (cr &optional (alpha 1.0)) nil
358 (alpha double-float))
360 (defbinding mask () nil
364 (defbinding mask-surface () nil
367 (surface-x double-float)
368 (surface-y double-float))
370 (defmacro defoperator (name &optional clip-p)
371 (let ((iname (intern (format nil "%~A" name)))
372 (pname (intern (format nil "%~A-PRESERVE" name))))
374 (defbinding ,iname () nil
376 (defbinding ,pname () nil
378 (defun ,name (cr &optional preserve)
383 (let ((tname (intern (format nil "IN-~A-P" name)))
384 (ename (intern (format nil "~A-EXTENTS" name))))
386 (defbinding ,tname () boolean
390 (defbinding ,ename () boolean
392 (x1 double-float :out)
393 (y1 double-float :out)
394 (x2 double-float :out)
395 (y2 double-float :out))))))))
401 (defbinding reset-clip () nil
404 (defbinding copy-page () nil
407 (defbinding show-page () nil
413 (defbinding get-current-point () nil
415 (x double-float :out)
416 (y double-float :out))
418 (defbinding new-path () nil
421 #?(pkg-exists-p "cairo" :atleast-version "1.2")
422 (defbinding new-sub-path () nil
425 (defbinding close-path () nil
428 (defmacro defpath (name args &optional relative-p)
429 (flet ((def (name type)
431 ,(when (eq type 'optimized-double-float)
432 `(declaim (ftype (function (context ,@(loop repeat (length args) collect 'double-float))) ,(first name))))
433 (defbinding ,name () nil
435 ,@(mapcar #'(lambda (arg) (list arg type)) args)))))
438 ,(def name 'double-float)
439 ,(let ((name (intern (format nil "FAST-~A" name)))
440 (cname (gffi::default-alien-fname name)))
441 (def (list name cname) 'optimized-double-float))
443 (let* ((rel-name (intern (format nil "REL-~A" name)))
444 (fast-rel-name (intern (format nil "FAST-REL-~A" name)))
445 (cname (gffi::default-alien-fname rel-name)))
447 (def rel-name 'double-float)
448 (def (list fast-rel-name cname) 'optimized-double-float)))))))
451 (defpath arc (xc yc radius angle1 angle2))
452 (defpath arc-negative (xc yc radius angle1 angle2))
453 (defpath curve-to (x1 y1 x2 y2 x3 y3) t)
454 (defpath line-to (x y) t)
455 (defpath move-to (x y) t)
456 (defpath rectangle (x y width height))
458 (defun circle (cr x y radius)
459 (arc cr x y radius 0.0 (* pi 2)))
461 (defbinding glyph-path (cr glyphs) nil
463 (glyphs (vector glyph))
464 ((length glyphs) int))
466 (defbinding text-path () nil
474 (defbinding (pattern-add-color-stop "cairo_pattern_add_color_stop_rgba")
475 (pattern offset red green blue &optional (alpha 1.0)) nil
477 (offset double-float)
478 ((ensure-color-component red) double-float)
479 ((ensure-color-component green) double-float)
480 ((ensure-color-component blue) double-float)
481 ((ensure-color-component alpha) double-float))
483 (defbinding (pattern-create "cairo_pattern_create_rgba")
484 (red green blue &optional (alpha 1.0)) pattern
485 ((ensure-color-component red) double-float)
486 ((ensure-color-component green) double-float)
487 ((ensure-color-component blue) double-float)
488 ((ensure-color-component alpha) double-float))
490 (defbinding pattern-create-for-surface () pattern
493 (defbinding pattern-create-linear () pattern
499 (defbinding pattern-create-radial () pattern
502 (radius0 double-float)
505 (radius1 double-float))
507 (defbinding %pattern-reference () nil
510 (defbinding %pattern-destroy () nil
513 (defbinding pattern-status () status
520 (defbinding translate () nil
525 (defbinding scale (cr sx &optional (sy sx)) nil
530 (defun scale-to-device (cr &optional keep-rotation-p)
532 (multiple-value-call #'scale cr (device-to-user-distance cr 1.0))
533 (multiple-value-bind (x y)
534 (multiple-value-call #'user-to-device cr (get-current-point cr))
536 (translate cr x y))))
538 (defbinding rotate () nil
540 (angle double-float))
542 (defbinding transform () nil
546 (defbinding (matrix "cairo_get_matrix") () nil
548 ((make-instance 'matrix) matrix :in/return))
550 (defbinding identity-matrix () nil
553 (defbinding user-to-device () nil
555 (x double-float :in/out)
556 (y double-float :in/out))
558 (defbinding user-to-device-distance (cr dx &optional (dy dx)) nil
560 (dx double-float :in/out)
561 (dy double-float :in/out))
563 (defbinding device-to-user () nil
565 (x double-float :in/out)
566 (y double-float :in/out))
568 (defbinding device-to-user-distance (cr dx &optional (dy dx)) nil
570 (dx double-float :in/out)
571 (dy double-float :in/out))
576 (defbinding select-font-face () nil
580 (weight font-weight))
582 (defbinding set-font-size () nil
586 (defbinding show-text () nil
590 (defbinding show-glyphs () nil
592 (glyphs (vector glyph))
593 ((length glyphs) int))
595 (defbinding font-extents () boolean
598 (defbinding text-extents (cr text &optional (extents (make-instance 'text-extents))) nil
601 (extents text-extents :in/return))
603 (defbinding glyph-extents (cr glyphs &optional (extents (make-instance 'text-extents))) nil
605 (glyphs (vector glyph))
606 ((length glyphs) int)
607 (extents text-extents :in/return))
612 (defbinding %font-face-reference () nil
615 (defbinding %font-face-destroy () nil
618 (defbinding font-face-status () status
619 (font-face font-face))
625 (defbinding %scaled-font-reference () nil
628 (defbinding %scaled-font-destroy () nil
631 (defbinding scaled-font-status () status
632 (scaled-font scaled-font))
634 (defbinding scaled-font-extents (scaled-font &optional (extents (make-instance 'text-extents))) nil
635 (scaled-font scaled-font)
636 (extents text-extents :in/return))
638 (defbinding scaled-font-glyph-extents (scaled-font glyphs &optional (extents (make-instance 'text-extents))) nil
639 (scaled-font scaled-font)
640 (glyphs (vector glyph))
641 ((length glyphs) int)
642 (extents text-extents :in/return))
644 (defbinding %scaled-font-create () pointer
645 (font-face font-face)
648 (options font-options))
650 (defmethod allocate-foreign ((scaled-font scaled-font) &key font-face font-matrix cmt options)
651 (%scaled-font-create font-face font-matrix cmt options))
658 (defbinding %font-options-copy () nil
661 (defbinding %font-options-destroy () nil
664 (defbinding font-options-status () status
665 (font-options font-options))
667 (defbinding %font-options-create () pointer)
669 (defmethod allocate-foreign ((font-options font-options) &rest initargs)
670 (declare (ignore initargs))
671 (%font-options-create))
673 (defbinding font-options-merge () nil
674 (options1 font-options :in/return)
675 (options2 font-options))
677 (defbinding font-options-hash () unsigned-int
678 (options font-options))
680 (defbinding font-options-equal-p () boolean
681 (options1 font-options)
682 (options2 font-options))
688 (defbinding %surface-reference () nil
691 (defbinding %surface-destroy () nil
694 (defbinding surface-create-similar () surface
696 (format surface-format )
700 (defbinding surface-finish () nil
703 (defbinding surface-flush () nil
706 (defbinding surface-get-font-options () nil
708 ((make-instance 'font-options) font-options :in/return))
710 (defbinding surface-set-device-offset () nil
712 (x-offset double-float)
713 (y-offset double-float))
715 (defbinding surface-status () status
718 (defbinding %surface-mark-dirty () nil
721 (defbinding %surface-mark-dirty-rectangle () nil
728 (defun surface-mark-dirty (surface &optional x y width height)
730 (%surface-mark-dirty-rectangle surface x y width height)
731 (%surface-mark-dirty surface)))
733 #?(pkg-exists-p "cairo" :atleast-version "1.2")
734 (defbinding surface-set-fallback-resolution () nil
736 (x-pixels-per-inch double-float)
737 (y-pixels-per-inch double-float))
742 ;; Should data be automatically freed when the surface is GCed?
743 (defmethod allocate-foreign ((surface image-surface)
744 &key width height stride format data)
746 (%image-surface-create format width height)
747 (%image-surface-create-for-data data format width height
750 (let ((element-size (cdr (assoc format '((:argb32 . 4) (:rgb24 . 4) (:a8 . 1) (:a1 1/8))))))
751 (ceiling (* width element-size)))))))
754 (defbinding %image-surface-create () image-surface
755 (format surface-format)
759 (defbinding %image-surface-create-for-data () image-surface
761 (format surface-format)
770 (defbinding image-surface-create-from-png (filename) image-surface
771 ((truename filename) pathname))
778 (defbinding matrix-init () nil
779 (matrix matrix :in/return)
780 (xx double-float) (yx double-float)
781 (xy double-float) (yy double-float)
782 (x0 double-float) (y0 double-float))
784 (defbinding matrix-init-identity () nil
785 (matrix matrix :in/return))
787 (defbinding matrix-init-translate () nil
788 (matrix matrix :in/return)
792 (defbinding matrix-init-scale () nil
793 (matrix matrix :in/return)
797 (defbinding matrix-init-rotate () nil
798 (matrix matrix :in/return)
799 (radians double-float))
801 (defbinding matrix-translate () nil
802 (matrix matrix :in/return)
806 (defbinding matrix-scale () nil
807 (matrix matrix :in/return)
811 (defbinding matrix-rotate () nil
812 (matrix matrix :in/return)
813 (radians double-float))
815 (defbinding matrix-invert () nil
816 (matrix matrix :in/return))
818 (defbinding matrix-multiply () nil
823 (defbinding matrix-transform-distance () nil
824 (matrix matrix :in/return)
828 (defbinding matrix-transform-point () nil
829 (matrix matrix :in/return)