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
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""Prepare editable code snapshots for a presentation build."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections import defaultdict
|
|
6
|
+
from collections.abc import Iterator, Mapping, Sequence
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from difflib import SequenceMatcher
|
|
9
|
+
from types import MappingProxyType
|
|
10
|
+
from typing import Literal
|
|
11
|
+
|
|
12
|
+
from ..animation import Animation, FallbackPolicy, Transition
|
|
13
|
+
from ._code_source import _code_text, _highlight_lines, _source_lines
|
|
14
|
+
from .code import Code, CodeHighlighter, CodeLine, CodeMark, _scope
|
|
15
|
+
|
|
16
|
+
_Change = Literal["added", "edited"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class _Line:
|
|
21
|
+
source: CodeLine
|
|
22
|
+
key: str
|
|
23
|
+
change: _Change | None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class _Version:
|
|
28
|
+
index: int
|
|
29
|
+
lines: tuple[_Line, ...]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True, init=False)
|
|
33
|
+
class CodeEvolution:
|
|
34
|
+
"""Prepare named code versions once, then select them from slide state.
|
|
35
|
+
|
|
36
|
+
Pass structured versions as pairs of a name and a sequence of CodeLine
|
|
37
|
+
values. A repeated explicit line key identifies the same line across builds.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
language: str | None
|
|
41
|
+
_versions: Mapping[str, _Version]
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
*versions: tuple[str, Sequence[CodeLine]],
|
|
46
|
+
language: str | None = None,
|
|
47
|
+
) -> None:
|
|
48
|
+
_scope(language, "CodeEvolution.language")
|
|
49
|
+
if not versions:
|
|
50
|
+
raise ValueError("CodeEvolution requires at least one version")
|
|
51
|
+
|
|
52
|
+
prepared: dict[str, _Version] = {}
|
|
53
|
+
previous: tuple[_Line, ...] = ()
|
|
54
|
+
for index, (name, source) in enumerate(versions):
|
|
55
|
+
if not isinstance(name, str) or not name.strip() or name in prepared:
|
|
56
|
+
raise ValueError(
|
|
57
|
+
"CodeEvolution version names must be nonempty and unique"
|
|
58
|
+
)
|
|
59
|
+
lines = tuple(source)
|
|
60
|
+
if not all(isinstance(line, CodeLine) for line in lines):
|
|
61
|
+
raise TypeError("CodeEvolution versions must contain CodeLine values")
|
|
62
|
+
keys = _explicit_keys(lines)
|
|
63
|
+
matches = _match_lines(tuple(line.source for line in previous), lines, keys)
|
|
64
|
+
current = []
|
|
65
|
+
for position, line in enumerate(lines):
|
|
66
|
+
prior = previous[matches[position]] if position in matches else None
|
|
67
|
+
change: _Change | None = None
|
|
68
|
+
if prior is None:
|
|
69
|
+
key = (
|
|
70
|
+
f"explicit:{line.key}"
|
|
71
|
+
if line.key is not None
|
|
72
|
+
else f"auto:{index}:{position}"
|
|
73
|
+
)
|
|
74
|
+
if index:
|
|
75
|
+
change = "added"
|
|
76
|
+
else:
|
|
77
|
+
key = prior.key
|
|
78
|
+
if _body(prior.source) != _body(line):
|
|
79
|
+
change = "edited"
|
|
80
|
+
current.append(_Line(line, key, change))
|
|
81
|
+
previous = tuple(current)
|
|
82
|
+
prepared[name] = _Version(index, previous)
|
|
83
|
+
object.__setattr__(self, "language", language)
|
|
84
|
+
object.__setattr__(self, "_versions", MappingProxyType(prepared))
|
|
85
|
+
|
|
86
|
+
@classmethod
|
|
87
|
+
def from_text(
|
|
88
|
+
cls,
|
|
89
|
+
*versions: tuple[str, str],
|
|
90
|
+
language: str | None = None,
|
|
91
|
+
highlighter: CodeHighlighter | None = None,
|
|
92
|
+
) -> CodeEvolution:
|
|
93
|
+
"""Prepare source versions, optionally highlighting each complete snippet."""
|
|
94
|
+
if highlighter is not None and language is None:
|
|
95
|
+
raise ValueError("A highlighter requires a language")
|
|
96
|
+
_scope(language, "CodeEvolution.language")
|
|
97
|
+
structured = []
|
|
98
|
+
for name, source in versions:
|
|
99
|
+
normalized, text = _source_lines(source)
|
|
100
|
+
lines = _highlight_lines(normalized, text, language, highlighter)
|
|
101
|
+
structured.append(
|
|
102
|
+
(
|
|
103
|
+
name,
|
|
104
|
+
tuple(
|
|
105
|
+
CodeLine(line) if isinstance(line, str) else line
|
|
106
|
+
for line in lines
|
|
107
|
+
),
|
|
108
|
+
)
|
|
109
|
+
)
|
|
110
|
+
return cls(*structured, language=language)
|
|
111
|
+
|
|
112
|
+
def at(
|
|
113
|
+
self,
|
|
114
|
+
version: str,
|
|
115
|
+
*,
|
|
116
|
+
focus_line: int | None = None,
|
|
117
|
+
show_changes: bool = True,
|
|
118
|
+
) -> Code:
|
|
119
|
+
"""Select a version and optionally emphasize a one-based source line.
|
|
120
|
+
|
|
121
|
+
Changes are relative to the preceding declared version. The focus band
|
|
122
|
+
follows its own identity across builds and can overlap an edit band.
|
|
123
|
+
"""
|
|
124
|
+
selected = self._versions[version]
|
|
125
|
+
if focus_line is not None and (
|
|
126
|
+
type(focus_line) is not int or not 1 <= focus_line <= len(selected.lines)
|
|
127
|
+
):
|
|
128
|
+
raise ValueError(
|
|
129
|
+
"focus_line must be a one-based line number in the selected version"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
lines = []
|
|
133
|
+
for number, line in enumerate(selected.lines, start=1):
|
|
134
|
+
source = line.source
|
|
135
|
+
marks = list(source.marks)
|
|
136
|
+
if show_changes and line.change is not None:
|
|
137
|
+
marks.append(
|
|
138
|
+
CodeMark(line.change, f"evolution:{selected.index}:{line.key}")
|
|
139
|
+
)
|
|
140
|
+
if number == focus_line:
|
|
141
|
+
marks.append(CodeMark("focus", "evolution:focus"))
|
|
142
|
+
lines.append(
|
|
143
|
+
CodeLine(
|
|
144
|
+
*source.spans,
|
|
145
|
+
background_style=source.background_style,
|
|
146
|
+
mark=source.mark,
|
|
147
|
+
mark_key=source.mark_key,
|
|
148
|
+
marks=marks,
|
|
149
|
+
key=line.key,
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
return (
|
|
153
|
+
Code(
|
|
154
|
+
lines[0] if lines else CodeLine(),
|
|
155
|
+
*lines[1:],
|
|
156
|
+
language=self.language,
|
|
157
|
+
animate_lines=True,
|
|
158
|
+
)
|
|
159
|
+
.animation(Animation.smooth(fallback=FallbackPolicy.CROSSFADE))
|
|
160
|
+
.transition(Transition.fade())
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _body(line: CodeLine) -> str:
|
|
165
|
+
return _code_text(line).lstrip(" \t")
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _explicit_keys(lines: Sequence[CodeLine]) -> dict[str, int]:
|
|
169
|
+
keys = {}
|
|
170
|
+
for index, line in enumerate(lines):
|
|
171
|
+
if line.key is not None:
|
|
172
|
+
if line.key in keys:
|
|
173
|
+
raise ValueError(
|
|
174
|
+
"CodeEvolution line keys must be unique within each version"
|
|
175
|
+
)
|
|
176
|
+
keys[line.key] = index
|
|
177
|
+
return keys
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _unkeyed_bodies(
|
|
181
|
+
lines: Sequence[CodeLine], indices: Iterator[int]
|
|
182
|
+
) -> dict[str, list[int]]:
|
|
183
|
+
by_text: dict[str, list[int]] = defaultdict(list)
|
|
184
|
+
for index in indices:
|
|
185
|
+
if lines[index].key is None:
|
|
186
|
+
by_text[_body(lines[index])].append(index)
|
|
187
|
+
return by_text
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _match_lines(
|
|
191
|
+
before: tuple[CodeLine, ...],
|
|
192
|
+
after: tuple[CodeLine, ...],
|
|
193
|
+
new_keys: dict[str, int],
|
|
194
|
+
) -> dict[int, int]:
|
|
195
|
+
# Exact unique lines keep their identity even across a reorder.
|
|
196
|
+
old_keys = _explicit_keys(before)
|
|
197
|
+
matched = {
|
|
198
|
+
index: old_keys[key] for key, index in new_keys.items() if key in old_keys
|
|
199
|
+
}
|
|
200
|
+
old_bodies = _unkeyed_bodies(before, iter(range(len(before))))
|
|
201
|
+
new_bodies = _unkeyed_bodies(after, iter(range(len(after))))
|
|
202
|
+
for body, new_indices in new_bodies.items():
|
|
203
|
+
old_indices = old_bodies.get(body, [])
|
|
204
|
+
if body and len(old_indices) == len(new_indices) == 1:
|
|
205
|
+
matched[new_indices[0]] = old_indices[0]
|
|
206
|
+
|
|
207
|
+
# Use the anchors that kept their relative order to bound local matching.
|
|
208
|
+
# Reordered lines already have identities, and are excluded from these gaps.
|
|
209
|
+
def token(line: CodeLine) -> tuple[str, str]:
|
|
210
|
+
return ("key", line.key) if line.key is not None else ("text", _body(line))
|
|
211
|
+
|
|
212
|
+
alignment = SequenceMatcher(
|
|
213
|
+
None, tuple(map(token, before)), tuple(map(token, after)), autojunk=False
|
|
214
|
+
)
|
|
215
|
+
anchors = [
|
|
216
|
+
(old + offset, new + offset)
|
|
217
|
+
for old, new, count in alignment.get_matching_blocks()
|
|
218
|
+
for offset in range(count)
|
|
219
|
+
if matched.get(new + offset) == old + offset
|
|
220
|
+
]
|
|
221
|
+
used = set(matched.values())
|
|
222
|
+
old_start = new_start = 0
|
|
223
|
+
for old_stop, new_stop in (*anchors, (len(before), len(after))):
|
|
224
|
+
old_gap = _unkeyed_bodies(
|
|
225
|
+
before, (index for index in range(old_start, old_stop) if index not in used)
|
|
226
|
+
)
|
|
227
|
+
new_gap = _unkeyed_bodies(
|
|
228
|
+
after,
|
|
229
|
+
(index for index in range(new_start, new_stop) if index not in matched),
|
|
230
|
+
)
|
|
231
|
+
# Equal occurrence counts within a gap give repeated lines a local order.
|
|
232
|
+
# When counts differ, leave the repeated lines unresolved.
|
|
233
|
+
for body in old_gap.keys() & new_gap.keys():
|
|
234
|
+
old_indices, new_indices = old_gap[body], new_gap[body]
|
|
235
|
+
if len(old_indices) == len(new_indices):
|
|
236
|
+
matched.update(zip(new_indices, old_indices))
|
|
237
|
+
used.update(old_indices)
|
|
238
|
+
edits = _match_edits(
|
|
239
|
+
[
|
|
240
|
+
(i, body)
|
|
241
|
+
for body in old_gap.keys() - new_gap.keys()
|
|
242
|
+
for i in old_gap[body]
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
(j, body)
|
|
246
|
+
for body in new_gap.keys() - old_gap.keys()
|
|
247
|
+
for j in new_gap[body]
|
|
248
|
+
],
|
|
249
|
+
)
|
|
250
|
+
matched.update(edits)
|
|
251
|
+
used.update(edits.values())
|
|
252
|
+
old_start, new_start = old_stop + 1, new_stop + 1
|
|
253
|
+
return matched
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _match_edits(
|
|
257
|
+
before: Sequence[tuple[int, str]], after: Sequence[tuple[int, str]]
|
|
258
|
+
) -> dict[int, int]:
|
|
259
|
+
scores = {
|
|
260
|
+
(old_index, new_index): score
|
|
261
|
+
for old_index, old in before
|
|
262
|
+
if old
|
|
263
|
+
for new_index, new in after
|
|
264
|
+
if new
|
|
265
|
+
if (score := SequenceMatcher(None, old, new, autojunk=False).ratio()) >= 0.65
|
|
266
|
+
}
|
|
267
|
+
old_best: dict[int, float] = defaultdict(float)
|
|
268
|
+
new_best: dict[int, float] = defaultdict(float)
|
|
269
|
+
for (old, new), score in scores.items():
|
|
270
|
+
old_best[old] = max(old_best[old], score)
|
|
271
|
+
new_best[new] = max(new_best[new], score)
|
|
272
|
+
|
|
273
|
+
# Pair only mutual best matches, and decline ties on either side.
|
|
274
|
+
old_ties: dict[int, int] = defaultdict(int)
|
|
275
|
+
new_ties: dict[int, int] = defaultdict(int)
|
|
276
|
+
for (old, new), score in scores.items():
|
|
277
|
+
old_ties[old] += score == old_best[old]
|
|
278
|
+
new_ties[new] += score == new_best[new]
|
|
279
|
+
return {
|
|
280
|
+
new: old
|
|
281
|
+
for (old, new), score in scores.items()
|
|
282
|
+
if score == old_best[old] == new_best[new]
|
|
283
|
+
and old_ties[old] == new_ties[new] == 1
|
|
284
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
from ..context import Context
|
|
6
|
+
from .view import View
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CompositeView(View, ABC):
|
|
10
|
+
"""A view described by composing other views."""
|
|
11
|
+
|
|
12
|
+
def _resolve_body(self, context: Context) -> View:
|
|
13
|
+
"""Resolve the body used during expansion."""
|
|
14
|
+
return self.body(context)
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def body(self, context: Context) -> View:
|
|
18
|
+
"""Return this component's composition for the current context."""
|
|
19
|
+
raise NotImplementedError
|
deckkit/views/divider.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
|
|
5
|
+
from ..canvas import Stroke
|
|
6
|
+
from ..context import Context
|
|
7
|
+
from ..expanded_views import ExpandedDivider, ExpandedMetadata
|
|
8
|
+
from ..layout import StackAxis
|
|
9
|
+
from ..layouts.primitives import DividerLayout
|
|
10
|
+
from ._drawing import resolve_stroke
|
|
11
|
+
from .primitive_view import PrimitiveView
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Divider(PrimitiveView):
|
|
16
|
+
"""A rule that spans the cross axis of a stack by default."""
|
|
17
|
+
|
|
18
|
+
stroke: Stroke = field(default_factory=Stroke)
|
|
19
|
+
orientation: StackAxis | None = None
|
|
20
|
+
|
|
21
|
+
def __post_init__(self) -> None:
|
|
22
|
+
if not isinstance(self.stroke, Stroke):
|
|
23
|
+
raise TypeError("Divider stroke must be a Stroke")
|
|
24
|
+
if self.orientation not in (None, "horizontal", "vertical"):
|
|
25
|
+
raise ValueError("Divider orientation must be horizontal or vertical")
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def _layout_stretches_cross_axis(self) -> bool:
|
|
29
|
+
return True
|
|
30
|
+
|
|
31
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedDivider:
|
|
32
|
+
stroke = resolve_stroke(self.stroke, context.environment)
|
|
33
|
+
return ExpandedDivider(
|
|
34
|
+
metadata,
|
|
35
|
+
DividerLayout(context.environment, stroke.width, self.orientation),
|
|
36
|
+
stroke,
|
|
37
|
+
self.orientation,
|
|
38
|
+
)
|
deckkit/views/ellipse.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ..canvas import Stroke
|
|
6
|
+
from ..canvas._geometry import validate_opacity
|
|
7
|
+
from ..canvas.elements import Ellipse as CanvasEllipse
|
|
8
|
+
from ..context import Context
|
|
9
|
+
from ..drawables import Bounds
|
|
10
|
+
from ..expanded_views import ExpandedDrawing, ExpandedMetadata
|
|
11
|
+
from ..theme import ColorStyle, ForegroundColor
|
|
12
|
+
from ..units import pt
|
|
13
|
+
from ._drawing import expand_drawing
|
|
14
|
+
from .primitive_view import PrimitiveView
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class Ellipse(PrimitiveView):
|
|
19
|
+
"""An ellipse that fills the space proposed by layout."""
|
|
20
|
+
|
|
21
|
+
fill: ColorStyle | None = ForegroundColor
|
|
22
|
+
stroke: Stroke | None = None
|
|
23
|
+
fill_opacity: float = 1.0
|
|
24
|
+
|
|
25
|
+
def __post_init__(self) -> None:
|
|
26
|
+
validate_opacity(self.fill_opacity)
|
|
27
|
+
|
|
28
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedDrawing:
|
|
29
|
+
return expand_drawing(
|
|
30
|
+
CanvasEllipse(
|
|
31
|
+
Bounds(pt(0), pt(0), pt(0), pt(0)),
|
|
32
|
+
fill=self.fill,
|
|
33
|
+
stroke=self.stroke,
|
|
34
|
+
fill_opacity=self.fill_opacity,
|
|
35
|
+
),
|
|
36
|
+
context,
|
|
37
|
+
metadata,
|
|
38
|
+
)
|
deckkit/views/grid.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""A two-dimensional layout for arbitrary views."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable, Sequence
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
from ..geometry import RelativePoint
|
|
9
|
+
from ..identity import ViewIdentity
|
|
10
|
+
from ..layout import Layout
|
|
11
|
+
from ..layouts.grid import ColumnWidth, GridLayout, _GridSlot
|
|
12
|
+
from ..units import Length, pt, require_length
|
|
13
|
+
from .layout_view import LayoutView
|
|
14
|
+
from .view import View
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class GridCell:
|
|
19
|
+
"""Set how a view spans, aligns within, or contributes to its columns."""
|
|
20
|
+
|
|
21
|
+
content: View
|
|
22
|
+
column_span: int = 1
|
|
23
|
+
alignment: RelativePoint | None = None
|
|
24
|
+
contributes_width: bool = True
|
|
25
|
+
|
|
26
|
+
def __post_init__(self) -> None:
|
|
27
|
+
if not isinstance(self.content, View):
|
|
28
|
+
raise TypeError("GridCell.content must be a View")
|
|
29
|
+
if type(self.column_span) is not int or self.column_span < 1:
|
|
30
|
+
raise ValueError("GridCell.column_span must be a positive integer")
|
|
31
|
+
if self.alignment is not None and not isinstance(self.alignment, RelativePoint):
|
|
32
|
+
raise TypeError("GridCell.alignment must be a RelativePoint")
|
|
33
|
+
if not isinstance(self.contributes_width, bool):
|
|
34
|
+
raise TypeError("GridCell.contributes_width must be a bool")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True, init=False)
|
|
38
|
+
class GridRow:
|
|
39
|
+
"""Place cells in successive columns. A None cell leaves an empty column."""
|
|
40
|
+
|
|
41
|
+
cells: tuple[View | GridCell | None, ...]
|
|
42
|
+
alignment: RelativePoint | None
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
*cells: View | GridCell | None,
|
|
47
|
+
alignment: RelativePoint | None = None,
|
|
48
|
+
) -> None:
|
|
49
|
+
if not all(
|
|
50
|
+
cell is None or isinstance(cell, (View, GridCell)) for cell in cells
|
|
51
|
+
):
|
|
52
|
+
raise TypeError("GridRow cells must be views, GridCell, or None")
|
|
53
|
+
if alignment is not None and not isinstance(alignment, RelativePoint):
|
|
54
|
+
raise TypeError("GridRow.alignment must be a RelativePoint")
|
|
55
|
+
object.__setattr__(self, "cells", cells)
|
|
56
|
+
object.__setattr__(self, "alignment", alignment)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(frozen=True, init=False)
|
|
60
|
+
class Grid(LayoutView):
|
|
61
|
+
"""Arrange rows with shared column widths and content-sized row heights."""
|
|
62
|
+
|
|
63
|
+
_layout: GridLayout
|
|
64
|
+
|
|
65
|
+
def __init__(
|
|
66
|
+
self,
|
|
67
|
+
*rows: GridRow | View | GridCell | None,
|
|
68
|
+
columns: Sequence[Length | ColumnWidth] = (),
|
|
69
|
+
alignment: RelativePoint = RelativePoint.top_left,
|
|
70
|
+
horizontal_spacing: Length = pt(16),
|
|
71
|
+
vertical_spacing: Length = pt(16),
|
|
72
|
+
) -> None:
|
|
73
|
+
for spacing in (horizontal_spacing, vertical_spacing):
|
|
74
|
+
require_length(spacing)
|
|
75
|
+
if spacing < pt(0):
|
|
76
|
+
raise ValueError("Grid spacing must be nonnegative")
|
|
77
|
+
if not isinstance(alignment, RelativePoint):
|
|
78
|
+
raise TypeError("Grid.alignment must be a RelativePoint")
|
|
79
|
+
widths = tuple(columns)
|
|
80
|
+
for width in widths:
|
|
81
|
+
if not isinstance(width, ColumnWidth):
|
|
82
|
+
require_length(width)
|
|
83
|
+
if width < pt(0):
|
|
84
|
+
raise ValueError("Grid column widths must be nonnegative")
|
|
85
|
+
|
|
86
|
+
count = len(widths)
|
|
87
|
+
for row in rows:
|
|
88
|
+
if isinstance(row, GridRow):
|
|
89
|
+
count = max(
|
|
90
|
+
count,
|
|
91
|
+
sum(
|
|
92
|
+
cell.column_span if isinstance(cell, GridCell) else 1
|
|
93
|
+
for cell in row.cells
|
|
94
|
+
),
|
|
95
|
+
)
|
|
96
|
+
elif row is not None:
|
|
97
|
+
count = max(count, 1)
|
|
98
|
+
|
|
99
|
+
children: list[View | None] = []
|
|
100
|
+
slots: list[tuple[_GridSlot, ...]] = []
|
|
101
|
+
for row in rows:
|
|
102
|
+
if row is None:
|
|
103
|
+
children.append(None)
|
|
104
|
+
continue
|
|
105
|
+
if isinstance(row, GridRow):
|
|
106
|
+
row_slots = []
|
|
107
|
+
row_alignment = row.alignment or alignment
|
|
108
|
+
start = 0
|
|
109
|
+
for content in row.cells:
|
|
110
|
+
cell = GridCell(content) if isinstance(content, View) else content
|
|
111
|
+
if cell is None:
|
|
112
|
+
children.append(None)
|
|
113
|
+
start += 1
|
|
114
|
+
continue
|
|
115
|
+
end = start + cell.column_span
|
|
116
|
+
children.append(cell.content)
|
|
117
|
+
row_slots.append(
|
|
118
|
+
_GridSlot(
|
|
119
|
+
start,
|
|
120
|
+
end,
|
|
121
|
+
cell.alignment or row_alignment,
|
|
122
|
+
cell.contributes_width,
|
|
123
|
+
)
|
|
124
|
+
)
|
|
125
|
+
start = end
|
|
126
|
+
slots.append(tuple(row_slots))
|
|
127
|
+
else:
|
|
128
|
+
cell = row if isinstance(row, GridCell) else GridCell(row)
|
|
129
|
+
if cell.column_span != 1:
|
|
130
|
+
raise ValueError("Specify a column span inside a GridRow")
|
|
131
|
+
children.append(cell.content)
|
|
132
|
+
slots.append(
|
|
133
|
+
(
|
|
134
|
+
_GridSlot(
|
|
135
|
+
0,
|
|
136
|
+
count,
|
|
137
|
+
cell.alignment or alignment,
|
|
138
|
+
cell.contributes_width,
|
|
139
|
+
),
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
object.__setattr__(
|
|
144
|
+
self,
|
|
145
|
+
"_layout",
|
|
146
|
+
GridLayout(
|
|
147
|
+
widths + (ColumnWidth(),) * (count - len(widths)),
|
|
148
|
+
tuple(slots),
|
|
149
|
+
horizontal_spacing,
|
|
150
|
+
vertical_spacing,
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
super().__init__(*children)
|
|
154
|
+
|
|
155
|
+
def resolve_layout(self, lookup: Callable[[str], ViewIdentity]) -> Layout:
|
|
156
|
+
return self._layout
|
deckkit/views/hstack.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ..layout import (
|
|
6
|
+
Layout,
|
|
7
|
+
VerticalAlignment,
|
|
8
|
+
VerticalAlignmentName,
|
|
9
|
+
resolve_vertical_alignment,
|
|
10
|
+
)
|
|
11
|
+
from ..units import Length, pt
|
|
12
|
+
from .stack import Stack
|
|
13
|
+
from .view import View
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True, init=False)
|
|
17
|
+
class HStack(Stack):
|
|
18
|
+
alignment: VerticalAlignmentName | VerticalAlignment
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
*children: View | None,
|
|
23
|
+
spacing: Length = pt(16),
|
|
24
|
+
alignment: VerticalAlignmentName | VerticalAlignment = "top",
|
|
25
|
+
) -> None:
|
|
26
|
+
resolve_vertical_alignment(alignment)
|
|
27
|
+
object.__setattr__(self, "alignment", alignment)
|
|
28
|
+
super().__init__(*children, spacing=spacing)
|
|
29
|
+
|
|
30
|
+
def _make_layout(self) -> Layout:
|
|
31
|
+
from ..layouts import StackLayout
|
|
32
|
+
|
|
33
|
+
box_alignment = (
|
|
34
|
+
{"center": 0.5, "bottom": 1.0}.get(self.alignment, 0.0)
|
|
35
|
+
if isinstance(self.alignment, str)
|
|
36
|
+
else 0.0
|
|
37
|
+
)
|
|
38
|
+
return StackLayout(
|
|
39
|
+
"horizontal",
|
|
40
|
+
self.spacing,
|
|
41
|
+
box_alignment,
|
|
42
|
+
vertical_alignment=resolve_vertical_alignment(self.alignment),
|
|
43
|
+
)
|
deckkit/views/image.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from io import BytesIO
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Literal
|
|
7
|
+
|
|
8
|
+
from PIL import Image as PILImage
|
|
9
|
+
|
|
10
|
+
from ..context import Context
|
|
11
|
+
from ..expanded_views import ExpandedImage, ExpandedMetadata
|
|
12
|
+
from ..layouts import ImageLayout
|
|
13
|
+
from ..units import inch
|
|
14
|
+
from .primitive_view import PrimitiveView
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class Image(PrimitiveView):
|
|
19
|
+
source: str
|
|
20
|
+
fit: Literal["contain", "cover"] = "contain"
|
|
21
|
+
dpi: float = 96
|
|
22
|
+
|
|
23
|
+
def __post_init__(self) -> None:
|
|
24
|
+
from math import isfinite
|
|
25
|
+
|
|
26
|
+
if not isfinite(self.dpi) or self.dpi <= 0:
|
|
27
|
+
raise ValueError("Image DPI must be finite and positive")
|
|
28
|
+
if self.fit not in ("contain", "cover"):
|
|
29
|
+
raise ValueError("Image fit must be contain or cover")
|
|
30
|
+
|
|
31
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedImage:
|
|
32
|
+
data = Path(self.source).read_bytes()
|
|
33
|
+
with PILImage.open(BytesIO(data)) as image:
|
|
34
|
+
width, height = image.size
|
|
35
|
+
mime = PILImage.MIME[image.format or ""]
|
|
36
|
+
return ExpandedImage(
|
|
37
|
+
metadata,
|
|
38
|
+
ImageLayout(inch(width / self.dpi), inch(height / self.dpi), self.fit),
|
|
39
|
+
data,
|
|
40
|
+
mime,
|
|
41
|
+
(width, height),
|
|
42
|
+
self.fit,
|
|
43
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Callable
|
|
6
|
+
|
|
7
|
+
from ..identity import ViewIdentity
|
|
8
|
+
from ..layout import Layout
|
|
9
|
+
from ..units import Length
|
|
10
|
+
from .view import View
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True, init=False)
|
|
14
|
+
class LayoutView(View, ABC):
|
|
15
|
+
"""Base class for views whose children are arranged by a layout policy."""
|
|
16
|
+
|
|
17
|
+
children: tuple[View | None, ...]
|
|
18
|
+
|
|
19
|
+
def __init__(self, *children: View | None) -> None:
|
|
20
|
+
object.__setattr__(self, "children", tuple(children))
|
|
21
|
+
|
|
22
|
+
@abstractmethod
|
|
23
|
+
def resolve_layout(self, lookup: Callable[[str], ViewIdentity]) -> Layout:
|
|
24
|
+
"""Return the policy used to arrange this occurrence's children."""
|
|
25
|
+
raise NotImplementedError
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def _expanded_spacing(self) -> Length | None:
|
|
29
|
+
"""Spacing metadata retained for logical reconciliation, when applicable."""
|
|
30
|
+
return None
|
deckkit/views/line.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
|
|
5
|
+
from ..canvas import Stroke
|
|
6
|
+
from ..canvas.elements import Line as CanvasLine
|
|
7
|
+
from ..context import Context
|
|
8
|
+
from ..expanded_views import ExpandedDrawing, ExpandedMetadata
|
|
9
|
+
from ..geometry import Point
|
|
10
|
+
from ._drawing import expand_drawing
|
|
11
|
+
from .primitive_view import PrimitiveView
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Line(PrimitiveView):
|
|
16
|
+
"""A stroked segment sized intrinsically from its endpoints and stroke width."""
|
|
17
|
+
|
|
18
|
+
start: Point
|
|
19
|
+
end: Point
|
|
20
|
+
stroke: Stroke = field(default_factory=Stroke)
|
|
21
|
+
|
|
22
|
+
def __post_init__(self) -> None:
|
|
23
|
+
if not isinstance(self.start, Point) or not isinstance(self.end, Point):
|
|
24
|
+
raise TypeError("Line endpoints must be Point values")
|
|
25
|
+
if not isinstance(self.stroke, Stroke):
|
|
26
|
+
raise TypeError("Line stroke must be a Stroke")
|
|
27
|
+
|
|
28
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedDrawing:
|
|
29
|
+
return expand_drawing(
|
|
30
|
+
CanvasLine(self.start, self.end, self.stroke), context, metadata
|
|
31
|
+
)
|