File: converter.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (53 lines) | stat: -rw-r--r-- 2,085 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(* Graph viewer
 * Copyright (C) 2010 Jérôme Vouillon
 * Laboratoire PPS - CNRS Université Paris Diderot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open Scene

let offset_command x y c =
  match c with
  | Move_to (x1, y1) -> Move_to (x1 -. x, y1 -. y)
  | Curve_to (x1, y1, x2, y2, x3, y3) ->
      Curve_to (x1 -. x, y1 -. y, x2 -. x, y2 -. y, x3 -. x, y3 -. y)

let offset_element x y e =
  match e with
  | Path (cmds, c1, c2) -> Path (Array.map (fun c -> offset_command x y c) cmds, c1, c2)
  | Polygon (pts, c1, c2) ->
      Polygon (Array.map (fun (x1, y1) -> x1 -. x, y1 -. y) pts, c1, c2)
  | Ellipse (cx, cy, rx, ry, c1, c2) -> Ellipse (cx -. x, cy -. y, rx, ry, c1, c2)
  | Text (x1, y1, txt, font, c1, c2) -> Text (x1 -. x, y1 -. y, txt, font, c1, c2)

let _ =
  let ch = if Array.length Sys.argv > 1 then open_in Sys.argv.(1) else stdin in
  let _, g = Dot_graph.of_channel ch in
  let bbox, scene = Dot_render.f g in
  let x1, y1, x2, y2 = bbox in
  let l = Scene.get scene in
  let l = Array.map (fun e -> offset_element x1 y1 e) l in
  let bbox = 0., 0., x2 -. x1, y2 -. y1 in
  let i = Cairo.image_surface_create Cairo.FORMAT_ARGB32 1024 1024 in
  let bboxes = Scene_extents.compute (Cairo.create i) l in
  Format.printf
    "@[<1>[0,@,%a,@,%a,@,%a]@]@."
    Scene_json.rect_stringify
    bbox
    Scene_json.rect_array_stringify
    bboxes
    Scene_json.stringify
    l