-
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
-- Copyright 2026 Shota FUJI
--
-- This source code and the design it expresses are licensed under
-- the Creative Commons Attribution 4.0 International License.
--
-- SPDX-License-Identifier: CC-BY-4.0
module Models.PWS01.Parameters exposing (..)
import Dict exposing (Dict)
import Length exposing (Length, mm, toMM)
import Models.PWS01.Parameters.Key as Key exposing (Key(..))
import Preferences exposing (DictKeyMode(..))
type TipStyle
= Round
| Pointed Int
tipStyleToString : TipStyle -> String
tipStyleToString shape =
case shape of
Round ->
"round"
Pointed _ ->
"pointed"
tipStyleToCompactString : TipStyle -> String
tipStyleToCompactString style =
case style of
Round ->
"r"
Pointed _ ->
"p"
type alias BuckleHole =
-- Distance between a base hole (center hole) and case-side strap end.
{ distance : Length
-- Adjustment level. The number of holes will be `1 + adjustments * 2`.
, adjustments : Int
-- Center-to-center.
, interval : Length
, diameter : Length
}
{-| Parameters for the long piece, which has buckle holes.
This does not have to be longer than `ShortPiece`.
-}
type alias LongPiece =
{ length : Length
, tip : TipStyle
, buckleHole : BuckleHole
}
type alias FixedLoop =
{ width : Length
, length : Length
}
type alias FreeLoop =
{ width : Length
, length : Length
, overlap : Length
}
type LoopStyle
= Simple
| Folded
loopStyleToString : LoopStyle -> String
loopStyleToString style =
case style of
Simple ->
"simple"
Folded ->
"folded"
loopStyleToCompactString : LoopStyle -> String
loopStyleToCompactString style =
case style of
Simple ->
"s"
Folded ->
"f"
type alias Loops =
{ fixed : Maybe FixedLoop
, free : Maybe FreeLoop
, style : LoopStyle
}
{-| Parameters for the short piece, which has clasp at the end.
This does not have to be shorter than `LongPiece`.
-}
type alias ShortPiece =
{ length : Length
, loops : Loops
}
type CanvasSize
= A4
canvasSizeDimension : CanvasSize -> ( Length, Length )
canvasSizeDimension size =
case size of
A4 ->
( mm 210, mm 297 )
type ColorSchema
= BlackOnWhite
| WhiteOnBlack
colorSchemaToString : ColorSchema -> String
colorSchemaToString schema =
case schema of
BlackOnWhite ->
"black-on-white"
WhiteOnBlack ->
"white-on-black"
colorSchemaToCompactString : ColorSchema -> String
colorSchemaToCompactString schema =
case schema of
BlackOnWhite ->
"w"
WhiteOnBlack ->
"b"
type alias Rendering =
{ size : CanvasSize
, margin : Length
, gap : Length
, lineWidth : Length
, colorSchema : ColorSchema
, qrCode : Bool
}
type Profile
= Straight
| Tapered Length
profileKind : Profile -> String
profileKind profile =
case profile of
Straight ->
"straight"
Tapered _ ->
"tapered"
profileToCompactString : Profile -> String
profileToCompactString profile =
case profile of
Straight ->
"s"
Tapered _ ->
"t"
type alias Parameters =
{ shoulderWidth : Length
, longPiece : LongPiece
, shortPiece : ShortPiece
, paddingOffset : Length
, profile : Profile
, rendering : Rendering
}
defaultFixedLoop : FixedLoop
defaultFixedLoop =
{ length = mm 55
, width = mm 5
}
defaultFreeLoop : FreeLoop
defaultFreeLoop =
{ width = mm 5
, length = mm 55
, overlap = mm 5
}
defaultTipSharpness : Int
defaultTipSharpness =
25
default : Parameters
default =
{ shoulderWidth = mm 20
, longPiece =
{ length = mm 112
, tip = Pointed defaultTipSharpness
, buckleHole =
{ distance = mm 70
, adjustments = 3
, interval = mm 6
, diameter = mm 2
}
}
, shortPiece =
{ length = mm 80
, loops =
{ fixed = Just defaultFixedLoop
, free = Just defaultFreeLoop
, style = Simple
}
}
, paddingOffset = mm 5
, profile = Straight
, rendering =
{ size = A4
, margin = mm 3
, gap = mm 10
, lineWidth = mm 0.2
, colorSchema = BlackOnWhite
, qrCode = False
}
}
type alias ParametersDict =
Dict String String
toDict : DictKeyMode -> Parameters -> ParametersDict
toDict mode params =
let
toString =
case mode of
Regular ->
Key.toString
Compact ->
Key.toCompactID
in
[ ( "version", "1.0" )
, ( toString TipStyle
, case mode of
Regular ->
tipStyleToString params.longPiece.tip
Compact ->
tipStyleToCompactString params.longPiece.tip
)
, ( toString ShoulderWidth, String.fromFloat (toMM params.shoulderWidth) )
, ( toString PaddingOffset, String.fromFloat (toMM params.paddingOffset) )
, ( toString LongPieceLength, String.fromFloat (toMM params.longPiece.length) )
, ( toString LoopStyle
, case mode of
Regular ->
loopStyleToString params.shortPiece.loops.style
Compact ->
loopStyleToCompactString params.shortPiece.loops.style
)
, ( toString ShortPieceLength, String.fromFloat (toMM params.shortPiece.length) )
, ( toString CanvasMargin, String.fromFloat (toMM params.rendering.margin) )
, ( toString LineWidth, String.fromFloat (toMM params.rendering.lineWidth) )
, ( toString ColorSchema
, case mode of
Regular ->
colorSchemaToString params.rendering.colorSchema
Compact ->
colorSchemaToCompactString params.rendering.colorSchema
)
, ( toString BuckleHoleAdjustments, String.fromInt params.longPiece.buckleHole.adjustments )
, ( toString BuckleHoleDistance, String.fromFloat (toMM params.longPiece.buckleHole.distance) )
, ( toString BuckleHoleInterval, String.fromFloat (toMM params.longPiece.buckleHole.interval) )
, ( toString BuckleHoleDiameter, String.fromFloat (toMM params.longPiece.buckleHole.diameter) )
, ( toString Profile
, case mode of
Regular ->
profileKind params.profile
Compact ->
profileToCompactString params.profile
)
]
|> (++)
(case params.longPiece.tip of
Round ->
[]
Pointed sharpness ->
[ ( toString TipSharpness, String.fromInt sharpness ) ]
)
|> (++)
(case params.profile of
Straight ->
[]
Tapered to ->
[ ( toString TaperTo, String.fromFloat (toMM to) ) ]
)
|> (++)
(case params.shortPiece.loops.fixed of
Just fixed ->
[ ( toString HasFixedLoop, "" )
, ( toString FixedLoopWidth, String.fromFloat (toMM fixed.width) )
, ( toString FixedLoopLength, String.fromFloat (toMM fixed.length) )
]
Nothing ->
[]
)
|> (++)
(case params.shortPiece.loops.free of
Just free ->
[ ( toString HasFreeLoop, "" )
, ( toString FreeLoopWidth, String.fromFloat (toMM free.width) )
, ( toString FreeLoopLength, String.fromFloat (toMM free.length) )
, ( toString FreeLoopOverlap, String.fromFloat (toMM free.overlap) )
]
Nothing ->
[]
)
|> (++)
(if params.rendering.qrCode then
[ ( toString QRCode, "" ) ]
else
[]
)
|> Dict.fromList
{-| Fields under `Maybe` record may be empty when the parsed parent is
`Nothing`. These values sets default values for those fields.
-}
fallbackValues : DictKeyMode -> ParametersDict
fallbackValues mode =
let
toString =
case mode of
Regular ->
Key.toString
Compact ->
Key.toCompactID
in
[ ( toString TaperTo, "18" )
, ( toString TipSharpness, String.fromInt defaultTipSharpness )
, ( toString FixedLoopWidth, String.fromFloat (toMM defaultFixedLoop.width) )
, ( toString FixedLoopLength, String.fromFloat (toMM defaultFixedLoop.length) )
, ( toString FreeLoopWidth, String.fromFloat (toMM defaultFreeLoop.width) )
, ( toString FreeLoopLength, String.fromFloat (toMM defaultFreeLoop.length) )
, ( toString FreeLoopOverlap, String.fromFloat (toMM defaultFreeLoop.overlap) )
]
|> Dict.fromList
getKey : Key -> ParametersDict -> Maybe String
getKey key fields =
case Dict.get (Key.toCompactID key) fields of
Just value ->
Just value
Nothing ->
Dict.get (Key.toString key) fields
hasKey : Key -> ParametersDict -> Bool
hasKey key fields =
not (getKey key fields == Nothing)