unmaintained: reanimated cairo demos, mostly working.

db4
John Benediktsson 2014-10-24 21:39:18 -07:00
parent 67dc51160c
commit 798f6445bd
6 changed files with 192 additions and 177 deletions

View File

@ -8,7 +8,7 @@
USING: cairo.ffi math math.constants byte-arrays kernel ui
ui.render combinators ui.gadgets opengl.gl accessors
namespaces opengl ;
namespaces opengl sequences ;
IN: cairo-demo
@ -21,16 +21,17 @@ IN: cairo-demo
TUPLE: cairo-demo-gadget < gadget image-array cairo-t ;
USE: io
USE: formatting
M: cairo-demo-gadget draw-gadget* ( gadget -- )
origin get [
0 0 glRasterPos2i
1.0 -1.0 glPixelZoom
[ 384 256 GL_RGBA GL_UNSIGNED_BYTE ] dip
image-array>> glDrawPixels
] with-translation ;
0 0 glRasterPos2i
1.0 -1.0 glPixelZoom
[ 384 256 GL_RGBA GL_UNSIGNED_BYTE ] dip
image-array>> glDrawPixels ;
: create-surface ( gadget -- cairo_surface_t )
make-image-array [ swap (>>image-array) ] keep
make-image-array [ swap image-array<< ] keep
convert-array-to-surface ;
: init-cairo ( gadget -- cairo_t )
@ -69,13 +70,13 @@ ERROR: no-cairo-t ;
PRIVATE>
M: cairo-demo-gadget graft* ( gadget -- )
dup dup init-cairo swap (>>cairo-t) draw-hello-world ;
dup dup init-cairo swap cairo-t<< draw-hello-world ;
M: cairo-demo-gadget ungraft* ( gadget -- )
cairo-t>> cairo_destroy ;
: <cairo-demo-gadget> ( -- gadget )
cairo-demo-gadget new-gadget ;
cairo-demo-gadget new ;
: run ( -- )
[

View File

@ -3,23 +3,41 @@
USING: sequences math kernel byte-arrays cairo.ffi cairo
io.backend ui.gadgets accessors opengl.gl arrays fry
classes ui.render namespaces destructors libc ;
IN: cairo.gadgets
IN: cairo-gadgets
SYMBOL: current-cairo
: cr ( -- cairo )
current-cairo get ;
<PRIVATE
: width>stride ( width -- stride ) 4 * ;
: image-dims ( gadget -- width height stride )
dim>> first2 over width>stride ; inline
: image-buffer ( width height stride -- alien )
* nip malloc ; inline
* nip malloc &free ; inline
: with-cairo ( cairo quot -- )
[ &cairo_destroy current-cairo ] dip
'[ @ current-cairo get check-cairo ] with-variable ; inline
: with-surface ( cairo_surface quot -- alien )
[ &cairo_surface_destroy ] dip [ check-surface ] bi ; inline
: with-cairo-from-surface ( cairo_surface quot -- )
'[ cairo_create _ with-cairo ] with-surface ; inline
PRIVATE>
GENERIC: render-cairo* ( gadget -- )
: render-cairo ( gadget -- alien )
[
image-dims
[ image-buffer dup CAIRO_FORMAT_ARGB32 ]
[ image-buffer dup CAIRO_FORMAT_ARGB32 ]
[ cairo_image_surface_create_for_data ] 3bi
] [ '[ _ render-cairo* ] with-cairo-from-surface ] bi ;
@ -31,8 +49,8 @@ TUPLE: cairo-gadget < gadget ;
M: cairo-gadget draw-gadget*
[
[ dim>> ] [ render-cairo &free ] bi
origin get first2 glRasterPos2i
[ dim>> ] [ render-cairo ] bi
0 0 glRasterPos2i
1.0 -1.0 glPixelZoom
[ first2 GL_BGRA GL_UNSIGNED_BYTE ] dip
glDrawPixels

View File

@ -0,0 +1,157 @@
! Copyright (C) 2008 Matthew Willis
! See http://factorcode.org/license.txt for BSD license.
!
! these samples are a subset of the samples on
! http://cairographics.org/samples/
USING: cairo cairo.ffi locals math.constants math
io.backend kernel alien.c-types libc namespaces
cairo-gadgets ui.gadgets accessors specialized-arrays
classes.struct ;
SPECIALIZED-ARRAY: double
IN: cairo-samples
TUPLE: arc-gadget < cairo-gadget ;
M:: arc-gadget render-cairo* ( gadget -- )
128.0 :> xc
128.0 :> yc
100.0 :> radius
pi 1/4 * :> angle1
pi :> angle2
cr 10.0 cairo_set_line_width
cr xc yc radius angle1 angle2 cairo_arc
cr cairo_stroke
! draw helping lines
cr 1 0.2 0.2 0.6 cairo_set_source_rgba
cr 6.0 cairo_set_line_width
cr xc yc 10.0 0 2 pi * cairo_arc
cr cairo_fill
cr xc yc radius angle1 angle1 cairo_arc
cr xc yc cairo_line_to
cr xc yc radius angle2 angle2 cairo_arc
cr xc yc cairo_line_to
cr cairo_stroke
;
TUPLE: clip-gadget < cairo-gadget ;
M: clip-gadget render-cairo* ( gadget -- )
drop
cr 128 128 76.8 0 2 pi * cairo_arc
cr cairo_clip
cr cairo_new_path
cr 0 0 256 256 cairo_rectangle
cr cairo_fill
cr 0 1 0 cairo_set_source_rgb
cr 0 0 cairo_move_to
cr 256 256 cairo_line_to
cr 256 0 cairo_move_to
cr 0 256 cairo_line_to
cr 10 cairo_set_line_width
cr cairo_stroke ;
TUPLE: clip-image-gadget < cairo-gadget ;
M:: clip-image-gadget render-cairo* ( gadget -- )
"resource:misc/icons/Factor_128x128.png"
normalize-path cairo_image_surface_create_from_png :> png
png cairo_image_surface_get_width :> w
png cairo_image_surface_get_height :> h
cr 128 128 76.8 0 2 pi * cairo_arc
cr cairo_clip
cr cairo_new_path
cr 192.0 w / 192.0 h / cairo_scale
cr png 32 32 cairo_set_source_surface
cr cairo_paint
png cairo_surface_destroy ;
TUPLE: dash-gadget < cairo-gadget ;
M:: dash-gadget render-cairo* ( gadget -- )
double-array{ 50 10 10 10 } underlying>> :> dashes
4 :> ndash
cr dashes ndash -50 cairo_set_dash
cr 10 cairo_set_line_width
cr 128.0 25.6 cairo_move_to
cr 230.4 230.4 cairo_line_to
cr -102.4 0 cairo_rel_line_to
cr 51.2 230.4 51.2 128.0 128.0 128.0 cairo_curve_to
cr cairo_stroke ;
TUPLE: gradient-gadget < cairo-gadget ;
M:: gradient-gadget render-cairo* ( gadget -- )
0 0 0 256 cairo_pattern_create_linear :> pat
115.2 102.4 25.6 102.4 102.4 128.0
cairo_pattern_create_radial :> radial
pat 1 0 0 0 1 cairo_pattern_add_color_stop_rgba
pat 0 1 1 1 1 cairo_pattern_add_color_stop_rgba
cr 0 0 256 256 cairo_rectangle
cr pat cairo_set_source
cr cairo_fill
pat cairo_pattern_destroy
radial 0 1 1 1 1 cairo_pattern_add_color_stop_rgba
radial 1 0 0 0 1 cairo_pattern_add_color_stop_rgba
cr radial cairo_set_source
cr 128.0 128.0 76.8 0 2 pi * cairo_arc
cr cairo_fill
radial cairo_pattern_destroy ;
TUPLE: text-gadget < cairo-gadget ;
M: text-gadget render-cairo* ( gadget -- )
drop
cr "Serif" CAIRO_FONT_SLANT_NORMAL CAIRO_FONT_WEIGHT_BOLD
cairo_select_font_face
cr 50 cairo_set_font_size
cr 10 135 cairo_move_to
cr "Hello" cairo_show_text
cr 70 165 cairo_move_to
cr "factor" cairo_text_path
cr 0.5 0.5 1 cairo_set_source_rgb
cr cairo_fill_preserve
cr 0 0 0 cairo_set_source_rgb
cr 2.56 cairo_set_line_width
cr cairo_stroke
! draw helping lines
cr 1 0.2 0.2 0.6 cairo_set_source_rgba
cr 10 135 5.12 0 2 pi * cairo_arc
cr cairo_close_path
cr 70 165 5.12 0 2 pi * cairo_arc
cr cairo_fill ;
TUPLE: utf8-gadget < cairo-gadget ;
M: utf8-gadget render-cairo* ( gadget -- )
drop
cr "Sans" CAIRO_FONT_SLANT_NORMAL CAIRO_FONT_WEIGHT_NORMAL
cairo_select_font_face
cr 50 cairo_set_font_size
cairo_text_extents_t malloc-struct
cr "日本語" pick cairo_text_extents
cr over
[ width>> 2 / ] [ x_bearing>> ] bi +
128 swap - pick
[ height>> 2 / ] [ y_bearing>> ] bi +
128 swap - cairo_move_to
free
cr "日本語" cairo_show_text
cr 1 0.2 0.2 0.6 cairo_set_source_rgba
cr 6 cairo_set_line_width
cr 128 0 cairo_move_to
cr 0 256 cairo_rel_line_to
cr 0 128 cairo_move_to
cr 256 0 cairo_rel_line_to
cr cairo_stroke ;
USING: quotations ui.gadgets.panes sequences ;
: samples ( -- )
{
arc-gadget clip-gadget clip-image-gadget dash-gadget
gradient-gadget text-gadget utf8-gadget
}
[ new { 256 256 } >>dim gadget. ] each ;
MAIN: samples

View File

@ -1,161 +0,0 @@
! Copyright (C) 2008 Matthew Willis
! See http://factorcode.org/license.txt for BSD license.
!
! these samples are a subset of the samples on
! http://cairographics.org/samples/
USING: cairo cairo.ffi locals math.constants math
io.backend kernel alien.c-types libc namespaces
cairo.gadgets ui.gadgets accessors specialized-arrays.double ;
IN: cairo-samples
TUPLE: arc-gadget < cairo-gadget ;
M:: arc-gadget render-cairo* ( gadget -- )
[let | xc [ 128.0 ]
yc [ 128.0 ]
radius [ 100.0 ]
angle1 [ pi 1/4 * ]
angle2 [ pi ] |
cr 10.0 cairo_set_line_width
cr xc yc radius angle1 angle2 cairo_arc
cr cairo_stroke
! draw helping lines
cr 1 0.2 0.2 0.6 cairo_set_source_rgba
cr 6.0 cairo_set_line_width
cr xc yc 10.0 0 2 pi * cairo_arc
cr cairo_fill
cr xc yc radius angle1 angle1 cairo_arc
cr xc yc cairo_line_to
cr xc yc radius angle2 angle2 cairo_arc
cr xc yc cairo_line_to
cr cairo_stroke
] ;
TUPLE: clip-gadget < cairo-gadget ;
M: clip-gadget render-cairo* ( gadget -- )
drop
cr 128 128 76.8 0 2 pi * cairo_arc
cr cairo_clip
cr cairo_new_path
cr 0 0 256 256 cairo_rectangle
cr cairo_fill
cr 0 1 0 cairo_set_source_rgb
cr 0 0 cairo_move_to
cr 256 256 cairo_line_to
cr 256 0 cairo_move_to
cr 0 256 cairo_line_to
cr 10 cairo_set_line_width
cr cairo_stroke ;
TUPLE: clip-image-gadget < cairo-gadget ;
M:: clip-image-gadget render-cairo* ( gadget -- )
[let* | png [ "resource:misc/icons/Factor_128x128.png"
normalize-path cairo_image_surface_create_from_png ]
w [ png cairo_image_surface_get_width ]
h [ png cairo_image_surface_get_height ] |
cr 128 128 76.8 0 2 pi * cairo_arc
cr cairo_clip
cr cairo_new_path
cr 192.0 w / 192.0 h / cairo_scale
cr png 32 32 cairo_set_source_surface
cr cairo_paint
png cairo_surface_destroy
] ;
TUPLE: dash-gadget < cairo-gadget ;
M:: dash-gadget render-cairo* ( gadget -- )
[let | dashes [ double-array{ 50 10 10 10 } underlying>> ]
ndash [ 4 ] |
cr dashes ndash -50 cairo_set_dash
cr 10 cairo_set_line_width
cr 128.0 25.6 cairo_move_to
cr 230.4 230.4 cairo_line_to
cr -102.4 0 cairo_rel_line_to
cr 51.2 230.4 51.2 128.0 128.0 128.0 cairo_curve_to
cr cairo_stroke
] ;
TUPLE: gradient-gadget < cairo-gadget ;
M:: gradient-gadget render-cairo* ( gadget -- )
[let | pat [ 0 0 0 256 cairo_pattern_create_linear ]
radial [ 115.2 102.4 25.6 102.4 102.4 128.0
cairo_pattern_create_radial ] |
pat 1 0 0 0 1 cairo_pattern_add_color_stop_rgba
pat 0 1 1 1 1 cairo_pattern_add_color_stop_rgba
cr 0 0 256 256 cairo_rectangle
cr pat cairo_set_source
cr cairo_fill
pat cairo_pattern_destroy
radial 0 1 1 1 1 cairo_pattern_add_color_stop_rgba
radial 1 0 0 0 1 cairo_pattern_add_color_stop_rgba
cr radial cairo_set_source
cr 128.0 128.0 76.8 0 2 pi * cairo_arc
cr cairo_fill
radial cairo_pattern_destroy
] ;
TUPLE: text-gadget < cairo-gadget ;
M: text-gadget render-cairo* ( gadget -- )
drop
cr "Serif" CAIRO_FONT_SLANT_NORMAL CAIRO_FONT_WEIGHT_BOLD
cairo_select_font_face
cr 50 cairo_set_font_size
cr 10 135 cairo_move_to
cr "Hello" cairo_show_text
cr 70 165 cairo_move_to
cr "factor" cairo_text_path
cr 0.5 0.5 1 cairo_set_source_rgb
cr cairo_fill_preserve
cr 0 0 0 cairo_set_source_rgb
cr 2.56 cairo_set_line_width
cr cairo_stroke
! draw helping lines
cr 1 0.2 0.2 0.6 cairo_set_source_rgba
cr 10 135 5.12 0 2 pi * cairo_arc
cr cairo_close_path
cr 70 165 5.12 0 2 pi * cairo_arc
cr cairo_fill ;
TUPLE: utf8-gadget < cairo-gadget ;
M: utf8-gadget render-cairo* ( gadget -- )
drop
cr "Sans" CAIRO_FONT_SLANT_NORMAL CAIRO_FONT_WEIGHT_NORMAL
cairo_select_font_face
cr 50 cairo_set_font_size
"cairo_text_extents_t" malloc-object
cr "日本語" pick cairo_text_extents
cr over
[ cairo_text_extents_t-width 2 / ]
[ cairo_text_extents_t-x_bearing ] bi +
128 swap - pick
[ cairo_text_extents_t-height 2 / ]
[ cairo_text_extents_t-y_bearing ] bi +
128 swap - cairo_move_to
free
cr "日本語" cairo_show_text
cr 1 0.2 0.2 0.6 cairo_set_source_rgba
cr 6 cairo_set_line_width
cr 128 0 cairo_move_to
cr 0 256 cairo_rel_line_to
cr 0 128 cairo_move_to
cr 256 0 cairo_rel_line_to
cr cairo_stroke ;
USING: quotations cairo.gadgets ui.gadgets.panes sequences ;
: samples ( -- )
{
arc-gadget clip-gadget clip-image-gadget dash-gadget
gradient-gadget text-gadget utf8-gadget
}
[ new-gadget { 256 256 } >>dim gadget. ] each ;
MAIN: samples