-
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
-- Copyright 2026 Shota FUJI
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
--
-- SPDX-License-Identifier: MPL-2.0
module Svg.Path exposing (LargeArcFlag(..), PathCommand(..), PointMode(..), SweepFlag(..), d)
import Svg
import Svg.Attributes
type PointMode
= Relative
| Absolute
type alias PointXY =
( Float, Float )
pointXY : PointXY -> String
pointXY ( x, y ) =
String.fromFloat x ++ "," ++ String.fromFloat y
type alias PointX =
Float
type alias PointY =
Float
type LargeArcFlag
= LargeArc
| SmallArc
largeArcFlag : LargeArcFlag -> String
largeArcFlag flag =
case flag of
LargeArc ->
"1"
SmallArc ->
"0"
type SweepFlag
= Clockwise
| Counterclockwise
sweepFlag : SweepFlag -> String
sweepFlag flag =
case flag of
Clockwise ->
"1"
Counterclockwise ->
"0"
type alias EllipticalArcCurveParameters =
{ rx : Float
, ry : Float
, angle : Float
, largeArcFlag : LargeArcFlag
, sweepFlag : SweepFlag
, x : Float
, y : Float
}
ellipticalArcCurveParamsToString : EllipticalArcCurveParameters -> String
ellipticalArcCurveParamsToString params =
String.fromFloat params.rx
++ " "
++ String.fromFloat params.ry
++ " "
++ String.fromFloat params.angle
++ " "
++ largeArcFlag params.largeArcFlag
++ " "
++ sweepFlag params.sweepFlag
++ " "
++ String.fromFloat params.x
++ ","
++ String.fromFloat params.y
type PathCommand
= MoveTo PointMode PointXY
| LineTo PointMode PointXY
| HorizontalLineTo PointMode PointX
| VerticalLineTo PointMode PointY
| CubicBezierCurve PointMode ( PointXY, PointXY, PointXY ) (List ( PointXY, PointXY, PointXY ))
| SmoothCubicBezierCurve PointMode ( PointXY, PointXY ) (List ( PointXY, PointXY ))
| QuadraticBezierCurve PointMode ( PointXY, PointXY ) (List ( PointXY, PointXY ))
| SmoothQuadraticBezierCurve PointMode PointXY (List PointXY)
| EllipticalArcCurve PointMode EllipticalArcCurveParameters (List EllipticalArcCurveParameters)
| ClosePath
pointsToString2 : ( PointXY, PointXY ) -> String
pointsToString2 ( p1, p2 ) =
pointXY p1 ++ " " ++ pointXY p2
pointsToString3 : ( PointXY, PointXY, PointXY ) -> String
pointsToString3 ( p1, p2, p3 ) =
pointXY p1 ++ " " ++ pointXY p2 ++ " " ++ pointXY p3
pathCommandToString : PathCommand -> String
pathCommandToString command =
case command of
MoveTo mode p ->
let
op =
case mode of
Absolute ->
"M"
Relative ->
"m"
in
op ++ " " ++ pointXY p
LineTo mode p ->
let
op =
case mode of
Absolute ->
"L"
Relative ->
"l"
in
op ++ " " ++ pointXY p
HorizontalLineTo mode x ->
let
op =
case mode of
Absolute ->
"H"
Relative ->
"h"
in
op ++ " " ++ String.fromFloat x
VerticalLineTo mode y ->
let
op =
case mode of
Absolute ->
"V"
Relative ->
"v"
in
op ++ " " ++ String.fromFloat y
CubicBezierCurve mode ps params ->
let
op =
case mode of
Absolute ->
"C"
Relative ->
"c"
in
(op
:: pointsToString3 ps
:: (params |> List.map pointsToString3 |> List.foldr (::) [])
)
|> String.join " "
SmoothCubicBezierCurve mode ps params ->
let
op =
case mode of
Absolute ->
"S"
Relative ->
"S"
in
(op
:: pointsToString2 ps
:: (params |> List.map pointsToString2 |> List.foldr (::) [])
)
|> String.join " "
QuadraticBezierCurve mode ps params ->
let
op =
case mode of
Absolute ->
"Q"
Relative ->
"q"
in
(op
:: pointsToString2 ps
:: (params |> List.map pointsToString2 |> List.foldr (::) [])
)
|> String.join " "
SmoothQuadraticBezierCurve mode p params ->
let
op =
case mode of
Absolute ->
"T"
Relative ->
"t"
in
(op
:: pointXY p
:: (params |> List.map pointXY |> List.foldr (::) [])
)
|> String.join " "
EllipticalArcCurve mode param params ->
let
op =
case mode of
Absolute ->
"A"
Relative ->
"a"
in
(op
:: ellipticalArcCurveParamsToString param
:: (params |> List.map ellipticalArcCurveParamsToString |> List.foldr (::) [])
)
|> String.join " "
ClosePath ->
"Z"
d : List PathCommand -> Svg.Attribute msg
d commands =
Svg.Attributes.d
(commands
|> List.map pathCommandToString
|> String.join " "
)