-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path_types.py
More file actions
253 lines (202 loc) · 5.98 KB
/
Copy path_types.py
File metadata and controls
253 lines (202 loc) · 5.98 KB
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
from __future__ import annotations
from datetime import datetime
import math
from dataclasses import dataclass
from datetime import timedelta
from decimal import Decimal
from typing import TYPE_CHECKING, Union
if TYPE_CHECKING:
from typing_extensions import Literal
Number = Union[Decimal, float, int]
@dataclass
class Length:
"""
https://developer.mozilla.org/en-US/docs/Web/SVG/Content_type#length
"""
value: Number
unit: Literal["em", "ex", "px", "pt", "pc", "cm", "mm", "in", "%"]
def __str__(self) -> str:
return f"{self.value}{self.unit}"
@dataclass
class PreserveAspectRatio:
"""
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
"""
alignment: Literal[
"none",
"xMinYMin",
"xMidYMin",
"xMaxYMin",
"xMinYMid",
"xMidYMid",
"xMaxYMid",
"xMinYMax",
"xMidYMax",
"xMaxYMax",
] = "xMidYMid"
scale_type: Literal["meet", "slice"] = "meet"
def __str__(self) -> str:
return f"{self.alignment} {self.scale_type}"
@dataclass
class ViewBoxSpec:
"""
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
"""
min_x: Number
min_y: Number
width: Number
height: Number
def __str__(self) -> str:
return f"{self.min_x} {self.min_y} {self.width} {self.height}"
@dataclass
class TimeBezierPoint:
x1: Number
y1: Number
x2: Number
y2: Number
def __post_init__(self) -> None:
assert self.x1 >= 0 and self.x1 <= 1
assert self.y1 >= 0 and self.y1 <= 1
assert self.x2 >= 0 and self.x2 <= 1
assert self.y2 >= 0 and self.y2 <= 1
def __str__(self) -> str:
return f"{self.x1} {self.y1} {self.x2} {self.y2}"
# https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/begin
# https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/end
@dataclass
class SyncbaseValue:
element_id: str
event: Literal["begin", "end"]
offset: timedelta | None
def __str__(self) -> str:
str_value = f"{self.element_id}.{self.event}"
if self.offset is not None:
offset_str = to_clock_value(self.offset)
if not offset_str.startswith("-"):
offset_str = "+" + offset_str
str_value += offset_str
return str_value
@dataclass
class EventValue:
element_id: str | None
event: Literal[
"focus",
"blur",
"focusin",
"focusout",
"DOMActivate",
"auxclick",
"click",
"dblclick",
"mousedown",
"mouseenter",
"mouseleave",
"mousemove",
"mouseout",
"mouseover",
"mouseup",
"wheel",
"beforeinput",
"input",
"keydown",
"keyup",
"compositionstart",
"compositionupdate",
"compositionend",
"load",
"unload",
"abort",
"error",
"select",
"resize",
"scroll",
"beginEvent",
"endEvent",
"repeatEvent",
]
offset: timedelta | None
def __str__(self) -> str:
str_value = ""
if self.element_id is not None:
str_value = f"{self.element_id}."
str_value += f"{self.event}"
if self.offset is not None:
offset_str = to_clock_value(self.offset)
if not offset_str.startswith("-"):
offset_str = "+" + offset_str
str_value += offset_str
return str_value
@dataclass
class RepeatValue:
element_id: str | None
repeat_number: int
offset: timedelta | None
def __str__(self) -> str:
str_value = ""
if self.element_id is not None:
str_value = f"{self.element_id}."
str_value += f"repeat({self.repeat_number})"
if self.offset is not None:
offset_str = to_clock_value(self.offset)
if not offset_str.startswith("-"):
offset_str = "+" + offset_str
str_value += offset_str
return str_value
@dataclass
class AccessKeyValue:
key: str
offset: timedelta | None
def __str__(self) -> str:
str_value = f"accessKey({self.key})"
if self.offset is not None:
offset_str = to_clock_value(self.offset)
if not offset_str.startswith("-"):
offset_str = "+" + offset_str
str_value += offset_str
return str_value
AnimationTimingEvent = Union[
timedelta,
SyncbaseValue,
EventValue,
RepeatValue,
AccessKeyValue,
datetime,
'Literal["indefinite"]'
]
def to_clock_value(delta: timedelta) -> str:
"""Format timedelta as ClockValue SVG type.
https://developer.mozilla.org/en-US/docs/Web/SVG/Guides/Content_type#clock-value
https://svgwg.org/specs/animations/#ClockValueSyntax
"""
seconds = delta.total_seconds()
sign = ""
if seconds < 0:
sign = "-"
seconds = abs(seconds)
partial_seconds = seconds - math.floor(seconds)
seconds = int(seconds)
fraction = ""
if partial_seconds > 0:
fraction = f"{partial_seconds:.6f}".strip("0")
# Format as Timecount-val.
if abs(seconds) < 60:
# The "s" suffix is optional but it's good for readability:
# in JS, time is represented in miliseconds, so
# just a number without a suffix may confuse JS engineers.
return f"{sign}{seconds}{fraction}s"
# Format as Full-clock-val.
minutes, full_seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return f"{sign}{hours}:{minutes:02}:{full_seconds:02}{fraction}"
def to_wallclock_sync_value(time: datetime) -> str:
iso_time = time.isoformat(" ")
return f"wallclock({iso_time})"
@dataclass
class Point:
"""Point for use in Polygon and Polyline elements.
https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/points
"""
x: Number
y: Number
def __str__(self) -> str:
return f"{self.x},{self.y}"