File: tips_font.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 (29 lines) | stat: -rw-r--r-- 892 bytes parent folder | download | duplicates (3)
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
(* This file is part of the tutorial
   http://cairo.forge.ocamlcore.org/tutorial/
*)

open Cairo

let () =
  let alphabet = "AbCdEfGhIjKlMnOpQrStUvWxYz" in
  let surface = Cairo.Image.create Cairo.Image.ARGB32 ~w:780 ~h:30 in
  let cr = Cairo.create surface in
  (* Examples are in 26.0 x 1.0 coordinate space *)
  Cairo.scale cr 30. 30.;
  Cairo.set_font_size cr 0.8;

  (* Drawing code goes here *)
  Cairo.set_source_rgb cr 0.0 0.0 0.0;
  Cairo.select_font_face cr "Georgia" ~weight:Bold;

  let fe = Cairo.font_extents cr in
  for i = 0 to String.length alphabet - 1 do
    let letter = String.make 1 (alphabet.[i]) in
    let te = Cairo.text_extents cr letter in
    Cairo.move_to cr (float i +. 0.5 -. te.x_bearing -. te.width /. 2.)
      (0.5 -. fe.descent +. fe.baseline /. 2.);
    Cairo.show_text cr letter;
  done;

  (* Write output *)
  Cairo.PNG.write surface "tips_font.png"