-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathattr.ml
430 lines (390 loc) · 9.61 KB
/
attr.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
open Core
open Virtual_dom.Vdom.Attr
module Number = Virtual_dom.Dom_float
type align_options =
| None
| X_max_y_max
| X_max_y_mid
| X_max_y_min
| X_mid_y_max
| X_mid_y_mid
| X_mid_y_min
| X_min_y_max
| X_min_y_mid
| X_min_y_min
type units =
| Object_bounding_box
| User_space_on_use
type angle =
| Deg of float
| Grad of float
| Rad of float
| Turn of float
type path_op =
| Move_to_abs of
{ x : float
; y : float
}
| Move_to_rel of
{ x : float
; y : float
}
| Line_to_abs of
{ x : float
; y : float
}
| Line_to_rel of
{ x : float
; y : float
}
| Arc_to_abs of
{ rx : float
; ry : float
; x_axis_rotation : float
; large_arc : bool
; sweep : bool
; x : float
; y : float
}
| Arc_to_rel of
{ rx : float
; ry : float
; x_axis_rotation : float
; large_arc : bool
; sweep : bool
; dx : float
; dy : float
}
| Cubic_abs of
{ x1 : float
; y1 : float
; x2 : float
; y2 : float
; x : float
; y : float
}
| Cubic_rel of
{ x1 : float
; y1 : float
; x2 : float
; y2 : float
; x : float
; y : float
}
| Cubic_smooth_abs of
{ x2 : float
; y2 : float
; x : float
; y : float
}
| Cubic_smooth_rel of
{ x2 : float
; y2 : float
; x : float
; y : float
}
| Quadratic_abs of
{ x1 : float
; y1 : float
; x : float
; y : float
}
| Quadratic_rel of
{ x1 : float
; y1 : float
; x : float
; y : float
}
| Quadratic_smooth_abs of
{ x : float
; y : float
}
| Quadratic_smooth_rel of
{ x : float
; y : float
}
| Close_path
type transform_op =
| Matrix of float * float * float * float * float * float
| Translate of
{ dx : float
; dy : float
}
| Scale of
{ sx : float
; sy : float
}
| Rotate of
{ a : [ `Deg of float ]
; x : float
; y : float
}
| Skew_x of float
| Skew_y of float
let viewbox =
let c = create "viewBox" in
fun ~min_x ~min_y ~width ~height ->
c (sprintf !"%{Number} %{Number} %{Number} %{Number}" min_x min_y width height)
;;
let href = create "href"
let cx = create_float "cx"
let cy = create_float "cy"
let r = create_float "r"
let rx = create_float "rx"
let ry = create_float "ry"
let x = create_float "x"
let y = create_float "y"
let width = create_float "width"
let height = create_float "height"
let xlink_href = create "xlink:href"
let transform_op_to_string = function
| Matrix (a, b, c, d, e, f) ->
sprintf
!"matrix(%{Number}, %{Number}, %{Number}, %{Number}, %{Number}, %{Number})"
a
b
c
d
e
f
| Translate { dx; dy } -> sprintf !"translate(%{Number}, %{Number})" dx dy
| Scale { sx; sy } -> sprintf !"scale(%{Number}, %{Number})" sx sy
| Rotate { a = `Deg a; x; y } ->
sprintf !"rotate(%{Number}, %{Number}, %{Number})" a x y
| Skew_x s -> sprintf !"skewX(%{Number})" s
| Skew_y s -> sprintf !"skewY(%{Number})" s
;;
let transform transforms =
transforms
|> List.map ~f:transform_op_to_string
|> String.concat ~sep:" "
|> create "transform"
;;
let preserve_aspect_ratio =
let c = create "preserveAspectRatio" in
fun ~(align : align_options) ?meet_or_slice () ->
let a =
match align with
| None -> "none"
| X_min_y_min -> "xMinYMin"
| X_mid_y_min -> "xMidYMin"
| X_max_y_min -> "xMaxYMin"
| X_min_y_mid -> "xMinYMid"
| X_mid_y_mid -> "xMidYMid"
| X_max_y_mid -> "xMaxYMid"
| X_min_y_max -> "xMinYMax"
| X_mid_y_max -> "xMidYMax"
| X_max_y_max -> "xMaxYMax"
in
let m =
match meet_or_slice with
| None -> a
| Some `Meet -> sprintf "%s meet" a
| Some `Slice -> sprintf "%s slice" a
in
c m
;;
let x1 = create_float "x1"
let x2 = create_float "x2"
let y1 = create_float "y1"
let y2 = create_float "y2"
let gradient_units =
let c = create "gradientUnits" in
function
| User_space_on_use -> c "userSpaceOnUse"
| Object_bounding_box -> c "objectBoundingBox"
;;
let gradient_transform transforms =
transforms
|> List.map ~f:transform_op_to_string
|> String.concat ~sep:" "
|> create "gradientTransform"
;;
let spread_method =
let c = create "spreadMethod" in
function
| `Pad -> c "pad"
| `Reflect -> c "reflect"
| `Repeat -> c "repeat"
;;
let marker_height = create_float "markerHeight"
let marker_width = create_float "markerWidth"
let units_helper c = function
| User_space_on_use -> c "userSpaceOnUse"
| Object_bounding_box -> c "objectBoundingBox"
;;
let marker_units =
let c = create "markerUnits" in
units_helper c
;;
let orient =
let c = create "orient" in
function
| `Auto -> c "auto"
| `Auto_start_reverse -> c "auto-start-reverse"
| `Angle (Deg f) -> c (sprintf !"%{Number}deg" f)
| `Angle (Grad f) -> c (sprintf !"%{Number}grad" f)
| `Angle (Rad f) -> c (sprintf !"%{Number}rad" f)
| `Angle (Turn f) -> c (sprintf !"%{Number}turn" f)
;;
let refX = create_float "refX"
let refY = create_float "refY"
let mask_units =
let c = create "maskUnits" in
units_helper c
;;
let mask_content_units =
let c = create "maskContentUnits" in
units_helper c
;;
let d =
let c = create "d" in
fun commands ->
commands
|> List.map ~f:(function
| Move_to_abs { x; y } -> sprintf !"M%{Number},%{Number}" x y
| Move_to_rel { x; y } -> sprintf !"m%{Number},%{Number}" x y
| Line_to_abs { x; y } -> sprintf !"L%{Number},%{Number}" x y
| Line_to_rel { x; y } -> sprintf !"l%{Number},%{Number}" x y
| Arc_to_abs { rx; ry; x_axis_rotation; large_arc; sweep; x; y } ->
sprintf
!"A %{Number} %{Number}, %{Number}, %{Int}, %{Int}, %{Number} %{Number}"
rx
ry
x_axis_rotation
(if large_arc then 1 else 0)
(if sweep then 1 else 0)
x
y
| Arc_to_rel { rx; ry; x_axis_rotation; large_arc; sweep; dx; dy } ->
sprintf
!"a %{Number} %{Number} %{Number} %{Int} %{Int} %{Number} %{Number}"
rx
ry
x_axis_rotation
(if large_arc then 1 else 0)
(if sweep then 1 else 0)
dx
dy
| Cubic_abs { x1; y1; x2; y2; x; y } ->
sprintf
!"C %{Number},%{Number},%{Number},%{Number},%{Number},%{Number}"
x1
y1
x2
y2
x
y
| Cubic_rel { x1; y1; x2; y2; x; y } ->
sprintf
!"c %{Number},%{Number},%{Number},%{Number},%{Number},%{Number}"
x1
y1
x2
y2
x
y
| Cubic_smooth_abs { x2; y2; x; y } ->
sprintf !"S %{Number},%{Number},%{Number},%{Number}" x2 y2 x y
| Cubic_smooth_rel { x2; y2; x; y } ->
sprintf !"s %{Number},%{Number},%{Number},%{Number}" x2 y2 x y
| Quadratic_abs { x1; y1; x; y } ->
sprintf !"Q %{Number},%{Number},%{Number},%{Number}" x1 y1 x y
| Quadratic_rel { x1; y1; x; y } ->
sprintf !"q %{Number},%{Number},%{Number},%{Number}" x1 y1 x y
| Quadratic_smooth_abs { x; y } -> sprintf !"T %{Number},%{Number}" x y
| Quadratic_smooth_rel { x; y } -> sprintf !"t %{Number},%{Number}" x y
| Close_path -> "Z")
|> String.concat
|> c
;;
let points =
let c = create "points" in
fun points ->
points
|> List.map ~f:(fun (x, y) -> sprintf !"%{Number},%{Number}" x y)
|> String.concat ~sep:" "
|> c
;;
let fill f =
let s =
match f with
| `Url url -> sprintf "url(%s)" url
| #Css_gen.Color.t as color -> Css_gen.Color.to_string_css color
in
create "fill" s
;;
let stroke style =
let c = create "stroke" in
c (Css_gen.Color.to_string_css style)
;;
(*
val stroke_width: float -> t
val stroke_linecap: [`Butt | `Round | `Square] -> t
val stroke_dasharray: float list -> t
*)
let stroke_width = create_float "stroke-width"
let stroke_dashoffset = create_float "stroke-dashoffset"
let stroke_linecap linecap =
create
"stroke-linecap"
(match linecap with
| `Butt -> "butt"
| `Round -> "round"
| `Square -> "square")
;;
let stroke_dasharray dashes =
dashes
|> List.map ~f:(sprintf !"%{Number}")
|> String.concat ~sep:","
|> create "stroke-dasharray"
;;
let fx = create_float "fx"
let fy = create_float "fy"
let fr = create_float "fr"
let offset percent =
percent |> Percent.to_percentage |> sprintf "%.2f%%" |> create "offset"
;;
let stop_color color =
let c = create "stop-color" in
c (Css_gen.Color.to_string_css color)
;;
let stop_opacity percent =
percent |> Percent.to_mult |> sprintf "%.5f" |> create "stop-opacity"
;;
let dx = create_float "dx"
let dy = create_float "dy"
module Text = struct
let length_adjust =
let attr_c = create "lengthAdjust" in
function
| `Spacing -> attr_c "spacing"
| `Spacing_and_glyphs -> attr_c "spacingAndGlyphs"
;;
let side =
let c = create "side" in
function
| `Left -> c "left"
| `Right -> c "right"
;;
let spacing =
let c = create "spacing" in
function
| `Auto -> c "auto"
| `Exact -> c "exact"
;;
let length_helper c = function
| #Css_gen.Length.t as length -> c (Css_gen.Length.to_string_css length)
| `Percentage p -> c (sprintf !"%{Number}%%" (Percent.to_percentage p))
| `Number n -> c (Number.to_string n)
;;
let start_offset =
let c = create "startOffset" in
length_helper c
;;
let text_length =
let c = create "textLengh" in
length_helper c
;;
end
let ( @ ) = ( @ )