-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.pony
105 lines (86 loc) · 2.88 KB
/
svg.pony
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
use "collections"
use "itertools"
primitive SVG
fun svg(): SVGNode =>
SVGNode("svg").>a("version", "1.1")
.>a("xmlns:xlink", "http://www.w3.org/1999/xlink")
.>a("xmlns", "http://www.w3.org/2000/svg")
fun circle(cx: F64, cy: F64, r: F64,
stroke: String = "black", stroke_width: String = "1"): SVGNode
=>
SVGNode("circle")
.>a("cx", cx.string())
.>a("cy", cy.string())
.>a("r", r.string())
.>a("stroke", stroke)
.>a("stroke-width", stroke_width)
.>a("fill", "none")
fun line(x1: F64, y1: F64, x2: F64, y2: F64,
stroke: String = "black", stroke_width: String = "1"): SVGNode
=>
SVGNode("line")
.>a("x1", x1.string())
.>a("y1", y1.string())
.>a("x2", x2.string())
.>a("y2", y2.string())
.>a("stroke", stroke)
.>a("stroke-width", stroke_width)
.>a("fill", "none")
fun polyline(points: Iterator[(F64, F64)],
stroke: String = "black", stroke_width: String = "1"): SVGNode
=>
let points_value: String = " ".join(Iter[(F64, F64)](points).map[String](
{(p): String => p._1.string() + "," + p._2.string()}))
SVGNode("polyline")
.>a("points", points_value)
.>a("stroke", stroke)
.>a("stroke-width", stroke_width)
.>a("fill", "none")
fun polygon(points: Iterator[(F64, F64)],
stroke: String = "black", stroke_width: String = "1"): SVGNode
=>
let points_value: String = " ".join(Iter[(F64, F64)](points).map[String](
{(p): String => p._1.string() + "," + p._2.string()}))
SVGNode("polygon")
.>a("points", points_value)
.>a("stroke", stroke)
.>a("stroke-width", stroke_width)
.>a("fill", "none")
class SVGNode
let svg_tag: String
let attributes: SVGAttributes
let children: SVGNodes
new create(svg_tag': String) =>
svg_tag = svg_tag'
attributes = SVGAttributes
children = SVGNodes
fun render(): String =>
"<" + svg_tag + " " + attributes.render() + ">" +
children.render() +
"</" + svg_tag + ">"
fun ref a(attribute: String, value: String) =>
attributes.add_attribute(attribute, value)
fun ref c(node: SVGNode) =>
children.add_child(node)
fun ref cs(nodes: Iterator[SVGNode]) =>
for n in nodes do
children.add_child(n)
end
class SVGAttributes
let attributes: Array[(String, String)]
new create() =>
attributes = Array[(String, String)]
fun ref add_attribute(attribute: String, value: String) =>
attributes.push((attribute, value))
fun render(): String =>
" ".join(Iter[(String, String)](attributes.values())
.map[String]({(x) => x._1 + "=" + "\"" + x._2 +"\""}))
class SVGNodes
let children: Array[SVGNode]
new create() =>
children = Array[SVGNode]
fun ref add_child(node: SVGNode) =>
children.push(node)
fun render(): String =>
"".join(Iter[SVGNode box](children.values())
.map[String]({(x) => x.render() }))