deckkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deckkit/__init__.py +12 -0
- deckkit/_bundled_fonts.py +54 -0
- deckkit/_diagnostics.py +48 -0
- deckkit/_font_assets/SOURCES.txt +9 -0
- deckkit/_font_assets/SourceCodePro-Bold.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-It.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceCodePro-Regular.ttf +0 -0
- deckkit/_font_assets/SourceSans3-Bold.ttf +0 -0
- deckkit/_font_assets/SourceSans3-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceSans3-It.ttf +0 -0
- deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceSans3-Regular.ttf +0 -0
- deckkit/_variable_evaluation.py +82 -0
- deckkit/animation.py +211 -0
- deckkit/annotations.py +161 -0
- deckkit/backends/__init__.py +1 -0
- deckkit/backends/_font_embedding.py +41 -0
- deckkit/backends/_pptx_fonts.py +230 -0
- deckkit/backends/_pptx_shadows.py +241 -0
- deckkit/backends/_pptx_tables.py +251 -0
- deckkit/backends/pptx.py +1225 -0
- deckkit/backends/pptx_animation.py +175 -0
- deckkit/backends/svg.py +527 -0
- deckkit/canvas/__init__.py +40 -0
- deckkit/canvas/_geometry.py +49 -0
- deckkit/canvas/clipping.py +336 -0
- deckkit/canvas/context.py +34 -0
- deckkit/canvas/elements.py +306 -0
- deckkit/canvas/path.py +250 -0
- deckkit/canvas/scene.py +74 -0
- deckkit/canvas/style.py +31 -0
- deckkit/charts/__init__.py +59 -0
- deckkit/charts/axes.py +249 -0
- deckkit/charts/chart.py +244 -0
- deckkit/charts/content.py +132 -0
- deckkit/charts/context.py +45 -0
- deckkit/charts/interval.py +166 -0
- deckkit/charts/legends.py +82 -0
- deckkit/charts/marks.py +410 -0
- deckkit/charts/scales.py +205 -0
- deckkit/citations.py +200 -0
- deckkit/compiler.py +410 -0
- deckkit/context.py +72 -0
- deckkit/deck.py +168 -0
- deckkit/drawables/__init__.py +42 -0
- deckkit/drawables/bounds.py +17 -0
- deckkit/drawables/connector.py +22 -0
- deckkit/drawables/drawable.py +38 -0
- deckkit/drawables/ellipse.py +21 -0
- deckkit/drawables/group.py +10 -0
- deckkit/drawables/image.py +14 -0
- deckkit/drawables/line.py +18 -0
- deckkit/drawables/path.py +78 -0
- deckkit/drawables/rectangle.py +24 -0
- deckkit/drawables/rich_text.py +176 -0
- deckkit/drawables/table.py +42 -0
- deckkit/drawables/text.py +29 -0
- deckkit/effects.py +84 -0
- deckkit/expanded_views.py +561 -0
- deckkit/fonts.py +329 -0
- deckkit/geometry.py +48 -0
- deckkit/identity.py +72 -0
- deckkit/layout.py +398 -0
- deckkit/layouts/__init__.py +20 -0
- deckkit/layouts/_content.py +27 -0
- deckkit/layouts/annotations.py +217 -0
- deckkit/layouts/containers.py +202 -0
- deckkit/layouts/grid.py +299 -0
- deckkit/layouts/modifiers.py +200 -0
- deckkit/layouts/primitives.py +213 -0
- deckkit/layouts/table.py +139 -0
- deckkit/measurement.py +420 -0
- deckkit/paint.py +73 -0
- deckkit/py.typed +0 -0
- deckkit/reconcile.py +119 -0
- deckkit/semantic.py +47 -0
- deckkit/theme.py +364 -0
- deckkit/themes/__init__.py +91 -0
- deckkit/themes/clarity.py +98 -0
- deckkit/tokens.py +226 -0
- deckkit/units.py +73 -0
- deckkit/variables.py +212 -0
- deckkit/view_expansion.py +185 -0
- deckkit/views/__init__.py +100 -0
- deckkit/views/_code_source.py +42 -0
- deckkit/views/_drawing.py +52 -0
- deckkit/views/canvas.py +27 -0
- deckkit/views/code.py +481 -0
- deckkit/views/code_evolution.py +284 -0
- deckkit/views/composite_view.py +19 -0
- deckkit/views/divider.py +38 -0
- deckkit/views/ellipse.py +38 -0
- deckkit/views/grid.py +156 -0
- deckkit/views/hstack.py +43 -0
- deckkit/views/image.py +43 -0
- deckkit/views/layout_view.py +30 -0
- deckkit/views/line.py +31 -0
- deckkit/views/modifier_views.py +281 -0
- deckkit/views/path.py +36 -0
- deckkit/views/primitive_view.py +16 -0
- deckkit/views/pygments_highlighter.py +67 -0
- deckkit/views/rectangle.py +61 -0
- deckkit/views/section_number.py +21 -0
- deckkit/views/semantic/__init__.py +51 -0
- deckkit/views/semantic/before_after.py +109 -0
- deckkit/views/semantic/bullet_graph.py +216 -0
- deckkit/views/semantic/card.py +129 -0
- deckkit/views/semantic/code_block.py +41 -0
- deckkit/views/semantic/diff.py +642 -0
- deckkit/views/semantic/estimate_interval.py +137 -0
- deckkit/views/semantic/figure.py +55 -0
- deckkit/views/semantic/item_list.py +171 -0
- deckkit/views/semantic/metric.py +74 -0
- deckkit/views/semantic/pill.py +69 -0
- deckkit/views/semantic/pipeline.py +94 -0
- deckkit/views/semantic/pipeline_stage.py +59 -0
- deckkit/views/semantic/semantic_view.py +76 -0
- deckkit/views/semantic/sparkline.py +164 -0
- deckkit/views/semantic/table.py +439 -0
- deckkit/views/semantic/terminal.py +361 -0
- deckkit/views/semantic/timeline.py +158 -0
- deckkit/views/slide.py +37 -0
- deckkit/views/slide_number.py +20 -0
- deckkit/views/slides/__init__.py +16 -0
- deckkit/views/slides/section_slide.py +89 -0
- deckkit/views/slides/slide_margins.py +32 -0
- deckkit/views/slides/slide_with_margins.py +22 -0
- deckkit/views/slides/title_and_content.py +49 -0
- deckkit/views/slides/title_and_content_slide.py +55 -0
- deckkit/views/spacer.py +45 -0
- deckkit/views/stack.py +36 -0
- deckkit/views/text.py +142 -0
- deckkit/views/view.py +229 -0
- deckkit/views/vstack.py +31 -0
- deckkit/views/zstack.py +27 -0
- deckkit-0.1.0.dist-info/METADATA +123 -0
- deckkit-0.1.0.dist-info/RECORD +144 -0
- deckkit-0.1.0.dist-info/WHEEL +5 -0
- deckkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/top_level.txt +1 -0
deckkit/fonts.py
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"""Font requests and deterministic discovery of installed font faces."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from collections.abc import Mapping
|
|
8
|
+
from dataclasses import dataclass, replace
|
|
9
|
+
from functools import lru_cache
|
|
10
|
+
from os import PathLike
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from types import MappingProxyType
|
|
13
|
+
from typing import Literal, Protocol
|
|
14
|
+
|
|
15
|
+
from fontTools.ttLib import TTFont, TTLibError
|
|
16
|
+
from PIL import ImageFont
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class Font:
|
|
21
|
+
family: str
|
|
22
|
+
weight: Literal["regular", "bold"] | int = "regular"
|
|
23
|
+
italic: bool = False
|
|
24
|
+
|
|
25
|
+
def __post_init__(self) -> None:
|
|
26
|
+
if not self.family.strip():
|
|
27
|
+
raise ValueError("Font family must not be empty")
|
|
28
|
+
if self.weight not in ("regular", "bold") and (
|
|
29
|
+
type(self.weight) is not int or not 1 <= self.weight <= 1000
|
|
30
|
+
):
|
|
31
|
+
raise ValueError(
|
|
32
|
+
"Font weight must be 'regular', 'bold', or an integer from 1 to 1000"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def numeric_weight(self) -> int:
|
|
37
|
+
if self.weight == "regular":
|
|
38
|
+
return 400
|
|
39
|
+
if self.weight == "bold":
|
|
40
|
+
return 700
|
|
41
|
+
return self.weight
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class PptxTypeface:
|
|
46
|
+
"""The typeface name and style PowerPoint will request for editable text."""
|
|
47
|
+
|
|
48
|
+
name: str
|
|
49
|
+
bold: bool = False
|
|
50
|
+
italic: bool = False
|
|
51
|
+
|
|
52
|
+
def __post_init__(self) -> None:
|
|
53
|
+
if not self.name.strip():
|
|
54
|
+
raise ValueError("PowerPoint typeface must not be empty")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True)
|
|
58
|
+
class FontFile:
|
|
59
|
+
path: str
|
|
60
|
+
index: int = 0
|
|
61
|
+
pptx_typeface: PptxTypeface | None = None
|
|
62
|
+
|
|
63
|
+
def __post_init__(self) -> None:
|
|
64
|
+
if self.index < 0:
|
|
65
|
+
raise ValueError("Font face index must be nonnegative")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@dataclass(frozen=True)
|
|
69
|
+
class EmbeddedFont:
|
|
70
|
+
"""Files for the styles of one typeface used in a PowerPoint export."""
|
|
71
|
+
|
|
72
|
+
typeface: str
|
|
73
|
+
regular: FontFile | str | PathLike[str] | None = None
|
|
74
|
+
bold: FontFile | str | PathLike[str] | None = None
|
|
75
|
+
italic: FontFile | str | PathLike[str] | None = None
|
|
76
|
+
bold_italic: FontFile | str | PathLike[str] | None = None
|
|
77
|
+
|
|
78
|
+
def __post_init__(self) -> None:
|
|
79
|
+
if not self.typeface.strip():
|
|
80
|
+
raise ValueError("Embedded font typeface must not be empty")
|
|
81
|
+
if all(
|
|
82
|
+
face is None
|
|
83
|
+
for face in (self.regular, self.bold, self.italic, self.bold_italic)
|
|
84
|
+
):
|
|
85
|
+
raise ValueError("An embedded font requires at least one face")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass(frozen=True)
|
|
89
|
+
class ResolvedFont:
|
|
90
|
+
path: str
|
|
91
|
+
index: int
|
|
92
|
+
family: str
|
|
93
|
+
style: str
|
|
94
|
+
bold: bool
|
|
95
|
+
italic: bool
|
|
96
|
+
pptx_typeface: PptxTypeface | None = None
|
|
97
|
+
weight: int | None = None
|
|
98
|
+
typographic_family: str | None = None
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def numeric_weight(self) -> int:
|
|
102
|
+
return self.weight if self.weight is not None else (700 if self.bold else 400)
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def powerpoint_typeface(self) -> PptxTypeface:
|
|
106
|
+
return self.pptx_typeface or PptxTypeface(self.family, self.bold, self.italic)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class FontNotFoundError(ValueError):
|
|
110
|
+
pass
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class FontEmbeddingError(ValueError):
|
|
114
|
+
"""A font cannot be packaged for the requested export."""
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class FontResolver(Protocol):
|
|
118
|
+
def resolve(self, source: Font) -> ResolvedFont: ...
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
_STYLE_LINKS = {
|
|
122
|
+
"regular": (False, False),
|
|
123
|
+
"roman": (False, False),
|
|
124
|
+
"normal": (False, False),
|
|
125
|
+
"book": (False, False),
|
|
126
|
+
"bold": (True, False),
|
|
127
|
+
"italic": (False, True),
|
|
128
|
+
"oblique": (False, True),
|
|
129
|
+
"bold italic": (True, True),
|
|
130
|
+
"bold oblique": (True, True),
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@lru_cache(maxsize=2048)
|
|
135
|
+
def _face(path: str, index: int) -> ResolvedFont:
|
|
136
|
+
face = ImageFont.truetype(path, 16, index=index)
|
|
137
|
+
family, style = face.getname()
|
|
138
|
+
if not family or not style:
|
|
139
|
+
raise ValueError(f"Font lacks family/style metadata: {path}")
|
|
140
|
+
normalized = style.casefold()
|
|
141
|
+
default_face = ResolvedFont(
|
|
142
|
+
path,
|
|
143
|
+
index,
|
|
144
|
+
family,
|
|
145
|
+
style,
|
|
146
|
+
"bold" in normalized,
|
|
147
|
+
"italic" in normalized or "oblique" in normalized,
|
|
148
|
+
)
|
|
149
|
+
try:
|
|
150
|
+
with TTFont(path, fontNumber=index, lazy=True) as font:
|
|
151
|
+
if "OS/2" not in font or "name" not in font:
|
|
152
|
+
return default_face
|
|
153
|
+
names = font["name"]
|
|
154
|
+
os2 = font["OS/2"]
|
|
155
|
+
legacy_family = names.getDebugName(1) or family
|
|
156
|
+
italic = bool(os2.fsSelection & 0x0201) or default_face.italic
|
|
157
|
+
bold = bool(os2.fsSelection & 0x0020)
|
|
158
|
+
linked_style = (names.getDebugName(2) or "").casefold()
|
|
159
|
+
linked_bold, linked_italic = _STYLE_LINKS.get(linked_style, (bold, italic))
|
|
160
|
+
return replace(
|
|
161
|
+
default_face,
|
|
162
|
+
bold=bold,
|
|
163
|
+
italic=italic,
|
|
164
|
+
pptx_typeface=PptxTypeface(legacy_family, linked_bold, linked_italic),
|
|
165
|
+
weight=os2.usWeightClass,
|
|
166
|
+
typographic_family=names.getDebugName(16),
|
|
167
|
+
)
|
|
168
|
+
except TTLibError as error:
|
|
169
|
+
raise ValueError(f"Cannot read font metadata: {path}") from error
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _system_directories() -> tuple[Path, ...]:
|
|
173
|
+
home = Path.home()
|
|
174
|
+
if sys.platform == "darwin":
|
|
175
|
+
return (
|
|
176
|
+
home / "Library/Fonts",
|
|
177
|
+
Path("/Library/Fonts"),
|
|
178
|
+
Path("/System/Library/Fonts"),
|
|
179
|
+
)
|
|
180
|
+
if sys.platform == "win32":
|
|
181
|
+
return (
|
|
182
|
+
Path(os.environ.get("LOCALAPPDATA", str(home))) / "Microsoft/Windows/Fonts",
|
|
183
|
+
Path(os.environ.get("WINDIR", "C:/Windows")) / "Fonts",
|
|
184
|
+
)
|
|
185
|
+
return (
|
|
186
|
+
home / ".fonts",
|
|
187
|
+
Path(os.environ.get("XDG_DATA_HOME", str(home / ".local/share"))) / "fonts",
|
|
188
|
+
*(
|
|
189
|
+
Path(p) / "fonts"
|
|
190
|
+
for p in os.environ.get(
|
|
191
|
+
"XDG_DATA_DIRS", "/usr/local/share:/usr/share"
|
|
192
|
+
).split(":")
|
|
193
|
+
),
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class SystemFontResolver:
|
|
198
|
+
"""Search standard font directories, or an explicitly supplied directory set.
|
|
199
|
+
|
|
200
|
+
Exact family, weight and italic face matching; no substitution,
|
|
201
|
+
synthetic styles or variable-axis instancing. First directory wins ties.
|
|
202
|
+
"""
|
|
203
|
+
|
|
204
|
+
def __init__(self, directories: tuple[Path, ...] | None = None) -> None:
|
|
205
|
+
self.directories = (
|
|
206
|
+
directories if directories is not None else _system_directories()
|
|
207
|
+
)
|
|
208
|
+
self._faces: tuple[ResolvedFont, ...] | None = None
|
|
209
|
+
self._cache: dict[Font, ResolvedFont] = {}
|
|
210
|
+
|
|
211
|
+
def _discover(self) -> tuple[ResolvedFont, ...]:
|
|
212
|
+
if self._faces is None:
|
|
213
|
+
faces = []
|
|
214
|
+
for directory in self.directories:
|
|
215
|
+
for path in sorted(directory.rglob("*")):
|
|
216
|
+
if path.suffix.lower() not in (".ttf", ".otf", ".ttc", ".otc"):
|
|
217
|
+
continue
|
|
218
|
+
count = 256 if path.suffix.lower() in (".ttc", ".otc") else 1
|
|
219
|
+
for index in range(count):
|
|
220
|
+
try:
|
|
221
|
+
faces.append(_face(str(path.resolve()), index))
|
|
222
|
+
except (OSError, ValueError):
|
|
223
|
+
break
|
|
224
|
+
self._faces = tuple(faces)
|
|
225
|
+
return self._faces
|
|
226
|
+
|
|
227
|
+
def resolve(self, source: Font) -> ResolvedFont:
|
|
228
|
+
if source in self._cache:
|
|
229
|
+
return self._cache[source]
|
|
230
|
+
if isinstance(source, Font):
|
|
231
|
+
matches = [
|
|
232
|
+
face
|
|
233
|
+
for face in self._discover()
|
|
234
|
+
if face.italic == source.italic and _matches_face(source, face)
|
|
235
|
+
]
|
|
236
|
+
if not matches:
|
|
237
|
+
raise FontNotFoundError(
|
|
238
|
+
f"Font {source.family!r} ({source.weight}, italic={source.italic}) "
|
|
239
|
+
"is not installed in the searched directories. Install it, configure FileFontResolver, "
|
|
240
|
+
"or supply a FontResolver. No font was substituted."
|
|
241
|
+
)
|
|
242
|
+
result = matches[0]
|
|
243
|
+
else:
|
|
244
|
+
raise TypeError('Expected Font("Family")')
|
|
245
|
+
self._cache[source] = result
|
|
246
|
+
return result
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _matches_face(request: Font, face: ResolvedFont) -> bool:
|
|
250
|
+
family = request.family.casefold()
|
|
251
|
+
typeface = face.powerpoint_typeface
|
|
252
|
+
legacy_family = typeface.name.casefold()
|
|
253
|
+
typographic_family = (face.typographic_family or face.family).casefold()
|
|
254
|
+
if request.numeric_weight == face.numeric_weight:
|
|
255
|
+
return family in (face.family.casefold(), typographic_family, legacy_family)
|
|
256
|
+
return (
|
|
257
|
+
family == legacy_family
|
|
258
|
+
and legacy_family != typographic_family
|
|
259
|
+
and request.numeric_weight == (700 if typeface.bold else 400)
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def _font_key(request: Font) -> tuple[str, int, bool]:
|
|
264
|
+
return request.family, request.numeric_weight, request.italic
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
class FileFontResolver:
|
|
268
|
+
"""Resolve named requests using pinned files, without searching system fonts.
|
|
269
|
+
|
|
270
|
+
Mapping keys may be logical aliases. Exports default to the loaded face's
|
|
271
|
+
actual metadata. FontFile can specify a different PowerPoint typeface.
|
|
272
|
+
"""
|
|
273
|
+
|
|
274
|
+
def __init__(self, files: Mapping[Font, FontFile | str]) -> None:
|
|
275
|
+
self.files = MappingProxyType(
|
|
276
|
+
{
|
|
277
|
+
key: FontFile(value) if isinstance(value, str) else value
|
|
278
|
+
for key, value in files.items()
|
|
279
|
+
}
|
|
280
|
+
)
|
|
281
|
+
self._files: dict[tuple[str, int, bool], FontFile] = {}
|
|
282
|
+
for request, location in self.files.items():
|
|
283
|
+
key = _font_key(request)
|
|
284
|
+
if self._files.setdefault(key, location) != location:
|
|
285
|
+
raise ValueError(f"Multiple file mappings for {request!r}")
|
|
286
|
+
|
|
287
|
+
def resolve(self, source: Font) -> ResolvedFont:
|
|
288
|
+
try:
|
|
289
|
+
location = self._files[_font_key(source)]
|
|
290
|
+
except KeyError:
|
|
291
|
+
raise FontNotFoundError(
|
|
292
|
+
f"No file mapping for {source!r}; add it to FileFontResolver"
|
|
293
|
+
) from None
|
|
294
|
+
try:
|
|
295
|
+
face = _face(
|
|
296
|
+
str(Path(location.path).expanduser().resolve()), location.index
|
|
297
|
+
)
|
|
298
|
+
except (OSError, ValueError) as error:
|
|
299
|
+
raise FontNotFoundError(
|
|
300
|
+
f"Cannot load {location.path!r}, face {location.index}"
|
|
301
|
+
) from error
|
|
302
|
+
if location.pptx_typeface is not None:
|
|
303
|
+
return replace(face, pptx_typeface=location.pptx_typeface)
|
|
304
|
+
return face
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
class StandInFontResolver(FileFontResolver):
|
|
308
|
+
"""Measure explicit substitutions and preserve the requested PPTX typeface."""
|
|
309
|
+
|
|
310
|
+
def __init__(
|
|
311
|
+
self,
|
|
312
|
+
files: Mapping[Font, FontFile | str],
|
|
313
|
+
*,
|
|
314
|
+
fallback: FontResolver | None = None,
|
|
315
|
+
) -> None:
|
|
316
|
+
from ._bundled_fonts import DefaultFontResolver
|
|
317
|
+
|
|
318
|
+
super().__init__(files)
|
|
319
|
+
self._fallback = fallback if fallback is not None else DefaultFontResolver()
|
|
320
|
+
|
|
321
|
+
def resolve(self, source: Font) -> ResolvedFont:
|
|
322
|
+
location = self._files.get(_font_key(source))
|
|
323
|
+
if location is None:
|
|
324
|
+
return self._fallback.resolve(source)
|
|
325
|
+
face = super().resolve(source)
|
|
326
|
+
typeface = location.pptx_typeface or PptxTypeface(
|
|
327
|
+
source.family, bold=source.numeric_weight == 700, italic=source.italic
|
|
328
|
+
)
|
|
329
|
+
return replace(face, pptx_typeface=typeface)
|
deckkit/geometry.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from math import isfinite
|
|
5
|
+
from typing import ClassVar
|
|
6
|
+
|
|
7
|
+
from .units import Length, require_length
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class Point:
|
|
12
|
+
x: Length
|
|
13
|
+
y: Length
|
|
14
|
+
|
|
15
|
+
def __post_init__(self) -> None:
|
|
16
|
+
require_length(self.x)
|
|
17
|
+
require_length(self.y)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class RelativePoint:
|
|
22
|
+
x: float
|
|
23
|
+
y: float
|
|
24
|
+
|
|
25
|
+
center: ClassVar[RelativePoint]
|
|
26
|
+
top: ClassVar[RelativePoint]
|
|
27
|
+
bottom: ClassVar[RelativePoint]
|
|
28
|
+
left: ClassVar[RelativePoint]
|
|
29
|
+
right: ClassVar[RelativePoint]
|
|
30
|
+
top_left: ClassVar[RelativePoint]
|
|
31
|
+
top_right: ClassVar[RelativePoint]
|
|
32
|
+
bottom_left: ClassVar[RelativePoint]
|
|
33
|
+
bottom_right: ClassVar[RelativePoint]
|
|
34
|
+
|
|
35
|
+
def __post_init__(self) -> None:
|
|
36
|
+
if not isfinite(self.x) or not isfinite(self.y):
|
|
37
|
+
raise ValueError("RelativePoint coordinates must be finite")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
RelativePoint.center = RelativePoint(0.5, 0.5)
|
|
41
|
+
RelativePoint.top = RelativePoint(0.5, 0)
|
|
42
|
+
RelativePoint.bottom = RelativePoint(0.5, 1)
|
|
43
|
+
RelativePoint.left = RelativePoint(0, 0.5)
|
|
44
|
+
RelativePoint.right = RelativePoint(1, 0.5)
|
|
45
|
+
RelativePoint.top_left = RelativePoint(0, 0)
|
|
46
|
+
RelativePoint.top_right = RelativePoint(1, 0)
|
|
47
|
+
RelativePoint.bottom_left = RelativePoint(0, 1)
|
|
48
|
+
RelativePoint.bottom_right = RelativePoint(1, 1)
|
deckkit/identity.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Typed occurrence identities and lexical key scopes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any, Union
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class RootSegment:
|
|
12
|
+
name: str
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class StructuralSegment:
|
|
17
|
+
slot: str | int
|
|
18
|
+
view_type: type[Any]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class KeySegment:
|
|
23
|
+
key: str
|
|
24
|
+
view_type: type[Any]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
IdentitySegment = Union[RootSegment, StructuralSegment, KeySegment]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class ViewIdentity:
|
|
32
|
+
segments: tuple[IdentitySegment, ...]
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def root(cls, name: str) -> ViewIdentity:
|
|
36
|
+
return cls((RootSegment(name),))
|
|
37
|
+
|
|
38
|
+
def child(self, slot: str | int, view_type: type[Any]) -> ViewIdentity:
|
|
39
|
+
return ViewIdentity((*self.segments, StructuralSegment(slot, view_type)))
|
|
40
|
+
|
|
41
|
+
def keyed(self, key: str, view_type: type[Any]) -> ViewIdentity:
|
|
42
|
+
return ViewIdentity((*self.segments, KeySegment(key, view_type)))
|
|
43
|
+
|
|
44
|
+
def __str__(self) -> str:
|
|
45
|
+
parts: list[str] = []
|
|
46
|
+
for segment in self.segments:
|
|
47
|
+
if isinstance(segment, RootSegment):
|
|
48
|
+
parts.append(segment.name)
|
|
49
|
+
continue
|
|
50
|
+
typename = (
|
|
51
|
+
segment.view_type.__module__ + "." + segment.view_type.__qualname__
|
|
52
|
+
)
|
|
53
|
+
if isinstance(segment, StructuralSegment):
|
|
54
|
+
parts.append(f"{segment.slot}:{typename}")
|
|
55
|
+
else:
|
|
56
|
+
keyed = f"key:{quote(segment.key, safe='')}:{typename}"
|
|
57
|
+
if len(parts) == 1 and isinstance(self.segments[0], RootSegment):
|
|
58
|
+
parts[0] = keyed
|
|
59
|
+
else:
|
|
60
|
+
parts.append(keyed)
|
|
61
|
+
return "/".join(parts)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class KeyScope:
|
|
66
|
+
owner: ViewIdentity
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass(frozen=True)
|
|
70
|
+
class ScopedKey:
|
|
71
|
+
scope: KeyScope
|
|
72
|
+
key: str
|