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,40 @@
|
|
|
1
|
+
"""Retained, size-dependent 2D drawing content."""
|
|
2
|
+
|
|
3
|
+
from .context import CanvasContext, CanvasSize
|
|
4
|
+
from .elements import CanvasElement, CanvasText, Ellipse, Line, Rect
|
|
5
|
+
from .path import (
|
|
6
|
+
ArcTo,
|
|
7
|
+
CanvasPathCommand,
|
|
8
|
+
ClosePath,
|
|
9
|
+
CubicTo,
|
|
10
|
+
LineTo,
|
|
11
|
+
MoveTo,
|
|
12
|
+
Path,
|
|
13
|
+
QuadraticTo,
|
|
14
|
+
ResolvedPathCommand,
|
|
15
|
+
)
|
|
16
|
+
from .scene import CanvasRenderer, CanvasScene, scene_from
|
|
17
|
+
from .style import Stroke
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"ArcTo",
|
|
21
|
+
"CanvasContext",
|
|
22
|
+
"CanvasElement",
|
|
23
|
+
"CanvasPathCommand",
|
|
24
|
+
"CanvasRenderer",
|
|
25
|
+
"CanvasScene",
|
|
26
|
+
"CanvasSize",
|
|
27
|
+
"CanvasText",
|
|
28
|
+
"ClosePath",
|
|
29
|
+
"CubicTo",
|
|
30
|
+
"Ellipse",
|
|
31
|
+
"Line",
|
|
32
|
+
"LineTo",
|
|
33
|
+
"MoveTo",
|
|
34
|
+
"Path",
|
|
35
|
+
"QuadraticTo",
|
|
36
|
+
"Rect",
|
|
37
|
+
"ResolvedPathCommand",
|
|
38
|
+
"Stroke",
|
|
39
|
+
"scene_from",
|
|
40
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from math import isfinite
|
|
4
|
+
|
|
5
|
+
from ..drawables import Bounds
|
|
6
|
+
from ..geometry import Point
|
|
7
|
+
from ..units import pt
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def validate_key(key: str | None) -> None:
|
|
11
|
+
if key is not None and not key:
|
|
12
|
+
raise ValueError("Canvas element keys must not be empty")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def validate_bounds(bounds: Bounds) -> None:
|
|
16
|
+
if bounds.width < pt(0) or bounds.height < pt(0):
|
|
17
|
+
raise ValueError("Canvas element dimensions must be non-negative")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def validate_opacity(value: float) -> None:
|
|
21
|
+
if not isfinite(value) or not 0 <= value <= 1:
|
|
22
|
+
raise ValueError("Opacity must be between zero and one")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def translate_point(point: Point, origin: Point) -> Point:
|
|
26
|
+
return Point(origin.x + point.x, origin.y + point.y)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def translate_bounds(bounds: Bounds, origin: Point) -> Bounds:
|
|
30
|
+
return Bounds(origin.x + bounds.x, origin.y + bounds.y, bounds.width, bounds.height)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def point_bounds(start: Point, end: Point) -> Bounds:
|
|
34
|
+
return Bounds(
|
|
35
|
+
min(start.x, end.x),
|
|
36
|
+
min(start.y, end.y),
|
|
37
|
+
max(start.x, end.x) - min(start.x, end.x),
|
|
38
|
+
max(start.y, end.y) - min(start.y, end.y),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def points_bounds(points: tuple[Point, ...]) -> Bounds:
|
|
43
|
+
if not points:
|
|
44
|
+
return Bounds(pt(0), pt(0), pt(0), pt(0))
|
|
45
|
+
left = min(point.x for point in points)
|
|
46
|
+
top = min(point.y for point in points)
|
|
47
|
+
right = max(point.x for point in points)
|
|
48
|
+
bottom = max(point.y for point in points)
|
|
49
|
+
return Bounds(left, top, right - left, bottom - top)
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"""Backend-independent rectangular clipping for Canvas drawables."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from math import cos, pi, sin
|
|
7
|
+
from typing import Literal
|
|
8
|
+
|
|
9
|
+
from ..drawables import (
|
|
10
|
+
Bounds,
|
|
11
|
+
Drawable,
|
|
12
|
+
DrawableEllipse,
|
|
13
|
+
DrawableGroup,
|
|
14
|
+
DrawableLine,
|
|
15
|
+
DrawablePath,
|
|
16
|
+
DrawablePathCommand,
|
|
17
|
+
DrawableRectangle,
|
|
18
|
+
DrawableRichText,
|
|
19
|
+
DrawableText,
|
|
20
|
+
PathClose,
|
|
21
|
+
PathCubic,
|
|
22
|
+
PathLine,
|
|
23
|
+
PathMove,
|
|
24
|
+
PathQuadratic,
|
|
25
|
+
)
|
|
26
|
+
from ..geometry import Point
|
|
27
|
+
from ..units import Length, pt
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def clip_drawable(node: Drawable, clip: Bounds) -> Drawable:
|
|
31
|
+
if isinstance(node, DrawableRectangle):
|
|
32
|
+
bounds = _intersection(node.content_bounds, clip)
|
|
33
|
+
if bounds is None:
|
|
34
|
+
return _empty(node)
|
|
35
|
+
return DrawableRectangle(
|
|
36
|
+
node.identity,
|
|
37
|
+
bounds,
|
|
38
|
+
node.metadata,
|
|
39
|
+
bounds,
|
|
40
|
+
node.color,
|
|
41
|
+
min(node.corner_radius, bounds.width / 2, bounds.height / 2),
|
|
42
|
+
node.stroke,
|
|
43
|
+
node.stroke_width,
|
|
44
|
+
node.dash,
|
|
45
|
+
node.fill_opacity,
|
|
46
|
+
node.stroke_opacity,
|
|
47
|
+
node.line_cap,
|
|
48
|
+
node.line_join,
|
|
49
|
+
)
|
|
50
|
+
if isinstance(node, DrawableLine):
|
|
51
|
+
segment = _clip_segment(node.start, node.end, clip)
|
|
52
|
+
if segment is None:
|
|
53
|
+
return _empty(node)
|
|
54
|
+
start, end = segment
|
|
55
|
+
return DrawableLine(
|
|
56
|
+
node.identity,
|
|
57
|
+
_point_bounds((start, end)),
|
|
58
|
+
node.metadata,
|
|
59
|
+
start,
|
|
60
|
+
end,
|
|
61
|
+
node.color,
|
|
62
|
+
node.width,
|
|
63
|
+
node.dash,
|
|
64
|
+
node.opacity,
|
|
65
|
+
node.line_cap,
|
|
66
|
+
node.line_join,
|
|
67
|
+
)
|
|
68
|
+
if isinstance(node, DrawableEllipse):
|
|
69
|
+
if _contains(clip, node.content_bounds):
|
|
70
|
+
return node
|
|
71
|
+
if _intersection(node.content_bounds, clip) is None:
|
|
72
|
+
return _empty(node)
|
|
73
|
+
center = Point(
|
|
74
|
+
node.content_bounds.x + node.content_bounds.width / 2,
|
|
75
|
+
node.content_bounds.y + node.content_bounds.height / 2,
|
|
76
|
+
)
|
|
77
|
+
points = tuple(
|
|
78
|
+
Point(
|
|
79
|
+
center.x + node.content_bounds.width / 2 * cos(i * 2 * pi / 48),
|
|
80
|
+
center.y + node.content_bounds.height / 2 * sin(i * 2 * pi / 48),
|
|
81
|
+
)
|
|
82
|
+
for i in range(48)
|
|
83
|
+
)
|
|
84
|
+
return _polygon_drawable(
|
|
85
|
+
node,
|
|
86
|
+
_clip_polygon(points, clip),
|
|
87
|
+
node.fill,
|
|
88
|
+
node.stroke,
|
|
89
|
+
node.stroke_width,
|
|
90
|
+
node.dash,
|
|
91
|
+
node.fill_opacity,
|
|
92
|
+
node.stroke_opacity,
|
|
93
|
+
node.line_cap,
|
|
94
|
+
node.line_join,
|
|
95
|
+
)
|
|
96
|
+
if isinstance(node, DrawablePath):
|
|
97
|
+
points, closed = _flatten(node)
|
|
98
|
+
if closed and node.fill is not None:
|
|
99
|
+
return _polygon_drawable(
|
|
100
|
+
node,
|
|
101
|
+
_clip_polygon(points, clip),
|
|
102
|
+
node.fill,
|
|
103
|
+
node.stroke,
|
|
104
|
+
node.stroke_width,
|
|
105
|
+
node.dash,
|
|
106
|
+
node.fill_opacity,
|
|
107
|
+
node.stroke_opacity,
|
|
108
|
+
node.line_cap,
|
|
109
|
+
node.line_join,
|
|
110
|
+
)
|
|
111
|
+
commands: list[DrawablePathCommand] = []
|
|
112
|
+
previous_end = None
|
|
113
|
+
for start, end in zip(points, points[1:]):
|
|
114
|
+
segment = _clip_segment(start, end, clip)
|
|
115
|
+
if segment is None:
|
|
116
|
+
previous_end = None
|
|
117
|
+
continue
|
|
118
|
+
clipped_start, clipped_end = segment
|
|
119
|
+
if previous_end != clipped_start:
|
|
120
|
+
commands.append(PathMove(clipped_start))
|
|
121
|
+
commands.append(PathLine(clipped_end))
|
|
122
|
+
previous_end = clipped_end
|
|
123
|
+
if not commands:
|
|
124
|
+
return _empty(node)
|
|
125
|
+
bounds = _point_bounds(
|
|
126
|
+
tuple(
|
|
127
|
+
command.point
|
|
128
|
+
for command in commands
|
|
129
|
+
if isinstance(command, (PathMove, PathLine))
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
return DrawablePath(
|
|
133
|
+
node.identity,
|
|
134
|
+
bounds,
|
|
135
|
+
node.metadata,
|
|
136
|
+
tuple(commands),
|
|
137
|
+
None,
|
|
138
|
+
node.stroke,
|
|
139
|
+
node.stroke_width,
|
|
140
|
+
node.dash,
|
|
141
|
+
1.0,
|
|
142
|
+
node.stroke_opacity,
|
|
143
|
+
node.line_cap,
|
|
144
|
+
node.line_join,
|
|
145
|
+
)
|
|
146
|
+
if isinstance(node, (DrawableText, DrawableRichText)):
|
|
147
|
+
return node if _intersection(node.bounds, clip) is not None else _empty(node)
|
|
148
|
+
return node
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _empty(node: Drawable) -> DrawableGroup:
|
|
152
|
+
return DrawableGroup(node.identity, node.bounds, node.metadata, ())
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _intersection(first: Bounds, second: Bounds) -> Bounds | None:
|
|
156
|
+
left = max(first.x, second.x)
|
|
157
|
+
top = max(first.y, second.y)
|
|
158
|
+
right = min(first.x + first.width, second.x + second.width)
|
|
159
|
+
bottom = min(first.y + first.height, second.y + second.height)
|
|
160
|
+
if right <= left or bottom <= top:
|
|
161
|
+
return None
|
|
162
|
+
return Bounds(left, top, right - left, bottom - top)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _contains(outer: Bounds, inner: Bounds) -> bool:
|
|
166
|
+
return (
|
|
167
|
+
inner.x >= outer.x
|
|
168
|
+
and inner.y >= outer.y
|
|
169
|
+
and inner.x + inner.width <= outer.x + outer.width
|
|
170
|
+
and inner.y + inner.height <= outer.y + outer.height
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _clip_segment(start: Point, end: Point, clip: Bounds) -> tuple[Point, Point] | None:
|
|
175
|
+
x0, y0 = start.x.points, start.y.points
|
|
176
|
+
dx, dy = end.x.points - x0, end.y.points - y0
|
|
177
|
+
minimum, maximum = 0.0, 1.0
|
|
178
|
+
for p, q in (
|
|
179
|
+
(-dx, x0 - clip.x.points),
|
|
180
|
+
(dx, clip.x.points + clip.width.points - x0),
|
|
181
|
+
(-dy, y0 - clip.y.points),
|
|
182
|
+
(dy, clip.y.points + clip.height.points - y0),
|
|
183
|
+
):
|
|
184
|
+
if p == 0:
|
|
185
|
+
if q < 0:
|
|
186
|
+
return None
|
|
187
|
+
continue
|
|
188
|
+
ratio = q / p
|
|
189
|
+
if p < 0:
|
|
190
|
+
minimum = max(minimum, ratio)
|
|
191
|
+
else:
|
|
192
|
+
maximum = min(maximum, ratio)
|
|
193
|
+
if minimum > maximum:
|
|
194
|
+
return None
|
|
195
|
+
return (
|
|
196
|
+
Point(pt(x0 + minimum * dx), pt(y0 + minimum * dy)),
|
|
197
|
+
Point(pt(x0 + maximum * dx), pt(y0 + maximum * dy)),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _flatten(node: DrawablePath) -> tuple[tuple[Point, ...], bool]:
|
|
202
|
+
result = []
|
|
203
|
+
current = None
|
|
204
|
+
start = None
|
|
205
|
+
closed = False
|
|
206
|
+
for command in node.commands:
|
|
207
|
+
if isinstance(command, PathMove):
|
|
208
|
+
current = start = command.point
|
|
209
|
+
result.append(current)
|
|
210
|
+
elif isinstance(command, PathLine) and current is not None:
|
|
211
|
+
current = command.point
|
|
212
|
+
result.append(current)
|
|
213
|
+
elif isinstance(command, PathQuadratic) and current is not None:
|
|
214
|
+
origin = current
|
|
215
|
+
for index in range(1, 17):
|
|
216
|
+
amount = index / 16
|
|
217
|
+
inverse = 1 - amount
|
|
218
|
+
result.append(
|
|
219
|
+
Point(
|
|
220
|
+
origin.x * (inverse * inverse)
|
|
221
|
+
+ command.control.x * (2 * inverse * amount)
|
|
222
|
+
+ command.end.x * (amount * amount),
|
|
223
|
+
origin.y * (inverse * inverse)
|
|
224
|
+
+ command.control.y * (2 * inverse * amount)
|
|
225
|
+
+ command.end.y * (amount * amount),
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
current = command.end
|
|
229
|
+
elif isinstance(command, PathCubic) and current is not None:
|
|
230
|
+
origin = current
|
|
231
|
+
for index in range(1, 17):
|
|
232
|
+
amount = index / 16
|
|
233
|
+
inverse = 1 - amount
|
|
234
|
+
result.append(
|
|
235
|
+
Point(
|
|
236
|
+
origin.x * (inverse**3)
|
|
237
|
+
+ command.control1.x * (3 * inverse * inverse * amount)
|
|
238
|
+
+ command.control2.x * (3 * inverse * amount * amount)
|
|
239
|
+
+ command.end.x * (amount**3),
|
|
240
|
+
origin.y * (inverse**3)
|
|
241
|
+
+ command.control1.y * (3 * inverse * inverse * amount)
|
|
242
|
+
+ command.control2.y * (3 * inverse * amount * amount)
|
|
243
|
+
+ command.end.y * (amount**3),
|
|
244
|
+
)
|
|
245
|
+
)
|
|
246
|
+
current = command.end
|
|
247
|
+
elif isinstance(command, PathClose) and start is not None:
|
|
248
|
+
result.append(start)
|
|
249
|
+
current = start
|
|
250
|
+
closed = True
|
|
251
|
+
return tuple(result), closed
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _clip_polygon(points: tuple[Point, ...], clip: Bounds) -> tuple[Point, ...]:
|
|
255
|
+
result = list(points)
|
|
256
|
+
edges: tuple[
|
|
257
|
+
tuple[Callable[[Point], bool], Callable[[Point, Point], Point]], ...
|
|
258
|
+
] = (
|
|
259
|
+
(lambda p: p.x >= clip.x, lambda a, b: _vertical(a, b, clip.x)),
|
|
260
|
+
(
|
|
261
|
+
lambda p: p.x <= clip.x + clip.width,
|
|
262
|
+
lambda a, b: _vertical(a, b, clip.x + clip.width),
|
|
263
|
+
),
|
|
264
|
+
(lambda p: p.y >= clip.y, lambda a, b: _horizontal(a, b, clip.y)),
|
|
265
|
+
(
|
|
266
|
+
lambda p: p.y <= clip.y + clip.height,
|
|
267
|
+
lambda a, b: _horizontal(a, b, clip.y + clip.height),
|
|
268
|
+
),
|
|
269
|
+
)
|
|
270
|
+
for inside, intersection in edges:
|
|
271
|
+
source, result = result, []
|
|
272
|
+
if not source:
|
|
273
|
+
break
|
|
274
|
+
previous = source[-1]
|
|
275
|
+
for current in source:
|
|
276
|
+
if inside(current):
|
|
277
|
+
if not inside(previous):
|
|
278
|
+
result.append(intersection(previous, current))
|
|
279
|
+
result.append(current)
|
|
280
|
+
elif inside(previous):
|
|
281
|
+
result.append(intersection(previous, current))
|
|
282
|
+
previous = current
|
|
283
|
+
return tuple(result)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _vertical(start: Point, end: Point, x: Length) -> Point:
|
|
287
|
+
amount = (x - start.x) / (end.x - start.x)
|
|
288
|
+
return Point(x, start.y + (end.y - start.y) * amount)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _horizontal(start: Point, end: Point, y: Length) -> Point:
|
|
292
|
+
amount = (y - start.y) / (end.y - start.y)
|
|
293
|
+
return Point(start.x + (end.x - start.x) * amount, y)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _polygon_drawable(
|
|
297
|
+
node: Drawable,
|
|
298
|
+
points: tuple[Point, ...],
|
|
299
|
+
fill: str | None,
|
|
300
|
+
stroke: str | None,
|
|
301
|
+
stroke_width: Length,
|
|
302
|
+
dash: tuple[Length, ...],
|
|
303
|
+
fill_opacity: float,
|
|
304
|
+
stroke_opacity: float,
|
|
305
|
+
line_cap: Literal["butt", "round", "square"],
|
|
306
|
+
line_join: Literal["miter", "round", "bevel"],
|
|
307
|
+
) -> Drawable:
|
|
308
|
+
if len(points) < 3:
|
|
309
|
+
return _empty(node)
|
|
310
|
+
commands: tuple[DrawablePathCommand, ...] = (
|
|
311
|
+
PathMove(points[0]),
|
|
312
|
+
*(PathLine(point) for point in points[1:]),
|
|
313
|
+
PathClose(),
|
|
314
|
+
)
|
|
315
|
+
return DrawablePath(
|
|
316
|
+
node.identity,
|
|
317
|
+
_point_bounds(points),
|
|
318
|
+
node.metadata,
|
|
319
|
+
commands,
|
|
320
|
+
fill,
|
|
321
|
+
stroke,
|
|
322
|
+
stroke_width,
|
|
323
|
+
dash,
|
|
324
|
+
fill_opacity,
|
|
325
|
+
stroke_opacity,
|
|
326
|
+
line_cap,
|
|
327
|
+
line_join,
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _point_bounds(points: tuple[Point, ...]) -> Bounds:
|
|
332
|
+
left = min(point.x for point in points)
|
|
333
|
+
top = min(point.y for point in points)
|
|
334
|
+
right = max(point.x for point in points)
|
|
335
|
+
bottom = max(point.y for point in points)
|
|
336
|
+
return Bounds(left, top, right - left, bottom - top)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from ..context import Environment
|
|
7
|
+
from ..theme import Color, ColorStyle, ForegroundColor
|
|
8
|
+
from ..units import Length, pt, require_length
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from ..fonts import FontResolver
|
|
12
|
+
from ..measurement import TextMeasurer
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class CanvasSize:
|
|
17
|
+
width: Length
|
|
18
|
+
height: Length
|
|
19
|
+
|
|
20
|
+
def __post_init__(self) -> None:
|
|
21
|
+
require_length(self.width)
|
|
22
|
+
require_length(self.height)
|
|
23
|
+
if self.width < pt(0) or self.height < pt(0):
|
|
24
|
+
raise ValueError("Canvas dimensions must be non-negative")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class CanvasContext:
|
|
29
|
+
environment: Environment
|
|
30
|
+
measurer: TextMeasurer
|
|
31
|
+
font_resolver: FontResolver
|
|
32
|
+
|
|
33
|
+
def color(self, style: ColorStyle = ForegroundColor) -> Color:
|
|
34
|
+
return style if isinstance(style, Color) else self.environment[style]
|