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
use crate::{Grapheme, TextStyle, View};
use super::cell_primitives::CellPath;
use super::command::{CellsCommand, RecordedCommand, TextCommand, ViewCommand};
use super::line_network::LineNetwork;
use super::{Composition, Position, PositionedCell};
use crate::view::geometry::Size;
/// Frame-scoped command recorder passed to [`super::CanvasItem::draw`].
#[derive(Debug)]
pub struct CanvasContext {
size: Size,
commands: Vec<RecordedCommand>,
}
impl CanvasContext {
pub(super) fn new(size: Size) -> Self {
Self {
size,
commands: Vec::new(),
}
}
pub const fn size(&self) -> Size {
self.size
}
pub const fn bounds(&self) -> (Position, Size) {
(Position::new(0, 0), self.size)
}
/// Records a View using [`Composition::Replace`].
///
/// Supply a finite allocation on every axis whose result depends on
/// [`crate::Length::Fill`]. Canvas bounds are not an implicit allocation.
pub fn view(
&mut self,
origin: Position,
view: View,
width: Option<usize>,
height: Option<usize>,
) {
self.view_with(origin, view, width, height, Composition::Replace);
}
/// Records a View with an explicit composition rule.
///
/// Allocation requirements are the same as for [`Self::view`].
pub fn view_with(
&mut self,
origin: Position,
view: View,
width: Option<usize>,
height: Option<usize>,
composition: Composition,
) {
self.record(
ViewCommand {
origin,
view,
allocation: (width, height),
},
composition,
);
}
/// Records plain text using [`Composition::Overlay`].
///
/// As with [`View::text`], `text` must not contain terminal controls.
pub fn text(&mut self, origin: Position, text: impl Into<String>, style: TextStyle) {
self.text_with(origin, text, style, Composition::Overlay);
}
/// Records plain text with an explicit composition rule.
///
/// The plain-text contract is the same as for [`Self::text`].
pub fn text_with(
&mut self,
origin: Position,
text: impl Into<String>,
style: TextStyle,
composition: Composition,
) {
self.record(
TextCommand {
origin,
text: text.into(),
style,
},
composition,
);
}
/// Draws a marker along one cell-space segment using [`Composition::Overlay`].
///
/// The segment may be horizontal, vertical, or diagonal. Intersecting
/// lines remain ordinary marker cells; use [`LineNetwork`] when crossings
/// must be derived from connectivity.
///
/// # Panics
///
/// Panics if `marker` is not exactly one printable, one-cell grapheme.
pub fn line(&mut self, from: Position, to: Position, marker: &Grapheme, style: TextStyle) {
self.line_with(from, to, marker, style, Composition::Overlay);
}
/// Draws a marker along one cell-space segment with explicit composition.
///
/// # Panics
///
/// Panics if `marker` is not exactly one printable, one-cell grapheme.
pub fn line_with(
&mut self,
from: Position,
to: Position,
marker: &Grapheme,
style: TextStyle,
composition: Composition,
) {
self.record(CellPath::line(from, to, marker, style), composition);
}
/// Draws connected marker segments using [`Composition::Overlay`].
///
/// # Panics
///
/// Panics if `marker` is not exactly one printable, one-cell grapheme.
pub fn polyline(
&mut self,
points: impl IntoIterator<Item = Position>,
marker: &Grapheme,
style: TextStyle,
) {
self.polyline_with(points, marker, style, Composition::Overlay);
}
/// Draws connected marker segments with explicit composition.
///
/// # Panics
///
/// Panics if `marker` is not exactly one printable, one-cell grapheme.
pub fn polyline_with(
&mut self,
points: impl IntoIterator<Item = Position>,
marker: &Grapheme,
style: TextStyle,
composition: Composition,
) {
self.record(CellPath::polyline(points, marker, style), composition);
}
/// Draws a cell-aligned marker rectangle using [`Composition::Overlay`].
///
/// # Panics
///
/// Panics if `marker` is not exactly one printable, one-cell grapheme.
pub fn rectangle(
&mut self,
origin: Position,
width: usize,
height: usize,
marker: &Grapheme,
style: TextStyle,
) {
self.rectangle_with(origin, width, height, marker, style, Composition::Overlay);
}
/// Draws a cell-aligned marker rectangle with explicit composition.
///
/// # Panics
///
/// Panics if `marker` is not exactly one printable, one-cell grapheme.
pub fn rectangle_with(
&mut self,
origin: Position,
width: usize,
height: usize,
marker: &Grapheme,
style: TextStyle,
composition: Composition,
) {
self.record(
CellPath::rectangle(origin, width, height, marker, style),
composition,
);
}
/// Draws horizontal and vertical segments as one connected network.
///
/// Intersections between segments in this network are rasterized as
/// corners, tees, and crossings. The resulting cells use
/// [`Composition::Overlay`], like other cell-producing commands.
pub fn line_network(&mut self, network: LineNetwork) {
self.line_network_with(network, Composition::Overlay);
}
/// Draws one connected line network with an explicit composition rule.
///
/// The rule combines this network's rasterized glyphs with earlier Canvas
/// cells. It does not merge this value's connections with another command.
pub fn line_network_with(&mut self, network: LineNetwork, composition: Composition) {
self.record(network, composition);
}
/// Records sparse cell contributions using [`Composition::Overlay`].
pub fn cells(&mut self, cells: impl IntoIterator<Item = PositionedCell>) {
self.cells_with(cells, Composition::Overlay);
}
/// Records sparse cell contributions with an explicit composition rule.
pub fn cells_with(
&mut self,
cells: impl IntoIterator<Item = PositionedCell>,
composition: Composition,
) {
self.record(CellsCommand(cells.into_iter().collect()), composition);
}
fn record(
&mut self,
command: impl super::command::CanvasCommand + 'static,
composition: Composition,
) {
self.commands
.push(RecordedCommand::new(command, composition));
}
pub(super) fn into_commands(self) -> Vec<RecordedCommand> {
self.commands
}
}