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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Direct2D / DirectWrite support for [`crate::Terminal`] cell grids
//! (issue #30).
//!
//! Painting moved to the shared [`crate::primitives::terminal::paint`] /
//! [`crate::primitives::terminal::paint_divider`] (#810, PaintSurface
//! Phase 2c) — see that fn's doc for the divergence (per-cell
//! bold/italic/underline styling: this backend keeps `bold` via
//! `DWrite::draw_text_styled`, `italic`/`underline` are still not wired
//! up) resolved while unifying `gtk::terminal::draw_terminal_cells`,
//! `macos::terminal::draw_terminal_cells` and
//! `win::terminal::draw_terminal_cells` into one implementation.
//!
//! This module now carries no rasteriser code of its own — both deleted
//! free functions had zero call sites in `coord-tui`/`vimcode` (`grep -rn
//! "draw_terminal_cells\|draw_terminal_divider" ~/src/coord-tui/src
//! ~/src/vimcode/src` — the only hits there are `Backend::draw_terminal`/
//! `Backend::draw_terminal_divider` trait calls, whose signatures are
//! unchanged); [`crate::win::multi_section_view`]'s embedded `Terminal`
//! section body — the one in-tree caller of the old free function
//! outside `WinBackend` itself — now goes through
//! [`super::surface::D2dSurface`] instead (see that struct's module doc).
//!
//! Only compiled on `target_os = "windows"` — see `super::mod`'s
//! `#[cfg(target_os = "windows")] mod terminal;` and `backend.rs`'s
//! module docs.
#[cfg(test)]
mod tests {
use crate::event::Rect;
use crate::primitives::terminal::{Terminal, TerminalCell, TerminalCursorShape};
use crate::terminal_style::wide_glyph_x_scale;
use crate::theme::Theme;
use crate::types::{Color, WidgetId};
use crate::win::testing::HeadlessSurface;
use crate::win::text::{fill_rect, DWrite};
const W: u32 = 200;
const H: u32 = 120;
const CHAR_W: f32 = 10.0;
const LINE_H: f32 = 20.0;
fn cell(ch: char, fg: Color, bg: Color) -> TerminalCell {
TerminalCell {
text: ch.to_string(),
fg,
bg,
bold: false,
italic: false,
underline: false,
dim: false,
selected: false,
is_cursor: false,
is_find_match: false,
is_find_active: false,
cursor_shape: TerminalCursorShape::Block,
cursor_blinking: false,
}
}
fn dwrite() -> DWrite {
DWrite::new("Segoe UI", 10.0, None)
.expect("create DWrite")
.0
}
/// Paints `term` via `super::surface::D2dSurface` — the same adapter
/// `win::multi_section_view`'s embedded `Terminal` section body
/// uses — so these tests keep controlling `char_width`/`line_height`
/// explicitly instead of depending on a live `WinBackend`'s measured
/// font metrics.
fn paint(surface: &HeadlessSurface, dwrite: &DWrite, term: &Terminal, theme: &Theme) {
surface
.paint(|target| {
let mut raw = crate::win::surface::D2dSurface {
target,
dwrite: Some(dwrite),
};
crate::primitives::terminal::paint(
term, &mut raw, theme, 0.0, 0.0, W as f32, H as f32, LINE_H, CHAR_W, None,
);
})
.expect("paint");
}
/// A cell's background must paint at its own grid position before
/// the glyph is drawn — probing a corner the glyph doesn't reach
/// isolates the fill from font rasterisation.
#[test]
fn cell_bg_paints_at_its_grid_position() {
let magenta = Color::rgb(200, 30, 200);
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![vec![cell('A', Color::rgb(255, 255, 255), magenta)]],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
let px = surface.pixel_at(1, 1);
assert_eq!((px.r, px.g, px.b), (magenta.r, magenta.g, magenta.b));
}
/// #703 regression, mirroring `gtk`/`macos::terminal`'s
/// `wide_cell_background_spans_two_columns`: a double-width glyph
/// (CJK) followed by vt100's blank continuation cell must have its
/// background span both columns — the continuation cell's own
/// (different) background must NOT paint over the second half of the
/// wide glyph's cell.
#[test]
fn wide_cell_background_spans_two_columns() {
let magenta = Color::rgb(200, 30, 200);
let cyan = Color::rgb(30, 200, 200);
// The wide glyph's foreground is set equal to its background
// (magenta on magenta) so antialiased glyph ink can't shift the
// probed colour — same trick as the GTK/macOS twin tests.
let row = vec![cell('日', magenta, magenta), cell(' ', magenta, cyan)];
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![row],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
// Probe just past the first single-cell-width boundary, still
// within the wide glyph's two-column span: must be magenta, not
// cyan.
let probe_x = (CHAR_W * 1.5) as u32;
let px = surface.pixel_at(probe_x, 5);
assert_eq!(
(px.r, px.g, px.b),
(magenta.r, magenta.g, magenta.b),
"wide cell's background should span both columns, not be \
overpainted by the continuation cell's background"
);
}
/// #703: mirrors `gtk::terminal`'s `narrow_wide_glyph_is_stretched_to_fill_two_cells`
/// — every backend now shares `wide_glyph_x_scale`, so a CJK glyph
/// measuring 15px in an 18px (2 × 9px) box scales 1.2x here too.
#[test]
fn narrow_wide_glyph_is_stretched_to_fill_two_cells() {
let cell_w = 18.0;
let scale = wide_glyph_x_scale(15.0, cell_w);
assert!(
(scale - 1.2).abs() < 1e-9,
"15px glyph in an 18px box should scale 1.2x, got {scale}"
);
assert!((15.0 * scale - cell_w).abs() < 1e-9);
}
/// #703: mirrors `gtk::terminal`'s `exact_fit_wide_glyph_is_not_scaled`.
#[test]
fn exact_fit_wide_glyph_is_not_scaled() {
assert_eq!(wide_glyph_x_scale(18.0, 18.0), 1.0);
}
/// #703: mirrors `gtk::terminal`'s `over_wide_glyph_is_shrunk_into_box`.
#[test]
fn over_wide_glyph_is_shrunk_into_box() {
let scale = wide_glyph_x_scale(24.0, 18.0);
assert!(
scale < 1.0,
"24px glyph in 18px box should shrink, got {scale}"
);
assert!((24.0 * scale - 18.0).abs() < 1e-9);
}
/// Companion regression, mirroring `gtk`/`macos::terminal`'s
/// `narrow_cells_advance_by_single_char_width`: ordinary narrow
/// (single-width) cells must still advance by exactly `char_width` —
/// the wide-cell fix must not widen unrelated cells.
#[test]
fn narrow_cells_advance_by_single_char_width() {
let magenta = Color::rgb(200, 30, 200);
let cyan = Color::rgb(30, 200, 200);
let white = Color::rgb(255, 255, 255);
let row = vec![cell('A', white, magenta), cell('B', cyan, cyan)];
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![row],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
let probe_x = (CHAR_W * 1.5) as u32;
let px = surface.pixel_at(probe_x, 5);
assert_eq!(
(px.r, px.g, px.b),
(cyan.r, cyan.g, cyan.b),
"narrow cells must still advance by exactly char_width"
);
}
/// The cursor overlay swaps fg/bg: the cell background paints in
/// the cell's *foreground* colour.
#[test]
fn cursor_cell_inverts_fg_bg() {
let fg = Color::rgb(10, 220, 30);
let bg = Color::rgb(40, 40, 40);
let mut c = cell('X', fg, bg);
c.is_cursor = true;
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![vec![c]],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
let px = surface.pixel_at(1, 1);
assert_eq!((px.r, px.g, px.b), (fg.r, fg.g, fg.b));
}
/// A find-active match paints its cell background orange —
/// matching the GTK/macOS overlay convention.
#[test]
fn find_active_cell_paints_orange() {
let mut c = cell('z', Color::rgb(255, 255, 255), Color::rgb(20, 20, 20));
c.is_find_active = true;
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![vec![c]],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
let px = surface.pixel_at(1, 1);
assert_eq!((px.r, px.g, px.b), (255, 165, 0));
}
/// A selected cell uses `theme.selection_bg`.
#[test]
fn selected_cell_uses_theme_selection_bg() {
let mut c = cell('x', Color::rgb(255, 255, 255), Color::rgb(10, 10, 10));
c.selected = true;
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![vec![c]],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
let px = surface.pixel_at(1, 1);
assert_eq!(
(px.r, px.g, px.b),
(
theme.selection_bg.r,
theme.selection_bg.g,
theme.selection_bg.b
)
);
}
/// Rows past the pane bottom must be clipped, not bled — the
/// interactive-resize regression `gtk`/`macos` already guard
/// (quadraui#437 / #484).
#[test]
fn rows_past_the_pane_bottom_are_clipped() {
let magenta = Color::rgb(200, 30, 200);
let bg_sentinel = Color::rgb(1, 2, 3);
let cells: Vec<Vec<_>> = (0..60)
.map(|_| {
(0..20)
.map(|_| cell(' ', Color::rgb(255, 255, 255), magenta))
.collect()
})
.collect();
let term = Terminal {
id: WidgetId::new("term"),
cells,
scrollbar: None,
};
let pane_h = 60.0_f32;
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
surface
.paint(|target| {
let _ = fill_rect(target, Rect::new(0.0, 0.0, W as f32, H as f32), bg_sentinel);
let mut raw = crate::win::surface::D2dSurface {
target,
dwrite: Some(&dwrite),
};
crate::primitives::terminal::paint(
&term, &mut raw, &theme, 0.0, 0.0, W as f32, pane_h, LINE_H, CHAR_W, None,
);
})
.expect("paint");
let inside = surface.pixel_at(4, 4);
assert_eq!(
(inside.r, inside.g, inside.b),
(magenta.r, magenta.g, magenta.b)
);
let below = surface.pixel_at(4, H - 4);
assert_eq!(
(below.r, below.g, below.b),
(bg_sentinel.r, bg_sentinel.g, bg_sentinel.b),
"rows below the pane bottom must be clipped, not bled"
);
}
/// A bold cell paints via `DWrite::draw_text_styled` rather than
/// silently dropping the attribute — smoke-tested by confirming the
/// paint call succeeds and the cell's own bg still lands correctly
/// (glyph shape/weight isn't independently probable via pixel
/// colour without OCR, so this only guards the plumbing).
#[test]
fn bold_cell_paints_without_error() {
let mut c = cell('B', Color::rgb(255, 255, 255), Color::rgb(20, 20, 20));
c.bold = true;
let term = Terminal {
id: WidgetId::new("term"),
cells: vec![vec![c]],
scrollbar: None,
};
let surface = HeadlessSurface::new(W, H).expect("create surface");
let dwrite = dwrite();
let theme = Theme::default();
paint(&surface, &dwrite, &term, &theme);
let px = surface.pixel_at(1, 1);
assert_eq!((px.r, px.g, px.b), (20, 20, 20));
}
/// [`crate::primitives::terminal::paint_divider`] paints a 1-DIP
/// line at `x`.
#[test]
fn divider_paints_a_one_dip_line() {
let surface = HeadlessSurface::new(W, H).expect("create surface");
let theme = Theme::default();
surface
.paint(|target| {
let _ = fill_rect(
target,
Rect::new(0.0, 0.0, W as f32, H as f32),
Color::rgb(0, 0, 0),
);
let dwrite = dwrite();
let mut raw = crate::win::surface::D2dSurface {
target,
dwrite: Some(&dwrite),
};
crate::primitives::terminal::paint_divider(&mut raw, 50.0, 0.0, H as f32, &theme);
})
.expect("paint");
let on_line = surface.pixel_at(50, 10);
let off_line = surface.pixel_at(52, 10);
assert_eq!(
(on_line.r, on_line.g, on_line.b),
(theme.separator.r, theme.separator.g, theme.separator.b)
);
assert_eq!((off_line.r, off_line.g, off_line.b), (0, 0, 0));
}
}