This repository has been archived by the owner on Sep 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Internal.hs
287 lines (254 loc) · 6.14 KB
/
Internal.hs
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
{-# OPTIONS_HADDOCK hide #-}
module Text.Blaze.Svg.Internal where
import Control.Monad.State
import Data.Monoid (mappend, mempty)
import Text.Blaze
-- | Type to represent an SVG document fragment.
type Svg = Markup
-- | Type to accumulate an SVG path.
type Path = State AttributeValue ()
-- | Construct SVG path values using path instruction combinators.
-- See simple example below of how you can use @mkPath@ to
-- specify a path using the path instruction combinators
-- that are included as part of the same module.
--
-- More information available at: <http://www.w3.org/TR/SVG/paths.html>
--
-- > import Text.Blaze.Svg11 ((!), mkPath, l, m)
-- > import qualified Text.Blaze.Svg11 as S
-- > import qualified Text.Blaze.Svg11.Attributes as A
-- >
-- > svgDoc :: S.Svg
-- > svgDoc = S.docTypeSvg ! A.version "1.1" ! A.width "150" ! A.height "100" $ do
-- > S.path ! A.d makeSimplePath
-- >
-- > makeSimplePath :: S.AttributeValue
-- > makeSimplePath = mkPath do
-- > l 2 3
-- > m 4 5
mkPath :: Path -> AttributeValue
mkPath path = snd $ runState path mempty
appendToPath :: [String] -> Path
appendToPath = modify . flip mappend . toValue . join
-- | Moveto
m :: Show a => a -> a -> Path
m x y = appendToPath
[ "M "
, show x, ",", show y
, " "
]
-- | Moveto (relative)
mr :: Show a => a -> a -> Path
mr dx dy = appendToPath
[ "m "
, show dx, ",", show dy
, " "
]
-- | ClosePath
z :: Path
z = modify (`mappend` toValue "Z")
-- | Lineto
l :: Show a => a -> a -> Path
l x y = appendToPath
[ "L "
, show x, ",", show y
, " "
]
-- | Lineto (relative)
lr :: Show a => a -> a -> Path
lr dx dy = appendToPath
[ "l "
, show dx, ",", show dy
, " "
]
-- | Horizontal lineto
h :: Show a => a -> Path
h x = appendToPath
[ "H "
, show x
, " "
]
-- | Horizontal lineto (relative)
hr :: Show a => a -> Path
hr dx = appendToPath
[ "h "
, show dx
, " "
]
-- | Vertical lineto
v :: Show a => a -> Path
v y = appendToPath
[ "V "
, show y
, " "
]
-- | Vertical lineto (relative)
vr :: Show a => a -> Path
vr dy = appendToPath
[ "v "
, show dy
, " "
]
-- | Cubic Bezier curve
c :: Show a => a -> a -> a -> a -> a -> a -> Path
c c1x c1y c2x c2y x y = appendToPath
[ "C "
, show c1x, ",", show c1y
, " "
, show c2x, ",", show c2y
, " "
, show x, " ", show y
]
-- | Cubic Bezier curve (relative)
cr :: Show a => a -> a -> a -> a -> a -> a -> Path
cr dc1x dc1y dc2x dc2y dx dy = appendToPath
[ "c "
, show dc1x, ",", show dc1y
, " "
, show dc2x, ",", show dc2y
, " "
, show dx, " ", show dy
]
-- | Smooth Cubic Bezier curve
s :: Show a => a -> a -> a -> a -> Path
s c2x c2y x y = appendToPath
[ "S "
, show c2x, ",", show c2y
, " "
, show x, ",", show y
, " "
]
-- | Smooth Cubic Bezier curve (relative)
sr :: Show a => a -> a -> a -> a -> Path
sr dc2x dc2y dx dy = appendToPath
[ "s "
, show dc2x, ",", show dc2y
, " "
, show dx, ",", show dy
, " "
]
-- | Quadratic Bezier curve
q :: Show a => a -> a -> a -> a -> Path
q cx cy x y = appendToPath
[ "Q "
, show cx, ",", show cy
, " "
, show x, ",", show y
, " "
]
-- | Quadratic Bezier curve (relative)
qr :: Show a => a -> a -> a -> a -> Path
qr dcx dcy dx dy = appendToPath
[ "q "
, show dcx, ",", show dcy
, " "
, show dx, ",", show dy
, " "
]
-- | Smooth Quadratic Bezier curve
t :: Show a => a -> a -> Path
t x y = appendToPath
[ "T "
, " "
, show x, ",", show y
, " "
]
-- | Smooth Quadratic Bezier curve (relative)
tr :: Show a => a -> a -> Path
tr x y = appendToPath
[ "t "
, " "
, show x, ",", show y
, " "
]
-- | Elliptical Arc (absolute)
aAbs
:: Show a
=> a -- ^ Radius in the x-direction
-> a -- ^ Radius in the y-direction
-> Int -- ^ The rotation of the arc's x-axis compared to the normal x-axis
-> Int -- ^ Draw the smaller or bigger arc satisfying the start point
-> Int -- ^ To mirror or not
-> a -- ^ The x-coordinate of the end point
-> a -- ^ The y-coordinate of the end point
-> Path
aAbs rx ry xAxisRotation largeArcFlag sweepFlag x y = appendToPath
[ "A "
, show rx, ",", show ry, " "
, show xAxisRotation, " "
, show largeArcFlag, ",", show sweepFlag, " "
, show x, ",", show y, " "
]
-- | Elliptical Arc (relative)
ar
:: Show a
=> a -- ^ Radius in the x-direction
-> a -- ^ Radius in the y-direction
-> Int -- ^ The rotation of the arc's x-axis compared to the normal x-axis
-> Int -- ^ Draw the smaller or bigger arc satisfying the start point
-> Int -- ^ To mirror or not
-> a -- ^ The x-coordinate of the end point
-> a -- ^ The y-coordinate of the end point
-> Path
ar rx ry xAxisRotation largeArcFlag sweepFlag x y = appendToPath
[ "a "
, show rx, ",", show ry, " "
, show xAxisRotation, " "
, show largeArcFlag, ",", show sweepFlag, " "
, show x, ",", show y, " "
]
-- | Specifies a translation by @x@ and @y@
translate :: Show a => a -> a -> AttributeValue
translate x y = toValue . join $
[ "translate("
, show x, " ", show y
, ")"
]
-- | Specifies a scale operation by @x@ and @y@
scale :: Show a => a -> a -> AttributeValue
scale x y = toValue . join $
[ "scale("
, show x, " ", show y
, ")"
]
-- | Specifies a rotation by @rotate-angle@ degrees
rotate :: Show a => a -> AttributeValue
rotate rotateAngle = toValue . join $
[ "rotate("
, show rotateAngle
, ")"
]
-- | Specifies a rotation by @rotate-angle@ degrees about the given time @rx,ry@
rotateAround :: Show a => a -> a -> a -> AttributeValue
rotateAround rotateAngle rx ry = toValue . join $
[ "rotate("
, show rotateAngle, ","
, show rx, ",", show ry
, ")"
]
-- | Skew tansformation along x-axis
skewX :: Show a => a -> AttributeValue
skewX skewAngle = toValue . join $
[ "skewX("
, show skewAngle
, ")"
]
-- | Skew tansformation along y-axis
skewY :: Show a => a -> AttributeValue
skewY skewAngle = toValue . join $
[ "skewY("
, show skewAngle
, ")"
]
-- | Specifies a transform in the form of a transformation matrix
matrix :: Show a => a -> a -> a -> a -> a -> a -> AttributeValue
matrix a b c_ d e f = toValue . join $
[ "matrix("
, show a, ","
, show b, ","
, show c_, ","
, show d, ","
, show e, ","
, show f
, ")"
]