File: scene_json.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 (111 lines) | stat: -rw-r--r-- 3,146 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(* 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 array_stringify f ch l =
  Format.fprintf ch "@[<1>[0";
  Array.iter (fun e -> Format.fprintf ch ",@,%a" f e) l;
  Format.fprintf ch "]@]"

let string_stringify ch s =
  (*XXX Escape! *)
  Format.fprintf ch "\"%s\"" s

let color_stringify ch c =
  match c with
  | None -> Format.fprintf ch "0"
  | Some (r, g, b) ->
      let h v = truncate (v *. 255.99) in
      Format.fprintf
        ch
        "@[<1>[0,@,%a]@]"
        string_stringify
        (Format.sprintf "#%02x%02x%02x" (h r) (h g) (h b))

let font_stringify ch (font, size) =
  Format.fprintf ch "%a" string_stringify (Format.sprintf "%gpx %s" size font)

let command_stringify ch c =
  match c with
  | Move_to (x, y) -> Format.fprintf ch "@[<1>[0,@,%g,@,%g]@]" x y
  | Curve_to (x1, y1, x2, y2, x3, y3) ->
      Format.fprintf ch "@[<1>[1,@,%g,@,%g,@,%g,@,%g,@,%g,@,%g]@]" x1 y1 x2 y2 x3 y3

let commands_stringify = array_stringify command_stringify

let point_stringify ch (x, y) = Format.fprintf ch "@[<1>[0,@,%g,@,%g]@]" x y

let points_stringify = array_stringify point_stringify

let rect_stringify ch (x1, y1, x2, y2) =
  Format.fprintf ch "@[<1>[0,@,%g,@,%g,@,%g,@,%g]@]" x1 y1 x2 y2

let rect_array_stringify = array_stringify rect_stringify

let element_stringify ch e =
  match e with
  | Path (cmds, fill, stroke) ->
      Format.fprintf
        ch
        "@[<1>[0,@,%a,@,%a,@,%a]@]"
        commands_stringify
        cmds
        color_stringify
        fill
        color_stringify
        stroke
  | Polygon (l, fill, stroke) ->
      Format.fprintf
        ch
        "@[<1>[1,@,%a,@,%a,@,%a]@]"
        points_stringify
        l
        color_stringify
        fill
        color_stringify
        stroke
  | Ellipse (cx, cy, rx, ry, fill, stroke) ->
      Format.fprintf
        ch
        "@[<1>[2,@,%g,@,%g,@,%g,@,%g,@,%a,@,%a]@]"
        cx
        cy
        rx
        ry
        color_stringify
        fill
        color_stringify
        stroke
  | Text (x, y, txt, font, fill, stroke) ->
      Format.fprintf
        ch
        "@[<1>[3,@,%g,@,%g,@,%a,@,%a,@,%a,@,%a]@]"
        x
        y
        string_stringify
        txt
        font_stringify
        font
        color_stringify
        fill
        color_stringify
        stroke

let stringify = array_stringify element_stringify