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.9 2007-01-12 10:32:43 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 (ref-counted-object)
82 (:metaclass proxy-class)
83 (:ref %font-face-reference)
84 (:unref %font-face-destroy))
86 (defclass font-options (ref-counted-object)
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 (ref-counted-object)
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 (ref-counted-object)
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 (ref-counted-object)
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 (ref-counted-object)
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))
290 ;; (defclass path (proxy)
292 ;; (:metaclass proxy-class))
299 (defbinding %reference () nil
302 (defbinding %destroy () nil
305 (defbinding (save-context "cairo_save") () nil
308 (defbinding (restore-context "cairo_restore") () nil
311 (defmacro with-context ((cr) &body body)
312 (let ((context (make-symbol "CONTEXT")))
313 `(let ((,context ,cr))
314 (save-context ,context)
317 (restore-context ,context)))))
319 (defbinding status () status
322 (defun ensure-color-component (component)
325 (integer (/ component 256.0))))
327 (defbinding (set-source-color "cairo_set_source_rgba") (cr red green blue &optional (alpha 1.0)) nil
329 ((ensure-color-component red) double-float)
330 ((ensure-color-component green) double-float)
331 ((ensure-color-component blue) double-float)
332 ((ensure-color-component alpha) double-float))
334 (defbinding set-source-surface (cr surface &optional (x 0.0) (y 0.0)) nil
340 (defun set-source (cr source)
342 (pattern (setf (source cr) source))
343 (surface (set-source-surface cr source))
344 (list (apply #'set-source-color cr source))
345 (vector (apply #'set-source-color cr (coerce source 'list)))
346 (null (set-source-color cr 0.0 0.0 0.0))))
348 (defbinding set-dash (cr dashes &optional (offset 0.0)) nil
350 (dashes (vector double-float))
351 ((length dashes) int)
352 (offset double-float))
354 (defbinding (paint "cairo_paint_with_alpha") (cr &optional (alpha 1.0)) nil
356 (alpha double-float))
358 (defbinding mask () nil
362 (defbinding mask-surface () nil
365 (surface-x double-float)
366 (surface-y double-float))
368 (defmacro defoperator (name &optional clip-p)
369 (let ((iname (intern (format nil "%~A" name)))
370 (pname (intern (format nil "%~A-PRESERVE" name))))
372 (defbinding ,iname () nil
374 (defbinding ,pname () nil
376 (defun ,name (cr &optional preserve)
381 (let ((tname (intern (format nil "IN-~A-P" name)))
382 (ename (intern (format nil "~A-EXTENTS" name))))
384 (defbinding ,tname () boolean
388 (defbinding ,ename () boolean
390 (x1 double-float :out)
391 (y1 double-float :out)
392 (x2 double-float :out)
393 (y2 double-float :out))))))))
399 (defbinding reset-clip () nil
402 (defbinding copy-page () nil
405 (defbinding show-page () nil
411 (defbinding get-current-point () nil
413 (x double-float :out)
414 (y double-float :out))
416 (defbinding new-path () nil
419 #?(pkg-exists-p "cairo" :atleast-version "1.2")
420 (defbinding new-sub-path () nil
423 (defbinding close-path () nil
426 (defmacro defpath (name args &optional relative-p)
427 (flet ((def (name type)
429 ,(when (eq type 'optimized-double-float)
430 `(declaim (ftype (function (context ,@(loop repeat (length args) collect 'double-float))) ,(first name))))
431 (defbinding ,name () nil
433 ,@(mapcar #'(lambda (arg) (list arg type)) args)))))
436 ,(def name 'double-float)
437 ,(let ((name (intern (format nil "FAST-~A" name)))
438 (cname (gffi::default-alien-fname name)))
439 (def (list name cname) 'optimized-double-float))
441 (let* ((rel-name (intern (format nil "REL-~A" name)))
442 (fast-rel-name (intern (format nil "FAST-REL-~A" name)))
443 (cname (gffi::default-alien-fname rel-name)))
445 (def rel-name 'double-float)
446 (def (list fast-rel-name cname) 'optimized-double-float)))))))
449 (defpath arc (xc yc radius angle1 angle2))
450 (defpath arc-negative (xc yc radius angle1 angle2))
451 (defpath curve-to (x1 y1 x2 y2 x3 y3) t)
452 (defpath line-to (x y) t)
453 (defpath move-to (x y) t)
454 (defpath rectangle (x y width height))
456 (defun circle (cr x y radius)
457 (arc cr x y radius 0.0 (* pi 2)))
459 (defbinding glyph-path (cr glyphs) nil
461 (glyphs (vector glyph))
462 ((length glyphs) int))
464 (defbinding text-path () nil
472 (defbinding (pattern-add-color-stop "cairo_pattern_add_color_stop_rgba")
473 (pattern offset red green blue &optional (alpha 1.0)) nil
475 (offset double-float)
476 ((ensure-color-component red) double-float)
477 ((ensure-color-component green) double-float)
478 ((ensure-color-component blue) double-float)
479 ((ensure-color-component alpha) double-float))
481 (defbinding (pattern-create "cairo_pattern_create_rgba")
482 (red green blue &optional (alpha 1.0)) pattern
483 ((ensure-color-component red) double-float)
484 ((ensure-color-component green) double-float)
485 ((ensure-color-component blue) double-float)
486 ((ensure-color-component alpha) double-float))
488 (defbinding pattern-create-for-surface () pattern
491 (defbinding pattern-create-linear () pattern
497 (defbinding pattern-create-radial () pattern
500 (radius0 double-float)
503 (radius1 double-float))
505 (defbinding %pattern-reference () nil
508 (defbinding %pattern-destroy () nil
511 (defbinding pattern-status () status
518 (defbinding translate () nil
523 (defbinding scale (cr sx &optional (sy sx)) nil
528 (defun scale-to-device (cr &optional keep-rotation-p)
530 (multiple-value-call #'scale cr (device-to-user-distance cr 1.0))
531 (multiple-value-bind (x y)
532 (multiple-value-call #'user-to-device cr (get-current-point cr))
534 (translate cr x y))))
536 (defbinding rotate () nil
538 (angle double-float))
540 (defbinding transform () nil
544 (defbinding (matrix "cairo_get_matrix") () nil
546 ((make-instance 'matrix) matrix :in/return))
548 (defbinding identity-matrix () nil
551 (defbinding user-to-device () nil
553 (x double-float :in/out)
554 (y double-float :in/out))
556 (defbinding user-to-device-distance (cr dx &optional (dy dx)) nil
558 (dx double-float :in/out)
559 (dy double-float :in/out))
561 (defbinding device-to-user () nil
563 (x double-float :in/out)
564 (y double-float :in/out))
566 (defbinding device-to-user-distance (cr dx &optional (dy dx)) nil
568 (dx double-float :in/out)
569 (dy double-float :in/out))
574 (defbinding select-font-face () nil
578 (weight font-weight))
580 (defbinding set-font-size () nil
584 (defbinding show-text () nil
588 (defbinding show-glyphs () nil
590 (glyphs (vector glyph))
591 ((length glyphs) int))
593 (defbinding font-extents () boolean
596 (defbinding text-extents (cr text &optional (extents (make-instance 'text-extents))) nil
599 (extents text-extents :in/return))
601 (defbinding glyph-extents (cr glyphs &optional (extents (make-instance 'text-extents))) nil
603 (glyphs (vector glyph))
604 ((length glyphs) int)
605 (extents text-extents :in/return))
610 (defbinding %font-face-reference () nil
613 (defbinding %font-face-destroy () nil
616 (defbinding font-face-status () status
617 (font-face font-face))
623 (defbinding %scaled-font-reference () nil
626 (defbinding %scaled-font-destroy () nil
629 (defbinding scaled-font-status () status
630 (scaled-font scaled-font))
632 (defbinding scaled-font-extents (scaled-font &optional (extents (make-instance 'text-extents))) nil
633 (scaled-font scaled-font)
634 (extents text-extents :in/return))
636 (defbinding scaled-font-glyph-extents (scaled-font glyphs &optional (extents (make-instance 'text-extents))) nil
637 (scaled-font scaled-font)
638 (glyphs (vector glyph))
639 ((length glyphs) int)
640 (extents text-extents :in/return))
642 (defbinding %scaled-font-create () pointer
643 (font-face font-face)
646 (options font-options))
648 (defmethod allocate-foreign ((scaled-font scaled-font) &key font-face font-matrix cmt options)
649 (%scaled-font-create font-face font-matrix cmt options))
656 (defbinding %font-options-copy () nil
659 (defbinding %font-options-destroy () nil
662 (defbinding font-options-status () status
663 (font-options font-options))
665 (defbinding %font-options-create () pointer)
667 (defmethod allocate-foreign ((font-options font-options) &rest initargs)
668 (declare (ignore initargs))
669 (%font-options-create))
671 (defbinding font-options-merge () nil
672 (options1 font-options :in/return)
673 (options2 font-options))
675 (defbinding font-options-hash () unsigned-int
676 (options font-options))
678 (defbinding font-options-equal-p () boolean
679 (options1 font-options)
680 (options2 font-options))
686 (defbinding %surface-reference () nil
689 (defbinding %surface-destroy () nil
692 (defbinding surface-create-similar () surface
694 (format surface-format )
698 (defbinding surface-finish () nil
701 (defbinding surface-flush () nil
704 (defbinding surface-get-font-options () nil
706 ((make-instance 'font-options) font-options :in/return))
708 (defbinding surface-set-device-offset () nil
710 (x-offset double-float)
711 (y-offset double-float))
713 (defbinding surface-status () status
716 (defbinding %surface-mark-dirty () nil
719 (defbinding %surface-mark-dirty-rectangle () nil
726 (defun surface-mark-dirty (surface &optional x y width height)
728 (%surface-mark-dirty-rectangle surface x y width height)
729 (%surface-mark-dirty surface)))
731 #?(pkg-exists-p "cairo" :atleast-version "1.2")
732 (defbinding surface-set-fallback-resolution () nil
734 (x-pixels-per-inch double-float)
735 (y-pixels-per-inch double-float))
740 ;; Should data be automatically freed when the surface is GCed?
741 (defmethod allocate-foreign ((surface image-surface)
742 &key width height stride format data)
744 (%image-surface-create format width height)
745 (%image-surface-create-for-data data format width height
748 (let ((element-size (cdr (assoc format '((:argb32 . 4) (:rgb24 . 4) (:a8 . 1) (:a1 1/8))))))
749 (ceiling (* width element-size)))))))
752 (defbinding %image-surface-create () image-surface
753 (format surface-format)
757 (defbinding %image-surface-create-for-data () image-surface
759 (format surface-format)
768 (defbinding image-surface-create-from-png (filename) image-surface
769 ((truename filename) pathname))
776 (defbinding matrix-init () nil
777 (matrix matrix :in/return)
778 (xx double-float) (yx double-float)
779 (xy double-float) (yy double-float)
780 (x0 double-float) (y0 double-float))
782 (defbinding matrix-init-identity () nil
783 (matrix matrix :in/return))
785 (defbinding matrix-init-translate () nil
786 (matrix matrix :in/return)
790 (defbinding matrix-init-scale () nil
791 (matrix matrix :in/return)
795 (defbinding matrix-init-rotate () nil
796 (matrix matrix :in/return)
797 (radians double-float))
799 (defbinding matrix-translate () nil
800 (matrix matrix :in/return)
804 (defbinding matrix-scale () nil
805 (matrix matrix :in/return)
809 (defbinding matrix-rotate () nil
810 (matrix matrix :in/return)
811 (radians double-float))
813 (defbinding matrix-invert () nil
814 (matrix matrix :in/return))
816 (defbinding matrix-multiply () nil
821 (defbinding matrix-transform-distance () nil
822 (matrix matrix :in/return)
826 (defbinding matrix-transform-point () nil
827 (matrix matrix :in/return)