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