File: test_path.ml

package info (click to toggle)
ocaml-cairo2 0.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 716 kB
  • sloc: ml: 2,955; ansic: 2,132; makefile: 24; sh: 17
file content (36 lines) | stat: -rw-r--r-- 1,106 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

open Format
open Cairo

let print_path ppf p =
  fprintf ppf "@[<2>[|";
  for i = 0 to Array.length p - 1 do
    if i > 0 then fprintf ppf ";@ ";
    match p.(i) with
    | MOVE_TO(x,y) -> fprintf ppf "MOVE_TO(%g,%g)" x y
    | LINE_TO(x,y) -> fprintf ppf "LINE_TO(%g,%g)" x y
    | CURVE_TO (x1,y1, x2,y2, x3,y3) ->
       fprintf ppf "CURVE_TO(%g,%g, %g,%g, %g,%g)" x1 y1 x2 y2 x3 y3
    | CLOSE_PATH -> fprintf ppf "CLOSE_PATH"
  done;
  fprintf ppf "@]|]"

let () =
  let tmp = Filename.get_temp_dir_name() in
  let surface = Cairo.PDF.create (Filename.concat tmp "test_path.pdf")
                  ~w:300. ~h:300. in
  let cr = Cairo.create surface in

  move_to cr 0. 0.;
  line_to cr 100. 100.;
  let p = Path.to_array (Path.copy cr) in
  printf "Current path: %a\n%!" print_path p;
  let p_rev = Path.fold (Path.copy cr) (fun l x -> x :: l) [] in
  assert(Array.to_list p = List.rev p_rev);
  let q = [| LINE_TO(110., 200.); LINE_TO(50., 150.) |] in
  Path.append cr (Path.of_array q);

  assert(Path.to_array (Path.copy cr) = Array.append p q);

  Cairo.stroke cr;
  Cairo.Surface.finish surface