forked from statebox/purescript-halogen-svg
/
Path.purs
196 lines (165 loc) · 4.2 KB
/
Path.purs
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
module Halogen.Svg.Attributes.Path
( PathCommand -- Constructor not exported. Use m, l, h, v, c, s, q, t, a, or z
, CommandPositionReference(..)
, CommandArcChoice(..)
, CommandSweepChoice(..)
, toArrayString
, m
, l
, h
, v
, c
, s
, q
, t
, a
, z
) where
import Prelude
import Data.String (toUpper)
import Safe.Coerce (coerce)
-- | Constructor intentionally not exported.
-- | use `m`, `l`, `h`, `v`, `c`, `s`, `q`, `t`, `s`, or `z` instead.
newtype PathCommand = PathCommand String
derive instance eqPathCommand :: Eq PathCommand
instance showPathCommand :: Show PathCommand where
show val = printPathCommand val
printPathCommand :: PathCommand -> String
printPathCommand (PathCommand s_) = s_
toArrayString :: Array PathCommand -> Array String
toArrayString = coerce
data CommandPositionReference = Rel | Abs
derive instance eqCommandPositionReference :: Eq CommandPositionReference
instance showCommandPositionReference :: Show CommandPositionReference where
show = case _ of
Abs -> "Abs"
Rel -> "Rel"
-- | Arc0 = Small arc
-- | Arc1 = Large arc
data CommandArcChoice = Arc0 | Arc1
derive instance eqCommandArcChoice :: Eq CommandArcChoice
instance showCommandArcChoice :: Show CommandArcChoice where
show = case _ of
Arc0 -> "Arc0"
Arc1 -> "Arc1"
printCommandArcChoice :: CommandArcChoice -> String
printCommandArcChoice = case _ of
Arc0 -> "0"
Arc1 -> "1"
-- | Sweep0 = Counter-Clockwise / Negative
-- | Sweep1 = Clockwise / Positive
data CommandSweepChoice = Sweep0 | Sweep1
derive instance eqCommandSweepChoice :: Eq CommandSweepChoice
instance showCommandSweepChoice :: Show CommandSweepChoice where
show = case _ of
Sweep0 -> "Sweep0"
Sweep1 -> "Sweep1"
printCommandSweepChoice :: CommandSweepChoice -> String
printCommandSweepChoice = case _ of
Sweep0 -> "0"
Sweep1 -> "1"
-- For internal use. Do not export.
renderCommand :: CommandPositionReference -> String -> String
renderCommand cmd s_ = case cmd of
Rel -> s_
Abs -> toUpper s_
m :: CommandPositionReference -> Number -> Number -> PathCommand
m = renderCommand2Args "m"
l :: CommandPositionReference -> Number -> Number -> PathCommand
l = renderCommand2Args "l"
h :: CommandPositionReference -> Number -> PathCommand
h = renderCommand1Arg "h"
v :: CommandPositionReference -> Number -> PathCommand
v = renderCommand1Arg "v"
c
:: CommandPositionReference
-> Number
-> Number
-> Number
-> Number
-> Number
-> Number
-> PathCommand
c ref x1_ y1_ x2_ y2_ x_ y_ =
PathCommand $ renderCommand ref "c"
<> show x1_
<> ","
<> show y1_
<> show x2_
<> ","
<> show y2_
<> show x_
<> ","
<> show y_
s
:: CommandPositionReference
-> Number
-> Number
-> Number
-> Number
-> PathCommand
s = renderCommand4Args "s"
q
:: CommandPositionReference
-> Number
-> Number
-> Number
-> Number
-> PathCommand
q = renderCommand4Args "q"
t :: CommandPositionReference -> Number -> Number -> PathCommand
t = renderCommand2Args "t"
a
:: CommandPositionReference
-> Number
-> Number
-> Number
-> CommandArcChoice
-> CommandSweepChoice
-> Number
-> Number
-> PathCommand
a ref rx_ ry_ rot arc sweep x_ y_ = PathCommand $
renderCommand ref "a"
<> show rx_
<> ", "
<> show ry_
<> ", "
<> show rot
<> " "
<> printCommandArcChoice arc
<> " "
<> printCommandSweepChoice sweep
<> " "
<> show x_
<> " "
<>
show y_
z :: PathCommand
z = PathCommand "z"
-- For internal use. Do not export.
renderCommand1Arg :: String -> CommandPositionReference -> Number -> PathCommand
renderCommand1Arg s_ ref a_ = PathCommand $ (renderCommand ref s_) <> show a_
-- For internal use. Do not export.
renderCommand2Args
:: String -> CommandPositionReference -> Number -> Number -> PathCommand
renderCommand2Args s_ ref a_ b =
PathCommand $ (renderCommand ref s_) <> show a_ <> ", " <> show b
-- For internal use. Do not export.
renderCommand4Args
:: String
-> CommandPositionReference
-> Number
-> Number
-> Number
-> Number
-> PathCommand
renderCommand4Args s_ ref a_ b c_ d_ =
PathCommand $ (renderCommand ref s_)
<> show a_
<> ", "
<> show b
<> ", "
<> show c_
<> ", "
<> show d_