diff options
author | Olivier Andrieu <oandrieu@gmail.com> | 2005-05-22 20:03:15 +0000 |
---|---|---|
committer | Hezekiah M. Carty <hcarty@atmos.umd.edu> | 2009-06-18 13:57:45 -0400 |
commit | 8bd02a2891a65fcafe7014bee11f226607b1478a (patch) | |
tree | 4a1010d4fc7d198c1c671d89b031394f92c41cc8 | |
parent | d7b446db0432c300202b6f550427000188e06e8d (diff) |
adapt to cairo big API shakeup
49 files changed, 2039 insertions, 2139 deletions
@@ -1,3 +1,13 @@ +2005-05-22 Olivier Andrieu <oliv__a@users.sourceforge.net> + + * src/*: adjust to big API shakeup. Remove Cairo_channel module, + add Cairo_ps, Cairo_pdf, Cairo_png. GtkCairo and Cairo_ft disabled + for now. + + * test/*: adjust to big API shakeup. + + * configure.ac, README: require cairo 0.5.0 + 2005-03-08 Olivier Andrieu <oliv__a@users.sourceforge.net> * src/ml_cairo_ft.c, src/cairo_ft.ml, src/cairo_ft.mli: @@ -14,8 +14,8 @@ Compiling Dependencies ============ - ocaml 3.08 (may work with 3.07 or even 3.06) - cairo 0.3.0 + ocaml 3.08 + cairo 0.5.0 libsvg-cairo optional 0.1.5 LablGTK optional diff --git a/config.make.in b/config.make.in index 0cf3d55..787bb17 100644 --- a/config.make.in +++ b/config.make.in @@ -20,8 +20,8 @@ CAIRO_LIBS = $(filter-out $(FILT),@CAIRO_LIBS@) GDK_CFLAGS = @GDK_CFLAGS@ GDK_LIBS = $(filter-out $(FILT),@GDK_LIBS@) -GTKCAIRO_CFLAGS = @GTKCAIRO_CFLAGS@ -GTKCAIRO_LIBS = $(filter-out $(FILT),@GTKCAIRO_LIBS@) +# GTKCAIRO_CFLAGS = @GTKCAIRO_CFLAGS@ +# GTKCAIRO_LIBS = $(filter-out $(FILT),@GTKCAIRO_LIBS@) LIBSVG_CAIRO_CFLAGS = @LIBSVG_CAIRO_CFLAGS@ LIBSVG_CAIRO_LIBS = @LIBSVG_CAIRO_LIBS@ diff --git a/configure.ac b/configure.ac index 0aef7d4..b5392e5 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,7 @@ AC_CONFIG_AUX_DIR(support) AC_PROG_OCAML() # Check for cairo -PKG_CHECK_MODULES(CAIRO, cairo >= 0.3.0) +PKG_CHECK_MODULES(CAIRO, cairo >= 0.5.0) # Optional GTK support (for the X11 backend) AC_ARG_WITH(gtk, @@ -24,19 +24,19 @@ if test $use_gtk = yes ; then fi fi -if test $use_gtk = yes ; then - # Check for gtkcairo - PKG_CHECK_MODULES(GTKCAIRO, gtkcairo, use_gtkcairo=yes, use_gtkcairo=no) -else - use_gtkcairo=no -fi +dnl if test $use_gtk = yes ; then +dnl # Check for gtkcairo +dnl PKG_CHECK_MODULES(GTKCAIRO, gtkcairo, use_gtkcairo=yes, use_gtkcairo=no) +dnl else +dnl use_gtkcairo=no +dnl fi # Optional libsvg-cairo support PKG_CHECK_MODULES(LIBSVG_CAIRO, libsvg-cairo, use_libsvg_cairo=yes, use_libsvg_cairo=no) echo echo " GTK+ support: $use_gtk" -echo " GTKCairo support: $use_gtkcairo" +dnl echo " GTKCairo support: $use_gtkcairo" echo " libsvg-cairo support: $use_libsvg_cairo" echo diff --git a/src/.depend_c b/src/.depend_c index d3670d2..b7201ab 100644 --- a/src/.depend_c +++ b/src/.depend_c @@ -1,13 +1,19 @@ -ml_cairo_bigarr.o: ml_cairo_bigarr.c -ml_cairo.o: ml_cairo.c ml_cairo_wrappers.h ml_cairo_channel.h \ - ml_cairo_status.h ml_cairo.h -ml_cairo_channel.o: ml_cairo_channel.c ml_cairo_wrappers.h caml_io.h \ - ml_cairo_channel.h -ml_cairo_ft.o: ml_cairo_ft.c ml_cairo_wrappers.h ml_cairo.h \ - ml_cairo_status.h -ml_cairo_gtkcairo.o: ml_cairo_gtkcairo.c ml_cairo.h -ml_cairo_lablgtk.o: ml_cairo_lablgtk.c ml_cairo.h ml_cairo_status.h -ml_cairo_path.o: ml_cairo_path.c ml_cairo_wrappers.h ml_cairo.h \ - ml_cairo_status.h -ml_cairo_status.o: ml_cairo_status.c ml_cairo.h ml_cairo_status.h +ml_cairo_bigarr.o: ml_cairo_bigarr.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo.o: ml_cairo.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_font.o: ml_cairo_font.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_ft.o: ml_cairo_ft.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_glitz.o: ml_cairo_glitz.c ml_cairo_wrappers.h ml_cairo_glitz.h \ + glitz.tags ml_cairo.h +ml_cairo_gtkcairo.o: ml_cairo_gtkcairo.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_lablgtk.o: ml_cairo_lablgtk.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_matrix.o: ml_cairo_matrix.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_path.o: ml_cairo_path.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_pattern.o: ml_cairo_pattern.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_pdf.o: ml_cairo_pdf.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_png.o: ml_cairo_png.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_ps.o: ml_cairo_ps.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_status.o: ml_cairo_status.c ml_cairo.h ml_cairo_wrappers.h +ml_cairo_surface.o: ml_cairo_surface.c ml_cairo.h ml_cairo_wrappers.h ml_cairo_wrappers.o: ml_cairo_wrappers.c ml_cairo_wrappers.h +ml_glitz_glx.o: ml_glitz_glx.c ml_cairo_wrappers.h ml_cairo_glitz.h +ml_svg_cairo.o: ml_svg_cairo.c ml_cairo.h ml_cairo_wrappers.h diff --git a/src/Makefile b/src/Makefile index 553a5c7..6698f0b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -25,13 +25,16 @@ gtkcairo.opt : gtkcairo.cmxa dllmlgtkcairo.so svgcairo : svg_cairo.cma libmlsvgcairo.a svgcairo.opt : svg_cairo.cmxa dllmlsvgcairo.so -cairo_SRC = cairo_channel.mli cairo_channel.ml cairo.mli cairo.ml \ +cairo_SRC = cairo.mli cairo.ml \ cairo_bigarray.mli cairo_bigarray.ml \ - cairo_ft.mli cairo_ft.ml \ + cairo_png.mli cairo_png.ml \ + cairo_pdf.mli cairo_pdf.ml \ + cairo_ps.mli cairo_ps.ml \ ml_cairo_wrappers.c \ - ml_cairo_status.c ml_cairo_channel.c \ - ml_cairo.c ml_cairo_bigarr.c ml_cairo_path.c \ - ml_cairo_ft.c + ml_cairo.c ml_cairo_status.c ml_cairo_bigarr.c ml_cairo_path.c \ + ml_cairo_surface.c ml_cairo_pattern.c ml_cairo_matrix.c \ + ml_cairo_font.c \ + ml_cairo_png.c ml_cairo_pdf.c ml_cairo_ps.c cairo.cma : $(call mlobjs,$(cairo_SRC)) $(OCAMLMKLIB) -o cairo -oc mlcairo $^ $(CAIRO_LIBS) @@ -89,7 +92,7 @@ endif for lib in dll*.so ; do \ ln -s $(INSTALLDIR)/$$lib $(DESTDIR)$(OCAMLLIB)/stublibs ; done ; fi -DOCFILES = cairo_channel.mli cairo.mli cairo_bigarray.mli cairo_ft.mli +DOCFILES = cairo.mli cairo_bigarray.mli cairo_png.mli cairo_pdf.mli cairo_ps.mli ifdef LABLGTKDIR DOCFILES += cairo_lablgtk.mli ifdef GTKCAIRO_CFLAGS @@ -111,7 +114,7 @@ clean : @$(OCAMLDEP) $^ > $@ -include .depend -include .depend_c +-include .depend_c depend : .depend $(wildcard *.h *.c) gcc -MM -isystem $(OCAMLLIB) -isystem $(C_LABLGTKDIR) $(patsubst -I%,-isystem %,$(GDK_CFLAGS)) $(filter %.c,$^) > .depend_c diff --git a/src/cairo.ml b/src/cairo.ml index 1dab66e..db86429 100644 --- a/src/cairo.ml +++ b/src/cairo.ml @@ -14,145 +14,126 @@ type status = | INVALID_MATRIX | NO_TARGET_SURFACE | NULL_POINTER + | INVALID_STRING + | INVALID_PATH_DATA + | READ_ERROR + | WRITE_ERROR + | SURFACE_FINISHED + | SURFACE_TYPE_MISMATCH + | BAD_NESTING exception Error of status -let init = Callback.register "cairo_status_exn" (Error NULL_POINTER) -type format = - FORMAT_ARGB32 - | FORMAT_RGB24 - | FORMAT_A8 - | FORMAT_A1 -type image = - { data : Obj.t; format : format; width : int; height : int; stride : int } -type point = { mutable x : float ; mutable y : float } +let init = Callback.register_exception "cairo_status_exn" (Error NULL_POINTER) + type t -type surface -type matrix -type pattern -external create : unit -> t = "ml_cairo_create" -external save : cr:t -> unit = "ml_cairo_save" -external restore : cr:t -> unit = "ml_cairo_restore" -external _copy : dest:t -> src:t -> unit = "ml_cairo_copy" -let copy src = let dest = create () in _copy ~dest ~src ; dest -external set_target_surface : - cr:t -> surface:surface -> unit = "ml_cairo_set_target_surface" -external set_target_image : - cr:t -> image -> unit = "ml_cairo_set_target_image" -external set_target_ps : - cr:t -> file:Cairo_channel.t -> width_inches:float -> height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> - unit = "ml_cairo_set_target_ps_bc" "ml_cairo_set_target_ps" -external set_target_pdf : - cr:t -> file:Cairo_channel.t -> width_inches:float -> height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> - unit = "ml_cairo_set_target_pdf_bc" "ml_cairo_set_target_pdf" -external set_target_png : - cr:t -> file:Cairo_channel.t -> format -> width:int -> height:int -> unit = "ml_cairo_set_target_png" - -external suspend_exn : t -> unit = "ml_cairo_suspend_exn" -external resume_exn : t -> unit = "ml_cairo_resume_exn" -external get_suspend_exn : t -> bool = "ml_cairo_get_suspend_exn" +type -'a surface +type -'a pattern +type font_face + +type point = { x : float ; y : float } +type matrix = { + xx : float ; yx : float ; + xy : float ; yy : float ; + x0 : float ; y0 : float + } + +external create : 'a surface -> t = "ml_cairo_create" +external save : t -> unit = "ml_cairo_save" +external restore : t -> unit = "ml_cairo_restore" type operator = OPERATOR_CLEAR - | OPERATOR_SRC - | OPERATOR_DST + + | OPERATOR_SOURCE | OPERATOR_OVER - | OPERATOR_OVER_REVERSE | OPERATOR_IN - | OPERATOR_IN_REVERSE | OPERATOR_OUT - | OPERATOR_OUT_REVERSE | OPERATOR_ATOP - | OPERATOR_ATOP_REVERSE + + | OPERATOR_DEST + | OPERATOR_DEST_OVER + | OPERATOR_DEST_IN + | OPERATOR_DEST_OUT + | OPERATOR_DEST_ATOP + | OPERATOR_XOR | OPERATOR_ADD | OPERATOR_SATURATE -external set_operator : cr:t -> op:operator -> unit = "ml_cairo_set_operator" -external set_rgb_color : - cr:t -> red:float -> green:float -> blue:float -> - unit = "ml_cairo_set_rgb_color" -external set_pattern : - cr:t -> pattern:pattern -> unit = "ml_cairo_set_pattern" -external set_alpha : cr:t -> alpha:float -> unit = "ml_cairo_set_alpha" -external set_tolerance : - cr:t -> tolerance:float -> unit = "ml_cairo_set_tolerance" + +external set_operator : t -> operator -> unit = "ml_cairo_set_operator" + +external set_source_rgb : t -> red:float -> green:float -> blue:float -> unit = "ml_cairo_set_source_rgb" +external set_source_rgba : t -> red:float -> green:float -> blue:float -> alpha:float ->unit = "ml_cairo_set_source_rgba" +external set_source : t -> 'a pattern -> unit = "ml_cairo_set_source" +external set_source_surface : t -> 'a surface -> float -> float -> unit = "ml_cairo_set_source_surface" + +external set_tolerance : t -> float -> unit = "ml_cairo_set_tolerance" + type fill_rule = FILL_RULE_WINDING | FILL_RULE_EVEN_ODD -external set_fill_rule : - cr:t -> fill_rule:fill_rule -> unit = "ml_cairo_set_fill_rule" -external set_line_width : - cr:t -> width:float -> unit = "ml_cairo_set_line_width" +external set_fill_rule : t -> fill_rule -> unit = "ml_cairo_set_fill_rule" +external set_line_width : t -> float -> unit = "ml_cairo_set_line_width" type line_cap = LINE_CAP_BUTT | LINE_CAP_ROUND | LINE_CAP_SQUARE -external set_line_cap : - cr:t -> line_cap:line_cap -> unit = "ml_cairo_set_line_cap" +external set_line_cap : t -> line_cap -> unit = "ml_cairo_set_line_cap" type line_join = LINE_JOIN_MITER | LINE_JOIN_ROUND | LINE_JOIN_BEVEL -external set_line_join : - cr:t -> line_join:line_join -> unit = "ml_cairo_set_line_join" -external set_dash : - cr:t -> dashes:float array -> offset:float -> unit = "ml_cairo_set_dash" -external set_miter_limit : - cr:t -> limit:float -> unit = "ml_cairo_set_miter_limit" -external translate : - cr:t -> tx:float -> ty:float -> unit = "ml_cairo_translate" -external scale : cr:t -> sx:float -> sy:float -> unit = "ml_cairo_scale" -external rotate : cr:t -> angle:float -> unit = "ml_cairo_rotate" -external concat_matrix : - cr:t -> matrix:matrix -> unit = "ml_cairo_concat_matrix" -external set_matrix : cr:t -> matrix:matrix -> unit = "ml_cairo_set_matrix" -external default_matrix : cr:t -> unit = "ml_cairo_default_matrix" -external identity_matrix : cr:t -> unit = "ml_cairo_identity_matrix" -external transform_point : - cr:t -> point -> unit = "ml_cairo_transform_point" -external transform_distance : - cr:t -> point -> unit = "ml_cairo_transform_distance" -external inverse_transform_point : - cr:t -> point -> unit = "ml_cairo_inverse_transform_point" -external inverse_transform_distance : - cr:t -> point -> unit = "ml_cairo_inverse_transform_distance" -external new_path : cr:t -> unit = "ml_cairo_new_path" -external move_to : cr:t -> x:float -> y:float -> unit = "ml_cairo_move_to" -let move_to_point ~cr { x = x ; y = y } = move_to ~cr ~x ~y -external line_to : cr:t -> x:float -> y:float -> unit = "ml_cairo_line_to" -let line_to_point ~cr { x = x ; y = y } = line_to ~cr ~x ~y -external curve_to : - cr:t -> x1:float -> y1:float -> x2:float -> y2:float -> x3:float -> - y3:float -> unit = "ml_cairo_curve_to_bc" "ml_cairo_curve_to" -let curve_to_point ~cr {x=x1; y=y1} {x=x2; y=y2} {x=x3; y=y3} = curve_to ~cr ~x1 ~y1 ~x2 ~y2 ~x3 ~y3 -external arc : - cr:t -> xc:float -> yc:float -> radius:float -> angle1:float -> - angle2:float -> unit = "ml_cairo_arc_bc" "ml_cairo_arc" -external arc_negative : - cr:t -> xc:float -> yc:float -> radius:float -> angle1:float -> - angle2:float -> unit = "ml_cairo_arc_negative_bc" "ml_cairo_arc_negative" -external rel_move_to : - cr:t -> dx:float -> dy:float -> unit = "ml_cairo_rel_move_to" -external rel_line_to : - cr:t -> dx:float -> dy:float -> unit = "ml_cairo_rel_line_to" -external rel_curve_to : - cr:t -> dx1:float -> dy1:float -> dx2:float -> dy2:float -> dx3:float -> - dy3:float -> unit = "ml_cairo_rel_curve_to_bc" "ml_cairo_rel_curve_to" -external rectangle : - cr:t -> x:float -> y:float -> width:float -> height:float -> - unit = "ml_cairo_rectangle" -external close_path : cr:t -> unit = "ml_cairo_close_path" -external stroke : cr:t -> unit = "ml_cairo_stroke" -external fill : cr:t -> unit = "ml_cairo_fill" -external copy_page : cr:t -> unit = "ml_cairo_copy_page" -external show_page : cr:t -> unit = "ml_cairo_show_page" -external in_stroke : cr:t -> x:float -> y:float -> bool = "ml_cairo_in_stroke" -external in_fill : cr:t -> x:float -> y:float -> bool = "ml_cairo_in_fill" -external stroke_extents : cr:t -> float * float * float * float = "ml_cairo_stroke_extents" -external fill_extents : cr:t -> float * float * float * float = "ml_cairo_fill_extents" -external init_clip : cr:t -> unit = "ml_cairo_init_clip" -external clip : cr:t -> unit = "ml_cairo_clip" -type font +external set_line_join : t -> line_join -> unit = "ml_cairo_set_line_join" +external set_dash : t -> float array -> float -> unit = "ml_cairo_set_dash" +external set_miter_limit : t -> float -> unit = "ml_cairo_set_miter_limit" +external translate : t -> tx:float -> ty:float -> unit = "ml_cairo_translate" +external scale : t -> sx:float -> sy:float -> unit = "ml_cairo_scale" +external rotate : t -> angle:float -> unit = "ml_cairo_rotate" +external transform : t -> matrix -> unit = "ml_cairo_trasnform" +external set_matrix : t -> matrix -> unit = "ml_cairo_set_matrix" +external identity_matrix : t -> unit = "ml_cairo_identity_matrix" +external user_to_device : t -> point -> point = "ml_cairo_user_to_device" +external user_to_device_distance : t -> point -> point = "ml_cairo_user_to_device_distance" +external device_to_user : t -> point -> point = "ml_cairo_device_to_user" +external device_to_user_distance : t -> point -> point = "ml_cairo_device_to_user_distance" + +external new_path : t -> unit = "ml_cairo_new_path" +external move_to : t -> x:float -> y:float -> unit = "ml_cairo_move_to" +let move_to_point cr { x = x ; y = y } = move_to cr ~x ~y +external line_to : t -> x:float -> y:float -> unit = "ml_cairo_line_to" +let line_to_point cr { x = x ; y = y } = line_to cr ~x ~y +external curve_to : t -> x1:float -> y1:float -> x2:float -> y2:float -> x3:float -> y3:float -> unit = "ml_cairo_curve_to_bc" "ml_cairo_curve_to" +let curve_to_point cr {x=x1; y=y1} {x=x2; y=y2} {x=x3; y=y3} = curve_to cr ~x1 ~y1 ~x2 ~y2 ~x3 ~y3 +external arc : t -> xc:float -> yc:float -> radius:float -> angle1:float -> angle2:float -> unit = "ml_cairo_arc_bc" "ml_cairo_arc" +external arc_negative : t -> xc:float -> yc:float -> radius:float -> angle1:float -> angle2:float -> unit = "ml_cairo_arc_negative_bc" "ml_cairo_arc_negative" +external rel_move_to : t -> dx:float -> dy:float -> unit = "ml_cairo_rel_move_to" +external rel_line_to : t -> dx:float -> dy:float -> unit = "ml_cairo_rel_line_to" +external rel_curve_to : t -> dx1:float -> dy1:float -> dx2:float -> dy2:float -> dx3:float -> dy3:float -> unit = "ml_cairo_rel_curve_to_bc" "ml_cairo_rel_curve_to" +external rectangle : t -> x:float -> y:float -> width:float -> height:float -> unit = "ml_cairo_rectangle" +external close_path : t -> unit = "ml_cairo_close_path" + +external paint : t -> unit = "ml_cairo_paint" +external paint_with_alpha : t -> float -> unit = "ml_cairo_paint_with_alpha" + +external mask : t -> 'a pattern -> unit = "ml_cairo_mask" +external mask_surface : t -> 'a surface -> surface_x:float -> surface_y:float -> unit = "ml_cairo_mask_surface" + +external stroke : t -> unit = "ml_cairo_stroke" +external stroke_preserve : t -> unit = "ml_cairo_stroke_preserve" +external fill : t -> unit = "ml_cairo_fill" +external fill_preserve : t -> unit = "ml_cairo_fill_preserve" +external copy_page : t -> unit = "ml_cairo_copy_page" +external show_page : t -> unit = "ml_cairo_show_page" + +external in_stroke : t -> point -> bool = "ml_cairo_in_stroke" +external in_fill : t -> point -> bool = "ml_cairo_in_fill" + +external stroke_extents : t -> float * float * float * float = "ml_cairo_stroke_extents" +external fill_extents : t -> float * float * float * float = "ml_cairo_fill_extents" +external clip : t -> unit = "ml_cairo_clip" +external clip_preserve : t -> unit = "ml_cairo_clip_preserve" +external reset_clip : t -> unit = "ml_cairo_reset_clip" + + type glyph = { index : int; glyph_x : float; glyph_y : float; } type text_extents = { x_bearing : float ; @@ -174,59 +155,81 @@ type font_slant = FONT_SLANT_NORMAL | FONT_SLANT_ITALIC | FONT_SLANT_OBLIQUE -external select_font : - cr:t -> family:string -> slant:font_slant -> weight:font_weight -> - unit = "ml_cairo_select_font" -external scale_font : cr:t -> scale:float -> unit = "ml_cairo_scale_font" -external transform_font : - cr:t -> matrix:matrix -> unit = "ml_cairo_transform_font" -external show_text : cr:t -> utf8:string -> unit = "ml_cairo_show_text" -external show_glyphs : - cr:t -> glyph array -> unit = "ml_cairo_show_glyphs" -external current_font : cr:t -> font = "ml_cairo_current_font" -external current_font_extents : - cr:t -> font_extents = "ml_cairo_current_font_extents" -external set_font : cr:t -> font:font -> unit = "ml_cairo_set_font" -external text_extents : t -> utf8:string -> text_extents = "ml_cairo_text_extents" +external select_font_face : t -> string -> font_slant -> font_weight -> unit = "ml_cairo_select_font_face" +external set_font_size : t -> float -> unit = "ml_cairo_set_font_size" +external set_font_matrix : t -> matrix -> unit = "ml_cairo_set_font_matrix" +external get_font_matrix : t -> matrix = "ml_cairo_get_font_matrix" +external show_text : t -> string -> unit = "ml_cairo_show_text" +external show_glyphs : t -> glyph array -> unit = "ml_cairo_show_glyphs" +external get_font_face : t -> font_face = "ml_cairo_get_font_face" +external font_extents : t -> font_extents = "ml_cairo_font_extents" +external set_font_face : t -> font_face -> unit = "ml_cairo_set_font_face" +external text_extents : t -> string -> text_extents = "ml_cairo_text_extents" external glyph_extents : t -> glyph array -> text_extents = "ml_cairo_glyph_extents" -external text_path : t -> utf8:string -> unit = "ml_cairo_text_path" +external text_path : t -> string -> unit = "ml_cairo_text_path" external glyph_path : t -> glyph array -> unit = "ml_cairo_glyph_path" -external font_extents : font -> matrix -> font_extents = "ml_cairo_font_extents" -external font_glyph_extents : font -> matrix -> glyph array -> text_extents = "ml_cairo_font_glyph_extents" -external show_surface : - cr:t -> surface:surface -> width:int -> height:int -> - unit = "ml_cairo_show_surface" -external current_operator : cr:t -> operator = "ml_cairo_current_operator" -external current_rgb_color : - cr:t -> float * float * float = "ml_cairo_current_rgb_color" -external current_pattern : cr:t -> pattern = "ml_cairo_current_pattern" -external current_alpha : cr:t -> float = "ml_cairo_current_alpha" -external current_tolerance : cr:t -> float = "ml_cairo_current_tolerance" -external current_point : cr:t -> point = "ml_cairo_current_point" -external current_fill_rule : cr:t -> fill_rule = "ml_cairo_current_fill_rule" -external current_line_width : cr:t -> float = "ml_cairo_current_line_width" -external current_line_cap : cr:t -> line_cap = "ml_cairo_current_line_cap" -external current_line_join : cr:t -> line_join = "ml_cairo_current_line_join" -external current_miter_limit : cr:t -> float = "ml_cairo_current_miter_limit" -external current_matrix : - cr:t -> matrix:matrix -> unit = "ml_cairo_current_matrix" -external current_target_surface : - cr:t -> surface = "ml_cairo_current_target_surface" + +external get_operator : t -> operator = "ml_cairo_get_operator" +external get_source : t -> 'a pattern = "ml_cairo_get_source" +external get_tolerance : t -> float = "ml_cairo_get_tolerance" +external get_current_point : t -> point = "ml_cairo_get_current_point" +external get_fill_rule : t -> fill_rule = "ml_cairo_get_fill_rule" +external get_line_width : t -> float = "ml_cairo_get_line_width" +external get_line_cap : t -> line_cap = "ml_cairo_get_line_cap" +external get_line_join : t -> line_join = "ml_cairo_get_line_join" +external get_miter_limit : t -> float = "ml_cairo_get_miter_limit" +external get_matrix : t -> matrix = "ml_cairo_get_matrix" +external get_target : t -> 'a surface = "ml_cairo_get_target" + +type flat_path = [ + | `MOVE_TO of point + | `LINE_TO of point + | `CLOSE ] +type path = [ + | flat_path + | `CURVE_TO of point * point * point ] +external fold_path : t -> ('a -> [> path] -> 'a) -> 'a -> 'a = "ml_cairo_copy_path" +external fold_path_flat : t -> ('a -> [> flat_path] -> 'a) -> 'a -> 'a = "ml_cairo_copy_path_flat" + +let append_path cr = function + | `MOVE_TO p -> move_to_point cr p + | `LINE_TO p -> line_to_point cr p + | `CLOSE -> close_path cr + | `CURVE_TO (p1, p2, p3) -> curve_to_point cr p1 p2 p3 external status : t -> status option = "ml_cairo_status" external status_string : t -> string = "ml_cairo_status_string" -external surface_create_for_image : - image -> surface = "ml_cairo_surface_create_for_image" -external surface_create_similar : - other:surface -> format:format -> width:int -> height:int -> - surface = "ml_cairo_surface_create_similar" -external surface_set_repeat : - surface:surface -> repeat:bool -> unit = "ml_cairo_surface_set_repeat" -external surface_set_matrix : - surface:surface -> matrix:matrix -> unit = "ml_cairo_surface_set_matrix" -external surface_get_matrix : - surface:surface -> matrix:matrix -> unit = "ml_cairo_surface_get_matrix" + + +(* surface *) +type format = + FORMAT_ARGB32 + | FORMAT_RGB24 + | FORMAT_A8 + | FORMAT_A1 + +external surface_create_similar : 'a surface -> format -> width:int -> height:int -> 'a surface = "ml_cairo_surface_create_similar" + +external surface_finish : 'a surface -> unit = "ml_cairo_surface_finish" + +external surface_set_device_offset : 'a surface -> float -> float -> unit = "ml_cairo_surface_set_device_offset" + + +type image_surface = [`Any|`Image] surface + +external image_surface_create : format -> width:int -> height:int -> image_surface = "ml_cairo_image_surface_create" +external image_surface_get_width : [>`Image] surface -> int = "ml_cairo_image_surface_get_width" +external image_surface_get_height : [>`Image] surface -> int = "ml_cairo_image_surface_get_height" + + + +(* pattern *) +type extend = + EXTEND_NONE + | EXTEND_REPEAT + | EXTEND_REFLECT + type filter = FILTER_FAST | FILTER_GOOD @@ -234,78 +237,56 @@ type filter = | FILTER_NEAREST | FILTER_BILINEAR | FILTER_GAUSSIAN -external surface_set_filter : - surface:surface -> filter:filter -> unit = "ml_cairo_surface_set_filter" -external surface_get_filter : surface:surface -> filter = "ml_cairo_surface_get_filter" -external pattern_create_for_surface : surface -> pattern = "ml_cairo_pattern_create_for_surface" -external pattern_create_linear : x0:float -> y0:float -> x1:float -> y1:float -> pattern - = "ml_cairo_pattern_create_linear" -external pattern_create_radial : - cx0:float -> cy0:float -> radius0:float -> - cx1:float -> cy1:float -> radius1:float -> pattern - = "ml_cairo_pattern_create_radial_bc" "ml_cairo_pattern_create_radial" -external pattern_add_color_stop : - pattern -> offset:float -> - red:float -> green:float -> blue:float -> alpha:float -> unit - = "ml_cairo_pattern_add_color_stop_bc" "ml_cairo_pattern_add_color_stop" -external pattern_set_matrix : pattern -> matrix -> unit = "ml_cairo_pattern_set_matrix" -external pattern_get_matrix : pattern -> matrix -> unit = "ml_cairo_pattern_get_matrix" -type extend = - | EXTEND_NONE - | EXTEND_REPEAT - | EXTEND_REFLECT -external pattern_set_extend : pattern -> extend -> unit = "ml_cairo_pattern_set_extend" -external pattern_get_extend : pattern -> extend = "ml_cairo_pattern_get_extend" -external pattern_set_filter : pattern -> filter -> unit = "ml_cairo_pattern_set_filter" -external pattern_get_filter : pattern -> filter = "ml_cairo_pattern_get_filter" -external image_surface_create : - format:format -> width:int -> height:int -> - surface = "ml_cairo_image_surface_create" -external image_surface_create_for_data : - image -> surface = "ml_cairo_image_surface_create_for_data" -external ps_surface_create : - file:Cairo_channel.t -> width_inches:float -> height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> - surface = "ml_cairo_ps_surface_create" -external pdf_surface_create : - file:Cairo_channel.t -> width_inches:float -> height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> - surface = "ml_cairo_pdf_surface_create" -external png_surface_create : - file:Cairo_channel.t -> format -> width:float -> height:float -> - surface = "ml_cairo_png_surface_create" - -external matrix_create : unit -> matrix = "ml_cairo_matrix_create" -external matrix_copy : - matrix:matrix -> other:matrix -> unit = "ml_cairo_matrix_copy" -external matrix_set_identity : - matrix:matrix -> unit = "ml_cairo_matrix_set_identity" -external matrix_set_affine : - matrix:matrix -> float array -> unit = "ml_cairo_matrix_set_affine" -external matrix_get_affine : - matrix:matrix -> float array = "ml_cairo_matrix_get_affine" -external matrix_translate : - matrix:matrix -> tx:float -> ty:float -> unit = "ml_cairo_matrix_translate" -external matrix_scale : - matrix:matrix -> sx:float -> sy:float -> unit = "ml_cairo_matrix_scale" -external matrix_rotate : - matrix:matrix -> radians:float -> unit = "ml_cairo_matrix_rotate" -external matrix_invert : matrix:matrix -> unit = "ml_cairo_matrix_invert" -external matrix_multiply : - result:matrix -> a:matrix -> b:matrix -> unit = "ml_cairo_matrix_multiply" -external matrix_transform_distance : - matrix:matrix -> point -> unit = "ml_cairo_matrix_transform_distance" -external matrix_transform_point : - matrix:matrix -> point -> unit = "ml_cairo_matrix_transform_point" -external finalise_target : cr:t -> unit = "ml_cairo_finalise_target" -external surface_finalise : surface -> unit = "ml_cairo_surface_finalise" -type flat_path = [ - | `MOVE_TO of point - | `LINE_TO of point - | `CLOSE ] -type basic_path = [ - | flat_path - | `CURVE_TO of point * point * point ] -external fold_current_path : t -> ('a -> [> basic_path] -> 'a) -> 'a -> 'a = "ml_cairo_current_path" -external fold_current_path_flat : t -> ('a -> [> flat_path] -> 'a) -> 'a -> 'a = "ml_cairo_current_path_flat" +type surface_pattern = [`Any|`Surface] pattern +type gradient_pattern = [`Any|`Gradient] pattern + +module Pattern = struct +external create_for_surface : 'a surface -> surface_pattern = "ml_cairo_pattern_create_for_surface" +external create_linear : x0:float -> y0:float -> x1:float -> y1:float -> gradient_pattern = "ml_cairo_pattern_create_linear" +external create_radial : cx0:float -> cy0:float -> radius0:float -> cx1:float -> cy1:float -> radius1:float -> gradient_pattern = "ml_cairo_pattern_create_radial_bc" "ml_cairo_pattern_create_radial" + +external add_color_stop_rgb : [>`Gradient] pattern -> off:float -> red:float -> green:float -> blue:float -> unit = "ml_cairo_pattern_add_color_stop_rgb" +external add_color_stop_rgba : [>`Gradient] pattern -> off:float -> red:float -> green:float -> blue:float -> alpha:float -> unit = "ml_cairo_pattern_add_color_stop_rgba_bc" "ml_cairo_pattern_add_color_stop_rgba" + +external set_matrix : 'a pattern -> matrix -> unit = "ml_cairo_pattern_set_matrix" +external get_matrix : 'a pattern -> matrix = "ml_cairo_pattern_get_matrix" + +external set_extend : [> `Surface] pattern -> extend -> unit = "ml_cairo_pattern_set_extend" +external get_extend : [> `Surface] pattern -> extend = "ml_cairo_pattern_get_extend" + +external set_filter : [> `Surface] pattern -> filter -> unit = "ml_cairo_pattern_set_filter" +external get_filter : [> `Surface] pattern -> filter = "ml_cairo_pattern_get_filter" +end + + +(* matrix *) +module Matrix = struct +let init_identity = { xx = 1.; yx = 0.; xy = 0.; yy = 1.; x0 = 0.; y0 = 0. } +let init_translate x y = { xx = 1.; yx = 0.; xy = 0.; yy = 1.; x0 = x; y0 = y } +let init_scale x y = { xx = x; yx = 0.; xy = 0.; yy = y; x0 = 0.; y0 = 0. } +let init_rotate a = + let s = sin a in + let c = cos a in + { xx = c; yx = s; xy = ~-. s; yy = c; x0 = 0.; y0 = 0. } + +external translate : matrix -> float -> float -> matrix = "ml_cairo_matrix_translate" +external scale : matrix -> float -> float -> matrix = "ml_cairo_matrix_scale" +external rotate : matrix -> float -> matrix = "ml_cairo_matrix_rotate" +external invert : matrix -> matrix = "ml_cairo_matrix_invert" +external multiply : matrix -> matrix -> matrix = "ml_cairo_matrix_multiply" + +external transform_distance : matrix -> point -> point = "ml_cairo_matrix_transform_distance" +external transform_point : matrix -> point -> point = "ml_cairo_matrix_transform_point" +end + + + +(* fonts *) +module Scaled_Font = struct +type t + +external create : font_face -> matrix -> matrix -> t = "ml_cairo_scaled_font_create" +external font_extents : t -> font_extents = "ml_cairo_scaled_font_extents" +external glyph_extents : t -> glyph array -> text_extents = "ml_cairo_scaled_font_glyph_extents" +end diff --git a/src/cairo.mli b/src/cairo.mli index 61ec876..b5c9df6 100644 --- a/src/cairo.mli +++ b/src/cairo.mli @@ -18,190 +18,143 @@ type status = | INVALID_MATRIX | NO_TARGET_SURFACE | NULL_POINTER + | INVALID_STRING + | INVALID_PATH_DATA + | READ_ERROR + | WRITE_ERROR + | SURFACE_FINISHED + | SURFACE_TYPE_MISMATCH + | BAD_NESTING exception Error of status val init : unit (** {3 Types} *) -type point = { mutable x : float ; mutable y : float } - type t -type surface -type matrix -type pattern -type image - -type format = - | FORMAT_ARGB32 - | FORMAT_RGB24 - | FORMAT_A8 - | FORMAT_A1 +type -'a surface +type -'a pattern +type font_face + +type point = { x : float ; y : float } +type matrix = { + xx : float ; yx : float ; + xy : float ; yy : float ; + x0 : float ; y0 : float + } (** {3 Core API} *) -val create : unit -> t -external save : cr:t -> unit = "ml_cairo_save" -external restore : cr:t -> unit = "ml_cairo_restore" -external _copy : dest:t -> src:t -> unit = "ml_cairo_copy" -val copy : t -> t +val create : 'a surface -> t +external save : t -> unit = "ml_cairo_save" +external restore : t -> unit = "ml_cairo_restore" external status : t -> status option = "ml_cairo_status" external status_string : t -> string = "ml_cairo_status_string" -external suspend_exn : t -> unit = "ml_cairo_suspend_exn" -(** The functions operating on cairo values normally raise an [Error] exception - immediately if the operation fails. Calling [suspend_exn] will prevent this - automatic exception-raising. *) -external resume_exn : t -> unit = "ml_cairo_resume_exn" -(** Switch back to exception-raising mode. If the cairo object has an error status, - an exception is raised right away. *) -external get_suspend_exn : t -> bool = "ml_cairo_get_suspend_exn" -(** Check the current exception-raising mode. *) - -(** {4 Target functions} *) - -external set_target_surface : cr:t -> surface:surface -> unit = "ml_cairo_set_target_surface" -external set_target_image : cr:t -> image -> unit = "ml_cairo_set_target_image" -external set_target_ps : - cr:t -> - file:Cairo_channel.t -> - width_inches:float -> - height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> unit - = "ml_cairo_set_target_ps_bc" "ml_cairo_set_target_ps" -external set_target_pdf : - cr:t -> - file:Cairo_channel.t -> - width_inches:float -> - height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> unit - = "ml_cairo_set_target_pdf_bc" "ml_cairo_set_target_pdf" -external set_target_png : - cr:t -> file:Cairo_channel.t -> format -> width:int -> height:int -> unit = "ml_cairo_set_target_png" -external finalise_target : cr:t -> unit = "ml_cairo_finalise_target" - (** {4 Renderer state} *) type operator = OPERATOR_CLEAR - | OPERATOR_SRC - | OPERATOR_DST + + | OPERATOR_SOURCE | OPERATOR_OVER - | OPERATOR_OVER_REVERSE | OPERATOR_IN - | OPERATOR_IN_REVERSE | OPERATOR_OUT - | OPERATOR_OUT_REVERSE | OPERATOR_ATOP - | OPERATOR_ATOP_REVERSE + + | OPERATOR_DEST + | OPERATOR_DEST_OVER + | OPERATOR_DEST_IN + | OPERATOR_DEST_OUT + | OPERATOR_DEST_ATOP + | OPERATOR_XOR | OPERATOR_ADD | OPERATOR_SATURATE -external set_operator : cr:t -> op:operator -> unit = "ml_cairo_set_operator" -external set_rgb_color : - cr:t -> red:float -> green:float -> blue:float -> unit - = "ml_cairo_set_rgb_color" -external set_pattern : cr:t -> pattern:pattern -> unit - = "ml_cairo_set_pattern" -external set_alpha : cr:t -> alpha:float -> unit = "ml_cairo_set_alpha" -external set_tolerance : cr:t -> tolerance:float -> unit - = "ml_cairo_set_tolerance" -type fill_rule = FILL_RULE_WINDING | FILL_RULE_EVEN_ODD -external set_fill_rule : cr:t -> fill_rule:fill_rule -> unit - = "ml_cairo_set_fill_rule" -external set_line_width : cr:t -> width:float -> unit - = "ml_cairo_set_line_width" -type line_cap = LINE_CAP_BUTT | LINE_CAP_ROUND | LINE_CAP_SQUARE -external set_line_cap : cr:t -> line_cap:line_cap -> unit - = "ml_cairo_set_line_cap" -type line_join = LINE_JOIN_MITER | LINE_JOIN_ROUND | LINE_JOIN_BEVEL -external set_line_join : cr:t -> line_join:line_join -> unit - = "ml_cairo_set_line_join" -external set_dash : cr:t -> dashes:float array -> offset:float -> unit - = "ml_cairo_set_dash" -external set_miter_limit : cr:t -> limit:float -> unit - = "ml_cairo_set_miter_limit" +external set_operator : t -> operator -> unit = "ml_cairo_set_operator" + +external set_source_rgb : t -> red:float -> green:float -> blue:float -> unit = "ml_cairo_set_source_rgb" +external set_source_rgba : t -> red:float -> green:float -> blue:float -> alpha:float ->unit = "ml_cairo_set_source_rgba" +external set_source : t -> 'a pattern -> unit = "ml_cairo_set_source" +external set_source_surface : t -> 'a surface -> float -> float -> unit = "ml_cairo_set_source_surface" + +external set_tolerance : t -> float -> unit = "ml_cairo_set_tolerance" + +type fill_rule = + FILL_RULE_WINDING + | FILL_RULE_EVEN_ODD +external set_fill_rule : t -> fill_rule -> unit = "ml_cairo_set_fill_rule" +external set_line_width : t -> float -> unit = "ml_cairo_set_line_width" +type line_cap = + LINE_CAP_BUTT + | LINE_CAP_ROUND + | LINE_CAP_SQUARE +external set_line_cap : t -> line_cap -> unit = "ml_cairo_set_line_cap" +type line_join = + LINE_JOIN_MITER + | LINE_JOIN_ROUND + | LINE_JOIN_BEVEL +external set_line_join : t -> line_join -> unit = "ml_cairo_set_line_join" +external set_dash : t -> float array -> float -> unit = "ml_cairo_set_dash" +external set_miter_limit : t -> float -> unit = "ml_cairo_set_miter_limit" (** {4 Transformations} *) -external translate : cr:t -> tx:float -> ty:float -> unit - = "ml_cairo_translate" -external scale : cr:t -> sx:float -> sy:float -> unit = "ml_cairo_scale" -external rotate : cr:t -> angle:float -> unit = "ml_cairo_rotate" -external concat_matrix : cr:t -> matrix:matrix -> unit - = "ml_cairo_concat_matrix" -external set_matrix : cr:t -> matrix:matrix -> unit = "ml_cairo_set_matrix" -external default_matrix : cr:t -> unit = "ml_cairo_default_matrix" -external identity_matrix : cr:t -> unit = "ml_cairo_identity_matrix" - -external transform_point : cr:t -> point -> unit - = "ml_cairo_transform_point" -external transform_distance : cr:t -> point -> unit - = "ml_cairo_transform_distance" -external inverse_transform_point : cr:t -> point -> unit - = "ml_cairo_inverse_transform_point" -external inverse_transform_distance : cr:t -> point -> unit - = "ml_cairo_inverse_transform_distance" +external translate : t -> tx:float -> ty:float -> unit = "ml_cairo_translate" +external scale : t -> sx:float -> sy:float -> unit = "ml_cairo_scale" +external rotate : t -> angle:float -> unit = "ml_cairo_rotate" +external transform : t -> matrix -> unit = "ml_cairo_trasnform" +external set_matrix : t -> matrix -> unit = "ml_cairo_set_matrix" +external identity_matrix : t -> unit = "ml_cairo_identity_matrix" + +external user_to_device : t -> point -> point = "ml_cairo_user_to_device" +external user_to_device_distance : t -> point -> point = "ml_cairo_user_to_device_distance" +external device_to_user : t -> point -> point = "ml_cairo_device_to_user" +external device_to_user_distance : t -> point -> point = "ml_cairo_device_to_user_distance" (** {4 Paths} *) -external new_path : cr:t -> unit = "ml_cairo_new_path" -external move_to : cr:t -> x:float -> y:float -> unit = "ml_cairo_move_to" -val move_to_point : cr:t -> point -> unit -external line_to : cr:t -> x:float -> y:float -> unit = "ml_cairo_line_to" -val line_to_point : cr:t -> point -> unit -external curve_to : - cr:t -> - x1:float -> - y1:float -> x2:float -> y2:float -> x3:float -> y3:float -> unit - = "ml_cairo_curve_to_bc" "ml_cairo_curve_to" -val curve_to_point : cr:t -> point -> point -> point -> unit -external arc : - cr:t -> - xc:float -> - yc:float -> radius:float -> angle1:float -> angle2:float -> unit - = "ml_cairo_arc_bc" "ml_cairo_arc" -external arc_negative : - cr:t -> - xc:float -> - yc:float -> radius:float -> angle1:float -> angle2:float -> unit - = "ml_cairo_arc_negative_bc" "ml_cairo_arc_negative" -external rel_move_to : cr:t -> dx:float -> dy:float -> unit - = "ml_cairo_rel_move_to" -external rel_line_to : cr:t -> dx:float -> dy:float -> unit - = "ml_cairo_rel_line_to" -external rel_curve_to : - cr:t -> - dx1:float -> - dy1:float -> dx2:float -> dy2:float -> dx3:float -> dy3:float -> unit - = "ml_cairo_rel_curve_to_bc" "ml_cairo_rel_curve_to" -external rectangle : - cr:t -> x:float -> y:float -> width:float -> height:float -> unit - = "ml_cairo_rectangle" -external close_path : cr:t -> unit = "ml_cairo_close_path" -external stroke : cr:t -> unit = "ml_cairo_stroke" -external fill : cr:t -> unit = "ml_cairo_fill" - -external in_stroke : cr:t -> x:float -> y:float -> bool = "ml_cairo_in_stroke" -external in_fill : cr:t -> x:float -> y:float -> bool = "ml_cairo_in_fill" - -external stroke_extents : cr:t -> float * float * float * float = "ml_cairo_stroke_extents" -external fill_extents : cr:t -> float * float * float * float = "ml_cairo_fill_extents" - -(** {3 Misc stuff I don't know how to categorize} *) - -external show_surface : - cr:t -> surface:surface -> width:int -> height:int -> unit - = "ml_cairo_show_surface" -external copy_page : cr:t -> unit = "ml_cairo_copy_page" -external show_page : cr:t -> unit = "ml_cairo_show_page" -external init_clip : cr:t -> unit = "ml_cairo_init_clip" -external clip : cr:t -> unit = "ml_cairo_clip" +external new_path : t -> unit = "ml_cairo_new_path" +external move_to : t -> x:float -> y:float -> unit = "ml_cairo_move_to" +val move_to_point : t -> point -> unit +external line_to : t -> x:float -> y:float -> unit = "ml_cairo_line_to" +val line_to_point : t -> point -> unit +external curve_to : t -> x1:float -> y1:float -> x2:float -> y2:float -> x3:float -> y3:float -> unit = "ml_cairo_curve_to_bc" "ml_cairo_curve_to" +val curve_to_point : t -> point -> point -> point -> unit +external arc : t -> xc:float -> yc:float -> radius:float -> angle1:float -> angle2:float -> unit = "ml_cairo_arc_bc" "ml_cairo_arc" +external arc_negative : t -> xc:float -> yc:float -> radius:float -> angle1:float -> angle2:float -> unit = "ml_cairo_arc_negative_bc" "ml_cairo_arc_negative" +external rel_move_to : t -> dx:float -> dy:float -> unit = "ml_cairo_rel_move_to" +external rel_line_to : t -> dx:float -> dy:float -> unit = "ml_cairo_rel_line_to" +external rel_curve_to : t -> dx1:float -> dy1:float -> dx2:float -> dy2:float -> dx3:float -> dy3:float -> unit = "ml_cairo_rel_curve_to_bc" "ml_cairo_rel_curve_to" +external rectangle : t -> x:float -> y:float -> width:float -> height:float -> unit = "ml_cairo_rectangle" +external close_path : t -> unit = "ml_cairo_close_path" + +external paint : t -> unit = "ml_cairo_paint" +external paint_with_alpha : t -> float -> unit = "ml_cairo_paint_with_alpha" + +external mask : t -> 'a pattern -> unit = "ml_cairo_mask" +external mask_surface : t -> 'a surface -> surface_x:float -> surface_y:float -> unit = "ml_cairo_mask_surface" + +external stroke : t -> unit = "ml_cairo_stroke" +external stroke_preserve : t -> unit = "ml_cairo_stroke_preserve" +external fill : t -> unit = "ml_cairo_fill" +external fill_preserve : t -> unit = "ml_cairo_fill_preserve" +external copy_page : t -> unit = "ml_cairo_copy_page" +external show_page : t -> unit = "ml_cairo_show_page" + +external in_stroke : t -> point -> bool = "ml_cairo_in_stroke" +external in_fill : t -> point -> bool = "ml_cairo_in_fill" + +external stroke_extents : t -> float * float * float * float = "ml_cairo_stroke_extents" +external fill_extents : t -> float * float * float * float = "ml_cairo_fill_extents" + +external clip : t -> unit = "ml_cairo_clip" +external clip_preserve : t -> unit = "ml_cairo_clip_preserve" +external reset_clip : t -> unit = "ml_cairo_reset_clip" (** {3 Text API} *) -type font type glyph = { index : int; glyph_x : float; glyph_y : float; } type text_extents = { x_bearing : float ; @@ -225,173 +178,131 @@ type font_slant = | FONT_SLANT_ITALIC | FONT_SLANT_OBLIQUE -external select_font : - cr:t -> family:string -> slant:font_slant -> weight:font_weight -> unit - = "ml_cairo_select_font" -external scale_font : cr:t -> scale:float -> unit = "ml_cairo_scale_font" -external transform_font : cr:t -> matrix:matrix -> unit - = "ml_cairo_transform_font" -external show_text : cr:t -> utf8:string -> unit = "ml_cairo_show_text" -external show_glyphs : cr:t -> glyph array -> unit - = "ml_cairo_show_glyphs" -external current_font : cr:t -> font = "ml_cairo_current_font" -external current_font_extents : cr:t -> font_extents - = "ml_cairo_current_font_extents" -external set_font : cr:t -> font:font -> unit = "ml_cairo_set_font" -external text_extents : t -> utf8:string -> text_extents = "ml_cairo_text_extents" +external select_font_face : t -> string -> font_slant -> font_weight -> unit = "ml_cairo_select_font_face" +external set_font_size : t -> float -> unit = "ml_cairo_set_font_size" +external set_font_matrix : t -> matrix -> unit = "ml_cairo_set_font_matrix" +external get_font_matrix : t -> matrix = "ml_cairo_get_font_matrix" +external show_text : t -> string -> unit = "ml_cairo_show_text" +external show_glyphs : t -> glyph array -> unit = "ml_cairo_show_glyphs" +external get_font_face : t -> font_face = "ml_cairo_get_font_face" +external font_extents : t -> font_extents = "ml_cairo_font_extents" +external set_font_face : t -> font_face -> unit = "ml_cairo_set_font_face" +external text_extents : t -> string -> text_extents = "ml_cairo_text_extents" external glyph_extents : t -> glyph array -> text_extents = "ml_cairo_glyph_extents" -external text_path : t -> utf8:string -> unit = "ml_cairo_text_path" +external text_path : t -> string -> unit = "ml_cairo_text_path" external glyph_path : t -> glyph array -> unit = "ml_cairo_glyph_path" (** {4 Renderer state querying} *) -external current_operator : cr:t -> operator = "ml_cairo_current_operator" -external current_rgb_color : cr:t -> float * float * float - = "ml_cairo_current_rgb_color" -external current_pattern : cr:t -> pattern = "ml_cairo_current_pattern" -external current_alpha : cr:t -> float = "ml_cairo_current_alpha" -external current_tolerance : cr:t -> float = "ml_cairo_current_tolerance" -external current_point : cr:t -> point = "ml_cairo_current_point" -external current_fill_rule : cr:t -> fill_rule = "ml_cairo_current_fill_rule" -external current_line_width : cr:t -> float = "ml_cairo_current_line_width" -external current_line_cap : cr:t -> line_cap = "ml_cairo_current_line_cap" -external current_line_join : cr:t -> line_join = "ml_cairo_current_line_join" -external current_miter_limit : cr:t -> float = "ml_cairo_current_miter_limit" -external current_matrix : cr:t -> matrix:matrix -> unit - = "ml_cairo_current_matrix" -external current_target_surface : cr:t -> surface - = "ml_cairo_current_target_surface" +external get_operator : t -> operator = "ml_cairo_get_operator" +external get_source : t -> 'a pattern = "ml_cairo_get_source" +external get_tolerance : t -> float = "ml_cairo_get_tolerance" +external get_current_point : t -> point = "ml_cairo_get_current_point" +external get_fill_rule : t -> fill_rule = "ml_cairo_get_fill_rule" +external get_line_width : t -> float = "ml_cairo_get_line_width" +external get_line_cap : t -> line_cap = "ml_cairo_get_line_cap" +external get_line_join : t -> line_join = "ml_cairo_get_line_join" +external get_miter_limit : t -> float = "ml_cairo_get_miter_limit" +external get_matrix : t -> matrix = "ml_cairo_get_matrix" +external get_target : t -> 'a surface = "ml_cairo_get_target" type flat_path = [ | `MOVE_TO of point | `LINE_TO of point | `CLOSE ] -type basic_path = [ +type path = [ | flat_path | `CURVE_TO of point * point * point ] -external fold_current_path : t -> ('a -> [> basic_path] -> 'a) -> 'a -> 'a = "ml_cairo_current_path" -external fold_current_path_flat : t -> ('a -> [> flat_path] -> 'a) -> 'a -> 'a = "ml_cairo_current_path_flat" +external fold_path : t -> ('a -> [> path] -> 'a) -> 'a -> 'a = "ml_cairo_copy_path" +external fold_path_flat : t -> ('a -> [> flat_path] -> 'a) -> 'a -> 'a = "ml_cairo_copy_path_flat" + +val append_path : t -> [< path] -> unit (** {3 Surface API} *) -external surface_create_for_image : image -> surface - = "ml_cairo_surface_create_for_image" -external surface_create_similar : - other:surface -> format:format -> width:int -> height:int -> surface - = "ml_cairo_surface_create_similar" -external surface_set_repeat : surface:surface -> repeat:bool -> unit - = "ml_cairo_surface_set_repeat" -external surface_set_matrix : surface:surface -> matrix:matrix -> unit - = "ml_cairo_surface_set_matrix" -external surface_get_matrix : surface:surface -> matrix:matrix -> unit - = "ml_cairo_surface_get_matrix" -type filter = - FILTER_FAST - | FILTER_GOOD - | FILTER_BEST - | FILTER_NEAREST - | FILTER_BILINEAR - | FILTER_GAUSSIAN -external surface_set_filter : surface:surface -> filter:filter -> unit - = "ml_cairo_surface_set_filter" -external surface_get_filter : surface:surface -> filter - = "ml_cairo_surface_get_filter" -external surface_finalise : surface -> unit = "ml_cairo_surface_finalise" +type format = + FORMAT_ARGB32 + | FORMAT_RGB24 + | FORMAT_A8 + | FORMAT_A1 -(** {4 Pattern functions} *) +external surface_create_similar : 'a surface -> format -> width:int -> height:int -> 'a surface = "ml_cairo_surface_create_similar" -external pattern_create_for_surface : surface -> pattern = "ml_cairo_pattern_create_for_surface" +external surface_finish : 'a surface -> unit = "ml_cairo_surface_finish" -external pattern_create_linear : x0:float -> y0:float -> x1:float -> y1:float -> pattern - = "ml_cairo_pattern_create_linear" -external pattern_create_radial : - cx0:float -> cy0:float -> radius0:float -> - cx1:float -> cy1:float -> radius1:float -> pattern - = "ml_cairo_pattern_create_radial_bc" "ml_cairo_pattern_create_radial" +external surface_set_device_offset : 'a surface -> float -> float -> unit = "ml_cairo_surface_set_device_offset" -external pattern_add_color_stop : - pattern -> offset:float -> - red:float -> green:float -> blue:float -> alpha:float -> unit - = "ml_cairo_pattern_add_color_stop_bc" "ml_cairo_pattern_add_color_stop" +(** {4 Image surface} *) + +type image_surface = [`Any|`Image] surface + +external image_surface_create : format -> width:int -> height:int -> image_surface = "ml_cairo_image_surface_create" +external image_surface_get_width : [>`Image] surface -> int = "ml_cairo_image_surface_get_width" +external image_surface_get_height : [>`Image] surface -> int = "ml_cairo_image_surface_get_height" -external pattern_set_matrix : pattern -> matrix -> unit = "ml_cairo_pattern_set_matrix" -external pattern_get_matrix : pattern -> matrix -> unit = "ml_cairo_pattern_get_matrix" +(** {4 Patterns} *) -type extend = - | EXTEND_NONE +type surface_pattern = [`Any|`Surface] pattern +type gradient_pattern = [`Any|`Gradient] pattern + +type extend = + EXTEND_NONE | EXTEND_REPEAT | EXTEND_REFLECT -external pattern_set_extend : pattern -> extend -> unit = "ml_cairo_pattern_set_extend" -external pattern_get_extend : pattern -> extend = "ml_cairo_pattern_get_extend" -external pattern_set_filter : pattern -> filter -> unit = "ml_cairo_pattern_set_filter" -external pattern_get_filter : pattern -> filter = "ml_cairo_pattern_get_filter" -(** {4 Image surface} *) +type filter = + FILTER_FAST + | FILTER_GOOD + | FILTER_BEST + | FILTER_NEAREST + | FILTER_BILINEAR + | FILTER_GAUSSIAN + +(** Patterns functions *) +module Pattern : sig +external create_for_surface : 'a surface -> surface_pattern = "ml_cairo_pattern_create_for_surface" +external create_linear : x0:float -> y0:float -> x1:float -> y1:float -> gradient_pattern = "ml_cairo_pattern_create_linear" +external create_radial : cx0:float -> cy0:float -> radius0:float -> cx1:float -> cy1:float -> radius1:float -> gradient_pattern = "ml_cairo_pattern_create_radial_bc" "ml_cairo_pattern_create_radial" -external image_surface_create : - format:format -> width:int -> height:int -> surface - = "ml_cairo_image_surface_create" -external image_surface_create_for_data : image -> surface - = "ml_cairo_image_surface_create_for_data" +external add_color_stop_rgb : [>`Gradient] pattern -> off:float -> red:float -> green:float -> blue:float -> unit = "ml_cairo_pattern_add_color_stop_rgb" +external add_color_stop_rgba : [>`Gradient] pattern -> off:float -> red:float -> green:float -> blue:float -> alpha:float -> unit = "ml_cairo_pattern_add_color_stop_rgba_bc" "ml_cairo_pattern_add_color_stop_rgba" -(** {4 PS surface} *) +external set_matrix : 'a pattern -> matrix -> unit = "ml_cairo_pattern_set_matrix" +external get_matrix : 'a pattern -> matrix = "ml_cairo_pattern_get_matrix" -external ps_surface_create : - file:Cairo_channel.t -> - width_inches:float -> - height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> surface - = "ml_cairo_ps_surface_create" +external set_extend : [> `Surface] pattern -> extend -> unit = "ml_cairo_pattern_set_extend" +external get_extend : [> `Surface] pattern -> extend = "ml_cairo_pattern_get_extend" -(** {4 PDF surface} *) +external set_filter : [> `Surface] pattern -> filter -> unit = "ml_cairo_pattern_set_filter" +external get_filter : [> `Surface] pattern -> filter = "ml_cairo_pattern_get_filter" +end -external pdf_surface_create : - file:Cairo_channel.t -> - width_inches:float -> - height_inches:float -> - x_pixels_per_inch:float -> y_pixels_per_inch:float -> surface - = "ml_cairo_pdf_surface_create" +(** {3 Matrix API} *) -(** {4 PNG surface} *) +(** Matrix functions *) +module Matrix : sig +val init_identity : matrix +val init_translate : float -> float -> matrix +val init_scale : float -> float -> matrix +val init_rotate : float -> matrix -external png_surface_create : - file:Cairo_channel.t -> format -> width:float -> height:float -> - surface = "ml_cairo_png_surface_create" +external translate : matrix -> float -> float -> matrix = "ml_cairo_matrix_translate" +external scale : matrix -> float -> float -> matrix = "ml_cairo_matrix_scale" +external rotate : matrix -> float -> matrix = "ml_cairo_matrix_rotate" +external invert : matrix -> matrix = "ml_cairo_matrix_invert" +external multiply : matrix -> matrix -> matrix = "ml_cairo_matrix_multiply" +external transform_distance : matrix -> point -> point = "ml_cairo_matrix_transform_distance" +external transform_point : matrix -> point -> point = "ml_cairo_matrix_transform_point" +end -(** {3 Matrix API} *) +(** {3 Scaled Fonts API} *) + +(** Scaled fonts functions *) +module Scaled_Font : sig +type t -external matrix_create : unit -> matrix = "ml_cairo_matrix_create" -external matrix_copy : matrix:matrix -> other:matrix -> unit - = "ml_cairo_matrix_copy" -external matrix_set_identity : matrix:matrix -> unit - = "ml_cairo_matrix_set_identity" -external matrix_set_affine : matrix:matrix -> float array -> unit - = "ml_cairo_matrix_set_affine" -external matrix_get_affine : matrix:matrix -> float array - = "ml_cairo_matrix_get_affine" - -external matrix_translate : matrix:matrix -> tx:float -> ty:float -> unit - = "ml_cairo_matrix_translate" -external matrix_scale : matrix:matrix -> sx:float -> sy:float -> unit - = "ml_cairo_matrix_scale" -external matrix_rotate : matrix:matrix -> radians:float -> unit - = "ml_cairo_matrix_rotate" -external matrix_invert : matrix:matrix -> unit = "ml_cairo_matrix_invert" -external matrix_multiply : result:matrix -> a:matrix -> b:matrix -> unit - = "ml_cairo_matrix_multiply" - -external matrix_transform_distance : matrix:matrix -> point -> unit - = "ml_cairo_matrix_transform_distance" -external matrix_transform_point : matrix:matrix -> point -> unit - = "ml_cairo_matrix_transform_point" - -(** {3 Font API} - - Mostly unusable ATM. It needs other libraries (freetype2/fontconfig). -*) - -external font_extents : font -> matrix -> font_extents - = "ml_cairo_font_extents" -external font_glyph_extents : font -> matrix -> glyph array -> text_extents - = "ml_cairo_font_glyph_extents" +external create : font_face -> matrix -> matrix -> t = "ml_cairo_scaled_font_create" +external font_extents : t -> font_extents = "ml_cairo_scaled_font_extents" +external glyph_extents : t -> glyph array -> text_extents = "ml_cairo_scaled_font_glyph_extents" +end diff --git a/src/cairo_bigarray.ml b/src/cairo_bigarray.ml index 978d708..8a132d1 100644 --- a/src/cairo_bigarray.ml +++ b/src/cairo_bigarray.ml @@ -6,73 +6,49 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -type image = { - data : Obj.t ; - format : Cairo.format ; - width : int ; - height : int ; - stride : int ; - } - -external conv : image -> Cairo.image = "%identity" - open Bigarray external bigarray_kind_float : ('a, 'b, c_layout) Array2.t -> bool - = "bigarray_kind_float" + = "ml_bigarray_kind_float" external bigarray_byte_size : ('a, 'b, c_layout) Array2.t -> int = "ml_bigarray_byte_size" -external bigarray_data : ('a, 'b, c_layout) Array2.t -> Obj.t = "%field1" + +external image_surface_create : + ('a, 'b, c_layout) Array2.t -> + Cairo.format -> width:int -> height:int -> stride:int -> + Cairo.image_surface = "ml_cairo_image_surface_create_for_data" + let of_bigarr arr format ~width ~height ~stride = if bigarray_kind_float arr then invalid_arg "wrong Bigarray kind" ; if bigarray_byte_size arr < stride * height then invalid_arg "Bigarray too small" ; - conv { data = bigarray_data arr ; - format = format ; - width = width ; - height = height ; - stride = stride } + image_surface_create arr format width height stride let of_bigarr_32 ~alpha (arr : (int32, int32_elt, c_layout) Array2.t) = + let h = Array2.dim1 arr in let w = Array2.dim2 arr in - conv { data = bigarray_data arr ; - format = if alpha then Cairo.FORMAT_ARGB32 else Cairo.FORMAT_RGB24 ; - width = w ; - height = Array2.dim1 arr ; - stride = 4 * w ; - } + of_bigarr arr + (if alpha then Cairo.FORMAT_ARGB32 else Cairo.FORMAT_RGB24) + w h (4 * w) let of_bigarr_24 (arr : (int, int_elt, c_layout) Array2.t) = if Sys.word_size <> 32 then failwith "your ints have 63 bits" ; + let h = Array2.dim1 arr in let w = Array2.dim2 arr in - conv { data = bigarray_data arr ; - format = Cairo.FORMAT_RGB24 ; - width = w ; - height = Array2.dim1 arr ; - stride = 4 * w ; - } + of_bigarr arr + Cairo.FORMAT_RGB24 + w h (4 * w) let of_bigarr_8 (arr : (int, int8_unsigned_elt, c_layout) Array2.t) = + let h = Array2.dim1 arr in let w = Array2.dim2 arr in - conv { data = bigarray_data arr ; - format = Cairo.FORMAT_A8 ; - width = w ; - height = Array2.dim1 arr ; - stride = w ; - } - -let of_bigarr_1 (arr : (int, int8_unsigned_elt, c_layout) Array2.t) = - let w = Array2.dim2 arr in - conv { data = bigarray_data arr ; - format = Cairo.FORMAT_A1 ; - width = w * 8 ; - height = Array2.dim1 arr ; - stride = w ; - } + of_bigarr arr + Cairo.FORMAT_A8 + w h w let output_pixel oc p = let r = (p lsr 16) land 0xff in diff --git a/src/cairo_bigarray.mli b/src/cairo_bigarray.mli index 9c69c73..244c261 100644 --- a/src/cairo_bigarray.mli +++ b/src/cairo_bigarray.mli @@ -6,18 +6,17 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -(** Support for the in-memory image backend, via Bigarray *) +(** image backend, via Bigarray *) open Bigarray val of_bigarr : ('a, 'b, c_layout) Array2.t -> Cairo.format -> - width:int -> height:int -> stride:int -> Cairo.image + width:int -> height:int -> stride:int -> Cairo.image_surface -val of_bigarr_32 : alpha:bool -> (int32, int32_elt, c_layout) Array2.t -> Cairo.image -val of_bigarr_24 : (int, int_elt, c_layout) Array2.t -> Cairo.image -val of_bigarr_8 : (int, int8_unsigned_elt, c_layout) Array2.t -> Cairo.image -val of_bigarr_1 : (int, int8_unsigned_elt, c_layout) Array2.t -> Cairo.image +val of_bigarr_32 : alpha:bool -> (int32, int32_elt, c_layout) Array2.t -> Cairo.image_surface +val of_bigarr_24 : (int, int_elt, c_layout) Array2.t -> Cairo.image_surface +val of_bigarr_8 : (int, int8_unsigned_elt, c_layout) Array2.t -> Cairo.image_surface val write_ppm_int32 : out_channel -> (int32, int32_elt, c_layout) Array2.t -> unit val write_ppm_int : out_channel -> (int, int_elt, c_layout) Array2.t -> unit diff --git a/src/cairo_lablgtk.ml b/src/cairo_lablgtk.ml index 0a09cb1..c2857fd 100644 --- a/src/cairo_lablgtk.ml +++ b/src/cairo_lablgtk.ml @@ -6,19 +6,10 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -external image_of_pixbuf : GdkPixbuf.pixbuf -> Cairo.image = "cairo_lablgtk_of_pixbuf" -external shuffle_pixels : GdkPixbuf.pixbuf -> unit = "cairo_lablgtk_shuffle_pixels" +type surface = [`Any|`Xlib] Cairo.surface -external surface_create_for_drawable : - [> `drawable] Gobject.obj -> - Cairo.format -> Cairo.surface = "cairo_lablgtk_surface_create_for_drawable" -external set_target_drawable : - Cairo.t -> [> `drawable] Gobject.obj -> unit - = "cairo_lablgtk_set_target_drawable" +external image_of_pixbuf : GdkPixbuf.pixbuf -> Cairo.image_surface = "ml_cairo_lablgtk_of_pixbuf" +external shuffle_pixels : GdkPixbuf.pixbuf -> unit = "ml_cairo_lablgtk_shuffle_pixels" -let create ?target () = - let c = Cairo.create () in - begin match target with - | None -> () - | Some d -> set_target_drawable c d end ; - c +external surface_create : [> `drawable] Gobject.obj -> surface = "ml_cairo_xlib_surface_create" +external surface_set_size : [> `Xlib] Cairo.surface -> int -> int -> unit = "ml_cairo_xlib_surface_set_size" diff --git a/src/cairo_lablgtk.mli b/src/cairo_lablgtk.mli index f8a1515..7d77839 100644 --- a/src/cairo_lablgtk.mli +++ b/src/cairo_lablgtk.mli @@ -6,16 +6,12 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -(** Support for the X11 backend, via LablGTK *) +(** Xlib backend, via LablGTK *) -external image_of_pixbuf : GdkPixbuf.pixbuf -> Cairo.image = "cairo_lablgtk_of_pixbuf" -external shuffle_pixels : GdkPixbuf.pixbuf -> unit = "cairo_lablgtk_shuffle_pixels" +type surface = [`Any|`Xlib] Cairo.surface -external surface_create_for_drawable : - [> `drawable] Gobject.obj -> - Cairo.format -> Cairo.surface = "cairo_lablgtk_surface_create_for_drawable" -external set_target_drawable : - Cairo.t -> [> `drawable] Gobject.obj -> unit - = "cairo_lablgtk_set_target_drawable" +external image_of_pixbuf : GdkPixbuf.pixbuf -> Cairo.image_surface = "ml_cairo_lablgtk_of_pixbuf" +external shuffle_pixels : GdkPixbuf.pixbuf -> unit = "ml_cairo_lablgtk_shuffle_pixels" -val create : ?target:[> `drawable] Gobject.obj -> unit -> Cairo.t +external surface_create : [> `drawable] Gobject.obj -> surface = "ml_cairo_xlib_surface_create" +external surface_set_size : [> `Xlib] Cairo.surface -> int -> int -> unit = "ml_cairo_xlib_surface_set_size" diff --git a/src/cairo_channel.mli b/src/cairo_pdf.ml index 1040668..a071c78 100644 --- a/src/cairo_channel.mli +++ b/src/cairo_pdf.ml @@ -6,11 +6,17 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -(** Support module for file-based backends (PostScript, PDF and PNG) *) +type surface = [`Any|`PDF] Cairo.surface -type t +external surface_create : + string -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_pdf_surface_create" -val open_out : string -> t -external of_out_channel : out_channel -> t = "ml_FILE_of_channel" -external of_file_descr : Unix.file_descr -> t = "ml_FILE_of_file_descr" -external close : t -> unit = "ml_fclose" +external surface_create_for_stream : + (string -> unit) -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_pdf_surface_create_for_stream" + +external set_dpi : + [> `PDF] Cairo.surface -> x_dpi:float -> y_dpi:float -> unit = "ml_cairo_pdf_surface_set_dpi" diff --git a/src/cairo_pdf.mli b/src/cairo_pdf.mli new file mode 100644 index 0000000..1ef016f --- /dev/null +++ b/src/cairo_pdf.mli @@ -0,0 +1,24 @@ +(**************************************************************************) +(* cairo-ocaml -- Objective Caml bindings for Cairo *) +(* Copyright © 2004-2005 Olivier Andrieu *) +(* *) +(* This code is free software and is licensed under the terms of the *) +(* GNU Lesser General Public License version 2.1 (the "LGPL"). *) +(**************************************************************************) + +(** PDF backend *) + +type surface = [`Any|`PDF] Cairo.surface + +external surface_create : + string -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_pdf_surface_create" + +external surface_create_for_stream : + (string -> unit) -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_pdf_surface_create_for_stream" + +external set_dpi : + [> `PDF] Cairo.surface -> x_dpi:float -> y_dpi:float -> unit = "ml_cairo_pdf_surface_set_dpi" diff --git a/src/cairo_png.ml b/src/cairo_png.ml new file mode 100644 index 0000000..62cb1c9 --- /dev/null +++ b/src/cairo_png.ml @@ -0,0 +1,20 @@ +(**************************************************************************) +(* cairo-ocaml -- Objective Caml bindings for Cairo *) +(* Copyright © 2004-2005 Olivier Andrieu *) +(* *) +(* This code is free software and is licensed under the terms of the *) +(* GNU Lesser General Public License version 2.1 (the "LGPL"). *) +(**************************************************************************) + +external image_surface_create_from_file : + string -> Cairo.image_surface = "ml_cairo_image_surface_create_from_png" + +external image_surface_create_from_stream : + (string -> unit) -> Cairo.image_surface = "ml_cairo_image_surface_create_from_stream" + + +external surface_write_to_file : + 'a Cairo.surface -> string -> unit = "ml_cairo_surface_write_to_png" + +external surface_write_to_stream : + 'a Cairo.surface -> (string -> unit) -> unit = "ml_cairo_surface_write_to_png_stream" diff --git a/src/cairo_png.mli b/src/cairo_png.mli new file mode 100644 index 0000000..260a745 --- /dev/null +++ b/src/cairo_png.mli @@ -0,0 +1,22 @@ +(**************************************************************************) +(* cairo-ocaml -- Objective Caml bindings for Cairo *) +(* Copyright © 2004-2005 Olivier Andrieu *) +(* *) +(* This code is free software and is licensed under the terms of the *) +(* GNU Lesser General Public License version 2.1 (the "LGPL"). *) +(**************************************************************************) + +(** PNG reading/writing functions *) + +external image_surface_create_from_file : + string -> Cairo.image_surface = "ml_cairo_image_surface_create_from_png" + +external image_surface_create_from_stream : + (string -> unit) -> Cairo.image_surface = "ml_cairo_image_surface_create_from_stream" + + +external surface_write_to_file : + 'a Cairo.surface -> string -> unit = "ml_cairo_surface_write_to_png" + +external surface_write_to_stream : + 'a Cairo.surface -> (string -> unit) -> unit = "ml_cairo_surface_write_to_png_stream" diff --git a/src/cairo_channel.ml b/src/cairo_ps.ml index 882ce20..380c32f 100644 --- a/src/cairo_channel.ml +++ b/src/cairo_ps.ml @@ -6,13 +6,17 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -type t -external of_out_channel : out_channel -> t = "ml_FILE_of_channel" -external of_file_descr : Unix.file_descr -> t = "ml_FILE_of_file_descr" -external close : t -> unit = "ml_fclose" +type surface = [`Any|`PS] Cairo.surface -let open_out fname = - let oc = Pervasives.open_out fname in - let c = of_out_channel oc in - close_out oc ; - c +external surface_create : + string -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_ps_surface_create" + +external surface_create_for_stream : + (string -> unit) -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_ps_surface_create_for_stream" + +(* external set_dpi : *) +(* [> `PS] Cairo.surface -> x_dpi:float -> y_dpi:float -> unit = "ml_cairo_ps_surface_set_dpi" *) diff --git a/src/cairo_ps.mli b/src/cairo_ps.mli new file mode 100644 index 0000000..7a8eac2 --- /dev/null +++ b/src/cairo_ps.mli @@ -0,0 +1,24 @@ +(**************************************************************************) +(* cairo-ocaml -- Objective Caml bindings for Cairo *) +(* Copyright © 2004-2005 Olivier Andrieu *) +(* *) +(* This code is free software and is licensed under the terms of the *) +(* GNU Lesser General Public License version 2.1 (the "LGPL"). *) +(**************************************************************************) + +(** PostScript backend *) + +type surface = [`Any|`PS] Cairo.surface + +external surface_create : + string -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_ps_surface_create" + +external surface_create_for_stream : + (string -> unit) -> + width_in_points:float -> + height_in_points:float -> surface = "ml_cairo_ps_surface_create_for_stream" + +(* external set_dpi : *) +(* [> `PS] Cairo.surface -> x_dpi:float -> y_dpi:float -> unit = "ml_cairo_ps_surface_set_dpi" *) diff --git a/src/ml_cairo.c b/src/ml_cairo.c index 7ff7769..6b076cb 100644 --- a/src/ml_cairo.c +++ b/src/ml_cairo.c @@ -6,269 +6,50 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <caml/mlvalues.h> -#include <caml/alloc.h> -#include <caml/memory.h> -#include <caml/fail.h> -#include <caml/custom.h> - -#include "ml_cairo_wrappers.h" - -#include <cairo.h> -/* import file-based backends */ -#ifdef CAIRO_HAS_PS_SURFACE -# include <cairo-ps.h> -#endif -#ifdef CAIRO_HAS_PDF_SURFACE -# include <cairo-pdf.h> -#endif -#ifdef CAIRO_HAS_PNG_SURFACE -# include <cairo-png.h> -#endif - -#include "ml_cairo_channel.h" -#include "ml_cairo_status.h" #include "ml_cairo.h" -static void -ml_final_cairo_t (value val) -{ - cairo_t *cr = cairo_t_val (val); - if (cr != NULL) - cairo_destroy (cr); -} - -static struct custom_operations ml_custom_cairo_t = { - "cairo_t/001", ml_final_cairo_t, ml_pointer_compare, - ml_pointer_hash, custom_serialize_default, custom_deserialize_default -}; - -value -Val_cairo_t (cairo_t * p) -{ - value ret; - struct ml_cairo *ml_c; - if (p == NULL) - report_null_pointer; - ret = alloc_custom (&ml_custom_cairo_t, sizeof (struct ml_cairo), 20, 1000); - ml_c = Data_custom_val (ret); - ml_c->cr = p; - ml_c->suspend_exn = 0; - return ret; -} - -Make_Val_final_pointer(cairo_surface_t, Id, cairo_surface_destroy, 20) - -Make_Val_final_pointer(cairo_matrix_t, Id, cairo_matrix_destroy, 100) - -Make_Val_final_pointer(cairo_pattern_t, Id, cairo_pattern_destroy, 20) -#define cairo_pattern_t_val(v) ((cairo_pattern_t *)Pointer_val(v)) - -CAMLprim value -ml_cairo_suspend_exn (value v_cr) -{ - struct ml_cairo *ml_c = Data_custom_val (v_cr); - ml_c->suspend_exn = 1; - return Val_unit; -} - -CAMLprim value -ml_cairo_resume_exn (value v_cr) -{ - struct ml_cairo *ml_c = Data_custom_val (v_cr); - ml_c->suspend_exn = 0; - cairo_treat_status (cairo_status (ml_c->cr)); - return Val_unit; -} - -CAMLprim value -ml_cairo_get_suspend_exn (value v_cr) -{ - struct ml_cairo *ml_c = Data_custom_val (v_cr); - return Val_bool (ml_c->suspend_exn); -} - -ML_0(cairo_create, Val_cairo_t) +wMake_Val_final_pointer(cairo_t, cairo_destroy, 0) -/* ML_1(cairo_destroy, cairo_t_val, Unit) */ +wML_1(cairo_create, cairo_surface_t_val, Val_cairo_t) -CAMLprim value -ml_cairo_save (value v_cr) -{ - cairo_save (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +/* cairo_reference */ +/* cairo_destroy */ -CAMLprim value -ml_cairo_restore (value v_cr) -{ - cairo_restore (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} -ML_2(cairo_copy, cairo_t_val, cairo_t_val, Unit) +wML_0_cairo(save) -CAMLprim value -ml_cairo_set_target_surface (value v_cr, value v_surface) -{ - cairo_set_target_surface (cairo_t_val (v_cr), - cairo_surface_t_val (v_surface)); - check_cairo_status (v_cr); - return Val_unit; -} - -CAMLprim value -ml_cairo_set_target_image (value cr, value img) -{ - cairo_set_target_image (cairo_t_val (cr), - Bp_val (Field (img, 0)), - cairo_format_t_val (Field (img, 1)), - Int_val (Field (img, 2)), - Int_val (Field (img, 3)), Int_val (Field (img, 4))); - check_cairo_status (cr); - return Val_unit; -} - -#ifdef CAIRO_HAS_PS_SURFACE -CAMLprim value -ml_cairo_set_target_ps (value v_cr, value v_file, value v_width_inches, - value v_height_inches, value v_x_pixels_per_inch, - value v_y_pixels_per_inch) -{ - cairo_set_target_ps (cairo_t_val (v_cr), FILE_val (v_file), - Double_val (v_width_inches), - Double_val (v_height_inches), - Double_val (v_x_pixels_per_inch), - Double_val (v_y_pixels_per_inch)); - check_cairo_status (v_cr); - return Val_unit; -} -#else -Unsupported(ml_cairo_set_target_ps) -#endif /* CAIRO_HAS_PS_SURFACE */ -ML_bc6(cairo_set_target_ps) - -#ifdef CAIRO_HAS_PDF_SURFACE -CAMLprim value -ml_cairo_set_target_pdf (value v_cr, value v_file, value v_width_inches, - value v_height_inches, value v_x_pixels_per_inch, - value v_y_pixels_per_inch) -{ - cairo_set_target_pdf (cairo_t_val (v_cr), FILE_val (v_file), - Double_val (v_width_inches), - Double_val (v_height_inches), - Double_val (v_x_pixels_per_inch), - Double_val (v_y_pixels_per_inch)); - check_cairo_status (v_cr); - return Val_unit; -} -#else -Unsupported(ml_cairo_set_target_pdf) -#endif /* CAIRO_HAS_PDF_SURFACE */ -ML_bc6(cairo_set_target_pdf) - -#ifdef CAIRO_HAS_PNG_SURFACE -CAMLprim value -ml_cairo_set_target_png (value v_cr, value v_file, value v_format, - value v_width, value v_height) -{ - cairo_set_target_png (cairo_t_val (v_cr), FILE_val (v_file), - cairo_format_t_val (v_format), - Int_val (v_width), Int_val (v_height)); - check_cairo_status (v_cr); - return Val_unit; -} -#else -Unsupported(ml_cairo_set_target_png) -#endif /* CAIRO_HAS_PNG_SURFACE */ +wML_0_cairo(restore) #define cairo_operator_t_val(v) ((cairo_operator_t) Int_val(v)) #define Val_cairo_operator_t(v) Val_int(v) -CAMLprim value -ml_cairo_set_operator (value v_cr, value v_op) -{ - cairo_set_operator (cairo_t_val (v_cr), cairo_operator_t_val (v_op)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_operator, cairo_operator_t_val) -CAMLprim value -ml_cairo_set_rgb_color (value v_cr, value v_red, value v_green, value v_blue) -{ - cairo_set_rgb_color (cairo_t_val (v_cr), Double_val (v_red), - Double_val (v_green), Double_val (v_blue)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_3_cairo(set_source_rgb, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_set_pattern (value v_cr, value v_pattern) -{ - cairo_set_pattern (cairo_t_val (v_cr), cairo_pattern_t_val (v_pattern)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_4_cairo(set_source_rgba, Double_val, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_set_alpha (value v_cr, value v_alpha) -{ - cairo_set_alpha (cairo_t_val (v_cr), Double_val (v_alpha)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_source, cairo_pattern_t_val) -CAMLprim value -ml_cairo_set_tolerance (value v_cr, value v_tolerance) -{ - cairo_set_tolerance (cairo_t_val (v_cr), Double_val (v_tolerance)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_3_cairo(set_source_surface, cairo_surface_t_val, Double_val, Double_val) + +wML_1_cairo(set_tolerance, Double_val) #define cairo_fill_rule_t_val(v) ((cairo_fill_rule_t) Int_val(v)) #define Val_cairo_fill_rule_t(v) Val_int(v) -CAMLprim value -ml_cairo_set_fill_rule (value v_cr, value v_fill_rule) -{ - cairo_set_fill_rule (cairo_t_val (v_cr), - cairo_fill_rule_t_val (v_fill_rule)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_fill_rule, cairo_fill_rule_t_val) -CAMLprim value -ml_cairo_set_line_width (value v_cr, value v_width) -{ - cairo_set_line_width (cairo_t_val (v_cr), Double_val (v_width)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_line_width, Double_val) #define cairo_line_cap_t_val(v) ((cairo_line_cap_t) Int_val(v)) #define Val_cairo_line_cap_t(v) Val_int(v) -CAMLprim value -ml_cairo_set_line_cap (value v_cr, value v_line_cap) -{ - cairo_set_line_cap (cairo_t_val (v_cr), cairo_line_cap_t_val (v_line_cap)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_line_cap, cairo_line_cap_t_val) #define cairo_line_join_t_val(v) ((cairo_line_join_t) Int_val(v)) #define Val_cairo_line_join_t(v) Val_int(v) -CAMLprim value -ml_cairo_set_line_join (value v_cr, value v_line_join) -{ - cairo_set_line_join (cairo_t_val (v_cr), - cairo_line_join_t_val (v_line_join)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_line_join, cairo_line_join_t_val) CAMLprim value ml_cairo_set_dash (value cr, value d, value off) @@ -288,42 +69,24 @@ ml_cairo_set_dash (value cr, value d, value off) return Val_unit; } -CAMLprim value -ml_cairo_set_miter_limit (value v_cr, value v_limit) -{ - cairo_set_miter_limit (cairo_t_val (v_cr), Double_val (v_limit)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_miter_limit, Double_val) -CAMLprim value -ml_cairo_translate (value v_cr, value v_tx, value v_ty) -{ - cairo_translate (cairo_t_val (v_cr), Double_val (v_tx), Double_val (v_ty)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_2_cairo(translate, Double_val, Double_val) -CAMLprim value -ml_cairo_scale (value v_cr, value v_sx, value v_sy) -{ - cairo_scale (cairo_t_val (v_cr), Double_val (v_sx), Double_val (v_sy)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_2_cairo(scale, Double_val, Double_val) -CAMLprim value -ml_cairo_rotate (value v_cr, value v_angle) -{ - cairo_rotate (cairo_t_val (v_cr), Double_val (v_angle)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(rotate, Double_val) CAMLprim value -ml_cairo_concat_matrix (value v_cr, value v_matrix) +ml_cairo_transform (value v_cr, value v_matrix) { - cairo_concat_matrix (cairo_t_val (v_cr), cairo_matrix_t_val (v_matrix)); +#ifndef ARCH_ALIGN_DOUBLE + cairo_transform (cairo_t_val (v_cr), cairo_matrix_t_val (v_matrix)); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (v_matrix, &mat); + cairo_transform (cairo_t_val (v_cr), &mat); +#endif check_cairo_status (v_cr); return Val_unit; } @@ -331,234 +94,131 @@ ml_cairo_concat_matrix (value v_cr, value v_matrix) CAMLprim value ml_cairo_set_matrix (value v_cr, value v_matrix) { +#ifndef ARCH_ALIGN_DOUBLE cairo_set_matrix (cairo_t_val (v_cr), cairo_matrix_t_val (v_matrix)); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (v_matrix, &mat); + cairo_set_matrix (cairo_t_val (v_cr), &mat); +#endif check_cairo_status (v_cr); return Val_unit; } -CAMLprim value -ml_cairo_default_matrix (value v_cr) -{ - cairo_default_matrix (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_0_cairo (identity_matrix) -CAMLprim value -ml_cairo_identity_matrix (value v_cr) +value +ml_cairo_point (double x, double y) { - cairo_identity_matrix (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; + value p; + p = caml_alloc_small (2 * Double_wosize, Double_array_tag); + Store_double_field (p, 0, x); + Store_double_field (p, 1, y); + return p; } CAMLprim value -ml_cairo_transform_point (value cr, value p) +ml_cairo_user_to_device (value cr, value p) { double x, y; x = Double_field (p, 0); y = Double_field (p, 1); - cairo_transform_point (cairo_t_val (cr), &x, &y); + cairo_user_to_device (cairo_t_val (cr), &x, &y); check_cairo_status (cr); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return Val_unit; + return ml_cairo_point (x, y); } CAMLprim value -ml_cairo_transform_distance (value cr, value p) +ml_cairo_user_to_device_distance (value cr, value p) { double x, y; x = Double_field (p, 0); y = Double_field (p, 1); - cairo_transform_distance (cairo_t_val (cr), &x, &y); + cairo_user_to_device_distance (cairo_t_val (cr), &x, &y); check_cairo_status (cr); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return Val_unit; + return ml_cairo_point (x, y); } CAMLprim value -ml_cairo_inverse_transform_point (value cr, value p) +ml_cairo_device_to_user (value cr, value p) { - double x = Double_field (p, 0); - double y = Double_field (p, 1); - cairo_inverse_transform_point (cairo_t_val (cr), &x, &y); + double x, y; + x = Double_field (p, 0); + y = Double_field (p, 1); + cairo_device_to_user (cairo_t_val (cr), &x, &y); check_cairo_status (cr); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return Val_unit; + return ml_cairo_point (x, y); } CAMLprim value -ml_cairo_inverse_transform_distance (value cr, value p) +ml_cairo_device_to_user_distance (value cr, value p) { - double x = Double_field (p, 0); - double y = Double_field (p, 1); - cairo_inverse_transform_distance (cairo_t_val (cr), &x, &y); + double x, y; + x = Double_field (p, 0); + y = Double_field (p, 1); + cairo_device_to_user_distance (cairo_t_val (cr), &x, &y); check_cairo_status (cr); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return Val_unit; + return ml_cairo_point (x, y); } -CAMLprim value -ml_cairo_new_path (value v_cr) -{ - cairo_new_path (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_0_cairo(new_path) -CAMLprim value -ml_cairo_move_to (value v_cr, value v_x, value v_y) -{ - cairo_move_to (cairo_t_val (v_cr), Double_val (v_x), Double_val (v_y)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_2_cairo(move_to, Double_val, Double_val) -CAMLprim value -ml_cairo_line_to (value v_cr, value v_x, value v_y) -{ - cairo_line_to (cairo_t_val (v_cr), Double_val (v_x), Double_val (v_y)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_2_cairo(line_to, Double_val, Double_val) -CAMLprim value -ml_cairo_curve_to (value v_cr, value v_x1, value v_y1, value v_x2, value v_y2, - value v_x3, value v_y3) -{ - cairo_curve_to (cairo_t_val (v_cr), Double_val (v_x1), Double_val (v_y1), - Double_val (v_x2), Double_val (v_y2), Double_val (v_x3), - Double_val (v_y3)); - check_cairo_status (v_cr); - return Val_unit; -} -ML_bc7(cairo_curve_to) +wML_6_cairo(curve_to, Double_val, Double_val, Double_val, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_arc (value v_cr, value v_xc, value v_yc, value v_radius, - value v_angle1, value v_angle2) -{ - cairo_arc (cairo_t_val (v_cr), Double_val (v_xc), Double_val (v_yc), - Double_val (v_radius), Double_val (v_angle1), - Double_val (v_angle2)); - check_cairo_status (v_cr); - return Val_unit; -} -ML_bc6(cairo_arc) +wML_5_cairo(arc, Double_val, Double_val, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_arc_negative (value v_cr, value v_xc, value v_yc, value v_radius, - value v_angle1, value v_angle2) -{ - cairo_arc_negative (cairo_t_val (v_cr), Double_val (v_xc), - Double_val (v_yc), Double_val (v_radius), - Double_val (v_angle1), Double_val (v_angle2)); - check_cairo_status (v_cr); - return Val_unit; -} -ML_bc6(cairo_arc_negative) +wML_5_cairo(arc_negative, Double_val, Double_val, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_rel_move_to (value v_cr, value v_dx, value v_dy) -{ - cairo_rel_move_to (cairo_t_val (v_cr), Double_val (v_dx), - Double_val (v_dy)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_2_cairo(rel_move_to, Double_val, Double_val) -CAMLprim value -ml_cairo_rel_line_to (value v_cr, value v_dx, value v_dy) -{ - cairo_rel_line_to (cairo_t_val (v_cr), Double_val (v_dx), - Double_val (v_dy)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_2_cairo(rel_line_to, Double_val, Double_val) -CAMLprim value -ml_cairo_rel_curve_to (value v_cr, value v_dx1, value v_dy1, value v_dx2, - value v_dy2, value v_dx3, value v_dy3) -{ - cairo_rel_curve_to (cairo_t_val (v_cr), Double_val (v_dx1), - Double_val (v_dy1), Double_val (v_dx2), - Double_val (v_dy2), Double_val (v_dx3), - Double_val (v_dy3)); - check_cairo_status (v_cr); - return Val_unit; -} -ML_bc7(cairo_rel_curve_to) +wML_6_cairo(rel_curve_to, Double_val, Double_val, Double_val, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_rectangle (value v_cr, value v_x, value v_y, value v_width, - value v_height) -{ - cairo_rectangle (cairo_t_val (v_cr), Double_val (v_x), Double_val (v_y), - Double_val (v_width), Double_val (v_height)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_4_cairo(rectangle, Double_val, Double_val, Double_val, Double_val) -CAMLprim value -ml_cairo_close_path (value v_cr) -{ - cairo_close_path (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_0_cairo(close_path) -CAMLprim value -ml_cairo_stroke (value v_cr) -{ - cairo_stroke (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_0_cairo(paint) -CAMLprim value -ml_cairo_fill (value v_cr) -{ - cairo_fill (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(paint_with_alpha, Double_val) -CAMLprim value -ml_cairo_copy_page (value v_cr) -{ - cairo_copy_page (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(mask, cairo_pattern_t_val) -CAMLprim value -ml_cairo_show_page (value v_cr) -{ - cairo_show_page (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_3_cairo(mask_surface, cairo_surface_t_val, Double_val, Double_val) + +wML_0_cairo(stroke) + +wML_0_cairo(stroke_preserve) + +wML_0_cairo(fill) + +wML_0_cairo(fill_preserve) + +wML_0_cairo(copy_page) + +wML_0_cairo(show_page) CAMLprim value -ml_cairo_in_stroke (value v_cr, value v_x, value v_y) +ml_cairo_in_stroke (value v_cr, value p) { - int c_ret; + cairo_bool_t c_ret; c_ret = - cairo_in_stroke (cairo_t_val (v_cr), Double_val (v_x), Double_val (v_y)); + cairo_in_stroke (cairo_t_val (v_cr), Double_field (p, 0), Double_field (p, 1)); check_cairo_status (v_cr); return Val_bool (c_ret); } CAMLprim value -ml_cairo_in_fill (value v_cr, value v_x, value v_y) +ml_cairo_in_fill (value v_cr, value p) { - int c_ret; + cairo_bool_t c_ret; c_ret = - cairo_in_fill (cairo_t_val (v_cr), Double_val (v_x), Double_val (v_y)); + cairo_in_fill (cairo_t_val (v_cr), Double_field (p, 0), Double_field (p, 1)); check_cairo_status (v_cr); return Val_bool (c_ret); } @@ -572,11 +232,11 @@ ml_cairo_stroke_extents (value v_cr) { CAMLparam0 (); CAMLlocal1 (t); - t = alloc_tuple (4); - Store_field (t, 0, copy_double (x1)); - Store_field (t, 1, copy_double (y1)); - Store_field (t, 2, copy_double (x2)); - Store_field (t, 3, copy_double (y2)); + t = caml_alloc_tuple (4); + Store_field (t, 0, caml_copy_double (x1)); + Store_field (t, 1, caml_copy_double (y1)); + Store_field (t, 2, caml_copy_double (x2)); + Store_field (t, 3, caml_copy_double (y2)); CAMLreturn (t); } } @@ -590,529 +250,270 @@ ml_cairo_fill_extents (value v_cr) { CAMLparam0 (); CAMLlocal1 (t); - t = alloc_tuple (4); - Store_field (t, 0, copy_double (x1)); - Store_field (t, 1, copy_double (y1)); - Store_field (t, 2, copy_double (x2)); - Store_field (t, 3, copy_double (y2)); + t = caml_alloc_tuple (4); + Store_field (t, 0, caml_copy_double (x1)); + Store_field (t, 1, caml_copy_double (y1)); + Store_field (t, 2, caml_copy_double (x2)); + Store_field (t, 3, caml_copy_double (y2)); CAMLreturn (t); } } -CAMLprim value -ml_cairo_init_clip (value v_cr) -{ - cairo_init_clip (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_0_cairo(clip) -CAMLprim value -ml_cairo_clip (value v_cr) -{ - cairo_clip (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_unit; -} - -Make_Val_final_pointer(cairo_font_t, Id, cairo_font_destroy, 20) - -static void -cairo_glyph_t_val (cairo_glyph_t * _s, value _v) -{ - _s->index = Long_val (Field (_v, 0)); - _s->x = Double_val (Field (_v, 1)); - _s->y = Double_val (Field (_v, 2)); -} +wML_0_cairo(clip_preserve) -static value -Val_cairo_font_extents_t (cairo_font_extents_t * _s) -{ - value _v; - _v = alloc_small (5 * Double_wosize, Double_array_tag); - Store_double_field (_v, 0, _s->ascent); - Store_double_field (_v, 1, _s->descent); - Store_double_field (_v, 2, _s->height); - Store_double_field (_v, 3, _s->max_x_advance); - Store_double_field (_v, 4, _s->max_y_advance); - return _v; -} +wML_0_cairo(reset_clip) -static value -Val_cairo_text_extents_t (cairo_text_extents_t * _s) -{ - value _v; - _v = alloc_small (6 * Double_wosize, Double_array_tag); - Store_double_field (_v, 0, _s->x_bearing); - Store_double_field (_v, 1, _s->y_bearing); - Store_double_field (_v, 2, _s->width); - Store_double_field (_v, 3, _s->height); - Store_double_field (_v, 4, _s->x_advance); - Store_double_field (_v, 5, _s->y_advance); - return _v; -} + #define cairo_font_weight_t_val(v) ((cairo_font_weight_t) Int_val(v)) #define Val_cairo_font_weight_t(v) Val_int(v) #define cairo_font_slant_t_val(v) ((cairo_font_slant_t) Int_val(v)) #define Val_cairo_font_slant_t(v) Val_int(v) -CAMLprim value -ml_cairo_select_font (value v_ct, value v_family, value v_slant, - value v_weight) -{ - cairo_select_font (cairo_t_val (v_ct), String_val (v_family), - cairo_font_slant_t_val (v_slant), - cairo_font_weight_t_val (v_weight)); - check_cairo_status (v_ct); - return Val_unit; -} +wML_3_cairo(select_font_face, String_val, cairo_font_slant_t_val, cairo_font_weight_t_val) -CAMLprim value -ml_cairo_scale_font (value v_cr, value v_scale) -{ - cairo_scale_font (cairo_t_val (v_cr), Double_val (v_scale)); - check_cairo_status (v_cr); - return Val_unit; -} +wML_1_cairo(set_font_size, Double_val) CAMLprim value -ml_cairo_transform_font (value v_cr, value v_matrix) +ml_cairo_set_font_matrix (value v_cr, value v_matrix) { - cairo_transform_font (cairo_t_val (v_cr), cairo_matrix_t_val (v_matrix)); +#ifndef ARCH_ALIGN_DOUBLE + cairo_set_font_matrix (cairo_t_val (v_cr), cairo_matrix_t_val (v_matrix)); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (v_matrix, &mat); + cairo_set_font_matrix (cairo_t_val (v_cr), &mat); +#endif check_cairo_status (v_cr); return Val_unit; } CAMLprim value -ml_cairo_show_text (value v_ct, value v_utf8) +ml_cairo_get_font_matrix (value v_cr) { - cairo_show_text (cairo_t_val (v_ct), String_val (v_utf8)); - check_cairo_status (v_ct); - return Val_unit; +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam1(v_cr); + value v = cairo_matrix_alloc(); + cairo_get_font_matrix (cairo_t_val (v_cr), cairo_matrix_t_val (v)); + CAMLreturn(v); +#else + cairo_matrix_t mat; + cairo_get_font_matrix (cairo_t_val (v_cr), &mat); + check_cairo_status (v_cr); + return ml_convert_cairo_matrix_out (&mat); +#endif } -CAMLprim value -ml_cairo_show_glyphs (value v_ct, value v_glyphs) -{ - size_t num_glyphs = Wosize_val (v_glyphs); - cairo_glyph_t c_glyphs[num_glyphs]; - unsigned int i; - for (i = 0; i < num_glyphs; i++) - cairo_glyph_t_val (&c_glyphs[i], Field (v_glyphs, i)); - cairo_show_glyphs (cairo_t_val (v_ct), c_glyphs, num_glyphs); - check_cairo_status (v_ct); - return Val_unit; -} +wML_1_cairo(show_text, String_val) -CAMLprim value -ml_cairo_current_font (value cr) +cairo_glyph_t * +ml_convert_cairo_glypth_in (value v, int *num_glyphs) { - cairo_font_t *f = cairo_current_font (cairo_t_val (cr)); - check_cairo_status (cr); - cairo_font_reference (f); - return Val_cairo_font_t (f); -} - -CAMLprim value -ml_cairo_current_font_extents (value cr) -{ - cairo_font_extents_t e; - cairo_current_font_extents (cairo_t_val (cr), &e); - check_cairo_status (cr); - return Val_cairo_font_extents_t (&e); + size_t i, n = Wosize_val (v); + cairo_glyph_t *g = caml_stat_alloc (n * sizeof (cairo_glyph_t)); + for (i = 0; i < n; i++) + { + value vg = Field (v, i); + g[i].index = Unsigned_long_val (Field (vg, 0)); + g[i].x = Double_val (Field (vg, 1)); + g[i].y = Double_val (Field (vg, 2)); + } + *num_glyphs = n; + return g; } CAMLprim value -ml_cairo_set_font (value v_ct, value v_font) +ml_cairo_show_glyphs (value v_cr, value v_glyphs) { - cairo_set_font (cairo_t_val (v_ct), cairo_font_t_val (v_font)); - check_cairo_status (v_ct); + int num_glyphs; + cairo_glyph_t *c_glyphs; + c_glyphs = ml_convert_cairo_glypth_in (v_glyphs, &num_glyphs); + cairo_show_glyphs (cairo_t_val (v_cr), c_glyphs, num_glyphs); + caml_stat_free (c_glyphs); + check_cairo_status (v_cr); return Val_unit; } -CAMLprim value -ml_cairo_text_extents (value v_ct, value v_utf8) -{ - cairo_text_extents_t c_extents; - cairo_text_extents (cairo_t_val (v_ct), String_val (v_utf8), &c_extents); - check_cairo_status (v_ct); - return Val_cairo_text_extents_t (&c_extents); -} - -CAMLprim value -ml_cairo_glyph_extents (value v_ct, value v_glyphs) -{ - size_t num_glyphs = Wosize_val (v_glyphs); - cairo_text_extents_t c_extents; - cairo_glyph_t c_glyphs[num_glyphs]; - unsigned int i; - for (i = 0; i < num_glyphs; i++) - cairo_glyph_t_val (&c_glyphs[i], Field (v_glyphs, i)); - cairo_glyph_extents (cairo_t_val (v_ct), c_glyphs, num_glyphs, &c_extents); - check_cairo_status (v_ct); - return Val_cairo_text_extents_t (&c_extents); -} +wML_1 (cairo_get_font_face, cairo_t_val, Val_cairo_font_face_ref) -CAMLprim value -ml_cairo_text_path (value v_ct, value v_utf8) -{ - cairo_text_path (cairo_t_val (v_ct), String_val (v_utf8)); - check_cairo_status (v_ct); - return Val_unit; -} - -CAMLprim value -ml_cairo_glyph_path (value v_ct, value v_glyphs) -{ - size_t num_glyphs = Wosize_val (v_glyphs); - cairo_glyph_t c_glyphs[num_glyphs]; - unsigned int i; - for (i = 0; i < num_glyphs; i++) - cairo_glyph_t_val (&c_glyphs[i], Field (v_glyphs, i)); - cairo_glyph_path (cairo_t_val (v_ct), c_glyphs, num_glyphs); - check_cairo_status (v_ct); - return Val_unit; +value +Val_cairo_font_extents (cairo_font_extents_t * s) +{ + value v = caml_alloc_small (5 * Double_wosize, Double_array_tag); + Store_double_field (v, 0, s->ascent); + Store_double_field (v, 1, s->descent); + Store_double_field (v, 2, s->height); + Store_double_field (v, 3, s->max_x_advance); + Store_double_field (v, 4, s->max_y_advance); + return v; } -/* ML_1(cairo_font_destroy, cairo_font_t_val, Unit) */ - CAMLprim value -ml_cairo_font_extents (value font, value matrix) +ml_cairo_font_extents (value cr) { cairo_font_extents_t e; - cairo_status_t status; - status = cairo_font_extents (cairo_font_t_val (font), cairo_matrix_t_val (matrix), &e); - cairo_treat_status (status); - return Val_cairo_font_extents_t (&e); -} - -CAMLprim value -ml_cairo_font_glyph_extents (value v_font, value v_matrix, value v_glyphs) -{ - size_t num_glyphs = Wosize_val (v_glyphs); - cairo_text_extents_t c_extents; - cairo_glyph_t c_glyphs[num_glyphs]; - unsigned int i; - for (i = 0; i < num_glyphs; i++) - cairo_glyph_t_val (&c_glyphs[i], Field (v_glyphs, i)); - cairo_font_glyph_extents (cairo_font_t_val (v_font), cairo_matrix_t_val (v_matrix), - c_glyphs, num_glyphs, &c_extents); - return Val_cairo_text_extents_t (&c_extents); -} - -CAMLprim value -ml_cairo_show_surface (value v_cr, value v_surface, value v_width, - value v_height) -{ - cairo_show_surface (cairo_t_val (v_cr), cairo_surface_t_val (v_surface), - Int_val (v_width), Int_val (v_height)); - check_cairo_status (v_cr); - return Val_unit; -} - -CAMLprim value -ml_cairo_current_operator (value v_cr) -{ - cairo_operator_t c_ret; - c_ret = cairo_current_operator (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_cairo_operator_t (c_ret); -} - -CAMLprim value -ml_cairo_current_rgb_color (value cr) -{ - CAMLparam1 (cr); - CAMLlocal4 (v, vr, vg, vb); - double r, g, b; - cairo_current_rgb_color (cairo_t_val (cr), &r, &g, &b); + cairo_font_extents (cairo_t_val (cr), &e); check_cairo_status (cr); - vr = copy_double (r); - vg = copy_double (g); - vb = copy_double (b); - v = alloc_small (3, 0); - Field (v, 0) = vr; - Field (v, 1) = vg; - Field (v, 2) = vb; - CAMLreturn (v); + return Val_cairo_font_extents (&e); } -CAMLprim value -ml_cairo_current_pattern (value v_cr) -{ - cairo_pattern_t *c_ret; - c_ret = cairo_current_pattern (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - cairo_pattern_reference (c_ret); - return Val_cairo_pattern_t (c_ret); -} +wML_1_cairo (set_font_face, cairo_font_face_t_val) -CAMLprim value -ml_cairo_current_alpha (value v_cr) -{ - double c_ret; - c_ret = cairo_current_alpha (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return copy_double (c_ret); -} - -CAMLprim value -ml_cairo_current_tolerance (value v_cr) -{ - double c_ret; - c_ret = cairo_current_tolerance (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return copy_double (c_ret); -} - -CAMLprim value -ml_cairo_current_point (value cr) -{ - value v; - double x, y; - cairo_current_point (cairo_t_val (cr), &x, &y); - check_cairo_status (cr); - v = alloc_small (2 * Double_wosize, Double_array_tag); - Store_double_field (v, 0, x); - Store_double_field (v, 1, y); +value +Val_cairo_text_extents (cairo_text_extents_t * s) +{ + value v = caml_alloc_small (6 * Double_wosize, Double_array_tag); + Store_double_field (v, 0, s->x_bearing); + Store_double_field (v, 1, s->y_bearing); + Store_double_field (v, 2, s->width); + Store_double_field (v, 3, s->height); + Store_double_field (v, 4, s->x_advance); + Store_double_field (v, 5, s->y_advance); return v; } CAMLprim value -ml_cairo_current_fill_rule (value v_cr) +ml_cairo_text_extents (value v_cr, value v_utf8) { - cairo_fill_rule_t c_ret; - c_ret = cairo_current_fill_rule (cairo_t_val (v_cr)); + cairo_text_extents_t c_extents; + cairo_text_extents (cairo_t_val (v_cr), String_val (v_utf8), &c_extents); check_cairo_status (v_cr); - return Val_cairo_fill_rule_t (c_ret); + return Val_cairo_text_extents (&c_extents); } CAMLprim value -ml_cairo_current_line_width (value v_cr) +ml_cairo_glyph_extents (value v_cr, value v_glyphs) { - double c_ret; - c_ret = cairo_current_line_width (cairo_t_val (v_cr)); + int num_glyphs; + cairo_glyph_t *c_glyphs; + cairo_text_extents_t c_extents; + c_glyphs = ml_convert_cairo_glypth_in (v_glyphs, &num_glyphs); + cairo_glyph_extents (cairo_t_val (v_cr), c_glyphs, num_glyphs, &c_extents); + caml_stat_free (c_glyphs); check_cairo_status (v_cr); - return copy_double (c_ret); + return Val_cairo_text_extents (&c_extents); } -CAMLprim value -ml_cairo_current_line_cap (value v_cr) -{ - cairo_line_cap_t c_ret; - c_ret = cairo_current_line_cap (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return Val_cairo_line_cap_t (c_ret); -} +wML_1_cairo(text_path, String_val) CAMLprim value -ml_cairo_current_line_join (value v_cr) +ml_cairo_glyph_path (value v_cr, value v_glyphs) { - cairo_line_join_t c_ret; - c_ret = cairo_current_line_join (cairo_t_val (v_cr)); + int num_glyphs; + cairo_glyph_t *c_glyphs; + c_glyphs = ml_convert_cairo_glypth_in (v_glyphs, &num_glyphs); + cairo_glyph_path (cairo_t_val (v_cr), c_glyphs, num_glyphs); + caml_stat_free (c_glyphs); check_cairo_status (v_cr); - return Val_cairo_line_join_t (c_ret); + return Val_unit; } -CAMLprim value -ml_cairo_current_miter_limit (value v_cr) -{ - double c_ret; - c_ret = cairo_current_miter_limit (cairo_t_val (v_cr)); - check_cairo_status (v_cr); - return copy_double (c_ret); -} + +#define cairo_get(cname, conv) wML_1(cairo_get_##cname, cairo_t_val, conv) -CAMLprim value -ml_cairo_current_matrix (value v_cr, value v_matrix) -{ - cairo_current_matrix (cairo_t_val (v_cr), cairo_matrix_t_val (v_matrix)); - check_cairo_status (v_cr); - return Val_unit; -} +cairo_get(operator, Val_cairo_operator_t) -CAMLprim value -ml_cairo_current_target_surface (value cr) -{ - cairo_surface_t *s = cairo_current_target_surface (cairo_t_val (cr)); - check_cairo_status (cr); - cairo_surface_reference (s); - return Val_cairo_surface_t (s); -} +cairo_get(source, Val_cairo_pattern_ref) -CAMLprim value -ml_cairo_status (value v_cr) -{ - value v; - int status = cairo_status (cairo_t_val (v_cr)); - if (status == CAIRO_STATUS_SUCCESS) - v = Val_unit; - else - { - v = alloc_small (1, 0); - Field (v, 0) = Val_int (status - 1); - } - return v; -} -ML_1(cairo_status_string, cairo_t_val, copy_string) +cairo_get(tolerance, caml_copy_double) CAMLprim value -ml_cairo_surface_create_for_image (value img) +ml_cairo_get_current_point (value cr) { - cairo_surface_t *s; - s = cairo_surface_create_for_image (Bp_val (Field (img, 0)), - cairo_format_t_val (Field (img, 1)), - Int_val (Field (img, 2)), - Int_val (Field (img, 3)), - Int_val (Field (img, 4))); - return Val_cairo_surface_t (s); + double x, y; + cairo_get_current_point (cairo_t_val (cr), &x, &y); + return ml_cairo_point (x, y); } -ML_4(cairo_surface_create_similar, cairo_surface_t_val, cairo_format_t_val, Int_val, Int_val, Val_cairo_surface_t) -/* ML_1(cairo_surface_destroy, cairo_surface_t_val, Unit) */ +cairo_get(fill_rule, Val_cairo_fill_rule_t) -ML_2(cairo_surface_set_repeat, cairo_surface_t_val, Int_val, Val_cairo_status_t) -ML_2(cairo_surface_set_matrix, cairo_surface_t_val, cairo_matrix_t_val, Val_cairo_status_t) -ML_2(cairo_surface_get_matrix, cairo_surface_t_val, cairo_matrix_t_val, Val_cairo_status_t) +cairo_get(line_width, caml_copy_double) -#define cairo_filter_t_val(v) ((cairo_filter_t) Int_val(v)) -#define Val_cairo_filter_t(v) Val_int(v) +cairo_get(line_cap, Val_cairo_line_cap_t) -ML_2(cairo_surface_set_filter, cairo_surface_t_val, cairo_filter_t_val, Val_cairo_status_t) -ML_1(cairo_surface_get_filter, cairo_surface_t_val, Val_cairo_filter_t) +cairo_get(line_join, Val_cairo_line_join_t) -ML_3(cairo_image_surface_create, cairo_format_t_val, Int_val, Int_val, Val_cairo_surface_t) +cairo_get(miter_limit, caml_copy_double) CAMLprim value -ml_cairo_image_surface_create_for_data (value img) +ml_cairo_get_matrix (value v_cr) { - cairo_surface_t *s; - s = cairo_image_surface_create_for_data (Bp_val (Field (img, 0)), - cairo_format_t_val (Field - (img, 1)), - Int_val (Field (img, 2)), - Int_val (Field (img, 3)), - Int_val (Field (img, 4))); - return Val_cairo_surface_t (s); +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam1(v_cr); + value v = cairo_matrix_alloc(); + cairo_get_matrix (cairo_t_val (v_cr), cairo_matrix_t_val (v)); + CAMLreturn(v); +#else + cairo_matrix_t mat; + cairo_get_matrix (cairo_t_val (v_cr), &mat); + return ml_convert_cairo_matrix_out (&mat); +#endif } -#ifdef CAIRO_HAS_PS_SURFACE -ML_5(cairo_ps_surface_create, FILE_val, Double_val, Double_val, Double_val, Double_val, Val_cairo_surface_t) -#else -Unsupported(ml_cairo_ps_surface_create) -#endif /* CAIRO_HAS_PS_SURFACE */ +cairo_get(target, Val_cairo_surface_ref) -#ifdef CAIRO_HAS_PDF_SURFACE -ML_5(cairo_pdf_surface_create, FILE_val, Double_val, Double_val, Double_val, Double_val, Val_cairo_surface_t) -#else -Unsupported(ml_cairo_pdf_surface_create) -#endif /* CAIRO_HAS_PDF_SURFACE */ +/* ml_cairo_path */ +/* ml_cairo_status */ -#ifdef CAIRO_HAS_PNG_SURFACE -ML_4(cairo_png_surface_create, FILE_val, cairo_format_t_val, Double_val, Double_val, Val_cairo_surface_t) -#else -Unsupported(ml_cairo_png_surface_create) -#endif /* CAIRO_HAS_PNG_SURFACE */ - -ML_1 (cairo_pattern_create_for_surface, cairo_surface_t_val, Val_cairo_pattern_t) -ML_4 (cairo_pattern_create_linear, Double_val, Double_val, Double_val, Double_val, Val_cairo_pattern_t) -ML_6 (cairo_pattern_create_radial, Double_val, Double_val, Double_val, Double_val, Double_val, Double_val, Val_cairo_pattern_t) -ML_bc6 (cairo_pattern_create_radial) -ML_6 (cairo_pattern_add_color_stop, cairo_pattern_t_val, Double_val, Double_val, Double_val, Double_val, Double_val, Val_cairo_status_t) -ML_bc6 (cairo_pattern_add_color_stop) -ML_2 (cairo_pattern_set_matrix, cairo_pattern_t_val, cairo_matrix_t_val, Val_cairo_status_t) -ML_2 (cairo_pattern_get_matrix, cairo_pattern_t_val, cairo_matrix_t_val, Val_cairo_status_t) - -#define cairo_extend_t_val(v) ((cairo_extend_t) Int_val(v)) -#define Val_cairo_extend_t(v) Val_int(v) - -ML_2 (cairo_pattern_set_extend, cairo_pattern_t_val, cairo_extend_t_val, Val_cairo_status_t) -ML_1 (cairo_pattern_get_extend, cairo_pattern_t_val, Val_cairo_extend_t) -ML_2 (cairo_pattern_set_filter, cairo_pattern_t_val, cairo_filter_t_val, Val_cairo_status_t) -ML_1 (cairo_pattern_get_filter, cairo_pattern_t_val, Val_cairo_filter_t) - -ML_0(cairo_matrix_create, Val_cairo_matrix_t) -/* ML_1(cairo_matrix_destroy, cairo_matrix_t_val, Unit) */ -ML_2(cairo_matrix_copy, cairo_matrix_t_val, cairo_matrix_t_val, Val_cairo_status_t) -ML_1(cairo_matrix_set_identity, cairo_matrix_t_val, Val_cairo_status_t) -CAMLprim value -ml_cairo_matrix_set_affine (value m, value aff) + +value * +ml_cairo_make_root (value v) { - if (Double_array_length (aff) != 6) - invalid_argument ("not a matrix"); - - cairo_matrix_set_affine (cairo_matrix_t_val (m), - Double_field (aff, 0), - Double_field (aff, 1), - Double_field (aff, 2), - Double_field (aff, 3), - Double_field (aff, 4), Double_field (aff, 5)); - return Val_unit; + value *root = caml_stat_alloc (sizeof (value *)); + *root = v; + caml_register_global_root (root); + return root; } -CAMLprim value -ml_cairo_matrix_get_affine (value m) +value * +ml_cairo_make_closure (value f) { - CAMLparam1 (m); - CAMLlocal1 (v); - double a, b, c, d, tx, ty; - cairo_matrix_get_affine (cairo_matrix_t_val (m), &a, &b, &c, &d, &tx, &ty); - v = alloc_small (6 * Double_wosize, Double_array_tag); - Store_double_field (v, 0, a); - Store_double_field (v, 1, b); - Store_double_field (v, 2, c); - Store_double_field (v, 3, d); - Store_double_field (v, 4, tx); - Store_double_field (v, 5, ty); - CAMLreturn (v); + CAMLparam1(f); + value c; + c = caml_alloc_small (2, 0); + Field (c, 0) = f; + Field (c, 1) = Val_unit; + CAMLreturn (ml_cairo_make_root (c)); } -ML_3(cairo_matrix_translate, cairo_matrix_t_val, Double_val, Double_val, Val_cairo_status_t) -ML_3(cairo_matrix_scale, cairo_matrix_t_val, Double_val, Double_val, Val_cairo_status_t) -ML_2(cairo_matrix_rotate, cairo_matrix_t_val, Double_val, Val_cairo_status_t) -ML_1(cairo_matrix_invert, cairo_matrix_t_val, Val_cairo_status_t) -ML_3(cairo_matrix_multiply, cairo_matrix_t_val, cairo_matrix_t_val, cairo_matrix_t_val, Val_cairo_status_t) - -CAMLprim value -ml_cairo_matrix_transform_distance (value m, value p) +cairo_status_t +ml_cairo_write_func (void *closure, const unsigned char *data, unsigned int length) { - cairo_status_t s; - double x, y; - x = Double_field (p, 0); - y = Double_field (p, 1); - s = cairo_matrix_transform_distance (cairo_matrix_t_val (m), &x, &y); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return Val_cairo_status_t (s); + value s, res, *c = closure; + s = caml_alloc_string (length); + memcpy (String_val (s), data, length); + res = caml_callback_exn (Field (*c, 0), s); + if (Is_exception_result (res)) + { + Store_field (*c, 1, res); + return CAIRO_STATUS_WRITE_ERROR; + } + return CAIRO_STATUS_SUCCESS; } -CAMLprim value -ml_cairo_matrix_transform_point (value m, value p) +cairo_status_t +ml_cairo_read_func (void *closure, unsigned char *data, unsigned int length) { - cairo_status_t s; - double x, y; - x = Double_field (p, 0); - y = Double_field (p, 1); - s = cairo_matrix_transform_point (cairo_matrix_t_val (m), &x, &y); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return Val_cairo_status_t (s); + value s, res, *c = closure; + s = caml_alloc_string (length); + res = caml_callback_exn (Field (*c, 0), s); + if (Is_exception_result (res)) + { + Store_field (*c, 1, res); + return CAIRO_STATUS_READ_ERROR; + } + memcpy (data, String_val (s), length); + return CAIRO_STATUS_SUCCESS; } -CAMLprim value -ml_cairo_finalise_target (value cr) -{ - cairo_set_target_surface (cairo_t_val (cr), NULL); - return Val_unit; -} -CAMLprim value -ml_cairo_surface_finalise (value s) -{ - cairo_surface_t *surf = cairo_surface_t_val (s); - cairo_surface_destroy (surf); - Store_pointer (s, NULL); - return Val_unit; -} + +wML_3(cairo_image_surface_create, cairo_format_t_val, Int_val, Int_val, Val_cairo_surface_t) + +/* image_surface_create_for_data */ + +wML_1 (cairo_image_surface_get_width, cairo_surface_t_val, Val_int) +wML_1 (cairo_image_surface_get_height, cairo_surface_t_val, Val_int) diff --git a/src/ml_cairo.h b/src/ml_cairo.h index 7010952..e0ec61d 100644 --- a/src/ml_cairo.h +++ b/src/ml_cairo.h @@ -6,21 +6,69 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -struct ml_cairo { - cairo_t *cr; - int suspend_exn; -}; -#define cairo_t_val(v) (((struct ml_cairo *) Data_custom_val(v))->cr) +#include <assert.h> +#include <string.h> + +#define CAML_NAME_SPACE + +#include <caml/alloc.h> +#include <caml/memory.h> +#include <caml/fail.h> +#include <caml/custom.h> +#include <caml/callback.h> + +#include "ml_cairo_wrappers.h" + +#include <cairo.h> + +/* cairo */ +#define cairo_t_val(v) wPointer_val(cairo_t, v) value Val_cairo_t (cairo_t *); +#define cairo_surface_t_val(v) wPointer_val(cairo_surface_t, v) +value Val_cairo_surface_t (cairo_surface_t *); +#define Val_cairo_surface_ref(p) Val_cairo_surface_t ((cairo_surface_reference(p), p)) + +#define cairo_pattern_t_val(v) wPointer_val(cairo_pattern_t, v) +value Val_cairo_pattern_t (cairo_pattern_t *); +#define Val_cairo_pattern_ref(p) Val_cairo_pattern_t ((cairo_pattern_reference(p), p)) + #define cairo_format_t_val(v) ((cairo_format_t) Int_val(v)) #define Val_cairo_format_t(v) Val_int(v) -#define cairo_surface_t_val(v) ((cairo_surface_t *)Pointer_val(v)) -value Val_cairo_surface_t (cairo_surface_t *); +#define cairo_font_face_t_val(v) wPointer_val(cairo_font_face_t, v) +value Val_cairo_font_face_t (cairo_font_face_t *); +#define Val_cairo_font_face_ref(p) Val_cairo_font_face_t ((cairo_font_face_reference(p), p)) + +#define cairo_scaled_font_t_val(v) wPointer_val(cairo_scaled_font_t, v) +value Val_cairo_scaled_font_t (cairo_scaled_font_t *); + +/* cairo_matrix */ +#ifdef ARCH_ALIGN_DOUBLE +void ml_convert_cairo_matrix_in (value, cairo_matrix_t *); +value ml_convert_cairo_matrix_out (cairo_matrix_t *); +#else +# define cairo_matrix_t_val(v) (cairo_matrix_t *)(v) +# define cairo_matrix_alloc() caml_alloc_small (6 * Double_wosize, Double_array_tag) +# define cairo_copy_matrix(dst, src) memcpy (Bp_val(dst), Bp_val(src), 6 * Double_wosize * sizeof (value)) +#endif + +value ml_cairo_point (double, double); + +cairo_glyph_t * ml_convert_cairo_glypth_in (value v, int *); +value Val_cairo_font_extents (cairo_font_extents_t *); +value Val_cairo_text_extents (cairo_text_extents_t *); + +/* cairo_status */ +void ml_cairo_treat_status (cairo_status_t) Noreturn; +#define cairo_treat_status(s) if (s != CAIRO_STATUS_SUCCESS) ml_cairo_treat_status (s) +#define check_cairo_status(cr) cairo_treat_status (cairo_status (cairo_t_val (cr))) +#define report_null_pointer() ml_cairo_treat_status (CAIRO_STATUS_NULL_POINTER) -#define cairo_font_t_val(v) ((cairo_font_t *)Pointer_val(v)) -value Val_cairo_font_t (cairo_font_t *); +/* stream callbacks */ +value *ml_cairo_make_closure (value); +value *ml_cairo_make_root (value); +cairo_status_t ml_cairo_write_func (void *, const unsigned char *, unsigned int); +cairo_status_t ml_cairo_read_func (void *, unsigned char *, unsigned int); -#define cairo_matrix_t_val(v) ((cairo_matrix_t *)Pointer_val(v)) -value Val_cairo_matrix_t (cairo_matrix_t *); +void ml_cairo_surface_set_user_data (cairo_surface_t *, const cairo_user_data_key_t *, value *); diff --git a/src/ml_cairo_bigarr.c b/src/ml_cairo_bigarr.c index db34174..8f8b184 100644 --- a/src/ml_cairo_bigarr.c +++ b/src/ml_cairo_bigarr.c @@ -6,6 +6,8 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ +#include "ml_cairo.h" + #include <caml/bigarray.h> unsigned long bigarray_byte_size (struct caml_bigarray *); @@ -17,7 +19,7 @@ ml_bigarray_byte_size (value b) } CAMLprim value -bigarray_kind_float (value v) +ml_bigarray_kind_float (value v) { struct caml_bigarray *ba = Bigarray_val (v); @@ -32,3 +34,18 @@ bigarray_kind_float (value v) return Val_false; } } + +CAMLprim value +ml_cairo_image_surface_create_for_data (value img, value fmt, value w, value h, value stride) +{ + static const cairo_user_data_key_t image_data_key; + cairo_surface_t *surf; + surf = cairo_image_surface_create_for_data (Data_bigarray_val (img), + cairo_format_t_val (fmt), + Int_val (w), + Int_val (h), + Int_val (stride)); + ml_cairo_surface_set_user_data (surf, &image_data_key, ml_cairo_make_root (img)); + + return Val_cairo_surface_t (surf); +} diff --git a/src/ml_cairo_channel.c b/src/ml_cairo_channel.c deleted file mode 100644 index c8b1a3d..0000000 --- a/src/ml_cairo_channel.c +++ /dev/null @@ -1,67 +0,0 @@ -/**************************************************************************/ -/* cairo-ocaml -- Objective Caml bindings for Cairo */ -/* Copyright © 2004-2005 Olivier Andrieu */ -/* */ -/* This code is free software and is licensed under the terms of the */ -/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ -/**************************************************************************/ - -#include <caml/mlvalues.h> -#include <caml/alloc.h> -#include <caml/memory.h> -#include <caml/fail.h> -#include <caml/custom.h> - -#include "ml_cairo_wrappers.h" - -#include <errno.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include "caml_io.h" -#include "ml_cairo_channel.h" - -static value -ml_FILE_of_fd (int fd) -{ - int new_fd; - FILE *f; - - new_fd = dup (fd); - if (new_fd < 0) - goto fail; - f = fdopen (new_fd, "w"); - if (!f) - goto fail; - return Val_ptr (f); - -fail: - raise_sys_error (copy_string (strerror (errno))); -} - -CAMLprim value -ml_FILE_of_channel (value v) -{ - struct channel *c = Channel (v); - flush (c); - return ml_FILE_of_fd (c->fd); -} - -CAMLprim value -ml_FILE_of_file_descr (value v) -{ -#ifndef _WIN32 - return ml_FILE_of_fd (Int_val (v)); -#else - return failwith ("unsupported"); -#endif -} - -CAMLprim value -ml_fclose (value v) -{ - FILE *f = FILE_val (v); - if (fclose (f)) - raise_sys_error (copy_string (strerror (errno))); - return Val_unit; -} diff --git a/src/ml_cairo_channel.h b/src/ml_cairo_channel.h deleted file mode 100644 index a9ac23f..0000000 --- a/src/ml_cairo_channel.h +++ /dev/null @@ -1,9 +0,0 @@ -/**************************************************************************/ -/* cairo-ocaml -- Objective Caml bindings for Cairo */ -/* Copyright © 2004-2005 Olivier Andrieu */ -/* */ -/* This code is free software and is licensed under the terms of the */ -/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ -/**************************************************************************/ - -#define FILE_val(v) ((FILE *) Pointer_val(v)) diff --git a/src/ml_cairo_font.c b/src/ml_cairo_font.c new file mode 100644 index 0000000..587362f --- /dev/null +++ b/src/ml_cairo_font.c @@ -0,0 +1,61 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +wMake_Val_final_pointer(cairo_font_face_t, cairo_font_face_destroy, 0) + +wMake_Val_final_pointer(cairo_scaled_font_t, cairo_scaled_font_destroy, 0) + +/* font_face_reference */ +/* font_face_destroy */ +/* font_face_get_user_data */ +/* font_face_set_user_data */ + +CAMLprim value +ml_cairo_scaled_font_create (value f, value fmat, value ctm) +{ + cairo_scaled_font_t *sf; +#ifndef ARCH_ALIGN_DOUBLE + sf = cairo_scaled_font_create (cairo_font_face_t_val (f), + cairo_matrix_t_val (fmat), + cairo_matrix_t_val (ctm)); +#else + cairo_matrix_t c_fmat, c_ctm; + ml_convert_cairo_matrix_in (fmat, &c_fmat); + ml_convert_cairo_matrix_in (ctm, &c_ctm); + sf = cairo_scaled_font_create (cairo_font_face_t_val (f), &c_fmat, &c_ctm); +#endif + return Val_cairo_scaled_font_t (sf); +} + +/* scaled_font_face_reference */ +/* scaled_font_face_destroy */ + +CAMLprim value +ml_cairo_scaled_font_extents (value sf) +{ + cairo_status_t status; + cairo_font_extents_t e; + status = cairo_scaled_font_extents (cairo_scaled_font_t_val (sf), &e); + cairo_treat_status (status); + return Val_cairo_font_extents (&e); +} + +CAMLprim value +ml_cairo_scaled_font_glyph_extents (value sf, value v_glyphs) +{ + int num_glyphs; + cairo_glyph_t *c_glyphs; + cairo_text_extents_t c_extents; + c_glyphs = ml_convert_cairo_glypth_in (v_glyphs, &num_glyphs); + cairo_scaled_font_glyph_extents (cairo_scaled_font_t_val (sf), + c_glyphs, num_glyphs, &c_extents); + caml_stat_free (c_glyphs); + return Val_cairo_text_extents (&c_extents); +} diff --git a/src/ml_cairo_ft.c b/src/ml_cairo_ft.c index 294d3c3..150e19e 100644 --- a/src/ml_cairo_ft.c +++ b/src/ml_cairo_ft.c @@ -6,24 +6,11 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <caml/mlvalues.h> -#include <caml/alloc.h> -#include <caml/memory.h> -#include <caml/fail.h> -#include <caml/custom.h> -#include <caml/callback.h> - -#include "ml_cairo_wrappers.h" - -#include <cairo.h> -#ifdef CAIRO_HAS_FT_FONT -# include <cairo-ft.h> -#endif #include "ml_cairo.h" -#include "ml_cairo_status.h" -#ifdef CAIRO_HAS_FT_FONT +#if CAIRO_HAS_FT_FONT +# include <cairo-ft.h> /* minimal Freetype interface */ static void diff --git a/src/ml_cairo_lablgtk.c b/src/ml_cairo_lablgtk.c index 7a87d9a..9d3521b 100644 --- a/src/ml_cairo_lablgtk.c +++ b/src/ml_cairo_lablgtk.c @@ -6,52 +6,50 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <gdk-pixbuf/gdk-pixbuf.h> -#include <gdk/gdkx.h> - -#include <cairo.h> -#ifdef CAIRO_HAS_XLIB_SURFACE +#include "ml_cairo.h" +#if CAIRO_HAS_XLIB_SURFACE # include <cairo-xlib.h> #endif -#include <caml/mlvalues.h> -#include <caml/alloc.h> +#include <gdk-pixbuf/gdk-pixbuf.h> +#include <gdk/gdk.h> +#include <gdk/gdkx.h> #include "wrappers.h" #include "ml_gobject.h" #include "ml_gdkpixbuf.h" #include "ml_gdk.h" -#include "ml_cairo.h" -#include "ml_cairo_status.h" - CAMLprim value -cairo_lablgtk_of_pixbuf(value pb) +ml_cairo_lablgtk_of_pixbuf (value pb) { - value v; + static const cairo_user_data_key_t pixbuf_key; + GdkPixbuf *pixbuf = GdkPixbuf_val(pb); cairo_format_t format; gboolean alpha = gdk_pixbuf_get_has_alpha(pixbuf); int nchan = gdk_pixbuf_get_n_channels(pixbuf); int bps = gdk_pixbuf_get_bits_per_sample(pixbuf); + cairo_surface_t *surf; if ((nchan == 4) && (bps == 8) && alpha) format = CAIRO_FORMAT_ARGB32; else - failwith("bad GdkPixbuf format"); + caml_invalid_argument ("bad GdkPixbuf format"); - v = alloc_small(5, 0); - Field(v, 0) = Val_bp(gdk_pixbuf_get_pixels(pixbuf)); - Field(v, 1) = Val_cairo_format_t(format); - Field(v, 2) = Val_int(gdk_pixbuf_get_width(pixbuf)); - Field(v, 3) = Val_int(gdk_pixbuf_get_height(pixbuf)); - Field(v, 4) = Val_int(gdk_pixbuf_get_rowstride(pixbuf)); + surf = cairo_image_surface_create_for_data (gdk_pixbuf_get_pixels (pixbuf), + format, + gdk_pixbuf_get_width(pixbuf), + gdk_pixbuf_get_height(pixbuf), + gdk_pixbuf_get_rowstride(pixbuf)); - return v; + ml_cairo_surface_set_user_data (surf, &pixbuf_key, ml_cairo_make_root (pb)); + + return Val_cairo_surface_t (surf); } CAMLprim value -cairo_lablgtk_shuffle_pixels(value pb) +ml_cairo_lablgtk_shuffle_pixels (value pb) { GdkPixbuf *pixbuf = GdkPixbuf_val(pb); guint w, h, s, i, j; @@ -61,10 +59,10 @@ cairo_lablgtk_shuffle_pixels(value pb) (gdk_pixbuf_get_n_channels(pixbuf) == 4) && (gdk_pixbuf_get_bits_per_sample(pixbuf) == 8), Val_unit); - w = gdk_pixbuf_get_width(pixbuf); - h = gdk_pixbuf_get_height(pixbuf); - s = gdk_pixbuf_get_rowstride(pixbuf); - pixels = gdk_pixbuf_get_pixels(pixbuf); + w = gdk_pixbuf_get_width (pixbuf); + h = gdk_pixbuf_get_height (pixbuf); + s = gdk_pixbuf_get_rowstride (pixbuf); + pixels = gdk_pixbuf_get_pixels (pixbuf); for (i=0; i<h; i++) { p = pixels; @@ -81,53 +79,50 @@ cairo_lablgtk_shuffle_pixels(value pb) } -#ifdef CAIRO_HAS_XLIB_SURFACE +#if CAIRO_HAS_XLIB_SURFACE CAMLprim value -cairo_lablgtk_surface_create_for_drawable(value d, value fmt) +ml_cairo_xlib_surface_create (value d) { - GdkDrawable *draw = GdkDrawable_val(d); - cairo_surface_t *s; - - s = cairo_xlib_surface_create ( - GDK_DRAWABLE_XDISPLAY(draw), - GDK_DRAWABLE_XID(draw), - GDK_VISUAL_XVISUAL(gdk_drawable_get_visual(draw)), - cairo_format_t_val(fmt), - GDK_COLORMAP_XCOLORMAP(gdk_drawable_get_colormap(draw))); - return Val_cairo_surface_t(s); -} + static const cairo_user_data_key_t drawable_key; -CAMLprim value -cairo_lablgtk_set_target_drawable(value cr, value d) -{ - cairo_t *c = cairo_t_val(cr); - GdkDrawable *draw = GdkDrawable_val(d); - GdkDrawable *real_drawable; - gint x_offset, y_offset; - - if (GDK_IS_WINDOW(draw)) - gdk_window_get_internal_paint_info(GDK_WINDOW(draw), - &real_drawable, - &x_offset, &y_offset); + cairo_surface_t *surface; + gint width, height; + GdkDrawable *drawable = GdkDrawable_val(d); + GdkVisual *visual = gdk_drawable_get_visual (drawable); + + gdk_drawable_get_size (drawable, &width, &height); + + if (visual) + surface = cairo_xlib_surface_create (GDK_DRAWABLE_XDISPLAY (drawable), + GDK_DRAWABLE_XID (drawable), + GDK_VISUAL_XVISUAL (visual), + width, height); + else if (gdk_drawable_get_depth (drawable) == 1) + surface = + cairo_xlib_surface_create_for_bitmap (GDK_PIXMAP_XDISPLAY (drawable), + GDK_PIXMAP_XID (drawable), + width, height); else { - real_drawable = draw; - x_offset = 0; - y_offset = 0; + g_warning ("Using Cairo rendering requires the drawable argument to\n" + "have a specified colormap. All windows have a colormap,\n" + "however, pixmaps only have colormap by default if they\n" + "were created with a non-NULL window argument. Otherwise\n" + "a colormap must be set on them with " + "gdk_drawable_set_colormap"); + surface = NULL; } - - cairo_set_target_drawable(c, - GDK_DRAWABLE_XDISPLAY(real_drawable), - GDK_DRAWABLE_XID(real_drawable)); - check_cairo_status(cr); - cairo_translate(c, -x_offset, -y_offset); - check_cairo_status(cr); - return Val_unit; + if (surface != NULL) + ml_cairo_surface_set_user_data (surface, &drawable_key, ml_cairo_make_root (d)); + + return Val_cairo_surface_t (surface); } +ML_3 (cairo_xlib_surface_set_size, cairo_surface_t_val, Int_val, Int_val, Unit) + #else -Unsupported(cairo_lablgtk_surface_create_for_drawable) -Unsupported(cairo_lablgtk_set_target_drawable) +Cairo_Unsupported(cairo_xlib_surface_create, "Xlib backend not supported"); +Cairo_Unsupported(cairo_xlib_surface_set_size, "Xlib backend not supported"); #endif /* CAIRO_HAS_XLIB_SURFACE */ diff --git a/src/ml_cairo_matrix.c b/src/ml_cairo_matrix.c new file mode 100644 index 0000000..de91779 --- /dev/null +++ b/src/ml_cairo_matrix.c @@ -0,0 +1,159 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +#ifdef ARCH_ALIGN_DOUBLE +void +ml_convert_cairo_matrix_in (value v, cairo_matrix_t *mat) +{ + mat->xx = Double_field (v, 0); + mat->yx = Double_field (v, 1); + mat->xy = Double_field (v, 2); + mat->yy = Double_field (v, 3); + mat->x0 = Double_field (v, 4); + mat->y0 = Double_field (v, 5); +} + +value +ml_convert_cairo_matrix_out (cairo_matrix_t *mat) +{ + value v; + v = caml_alloc_small (6 * Double_wosize, Double_array_tag); + Store_double_field (v, 0, mat->xx); + Store_double_field (v, 1, mat->yx); + Store_double_field (v, 2, mat->xy); + Store_double_field (v, 3, mat->yy); + Store_double_field (v, 4, mat->x0); + Store_double_field (v, 5, mat->y0); + return v; +} +#endif + +/* matrix_init */ +/* matrix_init_identity */ +/* matrix_init_translate */ +/* matrix_init_scale */ +/* matrix_init_rotate */ + +CAMLprim value +ml_cairo_matrix_translate (value m, value x, value y) +{ +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam3(m, x, y); + value v = cairo_matrix_alloc(); + cairo_copy_matrix (v, m); + cairo_matrix_translate (cairo_matrix_t_val (v), Double_val (x), Double_val (y)); + CAMLreturn (v); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + cairo_matrix_translate (&mat, Double_val (x), Double_val (y)); + return ml_convert_cairo_matrix_out (&mat); +#endif +} + +CAMLprim value +ml_cairo_matrix_scale (value m, value x, value y) +{ +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam3(m, x, y); + value v = cairo_matrix_alloc(); + cairo_copy_matrix (v, m); + cairo_matrix_scale (cairo_matrix_t_val (v), Double_val (x), Double_val (y)); + CAMLreturn (v); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + cairo_matrix_scale (&mat, Double_val (x), Double_val (y)); + return ml_convert_cairo_matrix_out (&mat); +#endif +} + +CAMLprim value +ml_cairo_matrix_rotate (value m, value a) +{ +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam2(m, a); + value v = cairo_matrix_alloc(); + cairo_copy_matrix (v, m); + cairo_matrix_rotate (cairo_matrix_t_val (v), Double_val (a)); + CAMLreturn (v); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + cairo_matrix_rotate (&mat, Double_val (a); + return ml_convert_cairo_matrix_out (&mat); +#endif +} + +CAMLprim value +ml_cairo_matrix_invert (value m) +{ +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam1(m); + value v = cairo_matrix_alloc(); + cairo_copy_matrix (v, m); + cairo_matrix_invert (cairo_matrix_t_val (v)); + CAMLreturn (v); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + cairo_matrix_invert (&mat); + return ml_convert_cairo_matrix_out (&mat); +#endif +} + +CAMLprim value +ml_cairo_matrix_multiply (value a, value b) +{ +#ifndef ARCH_ALIGN_DOUBLE + CAMLparam2(a, b); + value r = cairo_matrix_alloc(); + cairo_matrix_multiply (cairo_matrix_t_val (r), + cairo_matrix_t_val (a), + cairo_matrix_t_val (b)); + CAMLreturn (r); +#else + cairo_matrix_t r, m_a, m_b; + ml_convert_cairo_matrix_in (a, &m_a); + ml_convert_cairo_matrix_in (b, &m_b); + cairo_matrix_multiply (&r, &m_a, &m_b); + return ml_convert_cairo_matrix_out (&r); +#endif +} + +CAMLprim value +ml_cairo_matrix_transform_distance (value m, value p) +{ + double x = Double_field (p, 0); + double y = Double_field (p, 1); +#ifndef ARCH_ALIGN_DOUBLE + cairo_matrix_transform_distance (cairo_matrix_t_val (m), &x, &y); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + cairo_matrix_transform_distance (&mat, &x, &y); +#endif + return ml_cairo_point (x, y); +} + +CAMLprim value +ml_cairo_matrix_transform_point (value m, value p) +{ + double x = Double_field (p, 0); + double y = Double_field (p, 1); +#ifndef ARCH_ALIGN_DOUBLE + cairo_matrix_transform_point (cairo_matrix_t_val (m), &x, &y); +#else + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + cairo_matrix_transform_point (&mat, &x, &y); +#endif + return ml_cairo_point (x, y); +} diff --git a/src/ml_cairo_path.c b/src/ml_cairo_path.c index 7c178aa..c61a0c6 100644 --- a/src/ml_cairo_path.c +++ b/src/ml_cairo_path.c @@ -6,147 +6,86 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <caml/mlvalues.h> -#include <caml/alloc.h> -#include <caml/memory.h> -#include <caml/fail.h> -#include <caml/custom.h> -#include <caml/callback.h> - -#include "ml_cairo_wrappers.h" - -#include <cairo.h> #include "ml_cairo.h" -#include "ml_cairo_status.h" - #define CAML_MOVE_TO_TAG 0x95006a53L #define CAML_LINE_TO_TAG 0x3f23e04dL #define CAML_CLOSE_TAG 0x8ca29f31L #define CAML_CURVE_TO_TAG 0x84e3bcd7L -struct caml_fold_info -{ - value closure; - value val; -}; - static value -make_point (double x, double y) +ml_cairo_fold_path (cairo_path_t *path, value f, value acc) { - value p; - p = alloc_small (2 * Double_wosize, Double_array_tag); - Store_double_field (p, 0, x); - Store_double_field (p, 1, y); - return p; -} + CAMLparam2(f, acc); + CAMLlocal5(var, t, p1, p2, p3); + int i; -static void -ml_cairo_move_to_cb (void *data, double x, double y) -{ - struct caml_fold_info *p = data; - CAMLparam0 (); - CAMLlocal2 (path_elem, point); - if (!Is_exception_result (p->val)) + for (i = 0; i < path->num_data; i += path->data[i].header.length) { - point = make_point (x, y); - path_elem = alloc_small (2, 0); - Field (path_elem, 0) = CAML_MOVE_TO_TAG; - Field (path_elem, 1) = point; - p->val = callback2_exn (p->closure, p->val, path_elem); - } - CAMLreturn0; -} + cairo_path_data_t *data = &path->data[i]; + switch (data->header.type) + { + case CAIRO_PATH_MOVE_TO: + { + p1 = ml_cairo_point (data[1].point.x, data[1].point.y); + var = caml_alloc_small (2, 0); + Field (var, 0) = CAML_MOVE_TO_TAG; + Field (var, 1) = p1; + break; + } -static void -ml_cairo_line_to_cb (void *data, double x, double y) -{ - struct caml_fold_info *p = data; - CAMLparam0 (); - CAMLlocal2 (path_elem, point); - if (!Is_exception_result (p->val)) - { - point = make_point (x, y); - path_elem = alloc_small (2, 0); - Field (path_elem, 0) = CAML_LINE_TO_TAG; - Field (path_elem, 1) = point; - p->val = callback2_exn (p->closure, p->val, path_elem); - } - CAMLreturn0; -} + case CAIRO_PATH_LINE_TO: + { + p1 = ml_cairo_point (data[1].point.x, data[1].point.y); + var = caml_alloc_small (2, 0); + Field (var, 0) = CAML_LINE_TO_TAG; + Field (var, 1) = p1; + break; + } -static void -ml_cairo_curve_to_cb (void *data, - double x1, double y1, - double x2, double y2, double x3, double y3) -{ - struct caml_fold_info *p = data; - CAMLparam0 (); - CAMLlocal5 (path_elem, tuple, point1, point2, point3); - if (!Is_exception_result (p->val)) - { - point1 = make_point (x1, y1); - point2 = make_point (x2, y2); - point3 = make_point (x3, y3); - tuple = alloc_small (3, 0); - Field (tuple, 0) = point1; - Field (tuple, 1) = point2; - Field (tuple, 2) = point3; - path_elem = alloc_small (2, 0); - Field (path_elem, 0) = CAML_CURVE_TO_TAG; - Field (path_elem, 1) = tuple; - p->val = callback2_exn (p->closure, p->val, path_elem); - } - CAMLreturn0; -} + case CAIRO_PATH_CURVE_TO: + { + p1 = ml_cairo_point (data[1].point.x, data[1].point.y); + p2 = ml_cairo_point (data[2].point.x, data[2].point.y); + p3 = ml_cairo_point (data[3].point.x, data[3].point.y); + t = caml_alloc_small (3, 0); + Field (t, 0) = p1; + Field (t, 1) = p2; + Field (t, 2) = p3; + var = caml_alloc_small (2, 0); + Field (var, 0) = CAML_CURVE_TO_TAG; + Field (var, 1) = t; + break; + } -static void -ml_cairo_close_cb (void *data) -{ - struct caml_fold_info *p = data; - if (!Is_exception_result (p->val)) - { - p->val = callback2_exn (p->closure, p->val, CAML_CLOSE_TAG); + case CAIRO_PATH_CLOSE_PATH: + var = CAML_CLOSE_TAG; + break; + } + + acc = caml_callback2_exn (f, acc, var); + if (Is_exception_result (acc)) + break; } + + cairo_path_destroy (path); + + if (Is_exception_result (acc)) + caml_raise (Extract_exception (acc)); + + CAMLreturn (acc); } CAMLprim value -ml_cairo_current_path (value v_cr, value closure, value init) +ml_cairo_copy_path (value cr, value f, value init) { - CAMLparam1 (v_cr); - CAMLlocalN (v_p, 2); - struct caml_fold_info *p = (struct caml_fold_info *) v_p; - p->closure = closure; - p->val = init; - cairo_current_path (cairo_t_val (v_cr), - ml_cairo_move_to_cb, - ml_cairo_line_to_cb, - ml_cairo_curve_to_cb, ml_cairo_close_cb, p); - check_cairo_status (v_cr); - if (Is_exception_result (p->val)) - { - value ex = Extract_exception (p->val); - mlraise (ex); - } - CAMLreturn (p->val); + return ml_cairo_fold_path (cairo_copy_path (cairo_t_val (cr)), f, init); } CAMLprim value -ml_cairo_current_path_flat (value v_cr, value closure, value init) +ml_cairo_copy_path_flat (value cr, value f, value init) { - CAMLparam1 (v_cr); - CAMLlocalN (v_p, 2); - struct caml_fold_info *p = (struct caml_fold_info *) v_p; - p->closure = closure; - p->val = init; - cairo_current_path_flat (cairo_t_val (v_cr), - ml_cairo_move_to_cb, - ml_cairo_line_to_cb, ml_cairo_close_cb, p); - check_cairo_status (v_cr); - if (Is_exception_result (p->val)) - { - value ex = Extract_exception (p->val); - mlraise (ex); - } - CAMLreturn (p->val); + return ml_cairo_fold_path (cairo_copy_path_flat (cairo_t_val (cr)), f, init); } + +/* append_path */ diff --git a/src/ml_cairo_pattern.c b/src/ml_cairo_pattern.c new file mode 100644 index 0000000..29127d0 --- /dev/null +++ b/src/ml_cairo_pattern.c @@ -0,0 +1,103 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +wMake_Val_final_pointer(cairo_pattern_t, cairo_pattern_destroy, 0) + +wML_1(cairo_pattern_create_for_surface, cairo_surface_t_val, Val_cairo_pattern_t) + +wML_4(cairo_pattern_create_linear, Double_val, Double_val, Double_val, Double_val, Val_cairo_pattern_t) + +wML_6(cairo_pattern_create_radial, Double_val, Double_val, Double_val, Double_val, Double_val, Double_val, Val_cairo_pattern_t) + +/* pattern_reference */ +/* pattern_destroy */ + +CAMLprim value +ml_cairo_pattern_add_color_stop_rgb (value p, value off, value r, value g, value b) +{ + cairo_status_t s; + s = cairo_pattern_add_color_stop_rgb (cairo_pattern_t_val (p), Double_val (off), + Double_val (r), Double_val (g), Double_val (b)); + cairo_treat_status (s); + return Val_unit; +} + +CAMLprim value +ml_cairo_pattern_add_color_stop_rgba (value p, value off, value r, value g, value b, value a) +{ + cairo_status_t s; + s = cairo_pattern_add_color_stop_rgba (cairo_pattern_t_val (p), Double_val (off), + Double_val (r), Double_val (g), Double_val (b), + Double_val (a)); + cairo_treat_status (s); + return Val_unit; +} +wML_bc6(cairo_pattern_add_color_stop_rgba) + +CAMLprim value +ml_cairo_pattern_set_matrix (value p, value m) +{ + cairo_status_t s; +#ifdef ARCH_ALIGN_DOUBLE + cairo_matrix_t mat; + ml_convert_cairo_matrix_in (m, &mat); + s = cairo_pattern_set_matrix (cairo_pattern_t_val (p), &mat); +#else + s = cairo_pattern_set_matrix (cairo_pattern_t_val (p), cairo_matrix_t_val (m)); +#endif + cairo_treat_status (s); + return Val_unit; +} + +CAMLprim value +ml_cairo_pattern_get_matrix (value p) +{ + cairo_status_t s; +#ifdef ARCH_ALIGN_DOUBLE + cairo_matrix_t mat; + s = cairo_pattern_get_matrix (cairo_pattern_t_val (p), &mat); + cairo_treat_status (s); + return ml_convert_cairo_matrix_out (m, &mat); +#else + CAMLparam1(p); + value m = caml_alloc_small (6 * Double_wosize, Double_array_tag); + s = cairo_pattern_get_matrix (cairo_pattern_t_val (p), cairo_matrix_t_val (m)); + cairo_treat_status (s); + CAMLreturn (m); +#endif +} + +#define cairo_extend_t_val(v) ((cairo_extend_t) Int_val(v)) +#define Val_cairo_extend_t(v) Val_int(v) + +CAMLprim value +ml_cairo_pattern_set_extend (value p, value e) +{ + cairo_status_t s; + s = cairo_pattern_set_extend (cairo_pattern_t_val (p), cairo_extend_t_val (e)); + cairo_treat_status (s); + return Val_unit; +} + +wML_1(cairo_pattern_get_extend, cairo_pattern_t_val, Val_cairo_extend_t) + +#define cairo_filter_t_val(v) ((cairo_filter_t) Int_val(v)) +#define Val_cairo_filter_t(v) Val_int(v) + +CAMLprim value +ml_cairo_pattern_set_filter (value p, value e) +{ + cairo_status_t s; + s = cairo_pattern_set_filter (cairo_pattern_t_val (p), cairo_filter_t_val (e)); + cairo_treat_status (s); + return Val_unit; +} + +wML_1(cairo_pattern_get_filter, cairo_pattern_t_val, Val_cairo_filter_t) diff --git a/src/ml_cairo_pdf.c b/src/ml_cairo_pdf.c new file mode 100644 index 0000000..8ab2976 --- /dev/null +++ b/src/ml_cairo_pdf.c @@ -0,0 +1,42 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +#if CAIRO_HAS_PDF_SURFACE +# include <cairo-pdf.h> + +wML_3(cairo_pdf_surface_create, String_val, Double_val, Double_val, Val_cairo_surface_t) + +CAMLprim value +ml_cairo_pdf_surface_create_for_stream (value f, value w, value h) +{ + static const cairo_user_data_key_t pdf_stream_key; + + CAMLparam3(f, w, h); + value *c; + cairo_surface_t *surf; + + c = ml_cairo_make_closure (f); + surf = cairo_pdf_surface_create_for_stream (ml_cairo_write_func, c, + Double_val (w), Double_val (h)); + + ml_cairo_surface_set_user_data (surf, &pdf_stream_key, c); + + CAMLreturn (Val_cairo_surface_t (surf)); +} + +wML_3(cairo_pdf_surface_set_dpi, cairo_surface_t_val, Double_val, Double_val, Unit) + +#else + +Cairo_Unsupported(cairo_pdf_surface_create, "PDF backend not supported"); +Cairo_Unsupported(cairo_pdf_surface_create_for_stream, "PDF backend not supported"); +Cairo_Unsupported(cairo_pdf_surface_set_dpi, "PDF backend not supported"); + +#endif diff --git a/src/ml_cairo_png.c b/src/ml_cairo_png.c new file mode 100644 index 0000000..ac0d0d0 --- /dev/null +++ b/src/ml_cairo_png.c @@ -0,0 +1,68 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +#if CAIRO_HAS_PNG_FUNCTIONS +wML_1(cairo_image_surface_create_from_png, String_val, Val_cairo_surface_t) + +CAMLprim value +ml_cairo_image_surface_create_from_png_stream (value f) +{ + CAMLparam0(); + CAMLlocal1(c); + cairo_surface_t *surf; + + c = caml_alloc_small (2, 0); + Field (c, 0) = f; + Field (c, 1) = Val_unit; + surf = cairo_image_surface_create_from_png_stream (ml_cairo_read_func, &c); + if (Is_exception_result (Field (c, 1))) + caml_raise (Extract_exception (Field (c, 1))); + + CAMLreturn (Val_cairo_surface_t (surf)); +} + +CAMLprim value +ml_cairo_surface_write_to_png (value surf, value fname) +{ + cairo_status_t s; + s = cairo_surface_write_to_png (cairo_surface_t_val (surf), String_val (fname)); + cairo_treat_status (s); + return Val_unit; +} + +CAMLprim value +ml_cairo_surface_write_to_png_stream (value surf, value f) +{ + CAMLparam1(surf); + CAMLlocal1(c); + cairo_status_t s; + + c = caml_alloc_small (2, 0); + Field (c, 0) = f; + Field (c, 1) = Val_unit; + + s = cairo_surface_write_to_png_stream (cairo_surface_t_val (surf), + ml_cairo_write_func, + &c); + if (Is_exception_result (Field (c, 1))) + caml_raise (Extract_exception (Field (c, 1))); + cairo_treat_status (s); + + CAMLreturn (Val_unit); +} + +#else + +Cairo_Unsupported(cairo_image_surface_create_from_png, "PNG functions not supported") +Cairo_Unsupported(cairo_image_surface_create_from_png_stream, "PNG functions not supported") +Cairo_Unsupported(cairo_surface_write_to_png, "PNG functions not supported") +Cairo_Unsupported(cairo_surface_write_to_png_stream, "PNG functions not supported") + +#endif diff --git a/src/ml_cairo_ps.c b/src/ml_cairo_ps.c new file mode 100644 index 0000000..1b1fe31 --- /dev/null +++ b/src/ml_cairo_ps.c @@ -0,0 +1,42 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +#if CAIRO_HAS_PS_SURFACE +# include <cairo-ps.h> + +wML_3(cairo_ps_surface_create, String_val, Double_val, Double_val, Val_cairo_surface_t) + +CAMLprim value +ml_cairo_ps_surface_create_for_stream (value f, value w, value h) +{ + static const cairo_user_data_key_t ps_stream_key; + + CAMLparam3(f, w, h); + value *c; + cairo_surface_t *surf; + + c = ml_cairo_make_closure (f); + surf = cairo_ps_surface_create_for_stream (ml_cairo_write_func, c, + Double_val (w), Double_val (h)); + + ml_cairo_surface_set_user_data (surf, &ps_stream_key, c); + + CAMLreturn (Val_cairo_surface_t (surf)); +} + +/* ML_3(cairo_ps_surface_set_dpi, cairo_surface_t_val, Double_val, Double_val, Unit) */ + +#else + +Cairo_Unsupported(cairo_ps_surface_create, "PS backend not supported"); +Cairo_Unsupported(cairo_ps_surface_create_for_stream, "PS backend not supported"); +/* Cairo_Unsupported(cairo_ps_surface_set_dpi, "PS backend not supported"); */ + +#endif diff --git a/src/ml_cairo_status.c b/src/ml_cairo_status.c index 066b20e..a869529 100644 --- a/src/ml_cairo_status.c +++ b/src/ml_cairo_status.c @@ -6,34 +6,41 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <cairo.h> -#include <caml/callback.h> -#include <caml/fail.h> - #include "ml_cairo.h" -#include "ml_cairo_status.h" -void -cairo_treat_status (cairo_status_t status) +CAMLprim value +ml_cairo_status (value v_cr) { - static value *cairo_exn; + value v; + cairo_status_t status = cairo_status (cairo_t_val (v_cr)); - if (status != CAIRO_STATUS_SUCCESS) + if (status == CAIRO_STATUS_SUCCESS) + v = Val_unit; + else { - if (cairo_exn == NULL) - { - cairo_exn = caml_named_value ("cairo_status_exn"); - if (cairo_exn == NULL) - failwith ("cairo exception"); - } - raise_with_arg (*cairo_exn, Val_int (status - 1)); + v = caml_alloc_small (1, 0); + Field (v, 0) = Val_int (status - 1); } + return v; } +wML_1(cairo_status_string, cairo_t_val, caml_copy_string) + void -check_cairo_status (value cr) +ml_cairo_treat_status (cairo_status_t status) { - struct ml_cairo *ml_c = Data_custom_val (cr); - if (!ml_c->suspend_exn) - cairo_treat_status (cairo_status (ml_c->cr)); + static value *cairo_exn; + + assert (status != CAIRO_STATUS_SUCCESS); + + if (status == CAIRO_STATUS_NO_MEMORY) + caml_raise_out_of_memory (); + + if (cairo_exn == NULL) + { + cairo_exn = caml_named_value ("cairo_status_exn"); + if (cairo_exn == NULL) + caml_failwith ("cairo exception"); + } + caml_raise_with_arg (*cairo_exn, Val_int (status - 1)); } diff --git a/src/ml_cairo_status.h b/src/ml_cairo_status.h deleted file mode 100644 index 0e65e1b..0000000 --- a/src/ml_cairo_status.h +++ /dev/null @@ -1,13 +0,0 @@ -/**************************************************************************/ -/* cairo-ocaml -- Objective Caml bindings for Cairo */ -/* Copyright © 2004-2005 Olivier Andrieu */ -/* */ -/* This code is free software and is licensed under the terms of the */ -/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ -/**************************************************************************/ - -void cairo_treat_status (cairo_status_t); -void check_cairo_status (value cr); - -#define Val_cairo_status_t(s) (cairo_treat_status(s), Val_unit) -#define report_null_pointer cairo_treat_status(CAIRO_STATUS_NULL_POINTER) diff --git a/src/ml_cairo_surface.c b/src/ml_cairo_surface.c new file mode 100644 index 0000000..34755e5 --- /dev/null +++ b/src/ml_cairo_surface.c @@ -0,0 +1,52 @@ +/**************************************************************************/ +/* cairo-ocaml -- Objective Caml bindings for Cairo */ +/* Copyright © 2004-2005 Olivier Andrieu */ +/* */ +/* This code is free software and is licensed under the terms of the */ +/* GNU Lesser General Public License version 2.1 (the "LGPL"). */ +/**************************************************************************/ + +#include "ml_cairo.h" + +wMake_Val_final_pointer(cairo_surface_t, cairo_surface_destroy, 0) + +wML_4(cairo_surface_create_similar, \ + cairo_surface_t_val, cairo_format_t_val, \ + Int_val, Int_val, Val_cairo_surface_t) + +/* surface_reference */ +/* surface_destroy */ + +CAMLprim value +ml_cairo_surface_finish (value surf) +{ + cairo_status_t s; + s = cairo_surface_finish (cairo_surface_t_val (surf)); + cairo_treat_status (s); + return Val_unit; +} + +static void ml_cairo_destroy_user_data (void *data) +{ + caml_remove_global_root (data); + caml_stat_free (data); +} + +void +ml_cairo_surface_set_user_data (cairo_surface_t *surf, const cairo_user_data_key_t *key, value *v) +{ + cairo_status_t s; + s = cairo_surface_set_user_data (surf, + key, v, + ml_cairo_destroy_user_data); + if (s != CAIRO_STATUS_SUCCESS) + { + cairo_surface_destroy (surf); + ml_cairo_destroy_user_data (v); + cairo_treat_status (s); + } +} + +/* surface_get_user_data */ + +wML_3(cairo_surface_set_device_offset, cairo_surface_t_val, Double_val, Double_val, Unit) diff --git a/src/ml_cairo_wrappers.c b/src/ml_cairo_wrappers.c index 5eb3a41..0842344 100644 --- a/src/ml_cairo_wrappers.c +++ b/src/ml_cairo_wrappers.c @@ -6,15 +6,16 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <caml/mlvalues.h> +#define CAML_NAME_SPACE #include <caml/alloc.h> + #include "ml_cairo_wrappers.h" int ml_pointer_compare (value a, value b) { - void *p1 = Pointer_val (a); - void *p2 = Pointer_val (b); + void *p1 = wPointer_val (void, a); + void *p2 = wPointer_val (void, b); if (p1 == p2) return 0; else if (p1 < p2) @@ -26,6 +27,6 @@ ml_pointer_compare (value a, value b) long ml_pointer_hash (value a) { - void *p = Pointer_val (a); + void *p = wPointer_val (void, a); return (long) p; } diff --git a/src/ml_cairo_wrappers.h b/src/ml_cairo_wrappers.h index eb03466..a824519 100644 --- a/src/ml_cairo_wrappers.h +++ b/src/ml_cairo_wrappers.h @@ -6,107 +6,145 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#define Pointer_val(val) ((void*)Field(val,1)) -#define Store_pointer(val, p) (Field(val, 1)=Val_bp(p)) +#define wPointer_val(t, val) (* ((t **) Data_custom_val(val))) int ml_pointer_compare (value, value); long ml_pointer_hash (value); -#define Make_Val_final_pointer(type, init, final, adv) \ +#define wMake_Val_final_pointer(type, final, adv) \ static void ml_final_##type (value val) \ -{ if (Field(val,1)) final ((type*)Field(val,1)); } \ +{ type **p = Data_custom_val(val); \ + if (*p) final (*p); } \ static struct custom_operations ml_custom_##type = \ -{ #type "/001", ml_final_##type, ml_pointer_compare, \ - ml_pointer_hash, custom_serialize_default, custom_deserialize_default };\ +{ #type "/001", ml_final_##type, \ + ml_pointer_compare, ml_pointer_hash, \ + custom_serialize_default, custom_deserialize_default }; \ value Val_##type (type *p) \ -{ value ret; if (!p) report_null_pointer; \ - ret = alloc_custom (&ml_custom_##type, sizeof(value), adv, 1000); \ - p = init(p); \ - Field(ret,1) = Val_bp (p); return ret; } - -static inline value Val_ptr(void *p) -{ - value v; - v = alloc_small(2, Abstract_tag); - Field(v, 1) = (value) p; - return v; -} +{ type **store; value ret; \ + if (!p) report_null_pointer(); \ + ret = caml_alloc_custom (&ml_custom_##type, sizeof p, adv, 100); \ + store = Data_custom_val(ret); \ + *store = p; return ret; } #ifndef ARCH_ALIGN_DOUBLE #define Double_array_val(v) ((double *)(v)) #endif #define Double_array_length(v) (Wosize_val(v) / Double_wosize) -#define Option_val(v,conv,def) (Is_long(v) ? def : conv(Field((v),0))) -#define StringOption_val(v) Option_val(v, String_val, NULL) - #define Ignore(x) #define Unit(x) ((x), Val_unit) -#define Id(x) (x) -#define Unsupported(fun) \ -CAMLprim value fun() { failwith("Unsupported backend"); return Val_unit; } +#define Cairo_Unsupported(fun, msg) \ +CAMLprim value ml_##fun() { caml_failwith (msg); return Val_unit; } -#define ML_0(cname, conv) \ +#define wML_0(cname, conv) \ CAMLprim value ml_##cname (value unit) { return conv (cname ()); } -#define ML_1(cname, conv1, conv) \ +#define wML_1(cname, conv1, conv) \ CAMLprim value ml_##cname (value arg1) { return conv (cname (conv1 (arg1))); } -#define ML_1_post(cname, conv1, conv, t, post) \ +#define wML_1_post(cname, conv1, conv, t, post) \ CAMLprim value ml_##cname (value arg1) \ { t ret = cname (conv1(arg1)); post; return conv(ret); } -#define ML_2(cname, conv1, conv2, conv) \ +#define wML_2(cname, conv1, conv2, conv) \ CAMLprim value ml_##cname (value arg1, value arg2) \ { return conv (cname (conv1(arg1), conv2(arg2))); } -#define ML_2_post(cname, conv1, conv2, t, conv) \ +#define wML_2_post(cname, conv1, conv2, t, conv) \ CAMLprim value ml_##cname (value arg1, value arg2) \ { t ret = cname (conv1(arg1), conv2(arg2)); post; return conv(ret); } -#define ML_3(cname, conv1, conv2, conv3, conv) \ +#define wML_3(cname, conv1, conv2, conv3, conv) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3) \ { return conv (cname (conv1(arg1), conv2(arg2), conv3(arg3))); } -#define ML_3_post(cname, conv1, conv2, conv3, conv, t, post) \ +#define wML_3_post(cname, conv1, conv2, conv3, conv, t, post) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3) \ { t ret = cname (conv1(arg1), conv2(arg2), conv3(arg3)); post; return conv(t); } -#define ML_4(cname, conv1, conv2, conv3, conv4, conv) \ +#define wML_4(cname, conv1, conv2, conv3, conv4, conv) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4) \ { return conv (cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4))); } -#define ML_4_post(cname, conv1, conv2, conv3, conv4, conv, t, post) \ +#define wML_4_post(cname, conv1, conv2, conv3, conv4, conv, t, post) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4) \ { t ret = cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4)); post; return conv(ret); } -#define ML_5(cname, conv1, conv2, conv3, conv4, conv5, conv) \ +#define wML_5(cname, conv1, conv2, conv3, conv4, conv5, conv) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4, \ value arg5) \ { return conv (cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4), \ conv5(arg5))); } -#define ML_5_post(cname, conv1, conv2, conv3, conv4, conv5, conv, t, post) \ +#define wML_5_post(cname, conv1, conv2, conv3, conv4, conv5, conv, t, post) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4, \ value arg5) \ { t ret = cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4), conv5(arg5)); \ post; return conv(ret); } -#define ML_6(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv) \ +#define wML_6(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4, \ value arg5, value arg6) \ { return conv (cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4), \ conv5(arg5), conv6(arg6))); } -#define ML_6_post(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv, t, post) \ +#define wML_6_post(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv, t, post) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4, \ value arg5, value arg6) \ { t ret = cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4), \ conv5(arg5), conv6(arg6)); post; return conv(ret); } -#define ML_7(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv7, conv) \ +#define wML_7(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv7, conv) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4, \ value arg5, value arg6, value arg7) \ { return conv (cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4), \ conv5(arg5), conv6(arg6), conv7(arg7))); } -#define ML_7_post(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv7, conv, t, post) \ +#define wML_7_post(cname, conv1, conv2, conv3, conv4, conv5, conv6, conv7, conv, t, post) \ CAMLprim value ml_##cname (value arg1, value arg2, value arg3, value arg4, \ value arg5, value arg6, value arg7) \ { t ret = cname (conv1(arg1), conv2(arg2), conv3(arg3), conv4(arg4), \ conv5(arg5), conv6(arg6), conv7(arg7)); post; return conv(ret); } -#define ML_bc6(cname) \ +#define wML_bc6(cname) \ CAMLprim value ml_##cname##_bc (value *argv, int argn) \ { return ml_##cname(argv[0],argv[1],argv[2],argv[3],argv[4],argv[5]); } -#define ML_bc7(cname) \ +#define wML_bc7(cname) \ CAMLprim value ml_##cname##_bc (value *argv, int argn) \ { return ml_##cname(argv[0],argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]); } + +#define wML_0_cairo(cname) \ +CAMLprim value ml_cairo_##cname (value v_cr) \ +{ cairo_##cname (cairo_t_val (v_cr)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} +#define wML_1_cairo(cname, conv1) \ +CAMLprim value ml_cairo_##cname (value v_cr, value arg1) \ +{ cairo_##cname (cairo_t_val (v_cr), conv1 (arg1)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} +#define wML_2_cairo(cname, conv1, conv2) \ +CAMLprim value ml_cairo_##cname (value v_cr, value arg1, value arg2) \ +{ cairo_##cname (cairo_t_val (v_cr), conv1 (arg1), conv2 (arg2)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} +#define wML_3_cairo(cname, conv1, conv2, conv3) \ +CAMLprim value ml_cairo_##cname (value v_cr, value arg1, value arg2, value arg3) \ +{ cairo_##cname (cairo_t_val (v_cr), conv1 (arg1), conv2 (arg2), conv3 (arg3)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} +#define wML_4_cairo(cname, conv1, conv2, conv3, conv4) \ +CAMLprim value ml_cairo_##cname (value v_cr, value arg1, value arg2, value arg3, value arg4) \ +{ cairo_##cname (cairo_t_val (v_cr), conv1 (arg1), conv2 (arg2), conv3 (arg3), conv4 (arg4)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} +#define wML_5_cairo(cname, conv1, conv2, conv3, conv4, conv5) \ +CAMLprim value ml_cairo_##cname (value v_cr, value arg1, value arg2, value arg3, value arg4, value arg5) \ +{ cairo_##cname (cairo_t_val (v_cr), conv1 (arg1), conv2 (arg2), conv3 (arg3), conv4 (arg4), conv5 (arg5)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} \ +CAMLprim value ml_cairo_##cname##_bc (value *argv, int argn) \ +{ return ml_cairo_##cname (argv[0],argv[1],argv[2],argv[3],argv[4],argv[5]); } +#define wML_6_cairo(cname, conv1, conv2, conv3, conv4, conv5, conv6) \ +CAMLprim value ml_cairo_##cname (value v_cr, value arg1, value arg2, value arg3, value arg4, value arg5, value arg6) \ +{ cairo_##cname (cairo_t_val (v_cr), conv1 (arg1), conv2 (arg2), conv3 (arg3), conv4 (arg4), conv5 (arg5), conv6 (arg6)); \ + check_cairo_status (v_cr); \ + return Val_unit; \ +} \ +CAMLprim value ml_cairo_##cname##_bc (value *argv, int argn) \ +{ return ml_cairo_##cname (argv[0],argv[1],argv[2],argv[3],argv[4],argv[5], argv[6]); } + diff --git a/src/ml_svg_cairo.c b/src/ml_svg_cairo.c index 525c0b0..5851cac 100644 --- a/src/ml_svg_cairo.c +++ b/src/ml_svg_cairo.c @@ -6,35 +6,34 @@ /* GNU Lesser General Public License version 2.1 (the "LGPL"). */ /**************************************************************************/ -#include <caml/alloc.h> -#include <caml/memory.h> -#include <caml/fail.h> -#include <caml/callback.h> -#include <caml/custom.h> +#define CAML_NAME_SPACE + +#include "ml_cairo.h" #include <svg-cairo.h> -#define report_null_pointer failwith("null pointer") -#include "ml_cairo_wrappers.h" -#include "ml_cairo.h" +static value ml_svg_cairo_status (svg_cairo_status_t) Noreturn; static value ml_svg_cairo_status (svg_cairo_status_t s) { static value *exn; - if (s == SVG_CAIRO_STATUS_SUCCESS) - return Val_unit; + assert (s != SVG_CAIRO_STATUS_SUCCESS); + if (exn == NULL) { exn = caml_named_value ("svg_cairo_status_exn"); if (exn == NULL) - failwith ("Svg_cairo exception not registered"); + caml_failwith ("svg-cairo exception"); } - raise_with_arg (*exn, Val_int (s - 1)); + + caml_raise_with_arg (*exn, Val_int (s - 1)); } -Make_Val_final_pointer (svg_cairo_t, Id, svg_cairo_destroy, 100) -#define svg_cairo_t_val(v) (svg_cairo_t *)Pointer_val(v) +#define check_svg_cairo_status(s) if (s != SVG_CAIRO_STATUS_SUCCESS) ml_svg_cairo_status (s) + +wMake_Val_final_pointer (svg_cairo_t, svg_cairo_destroy, 0) +#define svg_cairo_t_val(v) wPointer_val(svg_cairo_t, v) CAMLprim value ml_svg_cairo_create (value unit) @@ -42,32 +41,80 @@ ml_svg_cairo_create (value unit) svg_cairo_status_t status; svg_cairo_t *s; status = svg_cairo_create (&s); - ml_svg_cairo_status (status); + check_svg_cairo_status (status); return Val_svg_cairo_t (s); } -ML_2 (svg_cairo_parse, svg_cairo_t_val, String_val, ml_svg_cairo_status) +CAMLprim value +ml_svg_cairo_parse (value v, value f) +{ + svg_cairo_status_t status; + status = svg_cairo_parse (svg_cairo_t_val (v), String_val (f)); + check_svg_cairo_status (status); + return Val_unit; +} CAMLprim value -ml_svg_cairo_parse_buffer (value s, value b) +ml_svg_cairo_parse_buffer (value v, value b) { - return ml_svg_cairo_status (svg_cairo_parse_buffer (svg_cairo_t_val (s), - String_val (b), - string_length (b))); + svg_cairo_status_t status; + status = svg_cairo_parse_buffer (svg_cairo_t_val (v), + String_val (b), + caml_string_length (b)); + check_svg_cairo_status (status); + return Val_unit; } -ML_1 (svg_cairo_parse_chunk_begin, svg_cairo_t_val, ml_svg_cairo_status) CAMLprim value -ml_svg_cairo_parse_chunk (value s, value b) +ml_svg_cairo_parse_chunk_begin (value v) { - return ml_svg_cairo_status (svg_cairo_parse_chunk (svg_cairo_t_val (s), - String_val (b), - string_length (b))); + svg_cairo_status_t status; + status = svg_cairo_parse_chunk_begin (svg_cairo_t_val (v)); + check_svg_cairo_status (status); + return Val_unit; } -ML_1 (svg_cairo_parse_chunk_end, svg_cairo_t_val, ml_svg_cairo_status) -ML_2 (svg_cairo_render, svg_cairo_t_val, cairo_t_val, ml_svg_cairo_status) -ML_3 (svg_cairo_set_viewport_dimension, svg_cairo_t_val, Unsigned_int_val, Unsigned_int_val, ml_svg_cairo_status) +CAMLprim value +ml_svg_cairo_parse_chunk (value v, value b, value o, value l) +{ + svg_cairo_status_t status; + if (Unsigned_int_val (o) + Unsigned_int_val (l) > caml_string_length (b)) + caml_invalid_argument ("Svg_cairo.parse_chunk: invalid substring"); + status = svg_cairo_parse_chunk (svg_cairo_t_val (v), + String_val (b) + Unsigned_int_val (o), + Unsigned_int_val (l)); + check_svg_cairo_status (status); + return Val_unit; +} + +CAMLprim value +ml_svg_cairo_parse_chunk_end (value v) +{ + svg_cairo_status_t status; + status = svg_cairo_parse_chunk_end (svg_cairo_t_val (v)); + check_svg_cairo_status (status); + return Val_unit; +} + +CAMLprim value +ml_svg_cairo_render (value v, value cr) +{ + svg_cairo_status_t status; + status = svg_cairo_render (svg_cairo_t_val (v), cairo_t_val (cr)); + check_svg_cairo_status (status); + return Val_unit; +} + +CAMLprim value +ml_svg_cairo_set_viewport_dimension (value v, value w, value h) +{ + svg_cairo_status_t status; + status = svg_cairo_set_viewport_dimension (svg_cairo_t_val (v), + Unsigned_int_val (w), + Unsigned_int_val (h)); + check_svg_cairo_status (status); + return Val_unit; +} CAMLprim value ml_svg_cairo_get_size (value s) @@ -75,7 +122,7 @@ ml_svg_cairo_get_size (value s) int w, h; value r; svg_cairo_get_size (svg_cairo_t_val (s), &w, &h); - r = alloc_small (2, 0); + r = caml_alloc_small (2, 0); Field (r, 0) = Val_int (w); Field (r, 1) = Val_int (h); return r; diff --git a/src/svg_cairo.ml b/src/svg_cairo.ml index e7a4b4d..84b92c1 100644 --- a/src/svg_cairo.ml +++ b/src/svg_cairo.ml @@ -14,7 +14,7 @@ type status = | INVALID_CALL | PARSE_ERROR exception Error of status -let init = Callback.register "svg_cairo_status_exn" (Error NO_MEMORY) +let init = Callback.register_exception "svg_cairo_status_exn" (Error NO_MEMORY) type t @@ -24,7 +24,7 @@ external parse : t -> string -> unit = "ml_svg_cairo_parse" external parse_string : t -> string -> unit = "ml_svg_cairo_parse_buffer" external parse_chunk_begin : t -> unit = "ml_svg_cairo_parse_chunk_begin" -external parse_chunk : t -> string -> unit = "ml_svg_cairo_parse_chunk" +external parse_chunk : t -> string -> int -> int -> unit = "ml_svg_cairo_parse_chunk" external parse_chunk_end : t -> unit = "ml_svg_cairo_parse_chunk_end" external render : t -> Cairo.t -> unit = "ml_svg_cairo_render" diff --git a/src/svg_cairo.mli b/src/svg_cairo.mli index 80de619..3814cf9 100644 --- a/src/svg_cairo.mli +++ b/src/svg_cairo.mli @@ -6,6 +6,9 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) +(** Rendering SVG documents with + cairo *) + type status = NO_MEMORY | IO_ERROR @@ -20,13 +23,17 @@ type t external create : unit -> t = "ml_svg_cairo_create" +(** {3 Parsing} *) + external parse : t -> string -> unit = "ml_svg_cairo_parse" external parse_string : t -> string -> unit = "ml_svg_cairo_parse_buffer" external parse_chunk_begin : t -> unit = "ml_svg_cairo_parse_chunk_begin" -external parse_chunk : t -> string -> unit = "ml_svg_cairo_parse_chunk" +external parse_chunk : t -> string -> int -> int -> unit = "ml_svg_cairo_parse_chunk" external parse_chunk_end : t -> unit = "ml_svg_cairo_parse_chunk_end" +(** {3 Rendering} *) + external render : t -> Cairo.t -> unit = "ml_svg_cairo_render" external set_viewport_dimenstion : t -> int -> int -> unit = "ml_svg_cairo_set_viewport_dimension" diff --git a/test/.cvsignore b/test/.cvsignore new file mode 100644 index 0000000..888177d --- /dev/null +++ b/test/.cvsignore @@ -0,0 +1,13 @@ +*.pdf +*.png +*.ps +*.ppm +*.svg +basket +demo +font +kapow +knockout +spline +svg2png +text diff --git a/test/Makefile b/test/Makefile index 1ab9053..d570701 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,12 +1,12 @@ include ../config.make -TARGETS = kapow +TARGETS = basket kapow ifdef LABLGTKDIR -TARGETS += text demo spline basket knockout font -# ifdef GTKCAIRO_CFLAGS -# TARGETS += cube -# endif +TARGETS += text demo spline knockout # font +ifdef GTKCAIRO_CFLAGS +TARGETS += cube +endif endif ifdef LIBSVG_CAIRO_CFLAGS TARGETS += svg2png @@ -36,10 +36,13 @@ spline : spline.ml $(OCAMLOPT) -w s -o $@ -I ../src -I $(LABLGTKDIR) lablgtk.cmxa cairo.cmxa cairo_lablgtk.cmxa gtkInit.cmx $^ basket : basket.ml - $(OCAMLOPT) -o $@ -I ../src -I $(LABLGTKDIR) bigarray.cmxa cairo.cmxa lablgtk.cmxa cairo_lablgtk.cmxa gtkInit.cmx $^ + $(OCAMLOPT) -o $@ -I ../src -I $(LABLGTKDIR) bigarray.cmxa cairo.cmxa $^ + +basket.b : basket.ml + $(OCAMLC) -g -o $@ -I ../src -I $(LABLGTKDIR) bigarray.cma cairo.cma $^ knockout : knockout.ml - $(OCAMLOPT) -w s -o $@ -I ../src -I $(LABLGTKDIR) cairo.cmxa lablgtk.cmxa cairo_lablgtk.cmxa gtkInit.cmx $^ + $(OCAMLOPT) -o $@ -I ../src -I $(LABLGTKDIR) cairo.cmxa lablgtk.cmxa cairo_lablgtk.cmxa gtkInit.cmx $^ clean : rm -f *.cm* *.o $(TARGETS) *.ps *.ppm *.png diff --git a/test/basket.ml b/test/basket.ml index e38ced3..6c5a63f 100644 --- a/test/basket.ml +++ b/test/basket.ml @@ -6,90 +6,88 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) +type point = Cairo.point = { x : float ; y : float } + let _ = Cairo.init let print_path_elem = function - | `MOVE_TO { Cairo.x = x ; Cairo.y = y } -> - Format.printf "@ move_to (%f, %f)" x y - | `LINE_TO { Cairo.x = x ; Cairo.y = y } -> - Format.printf "@ line_to (%f, %f)" x y - | `CURVE_TO ({ Cairo.x = x1 ; Cairo.y = y1 }, - { Cairo.x = x2 ; Cairo.y = y2 }, - { Cairo.x = x3 ; Cairo.y = y3 }) -> - Format.printf "@ curve_to (%f, %f, %f, %f, %f, %f)" x1 y1 x2 y2 x3 y3 + | `MOVE_TO p -> + Format.printf "@ move_to (%f, %f)" p.x p.y + | `LINE_TO p -> + Format.printf "@ line_to (%f, %f)" p.x p.y + | `CURVE_TO (p1, p2, p3) -> + Format.printf "@ curve_to (%f, %f, %f, %f, %f, %f)" p1.x p1.y p2.x p2.y p3.x p3.y | `CLOSE -> Format.printf "@ close\n" let print_path c = Format.printf "@[<v 2>current_path:" ; - let nb = Cairo.fold_current_path c + let nb = Cairo.fold_path c (fun nb el -> print_path_elem el ; nb+1) 0 in Format.printf "@]%d elements@." nb let draw ?(print=false) c = - Cairo.move_to c 10. 10. ; - Cairo.line_to c 510. 10. ; - Cairo.curve_to c 410. 200. 110. 200. 10. 10. ; + Cairo.move_to c 50. 50. ; + Cairo.line_to c 550. 50. ; + Cairo.curve_to c 450. 240. 150. 240. 50. 50. ; Cairo.close_path c ; if print then print_path c ; Cairo.save c ; begin - Cairo.set_rgb_color c 0.8 0.1 0.1 ; - Cairo.fill c end ; + Cairo.set_source_rgb c 0.8 0.1 0.1 ; + Cairo.fill_preserve c end ; Cairo.restore c ; Cairo.set_line_width c 6. ; - Cairo.set_rgb_color c 0. 0. 0. ; + Cairo.set_source_rgb c 0. 0. 0. ; Cairo.stroke c -let width = 520. -let height = 170. -let x_inches = width /. 96. -let y_inches = height /. 96. -let x_ppi = 300. -let y_ppi = 300. +let x_inches = 8. +let y_inches = 3. -let main = - let c = Cairo.create () in +let main () = - prerr_endline "PS" ; begin - let file = Cairo_channel.open_out "basket.ps" in - Cairo.set_target_ps c file x_inches y_inches x_ppi y_ppi ; + prerr_endline "PS" ; + let s = Cairo_ps.surface_create "basket.ps" (x_inches *. 72.) (y_inches *. 72.) in + let c = Cairo.create s in draw ~print:true c ; Cairo.show_page c ; - Cairo.finalise_target c ; - Cairo_channel.close file + Cairo.surface_finish s end ; - prerr_endline "PDF" ; begin - let file = Cairo_channel.open_out "basket.pdf" in - Cairo.set_target_pdf c file x_inches y_inches x_ppi y_ppi ; + prerr_endline "PDF" ; + let s = Cairo_pdf.surface_create "basket.pdf" (x_inches *. 72.) (y_inches *. 72.) in + let c = Cairo.create s in draw c ; Cairo.show_page c ; - Cairo.finalise_target c ; - Cairo_channel.close file + Cairo.surface_finish s end ; - prerr_endline "Bigarray and PPM" ; begin - let arr = Bigarray.Array2.create Bigarray.int Bigarray.c_layout - (int_of_float height) (int_of_float width) in + prerr_endline "Bigarray, PPM and PNG" ; + let arr = + Bigarray.Array2.create Bigarray.int Bigarray.c_layout + (int_of_float x_inches * 72) (int_of_float y_inches * 72) in Bigarray.Array2.fill arr 0xffffff ; - let img = Cairo_bigarray.of_bigarr_24 arr in - Cairo.set_target_image c img ; + let s = Cairo_bigarray.of_bigarr_24 arr in + let c = Cairo.create s in draw c ; - let oc = open_out "basket.ppm" in - Cairo_bigarray.write_ppm_int oc arr ; - close_out oc - end ; + begin + let oc = open_out "basket.ppm" in + Cairo_bigarray.write_ppm_int oc arr ; + close_out oc + end ; + Cairo_png.surface_write_to_file s "basket.png" + end - prerr_endline "GdkPixbuf and PNG" ; +(* begin + prerr_endline "GdkPixbuf and PNG" ; let pb = GdkPixbuf.create ~width:(int_of_float width) ~height:(int_of_float height) ~bits:8 ~has_alpha:true () in @@ -100,3 +98,9 @@ let main = Cairo_lablgtk.shuffle_pixels pb ; GdkPixbuf.save ~filename:"basket.png" ~typ:"png" pb end +*) + +let () = + try main () + with Cairo.Error s -> + Printf.eprintf "Fatal error: cairo exception: '%d'\n" (Obj.magic s) diff --git a/test/demo.ml b/test/demo.ml index 515075c..f04e982 100644 --- a/test/demo.ml +++ b/test/demo.ml @@ -64,12 +64,11 @@ let redraw (px : GDraw.pixmap) = let width, height = px#size in px#rectangle ~x:0 ~y:0 ~width ~height ~filled:true () end ; - let cr = Cairo.create () in - Cairo_lablgtk.set_target_drawable cr px#pixmap ; - Cairo.set_rgb_color cr 1. 1. 1. ; + let cr = Cairo.create (Cairo_lablgtk.surface_create px#pixmap) in + Cairo.set_source_rgb cr 1. 1. 1. ; Cairo.save cr ; begin - Cairo.scale_font cr 20. ; + Cairo.set_font_size cr 20. ; Cairo.move_to cr 10. 10. ; Cairo.rotate cr (pi /. 2.) ; Cairo.show_text cr "Hello World !" end ; @@ -101,7 +100,7 @@ let redraw (px : GDraw.pixmap) = Cairo.set_line_join cr Cairo.LINE_JOIN_BEVEL ; draw_shapes cr 0. 0. true ; - Cairo.set_rgb_color cr 1. 0. 0. ; + Cairo.set_source_rgb cr 1. 0. 0. ; draw_shapes cr 0. 0. false diff --git a/test/kapow.ml b/test/kapow.ml index 9273124..9a2d082 100644 --- a/test/kapow.ml +++ b/test/kapow.ml @@ -63,7 +63,7 @@ let make_text_path cr x y text = Cairo.move_to cr x y ; Cairo.text_path cr text ; ignore - (Cairo.fold_current_path_flat cr + (Cairo.fold_path_flat cr (fun first -> function | `MOVE_TO p -> if first then Cairo.new_path cr ; @@ -75,57 +75,53 @@ let make_text_path cr x y text = true) let draw text = - let file = Cairo_channel.open_out filename in - let cr = Cairo.create () in - Cairo.set_target_png cr file Cairo.FORMAT_ARGB32 (int_of_float width) (int_of_float height) ; + let cr = + Cairo.create + (Cairo.image_surface_create + Cairo.FORMAT_ARGB32 + (int_of_float width) (int_of_float height))in Cairo.set_line_width cr 2. ; Cairo.save cr ; begin Cairo.translate cr shadow_offset shadow_offset ; make_star_path cr ; - Cairo.set_alpha cr 0.5 ; - Cairo.set_rgb_color cr 0. 0. 0. ; - Cairo.fill cr ; end ; + Cairo.set_source_rgba cr 0. 0. 0. 0.5 ; + Cairo.fill cr end ; Cairo.restore cr ; make_star_path cr ; - Cairo.set_alpha cr 1. ; - - let pattern = Cairo.pattern_create_radial + let pattern = Cairo.Pattern.create_radial (width /. 2.) (height /. 2.) 10. (width /. 2.) (height /. 2.) 230. in - Cairo.pattern_add_color_stop pattern 0. 1. 1. 0.2 1. ; - Cairo.pattern_add_color_stop pattern 1. 1. 0. 0. 1. ; - Cairo.set_pattern cr pattern ; + Cairo.Pattern.add_color_stop_rgba pattern 0. 1. 1. 0.2 1. ; + Cairo.Pattern.add_color_stop_rgba pattern 1. 1. 0. 0. 1. ; + Cairo.set_source cr pattern ; Cairo.fill cr ; make_star_path cr ; - Cairo.set_rgb_color cr 0. 0. 0. ; + Cairo.set_source_rgb cr 0. 0. 0. ; Cairo.stroke cr ; - Cairo.select_font cr fontname Cairo.FONT_SLANT_NORMAL Cairo.FONT_WEIGHT_BOLD ; - Cairo.scale_font cr 50. ; + Cairo.select_font_face cr fontname Cairo.FONT_SLANT_NORMAL Cairo.FONT_WEIGHT_BOLD ; + Cairo.set_font_size cr 50. ; let extents = Cairo.text_extents cr text in let x = width /. 2. -. (extents.Cairo.text_width /. 2. +. extents.Cairo.x_bearing) in let y = height /. 2. -. (extents.Cairo.text_height /. 2. +. extents.Cairo.y_bearing) in make_text_path cr x y text ; - let pattern = Cairo.pattern_create_linear + let pattern = Cairo.Pattern.create_linear (width /. 2. -. 10.) (height /. 4.) (width /. 2. +. 10.) (3. *. height /. 4.) in - Cairo.pattern_add_color_stop pattern 0. 1. 1. 1. 1. ; - Cairo.pattern_add_color_stop pattern 1. 0. 0. 0.4 1. ; - Cairo.set_pattern cr pattern ; + Cairo.Pattern.add_color_stop_rgba pattern 0. 1. 1. 1. 1. ; + Cairo.Pattern.add_color_stop_rgba pattern 1. 0. 0. 0.4 1. ; + Cairo.set_source cr pattern ; Cairo.fill cr ; make_text_path cr x y text ; - Cairo.set_rgb_color cr 0. 0. 0. ; + Cairo.set_source_rgb cr 0. 0. 0. ; Cairo.stroke cr ; - Cairo.show_page cr ; - Cairo.finalise_target cr ; - - Cairo_channel.close file + Cairo_png.surface_write_to_file (Cairo.get_target cr) filename let _ = draw diff --git a/test/knockout.ml b/test/knockout.ml index dc3f04d..74fff24 100644 --- a/test/knockout.ml +++ b/test/knockout.ml @@ -6,137 +6,115 @@ (* GNU Lesser General Public License version 2.1 (the "LGPL"). *) (**************************************************************************) -let rect_path cr x y width height = - Cairo.new_path cr ; - Cairo.move_to cr x y ; - Cairo.rel_line_to cr 0. height ; - Cairo.rel_line_to cr width 0. ; - Cairo.rel_line_to cr 0. (~-. height) ; - Cairo.rel_line_to cr (~-. width) 0. ; - Cairo.close_path cr - - - let pi = 4. *. atan 1. let oval_path cr xc yc xr yr = - Cairo.new_path cr ; - Cairo.move_to cr (xc +. xr) yc ; - - let tangent_mult = 1.65591 /. 3. in - for i=0 to 3 do - let angle1 = (float i /. 2.) *. pi in - let angle2 = (float (i + 1) /. 2.) *. pi in - - let x0 = xc +. xr *. cos angle1 in - let y0 = yc -. yr *. sin angle1 in - let x1 = x0 -. xr *. sin angle1 *. tangent_mult in - let y1 = y0 -. yr *. cos angle1 *. tangent_mult in - let x3 = xc +. xr *. cos angle2 in - let y3 = yc -. yr *. sin angle2 in - let x2 = x3 +. xr *. sin angle2 *. tangent_mult in - let y2 = y3 +. yr *. cos angle2 *. tangent_mult in - - Cairo.curve_to ~cr ~x1 ~y1 ~x2 ~y2 ~x3 ~y3 - done ; - Cairo.close_path cr + let m = Cairo.get_matrix cr in + Cairo.translate cr xc yc ; + Cairo.scale cr 1. (yr /. xr) ; + Cairo.move_to cr xr 0. ; + Cairo.arc cr 0. 0. xr 0. (2. *. pi) ; + Cairo.close_path cr ; + Cairo.set_matrix cr m let check_size = 32 -let fill_checks c width height = +let fill_checks c x y width height = Cairo.save c ; begin - let check = Cairo.surface_create_similar - (Cairo.current_target_surface c) + let check = + Cairo.surface_create_similar + (Cairo.get_target c) Cairo.FORMAT_RGB24 (2 * check_size) (2 * check_size) in - Cairo.surface_set_repeat check true ; - Cairo.save c ; begin + begin let f_size = float check_size in - Cairo.set_target_surface c check ; - Cairo.set_operator c Cairo.OPERATOR_SRC ; - Cairo.set_rgb_color c 0.4 0.4 0.4 ; - rect_path c 0. 0. (2. *. f_size) (2. *. f_size) ; - - Cairo.set_rgb_color c 0.7 0.7 0.7 ; - rect_path c 0. 0. f_size f_size ; - Cairo.fill c ; - rect_path c f_size f_size f_size f_size ; - Cairo.fill c end ; - Cairo.restore c ; - - Cairo.set_pattern c (Cairo.pattern_create_for_surface check) ; - rect_path c 0. 0. (float width) (float height) ; + let cr2 = Cairo.create check in + Cairo.set_operator cr2 Cairo.OPERATOR_SOURCE ; + Cairo.set_source_rgb cr2 0.4 0.4 0.4 ; + Cairo.rectangle cr2 0. 0. (2. *. f_size) (2. *. f_size) ; + Cairo.fill cr2 ; + + Cairo.set_source_rgb cr2 0.7 0.7 0.7 ; + Cairo.rectangle cr2 x y f_size f_size ; + Cairo.fill cr2 ; + Cairo.rectangle cr2 (x +. f_size) (y +. f_size) f_size f_size ; + Cairo.fill cr2 + end ; + + let pattern = Cairo.Pattern.create_for_surface check in + Cairo.Pattern.set_extend pattern Cairo.EXTEND_REPEAT ; + Cairo.set_source c pattern ; + Cairo.rectangle c 0. 0. (float width) (float height) ; Cairo.fill c end ; Cairo.restore c -let draw_3circles c xc yc radius = +let draw_3circles c xc yc radius alpha = let subradius = radius *. (2. /. 3. -. 0.1) in - List.iter (fun ((r, g, b), off) -> - Cairo.set_rgb_color c r g b ; + List.iter (fun (r, g, b, off) -> + Cairo.set_source_rgba c r g b alpha ; oval_path c (xc +. radius /. 3. *. cos (pi *. (0.5 +. off))) (yc -. radius /. 3. *. sin (pi *. (0.5 +. off))) subradius subradius ; Cairo.fill c) - [ (1., 0., 0.), 0. ; - (0., 1., 0.), 2./.3. ; - (0., 0., 1.), 4./.3. ; ] + [ 1., 0., 0., 0. ; + 0., 1., 0., 2./.3. ; + 0., 0., 1., 4./.3. ; ] -let expose c d_area ev = - let { Gtk.width = width ; - Gtk.height = height } = d_area#misc#allocation in - let drawable = d_area#misc#window in - +let draw c width height = let radius = 0.5 *. float (min width height) -. 10. in let xc = float width /. 2. in let yc = float height /. 2. in - Cairo_lablgtk.set_target_drawable c drawable ; - let sur = Cairo.current_target_surface c in - + let sur = Cairo.get_target c in let overlay = Cairo.surface_create_similar sur Cairo.FORMAT_ARGB32 width height in let punch = Cairo.surface_create_similar sur Cairo.FORMAT_A8 width height in let circles = Cairo.surface_create_similar sur Cairo.FORMAT_ARGB32 width height in - fill_checks c width height ; - - Cairo.save c ; begin - Cairo.set_target_surface c overlay ; - Cairo.set_rgb_color c 0. 0. 0. ; - oval_path c xc yc radius radius ; - Cairo.fill c ; - Cairo.save c ; begin - Cairo.set_target_surface c punch ; - draw_3circles c xc yc radius end ; - Cairo.restore c ; - Cairo.set_operator c Cairo.OPERATOR_OUT_REVERSE ; - Cairo.show_surface c punch width height ; - Cairo.save c ; begin - Cairo.set_target_surface c circles ; - Cairo.set_alpha c 0.5 ; - Cairo.set_operator c Cairo.OPERATOR_OVER ; - draw_3circles c xc yc radius end ; - Cairo.restore c ; - Cairo.set_operator c Cairo.OPERATOR_ADD ; - Cairo.show_surface c circles width height end ; - Cairo.restore c ; - Cairo.show_surface c overlay width height ; + fill_checks c 0. 0. width height ; + + begin + let cr_o = Cairo.create overlay in + Cairo.set_source_rgb cr_o 0. 0. 0. ; + oval_path cr_o xc yc radius radius ; + Cairo.fill cr_o ; + begin + let cr_p = Cairo.create punch in + draw_3circles cr_p xc yc radius 1. + end ; + Cairo.set_operator cr_o Cairo.OPERATOR_DEST_OUT ; + Cairo.set_source_surface cr_o punch 0. 0. ; + Cairo.paint cr_o ; + begin + let cr_c = Cairo.create circles in + Cairo.set_operator cr_c Cairo.OPERATOR_OVER ; + draw_3circles cr_c xc yc radius 0.5 + end ; + Cairo.set_operator cr_o Cairo.OPERATOR_ADD ; + Cairo.set_source_surface cr_o circles 0. 0.; + Cairo.paint cr_o + end ; + Cairo.set_source_surface c overlay 0. 0. ; + Cairo.paint c + +let expose d_area ev = + let c = Cairo.create (Cairo_lablgtk.surface_create d_area#misc#window) in + let allocation = d_area#misc#allocation in + draw c allocation.Gtk.width allocation.Gtk.height ; true - let main () = let w = GWindow.window ~title:"Knockout Groups" ~width:400 ~height:400 () in - w#connect#destroy GMain.quit ; - - let c = Cairo.create () in + ignore (w#connect#destroy GMain.quit) ; let d = GMisc.drawing_area ~packing:w#add () in - d#event#connect#expose (expose c d) ; + d#misc#set_double_buffered false ; + ignore (d#event#connect#expose (expose d)) ; w#show () ; GMain.main () diff --git a/test/spline.ml b/test/spline.ml index f41ad17..8f3fcd1 100644 --- a/test/spline.ml +++ b/test/spline.ml @@ -7,8 +7,8 @@ (**************************************************************************) type point = Cairo.point = - { mutable x : float ; - mutable y : float } + { x : float ; + y : float } type spl = { mutable pm : GDraw.pixmap ; @@ -20,7 +20,7 @@ type spl = { mutable xtrans : float ; mutable ytrans : float ; mutable click : bool ; - drag_pt : point ; + mutable drag_pt : point ; mutable active : int ; mutable width : int ; mutable height : int ; @@ -68,7 +68,7 @@ let init_spl () = let draw_control_line cr a b w = Cairo.save cr ; begin - Cairo.set_rgb_color cr 0. 0. 1. ; + Cairo.set_source_rgb cr 0. 0. 1. ; Cairo.set_line_width cr w ; Cairo.move_to cr a.x a.y ; Cairo.line_to cr b.x b.y ; @@ -79,7 +79,7 @@ let two_pi = 8. *. atan 1. let draw_spline cr spl = let drag_pt = { x = spl.drag_pt.x ; y = spl.drag_pt.y } in - Cairo.inverse_transform_point cr drag_pt ; + let drag_pt = Cairo.device_to_user cr drag_pt in Cairo.save cr ; begin Cairo.move_to cr spl.pt.(0).x spl.pt.(0).y ; Cairo.curve_to cr @@ -87,7 +87,7 @@ let draw_spline cr spl = spl.pt.(2).x spl.pt.(2).y spl.pt.(3).x spl.pt.(3).y ; - if spl.click && Cairo.in_stroke cr drag_pt.x drag_pt.y + if spl.click && Cairo.in_stroke cr drag_pt then spl.active <- 0xf ; Cairo.stroke cr ; @@ -97,14 +97,13 @@ let draw_spline cr spl = for i=0 to 3 do Cairo.save cr ; begin - Cairo.set_rgb_color cr 1. 0. 0. ; - Cairo.set_alpha cr 0.5 ; + Cairo.set_source_rgba cr 1. 0. 0. 0.5 ; Cairo.new_path cr ; Cairo.arc cr spl.pt.(i).x spl.pt.(i).y (spl.line_width /. 1.25) 0. two_pi ; - if spl.click && Cairo.in_fill cr drag_pt.x drag_pt.y + if spl.click && Cairo.in_fill cr drag_pt then begin spl.active <- 1 lsl i ; spl.click <- false @@ -116,10 +115,10 @@ let draw_spline cr spl = let paint spl = - let cr = Cairo_lablgtk.create ~target:spl.pm#pixmap () in + let cr = Cairo.create (Cairo_lablgtk.surface_create spl.pm#pixmap) in spl.pm#rectangle ~x:0 ~y:0 ~width:spl.width ~height:spl.height ~filled:true () ; - Cairo.set_rgb_color cr 0. 0. 0. ; + Cairo.set_source_rgb cr 0. 0. 0. ; Cairo.set_line_width cr spl.line_width ; Cairo.set_line_cap cr spl.line_cap ; Cairo.translate cr spl.xtrans spl.ytrans ; @@ -250,8 +249,7 @@ let button_ev da spl ev = match GdkEvent.get_type ev with | `BUTTON_PRESS -> spl.click <- true ; - spl.drag_pt.x <- GdkEvent.Button.x ev ; - spl.drag_pt.y <- GdkEvent.Button.y ev ; + spl.drag_pt <- { x = GdkEvent.Button.x ev ; y = GdkEvent.Button.y ev } ; true | `BUTTON_RELEASE -> spl.click <- false ; @@ -265,12 +263,12 @@ let motion_notify_cb da spl ev = for i=0 to 3 do if (1 lsl i) land spl.active != 0 then begin - spl.pt.(i).x <- spl.pt.(i).x +. (x -. spl.drag_pt.x) /. spl.zoom ; - spl.pt.(i).y <- spl.pt.(i).y +. (y -. spl.drag_pt.y) /. spl.zoom + let x = spl.pt.(i).x +. (x -. spl.drag_pt.x) /. spl.zoom in + let y = spl.pt.(i).y +. (y -. spl.drag_pt.y) /. spl.zoom in + spl.pt.(i) <- { x = x ; y = y } end done ; - spl.drag_pt.x <- x ; - spl.drag_pt.y <- y ; + spl.drag_pt <- { x = x ; y = y } ; refresh da spl ; true diff --git a/test/svg2png.ml b/test/svg2png.ml index 6693f21..b41e5ad 100644 --- a/test/svg2png.ml +++ b/test/svg2png.ml @@ -49,37 +49,49 @@ let parse_args () = scale = !scale ; width = !width ; height = !height } let render_to_png args = - let cr = Cairo.create () in let svgc = Svg_cairo.create () in Svg_cairo.parse svgc args.svg_file ; let svg_width, svg_height = Svg_cairo.get_size svgc in - let scale, width, height = - if args.width < 0 && args.height < 0 then - let width = float svg_width *. args.scale +. 0.5 in - let height = float svg_height *. args.scale +. 0.5 in - args.scale, int_of_float width, int_of_float height - else if args.width < 0 then - let scale = float args.height /. float svg_height in - let width = float svg_width *. scale +. 0.5 in - scale, int_of_float width, args.height - else if args.height < 0 then - let scale = float args.width /. float svg_width in - let height = float svg_height *. scale +. 0.5 in - scale, args.width, int_of_float height - else - let scale = min (float args.height /. float svg_height) (float args.width /. float svg_width) in - let dx = (float args.width -. (float svg_width *. scale +. 0.5)) /. 2. in - let dy = (float args.height -. (float svg_height *. scale +. 0.5)) /. 2. in - Cairo.translate cr dx dy ; - scale, args.width, args.height in - - Cairo.scale cr scale scale ; - let chan = Cairo_channel.open_out args.png_file in - Cairo.set_target_png cr chan Cairo.FORMAT_ARGB32 width height ; - Cairo.set_rgb_color cr 1. 1. 1. ; + + let scale = ref args.scale in + let width = ref args.width in + let height = ref args.height in + let dx = ref 0. in + let dy = ref 0. in + + begin + if args.width < 0 && args.height < 0 then begin + width := int_of_float (float svg_width *. args.scale +. 0.5) ; + height := int_of_float (float svg_height *. args.scale +. 0.5) + end + else if args.width < 0 then begin + scale := float args.height /. float svg_height ; + width := int_of_float (float svg_width *. args.scale +. 0.5) ; + end + else if args.height < 0 then begin + scale := float args.width /. float svg_width ; + height := int_of_float (float svg_height *. args.scale +. 0.5) ; + end + else begin + scale := min (float args.height /. float svg_height) (float args.width /. float svg_width) ; + dx := (float args.width -. (float svg_width *. args.scale +. 0.5)) /. 2. ; + dy := (float args.height -. (float svg_height *. args.scale +. 0.5)) /. 2. + end + end ; + + let surf = Cairo.image_surface_create Cairo.FORMAT_ARGB32 !width !height in + let cr = Cairo.create surf in + Cairo.save cr ; begin + Cairo.set_operator cr Cairo.OPERATOR_CLEAR ; + Cairo.paint cr end ; + Cairo.restore cr ; + + Cairo.translate cr !dx !dy ; + Cairo.scale cr !scale !scale ; + + Cairo.set_source_rgb cr 1. 1. 1. ; Svg_cairo.render svgc cr ; - Cairo.show_page cr ; - Cairo_channel.close chan + Cairo_png.surface_write_to_file surf args.png_file let _ = render_to_png (parse_args ()) diff --git a/test/text.ml b/test/text.ml index cbb497d..80f5c5e 100644 --- a/test/text.ml +++ b/test/text.ml @@ -12,7 +12,7 @@ let text = "hello, world" let box_text cr txt x y = Cairo.save cr ; begin let ext = Cairo.text_extents cr text in - let line_width = Cairo.current_line_width cr in + let line_width = Cairo.get_line_width cr in Cairo.rectangle cr (x +. ext.Cairo.x_bearing -. line_width) (y +. ext.Cairo.y_bearing -. line_width) @@ -23,7 +23,7 @@ let box_text cr txt x y = Cairo.move_to cr x y ; Cairo.show_text cr txt ; Cairo.text_path cr txt ; - Cairo.set_rgb_color cr 1. 0. 0. ; + Cairo.set_source_rgb cr 1. 0. 0. ; Cairo.set_line_width cr 1.0 ; Cairo.stroke cr end ; @@ -33,7 +33,7 @@ let box_text cr txt x y = let box_glyphs cr gly x y = Cairo.save cr ; begin let ext = Cairo.glyph_extents cr gly in - let line_width = Cairo.current_line_width cr in + let line_width = Cairo.get_line_width cr in Cairo.rectangle cr (x +. ext.Cairo.x_bearing -. line_width) (y +. ext.Cairo.y_bearing -. line_width) @@ -48,26 +48,26 @@ let box_glyphs cr gly x y = gly in Cairo.show_glyphs cr gly ; Cairo.glyph_path cr gly ; - Cairo.set_rgb_color cr 1. 0. 0. ; + Cairo.set_source_rgb cr 1. 0. 0. ; Cairo.set_line_width cr 1. ; Cairo.stroke cr end ; Cairo.restore cr let draw cr w h = - Cairo.set_rgb_color cr 0. 0. 0. ; + Cairo.set_source_rgb cr 0. 0. 0. ; Cairo.set_line_width cr 2. ; Cairo.save cr ; begin - Cairo.set_rgb_color cr 1. 1. 1. ; + Cairo.set_source_rgb cr 1. 1. 1. ; Cairo.rectangle cr 0. 0. w h ; - Cairo.set_operator cr Cairo.OPERATOR_SRC ; + Cairo.set_operator cr Cairo.OPERATOR_SOURCE ; Cairo.fill cr end ; Cairo.restore cr ; - Cairo.select_font cr "serif" Cairo.FONT_SLANT_NORMAL Cairo.FONT_WEIGHT_NORMAL ; - Cairo.scale_font cr 40. ; + Cairo.select_font_face cr "serif" Cairo.FONT_SLANT_NORMAL Cairo.FONT_WEIGHT_NORMAL ; + Cairo.set_font_size cr 40. ; let { Cairo.font_height = height } as f_ext = - Cairo.current_font_extents cr in + Cairo.font_extents cr in let glyphs = begin @@ -96,9 +96,8 @@ let draw cr w h = Cairo.translate cr 0. (2. *. height) ; Cairo.save cr ; begin - let m = Cairo.matrix_create () in - Cairo.matrix_rotate m (10. *. atan 1. /. 45.) ; - Cairo.transform_font cr m ; + let m = Cairo.Matrix.init_rotate (10. *. atan 1. /. 45.) in + Cairo.set_font_matrix cr m ; box_text cr text 10. height end ; Cairo.restore cr ; @@ -126,8 +125,8 @@ let main () = w#connect#destroy GMain.quit ; let p = GDraw.pixmap ~width ~height ~window:w () in - let cr = Cairo.create () in - Cairo_lablgtk.set_target_drawable cr p#pixmap ; + let s = Cairo_lablgtk.surface_create p#pixmap in + let cr = Cairo.create s in draw cr (float width) (float height) ; GMisc.pixmap p ~packing:w#add () ; |