Separate WrappedLines from ShapedLines

ShapedLines are never wrapped, whereas WrappedLines are optionally wrapped if
they are associated with a wrap width. I tried to combine everything because
wrapping is inherently optional for the Text element, but we have a bunch of
APIs that don't make sense on a line that may wrap, so we need a distinct type
for that case.
This commit is contained in:
Nathan Sobo 2023-11-16 20:11:55 -07:00
parent e5ada92b7b
commit 9558da8681
12 changed files with 563 additions and 330 deletions

View file

@ -13,7 +13,8 @@ pub use block_map::{BlockMap, BlockPoint};
use collections::{BTreeMap, HashMap, HashSet}; use collections::{BTreeMap, HashMap, HashSet};
use fold_map::FoldMap; use fold_map::FoldMap;
use gpui::{ use gpui::{
Font, FontId, HighlightStyle, Hsla, Line, Model, ModelContext, Pixels, TextRun, UnderlineStyle, Font, FontId, HighlightStyle, Hsla, LineLayout, Model, ModelContext, Pixels, ShapedLine,
TextRun, UnderlineStyle, WrappedLine,
}; };
use inlay_map::InlayMap; use inlay_map::InlayMap;
use language::{ use language::{
@ -561,7 +562,7 @@ impl DisplaySnapshot {
}) })
} }
pub fn lay_out_line_for_row( pub fn layout_row(
&self, &self,
display_row: u32, display_row: u32,
TextLayoutDetails { TextLayoutDetails {
@ -569,7 +570,7 @@ impl DisplaySnapshot {
editor_style, editor_style,
rem_size, rem_size,
}: &TextLayoutDetails, }: &TextLayoutDetails,
) -> Line { ) -> Arc<LineLayout> {
let mut runs = Vec::new(); let mut runs = Vec::new();
let mut line = String::new(); let mut line = String::new();
@ -598,29 +599,27 @@ 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_text(&line, font_size, &runs, None) .layout_line(&line, font_size, &runs)
.unwrap() .expect("we expect the font to be loaded because it's rendered by the editor")
.pop()
.unwrap()
} }
pub fn x_for_point( pub fn x_for_display_point(
&self, &self,
display_point: DisplayPoint, display_point: DisplayPoint,
text_layout_details: &TextLayoutDetails, text_layout_details: &TextLayoutDetails,
) -> Pixels { ) -> Pixels {
let layout_line = self.lay_out_line_for_row(display_point.row(), text_layout_details); let line = self.layout_row(display_point.row(), text_layout_details);
layout_line.x_for_index(display_point.column() as usize) line.x_for_index(display_point.column() as usize)
} }
pub fn column_for_x( pub fn display_column_for_x(
&self, &self,
display_row: u32, display_row: u32,
x_coordinate: Pixels, x: Pixels,
text_layout_details: &TextLayoutDetails, details: &TextLayoutDetails,
) -> u32 { ) -> u32 {
let layout_line = self.lay_out_line_for_row(display_row, text_layout_details); let layout_line = self.layout_row(display_row, details);
layout_line.closest_index_for_x(x_coordinate) as u32 layout_line.closest_index_for_x(x) as u32
} }
pub fn chars_at( pub fn chars_at(

View file

@ -5445,7 +5445,9 @@ impl Editor {
*head.column_mut() += 1; *head.column_mut() += 1;
head = display_map.clip_point(head, Bias::Right); head = display_map.clip_point(head, Bias::Right);
let goal = SelectionGoal::HorizontalPosition( let goal = SelectionGoal::HorizontalPosition(
display_map.x_for_point(head, &text_layout_details).into(), display_map
.x_for_display_point(head, &text_layout_details)
.into(),
); );
selection.collapse_to(head, goal); selection.collapse_to(head, goal);
@ -6391,8 +6393,8 @@ impl Editor {
let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone(); let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone();
let range = oldest_selection.display_range(&display_map).sorted(); let range = oldest_selection.display_range(&display_map).sorted();
let start_x = display_map.x_for_point(range.start, &text_layout_details); let start_x = display_map.x_for_display_point(range.start, &text_layout_details);
let end_x = display_map.x_for_point(range.end, &text_layout_details); let end_x = display_map.x_for_display_point(range.end, &text_layout_details);
let positions = start_x.min(end_x)..start_x.max(end_x); let positions = start_x.min(end_x)..start_x.max(end_x);
selections.clear(); selections.clear();
@ -6431,15 +6433,16 @@ impl Editor {
let range = selection.display_range(&display_map).sorted(); let range = selection.display_range(&display_map).sorted();
debug_assert_eq!(range.start.row(), range.end.row()); debug_assert_eq!(range.start.row(), range.end.row());
let mut row = range.start.row(); let mut row = range.start.row();
let positions = if let SelectionGoal::HorizontalRange { start, end } = let positions =
selection.goal if let SelectionGoal::HorizontalRange { start, end } = selection.goal {
{ px(start)..px(end)
px(start)..px(end) } else {
} else { let start_x =
let start_x = display_map.x_for_point(range.start, &text_layout_details); display_map.x_for_display_point(range.start, &text_layout_details);
let end_x = display_map.x_for_point(range.end, &text_layout_details); let end_x =
start_x.min(end_x)..start_x.max(end_x) display_map.x_for_display_point(range.end, &text_layout_details);
}; start_x.min(end_x)..start_x.max(end_x)
};
while row != end_row { while row != end_row {
if above { if above {
@ -6992,7 +6995,7 @@ impl Editor {
let display_point = point.to_display_point(display_snapshot); let display_point = point.to_display_point(display_snapshot);
let goal = SelectionGoal::HorizontalPosition( let goal = SelectionGoal::HorizontalPosition(
display_snapshot display_snapshot
.x_for_point(display_point, &text_layout_details) .x_for_display_point(display_point, &text_layout_details)
.into(), .into(),
); );
(display_point, goal) (display_point, goal)
@ -9755,7 +9758,8 @@ impl InputHandler for Editor {
let scroll_left = scroll_position.x * em_width; let scroll_left = scroll_position.x * em_width;
let start = OffsetUtf16(range_utf16.start).to_display_point(&snapshot); let start = OffsetUtf16(range_utf16.start).to_display_point(&snapshot);
let x = snapshot.x_for_point(start, &text_layout_details) - scroll_left + self.gutter_width; let x = snapshot.x_for_display_point(start, &text_layout_details) - scroll_left
+ self.gutter_width;
let y = line_height * (start.row() as f32 - scroll_position.y); let y = line_height * (start.row() as f32 - scroll_position.y);
Some(Bounds { Some(Bounds {

View file

@ -20,10 +20,10 @@ use collections::{BTreeMap, HashMap};
use gpui::{ use gpui::{
div, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace, div, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace,
BorrowWindow, Bounds, Component, ContentMask, Corners, DispatchPhase, Edges, Element, BorrowWindow, Bounds, Component, ContentMask, Corners, DispatchPhase, Edges, Element,
ElementId, ElementInputHandler, Entity, EntityId, Hsla, InteractiveComponent, Line, ElementId, ElementInputHandler, Entity, EntityId, Hsla, InteractiveComponent, LineLayout,
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels,
ScrollWheelEvent, Size, StatefulInteractiveComponent, Style, Styled, TextRun, TextStyle, View, ScrollWheelEvent, ShapedLine, SharedString, Size, StatefulInteractiveComponent, Style, Styled,
ViewContext, WindowContext, TextRun, TextStyle, View, ViewContext, WindowContext, WrappedLine,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting; use language::language_settings::ShowWhitespaceSetting;
@ -476,7 +476,7 @@ impl EditorElement {
Self::paint_diff_hunks(bounds, layout, cx); Self::paint_diff_hunks(bounds, layout, cx);
} }
for (ix, line) in layout.line_number_layouts.iter().enumerate() { for (ix, line) in layout.line_numbers.iter().enumerate() {
if let Some(line) = line { if let Some(line) = line {
let line_origin = bounds.origin let line_origin = bounds.origin
+ point( + point(
@ -775,21 +775,21 @@ impl EditorElement {
.chars_at(cursor_position) .chars_at(cursor_position)
.next() .next()
.and_then(|(character, _)| { .and_then(|(character, _)| {
let text = character.to_string(); let text = SharedString::from(character.to_string());
let len = text.len();
cx.text_system() cx.text_system()
.layout_text( .shape_line(
&text, text,
cursor_row_layout.font_size, cursor_row_layout.font_size,
&[TextRun { &[TextRun {
len: text.len(), len,
font: self.style.text.font(), font: self.style.text.font(),
color: self.style.background, color: self.style.background,
background_color: None,
underline: None, underline: None,
}], }],
None,
) )
.unwrap() .log_err()
.pop()
}) })
} else { } else {
None None
@ -1244,20 +1244,20 @@ impl EditorElement {
let font_size = style.text.font_size.to_pixels(cx.rem_size()); let font_size = style.text.font_size.to_pixels(cx.rem_size());
let layout = cx let layout = cx
.text_system() .text_system()
.layout_text( .shape_line(
" ".repeat(column).as_str(), SharedString::from(" ".repeat(column)),
font_size, font_size,
&[TextRun { &[TextRun {
len: column, len: column,
font: style.text.font(), font: style.text.font(),
color: Hsla::default(), color: Hsla::default(),
background_color: None,
underline: None, underline: None,
}], }],
None,
) )
.unwrap(); .unwrap();
layout[0].width layout.width
} }
fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &ViewContext<Editor>) -> Pixels { fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &ViewContext<Editor>) -> Pixels {
@ -1338,7 +1338,7 @@ impl EditorElement {
relative_rows relative_rows
} }
fn layout_line_numbers( fn shape_line_numbers(
&self, &self,
rows: Range<u32>, rows: Range<u32>,
active_rows: &BTreeMap<u32, bool>, active_rows: &BTreeMap<u32, bool>,
@ -1347,12 +1347,12 @@ impl EditorElement {
snapshot: &EditorSnapshot, snapshot: &EditorSnapshot,
cx: &ViewContext<Editor>, cx: &ViewContext<Editor>,
) -> ( ) -> (
Vec<Option<gpui::Line>>, Vec<Option<ShapedLine>>,
Vec<Option<(FoldStatus, BufferRow, bool)>>, Vec<Option<(FoldStatus, BufferRow, bool)>>,
) { ) {
let font_size = self.style.text.font_size.to_pixels(cx.rem_size()); let font_size = self.style.text.font_size.to_pixels(cx.rem_size());
let include_line_numbers = snapshot.mode == EditorMode::Full; let include_line_numbers = snapshot.mode == EditorMode::Full;
let mut line_number_layouts = Vec::with_capacity(rows.len()); let mut shaped_line_numbers = Vec::with_capacity(rows.len());
let mut fold_statuses = Vec::with_capacity(rows.len()); let mut fold_statuses = Vec::with_capacity(rows.len());
let mut line_number = String::new(); let mut line_number = String::new();
let is_relative = EditorSettings::get_global(cx).relative_line_numbers; let is_relative = EditorSettings::get_global(cx).relative_line_numbers;
@ -1387,15 +1387,14 @@ impl EditorElement {
len: line_number.len(), len: line_number.len(),
font: self.style.text.font(), font: self.style.text.font(),
color, color,
background_color: None,
underline: None, underline: None,
}; };
let layout = cx let shaped_line = cx
.text_system() .text_system()
.layout_text(&line_number, font_size, &[run], None) .shape_line(line_number.clone().into(), font_size, &[run])
.unwrap()
.pop()
.unwrap(); .unwrap();
line_number_layouts.push(Some(layout)); shaped_line_numbers.push(Some(shaped_line));
fold_statuses.push( fold_statuses.push(
is_singleton is_singleton
.then(|| { .then(|| {
@ -1408,17 +1407,17 @@ impl EditorElement {
} }
} else { } else {
fold_statuses.push(None); fold_statuses.push(None);
line_number_layouts.push(None); shaped_line_numbers.push(None);
} }
} }
(line_number_layouts, fold_statuses) (shaped_line_numbers, fold_statuses)
} }
fn layout_lines( fn layout_lines(
&mut self, &mut self,
rows: Range<u32>, rows: Range<u32>,
line_number_layouts: &[Option<Line>], line_number_layouts: &[Option<ShapedLine>],
snapshot: &EditorSnapshot, snapshot: &EditorSnapshot,
cx: &ViewContext<Editor>, cx: &ViewContext<Editor>,
) -> Vec<LineWithInvisibles> { ) -> Vec<LineWithInvisibles> {
@ -1439,18 +1438,17 @@ impl EditorElement {
.chain(iter::repeat("")) .chain(iter::repeat(""))
.take(rows.len()); .take(rows.len());
placeholder_lines placeholder_lines
.map(|line| { .filter_map(move |line| {
let run = TextRun { let run = TextRun {
len: line.len(), len: line.len(),
font: self.style.text.font(), font: self.style.text.font(),
color: placeholder_color, color: placeholder_color,
background_color: None,
underline: Default::default(), underline: Default::default(),
}; };
cx.text_system() cx.text_system()
.layout_text(line, font_size, &[run], None) .shape_line(line.to_string().into(), font_size, &[run])
.unwrap() .log_err()
.pop()
.unwrap()
}) })
.map(|line| LineWithInvisibles { .map(|line| LineWithInvisibles {
line, line,
@ -1726,7 +1724,7 @@ impl EditorElement {
.head .head
}); });
let (line_number_layouts, fold_statuses) = self.layout_line_numbers( let (line_numbers, fold_statuses) = self.shape_line_numbers(
start_row..end_row, start_row..end_row,
&active_rows, &active_rows,
head_for_relative, head_for_relative,
@ -1740,8 +1738,7 @@ impl EditorElement {
let scrollbar_row_range = scroll_position.y..(scroll_position.y + height_in_lines); let scrollbar_row_range = scroll_position.y..(scroll_position.y + height_in_lines);
let mut max_visible_line_width = Pixels::ZERO; let mut max_visible_line_width = Pixels::ZERO;
let line_layouts = let line_layouts = self.layout_lines(start_row..end_row, &line_numbers, &snapshot, cx);
self.layout_lines(start_row..end_row, &line_number_layouts, &snapshot, cx);
for line_with_invisibles in &line_layouts { for line_with_invisibles in &line_layouts {
if line_with_invisibles.line.width > max_visible_line_width { if line_with_invisibles.line.width > max_visible_line_width {
max_visible_line_width = line_with_invisibles.line.width; max_visible_line_width = line_with_invisibles.line.width;
@ -1879,35 +1876,31 @@ impl EditorElement {
let invisible_symbol_font_size = font_size / 2.; let invisible_symbol_font_size = font_size / 2.;
let tab_invisible = cx let tab_invisible = cx
.text_system() .text_system()
.layout_text( .shape_line(
"", "".into(),
invisible_symbol_font_size, invisible_symbol_font_size,
&[TextRun { &[TextRun {
len: "".len(), len: "".len(),
font: self.style.text.font(), font: self.style.text.font(),
color: cx.theme().colors().editor_invisible, color: cx.theme().colors().editor_invisible,
background_color: None,
underline: None, underline: None,
}], }],
None,
) )
.unwrap()
.pop()
.unwrap(); .unwrap();
let space_invisible = cx let space_invisible = cx
.text_system() .text_system()
.layout_text( .shape_line(
"", "".into(),
invisible_symbol_font_size, invisible_symbol_font_size,
&[TextRun { &[TextRun {
len: "".len(), len: "".len(),
font: self.style.text.font(), font: self.style.text.font(),
color: cx.theme().colors().editor_invisible, color: cx.theme().colors().editor_invisible,
background_color: None,
underline: None, underline: None,
}], }],
None,
) )
.unwrap()
.pop()
.unwrap(); .unwrap();
LayoutState { LayoutState {
@ -1939,7 +1932,7 @@ impl EditorElement {
active_rows, active_rows,
highlighted_rows, highlighted_rows,
highlighted_ranges, highlighted_ranges,
line_number_layouts, line_numbers,
display_hunks, display_hunks,
blocks, blocks,
selections, selections,
@ -2199,7 +2192,7 @@ impl EditorElement {
#[derive(Debug)] #[derive(Debug)]
pub struct LineWithInvisibles { pub struct LineWithInvisibles {
pub line: Line, pub line: ShapedLine,
invisibles: Vec<Invisible>, invisibles: Vec<Invisible>,
} }
@ -2209,7 +2202,7 @@ impl LineWithInvisibles {
text_style: &TextStyle, text_style: &TextStyle,
max_line_len: usize, max_line_len: usize,
max_line_count: usize, max_line_count: usize,
line_number_layouts: &[Option<Line>], line_number_layouts: &[Option<ShapedLine>],
editor_mode: EditorMode, editor_mode: EditorMode,
cx: &WindowContext, cx: &WindowContext,
) -> Vec<Self> { ) -> Vec<Self> {
@ -2229,11 +2222,12 @@ impl LineWithInvisibles {
}]) { }]) {
for (ix, mut line_chunk) in highlighted_chunk.chunk.split('\n').enumerate() { for (ix, mut line_chunk) in highlighted_chunk.chunk.split('\n').enumerate() {
if ix > 0 { if ix > 0 {
let layout = cx let shaped_line = cx
.text_system() .text_system()
.layout_text(&line, font_size, &styles, None); .shape_line(line.clone().into(), font_size, &styles)
.unwrap();
layouts.push(Self { layouts.push(Self {
line: layout.unwrap().pop().unwrap(), line: shaped_line,
invisibles: invisibles.drain(..).collect(), invisibles: invisibles.drain(..).collect(),
}); });
@ -2267,6 +2261,7 @@ impl LineWithInvisibles {
len: line_chunk.len(), len: line_chunk.len(),
font: text_style.font(), font: text_style.font(),
color: text_style.color, color: text_style.color,
background_color: None,
underline: text_style.underline, underline: text_style.underline,
}); });
@ -3087,7 +3082,7 @@ pub struct LayoutState {
visible_display_row_range: Range<u32>, visible_display_row_range: Range<u32>,
active_rows: BTreeMap<u32, bool>, active_rows: BTreeMap<u32, bool>,
highlighted_rows: Option<Range<u32>>, highlighted_rows: Option<Range<u32>>,
line_number_layouts: Vec<Option<gpui::Line>>, line_numbers: Vec<Option<ShapedLine>>,
display_hunks: Vec<DisplayDiffHunk>, display_hunks: Vec<DisplayDiffHunk>,
blocks: Vec<BlockLayout>, blocks: Vec<BlockLayout>,
highlighted_ranges: Vec<(Range<DisplayPoint>, Hsla)>, highlighted_ranges: Vec<(Range<DisplayPoint>, Hsla)>,
@ -3100,8 +3095,8 @@ pub struct LayoutState {
code_actions_indicator: Option<CodeActionsIndicator>, code_actions_indicator: Option<CodeActionsIndicator>,
// hover_popovers: Option<(DisplayPoint, Vec<AnyElement<Editor>>)>, // hover_popovers: Option<(DisplayPoint, Vec<AnyElement<Editor>>)>,
fold_indicators: Vec<Option<AnyElement<Editor>>>, fold_indicators: Vec<Option<AnyElement<Editor>>>,
tab_invisible: Line, tab_invisible: ShapedLine,
space_invisible: Line, space_invisible: ShapedLine,
} }
struct CodeActionsIndicator { struct CodeActionsIndicator {
@ -3201,7 +3196,7 @@ fn layout_line(
snapshot: &EditorSnapshot, snapshot: &EditorSnapshot,
style: &EditorStyle, style: &EditorStyle,
cx: &WindowContext, cx: &WindowContext,
) -> Result<Line> { ) -> Result<ShapedLine> {
let mut line = snapshot.line(row); let mut line = snapshot.line(row);
if line.len() > MAX_LINE_LEN { if line.len() > MAX_LINE_LEN {
@ -3213,21 +3208,17 @@ fn layout_line(
line.truncate(len); line.truncate(len);
} }
Ok(cx cx.text_system().shape_line(
.text_system() line.into(),
.layout_text( style.text.font_size.to_pixels(cx.rem_size()),
&line, &[TextRun {
style.text.font_size.to_pixels(cx.rem_size()), len: snapshot.line_len(row) as usize,
&[TextRun { font: style.text.font(),
len: snapshot.line_len(row) as usize, color: Hsla::default(),
font: style.text.font(), background_color: None,
color: Hsla::default(), underline: None,
underline: None, }],
}], )
None,
)?
.pop()
.unwrap())
} }
#[derive(Debug)] #[derive(Debug)]
@ -3237,7 +3228,7 @@ pub struct Cursor {
line_height: Pixels, line_height: Pixels,
color: Hsla, color: Hsla,
shape: CursorShape, shape: CursorShape,
block_text: Option<Line>, block_text: Option<ShapedLine>,
} }
impl Cursor { impl Cursor {
@ -3247,7 +3238,7 @@ impl Cursor {
line_height: Pixels, line_height: Pixels,
color: Hsla, color: Hsla,
shape: CursorShape, shape: CursorShape,
block_text: Option<Line>, block_text: Option<ShapedLine>,
) -> Cursor { ) -> Cursor {
Cursor { Cursor {
origin, origin,

View file

@ -98,7 +98,7 @@ pub fn up_by_rows(
SelectionGoal::HorizontalPosition(x) => x.into(), // todo!("Can the fields in SelectionGoal by Pixels? We should extract a geometry crate and depend on that.") SelectionGoal::HorizontalPosition(x) => x.into(), // todo!("Can the fields in SelectionGoal by Pixels? We should extract a geometry crate and depend on that.")
SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(), SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(),
SelectionGoal::HorizontalRange { end, .. } => end.into(), SelectionGoal::HorizontalRange { end, .. } => end.into(),
_ => map.x_for_point(start, text_layout_details), _ => map.x_for_display_point(start, text_layout_details),
}; };
let prev_row = start.row().saturating_sub(row_count); let prev_row = start.row().saturating_sub(row_count);
@ -107,7 +107,7 @@ pub fn up_by_rows(
Bias::Left, Bias::Left,
); );
if point.row() < start.row() { if point.row() < start.row() {
*point.column_mut() = map.column_for_x(point.row(), goal_x, text_layout_details) *point.column_mut() = map.display_column_for_x(point.row(), goal_x, text_layout_details)
} else if preserve_column_at_start { } else if preserve_column_at_start {
return (start, goal); return (start, goal);
} else { } else {
@ -137,18 +137,18 @@ pub fn down_by_rows(
SelectionGoal::HorizontalPosition(x) => x.into(), SelectionGoal::HorizontalPosition(x) => x.into(),
SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(), SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(),
SelectionGoal::HorizontalRange { end, .. } => end.into(), SelectionGoal::HorizontalRange { end, .. } => end.into(),
_ => map.x_for_point(start, text_layout_details), _ => map.x_for_display_point(start, text_layout_details),
}; };
let new_row = start.row() + row_count; let new_row = start.row() + row_count;
let mut point = map.clip_point(DisplayPoint::new(new_row, 0), Bias::Right); let mut point = map.clip_point(DisplayPoint::new(new_row, 0), Bias::Right);
if point.row() > start.row() { if point.row() > start.row() {
*point.column_mut() = map.column_for_x(point.row(), goal_x, text_layout_details) *point.column_mut() = map.display_column_for_x(point.row(), goal_x, text_layout_details)
} else if preserve_column_at_end { } else if preserve_column_at_end {
return (start, goal); return (start, goal);
} else { } else {
point = map.max_point(); point = map.max_point();
goal_x = map.x_for_point(point, text_layout_details) goal_x = map.x_for_display_point(point, text_layout_details)
} }
let mut clipped_point = map.clip_point(point, Bias::Right); let mut clipped_point = map.clip_point(point, Bias::Right);

View file

@ -313,14 +313,14 @@ impl SelectionsCollection {
let is_empty = positions.start == positions.end; let is_empty = positions.start == positions.end;
let line_len = display_map.line_len(row); let line_len = display_map.line_len(row);
let layed_out_line = display_map.lay_out_line_for_row(row, &text_layout_details); let line = display_map.layout_row(row, &text_layout_details);
dbg!("****START COL****"); dbg!("****START COL****");
let start_col = layed_out_line.closest_index_for_x(positions.start) as u32; let start_col = line.closest_index_for_x(positions.start) as u32;
if start_col < line_len || (is_empty && positions.start == layed_out_line.width) { if start_col < line_len || (is_empty && positions.start == line.width) {
let start = DisplayPoint::new(row, start_col); let start = DisplayPoint::new(row, start_col);
dbg!("****END COL****"); dbg!("****END COL****");
let end_col = layed_out_line.closest_index_for_x(positions.end) as u32; let end_col = line.closest_index_for_x(positions.end) as u32;
let end = DisplayPoint::new(row, end_col); let end = DisplayPoint::new(row, end_col);
dbg!(start_col, end_col); dbg!(start_col, end_col);

View file

@ -13,7 +13,7 @@ pub trait Element<V: 'static> {
fn layout( fn layout(
&mut self, &mut self,
view_state: &mut V, view_state: &mut V,
previous_element_state: Option<Self::ElementState>, element_state: Option<Self::ElementState>,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
) -> (LayoutId, Self::ElementState); ) -> (LayoutId, Self::ElementState);

View file

@ -1,76 +1,39 @@
use crate::{ use crate::{
AnyElement, BorrowWindow, Bounds, Component, Element, LayoutId, Line, Pixels, SharedString, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, LayoutId, Pixels,
Size, TextRun, ViewContext, SharedString, Size, TextRun, ViewContext, WrappedLine,
}; };
use parking_lot::Mutex; use parking_lot::{Mutex, MutexGuard};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{marker::PhantomData, sync::Arc}; use std::{cell::Cell, rc::Rc, sync::Arc};
use util::ResultExt; use util::ResultExt;
impl<V: 'static> Component<V> for SharedString { pub struct Text {
fn render(self) -> AnyElement<V> {
Text {
text: self,
runs: None,
state_type: PhantomData,
}
.render()
}
}
impl<V: 'static> Component<V> for &'static str {
fn render(self) -> AnyElement<V> {
Text {
text: self.into(),
runs: None,
state_type: PhantomData,
}
.render()
}
}
// TODO: Figure out how to pass `String` to `child` without this.
// This impl doesn't exist in the `gpui2` crate.
impl<V: 'static> Component<V> for String {
fn render(self) -> AnyElement<V> {
Text {
text: self.into(),
runs: None,
state_type: PhantomData,
}
.render()
}
}
pub struct Text<V> {
text: SharedString, text: SharedString,
runs: Option<Vec<TextRun>>, runs: Option<Vec<TextRun>>,
state_type: PhantomData<V>,
} }
impl<V: 'static> Text<V> { impl Text {
/// styled renders text that has different runs of different styles. /// Renders text with runs of different styles.
/// callers are responsible for setting the correct style for each run. ///
//// /// Callers are responsible for setting the correct style for each run.
/// For uniform text you can usually just pass a string as a child, and /// For text with a uniform style, you can usually avoid calling this constructor
/// cx.text_style() will be used automatically. /// and just pass text directly.
pub fn styled(text: SharedString, runs: Vec<TextRun>) -> Self { pub fn styled(text: SharedString, runs: Vec<TextRun>) -> Self {
Text { Text {
text, text,
runs: Some(runs), runs: Some(runs),
state_type: Default::default(),
} }
} }
} }
impl<V: 'static> Component<V> for Text<V> { impl<V: 'static> Component<V> for Text {
fn render(self) -> AnyElement<V> { fn render(self) -> AnyElement<V> {
AnyElement::new(self) AnyElement::new(self)
} }
} }
impl<V: 'static> Element<V> for Text<V> { impl<V: 'static> Element<V> for Text {
type ElementState = Arc<Mutex<Option<TextElementState>>>; type ElementState = TextState;
fn element_id(&self) -> Option<crate::ElementId> { fn element_id(&self) -> Option<crate::ElementId> {
None None
@ -103,7 +66,7 @@ impl<V: 'static> Element<V> for Text<V> {
let element_state = element_state.clone(); let element_state = element_state.clone();
move |known_dimensions, _| { move |known_dimensions, _| {
let Some(lines) = text_system let Some(lines) = text_system
.layout_text( .shape_text(
&text, &text,
font_size, font_size,
&runs[..], &runs[..],
@ -111,30 +74,23 @@ impl<V: 'static> Element<V> for Text<V> {
) )
.log_err() .log_err()
else { else {
element_state.lock().replace(TextElementState { element_state.lock().replace(TextStateInner {
lines: Default::default(), lines: Default::default(),
line_height, line_height,
}); });
return Size::default(); return Size::default();
}; };
let line_count = lines let mut size: Size<Pixels> = Size::default();
.iter() for line in &lines {
.map(|line| line.wrap_count() + 1) let line_size = line.size(line_height);
.sum::<usize>(); size.height += line_size.height;
let size = Size { size.width = size.width.max(line_size.width);
width: lines }
.iter()
.map(|line| line.layout.width)
.max()
.unwrap()
.ceil(),
height: line_height * line_count,
};
element_state element_state
.lock() .lock()
.replace(TextElementState { lines, line_height }); .replace(TextStateInner { lines, line_height });
size size
} }
@ -165,7 +121,104 @@ impl<V: 'static> Element<V> for Text<V> {
} }
} }
pub struct TextElementState { #[derive(Default, Clone)]
lines: SmallVec<[Line; 1]>, pub struct TextState(Arc<Mutex<Option<TextStateInner>>>);
impl TextState {
fn lock(&self) -> MutexGuard<Option<TextStateInner>> {
self.0.lock()
}
}
struct TextStateInner {
lines: SmallVec<[WrappedLine; 1]>,
line_height: Pixels, line_height: Pixels,
} }
struct InteractiveText {
id: ElementId,
text: Text,
}
struct InteractiveTextState {
text_state: TextState,
clicked_range_ixs: Rc<Cell<SmallVec<[usize; 1]>>>,
}
impl<V: 'static> Element<V> for InteractiveText {
type ElementState = InteractiveTextState;
fn element_id(&self) -> Option<ElementId> {
Some(self.id.clone())
}
fn layout(
&mut self,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<V>,
) -> (LayoutId, Self::ElementState) {
if let Some(InteractiveTextState {
text_state,
clicked_range_ixs,
}) = element_state
{
let (layout_id, text_state) = self.text.layout(view_state, Some(text_state), cx);
let element_state = InteractiveTextState {
text_state,
clicked_range_ixs,
};
(layout_id, element_state)
} else {
let (layout_id, text_state) = self.text.layout(view_state, None, cx);
let element_state = InteractiveTextState {
text_state,
clicked_range_ixs: Rc::default(),
};
(layout_id, element_state)
}
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) {
self.text
.paint(bounds, view_state, &mut element_state.text_state, cx)
}
}
impl<V: 'static> Component<V> for SharedString {
fn render(self) -> AnyElement<V> {
Text {
text: self,
runs: None,
}
.render()
}
}
impl<V: 'static> Component<V> for &'static str {
fn render(self) -> AnyElement<V> {
Text {
text: self.into(),
runs: None,
}
.render()
}
}
// TODO: Figure out how to pass `String` to `child` without this.
// This impl doesn't exist in the `gpui2` crate.
impl<V: 'static> Component<V> for String {
fn render(self) -> AnyElement<V> {
Text {
text: self.into(),
runs: None,
}
.render()
}
}

View file

@ -343,10 +343,10 @@ impl MacTextSystemState {
// Construct the attributed string, converting UTF8 ranges to UTF16 ranges. // Construct the attributed string, converting UTF8 ranges to UTF16 ranges.
let mut string = CFMutableAttributedString::new(); let mut string = CFMutableAttributedString::new();
{ {
string.replace_str(&CFString::new(text), CFRange::init(0, 0)); string.replace_str(&CFString::new(text.as_ref()), CFRange::init(0, 0));
let utf16_line_len = string.char_len() as usize; let utf16_line_len = string.char_len() as usize;
let mut ix_converter = StringIndexConverter::new(text); let mut ix_converter = StringIndexConverter::new(text.as_ref());
for run in font_runs { for run in font_runs {
let utf8_end = ix_converter.utf8_ix + run.len; let utf8_end = ix_converter.utf8_ix + run.len;
let utf16_start = ix_converter.utf16_ix; let utf16_start = ix_converter.utf16_ix;
@ -390,7 +390,7 @@ impl MacTextSystemState {
}; };
let font_id = self.id_for_native_font(font); let font_id = self.id_for_native_font(font);
let mut ix_converter = StringIndexConverter::new(text); let mut ix_converter = StringIndexConverter::new(text.as_ref());
let mut glyphs = SmallVec::new(); let mut glyphs = SmallVec::new();
for ((glyph_id, position), glyph_utf16_ix) in run for ((glyph_id, position), glyph_utf16_ix) in run
.glyphs() .glyphs()
@ -413,11 +413,11 @@ impl MacTextSystemState {
let typographic_bounds = line.get_typographic_bounds(); let typographic_bounds = line.get_typographic_bounds();
LineLayout { LineLayout {
runs,
font_size,
width: typographic_bounds.width.into(), width: typographic_bounds.width.into(),
ascent: typographic_bounds.ascent.into(), ascent: typographic_bounds.ascent.into(),
descent: typographic_bounds.descent.into(), descent: typographic_bounds.descent.into(),
runs,
font_size,
len: text.len(), len: text.len(),
} }
} }

View file

@ -203,6 +203,7 @@ impl TextStyle {
style: self.font_style, style: self.font_style,
}, },
color: self.color, color: self.color,
background_color: None,
underline: self.underline.clone(), underline: self.underline.clone(),
} }
} }

View file

@ -3,20 +3,20 @@ mod line;
mod line_layout; mod line_layout;
mod line_wrapper; mod line_wrapper;
use anyhow::anyhow;
pub use font_features::*; pub use font_features::*;
pub use line::*; pub use line::*;
pub use line_layout::*; pub use line_layout::*;
pub use line_wrapper::*; pub use line_wrapper::*;
use smallvec::SmallVec;
use crate::{ use crate::{
px, Bounds, DevicePixels, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, Size, px, Bounds, DevicePixels, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, Size,
UnderlineStyle, UnderlineStyle,
}; };
use anyhow::anyhow;
use collections::HashMap; use collections::HashMap;
use core::fmt; use core::fmt;
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
use smallvec::SmallVec;
use std::{ use std::{
cmp, cmp,
fmt::{Debug, Display, Formatter}, fmt::{Debug, Display, Formatter},
@ -151,13 +151,79 @@ impl TextSystem {
} }
} }
pub fn layout_text( pub fn layout_line(
&self, &self,
text: &str, text: &str,
font_size: Pixels, font_size: Pixels,
runs: &[TextRun], runs: &[TextRun],
) -> Result<Arc<LineLayout>> {
let mut font_runs = self.font_runs_pool.lock().pop().unwrap_or_default();
for run in runs.iter() {
let font_id = self.font_id(&run.font)?;
if let Some(last_run) = font_runs.last_mut() {
if last_run.font_id == font_id {
last_run.len += run.len;
continue;
}
}
font_runs.push(FontRun {
len: run.len,
font_id,
});
}
let layout = self
.line_layout_cache
.layout_line(&text, font_size, &font_runs);
font_runs.clear();
self.font_runs_pool.lock().push(font_runs);
Ok(layout)
}
pub fn shape_line(
&self,
text: SharedString,
font_size: Pixels,
runs: &[TextRun],
) -> Result<ShapedLine> {
debug_assert!(
text.find('\n').is_none(),
"text argument should not contain newlines"
);
let mut decoration_runs = SmallVec::<[DecorationRun; 32]>::new();
for run in runs {
if let Some(last_run) = decoration_runs.last_mut() {
if last_run.color == run.color && last_run.underline == run.underline {
last_run.len += run.len as u32;
continue;
}
}
decoration_runs.push(DecorationRun {
len: run.len as u32,
color: run.color,
underline: run.underline.clone(),
});
}
let layout = self.layout_line(text.as_ref(), font_size, runs)?;
Ok(ShapedLine {
layout,
text,
decoration_runs,
})
}
pub fn shape_text(
&self,
text: &str, // todo!("pass a SharedString and preserve it when passed a single line?")
font_size: Pixels,
runs: &[TextRun],
wrap_width: Option<Pixels>, wrap_width: Option<Pixels>,
) -> Result<SmallVec<[Line; 1]>> { ) -> Result<SmallVec<[WrappedLine; 1]>> {
let mut runs = runs.iter().cloned().peekable(); let mut runs = runs.iter().cloned().peekable();
let mut font_runs = self.font_runs_pool.lock().pop().unwrap_or_default(); let mut font_runs = self.font_runs_pool.lock().pop().unwrap_or_default();
@ -210,10 +276,11 @@ impl TextSystem {
let layout = self let layout = self
.line_layout_cache .line_layout_cache
.layout_line(&line_text, font_size, &font_runs, wrap_width); .layout_wrapped_line(&line_text, font_size, &font_runs, wrap_width);
lines.push(Line { lines.push(WrappedLine {
layout, layout,
decorations: decoration_runs, decoration_runs,
text: SharedString::from(line_text),
}); });
line_start = line_end + 1; // Skip `\n` character. line_start = line_end + 1; // Skip `\n` character.
@ -384,6 +451,7 @@ pub struct TextRun {
pub len: usize, pub len: usize,
pub font: Font, pub font: Font,
pub color: Hsla, pub color: Hsla,
pub background_color: Option<Hsla>,
pub underline: Option<UnderlineStyle>, pub underline: Option<UnderlineStyle>,
} }

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
black, point, px, size, BorrowWindow, Bounds, Hsla, Pixels, Point, Result, Size, black, point, px, BorrowWindow, Bounds, Hsla, LineLayout, Pixels, Point, Result, SharedString,
UnderlineStyle, WindowContext, WrapBoundary, WrappedLineLayout, UnderlineStyle, WindowContext, WrapBoundary, WrappedLineLayout,
}; };
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
@ -14,23 +14,17 @@ pub struct DecorationRun {
} }
#[derive(Clone, Default, Debug, Deref, DerefMut)] #[derive(Clone, Default, Debug, Deref, DerefMut)]
pub struct Line { pub struct ShapedLine {
#[deref] #[deref]
#[deref_mut] #[deref_mut]
pub(crate) layout: Arc<WrappedLineLayout>, pub(crate) layout: Arc<LineLayout>,
pub(crate) decorations: SmallVec<[DecorationRun; 32]>, pub text: SharedString,
pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
} }
impl Line { impl ShapedLine {
pub fn size(&self, line_height: Pixels) -> Size<Pixels> { pub fn len(&self) -> usize {
size( self.layout.len
self.layout.width,
line_height * (self.layout.wrap_boundaries.len() + 1),
)
}
pub fn wrap_count(&self) -> usize {
self.layout.wrap_boundaries.len()
} }
pub fn paint( pub fn paint(
@ -39,75 +33,84 @@ impl Line {
line_height: Pixels, line_height: Pixels,
cx: &mut WindowContext, cx: &mut WindowContext,
) -> Result<()> { ) -> Result<()> {
let padding_top = paint_line(
(line_height - self.layout.layout.ascent - self.layout.layout.descent) / 2.; origin,
let baseline_offset = point(px(0.), padding_top + self.layout.layout.ascent); &self.layout,
line_height,
&self.decoration_runs,
None,
&[],
cx,
)?;
let mut style_runs = self.decorations.iter(); Ok(())
let mut wraps = self.layout.wrap_boundaries.iter().peekable(); }
let mut run_end = 0; }
let mut color = black();
let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
let text_system = cx.text_system().clone();
let mut glyph_origin = origin; #[derive(Clone, Default, Debug, Deref, DerefMut)]
let mut prev_glyph_position = Point::default(); pub struct WrappedLine {
for (run_ix, run) in self.layout.layout.runs.iter().enumerate() { #[deref]
let max_glyph_size = text_system #[deref_mut]
.bounding_box(run.font_id, self.layout.layout.font_size)? pub(crate) layout: Arc<WrappedLineLayout>,
.size; pub text: SharedString,
pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
}
for (glyph_ix, glyph) in run.glyphs.iter().enumerate() { impl WrappedLine {
glyph_origin.x += glyph.position.x - prev_glyph_position.x; pub fn len(&self) -> usize {
self.layout.len()
}
if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) { pub fn paint(
wraps.next(); &self,
if let Some((underline_origin, underline_style)) = current_underline.take() { origin: Point<Pixels>,
cx.paint_underline( line_height: Pixels,
underline_origin, cx: &mut WindowContext,
glyph_origin.x - underline_origin.x, ) -> Result<()> {
&underline_style, paint_line(
)?; origin,
} &self.layout.unwrapped_layout,
line_height,
&self.decoration_runs,
self.wrap_width,
&self.wrap_boundaries,
cx,
)?;
glyph_origin.x = origin.x; Ok(())
glyph_origin.y += line_height; }
} }
prev_glyph_position = glyph.position;
let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None; fn paint_line(
if glyph.index >= run_end { origin: Point<Pixels>,
if let Some(style_run) = style_runs.next() { layout: &LineLayout,
if let Some((_, underline_style)) = &mut current_underline { line_height: Pixels,
if style_run.underline.as_ref() != Some(underline_style) { decoration_runs: &[DecorationRun],
finished_underline = current_underline.take(); wrap_width: Option<Pixels>,
} wrap_boundaries: &[WrapBoundary],
} cx: &mut WindowContext<'_>,
if let Some(run_underline) = style_run.underline.as_ref() { ) -> Result<()> {
current_underline.get_or_insert(( let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
point( let baseline_offset = point(px(0.), padding_top + layout.ascent);
glyph_origin.x, let mut decoration_runs = decoration_runs.iter();
origin.y let mut wraps = wrap_boundaries.iter().peekable();
+ baseline_offset.y let mut run_end = 0;
+ (self.layout.layout.descent * 0.618), let mut color = black();
), let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
UnderlineStyle { let text_system = cx.text_system().clone();
color: Some(run_underline.color.unwrap_or(style_run.color)), let mut glyph_origin = origin;
thickness: run_underline.thickness, let mut prev_glyph_position = Point::default();
wavy: run_underline.wavy, for (run_ix, run) in layout.runs.iter().enumerate() {
}, let max_glyph_size = text_system
)); .bounding_box(run.font_id, layout.font_size)?
} .size;
run_end += style_run.len as usize; for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
color = style_run.color; glyph_origin.x += glyph.position.x - prev_glyph_position.x;
} else {
run_end = self.layout.text.len();
finished_underline = current_underline.take();
}
}
if let Some((underline_origin, underline_style)) = finished_underline { if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
wraps.next();
if let Some((underline_origin, underline_style)) = current_underline.take() {
cx.paint_underline( cx.paint_underline(
underline_origin, underline_origin,
glyph_origin.x - underline_origin.x, glyph_origin.x - underline_origin.x,
@ -115,42 +118,84 @@ impl Line {
)?; )?;
} }
let max_glyph_bounds = Bounds { glyph_origin.x = origin.x;
origin: glyph_origin, glyph_origin.y += line_height;
size: max_glyph_size, }
}; prev_glyph_position = glyph.position;
let content_mask = cx.content_mask(); let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
if max_glyph_bounds.intersects(&content_mask.bounds) { if glyph.index >= run_end {
if glyph.is_emoji { if let Some(style_run) = decoration_runs.next() {
cx.paint_emoji( if let Some((_, underline_style)) = &mut current_underline {
glyph_origin + baseline_offset, if style_run.underline.as_ref() != Some(underline_style) {
run.font_id, finished_underline = current_underline.take();
glyph.id, }
self.layout.layout.font_size,
)?;
} else {
cx.paint_glyph(
glyph_origin + baseline_offset,
run.font_id,
glyph.id,
self.layout.layout.font_size,
color,
)?;
} }
if let Some(run_underline) = style_run.underline.as_ref() {
current_underline.get_or_insert((
point(
glyph_origin.x,
origin.y + baseline_offset.y + (layout.descent * 0.618),
),
UnderlineStyle {
color: Some(run_underline.color.unwrap_or(style_run.color)),
thickness: run_underline.thickness,
wavy: run_underline.wavy,
},
));
}
run_end += style_run.len as usize;
color = style_run.color;
} else {
run_end = layout.len;
finished_underline = current_underline.take();
}
}
if let Some((underline_origin, underline_style)) = finished_underline {
cx.paint_underline(
underline_origin,
glyph_origin.x - underline_origin.x,
&underline_style,
)?;
}
let max_glyph_bounds = Bounds {
origin: glyph_origin,
size: max_glyph_size,
};
let content_mask = cx.content_mask();
if max_glyph_bounds.intersects(&content_mask.bounds) {
if glyph.is_emoji {
cx.paint_emoji(
glyph_origin + baseline_offset,
run.font_id,
glyph.id,
layout.font_size,
)?;
} else {
cx.paint_glyph(
glyph_origin + baseline_offset,
run.font_id,
glyph.id,
layout.font_size,
color,
)?;
} }
} }
} }
if let Some((underline_start, underline_style)) = current_underline.take() {
let line_end_x = origin.x + self.layout.layout.width;
cx.paint_underline(
underline_start,
line_end_x - underline_start.x,
&underline_style,
)?;
}
Ok(())
} }
if let Some((underline_start, underline_style)) = current_underline.take() {
let line_end_x = origin.x + wrap_width.unwrap_or(Pixels::MAX).min(layout.width);
cx.paint_underline(
underline_start,
line_end_x - underline_start.x,
&underline_style,
)?;
}
Ok(())
} }

View file

@ -1,5 +1,4 @@
use crate::{px, FontId, GlyphId, Pixels, PlatformTextSystem, Point, SharedString}; use crate::{px, FontId, GlyphId, Pixels, PlatformTextSystem, Point, Size};
use derive_more::{Deref, DerefMut};
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
@ -149,13 +148,11 @@ impl LineLayout {
} }
} }
#[derive(Deref, DerefMut, Default, Debug)] #[derive(Default, Debug)]
pub struct WrappedLineLayout { pub struct WrappedLineLayout {
#[deref] pub unwrapped_layout: Arc<LineLayout>,
#[deref_mut]
pub layout: LineLayout,
pub text: SharedString,
pub wrap_boundaries: SmallVec<[WrapBoundary; 1]>, pub wrap_boundaries: SmallVec<[WrapBoundary; 1]>,
pub wrap_width: Option<Pixels>,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
@ -164,31 +161,74 @@ pub struct WrapBoundary {
pub glyph_ix: usize, pub glyph_ix: usize,
} }
impl WrappedLineLayout {
pub fn len(&self) -> usize {
self.unwrapped_layout.len
}
pub fn width(&self) -> Pixels {
self.wrap_width
.unwrap_or(Pixels::MAX)
.min(self.unwrapped_layout.width)
}
pub fn size(&self, line_height: Pixels) -> Size<Pixels> {
Size {
width: self.width(),
height: line_height * (self.wrap_boundaries.len() + 1),
}
}
pub fn ascent(&self) -> Pixels {
self.unwrapped_layout.ascent
}
pub fn descent(&self) -> Pixels {
self.unwrapped_layout.descent
}
pub fn wrap_boundaries(&self) -> &[WrapBoundary] {
&self.wrap_boundaries
}
pub fn font_size(&self) -> Pixels {
self.unwrapped_layout.font_size
}
pub fn runs(&self) -> &[ShapedRun] {
&self.unwrapped_layout.runs
}
}
pub(crate) struct LineLayoutCache { pub(crate) struct LineLayoutCache {
prev_frame: Mutex<HashMap<CacheKey, Arc<WrappedLineLayout>>>, previous_frame: Mutex<HashMap<CacheKey, Arc<LineLayout>>>,
curr_frame: RwLock<HashMap<CacheKey, Arc<WrappedLineLayout>>>, current_frame: RwLock<HashMap<CacheKey, Arc<LineLayout>>>,
previous_frame_wrapped: Mutex<HashMap<CacheKey, Arc<WrappedLineLayout>>>,
current_frame_wrapped: RwLock<HashMap<CacheKey, Arc<WrappedLineLayout>>>,
platform_text_system: Arc<dyn PlatformTextSystem>, platform_text_system: Arc<dyn PlatformTextSystem>,
} }
impl LineLayoutCache { impl LineLayoutCache {
pub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self { pub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self {
Self { Self {
prev_frame: Mutex::new(HashMap::new()), previous_frame: Mutex::default(),
curr_frame: RwLock::new(HashMap::new()), current_frame: RwLock::default(),
previous_frame_wrapped: Mutex::default(),
current_frame_wrapped: RwLock::default(),
platform_text_system, platform_text_system,
} }
} }
pub fn start_frame(&self) { pub fn start_frame(&self) {
let mut prev_frame = self.prev_frame.lock(); let mut prev_frame = self.previous_frame.lock();
let mut curr_frame = self.curr_frame.write(); let mut curr_frame = self.current_frame.write();
std::mem::swap(&mut *prev_frame, &mut *curr_frame); std::mem::swap(&mut *prev_frame, &mut *curr_frame);
curr_frame.clear(); curr_frame.clear();
} }
pub fn layout_line( pub fn layout_wrapped_line(
&self, &self,
text: &SharedString, text: &str,
font_size: Pixels, font_size: Pixels,
runs: &[FontRun], runs: &[FontRun],
wrap_width: Option<Pixels>, wrap_width: Option<Pixels>,
@ -199,34 +239,66 @@ impl LineLayoutCache {
runs, runs,
wrap_width, wrap_width,
} as &dyn AsCacheKeyRef; } as &dyn AsCacheKeyRef;
let curr_frame = self.curr_frame.upgradable_read();
if let Some(layout) = curr_frame.get(key) { let current_frame = self.current_frame_wrapped.upgradable_read();
if let Some(layout) = current_frame.get(key) {
return layout.clone(); return layout.clone();
} }
let mut curr_frame = RwLockUpgradableReadGuard::upgrade(curr_frame); let mut current_frame = RwLockUpgradableReadGuard::upgrade(current_frame);
if let Some((key, layout)) = self.prev_frame.lock().remove_entry(key) { if let Some((key, layout)) = self.previous_frame_wrapped.lock().remove_entry(key) {
curr_frame.insert(key, layout.clone()); current_frame.insert(key, layout.clone());
layout layout
} else { } else {
let layout = self.platform_text_system.layout_line(text, font_size, runs); let unwrapped_layout = self.layout_line(text, font_size, runs);
let wrap_boundaries = wrap_width let wrap_boundaries = if let Some(wrap_width) = wrap_width {
.map(|wrap_width| layout.compute_wrap_boundaries(text.as_ref(), wrap_width)) unwrapped_layout.compute_wrap_boundaries(text.as_ref(), wrap_width)
.unwrap_or_default(); } else {
let wrapped_line = Arc::new(WrappedLineLayout { SmallVec::new()
layout, };
text: text.clone(), let layout = Arc::new(WrappedLineLayout {
unwrapped_layout,
wrap_boundaries, wrap_boundaries,
wrap_width,
}); });
let key = CacheKey { let key = CacheKey {
text: text.clone(), text: text.into(),
font_size, font_size,
runs: SmallVec::from(runs), runs: SmallVec::from(runs),
wrap_width, wrap_width,
}; };
curr_frame.insert(key, wrapped_line.clone()); current_frame.insert(key, layout.clone());
wrapped_line layout
}
}
pub fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> Arc<LineLayout> {
let key = &CacheKeyRef {
text,
font_size,
runs,
wrap_width: None,
} as &dyn AsCacheKeyRef;
let current_frame = self.current_frame.upgradable_read();
if let Some(layout) = current_frame.get(key) {
return layout.clone();
}
let mut current_frame = RwLockUpgradableReadGuard::upgrade(current_frame);
if let Some((key, layout)) = self.previous_frame.lock().remove_entry(key) {
current_frame.insert(key, layout.clone());
layout
} else {
let layout = Arc::new(self.platform_text_system.layout_line(text, font_size, runs));
let key = CacheKey {
text: text.into(),
font_size,
runs: SmallVec::from(runs),
wrap_width: None,
};
current_frame.insert(key, layout.clone());
layout
} }
} }
} }
@ -243,7 +315,7 @@ trait AsCacheKeyRef {
#[derive(Eq)] #[derive(Eq)]
struct CacheKey { struct CacheKey {
text: SharedString, text: String,
font_size: Pixels, font_size: Pixels,
runs: SmallVec<[FontRun; 1]>, runs: SmallVec<[FontRun; 1]>,
wrap_width: Option<Pixels>, wrap_width: Option<Pixels>,