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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// ARC-005 completed: file reduced from ~1004 lines to ~791 lines (target: <800).
//
// Extracted submodules:
// powerline.rs — Powerline fringe-extension logic (pure fn, no self access).
// `extend_powerline_fringes()` adjusts bg-quad x0/x1 to eliminate
// anti-aliased dark fringes at separator boundaries.
// block_char_render.rs — Geometric rendering of block/box-drawing characters via the text
// pipeline. `render_block_char_geometrically()` returns Some(new_idx)
// when rendered; caller continues the per-cell loop.
//
// Note: The glyph font-fallback loop was extracted to `CellRenderer::resolve_glyph_with_fallback()`
// in `atlas.rs` (ARC-004 / QA-003). The RLE bg-instance merge inner loop remains inlined
// as it mutates self.bg_instances in place with no clean free-function boundary.
//
// IMPORTANT invariants to preserve (see MEMORY.md and CLAUDE.md):
// • 3-phase draw ordering: bg instances → text instances → cursor overlays
// • `fill_default_bg_cells` controls default-bg skip in bg-image mode
// • `skip_solid_background` must NOT be used to gate default-bg rendering
//
// Tracking: Issues ARC-005 and ARC-009 in AUDIT.md.
use super::block_chars;
use super::instance_buffers::{
STIPPLE_OFF_PX, STIPPLE_ON_PX, UNDERLINE_HEIGHT_RATIO, compute_cursor_text_color,
};
use super::{BackgroundInstance, Cell, CellRenderer, PaneViewport, TextInstance};
use anyhow::Result;
use par_term_config::{SeparatorMark, color_u8x4_rgb_to_f32, color_u8x4_rgb_to_f32_a};
mod block_char_render;
mod cursor_overlays;
mod powerline;
mod separators;
use block_char_render::BlockCharRenderParams;
use cursor_overlays::CursorOverlayParams;
/// Atlas texture size in pixels. Must match the value used at atlas creation time.
/// See `PREFERRED_ATLAS_SIZE` in `pipeline.rs` and `atlas_size` on `CellRendererAtlas`.
pub(crate) const ATLAS_SIZE: f32 = 2048.0;
/// Parameters for rendering a single pane to a surface texture view.
pub struct PaneRenderViewParams<'a> {
pub viewport: &'a PaneViewport,
pub cells: &'a [Cell],
pub cols: usize,
pub rows: usize,
pub cursor_pos: Option<(usize, usize)>,
pub cursor_opacity: f32,
pub show_scrollbar: bool,
pub clear_first: bool,
pub skip_background_image: bool,
/// When true, emit background quads for default-bg cells (fills gaps in background-image mode).
/// Set to false in custom shader mode so the shader output shows through.
pub fill_default_bg_cells: bool,
pub separator_marks: &'a [SeparatorMark],
pub pane_background: Option<&'a par_term_config::PaneBackground>,
}
/// Parameters for building GPU instance buffers for a pane.
pub(super) struct PaneInstanceBuildParams<'a> {
pub viewport: &'a PaneViewport,
pub cells: &'a [Cell],
pub cols: usize,
pub rows: usize,
pub cursor_pos: Option<(usize, usize)>,
pub cursor_opacity: f32,
pub skip_solid_background: bool,
pub fill_default_bg_cells: bool,
pub separator_marks: &'a [SeparatorMark],
}
impl CellRenderer {
/// Render a single pane's content within a viewport to an existing surface texture
///
/// This method renders cells to a specific region of the render target,
/// using a GPU scissor rect to clip to the pane bounds.
///
/// # Arguments
/// * `surface_view` - The texture view to render to
/// * `viewport` - The pane's viewport (position, size, focus state, opacity)
/// * `cells` - The cells to render (should match viewport grid size)
/// * `cols` - Number of columns in the cell grid
/// * `rows` - Number of rows in the cell grid
/// * `cursor_pos` - Cursor position (col, row) within this pane, or None if no cursor
/// * `cursor_opacity` - Cursor opacity (0.0 = hidden, 1.0 = fully visible)
/// * `show_scrollbar` - Whether to render the scrollbar for this pane
/// * `clear_first` - If true, clears the viewport region before rendering
/// * `skip_background_image` - If true, skip rendering the background image. Use this
/// when the background image has already been rendered full-screen (for split panes).
pub fn render_pane_to_view(
&mut self,
surface_view: &wgpu::TextureView,
p: PaneRenderViewParams<'_>,
) -> Result<()> {
let PaneRenderViewParams {
viewport,
cells,
cols,
rows,
cursor_pos,
cursor_opacity,
show_scrollbar,
clear_first,
skip_background_image,
fill_default_bg_cells,
separator_marks,
pane_background,
} = p;
// Build instance buffers for this pane's cells.
// Returns cursor_overlay_start: the bg_instance index where cursor overlays begin.
// Used for 3-phase rendering (bgs → text → cursor overlays).
let cursor_overlay_start = self.build_pane_instance_buffers(PaneInstanceBuildParams {
viewport,
cells,
cols,
rows,
cursor_pos,
cursor_opacity,
skip_solid_background: skip_background_image,
fill_default_bg_cells,
separator_marks,
})?;
// Pre-update per-pane background uniform buffer and bind group if needed (must happen
// before the render pass). Buffers are allocated once and reused across frames.
// Per-pane backgrounds are explicit user overrides and always prepared, even when a
// custom shader or global background would normally be skipped.
let has_pane_bg = if let Some(pane_bg) = pane_background
&& let Some(ref path) = pane_bg.image_path
&& self.bg_state.pane_bg_cache.contains_key(path.as_str())
{
self.prepare_pane_bg_bind_group(
path.as_str(),
super::background::PaneBgBindGroupParams {
pane_x: viewport.x,
pane_y: viewport.y,
pane_width: viewport.width,
pane_height: viewport.height,
mode: pane_bg.mode,
opacity: pane_bg.opacity,
darken: pane_bg.darken,
},
);
true
} else {
false
};
// Retrieve cached path for use in the render pass (must be done before borrow in pass).
let pane_bg_path: Option<String> = if has_pane_bg {
pane_background
.and_then(|pb| pb.image_path.as_ref())
.map(|p| p.to_string())
} else {
None
};
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("pane render encoder"),
});
// Determine load operation and clear color
let load_op = if clear_first {
let clear_color = if self.bg_state.bg_is_solid_color {
wgpu::Color {
r: self.bg_state.solid_bg_color[0] as f64
* self.window_opacity as f64
* viewport.opacity as f64,
g: self.bg_state.solid_bg_color[1] as f64
* self.window_opacity as f64
* viewport.opacity as f64,
b: self.bg_state.solid_bg_color[2] as f64
* self.window_opacity as f64
* viewport.opacity as f64,
a: self.window_opacity as f64 * viewport.opacity as f64,
}
} else {
wgpu::Color {
r: self.background_color[0] as f64
* self.window_opacity as f64
* viewport.opacity as f64,
g: self.background_color[1] as f64
* self.window_opacity as f64
* viewport.opacity as f64,
b: self.background_color[2] as f64
* self.window_opacity as f64
* viewport.opacity as f64,
a: self.window_opacity as f64 * viewport.opacity as f64,
}
};
wgpu::LoadOp::Clear(clear_color)
} else {
wgpu::LoadOp::Load
};
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("pane render pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: surface_view,
resolve_target: None,
ops: wgpu::Operations {
load: load_op,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
// Set scissor rect to clip rendering to pane bounds
let (sx, sy, sw, sh) = viewport.to_scissor_rect();
render_pass.set_scissor_rect(sx, sy, sw, sh);
// Render per-pane background image within scissor rect.
// Per-pane backgrounds are explicit user overrides and always render,
// even when a custom shader or global background is active.
if let Some(ref path) = pane_bg_path
&& let Some(cached) = self.bg_state.pane_bg_uniform_cache.get(path.as_str())
{
render_pass.set_pipeline(&self.pipelines.bg_image_pipeline);
render_pass.set_bind_group(0, &cached.bind_group, &[]);
render_pass.set_vertex_buffer(0, self.buffers.vertex_buffer.slice(..));
render_pass.draw(0..4, 0..1);
}
self.emit_three_phase_draw_calls(
&mut render_pass,
cursor_overlay_start as u32,
self.buffers.actual_bg_instances as u32,
);
// Render scrollbar if requested (uses its own scissor rect internally)
if show_scrollbar {
// Reset scissor to full surface for scrollbar
render_pass.set_scissor_rect(0, 0, self.config.width, self.config.height);
self.scrollbar.render(&mut render_pass);
}
}
self.queue.submit(std::iter::once(encoder.finish()));
Ok(())
}
/// Build instance buffers for a pane's cells with viewport offset.
///
/// Similar to `build_instance_buffers` but adjusts all positions to be relative to the
/// viewport origin. Also appends cursor overlay instances (beam bar and hollow borders)
/// after the cell background instances.
///
/// Returns the index in `bg_instances` where cursor overlays begin (`cursor_overlay_start`).
/// The caller uses this for 3-phase rendering: cell bgs, text, then cursor overlays on top.
///
/// `skip_solid_background`: if true, skip the solid background fill for the viewport
/// (use when a custom shader or background image was already rendered full-screen).
fn build_pane_instance_buffers(&mut self, p: PaneInstanceBuildParams<'_>) -> Result<usize> {
let PaneInstanceBuildParams {
viewport,
cells,
cols,
rows,
cursor_pos,
cursor_opacity,
skip_solid_background,
fill_default_bg_cells,
separator_marks,
} = p;
// Clear previous instance buffers
for instance in &mut self.bg_instances {
instance.size = [0.0, 0.0];
instance.color = [0.0, 0.0, 0.0, 0.0];
}
// Add a background rectangle covering the entire pane viewport (unless skipped)
// This ensures the pane has a proper background even when cells are skipped.
// Skip when a custom shader or background image was already rendered full-screen.
let bg_start_index = if !skip_solid_background && !self.bg_instances.is_empty() {
let bg_color = self.background_color;
let opacity = self.window_opacity * viewport.opacity;
let width_f = self.config.width as f32;
let height_f = self.config.height as f32;
self.bg_instances[0] = super::types::BackgroundInstance {
position: [
viewport.x / width_f * 2.0 - 1.0,
1.0 - (viewport.y / height_f * 2.0),
],
size: [
viewport.width / width_f * 2.0,
viewport.height / height_f * 2.0,
],
color: [
bg_color[0] * opacity,
bg_color[1] * opacity,
bg_color[2] * opacity,
opacity,
],
};
1 // Start cell backgrounds at index 1
} else {
0 // Start cell backgrounds at index 0 (no viewport fill)
};
for instance in &mut self.text_instances {
instance.size = [0.0, 0.0];
}
// Start at bg_start_index (1 if viewport fill was added, 0 otherwise)
let mut bg_index = bg_start_index;
let mut text_index = 0;
// Content offset - positions are relative to content area (with padding applied)
let (content_x, content_y) = viewport.content_origin();
let opacity_multiplier = viewport.opacity;
for row in 0..rows {
let row_start = row * cols;
let row_end = (row + 1) * cols;
if row_start >= cells.len() {
break;
}
let row_cells = &cells[row_start..row_end.min(cells.len())];
// Background - use RLE to merge consecutive cells with same color
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() < 0.001
&& (bg_f[1] - self.background_color[1]).abs() < 0.001
&& (bg_f[2] - self.background_color[2]).abs() < 0.001;
// Check for cursor at this position (position check only, no opacity gate)
let cursor_at_cell = cursor_pos.is_some_and(|(cx, cy)| cx == col && cy == row)
&& !self.cursor.hidden_for_shader;
// Hollow cursor (unfocused + Hollow style) must show regardless of blink opacity
let render_hollow_here = cursor_at_cell
&& !self.is_focused
&& self.cursor.unfocused_style == par_term_config::UnfocusedCursorStyle::Hollow;
let has_cursor = (cursor_at_cell && cursor_opacity > 0.0) || render_hollow_here;
// 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()
};
// Skip default-bg cells only when NOT in background-image/shader mode.
// When skip_solid_background is true (background image or custom shader active),
// no viewport fill is drawn, so default-bg cells between colored segments would
// show the background image through — causing visible gaps/lines in the tmux
// status bar. In that mode we render them with the theme background color instead.
// Skip default-bg cells unless fill_default_bg_cells is set (background-image mode).
// In normal mode: viewport fill quad covers them — no individual quad needed.
// In shader mode: shader output must show through — do not paint over it.
// In bg-image mode: fill_default_bg_cells=true — render with theme bg color to
// close gaps that would otherwise show the background image unexpectedly.
if is_half_block || (is_default_bg && !has_cursor && !fill_default_bg_cells) {
col += 1;
continue;
}
// Calculate background color with alpha and pane opacity
let bg_alpha =
if self.transparency_affects_only_default_background && !is_default_bg {
1.0
} else {
self.window_opacity
};
let pane_alpha = bg_alpha * opacity_multiplier;
let mut bg_color = color_u8x4_rgb_to_f32_a(cell.bg_color, pane_alpha);
// TODO(QA-002/QA-008): extract into `render_cursor_cell()` helper.
// Signature would be:
// fn render_cursor_cell(&mut self, col: usize, row: usize,
// content_x: f32, content_y: f32, bg_color: [f32; 4],
// cursor_opacity: f32, render_hollow_here: bool,
// bg_index: &mut usize)
// Handle cursor at this position
if has_cursor {
use par_term_emu_core_rust::cursor::CursorStyle;
match self.cursor.style {
CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => {
if !render_hollow_here {
// Solid block cursor: blend cursor color into background
for (bg, &cursor) in
bg_color.iter_mut().take(3).zip(&self.cursor.color)
{
*bg = *bg * (1.0 - cursor_opacity) + cursor * cursor_opacity;
}
bg_color[3] = bg_color[3].max(cursor_opacity * opacity_multiplier);
}
// If hollow: keep original background color (outline added as overlay)
}
_ => {}
}
// Cursor cell can't be merged
// Snap to pixel boundaries to match text pipeline alignment
let x0 = (content_x + col as f32 * self.grid.cell_width).round();
let x1 = (content_x + (col + 1) as f32 * self.grid.cell_width).round();
let y0 = (content_y + row as f32 * self.grid.cell_height).round();
let y1 = (content_y + (row + 1) as f32 * self.grid.cell_height).round();
if bg_index < self.buffers.max_bg_instances {
self.bg_instances[bg_index] = 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,
};
bg_index += 1;
}
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_cursor_at_cell = cursor_pos
.is_some_and(|(cx, cy)| cx == col && cy == row)
&& !self.cursor.hidden_for_shader;
let next_hollow = next_cursor_at_cell
&& !self.is_focused
&& self.cursor.unfocused_style
== par_term_config::UnfocusedCursorStyle::Hollow;
let next_has_cursor =
(next_cursor_at_cell && cursor_opacity > 0.0) || next_hollow;
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 = (content_x + start_col as f32 * self.grid.cell_width).round();
let x1 =
(content_x + (start_col + run_length) as f32 * self.grid.cell_width).round();
let y0 = (content_y + row as f32 * self.grid.cell_height).round();
let y1 = (content_y + (row + 1) as f32 * self.grid.cell_height).round();
// Extend the colored bg quad 1 px under adjacent powerline separator glyphs
// to eliminate the dark fringe at their anti-aliased edges.
// See powerline.rs for full rationale.
let (x0, x1) =
powerline::extend_powerline_fringes(powerline::PowerlineFringeParams {
row_cells,
start_col,
col,
x0,
x1,
skip_solid_background,
is_default_bg,
background_color: self.background_color,
});
if bg_index < self.buffers.max_bg_instances {
self.bg_instances[bg_index] = 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,
};
bg_index += 1;
}
}
// Text rendering
let natural_line_height =
self.font.font_ascent + self.font.font_descent + self.font.font_leading;
let vertical_padding = (self.grid.cell_height - natural_line_height).max(0.0) / 2.0;
let baseline_y = content_y
+ (row as f32 * self.grid.cell_height)
+ vertical_padding
+ self.font.font_ascent;
// Compute text alpha - force opaque if keep_text_opaque is enabled
let text_alpha = if self.keep_text_opaque {
opacity_multiplier // Only apply pane dimming, not window transparency
} else {
self.window_opacity * opacity_multiplier
};
// Check if this row has the cursor and it's a visible block cursor
// (for cursor text color override in split-pane rendering)
let cursor_is_block_on_this_row = {
use par_term_emu_core_rust::cursor::CursorStyle;
cursor_pos.is_some_and(|(_, cy)| cy == row)
&& cursor_opacity > 0.0
&& !self.cursor.hidden_for_shader
&& matches!(
self.cursor.style,
CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock
)
&& (self.is_focused
|| self.cursor.unfocused_style
== par_term_config::UnfocusedCursorStyle::Same)
};
for (col_idx, cell) in row_cells.iter().enumerate() {
if cell.wide_char_spacer || cell.grapheme == " " {
continue;
}
// Avoid Vec<char> allocation: use iterator-based char access.
let Some(ch) = cell.grapheme.chars().next() else {
continue;
};
let second_char = cell.grapheme.chars().nth(1);
// grapheme_len is 1, 2, or "more than 2" — we stop counting at 3.
let grapheme_len = match second_char {
None => 1usize,
Some(_) => {
if cell.grapheme.chars().nth(2).is_none() {
2
} else {
3
}
}
};
// Determine text color - apply cursor_text_color (or auto-contrast) when the
// block cursor is on this cell, otherwise use the cell's foreground color.
let render_fg_color: [f32; 4] = if cursor_is_block_on_this_row
&& cursor_pos.is_some_and(|(cx, _)| cx == col_idx)
{
compute_cursor_text_color(self.cursor.color, self.cursor.text_color, text_alpha)
} else {
color_u8x4_rgb_to_f32_a(cell.fg_color, text_alpha)
};
// Classify character for block/box-drawing detection and glyph snapping.
let char_type = block_chars::classify_char(ch);
// Attempt geometric rendering for block/box-drawing characters.
// See block_char_render.rs for the full implementation.
let block_x0 = (content_x + col_idx as f32 * self.grid.cell_width).round();
let block_y0 = (content_y + row as f32 * self.grid.cell_height).round();
let block_y1 = (content_y + (row + 1) as f32 * self.grid.cell_height).round();
if let Some(new_idx) = self.render_block_char_geometrically(BlockCharRenderParams {
cell,
ch,
grapheme_len,
x0_pixel: block_x0,
y0_pixel: block_y0,
y1_pixel: block_y1,
render_fg_color,
text_alpha,
text_index,
}) {
text_index = new_idx;
continue;
}
// Check if this character should be rendered as a monochrome symbol.
// Also handle symbol + VS16 (U+FE0F): strip VS16, render monochrome.
let (force_monochrome, base_char) = if grapheme_len == 1 {
(super::atlas::should_render_as_symbol(ch), ch)
} else if grapheme_len == 2
&& second_char == Some('\u{FE0F}')
&& super::atlas::should_render_as_symbol(ch)
{
// Symbol + VS16: strip VS16 and render base char as monochrome
(true, ch)
} else {
(false, ch)
};
// Resolve a renderable glyph via the shared font-fallback helper (ARC-004 / QA-003).
// This replaces the duplicated excluded_fonts/get_or_rasterize_glyph loop
// that previously existed in both pane_render/mod.rs and text_instance_builder.rs.
let resolved_info = self.resolve_glyph_with_fallback(
base_char,
&cell.grapheme,
cell.bold,
cell.italic,
force_monochrome,
);
if let Some(info) = resolved_info {
let char_w = if cell.wide_char {
self.grid.cell_width * 2.0
} else {
self.grid.cell_width
};
let x0 = content_x + col_idx as f32 * self.grid.cell_width;
let y0 = content_y + row as f32 * self.grid.cell_height;
let x1 = x0 + char_w;
let y1 = y0 + self.grid.cell_height;
let cell_w = x1 - x0;
let cell_h = y1 - y0;
let scale_x = cell_w / char_w;
let scale_y = cell_h / self.grid.cell_height;
let baseline_offset =
baseline_y - (content_y + row as f32 * self.grid.cell_height);
let glyph_left = x0 + (info.bearing_x * scale_x).round();
let baseline_in_cell = (baseline_offset * scale_y).round();
let glyph_top = y0 + baseline_in_cell - info.bearing_y;
let render_w = info.width as f32 * scale_x;
let render_h = info.height as f32 * scale_y;
let (final_left, final_top, final_w, final_h) = if grapheme_len == 1
&& matches!(
char_type,
block_chars::BlockCharType::Symbol
| block_chars::BlockCharType::Geometric
) {
let height_scale = cell_h / render_h;
let width_scale = cell_w / render_w;
let symbol_scale = height_scale.min(width_scale).max(1.0);
let final_w = render_w * symbol_scale;
let final_h = render_h * symbol_scale;
(
x0 + (cell_w - final_w) / 2.0,
y0 + (cell_h - final_h) / 2.0,
final_w,
final_h,
)
} else if grapheme_len == 1 && block_chars::should_snap_to_boundaries(char_type)
{
block_chars::snap_glyph_to_cell(block_chars::SnapGlyphParams {
glyph_left,
glyph_top,
render_w,
render_h,
cell_x0: x0,
cell_y0: y0,
cell_x1: x1,
cell_y1: y1,
snap_threshold: 3.0,
extension: 0.5,
})
} else {
(glyph_left, glyph_top, render_w, render_h)
};
if text_index < self.buffers.max_text_instances {
self.text_instances[text_index] = TextInstance {
position: [
final_left / self.config.width as f32 * 2.0 - 1.0,
1.0 - (final_top / self.config.height as f32 * 2.0),
],
size: [
final_w / self.config.width as f32 * 2.0,
final_h / self.config.height as f32 * 2.0,
],
tex_offset: [info.x as f32 / ATLAS_SIZE, info.y as f32 / ATLAS_SIZE],
tex_size: [
info.width as f32 / ATLAS_SIZE,
info.height as f32 / ATLAS_SIZE,
],
color: render_fg_color,
is_colored: if info.is_colored { 1 } else { 0 },
};
text_index += 1;
}
}
}
// Underlines: emit a thin rectangle at the bottom of each underlined cell.
// Mirrors the logic in text_instance_builder.rs but uses pane-local coordinates.
{
let underline_thickness = (self.grid.cell_height * UNDERLINE_HEIGHT_RATIO)
.max(1.0)
.round();
let tex_offset = [
self.atlas.solid_pixel_offset.0 as f32 / ATLAS_SIZE,
self.atlas.solid_pixel_offset.1 as f32 / ATLAS_SIZE,
];
let tex_size = [1.0 / ATLAS_SIZE, 1.0 / ATLAS_SIZE];
let y0 = content_y + (row + 1) as f32 * self.grid.cell_height - underline_thickness;
let ndc_y = 1.0 - (y0 / self.config.height as f32 * 2.0);
let ndc_h = underline_thickness / self.config.height as f32 * 2.0;
let is_stipple =
self.link_underline_style == par_term_config::LinkUnderlineStyle::Stipple;
let stipple_period = STIPPLE_ON_PX + STIPPLE_OFF_PX;
for col_idx in 0..cols {
if row_start + col_idx >= cells.len() {
break;
}
let cell = &cells[row_start + col_idx];
if !cell.underline {
continue;
}
let fg = color_u8x4_rgb_to_f32_a(cell.fg_color, text_alpha);
let cell_x0 = content_x + col_idx as f32 * self.grid.cell_width;
if is_stipple {
let mut px = 0.0;
while px < self.grid.cell_width
&& text_index < self.buffers.max_text_instances
{
let seg_w = STIPPLE_ON_PX.min(self.grid.cell_width - px);
let x = cell_x0 + px;
self.text_instances[text_index] = TextInstance {
position: [x / self.config.width as f32 * 2.0 - 1.0, ndc_y],
size: [seg_w / self.config.width as f32 * 2.0, ndc_h],
tex_offset,
tex_size,
color: fg,
is_colored: 0,
};
text_index += 1;
px += stipple_period;
}
} else if text_index < self.buffers.max_text_instances {
self.text_instances[text_index] = TextInstance {
position: [cell_x0 / self.config.width as f32 * 2.0 - 1.0, ndc_y],
size: [self.grid.cell_width / self.config.width as f32 * 2.0, ndc_h],
tex_offset,
tex_size,
color: fg,
is_colored: 0,
};
text_index += 1;
}
}
}
}
// Inject command separator line instances — see separators.rs
bg_index = self.emit_separator_instances(
separator_marks,
cols,
rows,
content_x,
content_y,
opacity_multiplier,
bg_index,
);
// --- Cursor overlays (beam/underline bar + hollow borders) ---
// These are rendered in Phase 3 (on top of text) via the 3-phase draw in render_pane_to_view.
// Record where cursor overlays start — everything after this index is an overlay.
let cursor_overlay_start = bg_index;
if let Some((cursor_col, cursor_row)) = cursor_pos {
let cursor_x0 = content_x + cursor_col as f32 * self.grid.cell_width;
let cursor_x1 = cursor_x0 + self.grid.cell_width;
let cursor_y0 = (content_y + cursor_row as f32 * self.grid.cell_height).round();
let cursor_y1 = (content_y + (cursor_row + 1) as f32 * self.grid.cell_height).round();
// Emit guide, shadow, beam/underline bar, hollow outline — see cursor_overlays.rs
bg_index = self.emit_cursor_overlays(
CursorOverlayParams {
cursor_x0,
cursor_x1,
cursor_y0,
cursor_y1,
cols,
content_x,
cursor_opacity,
},
bg_index,
);
}
// Update actual instance counts for draw calls
self.buffers.actual_bg_instances = bg_index;
self.buffers.actual_text_instances = text_index;
// Upload only the used portion of instance buffers to GPU.
// Each pane typically uses a fraction of the full-window buffer, so uploading
// only [0..count] instead of the entire array significantly reduces per-pane
// staging bandwidth — critical when rendering many split panes per frame.
if bg_index > 0 {
self.queue.write_buffer(
&self.buffers.bg_instance_buffer,
0,
bytemuck::cast_slice(&self.bg_instances[..bg_index]),
);
}
if text_index > 0 {
self.queue.write_buffer(
&self.buffers.text_instance_buffer,
0,
bytemuck::cast_slice(&self.text_instances[..text_index]),
);
}
Ok(cursor_overlay_start)
}
}