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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use super::instance_buffers::{
COLOR_COMPONENT_EPSILON, CURSOR_BOOST_MAX_ALPHA, CURSOR_OVERLAY_SLOTS, GUTTER_WIDTH_CELLS,
HOLLOW_CURSOR_BORDER_PX,
};
use super::{BackgroundInstance, Cell, CellRenderer};
use par_term_config::{color_u8x4_rgb_to_f32, color_u8x4_rgb_to_f32_a};
impl CellRenderer {
/// Build background instances for a single row, populating `self.scratch_row_bg`.
///
/// Uses RLE to merge consecutive cells with the same background color, eliminating
/// seams between adjacent same-colored cells. The scratch buffer is cleared on entry
/// and padded to `self.grid.cols` entries on return.
pub(crate) fn build_row_bg_instances(&mut self, row: usize, row_cells: &[Cell]) {
let mut col = 0;
while col < row_cells.len() {
let cell = &row_cells[col];
let bg_f = color_u8x4_rgb_to_f32(cell.bg_color);
let is_default_bg = (bg_f[0] - self.background_color[0]).abs()
< COLOR_COMPONENT_EPSILON
&& (bg_f[1] - self.background_color[1]).abs() < COLOR_COMPONENT_EPSILON
&& (bg_f[2] - self.background_color[2]).abs() < COLOR_COMPONENT_EPSILON;
// Check for cursor at this position, accounting for unfocused state
let cursor_at_cell = !self.cursor.hidden_for_shader
&& self.cursor.pos.1 == row
&& self.cursor.pos.0 == col;
let cursor_visible = self.cursor.opacity > 0.0 && cursor_at_cell;
// Hollow cursor (unfocused + Hollow style) must always show regardless of blink opacity.
let render_hollow_here = cursor_at_cell
&& !self.is_focused
&& self.cursor.unfocused_style == par_term_config::UnfocusedCursorStyle::Hollow;
// Handle unfocused cursor visibility
let has_cursor = if render_hollow_here {
// Hollow cursor: always show the cell reservation even in blink-off phase
true
} else if cursor_visible && !self.is_focused {
match self.cursor.unfocused_style {
par_term_config::UnfocusedCursorStyle::Hidden => false,
par_term_config::UnfocusedCursorStyle::Same => true,
par_term_config::UnfocusedCursorStyle::Hollow => {
// Should be unreachable: Hollow is handled by `render_hollow_here` above.
// Log and treat as hidden to avoid a panic in production.
log::warn!(
"[RENDER] Unexpected Hollow cursor variant in unfocused_style arm \
— treating as Hidden"
);
false
}
}
} else {
cursor_visible
};
// Skip cells with half-block characters (▄/▀).
// These are rendered entirely through the text pipeline to avoid
// cross-pipeline coordinate seams that cause visible banding.
let is_half_block = {
let mut chars = cell.grapheme.chars();
matches!(chars.next(), Some('\u{2580}' | '\u{2584}')) && chars.next().is_none()
};
if is_half_block || (is_default_bg && !has_cursor) {
col += 1;
continue;
}
// Calculate background color with alpha
let bg_alpha = if self.transparency_affects_only_default_background && !is_default_bg {
1.0
} else {
self.window_opacity
};
let mut bg_color = color_u8x4_rgb_to_f32_a(cell.bg_color, bg_alpha);
// Handle cursor at this position
if has_cursor {
use par_term_emu_core_rust::cursor::CursorStyle;
// render_hollow_here was computed above; reuse it.
let render_hollow = render_hollow_here;
match self.cursor.style {
CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => {
if render_hollow {
// Hollow cursor: don't fill the cell, outline will be added later
// Keep original background color
} else {
// Solid block cursor
for (bg, &cursor) in bg_color.iter_mut().take(3).zip(&self.cursor.color)
{
*bg = *bg * (1.0 - self.cursor.opacity)
+ cursor * self.cursor.opacity;
}
bg_color[3] = bg_color[3].max(self.cursor.opacity);
}
}
_ => {}
}
// Cursor cell can't be merged, render it alone
// Snap to pixel boundaries to match text pipeline alignment
let x0 = (self.grid.window_padding
+ self.grid.content_offset_x
+ col as f32 * self.grid.cell_width)
.round();
let x1 = (self.grid.window_padding
+ self.grid.content_offset_x
+ (col + 1) as f32 * self.grid.cell_width)
.round();
// Snap Y to pixel boundaries to match text pipeline alignment
let y0 = (self.grid.window_padding
+ self.grid.content_offset_y
+ row as f32 * self.grid.cell_height)
.round();
let y1 = (self.grid.window_padding
+ self.grid.content_offset_y
+ (row + 1) as f32 * self.grid.cell_height)
.round();
self.scratch_row_bg.push(BackgroundInstance {
position: [
x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - (y0 / self.config.height as f32 * 2.0),
],
size: [
(x1 - x0) / self.config.width as f32 * 2.0,
(y1 - y0) / self.config.height as f32 * 2.0,
],
color: bg_color,
});
col += 1;
continue;
}
// RLE: Find run of consecutive cells with same background color
let start_col = col;
let run_color = cell.bg_color;
col += 1;
while col < row_cells.len() {
let next_cell = &row_cells[col];
let next_has_cursor = self.cursor.opacity > 0.0
&& !self.cursor.hidden_for_shader
&& self.cursor.pos.1 == row
&& self.cursor.pos.0 == col;
// Stop run if color differs, cursor is here, or next cell is a half-block
let next_is_half_block = {
let mut chars = next_cell.grapheme.chars();
matches!(chars.next(), Some('\u{2580}' | '\u{2584}')) && chars.next().is_none()
};
if next_cell.bg_color != run_color || next_has_cursor || next_is_half_block {
break;
}
col += 1;
}
let run_length = col - start_col;
// Create single quad spanning entire run.
// Snap all edges to pixel boundaries to match the text pipeline and eliminate
// sub-pixel gaps between adjacent differently-colored cell runs.
let x0 = (self.grid.window_padding
+ self.grid.content_offset_x
+ start_col as f32 * self.grid.cell_width)
.round();
let x1 = (self.grid.window_padding
+ self.grid.content_offset_x
+ (start_col + run_length) as f32 * self.grid.cell_width)
.round();
let y0 = (self.grid.window_padding
+ self.grid.content_offset_y
+ row as f32 * self.grid.cell_height)
.round();
let y1 = (self.grid.window_padding
+ self.grid.content_offset_y
+ (row + 1) as f32 * self.grid.cell_height)
.round();
let pos_x = x0 / self.config.width as f32 * 2.0 - 1.0;
let pos_y = 1.0 - (y0 / self.config.height as f32 * 2.0);
let sz_x = (x1 - x0) / self.config.width as f32 * 2.0;
let sz_y = (y1 - y0) / self.config.height as f32 * 2.0;
self.scratch_row_bg.push(BackgroundInstance {
position: [pos_x, pos_y],
size: [sz_x, sz_y],
color: bg_color,
});
}
// Pad row_bg to expected size with empty instances
// (RLE creates fewer instances than cells, but buffer expects cols entries)
while self.scratch_row_bg.len() < self.grid.cols {
self.scratch_row_bg.push(BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
});
}
}
/// Build cursor overlay background instances for the CURSOR_OVERLAY_SLOTS slots.
///
/// Returns a `Vec` of exactly `CURSOR_OVERLAY_SLOTS` entries. Slots that are not
/// active carry a zero-sized transparent instance so the GPU draw call is a no-op
/// for those entries.
///
/// Slot layout:
/// [0] cursor overlay (beam/underline, from `cursor.overlay`)
/// [1] cursor guide line
/// [2] cursor shadow
/// [3] cursor boost glow
/// [4-7] hollow cursor outline (top, bottom, left, right)
/// [8-9] reserved (zero)
pub(crate) fn build_cursor_overlay_instances(&self) -> Vec<BackgroundInstance> {
let mut overlay_instances = vec![
BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
};
CURSOR_OVERLAY_SLOTS
];
// Check if cursor should be visible
let cursor_visible = self.cursor.opacity > 0.0
&& !self.cursor.hidden_for_shader
&& (self.is_focused
|| self.cursor.unfocused_style != par_term_config::UnfocusedCursorStyle::Hidden);
// Calculate cursor pixel positions
let cursor_col = self.cursor.pos.0;
let cursor_row = self.cursor.pos.1;
let cursor_x0 = self.grid.window_padding
+ self.grid.content_offset_x
+ cursor_col as f32 * self.grid.cell_width;
let cursor_x1 = cursor_x0 + self.grid.cell_width;
let cursor_y0 = self.grid.window_padding
+ self.grid.content_offset_y
+ cursor_row as f32 * self.grid.cell_height;
let cursor_y1 = cursor_y0 + self.grid.cell_height;
// Slot 0: Cursor overlay (beam/underline) — computed fresh each frame from current
// grid dimensions and cursor color. Computing here (not cached in cursor.overlay)
// avoids stale coordinates after a resize or stale color after update_cursor_color().
overlay_instances[0] = if cursor_visible {
use par_term_emu_core_rust::cursor::CursorStyle;
let w = self.config.width as f32;
let h = self.config.height as f32;
let cc = self.cursor.color;
let op = self.cursor.opacity;
match self.cursor.style {
CursorStyle::SteadyBar | CursorStyle::BlinkingBar => BackgroundInstance {
position: [cursor_x0 / w * 2.0 - 1.0, 1.0 - (cursor_y0 / h * 2.0)],
size: [2.0 / w * 2.0, (cursor_y1 - cursor_y0) / h * 2.0],
color: [cc[0], cc[1], cc[2], op],
},
CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => {
BackgroundInstance {
position: [
cursor_x0 / w * 2.0 - 1.0,
1.0 - ((cursor_y1 - 2.0) / h * 2.0),
],
size: [(cursor_x1 - cursor_x0) / w * 2.0, 2.0 / h * 2.0],
color: [cc[0], cc[1], cc[2], op],
}
}
_ => BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
},
}
} else {
BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
}
};
// Slot 1: Cursor guide (horizontal line spanning full width at cursor row)
if cursor_visible && self.cursor.guide_enabled {
let guide_x0 = self.grid.window_padding + self.grid.content_offset_x;
let guide_x1 =
self.config.width as f32 - self.grid.window_padding - self.grid.content_inset_right;
overlay_instances[1] = BackgroundInstance {
position: [
guide_x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - (cursor_y0 / self.config.height as f32 * 2.0),
],
size: [
(guide_x1 - guide_x0) / self.config.width as f32 * 2.0,
(cursor_y1 - cursor_y0) / self.config.height as f32 * 2.0,
],
color: self.cursor.guide_color,
};
}
// Slot 2: Cursor shadow (offset rectangle behind cursor)
if cursor_visible && self.cursor.shadow_enabled {
let shadow_x0 = cursor_x0 + self.cursor.shadow_offset[0];
let shadow_y0 = cursor_y0 + self.cursor.shadow_offset[1];
overlay_instances[2] = BackgroundInstance {
position: [
shadow_x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - (shadow_y0 / self.config.height as f32 * 2.0),
],
size: [
self.grid.cell_width / self.config.width as f32 * 2.0,
self.grid.cell_height / self.config.height as f32 * 2.0,
],
color: self.cursor.shadow_color,
};
}
// Slot 3: Cursor boost glow (larger rectangle around cursor with low opacity)
if cursor_visible && self.cursor.boost > 0.0 {
let glow_expand = 4.0 * self.scale_factor * self.cursor.boost; // Expand by up to 4 logical pixels
let glow_x0 = cursor_x0 - glow_expand;
let glow_y0 = cursor_y0 - glow_expand;
let glow_w = self.grid.cell_width + glow_expand * 2.0;
let glow_h = self.grid.cell_height + glow_expand * 2.0;
overlay_instances[3] = BackgroundInstance {
position: [
glow_x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - (glow_y0 / self.config.height as f32 * 2.0),
],
size: [
glow_w / self.config.width as f32 * 2.0,
glow_h / self.config.height as f32 * 2.0,
],
color: [
self.cursor.boost_color[0],
self.cursor.boost_color[1],
self.cursor.boost_color[2],
self.cursor.boost * CURSOR_BOOST_MAX_ALPHA * self.cursor.opacity,
],
};
}
// Slots 4-7: Hollow cursor outline (4 thin rectangles forming a border).
// Independent of opacity so the hollow outline is always visible when unfocused,
// even if the cursor blink animation is in the off phase.
let render_hollow = !self.cursor.hidden_for_shader
&& !self.is_focused
&& self.cursor.unfocused_style == par_term_config::UnfocusedCursorStyle::Hollow;
if render_hollow {
{
// Hollow cursor border is always fully opaque — independent of blink phase.
// The cursor must remain visible even when opacity=0 (blink-off cycle).
let border_width = HOLLOW_CURSOR_BORDER_PX;
let color = [
self.cursor.color[0],
self.cursor.color[1],
self.cursor.color[2],
1.0,
];
// Top border
overlay_instances[4] = BackgroundInstance {
position: [
cursor_x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - (cursor_y0 / self.config.height as f32 * 2.0),
],
size: [
self.grid.cell_width / self.config.width as f32 * 2.0,
border_width / self.config.height as f32 * 2.0,
],
color,
};
// Bottom border
overlay_instances[5] = BackgroundInstance {
position: [
cursor_x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - ((cursor_y1 - border_width) / self.config.height as f32 * 2.0),
],
size: [
self.grid.cell_width / self.config.width as f32 * 2.0,
border_width / self.config.height as f32 * 2.0,
],
color,
};
// Left border
overlay_instances[6] = BackgroundInstance {
position: [
cursor_x0 / self.config.width as f32 * 2.0 - 1.0,
1.0 - ((cursor_y0 + border_width) / self.config.height as f32 * 2.0),
],
size: [
border_width / self.config.width as f32 * 2.0,
(self.grid.cell_height - border_width * 2.0) / self.config.height as f32
* 2.0,
],
color,
};
// Right border
overlay_instances[7] = BackgroundInstance {
position: [
(cursor_x1 - border_width) / self.config.width as f32 * 2.0 - 1.0,
1.0 - ((cursor_y0 + border_width) / self.config.height as f32 * 2.0),
],
size: [
border_width / self.config.width as f32 * 2.0,
(self.grid.cell_height - border_width * 2.0) / self.config.height as f32
* 2.0,
],
color,
};
}
}
overlay_instances
}
/// Build separator line background instances (one per row).
///
/// Returns a `Vec` of exactly `self.grid.rows` entries. Inactive rows carry
/// a zero-sized transparent instance.
pub(crate) fn build_separator_instances(&self) -> Vec<BackgroundInstance> {
let mut separator_instances = vec![
BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
};
self.grid.rows
];
if self.separator.enabled {
let width_f = self.config.width as f32;
let height_f = self.config.height as f32;
for &(screen_row, exit_code, custom_color) in &self.separator.visible_marks {
if screen_row < self.grid.rows {
let x0 = self.grid.window_padding + self.grid.content_offset_x;
let x1 = width_f - self.grid.window_padding - self.grid.content_inset_right;
let y0 = self.grid.window_padding
+ self.grid.content_offset_y
+ screen_row as f32 * self.grid.cell_height;
let color = self.separator_color(exit_code, custom_color, 1.0);
separator_instances[screen_row] = BackgroundInstance {
position: [x0 / width_f * 2.0 - 1.0, 1.0 - (y0 / height_f * 2.0)],
size: [
(x1 - x0) / width_f * 2.0,
self.separator.thickness / height_f * 2.0,
],
color,
};
}
}
}
separator_instances
}
/// Build gutter indicator background instances (one per row).
///
/// Returns a `Vec` of exactly `self.grid.rows` entries. Rows without a gutter
/// indicator carry a zero-sized transparent instance.
pub(crate) fn build_gutter_instances(&self) -> Vec<BackgroundInstance> {
let mut gutter_instances = vec![
BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
};
self.grid.rows
];
if !self.gutter_indicators.is_empty() {
let width_f = self.config.width as f32;
let height_f = self.config.height as f32;
for &(screen_row, color) in &self.gutter_indicators {
if screen_row < self.grid.rows {
let x0 = self.grid.window_padding + self.grid.content_offset_x;
let x1 = x0 + GUTTER_WIDTH_CELLS * self.grid.cell_width;
let y0 = self.grid.window_padding
+ self.grid.content_offset_y
+ screen_row as f32 * self.grid.cell_height;
gutter_instances[screen_row] = BackgroundInstance {
position: [x0 / width_f * 2.0 - 1.0, 1.0 - (y0 / height_f * 2.0)],
size: [
(x1 - x0) / width_f * 2.0,
self.grid.cell_height / height_f * 2.0,
],
color,
};
}
}
}
gutter_instances
}
}