-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdiagram.ml
279 lines (237 loc) · 7.97 KB
/
diagram.ml
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
(* This file is part of the tutorial
http://cairo.forge.ocamlcore.org/tutorial/
*)
open Cairo
let diagram_draw_source cr =
Cairo.set_source_rgb cr 0. 0. 0.;
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. ~w:1. ~h:1.;
Cairo.fill cr
let diagram_draw_mask_pattern cr pat =
Cairo.set_source_rgb cr 1. 0.9 0.6;
Cairo.mask cr pat
let diagram_draw_dest cr =
Cairo.set_source_rgb cr 1. 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. ~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 ~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);
Cairo.stroke cr
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 ~w:0.5 ~h:0.5;
Cairo.stroke cr
let fill_draw_mask cr =
Cairo.Group.push cr;
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 ~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);
Cairo.stroke 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 ~w:0.5 ~h:0.5;
Cairo.fill cr
let showtext_draw_mask cr =
(* yellow mask color *)
Cairo.set_source_rgb cr 1. 0.9 0.6;
(* rectangle with an "a"-shaped hole *)
Cairo.select_font_face cr "Georgia" ~weight:Bold;
Cairo.set_font_size cr 1.2;
let te = Cairo.text_extents cr "a" in
Cairo.Group.push cr;
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";
Cairo.set_fill_rule cr EVEN_ODD;
Cairo.fill cr;
Cairo.set_fill_rule cr WINDING;
Cairo.Group.pop_to_source cr;
Cairo.paint cr;
(* show the outline of the glyph with a green line *)
Cairo.move_to cr (0.5 -. te.width /. 2. -. te.x_bearing)
(0.5 -. te.height /. 2. -. te.y_bearing);
Cairo.set_source_rgb cr 0. 0.6 0.;
let ux, uy = Cairo.device_to_user_distance cr 1. 1. in
Cairo.set_line_width cr (max ux uy);
Cairo.Path.text cr "a";
Cairo.stroke cr
let showtext_draw_dest cr =
(* white background *)
Cairo.set_source_rgb cr 1. 1. 1.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr;
(* black letter "a" *)
Cairo.set_source_rgb cr 0.0 0.0 0.0;
Cairo.select_font_face cr "Georgia" ~weight:Bold;
Cairo.set_font_size cr 1.2;
let te = Cairo.text_extents cr "a" in
Cairo.move_to cr (0.5 -. te.width /. 2. -. te.x_bearing)
(0.5 -. te.height /. 2. -. te.y_bearing);
Cairo.show_text cr "a"
let paint_draw_source cr =
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.fill cr
let paint_draw_dest cr =
diagram_draw_dest cr;
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.paint cr ~alpha:0.5
let mask_draw_source cr =
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. ~w:1. ~h:1.;
Cairo.fill cr
let mask_draw_mask cr =
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. ~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 ~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 ~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. ~w:1. ~h:1.;
Cairo.clip cr;
Cairo.set_source cr linpat;
Cairo.mask cr radpat;
Cairo.restore cr
let draw_dest name cr =
if name = "stroke" then stroke_draw_dest cr
else if name = "fill" then fill_draw_dest cr
else if name = "showtext" then showtext_draw_dest cr
else if name = "paint" then paint_draw_dest cr
else if name = "mask" then mask_draw_dest cr
else diagram_draw_dest cr
let draw_mask name cr =
if name = "stroke" then stroke_draw_mask cr
else if name = "fill" then fill_draw_mask cr
else if name = "showtext" then showtext_draw_mask cr
else if name = "paint" then ()
else if name = "mask" then mask_draw_mask cr
else diagram_draw_mask cr
let draw_source name cr =
if name = "paint" then paint_draw_source cr
else if name = "mask" then mask_draw_source cr
else diagram_draw_source cr
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 ~w:width ~h:height in
let cr = Cairo.create surf in
(*
* show layers separately on the right
*)
let layer draw =
Cairo.save cr;
Cairo.Group.push cr;
Cairo.rectangle cr 0. 0. ~w:1. ~h:1.;
Cairo.clip cr;
draw fname cr;
Cairo.Group.pop_to_source cr;
Cairo.paint cr;
Cairo.restore cr;
in
Cairo.save cr;
Cairo.scale cr (height /. 3.) (height /. 3.);
(* source *)
Cairo.translate cr (3. *. width /. height -. 1.) 0.;
layer draw_source;
(* mask *)
Cairo.translate cr 0. 1.;
layer draw_mask;
(* destination *)
Cairo.translate cr 0. 1.;
layer draw_dest;
Cairo.restore cr;
(* draw a border around the layers *)
Cairo.save cr;
Cairo.scale cr (height /. 3.) (height /. 3.);
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. ~w:1. ~h:3.;
Cairo.clip_preserve cr;
Cairo.stroke cr;
Cairo.rectangle cr 0. 1. ~w:1. ~h:1.;
Cairo.stroke cr;
Cairo.restore cr;
(*
* layer diagram on the left
*)
let left_layers ~tx ~ty alpha draw =
Cairo.save cr;
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. ~w:1. ~h:1.;
Cairo.clip cr;
draw fname cr;
Cairo.Group.pop_to_source cr;
Cairo.paint cr ~alpha;
Cairo.restore cr;
in
(* destination layer *)
left_layers alpha0 ~tx:0.02 ~ty:0.45 begin fun fname cr ->
draw_dest fname cr;
(* this layer gets a black border *)
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. ~w:1. ~h:1.;
Cairo.stroke cr
end;
(* mask layer *)
left_layers alpha1 draw_mask ~tx:0.04 ~ty:0.25;
(* source layer *)
left_layers alpha2 draw_source ~tx:0.06 ~ty:0.05;
(* write output *)
Cairo.PNG.write surf png_filename;
Cairo.Surface.finish surf
let () =
diagram "destination" 1.0 0.15 0.15;
diagram "the-mask" 0.15 1.0 0.15;
diagram "source" 0.15 0.15 1.0;
diagram "stroke" 1.0 0.8 0.4;
diagram "fill" 1.0 0.8 0.4;
diagram "showtext" 1.0 0.8 0.4;
diagram "paint" 1.0 0.8 0.4;
diagram "mask" 1.0 0.8 0.4