[cairo-commit] cairo-ocaml/test svg2png.ml, NONE, 1.1 font.ml, 1.1, 1.2 Makefile, 1.6, 1.7

Olivier Andrieu commit at pdx.freedesktop.org
Tue Mar 1 14:19:54 PST 2005


Committed by: oandrieu

Update of /cvs/cairo/cairo-ocaml/test
In directory gabe:/tmp/cvs-serv25584/test

Modified Files:
	font.ml Makefile 
Added Files:
	svg2png.ml 
Log Message:
add libsvg-cairo bindings


--- NEW FILE: svg2png.ml ---

type args = {
    svg_file : string ;
    png_file : string ;
    scale    : float ;
    width    : int ;
    height   : int ;
  }

let parse_args () =
  let svg_file = ref "" in
  let png_file = ref "" in
  let scale = ref 1. in
  let width = ref (-1) in
  let height = ref (-1) in
  let spec = [
    "-s", Arg.Set_float scale, "scale";
    "-w", Arg.Set_int width, "width";
    "-h", Arg.Set_int height, "height" ] in
  let msg = 
    Printf.sprintf "usage: %s [options] <svg_file> [png_file]"
      (Filename.basename Sys.executable_name) in
  Arg.parse 
    spec
    (fun arg -> 
      if !svg_file = "" then svg_file := arg else
      if !png_file = "" then png_file := arg else
      ())
    msg ;
  if !svg_file = "" then begin
    Arg.usage spec msg ;
    exit 1
  end ;
  if !png_file = "" then
    png_file :=
      begin
	if Filename.check_suffix !svg_file ".svg"
	then Filename.chop_suffix !svg_file ".svg"
	else !svg_file 
      end ^ ".png" ;
  { svg_file = !svg_file ; png_file = !png_file ;
    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. ;
  Svg_cairo.render svgc cr ;
  Cairo.show_page cr ;
  Cairo_channel.close chan

let _ = 
  render_to_png (parse_args ())

Index: font.ml
===================================================================
RCS file: /cvs/cairo/cairo-ocaml/test/font.ml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- font.ml	1 Nov 2004 15:50:28 -0000	1.1
+++ font.ml	1 Mar 2005 22:19:51 -0000	1.2
@@ -1,10 +1,4 @@
 
-let out_file name =
-  let oc = open_out name in
-  let channel = Cairo_channel.of_out_channel oc in
-  close_out oc ;
-  channel
-
 let pi = 4. *. atan 1.
 
 let main font_arg =
@@ -22,7 +16,7 @@
   in
 
   let cr = Cairo.create () in
-  let file = out_file "test_font.png" in
+  let file = Cairo_channel.open_out "test_font.png" in
   Cairo.set_target_png ~cr ~file Cairo.FORMAT_ARGB32 ~width:200 ~height:200 ;
 
   Cairo.set_font ~cr ~font ;

Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-ocaml/test/Makefile,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- Makefile	28 Feb 2005 11:17:22 -0000	1.6
+++ Makefile	1 Mar 2005 22:19:51 -0000	1.7
@@ -8,9 +8,15 @@
 # TARGETS += cube
 # endif
 endif
+ifdef LIBSVG_CAIRO_CFLAGS
+TARGETS += svg2png 
+endif
 
 all : $(TARGETS)
 
+svg2png : svg2png.ml
+	$(OCAMLOPT) -o $@ -I ../src cairo.cmxa svg_cairo.cmxa $^
+
 kapow : kapow.ml
 	$(OCAMLOPT) -o $@ -I ../src cairo.cmxa $^
 




More information about the cairo-commit mailing list