Skip to content

Commit

Permalink
Compile the tutorial examples by default & fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Sep 7, 2018
1 parent eb453de commit c5bfd27
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PKGVERSION = $(shell git describe --always)
PACKAGES = $(basename $(wildcard *.opam))

build:
dune build @install @examples
dune build @install @examples @tutorial

test:
dune runtest --force
Expand Down
58 changes: 30 additions & 28 deletions examples/diagram.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ open Cairo

let diagram_draw_source cr =
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr

let diagram_draw_mask cr =
Cairo.set_source_rgb cr 1. 0.9 0.6;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr

let diagram_draw_mask_pattern cr pat =
Expand All @@ -20,21 +20,21 @@ let diagram_draw_mask_pattern cr pat =

let diagram_draw_dest cr =
Cairo.set_source_rgb cr 1. 1. 1.;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr

let stroke_draw_mask cr =
Cairo.Group.push cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0.20 0.20 0.6 0.6;
Cairo.rectangle cr 0.30 0.30 0.4 0.4;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.rectangle cr 0.20 0.20 ~w:0.6 ~h:0.6;
Cairo.rectangle cr 0.30 0.30 ~w:0.4 ~h:0.4;
Cairo.set_fill_rule cr EVEN_ODD;
Cairo.fill cr;
Cairo.set_fill_rule cr WINDING;

diagram_draw_mask_pattern cr (Cairo.Group.pop cr);

Cairo.rectangle cr 0.25 0.25 0.5 0.5;
Cairo.rectangle cr 0.25 0.25 ~w:0.5 ~h:0.5;
Cairo.set_source_rgb cr 0. 0.6 0.;

let px, py = Cairo.device_to_user_distance cr 1. 1. in
Expand All @@ -45,20 +45,20 @@ let stroke_draw_dest cr =
diagram_draw_dest cr;
Cairo.set_line_width cr 0.1;
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0.25 0.25 0.5 0.5;
Cairo.rectangle cr 0.25 0.25 ~w:0.5 ~h:0.5;
Cairo.stroke cr

let fill_draw_mask cr =
Cairo.Group.push cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0.25 0.25 0.5 0.5;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.rectangle cr 0.25 0.25 ~w:0.5 ~h:0.5;
Cairo.set_fill_rule cr EVEN_ODD;
Cairo.fill cr;
Cairo.set_fill_rule cr WINDING;

diagram_draw_mask_pattern cr (Cairo.Group.pop cr);

Cairo.rectangle cr 0.25 0.25 0.5 0.5;
Cairo.rectangle cr 0.25 0.25 ~w:0.5 ~h:0.5;
Cairo.set_source_rgb cr 0. 0.6 0.;
let px, py = Cairo.device_to_user_distance cr 1. 1. in
Cairo.set_line_width cr (max px py);
Expand All @@ -67,7 +67,7 @@ let fill_draw_mask cr =
let fill_draw_dest cr =
diagram_draw_dest cr;
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0.25 0.25 0.5 0.5;
Cairo.rectangle cr 0.25 0.25 ~w:0.5 ~h:0.5;
Cairo.fill cr

let showtext_draw_mask cr =
Expand All @@ -79,7 +79,7 @@ let showtext_draw_mask cr =
Cairo.set_font_size cr 1.2;
let te = Cairo.text_extents cr "a" in
Cairo.Group.push cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.move_to cr (0.5 -. te.width /. 2. -. te.x_bearing)
(0.5 -. te.height /. 2. -. te.y_bearing);
Cairo.Path.text cr "a";
Expand All @@ -102,7 +102,7 @@ let showtext_draw_mask cr =
let showtext_draw_dest cr =
(* white background *)
Cairo.set_source_rgb cr 1. 1. 1.;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr;

(* black letter "a" *)
Expand All @@ -116,7 +116,7 @@ let showtext_draw_dest cr =

let paint_draw_source cr =
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr

let paint_draw_dest cr =
Expand All @@ -125,35 +125,37 @@ let paint_draw_dest cr =
Cairo.paint cr ~alpha:0.5

let mask_draw_source cr =
let linpat = Cairo.Pattern.create_linear 0. 0. 1. 1. in
let linpat = Cairo.Pattern.create_linear ~x0:0. ~y0:0. ~x1:1. ~y1:1. in
Cairo.Pattern.add_color_stop_rgb linpat 0. 0.3 0.8;
Cairo.Pattern.add_color_stop_rgb linpat 0. 0.8 0.3 ~ofs:1.;
Cairo.set_source cr linpat;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr

let mask_draw_mask cr =
let radialinv = Cairo.Pattern.create_radial 0.5 0.5 0.25 0.5 0.5 0.75 in
let radialinv = Cairo.Pattern.create_radial ~x0:0.5 ~y0:0.5 ~r0:0.25
~x1:0.5 ~y1:0.5 ~r1:0.75 in
Cairo.Pattern.add_color_stop_rgba radialinv 0. 0. 0. 0.;
Cairo.Pattern.add_color_stop_rgba radialinv ~ofs:0.5 0. 0. 0. 1.;
Cairo.save cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.clip cr;
diagram_draw_mask_pattern cr radialinv;
Cairo.restore cr

let mask_draw_dest cr =
let linpat = Cairo.Pattern.create_linear 0. 0. 1. 1. in
let linpat = Cairo.Pattern.create_linear ~x0:0. ~y0:0. ~x1:1. ~y1:1. in
Cairo.Pattern.add_color_stop_rgb linpat 0. 0.3 0.8;
Cairo.Pattern.add_color_stop_rgb linpat ~ofs:1. 0. 0.8 0.3;

let radpat = Cairo.Pattern.create_radial 0.5 0.5 0.25 0.5 0.5 0.75 in
let radpat = Cairo.Pattern.create_radial ~x0:0.5 ~y0:0.5 ~r0:0.25
~x1:0.5 ~y1:0.5 ~r1:0.75 in
Cairo.Pattern.add_color_stop_rgba radpat 0. 0. 0. 1.;
Cairo.Pattern.add_color_stop_rgba radpat ~ofs:0.5 0. 0. 0. 0.;

diagram_draw_dest cr;
Cairo.save cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.clip cr;
Cairo.set_source cr linpat;
Cairo.mask cr radpat;
Expand Down Expand Up @@ -188,7 +190,7 @@ let diagram fname alpha0 alpha1 alpha2 =
let width=160. and height=120. in
let svg_filename = fname ^ ".svg"
and png_filename = fname ^ ".png" in
let surf = Cairo.SVG.create svg_filename width height in
let surf = Cairo.SVG.create svg_filename ~w:width ~h:height in
let cr = Cairo.create surf in

(*
Expand All @@ -197,7 +199,7 @@ let diagram fname alpha0 alpha1 alpha2 =
let layer draw =
Cairo.save cr;
Cairo.Group.push cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.clip cr;
draw fname cr;
Cairo.Group.pop_to_source cr;
Expand All @@ -223,10 +225,10 @@ let diagram fname alpha0 alpha1 alpha2 =
Cairo.translate cr (3. *. width /. height -. 1.) 0.;
let ux, uy = Cairo.device_to_user_distance cr 2. 2. in
Cairo.set_line_width cr (max ux uy);
Cairo.rectangle cr 0. 0. 1. 3.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:3.;
Cairo.clip_preserve cr;
Cairo.stroke cr;
Cairo.rectangle cr 0. 1. 1. 1.;
Cairo.rectangle cr 0. 1. ~w:1. ~h:1.;
Cairo.stroke cr;
Cairo.restore cr;

Expand All @@ -238,7 +240,7 @@ let diagram fname alpha0 alpha1 alpha2 =
Cairo.scale cr (width -. height /. 3.) height;
Cairo.transform cr { xx=0.6; yx=0.; xy=1./.3.; yy=0.5; x0=tx; y0=ty };
Cairo.Group.push cr;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.clip cr;
draw fname cr;
Cairo.Group.pop_to_source cr;
Expand All @@ -252,7 +254,7 @@ let diagram fname alpha0 alpha1 alpha2 =
Cairo.set_source_rgb cr 0. 0. 0.;
let ux, uy = Cairo.device_to_user_distance cr 2. 2. in
Cairo.set_line_width cr (max ux uy);
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.stroke cr
end;
(* mask layer *)
Expand Down
38 changes: 20 additions & 18 deletions examples/draw.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let draw_path_lineto cr =

let draw_path_arcto cr =
draw_path_lineto cr;
Cairo.arc cr 0.5 0.5 (0.25 *. sqrt 2.) (-. pi_4) pi_4
Cairo.arc cr 0.5 0.5 ~r:(0.25 *. sqrt 2.) ~a1:(-. pi_4) ~a2:pi_4

let draw_path_curveto cr =
draw_path_arcto cr;
Expand Down Expand Up @@ -60,7 +60,7 @@ let draw_textextents cr =
Cairo.set_line_width cr px;
Cairo.set_dash cr [| 3. *. px |];
Cairo.rectangle cr (x +. te.Cairo.x_bearing)
(y +. te.Cairo.y_bearing) te.Cairo.width te.Cairo.height;
(y +. te.Cairo.y_bearing) ~w:te.Cairo.width ~h:te.Cairo.height;
Cairo.stroke cr;

(* text *)
Expand All @@ -79,37 +79,39 @@ let draw_textextents cr =
(* text's advance (blue dot) *)
Cairo.set_source_rgba cr 0. 0. 0.75 0.5;
Cairo.arc cr (x +. te.Cairo.x_advance) (y +. te.Cairo.y_advance)
(6. *. px) 0. two_pi;
~r:(6. *. px) ~a1:0. ~a2:two_pi;
Cairo.fill cr;

(* reference point (x,y) (red dot) *)
Cairo.arc cr x y (6. *. px) 0. two_pi;
Cairo.arc cr x y ~r:(6. *. px) ~a1:0. ~a2:two_pi;
Cairo.set_source_rgba cr 0.75 0. 0. 0.5;
Cairo.fill cr
;;


let draw_setsourcegradient cr =
let radpat = Cairo.Pattern.create_radial 0.25 0.25 0.1 0.5 0.5 0.5 in
let radpat = Cairo.Pattern.create_radial ~x0:0.25 ~y0:0.25 ~r0:0.1
~x1:0.5 ~y1:0.5 ~r1:0.5 in
Cairo.Pattern.add_color_stop_rgb radpat 1.0 0.8 0.8;
Cairo.Pattern.add_color_stop_rgb radpat ~ofs:1. 0.9 0.0 0.0;

for i = 1 to 9 do
for j = 1 to 9 do
Cairo.rectangle cr (float i /. 10.0 -. 0.04) (float j /. 10.0 -. 0.04)
0.08 0.08;
~w:0.08 ~h:0.08;
done;
done;
Cairo.set_source cr radpat;
Cairo.fill cr;

let linpat = Cairo.Pattern.create_linear 0.25 0.35 0.75 0.65 in
let linpat = Cairo.Pattern.create_linear ~x0:0.25 ~y0:0.35
~x1:0.75 ~y1:0.65 in
Cairo.Pattern.add_color_stop_rgba linpat ~ofs:0.00 1. 1. 1. 0.;
Cairo.Pattern.add_color_stop_rgba linpat ~ofs:0.25 0. 1. 0. 0.5;
Cairo.Pattern.add_color_stop_rgba linpat ~ofs:0.50 1. 1. 1. 0.;
Cairo.Pattern.add_color_stop_rgba linpat ~ofs:0.75 0. 0. 1. 0.5;
Cairo.Pattern.add_color_stop_rgba linpat ~ofs:1.00 1. 1. 1. 0.;
Cairo.rectangle cr 0.0 0.0 1. 1.;
Cairo.rectangle cr 0.0 0.0 ~w:1. ~h:1.;
Cairo.set_source cr linpat;
Cairo.fill cr
;;
Expand Down Expand Up @@ -141,12 +143,12 @@ let path_diagram cr =
let x, y = get_point path.(0) in
let px, py = Cairo.device_to_user_distance cr 5. 5. in
let px = max px py in
Cairo.arc cr x y px 0. two_pi;
Cairo.arc cr x y ~r:px ~a1:0. ~a2:two_pi;
Cairo.set_source_rgba cr 0.0 0.6 0.0 0.5;
Cairo.fill cr;

let x, y = get_point path.(len - 1) in
Cairo.arc cr x y px 0. two_pi;
Cairo.arc cr x y ~r:px ~a1:0. ~a2:two_pi;
Cairo.set_source_rgba cr 0.0 0.0 0.75 0.5;
Cairo.fill cr;
)
Expand All @@ -158,9 +160,9 @@ let draw_path_curveto_hints cr =
let px = max px py in
Cairo.set_source_rgba cr 0.5 0. 0. 0.5;
Cairo.Path.sub cr;
Cairo.arc cr 0.5 0.625 px 0. two_pi;
Cairo.arc cr 0.5 0.625 ~r:px ~a1:0. ~a2:two_pi;
Cairo.fill cr;
Cairo.arc cr 0.5 0.875 px 0. two_pi;
Cairo.arc cr 0.5 0.875 ~r:px ~a1:0. ~a2:two_pi;
Cairo.fill cr;

let px, py = Cairo.device_to_user_distance cr 2. 2. in
Expand All @@ -187,15 +189,15 @@ let draw_setsourcergba cr =
Cairo.set_line_width cr 0.2;
Cairo.stroke cr;

Cairo.rectangle cr 0. 0. 0.5 0.5;
Cairo.rectangle cr 0. 0. ~w:0.5 ~h:0.5;
Cairo.set_source_rgba cr 1. 0. 0. 0.80;
Cairo.fill cr;

Cairo.rectangle cr 0. 0.5 0.5 0.5;
Cairo.rectangle cr 0. 0.5 ~w:0.5 ~h:0.5;
Cairo.set_source_rgba cr 0. 1. 0. 0.60;
Cairo.fill cr;

Cairo.rectangle cr 0.5 0. 0.5 0.5;
Cairo.rectangle cr 0.5 0. ~w:0.5 ~h:0.5;
Cairo.set_source_rgba cr 0. 0. 1. 0.40;
Cairo.fill cr
;;
Expand All @@ -220,13 +222,13 @@ let diagram name =
let width = 120. and height = 120. in
let svg_filename = name ^ ".svg"
and png_filename = name ^ ".png" in
let surf = Cairo.SVG.create svg_filename width height in
let surf = Cairo.SVG.create svg_filename ~w:width ~h:height in
let cr = Cairo.create surf in

Cairo.scale cr width height;
Cairo.set_line_width cr 0.01;

Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.set_source_rgb cr 1. 1. 1.;
Cairo.fill cr;

Expand All @@ -235,7 +237,7 @@ let diagram name =
let ux, uy = Cairo.device_to_user_distance cr 2. 2. in
Cairo.set_line_width cr (max ux uy);
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0. 0. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.stroke cr;

(* write output *)
Expand Down
4 changes: 2 additions & 2 deletions examples/fill.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*)

let () =
let surface = Cairo.Image.create Cairo.Image.ARGB32 120 120 in
let surface = Cairo.Image.create Cairo.Image.ARGB32 ~w:120 ~h:120 in
let cr = Cairo.create surface in
(* Examples are in 1.0 x 1.0 coordinate space *)
Cairo.scale cr 120. 120.;

(* Drawing code goes here *)
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0.25 0.25 0.5 0.5;
Cairo.rectangle cr 0.25 0.25 ~w:0.5 ~h:0.5;
Cairo.fill cr;

(* Write output *)
Expand Down
7 changes: 4 additions & 3 deletions examples/mask.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
*)

let () =
let surface = Cairo.Image.create Cairo.Image.ARGB32 120 120 in
let surface = Cairo.Image.create Cairo.Image.ARGB32 ~w:120 ~h:120 in
let cr = Cairo.create surface in
(* Examples are in 1.0 x 1.0 coordinate space *)
Cairo.scale cr 120. 120.;

(* Drawing code goes here *)
let linpat = Cairo.Pattern.create_linear 0. 0. 1. 1. in
let linpat = Cairo.Pattern.create_linear ~x0:0. ~y0:0. ~x1:1. ~y1:1. in
Cairo.Pattern.add_color_stop_rgb linpat 0. 0.3 0.8;
Cairo.Pattern.add_color_stop_rgb linpat ~ofs:1. 0. 0.8 0.3;

let radpat = Cairo.Pattern.create_radial 0.5 0.5 0.25 0.5 0.5 0.75 in
let radpat = Cairo.Pattern.create_radial ~x0:0.5 ~y0:0.5 ~r0:0.25
~x1:0.5 ~y1:0.5 ~r1:0.75 in
Cairo.Pattern.add_color_stop_rgba radpat 0. 0. 0. 1.;
Cairo.Pattern.add_color_stop_rgba radpat ~ofs:0.5 0. 0. 0. 0.;

Expand Down
2 changes: 1 addition & 1 deletion examples/paint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*)

let () =
let surface = Cairo.Image.create Cairo.Image.ARGB32 120 120 in
let surface = Cairo.Image.create Cairo.Image.ARGB32 ~w:120 ~h:120 in
let cr = Cairo.create surface in
(* Examples are in 1.0 x 1.0 coordinate space *)
Cairo.scale cr 120. 120.;
Expand Down
Loading

0 comments on commit c5bfd27

Please sign in to comment.