Remove unnecessary result in line shaping (#30721)

Updates #29879

Release Notes:

- N/A
This commit is contained in:
Conrad Irwin 2025-05-16 23:48:36 +02:00 committed by GitHub
parent d791c6cdb1
commit ff0060aa36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 118 additions and 155 deletions

View file

@ -1026,9 +1026,7 @@ impl DisplaySnapshot {
} }
let font_size = editor_style.text.font_size.to_pixels(*rem_size); let font_size = editor_style.text.font_size.to_pixels(*rem_size);
text_system text_system.layout_line(&line, font_size, &runs)
.layout_line(&line, font_size, &runs)
.expect("we expect the font to be loaded because it's rendered by the editor")
} }
pub fn x_for_display_point( pub fn x_for_display_point(

View file

@ -1343,7 +1343,7 @@ impl EditorElement {
None None
} }
}) })
.and_then(|text| { .map(|text| {
let len = text.len(); let len = text.len();
let font = cursor_row_layout let font = cursor_row_layout
@ -1369,21 +1369,18 @@ impl EditorElement {
cx.theme().colors().editor_background cx.theme().colors().editor_background
}; };
window window.text_system().shape_line(
.text_system() text,
.shape_line( cursor_row_layout.font_size,
text, &[TextRun {
cursor_row_layout.font_size, len,
&[TextRun { font,
len, color,
font, background_color: None,
color, strikethrough: None,
background_color: None, underline: None,
strikethrough: None, }],
underline: None, )
}],
)
.log_err()
}) })
} else { } else {
None None
@ -2690,9 +2687,8 @@ impl EditorElement {
} }
}) })
.unwrap_or_else(|| cx.theme().colors().editor_line_number); .unwrap_or_else(|| cx.theme().colors().editor_line_number);
let shaped_line = self let shaped_line =
.shape_line_number(SharedString::from(&line_number), color, window) self.shape_line_number(SharedString::from(&line_number), color, window);
.log_err()?;
let scroll_top = scroll_position.y * line_height; let scroll_top = scroll_position.y * line_height;
let line_origin = gutter_hitbox.map(|hitbox| { let line_origin = gutter_hitbox.map(|hitbox| {
hitbox.origin hitbox.origin
@ -2808,7 +2804,7 @@ impl EditorElement {
.chain(iter::repeat("")) .chain(iter::repeat(""))
.take(rows.len()); .take(rows.len());
placeholder_lines placeholder_lines
.filter_map(move |line| { .map(move |line| {
let run = TextRun { let run = TextRun {
len: line.len(), len: line.len(),
font: style.text.font(), font: style.text.font(),
@ -2817,17 +2813,17 @@ impl EditorElement {
underline: None, underline: None,
strikethrough: None, strikethrough: None,
}; };
window let line =
.text_system() window
.shape_line(line.to_string().into(), font_size, &[run]) .text_system()
.log_err() .shape_line(line.to_string().into(), font_size, &[run]);
}) LineWithInvisibles {
.map(|line| LineWithInvisibles { width: line.width,
width: line.width, len: line.len,
len: line.len, fragments: smallvec![LineFragment::Text(line)],
fragments: smallvec![LineFragment::Text(line)], invisibles: Vec::new(),
invisibles: Vec::new(), font_size,
font_size, }
}) })
.collect() .collect()
} else { } else {
@ -4764,13 +4760,7 @@ impl EditorElement {
let Some(()) = (if !is_singleton && hitbox.is_hovered(window) { let Some(()) = (if !is_singleton && hitbox.is_hovered(window) {
let color = cx.theme().colors().editor_hover_line_number; let color = cx.theme().colors().editor_hover_line_number;
let Some(line) = self let line = self.shape_line_number(shaped_line.text.clone(), color, window);
.shape_line_number(shaped_line.text.clone(), color, window)
.log_err()
else {
continue;
};
line.paint(hitbox.origin, line_height, window, cx).log_err() line.paint(hitbox.origin, line_height, window, cx).log_err()
} else { } else {
shaped_line shaped_line
@ -6137,21 +6127,18 @@ impl EditorElement {
fn column_pixels(&self, column: usize, window: &mut Window, _: &mut App) -> Pixels { fn column_pixels(&self, column: usize, window: &mut Window, _: &mut App) -> Pixels {
let style = &self.style; let style = &self.style;
let font_size = style.text.font_size.to_pixels(window.rem_size()); let font_size = style.text.font_size.to_pixels(window.rem_size());
let layout = window let layout = window.text_system().shape_line(
.text_system() SharedString::from(" ".repeat(column)),
.shape_line( font_size,
SharedString::from(" ".repeat(column)), &[TextRun {
font_size, len: column,
&[TextRun { font: style.text.font(),
len: column, color: Hsla::default(),
font: style.text.font(), background_color: None,
color: Hsla::default(), underline: None,
background_color: None, strikethrough: None,
underline: None, }],
strikethrough: None, );
}],
)
.unwrap();
layout.width layout.width
} }
@ -6171,7 +6158,7 @@ impl EditorElement {
text: SharedString, text: SharedString,
color: Hsla, color: Hsla,
window: &mut Window, window: &mut Window,
) -> anyhow::Result<ShapedLine> { ) -> ShapedLine {
let run = TextRun { let run = TextRun {
len: text.len(), len: text.len(),
font: self.style.text.font(), font: self.style.text.font(),
@ -6451,10 +6438,10 @@ impl LineWithInvisibles {
}]) { }]) {
if let Some(replacement) = highlighted_chunk.replacement { if let Some(replacement) = highlighted_chunk.replacement {
if !line.is_empty() { if !line.is_empty() {
let shaped_line = window let shaped_line =
.text_system() window
.shape_line(line.clone().into(), font_size, &styles) .text_system()
.unwrap(); .shape_line(line.clone().into(), font_size, &styles);
width += shaped_line.width; width += shaped_line.width;
len += shaped_line.len; len += shaped_line.len;
fragments.push(LineFragment::Text(shaped_line)); fragments.push(LineFragment::Text(shaped_line));
@ -6470,14 +6457,11 @@ impl LineWithInvisibles {
} else { } else {
SharedString::from(Arc::from(highlighted_chunk.text)) SharedString::from(Arc::from(highlighted_chunk.text))
}; };
let shaped_line = window let shaped_line = window.text_system().shape_line(
.text_system() chunk,
.shape_line( font_size,
chunk, &[text_style.to_run(highlighted_chunk.text.len())],
font_size, );
&[text_style.to_run(highlighted_chunk.text.len())],
)
.unwrap();
AvailableSpace::Definite(shaped_line.width) AvailableSpace::Definite(shaped_line.width)
} else { } else {
AvailableSpace::MinContent AvailableSpace::MinContent
@ -6522,7 +6506,6 @@ impl LineWithInvisibles {
let line_layout = window let line_layout = window
.text_system() .text_system()
.shape_line(x, font_size, &[run]) .shape_line(x, font_size, &[run])
.unwrap()
.with_len(highlighted_chunk.text.len()); .with_len(highlighted_chunk.text.len());
width += line_layout.width; width += line_layout.width;
@ -6533,10 +6516,11 @@ impl LineWithInvisibles {
} else { } else {
for (ix, mut line_chunk) in highlighted_chunk.text.split('\n').enumerate() { for (ix, mut line_chunk) in highlighted_chunk.text.split('\n').enumerate() {
if ix > 0 { if ix > 0 {
let shaped_line = window let shaped_line = window.text_system().shape_line(
.text_system() line.clone().into(),
.shape_line(line.clone().into(), font_size, &styles) font_size,
.unwrap(); &styles,
);
width += shaped_line.width; width += shaped_line.width;
len += shaped_line.len; len += shaped_line.len;
fragments.push(LineFragment::Text(shaped_line)); fragments.push(LineFragment::Text(shaped_line));
@ -8038,36 +8022,30 @@ impl Element for EditorElement {
}); });
let invisible_symbol_font_size = font_size / 2.; let invisible_symbol_font_size = font_size / 2.;
let tab_invisible = window let tab_invisible = window.text_system().shape_line(
.text_system() "".into(),
.shape_line( invisible_symbol_font_size,
"".into(), &[TextRun {
invisible_symbol_font_size, len: "".len(),
&[TextRun { font: self.style.text.font(),
len: "".len(), color: cx.theme().colors().editor_invisible,
font: self.style.text.font(), background_color: None,
color: cx.theme().colors().editor_invisible, underline: None,
background_color: None, strikethrough: None,
underline: None, }],
strikethrough: None, );
}], let space_invisible = window.text_system().shape_line(
) "".into(),
.unwrap(); invisible_symbol_font_size,
let space_invisible = window &[TextRun {
.text_system() len: "".len(),
.shape_line( font: self.style.text.font(),
"".into(), color: cx.theme().colors().editor_invisible,
invisible_symbol_font_size, background_color: None,
&[TextRun { underline: None,
len: "".len(), strikethrough: None,
font: self.style.text.font(), }],
color: cx.theme().colors().editor_invisible, );
background_color: None,
underline: None,
strikethrough: None,
}],
)
.unwrap();
let mode = snapshot.mode.clone(); let mode = snapshot.mode.clone();

View file

@ -481,8 +481,7 @@ impl Element for TextElement {
let font_size = style.font_size.to_pixels(window.rem_size()); let font_size = style.font_size.to_pixels(window.rem_size());
let line = window let line = window
.text_system() .text_system()
.shape_line(display_text, font_size, &runs) .shape_line(display_text, font_size, &runs);
.unwrap();
let cursor_pos = line.x_for_index(cursor); let cursor_pos = line.x_for_index(cursor);
let (selection, cursor) = if selected_range.is_empty() { let (selection, cursor) = if selected_range.is_empty() {

View file

@ -343,7 +343,7 @@ impl WindowTextSystem {
text: SharedString, text: SharedString,
font_size: Pixels, font_size: Pixels,
runs: &[TextRun], runs: &[TextRun],
) -> Result<ShapedLine> { ) -> ShapedLine {
debug_assert!( debug_assert!(
text.find('\n').is_none(), text.find('\n').is_none(),
"text argument should not contain newlines" "text argument should not contain newlines"
@ -370,13 +370,13 @@ impl WindowTextSystem {
}); });
} }
let layout = self.layout_line(&text, font_size, runs)?; let layout = self.layout_line(&text, font_size, runs);
Ok(ShapedLine { ShapedLine {
layout, layout,
text, text,
decoration_runs, decoration_runs,
}) }
} }
/// Shape a multi line string of text, at the given font_size, for painting to the screen. /// Shape a multi line string of text, at the given font_size, for painting to the screen.
@ -510,7 +510,7 @@ impl WindowTextSystem {
text: Text, text: Text,
font_size: Pixels, font_size: Pixels,
runs: &[TextRun], runs: &[TextRun],
) -> Result<Arc<LineLayout>> ) -> Arc<LineLayout>
where where
Text: AsRef<str>, Text: AsRef<str>,
SharedString: From<Text>, SharedString: From<Text>,
@ -537,7 +537,7 @@ impl WindowTextSystem {
font_runs.clear(); font_runs.clear();
self.font_runs_pool.lock().push(font_runs); self.font_runs_pool.lock().push(font_runs);
Ok(layout) layout
} }
} }

View file

@ -106,10 +106,7 @@ impl TableView {
for field in table.schema.fields.iter() { for field in table.schema.fields.iter() {
runs[0].len = field.name.len(); runs[0].len = field.name.len();
let mut width = text_system let mut width = text_system.layout_line(&field.name, font_size, &runs).width;
.layout_line(&field.name, font_size, &runs)
.map(|layout| layout.width)
.unwrap_or(px(0.));
let Some(data) = table.data.as_ref() else { let Some(data) = table.data.as_ref() else {
widths.push(width); widths.push(width);
@ -122,8 +119,7 @@ impl TableView {
let cell_width = window let cell_width = window
.text_system() .text_system()
.layout_line(&content, font_size, &runs) .layout_line(&content, font_size, &runs)
.map(|layout| layout.width) .width;
.unwrap_or(px(0.));
width = width.max(cell_width) width = width.max(cell_width)
} }

View file

@ -288,13 +288,11 @@ impl TerminalElement {
let cell_style = let cell_style =
TerminalElement::cell_style(&cell, fg, theme, text_style, hyperlink); TerminalElement::cell_style(&cell, fg, theme, text_style, hyperlink);
let layout_cell = text_system let layout_cell = text_system.shape_line(
.shape_line( cell_text.into(),
cell_text.into(), text_style.font_size.to_pixels(window.rem_size()),
text_style.font_size.to_pixels(window.rem_size()), &[cell_style],
&[cell_style], );
)
.unwrap();
cells.push(LayoutCell::new( cells.push(LayoutCell::new(
AlacPoint::new(line_index as i32, cell.point.column.0 as i32), AlacPoint::new(line_index as i32, cell.point.column.0 as i32),
@ -813,21 +811,18 @@ impl Element for TerminalElement {
let cursor_text = { let cursor_text = {
let str_trxt = cursor_char.to_string(); let str_trxt = cursor_char.to_string();
let len = str_trxt.len(); let len = str_trxt.len();
window window.text_system().shape_line(
.text_system() str_trxt.into(),
.shape_line( text_style.font_size.to_pixels(window.rem_size()),
str_trxt.into(), &[TextRun {
text_style.font_size.to_pixels(window.rem_size()), len,
&[TextRun { font: text_style.font(),
len, color: theme.colors().terminal_ansi_background,
font: text_style.font(), background_color: None,
color: theme.colors().terminal_ansi_background, underline: Default::default(),
background_color: None, strikethrough: None,
underline: Default::default(), }],
strikethrough: None, )
}],
)
.unwrap()
}; };
let focused = self.focused; let focused = self.focused;
@ -1008,21 +1003,18 @@ impl Element for TerminalElement {
wavy: false, wavy: false,
}); });
let shaped_line = window let shaped_line = window.text_system().shape_line(
.text_system() text_to_mark.clone().into(),
.shape_line( ime_style.font_size.to_pixels(window.rem_size()),
text_to_mark.clone().into(), &[TextRun {
ime_style.font_size.to_pixels(window.rem_size()), len: text_to_mark.len(),
&[TextRun { font: ime_style.font(),
len: text_to_mark.len(), color: ime_style.color,
font: ime_style.font(), background_color: None,
color: ime_style.color, underline: ime_style.underline,
background_color: None, strikethrough: None,
underline: ime_style.underline, }],
strikethrough: None, );
}],
)
.unwrap();
shaped_line shaped_line
.paint(ime_position, layout.dimensions.line_height, window, cx) .paint(ime_position, layout.dimensions.line_height, window, cx)
.log_err(); .log_err();