1 ! Copyright (C) 2008 Doug Coleman.
2 ! Copyright (C) 2009 Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: colors fonts cairo.ffi alien alien.c-types kernel accessors
5 sequences namespaces fry continuations destructors math images
9 ERROR: cairo-error message ;
11 : (check-cairo) ( cairo_status_t -- )
12 dup CAIRO_STATUS_SUCCESS =
13 [ drop ] [ cairo_status_to_string cairo-error ] if ;
15 : check-cairo ( cairo -- ) cairo_status (check-cairo) ;
17 : with-cairo ( cairo quot -- )
21 ] with-destructors ; inline
23 : check-surface ( surface -- ) cairo_surface_status (check-cairo) ;
25 : with-surface ( cairo_surface quot -- )
27 _ &cairo_surface_destroy
28 _ [ check-surface ] bi
29 ] with-destructors ; inline
31 : with-cairo-from-surface ( cairo_surface quot -- )
32 '[ cairo_create _ with-cairo ] with-surface ; inline
34 : width>stride ( width -- stride ) "uint" heap-size * ; inline
36 : <image-surface> ( data dim -- surface )
37 [ CAIRO_FORMAT_ARGB32 ] dip first2 over width>stride
38 cairo_image_surface_create_for_data
41 : <cairo> ( surface -- cairo ) cairo_create dup check-cairo ; inline
43 : make-bitmap-image ( dim quot -- image )
45 <image-surface> &cairo_surface_destroy
46 cairo_create &cairo_destroy
49 BGRA >>component-order ; inline
51 : dummy-cairo ( -- cr )
52 #! Sometimes we want a dummy context; eg with Pango, we want
53 #! to measure text dimensions to create a new image context with,
54 #! but we need an existing context to measure text dimensions
55 #! with so we use the dummy.
57 CAIRO_FORMAT_ARGB32 0 0 cairo_image_surface_create
61 : set-source-color ( cr color -- )
62 >rgba-components cairo_set_source_rgba ;