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