Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add sextant glyph support to terminal rendering (#51072)
Matthew Chisolmc2a610f7eb0a
diff --git a/crates/repl/src/outputs/plain.rs b/crates/repl/src/outputs/plain.rsindex c127d625e22..f5fa70f3e2c 100644--- a/crates/repl/src/outputs/plain.rs+++ b/crates/repl/src/outputs/plain.rs@@ -362,15 +362,17 @@ impl Render for TerminalOutput {let text_style = text_style(window, cx);let minimum_contrast = TerminalSettings::get_global(cx).minimum_contrast;- let (rects, batched_text_runs) = terminal.read(cx).with_renderable_cells(|cells| {- TerminalElement::layout_grid(cells, 0, &text_style, None, minimum_contrast, cx)- });+ let (rects, batched_text_runs, block_element_rects) =+ terminal.read(cx).with_renderable_cells(|cells| {+ TerminalElement::layout_grid(cells, 0, &text_style, None, minimum_contrast, cx)+ });// lines are 0-indexed, so we must add 1 to get the number of lineslet text_line_height = text_style.line_height_in_pixels(window.rem_size());let num_lines = batched_text_runs.iter().map(|b| b.start_point.line())+ .chain(block_element_rects.iter().map(|rect| rect.line())).max().unwrap_or(0)+ 1;@@ -414,6 +416,18 @@ impl Render for TerminalOutput {cx,);}++ for block_element_rect in block_element_rects {+ block_element_rect.paint(+ bounds.origin,+ &terminal::TerminalBounds {+ cell_width,+ line_height: text_line_height,+ bounds,+ },+ window,+ );+ }},)// We must set the height explicitly for the editor block to size itself correctlydiff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rsindex 5d94ff37aeb..f580e769dd1 100644--- a/crates/terminal_view/src/terminal_element.rs+++ b/crates/terminal_view/src/terminal_element.rs@@ -33,6 +33,7 @@ use crate::{BlockContext, BlockProperties, ContentMode, TerminalMode, TerminalVipub struct LayoutState {hitbox: Hitbox,batched_text_runs: Vec<BatchedTextRun>,+ block_element_rects: Vec<BlockElementLayoutRect>,rects: Vec<LayoutRect>,relative_highlighted_ranges: Vec<(Range, Hsla)>,cursor: Option<CursorLayout>,@@ -177,6 +178,55 @@ impl BatchedTextRun {}}+/// Block element glyphs are painted on a subcell grid: each terminal cell is+/// divided into 8 columns (for eighth blocks) and 24 lines (LCM of the 8-way+/// splits of eighth blocks and the 3-way splits of sextants).+const BLOCK_SUBCELL_COLUMNS: i32 = 8;+const BLOCK_SUBCELL_LINES: i32 = 24;++#[derive(Clone, Debug)]+pub struct BlockElementLayoutRect {+ point: LayoutPoint,+ num_of_columns: usize,+ num_of_lines: usize,+ color: Hsla,+}++impl BlockElementLayoutRect {+ fn new(point: LayoutPoint, num_of_columns: usize, num_of_lines: usize, color: Hsla) -> Self {+ Self {+ point,+ num_of_columns,+ num_of_lines,+ color,+ }+ }++ pub fn paint(+ &self,+ origin: GpuiPoint<Pixels>,+ dimensions: &TerminalBounds,+ window: &mut Window,+ ) {+ let subcell_width = dimensions.cell_width / BLOCK_SUBCELL_COLUMNS as f32;+ let subcell_height = dimensions.line_height / BLOCK_SUBCELL_LINES as f32;+ let position = point(+ origin.x + self.point.column as f32 * subcell_width,+ origin.y + self.point.line as f32 * subcell_height,+ );+ let size = size(+ subcell_width * self.num_of_columns as f32,+ subcell_height * self.num_of_lines as f32,+ );++ window.paint_quad(fill(Bounds::new(position, size), self.color));+ }++ pub fn line(&self) -> i32 {+ (self.point.line + self.num_of_lines as i32 - 1) / BLOCK_SUBCELL_LINES+ }+}+#[derive(Clone, Debug, Default)]pub struct LayoutRect {point: LayoutPoint,@@ -216,7 +266,7 @@ impl LayoutRect {}}-/// Represents a rectangular region with a specific background color+/// Represents a rectangular region with a specific color on a logical grid.#[derive(Debug, Clone)]struct BackgroundRegion {start_line: i32,@@ -237,6 +287,22 @@ impl BackgroundRegion {}}+ fn with_extents(+ start_line: i32,+ start_col: i32,+ end_line: i32,+ end_col: i32,+ color: Hsla,+ ) -> Self {+ BackgroundRegion {+ start_line,+ start_col,+ end_line,+ end_col,+ color,+ }+ }+/// Check if this region can be merged with another regionfn can_merge_with(&self, other: &BackgroundRegion) -> bool {if self.color != other.color {@@ -290,7 +356,7 @@ impl TerminalLayoutCell for &IndexedCell {}}-/// Merge background regions to minimize the number of rectangles+/// Merge grid regions to minimize the number of rectangles.fn merge_background_regions(regions: Vec<BackgroundRegion>) -> Vec<BackgroundRegion> {if regions.is_empty() {return regions;@@ -376,7 +442,11 @@ impl TerminalElement {hyperlink: Option<(HighlightStyle, &Range)>,minimum_contrast: f32,cx: &App,- ) -> (Vec<LayoutRect>, Vec<BatchedTextRun>) {+ ) -> (+ Vec<LayoutRect>,+ Vec<BatchedTextRun>,+ Vec<BlockElementLayoutRect>,+ ) {let start_time = Instant::now();let theme = cx.theme();@@ -386,6 +456,7 @@ impl TerminalElement {let estimated_regions = estimated_cells / 20; // Estimate ~20 cells per background regionlet mut batched_runs = Vec::with_capacity(estimated_runs);+ let mut block_element_regions = Vec::new();let mut cell_count = 0;// Collect background regions for efficient merging@@ -460,6 +531,18 @@ impl TerminalElement {);let cell_point = LayoutPoint::new(display_line, point.column as i32);+ if Self::collect_block_element_regions(+ cell_point,+ cell.character(),+ cell_style.color,+ &mut block_element_regions,+ ) {+ if let Some(batch) = current_batch.take() {+ batched_runs.push(batch);+ }+ continue;+ }+let zero_width_chars = cell.zerowidth();// Try to batch with existing run@@ -528,20 +611,24 @@ impl TerminalElement {}}+ let block_element_region_count = block_element_regions.len();+ let block_element_rects = Self::block_element_regions_to_rects(block_element_regions);let layout_time = start_time.elapsed();log::debug!("Terminal layout_grid: {} cells processed, \- {} batched runs created, {} rects (from {} merged regions), \+ {} batched runs created, {} block element rects (from {} regions), {} rects (from {} merged regions), \layout took {:?}",cell_count,batched_runs.len(),+ block_element_rects.len(),+ block_element_region_count,rects.len(),region_count,layout_time);- (rects, batched_runs)+ (rects, batched_runs, block_element_rects)}/// Computes the cursor position based on the cursor point and terminal dimensions.@@ -575,6 +662,7 @@ impl TerminalElement {0x2500..=0x257F // Box Drawing (└ ┐ ─ │ etc.)| 0x2580..=0x259F // Block Elements (▀ ▄ █ ░ ▒ ▓ etc.)| 0x25A0..=0x25FF // Geometric Shapes (■ ▶ ● etc. - includes triangular/circular separators)+ | 0x1FB00..=0x1FB3B // Symbols for Legacy Computing sextants used by terminal QR renderers// Private Use Area - Powerline separator symbols only| 0xE0B0..=0xE0B7 // Powerline separators: triangles (E0B0-E0B3) and half circles (E0B4-E0B7)@@ -595,7 +683,188 @@ impl TerminalElement {terminal_is_app_chosen_exact_color(*fg)}- /// Converts terminal cell styles to GPUI text styles and background color.+ /// Returns the filled subcells of a sextant character as a bitmap, where+ /// bit `row * 2 + column` is set when that 2x3 subcell is filled.+ ///+ /// U+1FB00..=U+1FB3B enumerate all 2x3 fill combinations except the four+ /// that already exist as Block Elements (empty, `▌` = 0b010101,+ /// `▐` = 0b101010, and `█` = 0b111111), hence the gap adjustments.+ fn sextant_char_to_filled_bits(ch: char) -> Option<u8> {+ let offset = (ch as u32).checked_sub(0x1FB00)?;+ if offset > 0x3B {+ return None;+ }++ Some((offset + 1 + u32::from(offset >= 20) + u32::from(offset >= 40)) as u8)+ }++ /// Returns the filled quadrants of a quadrant character as a bitmap, where+ /// bit `row * 2 + column` is set when that 2x2 subcell is filled.+ fn quadrant_char_to_filled_bits(ch: char) -> Option<u8> {+ Some(match ch {+ '▘' => 0b0001,+ '▝' => 0b0010,+ '▖' => 0b0100,+ '▗' => 0b1000,+ '▚' => 0b1001,+ '▞' => 0b0110,+ '▛' => 0b0111,+ '▜' => 0b1011,+ '▙' => 0b1101,+ '▟' => 0b1110,+ _ => return None,+ })+ }++ /// Returns `(column, line, num_of_columns, num_of_lines)` in subcell units+ /// for block element characters that consist of a single rectangle.+ fn block_char_to_rect(ch: char) -> Option<(i32, i32, i32, i32)> {+ let codepoint = ch as u32;+ Some(match codepoint {+ // ▀ upper half+ 0x2580 => (0, 0, 8, 12),+ // ▁▂▃▄▅▆▇█ lower blocks of 1..=8 eighths+ 0x2581..=0x2588 => {+ let eighths = (codepoint - 0x2580) as i32;+ (0, 24 - eighths * 3, 8, eighths * 3)+ }+ // ▉▊▋▌▍▎▏ left blocks of 7..=1 eighths+ 0x2589..=0x258F => (0, 0, (0x2590 - codepoint) as i32, 24),+ // ▐ right half+ 0x2590 => (4, 0, 4, 24),+ // ▔ upper eighth+ 0x2594 => (0, 0, 8, 3),+ // ▕ right eighth+ 0x2595 => (7, 0, 1, 24),+ _ => return None,+ })+ }++ /// Approximates the shade characters `░▒▓` with the foreground color at+ /// reduced opacity instead of the stipple patterns fonts use, trading+ /// pattern fidelity for seamless cell coverage.+ fn shade_char_to_opacity(ch: char) -> Option<f32> {+ match ch {+ '░' => Some(0.25),+ '▒' => Some(0.5),+ '▓' => Some(0.75),+ _ => None,+ }+ }++ fn collect_block_element_regions(+ point: LayoutPoint,+ ch: char,+ color: Hsla,+ regions: &mut Vec<BackgroundRegion>,+ ) -> bool {+ if let Some((column, line, num_of_columns, num_of_lines)) = Self::block_char_to_rect(ch) {+ Self::push_block_element_region(+ point,+ column,+ line,+ num_of_columns,+ num_of_lines,+ color,+ regions,+ );+ return true;+ }++ if let Some(filled) = Self::quadrant_char_to_filled_bits(ch) {+ for row in 0..2 {+ for column in 0..2 {+ if filled & (1 << (row * 2 + column)) != 0 {+ Self::push_block_element_region(+ point,+ column * 4,+ row * 12,+ 4,+ 12,+ color,+ regions,+ );+ }+ }+ }+ return true;+ }++ if let Some(filled) = Self::sextant_char_to_filled_bits(ch) {+ for row in 0..3 {+ for column in 0..2 {+ if filled & (1 << (row * 2 + column)) != 0 {+ Self::push_block_element_region(+ point,+ column * 4,+ row * 8,+ 4,+ 8,+ color,+ regions,+ );+ }+ }+ }+ return true;+ }++ if let Some(opacity) = Self::shade_char_to_opacity(ch) {+ Self::push_block_element_region(point, 0, 0, 8, 24, color.opacity(opacity), regions);+ return true;+ }++ false+ }++ fn push_block_element_region(+ point: LayoutPoint,+ column: i32,+ line: i32,+ num_of_columns: i32,+ num_of_lines: i32,+ color: Hsla,+ regions: &mut Vec<BackgroundRegion>,+ ) {+ let start_line = point.line * BLOCK_SUBCELL_LINES + line;+ let start_col = point.column * BLOCK_SUBCELL_COLUMNS + column;+ let end_line = start_line + num_of_lines - 1;+ let end_col = start_col + num_of_columns - 1;++ // Extend the previous region when possible (e.g. runs of `█` in a QR+ // code) to keep the quadratic merge pass over a small input.+ if let Some(last_region) = regions.last_mut()+ && last_region.color == color+ && last_region.start_line == start_line+ && last_region.end_line == end_line+ && last_region.end_col + 1 == start_col+ {+ last_region.end_col = end_col;+ return;+ }++ regions.push(BackgroundRegion::with_extents(+ start_line, start_col, end_line, end_col, color,+ ));+ }++ fn block_element_regions_to_rects(+ regions: Vec<BackgroundRegion>,+ ) -> Vec<BlockElementLayoutRect> {+ merge_background_regions(regions)+ .into_iter()+ .map(|region| {+ BlockElementLayoutRect::new(+ LayoutPoint::new(region.start_line, region.start_col),+ (region.end_col - region.start_col + 1) as usize,+ (region.end_line - region.start_line + 1) as usize,+ region.color,+ )+ })+ .collect()+ }++ /// Converts the Alacritty cell styles to GPUI text styles and background color.fn cell_style(point: Point,cell: &Cell,@@ -1163,10 +1432,11 @@ impl Element for TerminalElement {// This handles the case where the terminal has been scrolled past (above or// below the viewport), similar to the editor fix in PR #45077 where start_row// could exceed max_row when the editor was positioned above the viewport.- let (rects, batched_text_runs) = if intersection.size.height <= px(0.)+ let (rects, batched_text_runs, block_element_rects) = if intersection.size.height+ <= px(0.)|| intersection.size.width <= px(0.){- (Vec::new(), Vec::new())+ (Vec::new(), Vec::new(), Vec::new())} else if intersection == content_bounds {// Fast path: terminal fully visible, no clipping needed.// Avoid grouping/allocation overhead by streaming cells directly.@@ -1309,6 +1579,7 @@ impl Element for TerminalElement {LayoutState {hitbox,batched_text_runs,+ block_element_rects,cursor,ime_cursor_bounds,background_color,@@ -1430,6 +1701,9 @@ impl Element for TerminalElement {for batch in &layout.batched_text_runs {batch.paint(origin, &layout.dimensions, window, cx);}+ for block_element_rect in &layout.block_element_rects {+ block_element_rect.paint(origin, &layout.dimensions, window);+ }let text_paint_time = text_paint_start.elapsed();if let Some(text_to_mark) = &marked_text_cloned@@ -1825,6 +2099,232 @@ mod tests {assert!(TerminalElement::is_decorative_character('\u{25A0}')); // First charassert!(TerminalElement::is_decorative_character('\u{25FF}')); // Last charassert!(!TerminalElement::is_decorative_character('\u{2600}')); // Just after++ // Sextant range boundaries+ assert!(TerminalElement::is_decorative_character('\u{1FB00}')); // First char+ assert!(TerminalElement::is_decorative_character('\u{1FB3B}')); // Last char+ assert!(!TerminalElement::is_decorative_character('\u{1FAFF}')); // Just before+ assert!(!TerminalElement::is_decorative_character('\u{1FB3C}')); // Just after+ }++ #[test]+ fn test_sextant_char_to_filled_bits() {+ // U+1FB00 BLOCK SEXTANT-1: only the top-left subcell.+ assert_eq!(+ TerminalElement::sextant_char_to_filled_bits('\u{1FB00}'),+ Some(0b00_0001)+ );+ // U+1FB13 BLOCK SEXTANT-35 and U+1FB14 BLOCK SEXTANT-235 straddle the+ // gap left by `▌` (0b01_0101).+ assert_eq!(+ TerminalElement::sextant_char_to_filled_bits('\u{1FB13}'),+ Some(0b01_0100)+ );+ assert_eq!(+ TerminalElement::sextant_char_to_filled_bits('\u{1FB14}'),+ Some(0b01_0110)+ );+ // U+1FB3B BLOCK SEXTANT-12356: everything except the bottom-left subcell.+ assert_eq!(+ TerminalElement::sextant_char_to_filled_bits('\u{1FB3B}'),+ Some(0b11_1110)+ );+ assert_eq!(TerminalElement::sextant_char_to_filled_bits('█'), None);+ assert_eq!(+ TerminalElement::sextant_char_to_filled_bits('\u{1FB3C}'),+ None+ );+ }++ #[test]+ fn test_block_element_rects_merge_across_adjacent_full_blocks() {+ let color = Hsla::default();+ let mut regions = Vec::new();+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 0),+ '█',+ color,+ &mut regions,+ ));+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 1),+ '█',+ color,+ &mut regions,+ ));++ assert_eq!(+ regions.len(),+ 1,+ "adjacent full blocks should be merged eagerly by push_block_element_region"+ );++ let rects = TerminalElement::block_element_regions_to_rects(regions);++ assert_eq!(rects.len(), 1);+ assert_eq!(rects[0].point.line(), 0);+ assert_eq!(rects[0].point.column(), 0);+ assert_eq!(rects[0].num_of_columns, 16);+ assert_eq!(rects[0].num_of_lines, 24);+ assert_eq!(rects[0].line(), 0);+ }++ #[test]+ fn test_block_element_rects_cover_half_blocks_and_sextants() {+ let color = Hsla::default();+ let mut regions = Vec::new();+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 0),+ '▄',+ color,+ &mut regions,+ ));+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(1, 0),+ '\u{1FB00}',+ color,+ &mut regions,+ ));++ let rects = TerminalElement::block_element_regions_to_rects(regions);++ assert!(rects.iter().any(|rect| {+ rect.point.line() == 12+ && rect.point.column() == 0+ && rect.num_of_columns == 8+ && rect.num_of_lines == 12+ }));+ assert!(rects.iter().any(|rect| {+ rect.point.line() == 24+ && rect.point.column() == 0+ && rect.num_of_columns == 4+ && rect.num_of_lines == 8+ }));+ }++ #[test]+ fn test_block_element_rects_cover_eighth_blocks() {+ let color = Hsla::default();++ for (ch, expected_column, expected_line, expected_columns, expected_lines) in [+ ('▁', 0, 21, 8, 3),+ ('▇', 0, 3, 8, 21),+ ('▉', 0, 0, 7, 24),+ ('▏', 0, 0, 1, 24),+ ('▔', 0, 0, 8, 3),+ ('▕', 7, 0, 1, 24),+ ] {+ let mut regions = Vec::new();+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 0),+ ch,+ color,+ &mut regions,+ ));+ let rects = TerminalElement::block_element_regions_to_rects(regions);++ assert_eq!(rects.len(), 1, "unexpected rect count for {ch}");+ assert_eq!(rects[0].point.column(), expected_column, "column for {ch}");+ assert_eq!(rects[0].point.line(), expected_line, "line for {ch}");+ assert_eq!(+ rects[0].num_of_columns, expected_columns,+ "columns for {ch}"+ );+ assert_eq!(rects[0].num_of_lines, expected_lines, "lines for {ch}");+ }+ }++ #[test]+ fn test_block_element_rects_cover_quadrants() {+ let color = Hsla::default();+ let mut regions = Vec::new();+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 0),+ '▚',+ color,+ &mut regions,+ ));++ let rects = TerminalElement::block_element_regions_to_rects(regions);++ assert_eq!(rects.len(), 2);+ assert!(rects.iter().any(|rect| {+ rect.point.line() == 0+ && rect.point.column() == 0+ && rect.num_of_columns == 4+ && rect.num_of_lines == 12+ }));+ assert!(rects.iter().any(|rect| {+ rect.point.line() == 12+ && rect.point.column() == 4+ && rect.num_of_columns == 4+ && rect.num_of_lines == 12+ }));+ }++ #[test]+ fn test_block_element_rects_cover_shades() {+ let color = gpui::red();+ let mut regions = Vec::new();+ assert!(TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 0),+ '▒',+ color,+ &mut regions,+ ));++ let rects = TerminalElement::block_element_regions_to_rects(regions);++ assert_eq!(rects.len(), 1);+ assert_eq!(rects[0].num_of_columns, 8);+ assert_eq!(rects[0].num_of_lines, 24);+ assert_eq!(rects[0].color, color.opacity(0.5));+ }++ #[test]+ fn test_block_element_chars_fully_handled_within_cell() {+ let color = Hsla::default();++ for codepoint in (0x2580..=0x259F).chain(0x1FB00..=0x1FB3B) {+ let ch = char::from_u32(codepoint).expect("valid block element codepoint");+ let mut regions = Vec::new();+ assert!(+ TerminalElement::collect_block_element_regions(+ LayoutPoint::new(0, 0),+ ch,+ color,+ &mut regions,+ ),+ "U+{codepoint:04X} {ch} should be custom-painted"+ );+ assert!(+ !regions.is_empty(),+ "U+{codepoint:04X} {ch} should fill at least one subcell"+ );++ let mut filled =+ [[false; BLOCK_SUBCELL_COLUMNS as usize]; BLOCK_SUBCELL_LINES as usize];+ for region in ®ions {+ assert!(+ region.start_line <= region.end_line+ && region.start_col <= region.end_col+ && (0..BLOCK_SUBCELL_LINES).contains(®ion.start_line)+ && (0..BLOCK_SUBCELL_LINES).contains(®ion.end_line)+ && (0..BLOCK_SUBCELL_COLUMNS).contains(®ion.start_col)+ && (0..BLOCK_SUBCELL_COLUMNS).contains(®ion.end_col),+ "U+{codepoint:04X} {ch} paints outside its cell: {region:?}"+ );+ for line in region.start_line..=region.end_line {+ for column in region.start_col..=region.end_col {+ assert!(+ !filled[line as usize][column as usize],+ "U+{codepoint:04X} {ch} paints subcell ({line}, {column}) twice"+ );+ filled[line as usize][column as usize] = true;+ }+ }+ }+ }}#[test]