par-term-render 0.6.6

GPU-accelerated rendering engine for par-term terminal emulator
Documentation
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
use super::block_chars;
use super::instance_buffers::{
    GLYPH_SNAP_EXTENSION_PX, GLYPH_SNAP_THRESHOLD_PX, STIPPLE_OFF_PX, STIPPLE_ON_PX,
    UNDERLINE_HEIGHT_RATIO, compute_cursor_text_color,
};
use super::{Cell, CellRenderer, TextInstance};
use par_term_config::color_u8x4_rgb_to_f32_a;

impl CellRenderer {
    /// Build text (and underline) instances for a single row, populating `self.scratch_row_text`.
    ///
    /// Handles glyph lookup, atlas rasterization, block-character geometric rendering,
    /// and underline decoration. The scratch buffer is assumed to be cleared on entry.
    ///
    /// `start` is the flat index into `self.cells` for the beginning of `row`.
    pub(crate) fn build_row_text_instances(
        &mut self,
        row: usize,
        row_cells: &[Cell],
        start: usize,
    ) {
        let mut x_offset = 0.0;

        // Dynamic baseline calculation based on font metrics
        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_unrounded = self.grid.window_padding
            + self.grid.content_offset_y
            + (row as f32 * self.grid.cell_height)
            + vertical_padding
            + self.font.font_ascent;

        // Check if this row has the cursor and it's a visible block cursor
        // (for cursor text color override)
        let cursor_is_block_on_this_row = {
            use par_term_emu_core_rust::cursor::CursorStyle;
            self.cursor.pos.1 == row
                && self.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)
        };

        let mut current_col = 0usize;
        for cell in row_cells {
            let grapheme = &cell.grapheme;
            let bold = cell.bold;
            let italic = cell.italic;
            let fg_color = cell.fg_color;
            let bg_color = cell.bg_color;
            let is_spacer = cell.wide_char_spacer;
            let is_wide = cell.wide_char;
            if is_spacer || grapheme == " " {
                x_offset += self.grid.cell_width;
                current_col += 1;
                continue;
            }

            // Compute text alpha - force opaque if keep_text_opaque is enabled,
            // otherwise use window opacity so text becomes transparent with the window
            let text_alpha = if self.keep_text_opaque {
                1.0
            } else {
                self.window_opacity
            };

            // Determine text color - use cursor_text_color if this is the cursor position
            // with a block cursor, otherwise use the cell's foreground color
            let render_fg_color: [f32; 4] =
                if cursor_is_block_on_this_row && current_col == self.cursor.pos.0 {
                    compute_cursor_text_color(self.cursor.color, self.cursor.text_color, text_alpha)
                } else {
                    // Determine the effective background color for contrast calculation
                    // If the cell has a non-default bg, use that; otherwise use terminal background
                    let effective_bg = if bg_color[3] > 0 {
                        // Cell has explicit background
                        color_u8x4_rgb_to_f32_a(bg_color, 1.0)
                    } else {
                        // Use terminal default background
                        [
                            self.background_color[0],
                            self.background_color[1],
                            self.background_color[2],
                            1.0,
                        ]
                    };

                    let base_fg = color_u8x4_rgb_to_f32_a(fg_color, text_alpha);

                    // Apply minimum contrast adjustment if enabled
                    self.ensure_minimum_contrast(base_fg, effective_bg)
                };

            // Avoid Vec<char> allocation: determine first/second char from iterator directly.
            // grapheme_len is 1, 2, or "more than 2" (we only care which case we're in).
            let first_char = grapheme.chars().next();
            let second_char = grapheme.chars().nth(1);
            // Count chars lazily: stop after 2 to avoid scanning long sequences.
            let grapheme_len = match second_char {
                None => 1usize,
                Some(_) => {
                    if grapheme.chars().nth(2).is_none() {
                        2
                    } else {
                        3
                    }
                }
            };
            #[allow(clippy::collapsible_if)]
            if let Some(ch) = first_char {
                // Classify the character for rendering optimization
                // Only classify based on first char for block drawing detection
                let char_type = block_chars::classify_char(ch);

                // Check if we should render this character geometrically
                // (only for single-char graphemes that are block drawing chars)
                if grapheme_len == 1 && block_chars::should_render_geometrically(char_type) {
                    let char_w = if is_wide {
                        self.grid.cell_width * 2.0
                    } else {
                        self.grid.cell_width
                    };
                    let x0 =
                        (self.grid.window_padding + self.grid.content_offset_x + x_offset).round();
                    let y0 = (self.grid.window_padding
                        + self.grid.content_offset_y
                        + row as f32 * self.grid.cell_height)
                        .round();
                    // Compute pixel-snapped cell height to match bg pipeline alignment
                    let y1 = (self.grid.window_padding
                        + self.grid.content_offset_y
                        + (row + 1) as f32 * self.grid.cell_height)
                        .round();
                    let snapped_cell_height = y1 - y0;

                    // Try box drawing geometry first (for lines, corners, junctions)
                    // Pass aspect ratio so vertical lines have same visual thickness as horizontal
                    let aspect_ratio = snapped_cell_height / char_w;
                    if let Some(box_geo) = block_chars::get_box_drawing_geometry(ch, aspect_ratio) {
                        for segment in &box_geo.segments {
                            let rect = segment
                                .to_pixel_rect(x0, y0, char_w, snapped_cell_height)
                                .snap_to_pixels();

                            // Extend segments that touch cell edges
                            let extension = 1.0;
                            let ext_x = if segment.x <= 0.01 { extension } else { 0.0 };
                            let ext_y = if segment.y <= 0.01 { extension } else { 0.0 };
                            let ext_w = if segment.x + segment.width >= 0.99 {
                                extension
                            } else {
                                0.0
                            };
                            let ext_h = if segment.y + segment.height >= 0.99 {
                                extension
                            } else {
                                0.0
                            };

                            let final_x = rect.x - ext_x;
                            let final_y = rect.y - ext_y;
                            let final_w = rect.width + ext_x + ext_w;
                            let final_h = rect.height + ext_y + ext_h;

                            self.scratch_row_text.push(TextInstance {
                                position: [
                                    final_x / self.config.width as f32 * 2.0 - 1.0,
                                    1.0 - (final_y / 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: [
                                    self.atlas.solid_pixel_offset.0 as f32
                                        / self.atlas.atlas_size as f32,
                                    self.atlas.solid_pixel_offset.1 as f32
                                        / self.atlas.atlas_size as f32,
                                ],
                                tex_size: [
                                    1.0 / self.atlas.atlas_size as f32,
                                    1.0 / self.atlas.atlas_size as f32,
                                ],
                                color: render_fg_color,
                                is_colored: 0,
                            });
                        }
                        x_offset += self.grid.cell_width;
                        current_col += 1;
                        continue;
                    }

                    // Half-block characters (â–„/â–€): render BOTH halves through the
                    // text pipeline to eliminate cross-pipeline coordinate seams.
                    // The BG pipeline skips these cells entirely.
                    // Use snapped cell edges (no extensions) so adjacent cells tile
                    // perfectly without overlap artifacts.
                    if ch == '\u{2584}' || ch == '\u{2580}' {
                        // Compute snapped right edge to match next cell's x0
                        let x1 = (self.grid.window_padding
                            + self.grid.content_offset_x
                            + x_offset
                            + char_w)
                            .round();
                        let cell_w = x1 - x0;
                        // y0 and y1 (snapped_cell_height) already computed above
                        let y_mid = y0 + self.grid.cell_height / 2.0;

                        let bg_half_color = color_u8x4_rgb_to_f32_a(bg_color, text_alpha);
                        let (top_color, bottom_color) = if ch == '\u{2584}' {
                            (bg_half_color, render_fg_color) // â–„: top=bg, bottom=fg
                        } else {
                            (render_fg_color, bg_half_color) // â–€: top=fg, bottom=bg
                        };

                        let tex_offset = [
                            self.atlas.solid_pixel_offset.0 as f32 / self.atlas.atlas_size as f32,
                            self.atlas.solid_pixel_offset.1 as f32 / self.atlas.atlas_size as f32,
                        ];
                        let tex_size = [
                            1.0 / self.atlas.atlas_size as f32,
                            1.0 / self.atlas.atlas_size as f32,
                        ];

                        // Top half: [y0, y_mid)
                        self.scratch_row_text.push(TextInstance {
                            position: [
                                x0 / self.config.width as f32 * 2.0 - 1.0,
                                1.0 - (y0 / self.config.height as f32 * 2.0),
                            ],
                            size: [
                                cell_w / self.config.width as f32 * 2.0,
                                (y_mid - y0) / self.config.height as f32 * 2.0,
                            ],
                            tex_offset,
                            tex_size,
                            color: top_color,
                            is_colored: 0,
                        });

                        // Bottom half: [y_mid, y1)
                        self.scratch_row_text.push(TextInstance {
                            position: [
                                x0 / self.config.width as f32 * 2.0 - 1.0,
                                1.0 - (y_mid / self.config.height as f32 * 2.0),
                            ],
                            size: [
                                cell_w / self.config.width as f32 * 2.0,
                                (y1 - y_mid) / self.config.height as f32 * 2.0,
                            ],
                            tex_offset,
                            tex_size,
                            color: bottom_color,
                            is_colored: 0,
                        });

                        x_offset += self.grid.cell_width;
                        current_col += 1;
                        continue;
                    }

                    // Try block element geometry (for solid blocks, partial blocks, etc.)
                    if let Some(geo_block) = block_chars::get_geometric_block(ch) {
                        let rect = geo_block.to_pixel_rect(x0, y0, char_w, self.grid.cell_height);

                        // Add small extension to prevent gaps (1 pixel overlap).
                        let extension = 1.0;
                        let ext_x = if geo_block.x == 0.0 { extension } else { 0.0 };
                        let ext_y = if geo_block.y == 0.0 { extension } else { 0.0 };
                        let ext_w = if geo_block.x + geo_block.width >= 1.0 {
                            extension
                        } else {
                            0.0
                        };
                        let ext_h = if geo_block.y + geo_block.height >= 1.0 {
                            extension
                        } else {
                            0.0
                        };

                        let final_x = rect.x - ext_x;
                        let final_y = rect.y - ext_y;
                        let final_w = rect.width + ext_x + ext_w;
                        let final_h = rect.height + ext_y + ext_h;

                        // Render as a colored rectangle using the solid white pixel in atlas
                        self.scratch_row_text.push(TextInstance {
                            position: [
                                final_x / self.config.width as f32 * 2.0 - 1.0,
                                1.0 - (final_y / 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: [
                                self.atlas.solid_pixel_offset.0 as f32
                                    / self.atlas.atlas_size as f32,
                                self.atlas.solid_pixel_offset.1 as f32
                                    / self.atlas.atlas_size as f32,
                            ],
                            tex_size: [
                                1.0 / self.atlas.atlas_size as f32,
                                1.0 / self.atlas.atlas_size as f32,
                            ],
                            color: render_fg_color,
                            is_colored: 0,
                        });

                        x_offset += self.grid.cell_width;
                        current_col += 1;
                        continue;
                    }

                    // Try geometric shape (aspect-ratio-aware squares, rectangles)
                    if let Some(rect) = block_chars::get_geometric_shape_rect(
                        ch,
                        x0,
                        y0,
                        char_w,
                        snapped_cell_height,
                    ) {
                        self.scratch_row_text.push(TextInstance {
                            position: [
                                rect.x / self.config.width as f32 * 2.0 - 1.0,
                                1.0 - (rect.y / self.config.height as f32 * 2.0),
                            ],
                            size: [
                                rect.width / self.config.width as f32 * 2.0,
                                rect.height / self.config.height as f32 * 2.0,
                            ],
                            tex_offset: [
                                self.atlas.solid_pixel_offset.0 as f32
                                    / self.atlas.atlas_size as f32,
                                self.atlas.solid_pixel_offset.1 as f32
                                    / self.atlas.atlas_size as f32,
                            ],
                            tex_size: [
                                1.0 / self.atlas.atlas_size as f32,
                                1.0 / self.atlas.atlas_size as f32,
                            ],
                            color: render_fg_color,
                            is_colored: 0,
                        });

                        x_offset += self.grid.cell_width;
                        current_col += 1;
                        continue;
                    }
                }

                // Check if this character should be rendered as a monochrome symbol
                // (dingbats, etc.) rather than colorful emoji.
                // Also handle symbol + VS16 (U+FE0F emoji presentation selector):
                // in terminal contexts, symbols should remain monochrome even with VS16.
                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).
                // This replaces the duplicated excluded_fonts/get_or_rasterize_glyph loop
                // that previously existed in both this file and pane_render/mod.rs.
                let resolved_info = self.resolve_glyph_with_fallback(
                    base_char,
                    grapheme,
                    bold,
                    italic,
                    force_monochrome,
                );

                let info = match resolved_info {
                    Some(info) => info,
                    None => {
                        x_offset += self.grid.cell_width;
                        current_col += 1;
                        continue;
                    }
                };

                let char_w = if is_wide {
                    self.grid.cell_width * 2.0
                } else {
                    self.grid.cell_width
                };
                let x0 = (self.grid.window_padding + self.grid.content_offset_x + x_offset).round();
                let x1 =
                    (self.grid.window_padding + self.grid.content_offset_x + x_offset + char_w)
                        .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 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;

                // Position glyph relative to snapped cell top-left.
                // Round the scaled baseline position once, then subtract
                // the integer bearing_y. This ensures all glyphs on a row
                // share the same rounded baseline, with bearing offsets
                // applied exactly (no scale_y on bearing avoids rounding
                // artifacts between glyphs with different bearings).
                let baseline_offset = baseline_y_unrounded
                    - (self.grid.window_padding
                        + self.grid.content_offset_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
                    && char_type == block_chars::BlockCharType::Symbol
                {
                    // Symbol characters (ballot boxes, dingbats, etc.) use baseline-relative
                    // font metrics that make them appear vertically squished. Center in cell
                    // and scale to fill cell height while maintaining aspect ratio.
                    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: GLYPH_SNAP_THRESHOLD_PX,
                        extension: GLYPH_SNAP_EXTENSION_PX,
                    })
                } else {
                    (glyph_left, glyph_top, render_w, render_h)
                };

                self.scratch_row_text.push(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 / self.atlas.atlas_size as f32,
                        info.y as f32 / self.atlas.atlas_size as f32,
                    ],
                    tex_size: [
                        info.width as f32 / self.atlas.atlas_size as f32,
                        info.height as f32 / self.atlas.atlas_size as f32,
                    ],
                    color: render_fg_color,
                    is_colored: if info.is_colored { 1 } else { 0 },
                });
            }
            x_offset += self.grid.cell_width;
            current_col += 1;
        }

        // Underlines: emit thin rectangle(s) at the bottom of each underlined cell
        {
            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 / self.atlas.atlas_size as f32,
                self.atlas.solid_pixel_offset.1 as f32 / self.atlas.atlas_size as f32,
            ];
            let tex_size = [
                1.0 / self.atlas.atlas_size as f32,
                1.0 / self.atlas.atlas_size as f32,
            ];
            let y0 = self.grid.window_padding
                + self.grid.content_offset_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;
            // Stipple: STIPPLE_ON_PX on, STIPPLE_OFF_PX off pattern
            let stipple_on = STIPPLE_ON_PX;
            let stipple_off = STIPPLE_OFF_PX;
            let stipple_period = stipple_on + stipple_off;

            for col_idx in 0..self.grid.cols {
                let cell = &self.cells[start + col_idx];
                if !cell.underline || self.scratch_row_text.len() >= self.grid.cols * 2 {
                    continue;
                }
                let text_alpha = if self.keep_text_opaque {
                    1.0
                } else {
                    self.window_opacity
                };
                let fg = color_u8x4_rgb_to_f32_a(cell.fg_color, text_alpha);
                let cell_x0 = self.grid.window_padding
                    + self.grid.content_offset_x
                    + col_idx as f32 * self.grid.cell_width;

                if is_stipple {
                    // Emit alternating dot segments across the cell width
                    let mut px = 0.0;
                    while px < self.grid.cell_width
                        && self.scratch_row_text.len() < self.grid.cols * 2
                    {
                        let seg_w = stipple_on.min(self.grid.cell_width - px);
                        let x = cell_x0 + px;
                        self.scratch_row_text.push(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,
                        });
                        px += stipple_period;
                    }
                } else {
                    self.scratch_row_text.push(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,
                    });
                }
            }
        }
    }
}