chiark / gitweb /
PATHNAME correctly registered as an alias type the foreign gchararray
[clg] / cairo / cairo.lisp
1 ;; Common Lisp bindings for Cairo
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
23 ;; $Id: cairo.lisp,v 1.16 2007-10-16 07:48:39 espen Exp $
24
25 (in-package "CAIRO")
26
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)
31
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
38     :invalid-dash)
39
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 :italic :oblique)
44   (define-enum-type font-weight :normal :bold)
45
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)
49
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)
56
57
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)
62
63   (defclass surface-class (proxy-class)
64     ())
65
66   (defmethod validate-superclass ((class surface-class) (super standard-class))
67     (subtypep (class-name super) 'surface))
68
69   (defclass glyph (struct)
70     ((index 
71       :allocation :alien 
72       :initarg :index 
73       :accessor glyph-index 
74       :type unsigned-long)
75      (x     
76       :allocation :alien 
77       :initarg :x 
78       :accessor glyph-x 
79       :type double-float)
80      (y     
81       :allocation :alien 
82       :initarg :y 
83       :accessor glyph-y
84       :type double-float))
85     (:metaclass struct-class))
86
87   (defclass font-face (ref-counted-object)
88     ()
89     (:metaclass proxy-class)
90     (:ref %font-face-reference)
91     (:unref %font-face-destroy))
92
93   (defclass font-options (ref-counted-object)
94     ((antialias
95       :allocation :virtual 
96       :getter "font_options_get_antialias"
97       :setter "font_options_set_antialias"
98       :accessor font-options-antialias
99       :type antialias)
100      (subpixel-order
101       :allocation :virtual 
102       :getter "font_options_get_subpixel_order"
103       :setter "font_options_set_subpixel_order"
104       :accessor font-options-subpixel-order
105       :type subpixel-order)
106      (hint-style
107       :allocation :virtual 
108       :getter "font_options_get_hint_style"
109       :setter "font_options_set_hint_style"
110       :accessor font-options-hint-style
111       :type hint-style)
112      (hint-metrics
113       :allocation :virtual 
114       :getter "font_options_get_hint_metrics"
115       :setter "font_options_set_hint_metrics"
116       :accessor font-options-hint-metrics
117       :type hint-metrics))
118     (:metaclass proxy-class)    
119     (:ref %font-options-reference)
120     (:unref %font-options-destroy))
121
122   (defclass scaled-font (ref-counted-object)
123     ()
124     (:metaclass proxy-class)
125     (:ref %scaled-font-reference)
126     (:unref %scaled-font-destroy))
127
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))
142
143
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))
152
153   (defclass pattern (ref-counted-object)
154     ((extend
155       :allocation :virtual 
156       :getter "cairo_pattern_get_extend"
157       :setter "cairo_pattern_set_extend"
158       :accessor pattern-extend
159       :type extend)
160      (filter
161       :allocation :virtual 
162       :getter "cairo_pattern_get_filter"
163       :setter "cairo_pattern_set_filter"
164       :accessor pattern-filter
165       :type filter)
166      (matrix
167       :allocation :virtual 
168       :getter "cairo_pattern_get_matrix"
169       :setter "cairo_pattern_set_matrix"
170       :accessor pattern-matrix
171       :type matrix))
172     (:metaclass proxy-class)
173     (:ref %pattern-reference)
174     (:unref %pattern-destroy))
175
176
177   (defclass surface (ref-counted-object)
178     ((content 
179       :allocation :virtual 
180       :getter "cairo_surface_get_content"
181       :reader surface-content
182       :type content))
183     (:metaclass surface-class))
184
185   (defclass image-surface (surface)
186     ((data
187       :allocation :virtual 
188       :getter "cairo_image_surface_get_data"
189       :reader surface-data
190       :type pointer)
191      (format
192       :allocation :virtual 
193       :getter "cairo_image_surface_get_format"
194       :reader surface-format
195       :type surface-format)
196      (width
197       :allocation :virtual 
198       :getter "cairo_image_surface_get_width"
199       :reader surface-width
200       :type int)
201      (height
202       :allocation :virtual 
203       :getter "cairo_image_surface_get_height"
204       :reader surface-height
205       :type int)
206      (stride
207       :allocation :virtual 
208       :getter "cairo_image_surface_get_stride"
209       :reader surface-stride
210       :type int))
211     (:metaclass surface-class))
212
213   (defclass xlib-surface (surface)
214     ((width
215       :allocation :virtual 
216       :getter "cairo_xlib_surface_get_width"
217       :reader surface-width
218       :type int)
219      (height
220       :allocation :virtual 
221       :getter "cairo_xlib_surface_get_height"
222       :reader surface-height
223       :type int))
224     (:metaclass surface-class))
225
226   (defclass pdf-surface (surface)
227     ()
228     (:metaclass surface-class))
229   
230   (defclass ps-surface (surface)
231     ()
232     (:metaclass surface-class))
233     
234   (defclass svg-surface (surface)
235     ()
236     (:metaclass surface-class))
237
238
239   (defclass context (ref-counted-object)
240     ((target
241       :allocation :virtual 
242       :getter "cairo_get_target"
243       :reader target
244       :type surface)
245      (source
246       :allocation :virtual 
247       :getter "cairo_get_source"
248       :setter "cairo_set_source"
249       :accessor source
250       :type pattern)
251      (antialias
252       :allocation :virtual 
253       :getter "cairo_get_antialias"
254       :setter "cairo_set_antialias"
255       :accessor antialias
256       :type antialias)
257      (tolerance
258       :allocation :virtual 
259       :getter "cairo_get_tolerance"
260       :setter "cairo_set_tolerance"
261       :accessor tolerance
262       :type double-float)
263      (fill-rule
264       :allocation :virtual 
265       :getter "cairo_get_fill_rule"
266       :setter "cairo_set_fill_rule"
267       :accessor fill-rule
268       :type fill-rule)
269      (line-width
270       :allocation :virtual 
271       :getter "cairo_get_line_width"
272       :setter "cairo_set_line_width"
273       :accessor line-width
274       :type double-float)
275      (line-cap
276       :allocation :virtual 
277       :getter "cairo_get_line_cap"
278       :setter "cairo_set_line_cap"
279       :accessor line-cap
280       :type line-cap)
281      (line-join
282       :allocation :virtual 
283       :getter "cairo_get_line_join"
284       :setter "cairo_set_line_join"
285       :accessor line-join
286       :type line-join)
287      (miter-limit
288       :allocation :virtual 
289       :getter "cairo_get_miter_limit"
290       :setter "cairo_set_miter_limit"
291       :accessor miter-limit
292       :type double-float)
293      (font-matrix
294       :allocation :virtual 
295       :getter "cairo_get_font_matrix"
296       :setter "cairo_set_font_matrix"
297       :accessor font-matrix
298       :type matrix)
299      (font-options
300       :allocation :virtual 
301       :getter "cairo_get_font_options"
302       :setter "cairo_set_font_options"
303       :accessor font-options
304       :type font-options)
305      (font-face
306       :allocation :virtual 
307       :getter "cairo_get_font_face"
308       :setter "cairo_set_font_face"
309       :accessor font-face
310       :type font-face)
311      (operator
312       :allocation :virtual 
313       :getter "cairo_get_operator"
314       :setter "cairo_set_operator"
315       :accessor operator
316       :type operator)
317      (matrix
318       :allocation :virtual 
319       :getter matrix
320       :setter "cairo_set_matrix"
321       :writer (setf matrix)
322       :type matrix)
323      )
324     (:metaclass proxy-class)
325     (:ref %reference)
326     (:unref %destroy))
327
328
329 ;;   (defclass path (proxy)
330 ;;     ()
331 ;;     (:metaclass proxy-class))
332
333   )
334
335
336 ;;; Cairo context
337
338 (defmethod allocate-foreign ((context context) &key target)
339   (%create-context target))
340
341 (defbinding (%create-context "cairo_create") () pointer
342   (target surface))
343
344 (defbinding %reference () pointer
345   (location pointer))
346
347 (defbinding %destroy () nil
348   (location pointer))
349
350 (defbinding (save-context "cairo_save") () nil
351   (cr context))
352
353 (defbinding (restore-context "cairo_restore") () nil
354   (cr context))
355
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)
360        (unwind-protect
361            (progn ,@body)
362          (restore-context ,context)))))
363
364 (defbinding status () status
365   (cr context))
366
367 (defun ensure-color-component (component)
368   (etypecase component
369     (float component)
370     (integer (/ component 256.0))))
371
372 (defbinding (set-source-color "cairo_set_source_rgba") (cr red green blue &optional (alpha 1.0)) nil
373   (cr context)
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))
378
379 (defbinding set-source-surface (cr surface &optional (x 0.0) (y 0.0)) nil
380   (cr context)
381   (surface surface)
382   (x double-float)
383   (y double-float))
384
385 (defun set-source (cr source)
386   (etypecase 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)))))
392
393 (defbinding set-dash (cr dashes &optional (offset 0.0)) nil
394   (cr context)
395   (dashes (vector double-float))
396   ((length dashes) int)
397   (offset double-float))
398
399 (defbinding (paint "cairo_paint_with_alpha") (cr &optional (alpha 1.0)) nil
400   (cr context)
401   (alpha double-float))
402
403 (defbinding mask () nil
404   (cr context)
405   (pattern pattern))
406
407 (defbinding mask-surface () nil
408   (cr context)
409   (surface surface)
410   (surface-x double-float)
411   (surface-y double-float))
412
413 (defmacro defoperator (name &optional clip-p)
414   (let ((iname (intern (format nil "%~A" name)))
415         (pname (intern (format nil "%~A-PRESERVE" name))))
416     `(progn
417        (defbinding ,iname () nil
418          (cr context))
419        (defbinding ,pname () nil
420          (cr context))
421        (defun ,name (cr &optional preserve)
422          (if preserve
423              (,pname cr)
424            (,iname cr)))
425        ,(unless clip-p
426           (let ((tname (intern (format nil "IN-~A-P" name)))
427                 (ename (intern (format nil "~A-EXTENTS" name))))
428             `(progn
429                (defbinding ,tname () boolean
430                  (cr context)
431                  (x double-float)
432                  (y double-float))
433                (defbinding ,ename () nil
434                  (cr context)
435                  (x1 double-float :out)
436                  (y1 double-float :out)
437                  (x2 double-float :out)
438                  (y2 double-float :out))))))))
439
440 (defoperator clip t)
441 (defoperator stroke)
442 (defoperator fill)
443
444 (defbinding reset-clip () nil
445   (cr context))
446
447 (defbinding copy-page () nil
448   (cr context))
449
450 (defbinding show-page () nil
451   (cr context))
452
453
454 ;;; Paths
455
456 (defbinding get-current-point () nil
457   (cr context)
458   (x double-float :out)
459   (y double-float :out))
460
461 (defbinding new-path () nil
462   (cr context))
463
464 #?(pkg-exists-p "cairo" :atleast-version "1.2")
465 (defbinding new-sub-path () nil
466   (cr context))
467
468 (defbinding close-path () nil
469   (cr context))
470
471 (defmacro defpath (name args &optional relative-p)
472   (flet ((def (name type)
473            `(progn
474               ,(when (eq type 'optimized-double-float)
475                  `(declaim (inline ,(first name))))
476               (defbinding ,name () nil
477                 (cr context)
478                 ,@(mapcar #'(lambda (arg) (list arg type)) args)))))
479
480   `(progn
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))
485      ,@(when relative-p
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)))
489            (list
490             (def rel-name 'double-float)
491             (def (list fast-rel-name cname) 'optimized-double-float)))))))
492
493
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))
500
501 (defun circle (cr x y radius &optional negative-p)
502   (move-to cr radius 0.0d0)
503   (if negative-p
504       (arc-negative cr x y radius (* pi 2) 0.0d0)
505     (arc cr x y radius 0.0d0 (* pi 2)))
506   (close-path cr))
507
508
509 (defbinding glyph-path (cr glyphs) nil
510   (cr context)
511   (glyphs (vector glyph))
512   ((length glyphs) int))
513
514 (defbinding text-path () nil
515   (cr context)
516   (text string))
517
518
519
520 ;;; Patterns
521
522 (defbinding (pattern-add-color-stop "cairo_pattern_add_color_stop_rgba")
523     (pattern offset red green blue &optional (alpha 1.0)) nil
524   (pattern pattern)
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))
530
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))
537
538 (defbinding pattern-create-for-surface () pattern
539   (surface surface))
540
541 (defbinding pattern-create-linear () pattern
542   (x0 double-float)
543   (y0 double-float)
544   (x1 double-float)
545   (y1 double-float))
546
547 (defbinding pattern-create-radial () pattern
548   (cx0 double-float)
549   (cy0 double-float)
550   (radius0 double-float)
551   (cx1 double-float)
552   (cy1 double-float)
553   (radius1 double-float))
554
555 (defbinding %pattern-reference () pointer
556   (location pointer))
557
558 (defbinding %pattern-destroy () nil
559   (location pointer))
560
561 (defbinding pattern-status () status
562   (pattern pattern))
563
564
565
566 ;;; Transformations
567
568 (defbinding translate () nil
569   (cr context)
570   (tx double-float)
571   (ty double-float))
572
573 (defbinding scale (cr sx &optional (sy sx)) nil
574   (cr context)
575   (sx double-float)
576   (sy double-float))
577
578 (defun scale-to-device (cr &optional keep-rotation-p)
579   (if keep-rotation-p
580       (multiple-value-bind (dx dy) (device-to-user-distance cr 1.0 0.0)
581         (scale cr (sqrt (+ (* dx dx) (* dy dy)))))
582     (multiple-value-bind (x y)
583         (with-context (cr)
584           (move-to cr 0.0 0.0)
585           (multiple-value-call #'user-to-device cr (get-current-point cr)))
586       (identity-matrix cr)
587       (translate cr x y))))
588
589 (defbinding rotate () nil
590   (cr context)
591   (angle double-float))
592
593 (defbinding transform () nil
594   (cr context)
595   (matrix matrix))
596
597 (defbinding (matrix "cairo_get_matrix") () nil
598   (cr context)
599   ((make-instance 'matrix) matrix :in/return))
600
601 (defbinding identity-matrix () nil
602   (cr context))
603
604 (defbinding user-to-device () nil
605   (cr context)
606   (x double-float :in/out)
607   (y double-float :in/out))
608
609 (defbinding user-to-device-distance (cr dx &optional (dy dx)) nil
610   (cr context)
611   (dx double-float :in/out)
612   (dy double-float :in/out))
613
614 (defbinding device-to-user () nil
615   (cr context)
616   (x double-float :in/out)
617   (y double-float :in/out))
618
619 (defbinding device-to-user-distance (cr dx &optional (dy dx)) nil
620   (cr context)
621   (dx double-float :in/out)
622   (dy double-float :in/out))
623
624
625 ;;; Text
626
627 (defbinding select-font-face () nil
628   (cr context)
629   (family string)
630   (slant font-slant)
631   (weight font-weight))
632
633 (defbinding set-font-size () nil
634   (cr context)
635   (size double-float))
636
637 (defbinding show-text () nil
638   (cr context)
639   (text string))
640
641 (defbinding show-glyphs () nil
642   (cr context)
643   (glyphs (vector glyph))
644   ((length glyphs) int))
645
646 (defbinding font-extents () boolean
647   (cr context))
648
649 (defbinding text-extents (cr text &optional (extents (make-instance 'text-extents))) nil
650   (cr context)
651   (text string)
652   (extents text-extents :in/return))
653
654 (defbinding glyph-extents (cr glyphs &optional (extents (make-instance 'text-extents))) nil
655   (cr context)
656   (glyphs (vector glyph))
657   ((length glyphs) int)
658   (extents text-extents :in/return))
659
660
661 ;;; Fonts
662
663 (defbinding %font-face-reference () pointer
664   (location pointer))
665
666 (defbinding %font-face-destroy () nil
667   (location pointer))
668
669 (defbinding font-face-status () status
670   (font-face font-face))
671
672
673
674 ;;; Scaled Fonts
675
676 (defbinding %scaled-font-reference () pointer
677   (location pointer))
678
679 (defbinding %scaled-font-destroy () nil
680   (location pointer))
681
682 (defbinding scaled-font-status () status
683   (scaled-font scaled-font))
684
685 (defbinding scaled-font-extents (scaled-font &optional (extents (make-instance 'text-extents))) nil
686   (scaled-font scaled-font)
687   (extents text-extents :in/return))
688
689 (defbinding scaled-font-glyph-extents (scaled-font glyphs &optional (extents (make-instance 'text-extents))) nil
690   (scaled-font scaled-font)
691   (glyphs (vector glyph))
692   ((length glyphs) int)
693   (extents text-extents :in/return))
694
695 (defbinding %scaled-font-create () pointer
696   (font-face font-face)
697   (font-matrix matrix)
698   (ctm matrix)
699   (options font-options))
700
701 (defmethod allocate-foreign ((scaled-font scaled-font) &key font-face font-matrix cmt options)
702   (%scaled-font-create font-face font-matrix cmt options))
703
704
705
706 ;;; Font Options
707
708
709 (defbinding %font-options-copy () nil
710   (location pointer))
711
712 (defbinding %font-options-destroy () nil
713   (location pointer))
714
715 (defbinding font-options-status () status
716   (font-options font-options))
717
718 (defbinding %font-options-create () pointer)
719
720 (defmethod allocate-foreign ((font-options font-options) &rest initargs)
721   (declare (ignore initargs))
722   (%font-options-create))
723
724 (defbinding font-options-merge () nil
725   (options1 font-options :in/return)
726   (options2 font-options))
727
728 (defbinding font-options-hash () unsigned-int
729   (options font-options))
730
731 (defbinding font-options-equal-p () boolean
732   (options1 font-options)
733   (options2 font-options))
734
735
736
737 ;;; Surfaces
738
739 (defmethod make-proxy-instance :around ((class surface-class) location 
740                                         &rest initargs)
741   (let ((class (find-class (%surface-get-type location))))
742     (apply #'call-next-method class location initargs)))
743
744 (defbinding %surface-get-type () surface-type
745   (location pointer))
746
747 (defbinding %surface-reference () pointer
748   (location pointer))
749
750 (defbinding %surface-destroy () nil
751   (location pointer))
752
753 (defmethod reference-function ((class surface-class))
754   (declare (ignore class))
755   #'%surface-reference)
756
757 (defmethod unreference-function ((class surface-class))
758   (declare (ignore class))
759   #'%surface-destroy)
760
761 (defbinding %surface-set-user-data (surface key data-id) status
762   (surface pointer)
763   ((quark-intern key) pointer-data)
764   (data-id pointer-data)
765   (user-data-destroy-callback callback))
766
767 (defmethod (setf user-data) (data (surface surface) key)
768   (%surface-set-user-data (foreign-location surface) key (register-user-data data))
769   data)
770
771 (defbinding %surface-get-user-data () pointer-data
772   (surface surface)
773   (key pointer-data))
774
775 (defmethod user-data ((surface surface) key)
776   (find-user-data (%surface-get-user-data surface (quark-intern key))))
777
778 (defbinding surface-create-similar () surface
779   (other surface)
780   (format surface-format )
781   (width int)
782   (height int))
783
784 (defbinding surface-finish () nil
785   (surface surface))
786
787 (defbinding surface-flush () nil
788   (surface surface))
789
790 (defbinding surface-get-font-options () nil
791   (surface surface)
792   ((make-instance 'font-options) font-options :in/return))
793
794 (defbinding surface-set-device-offset () nil
795   (surface surface)
796   (x-offset double-float)
797   (y-offset double-float))
798
799 (defbinding surface-status () status
800   (surface surface))
801
802 (defbinding %surface-mark-dirty () nil
803   (surface surface))
804
805 (defbinding %surface-mark-dirty-rectangle () nil
806   (surface surface)
807   (x int)
808   (y int)
809   (width int)
810   (height int))
811
812 (defun surface-mark-dirty (surface &optional x y width height)
813   (if x
814       (%surface-mark-dirty-rectangle surface x y width height)
815     (%surface-mark-dirty surface)))
816
817 (defbinding surface-set-fallback-resolution () nil
818   (surface surface)
819   (x-pixels-per-inch double-float)
820   (y-pixels-per-inch double-float))
821
822 (define-callback stream-write-func status 
823     ((stream-id pointer-data) (data pointer) (length unsigned-int))
824   (let ((stream (find-user-data stream-id)))
825     (typecase stream
826       (stream
827        (map-c-vector 'nil #'(lambda (octet) (write-byte octet stream))
828         data '(unsigned-byte 8) length))
829       ((or symbol function)
830        (funcall stream 
831         (map-c-vector 'vector #'identity data '(unsigned-byte 8) length)))))
832   :success)
833
834
835 (defmacro with-surface ((surface cr) &body body)
836   `(let ((,cr (make-instance 'context :target ,surface)))
837      ,@body))
838
839
840 ;; Image Surface
841
842 ;; Should data be automatically freed when the surface is GCed?
843 (defmethod allocate-foreign ((surface image-surface) 
844                              &key filename width height stride format data)
845   (cond
846    (filename (%image-surface-create-from-png filename))
847    ((not data) (%image-surface-create format width height))
848    (t
849     (%image-surface-create-for-data data format width height 
850      (or 
851       stride
852       (let ((element-size (cdr (assoc format '((:argb32 . 4) (:rgb24 . 4) (:a8 . 1) (:a1 1/8))))))
853         (ceiling (* width element-size))))))))
854
855
856 (defbinding %image-surface-create () pointer
857   (format surface-format)
858   (width int)
859   (hegit int))
860
861 (defbinding %image-surface-create-for-data () pointer
862   (data pointer)
863   (format surface-format)
864   (width int)
865   (hegit int)
866   (stride int))
867
868 (defbinding %image-surface-create-from-png () pointer
869   (filename pathname))
870
871 (defbinding surface-write-to-png () status
872   (surface surface)
873   (filename pathname))
874
875
876 ;;; PDF Surface
877
878 (defmethod allocate-foreign ((surface pdf-surface)
879                              &key filename stream width height)
880   (cond
881    ((and filename stream)
882     (error "Only one of the arguments :filename and :stream may be specified"))
883    (filename (%pdf-surface-create filename width height))
884    (stream 
885     (let* ((stream-id (register-user-data stream))
886            (location (%pdf-surface-create-for-stream stream-id width height)))
887       (%surface-set-user-data location 'stream stream-id)
888       location))))
889
890
891 (defbinding %pdf-surface-create () pointer
892   (filename pathname)
893   (width double-float)
894   (height double-float))
895
896 (defbinding %pdf-surface-create-for-stream (stream width height) pointer
897   (stream-write-func callback)
898   (stream pointer-data)
899   (width double-float)
900   (height double-float))
901
902 (defbinding pdf-surface-set-size () nil
903   (surface pdf-surface)
904   (width double-float)
905   (height double-float))
906
907
908 ;;; PS Surface
909
910 (defmethod allocate-foreign ((surface ps-surface) 
911                              &key filename stream width height)
912   (cond
913    ((and filename stream)
914     (error "Only one of the arguments :filename and :stream may be specified"))
915    (filename (%ps-surface-create filename width height))
916    (stream 
917     (let* ((stream-id (register-user-data stream))
918            (location (%ps-surface-create-for-stream stream-id width height)))
919       (%surface-set-user-data location 'stream stream-id)
920       location))))
921
922 (defbinding %ps-surface-create () pointer
923   (filename pathname)
924   (width double-float)
925   (height double-float))
926
927 (defbinding %ps-surface-create-for-stream (stream width height) pointer
928   (stream-write-func callback)
929   (stream pointer-data)
930   (width double-float)
931   (height double-float))
932
933 (defbinding ps-surface-set-size () nil
934   (surface ps-surface)
935   (width double-float)
936   (height double-float))
937
938 (defbinding ps-surface-dsc-begin-setup () nil
939   (surface ps-surface))
940
941 (defbinding ps-surface-dsc-begin-page-setup () nil
942   (surface ps-surface))
943
944 (defbinding ps-surface-dsc-comment () nil
945   (surface ps-surface)
946   (comment string))
947
948
949 ;;; SVG Surface
950
951 (defmethod allocate-foreign ((surface svg-surface) 
952                              &key filename stream width height)
953   (cond
954    ((and filename stream)
955     (error "Only one of the arguments :filename and :stream may be specified"))
956    (filename (%svg-surface-create filename width height))
957    (stream 
958     (let* ((stream-id (register-user-data stream))
959            (location (%svg-surface-create-for-stream stream-id width height)))
960       (%surface-set-user-data location 'stream stream-id)
961       location))))
962
963 (defbinding %svg-surface-create () pointer
964   (filename pathname)
965   (width double-float)
966   (height double-float))
967
968 (defbinding %svg-surface-create-for-stream (stream width height) pointer
969   (stream-write-func callback)
970   (stream pointer-data)
971   (width double-float)
972   (height double-float))
973
974 (defbinding svg-surface-restrict-to-version () nil
975   (surface svg-surface)
976   (version svg-version))
977
978
979
980 ;;; Matrices
981
982 (defbinding matrix-init () nil
983   (matrix matrix :in/return)
984   (xx double-float) (yx double-float) 
985   (xy double-float) (yy double-float) 
986   (x0 double-float) (y0 double-float))
987
988 (defbinding matrix-init-identity (&optional (matrix (make-instance 'matrix))) nil
989   (matrix matrix :in/return))
990
991 (defun identity-matrix-p (matrix)
992   (with-slots (xx yx xy yy x0 y0) matrix
993     (and 
994      (= xx 1.0d0) (= yx 0.0d0) (= xy 0.0d0)
995      (= yy 1.0d0) (= x0 0.0d0) (= y0 0.0d0))))
996
997 (defbinding matrix-init-translate () nil
998   (matrix matrix :in/return)
999   (tx double-float)
1000   (ty double-float))
1001
1002 (defbinding matrix-init-scale (matrix sx &optional (sy sx)) nil
1003   (matrix matrix :in/return)
1004   (sx double-float)
1005   (sy double-float))
1006
1007 (defbinding matrix-init-rotate () nil
1008   (matrix matrix :in/return)
1009   (radians double-float))
1010
1011 (defbinding matrix-translate () nil
1012   (matrix matrix :in/return)
1013   (tx double-float)
1014   (ty double-float))
1015
1016 (defbinding matrix-scale (matrix sx &optional (sy sx)) nil
1017   (matrix matrix :in/return)
1018   (sx double-float)
1019   (sy double-float))
1020
1021 (defbinding matrix-rotate () nil
1022   (matrix matrix :in/return)
1023   (radians double-float))
1024
1025 (defbinding matrix-invert () nil
1026   (matrix matrix :in/return))
1027
1028 (defbinding matrix-multiply () nil
1029   (result matrix :out)
1030   (a matrix)
1031   (b matrix))
1032
1033 (defbinding matrix-transform-distance (matrix dx &optional (dy dx)) nil
1034   (matrix matrix)
1035   (dx double-float :in/out)
1036   (dy double-float :in/out))
1037
1038 (defbinding matrix-transform-point () nil
1039   (matrix matrix)
1040   (x double-float :in/out)
1041   (y double-float :in/out))