chiark / gitweb /
Exporting QUERY-ENUM-VALUES and TYPE-FROM-GLIB-NAME
[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.6 2006-04-26 12:37:48 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
30   (define-enum-type status
31     :success :no-memory :invalid-restore :invalid-pop-group
32     :no-current-point :invalid-matrix :invalid-status :null-pointer
33     :invalid-string :invalid-path-data :read-error :write-error
34     :surface-finished :surface-type-mismatch :pattern-type-mismatch
35     :invalid-content :invalid-format :invalid-visual :file-not-found
36     :invalid-dash)
37
38   (define-enum-type fill-rule :winding :even-odd)
39   (define-enum-type line-cap :butt :round :square)
40   (define-enum-type line-join :miter :round :bevel)    
41   (define-enum-type font-slant :normal :itaic :oblique)
42   (define-enum-type font-weight :normal :bold)
43
44   (define-enum-type operator
45    :clear :source :over :in :out :atop :dest :dest-over 
46    :dest-in :dest-out :dest-atop :xor :add :saturate)
47
48   (define-enum-type antialias :default :none :gray :subpixel)
49   (define-enum-type extend :none :repeat :reflect)
50   (define-enum-type filter :fast :good :best :nearest :bilinear :gaussian)
51   (define-enum-type subpixel-order :default :rgb :bgr :vrgb :vbgr)
52   (define-enum-type hint-style :default :none :slight :medium :full)
53   (define-enum-type hint-metrics :default :off :on)
54
55   (defclass glyph (struct)
56     ((index 
57       :allocation :alien 
58       :initarg :index 
59       :accessor glyph-index 
60       :type unsigned-long)
61      (x     
62       :allocation :alien 
63       :initarg :x 
64       :accessor glyph-x 
65       :type double-float)
66      (y     
67       :allocation :alien 
68       :initarg :y 
69       :accessor glyph-y
70       :type double-float))
71     (:metaclass struct-class))
72
73   (defclass font-face (proxy)
74     ()
75     (:metaclass proxy-class)
76     (:ref %font-face-reference)
77     (:unref %font-face-destroy))
78
79   (defclass font-options (proxy)
80     ((antialias
81       :allocation :virtual 
82       :getter "font_options_get_antialias"
83       :setter "font_options_set_antialias"
84       :accessor font-options-antialias
85       :type antialias)
86      (subpixel-order
87       :allocation :virtual 
88       :getter "font_options_get_subpixel_order"
89       :setter "font_options_set_subpixel_order"
90       :accessor font-options-subpixel-order
91       :type subpixel-order)
92      (hint-style
93       :allocation :virtual 
94       :getter "font_options_get_hint_style"
95       :setter "font_options_set_hint_style"
96       :accessor font-options-hint-style
97       :type hint-style)
98      (hint-metrics
99       :allocation :virtual 
100       :getter "font_options_get_hint_metrics"
101       :setter "font_options_set_hint_metrics"
102       :accessor font-options-hint-metrics
103       :type hint-metrics))
104     (:metaclass proxy-class)    
105     (:ref %font-options-reference)
106     (:unref %font-options-destroy))
107
108   (defclass scaled-font (proxy)
109     ()
110     (:metaclass proxy-class)
111     (:ref %scaled-font-reference)
112     (:unref %scaled-font-destroy))
113
114   (defclass matrix (struct)
115     ((xx :allocation :alien :initarg :xx :initform 1.0
116          :accessor matrix-xx :type double-float)
117      (yx :allocation :alien  :initarg :yx :initform 0.0
118          :accessor matrix-yx :type double-float)
119      (xy :allocation :alien  :initarg :xy :initform 1.0
120          :accessor matrix-xy :type double-float)
121      (yy :allocation :alien  :initarg :yy :initform 0.0
122          :accessor matrix-yy :type double-float)
123      (x0 :allocation :alien  :initarg :x0 :initform 0.0
124          :accessor matrix-x0 :type double-float)
125      (y0 :allocation :alien  :initarg :y0 :initform 0.0
126          :accessor matrix-y0 :type double-float))
127     (:metaclass struct-class))
128
129
130   (defclass text-extents (struct)
131     ((x-bearing :allocation :alien :reader text-extents-x-bearing :type double-float)
132      (y-bearing :allocation :alien :reader text-extents-y-bearing :type double-float)
133      (width :allocation :alien :reader text-extents-width :type double-float)
134      (height :allocation :alien :reader text-extents-height :type double-float)
135      (x-advance :allocation :alien :reader text-extents-x-advance :type double-float)
136      (y-advance :allocation :alien :reader text-extents-y-advance :type double-float))
137     (:metaclass struct-class))
138
139   (defclass pattern (proxy)
140     ((extend
141       :allocation :virtual 
142       :getter "cairo_pattern_get_extend"
143       :setter "cairo_pattern_set_extend"
144       :accessor pattern-extend
145       :type extend)
146      (filter
147       :allocation :virtual 
148       :getter "cairo_pattern_get_filter"
149       :setter "cairo_pattern_set_filter"
150       :accessor pattern-filter
151       :type filter)
152      (matrix
153       :allocation :virtual 
154       :getter "cairo_pattern_get_matrix"
155       :setter "cairo_pattern_set_matrix"
156       :accessor pattern-matrix
157       :type matrix))
158     (:metaclass proxy-class)
159     (:ref %pattern-reference)
160     (:unref %pattern-destroy))
161
162
163   (defclass surface (proxy)
164     ()
165     (:metaclass proxy-class)
166     (:ref %surface-reference)
167     (:unref %surface-destroy))
168
169   (defclass context (proxy)
170     ((target
171       :allocation :virtual 
172       :getter "cairo_get_target"
173       :reader target
174       :type surface)
175      (source
176       :allocation :virtual 
177       :getter "cairo_get_source"
178       :setter "cairo_set_source"
179       :accessor source
180       :type pattern)
181      (antialias
182       :allocation :virtual 
183       :getter "cairo_get_antialias"
184       :setter "cairo_set_antialias"
185       :accessor antialias
186       :type antialias)
187      (tolerance
188       :allocation :virtual 
189       :getter "cairo_get_tolerance"
190       :setter "cairo_set_tolerance"
191       :accessor tolerance
192       :type double-float)
193      (fill-rule
194       :allocation :virtual 
195       :getter "cairo_get_fill_rule"
196       :setter "cairo_set_fill_rule"
197       :accessor fill-rule
198       :type fill-rule)
199      (line-width
200       :allocation :virtual 
201       :getter "cairo_get_line_width"
202       :setter "cairo_set_line_width"
203       :accessor line-width
204       :type double-float)
205      (line-cap
206       :allocation :virtual 
207       :getter "cairo_get_line_cap"
208       :setter "cairo_set_line_cap"
209       :accessor line-cap
210       :type line-cap)
211      (line-join
212       :allocation :virtual 
213       :getter "cairo_get_line_join"
214       :setter "cairo_set_line_join"
215       :accessor line-join
216       :type line-join)
217      (miter-limit
218       :allocation :virtual 
219       :getter "cairo_get_miter_limit"
220       :setter "cairo_set_miter_limit"
221       :accessor miter-limit
222       :type double-float)
223      (font-matrix
224       :allocation :virtual 
225       :getter "cairo_get_font_matrix"
226       :setter "cairo_set_font_matrix"
227       :accessor font-matrix
228       :type matrix)
229      (font-options
230       :allocation :virtual 
231       :getter "cairo_get_font_options"
232       :setter "cairo_set_font_options"
233       :accessor font-options
234       :type font-options)
235      (font-face
236       :allocation :virtual 
237       :getter "cairo_get_font_face"
238       :setter "cairo_set_font_face"
239       :accessor font-face
240       :type font-face)
241      (operator
242       :allocation :virtual 
243       :getter "cairo_get_operator"
244       :setter "cairo_set_operator"
245       :accessor operator
246       :type operator)
247      (matrix
248       :allocation :virtual 
249       :getter matrix
250       :setter "cairo_set_matrix"
251       :writer (setf matrix)
252       :type matrix)
253      )
254     (:metaclass proxy-class)
255     (:ref %reference)
256     (:unref %destroy))
257
258   (defclass image-surface (surface)
259     ((width
260       :allocation :virtual 
261       :getter "cairo_image_surface_get_width"
262       :reader surface-width
263       :type int)
264      (height
265       :allocation :virtual 
266       :getter "cairo_image_surface_get_height"
267       :reader surface-height
268       :type int))
269     (:metaclass proxy-class)
270     (:ref %surface-reference)
271     (:unref %surface-destroy))
272
273
274 ;;   (defclass path (proxy)
275 ;;     ()
276 ;;     (:metaclass proxy-class))
277
278 )
279
280
281 ;;; Cairo context
282
283 (defbinding %reference () nil
284   (location pointer))
285
286 (defbinding %destroy () nil
287   (location pointer))
288
289 (defbinding (save-context "cairo_save") () nil
290   (cr context))
291
292 (defbinding (restore-context "cairo_restore") () nil
293   (cr context))
294
295 (defmacro with-context ((cr) &body body)
296   (let ((context (make-symbol "CONTEXT")))
297     `(let ((,context ,cr))
298        (save-context ,context)
299        (unwind-protect
300            (progn ,@body)
301          (restore-context ,context)))))
302
303 (defbinding status () status
304   (cr context))
305
306 (defbinding (set-source-color "cairo_set_source_rgba") (cr red green blue &optional (alpha 1.0)) nil
307   (cr context)
308   (red double-float)
309   (green double-float)
310   (blue double-float)
311   (alpha double-float))
312
313 (defbinding set-source-surface () nil
314   (cr context)
315   (surface surface)
316   (x double-float)
317   (y double-float))
318
319 (defbinding set-dash (cr dashes &optional (offset 0.0)) nil
320   (cr context)
321   (dashes (vector double-float))
322   ((length dashes) int)
323   (offset double-float))
324
325 (defbinding (paint "cairo_paint_with_alpha") (cr &optional (alpha 1.0)) nil
326   (cr context)
327   (alpha double-float))
328
329 (defbinding mask () nil
330   (cr context)
331   (pattern pattern))
332
333 (defbinding mask-surface () nil
334   (cr context)
335   (surface surface)
336   (surface-x double-float)
337   (surface-y double-float))
338
339 (defmacro defoperator (name &optional clip-p)
340   (let ((iname (intern (format nil "%~A" name)))
341         (pname (intern (format nil "%~A-PRESERVE" name))))
342     `(progn
343        (defbinding ,iname () nil
344          (cr context))
345        (defbinding ,pname () nil
346          (cr context))
347        (defun ,name (cr &optional preserve)
348          (if preserve
349              (,pname cr)
350            (,iname cr)))
351        ,(unless clip-p
352           (let ((tname (intern (format nil "IN-~A-P" name)))
353                 (ename (intern (format nil "~A-EXTENTS" name))))
354             `(progn
355                (defbinding ,tname () boolean
356                  (cr context)
357                  (x double-float)
358                  (y double-float))
359                (defbinding ,ename () boolean
360                  (cr context)
361                  (x1 double-float :out)
362                  (y1 double-float :out)
363                  (x2 double-float :out)
364                  (y2 double-float :out))))))))
365
366 (defoperator clip t)
367 (defoperator stroke)
368 (defoperator fill)
369
370 (defbinding reset-clip () nil
371   (cr context))
372
373 (defbinding copy-page () nil
374   (cr context))
375
376 (defbinding show-page () nil
377   (cr context))
378
379
380 ;;; Paths
381
382 (defbinding get-current-point () nil
383   (cr context)
384   (x double-float :out)
385   (y double-float :out))
386
387 (defbinding new-path () nil
388   (cr context))
389
390 (defbinding close-path () nil
391   (cr context))
392
393 (defbinding arc () nil
394   (cr context)
395   (xc double-float)
396   (yc double-float)
397   (radius double-float)
398   (angle1 double-float)
399   (angle2 double-float))
400
401 (defbinding arc-negative () nil
402   (cr context)
403   (xc double-float)
404   (yc double-float)
405   (radius double-float)
406   (angle1 double-float)
407   (angle2 double-float))
408
409 (defun circle (cr x y radius)
410   (arc cr x y radius 0.0 (* pi 2)))
411
412 (defmacro defpath (name &rest args)
413   (let ((relname (intern (format nil "REL-~A" name))))
414     `(progn
415        (defbinding ,name () nil
416          (cr context)
417          ,@args)
418        (defbinding ,relname () nil
419          (cr context)
420          ,@args))))
421
422 (defpath curve-to
423   (x1 double-float)
424   (y1 double-float)
425   (x2 double-float)
426   (y2 double-float)
427   (x3 double-float)
428   (y3 double-float))
429
430 (defpath line-to
431   (x double-float)
432   (y double-float))
433
434 (defpath move-to
435   (x double-float)
436   (y double-float))
437
438 (defbinding rectangle () nil
439   (cr context)
440   (x double-float)
441   (y double-float)
442   (width double-float)
443   (height double-float))
444
445 (defbinding glyph-path (cr glyphs) nil
446   (cr context)
447   (glyphs (vector glyph))
448   ((length glyphs) int))
449
450 (defbinding text-path () nil
451   (cr context)
452   (text string))
453
454
455
456 ;;; Patterns
457
458 (defbinding (pattern-add-color-stop "cairo_pattern_add_color_stop_rgba")
459     (pattern offset red green blue &optional (alpha 1.0)) nil
460   (pattern pattern)
461   (offset double-float)
462   (red double-float)
463   (green double-float)
464   (blue double-float)
465   (alpha double-float))
466
467 (defbinding (pattern-create "cairo_pattern_create_rgba")
468     (red green blue &optional (alpha 1.0)) pattern   
469   (red double-float)
470   (green double-float)
471   (blue double-float)
472   (alpha double-float))
473
474 (defbinding pattern-create-for-surface () pattern
475   (surface surface))
476
477 (defbinding pattern-create-linear () pattern
478   (x0 double-float)
479   (y0 double-float)
480   (x1 double-float)
481   (y1 double-float))
482
483 (defbinding pattern-create-radial () pattern
484   (cx0 double-float)
485   (cy0 double-float)
486   (radius0 double-float)
487   (cx1 double-float)
488   (cy1 double-float)
489   (radius1 double-float))
490
491 (defbinding %pattern-reference () nil
492   (location pointer))
493
494 (defbinding %pattern-destroy () nil
495   (location pointer))
496
497 (defbinding pattern-status () status
498   (pattern pattern))
499
500
501
502 ;;; Transformations
503
504 (defbinding translate () nil
505   (cr context)
506   (tx double-float)
507   (ty double-float))
508
509 (defbinding scale () nil
510   (cr context)
511   (sx double-float)
512   (sy double-float))
513
514 (defbinding rotate () nil
515   (cr context)
516   (angle double-float))
517
518 (defbinding transform () nil
519   (cr context)
520   (matrix matrix))
521
522 (defbinding (matrix "cairo_get_matrix") () nil
523   (cr context)
524   ((make-instance 'matrix) matrix :in/return))
525
526 (defbinding identity-matrix () nil
527   (cr context))
528
529 (defbinding user-to-device () nil
530   (cr context)
531   (x double-float :in/out)
532   (y double-float :in/out))
533
534 (defbinding user-to-device-distance () nil
535   (cr context)
536   (dx double-float :in/out)
537   (dy double-float :in/out))
538
539 (defbinding device-to-user () nil
540   (cr context)
541   (x double-float :in/out)
542   (y double-float :in/out))
543
544 (defbinding device-to-user-distance () nil
545   (cr context)
546   (dx double-float :in/out)
547   (dy double-float :in/out))
548
549
550 ;;; Text
551
552 (defbinding select-font-face () nil
553   (cr context)
554   (family string)
555   (slant font-slant)
556   (weight font-weight))
557
558 (defbinding set-font-size () nil
559   (cr context)
560   (size double-float))
561
562 (defbinding show-text () nil
563   (cr context)
564   (text string))
565
566 (defbinding show-glyphs () nil
567   (cr context)
568   (glyphs (vector glyph))
569   ((length glyphs) int))
570
571 (defbinding font-extents () boolean
572   (cr context))
573
574 (defbinding text-extents (cr text &optional (extents (make-instance 'text-extents))) nil
575   (cr context)
576   (text string)
577   (extents text-extents :in/return))
578
579 (defbinding glyph-extents (cr glyphs &optional (extents (make-instance 'text-extents))) nil
580   (cr context)
581   (glyphs (vector glyph))
582   ((length glyphs) int)
583   (extents text-extents :in/return))
584
585
586 ;;; Fonts
587
588 (defbinding %font-face-reference () nil
589   (location pointer))
590
591 (defbinding %font-face-destroy () nil
592   (location pointer))
593
594 (defbinding font-face-status () status
595   (font-face font-face))
596
597
598
599 ;;; Scaled Fonts
600
601 (defbinding %scaled-font-reference () nil
602   (location pointer))
603
604 (defbinding %scaled-font-destroy () nil
605   (location pointer))
606
607 (defbinding scaled-font-status () status
608   (scaled-font scaled-font))
609
610 (defbinding scaled-font-extents (scaled-font &optional (extents (make-instance 'text-extents))) nil
611   (scaled-font scaled-font)
612   (extents text-extents :in/return))
613
614 (defbinding scaled-font-glyph-extents (scaled-font glyphs &optional (extents (make-instance 'text-extents))) nil
615   (scaled-font scaled-font)
616   (glyphs (vector glyph))
617   ((length glyphs) int)
618   (extents text-extents :in/return))
619
620 (defbinding %scaled-font-create () pointer
621   (font-face font-face)
622   (font-matrix matrix)
623   (ctm matrix)
624   (options font-options))
625
626 (defmethod allocate-foreign ((scaled-font scaled-font) &key font-face font-matrix cmt options)
627   (%scaled-font-create font-face font-matrix cmt options))
628
629
630
631 ;;; Font Options
632
633
634 (defbinding %font-options-copy () nil
635   (location pointer))
636
637 (defbinding %font-options-destroy () nil
638   (location pointer))
639
640 (defbinding font-options-status () status
641   (font-options font-options))
642
643 (defbinding %font-options-create () pointer)
644
645 (defmethod allocate-foreign ((font-options font-options) &rest initargs)
646   (declare (ignore initargs))
647   (%font-options-create))
648
649 (defbinding font-options-merge () nil
650   (options1 font-options :in/return)
651   (options2 font-options))
652
653 (defbinding font-options-hash () unsigned-int
654   (options font-options))
655
656 (defbinding font-options-equal-p () boolean
657   (options1 font-options)
658   (options2 font-options))
659
660
661
662 ;;; Surfaces
663
664 (defbinding %surface-reference () nil
665   (location pointer))
666
667 (defbinding %surface-destroy () nil
668   (location pointer))
669
670 (defbinding surface-create-similar () surface
671   (other surface)
672   (format surface-format )
673   (width int)
674   (height int))
675
676 (defbinding surface-finish () nil
677   (surface surface))
678
679 (defbinding surface-flush () nil
680   (surface surface))
681
682 (defbinding surface-get-font-options () nil
683   (surface surface)
684   ((make-instance 'font-options) font-options :in/return))
685
686 (defbinding surface-set-device-offset () nil
687   (surface surface)
688   (x-offset double-float)
689   (y-offset double-float))
690
691 (defbinding surface-status () status
692   (surface surface))
693
694 (defbinding %surface-mark-dirty () nil
695   (surface surface))
696
697 (defbinding %surface-mark-dirty-rectangle () nil
698   (surface surface)
699   (x int)
700   (y int)
701   (width int)
702   (height int))
703
704 (defun surface-mark-dirty (surface &optional x y width height)
705   (if x
706       (%surface-mark-dirty-rectangle surface x y width height)
707     (%surface-mark-dirty surface)))
708
709
710
711 ;; Image Surface
712
713 ;; Should data be automatically freed when the surface is GCed?
714 (defmethod allocate-foreign ((surface image-surface) 
715                              &key width height stride format data)
716   (if (not data)
717       (%image-surface-create format width height)
718     (%image-surface-create-for-data data format width height 
719      (or 
720       stride
721       (let ((element-size (cdr (assoc format '((:argb32 . 4) (:rgb24 . 4) (:a8 . 1) (:a1 1/8))))))
722         (ceiling (* width element-size)))))))
723
724
725 (defbinding %image-surface-create () image-surface
726   (format surface-format)
727   (width int)
728   (hegit int))
729
730 (defbinding %image-surface-create-for-data () image-surface
731   (data pointer)
732   (format surface-format)
733   (width int)
734   (hegit int)
735   (stride int))
736
737
738
739 ;;; PNG Surface
740
741 (defbinding image-surface-create-from-png (filename) image-surface
742   ((truename filename) pathname))
743
744
745
746
747 ;;; Matrices
748
749 (defbinding matrix-init () nil
750   (matrix matrix :in/return)
751   (xx double-float) (yx double-float) 
752   (xy double-float) (yy double-float) 
753   (x0 double-float) (y0 double-float))
754
755 (defbinding matrix-init-identity () nil
756   (matrix matrix :in/return))
757
758 (defbinding matrix-init-translate () nil
759   (matrix matrix :in/return)
760   (tx double-float)
761   (ty double-float))
762
763 (defbinding matrix-init-scale () nil
764   (matrix matrix :in/return)
765   (sx double-float)
766   (sy double-float))
767
768 (defbinding matrix-init-rotate () nil
769   (matrix matrix :in/return)
770   (radians double-float))
771
772 (defbinding matrix-translate () nil
773   (matrix matrix :in/return)
774   (tx double-float)
775   (ty double-float))
776
777 (defbinding matrix-scale () nil
778   (matrix matrix :in/return)
779   (sx double-float)
780   (sy double-float))
781
782 (defbinding matrix-rotate () nil
783   (matrix matrix :in/return)
784   (radians double-float))
785
786 (defbinding matrix-invert () nil
787   (matrix matrix :in/return))
788
789 (defbinding matrix-multiply () nil
790   (result matrix :out)
791   (a matrix)
792   (b matrix))
793
794 (defbinding matrix-transform-distance () nil
795   (matrix matrix :in/return)
796   (dx double-float)
797   (dy double-float))
798
799 (defbinding matrix-transform-point () nil
800   (matrix matrix :in/return)
801   (x double-float)
802   (y double-float))
803
804
805