Lazy shaping
This commit is contained in:
parent
2206193fff
commit
aed6860568
2 changed files with 30 additions and 15 deletions
|
|
@ -41,7 +41,7 @@ impl<'a> TextBuffer<'a> {
|
||||||
if text_lines.is_empty() {
|
if text_lines.is_empty() {
|
||||||
text_lines.push(String::new());
|
text_lines.push(String::new());
|
||||||
}
|
}
|
||||||
let mut buffer = Self {
|
Self {
|
||||||
font_matches,
|
font_matches,
|
||||||
text_lines,
|
text_lines,
|
||||||
shape_lines: Vec::new(),
|
shape_lines: Vec::new(),
|
||||||
|
|
@ -50,31 +50,36 @@ impl<'a> TextBuffer<'a> {
|
||||||
line_width,
|
line_width,
|
||||||
cursor: TextCursor::default(),
|
cursor: TextCursor::default(),
|
||||||
redraw: false,
|
redraw: false,
|
||||||
};
|
}
|
||||||
buffer.reshape();
|
|
||||||
buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reshape(&mut self) {
|
pub fn shape_until(&mut self, lines: i32) {
|
||||||
let instant = Instant::now();
|
let instant = Instant::now();
|
||||||
|
|
||||||
self.shape_lines.clear();
|
let mut reshaped = 0;
|
||||||
for (line_i, text_line) in self.text_lines.iter().enumerate() {
|
while self.shape_lines.len() < self.text_lines.len()
|
||||||
self.shape_lines.push(
|
&& (self.layout_lines.len() as i32) < lines
|
||||||
self.font_matches.shape_line(FontLineIndex::new(line_i), text_line)
|
{
|
||||||
);
|
let line_i = FontLineIndex::new(self.shape_lines.len());
|
||||||
|
self.reshape_line(line_i);
|
||||||
|
reshaped += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let duration = instant.elapsed();
|
let duration = instant.elapsed();
|
||||||
eprintln!("reshape: {:?}", duration);
|
if reshaped > 0 {
|
||||||
|
eprintln!("shape_until {}: {:?}", reshaped, duration);
|
||||||
self.relayout();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reshape_line(&mut self, line_i: FontLineIndex) {
|
pub fn reshape_line(&mut self, line_i: FontLineIndex) {
|
||||||
let instant = Instant::now();
|
let instant = Instant::now();
|
||||||
|
|
||||||
self.shape_lines[line_i.get()] = self.font_matches.shape_line(line_i, &self.text_lines[line_i.get()]);
|
let shape_line = self.font_matches.shape_line(line_i, &self.text_lines[line_i.get()]);
|
||||||
|
if line_i.get() < self.shape_lines.len() {
|
||||||
|
self.shape_lines[line_i.get()] = shape_line;
|
||||||
|
} else {
|
||||||
|
self.shape_lines.insert(line_i.get(), shape_line);
|
||||||
|
}
|
||||||
|
|
||||||
let duration = instant.elapsed();
|
let duration = instant.elapsed();
|
||||||
eprintln!("reshape line {}: {:?}", line_i.get(), duration);
|
eprintln!("reshape line {}: {:?}", line_i.get(), duration);
|
||||||
|
|
@ -114,8 +119,15 @@ impl<'a> TextBuffer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let insert_i = insert_opt.unwrap_or(self.layout_lines.len());
|
||||||
|
|
||||||
let shape_line = &self.shape_lines[line_i.get()];
|
let shape_line = &self.shape_lines[line_i.get()];
|
||||||
shape_line.layout(self.font_size, self.line_width, &mut self.layout_lines, insert_opt.unwrap());
|
shape_line.layout(
|
||||||
|
self.font_size,
|
||||||
|
self.line_width,
|
||||||
|
&mut self.layout_lines,
|
||||||
|
insert_i
|
||||||
|
);
|
||||||
|
|
||||||
self.redraw = true;
|
self.redraw = true;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,9 @@ fn main() {
|
||||||
let line_height = font_sizes[font_size_i].1 * display_scale;
|
let line_height = font_sizes[font_size_i].1 * display_scale;
|
||||||
|
|
||||||
let window_lines = (window.height() as i32 + line_height - 1) / line_height;
|
let window_lines = (window.height() as i32 + line_height - 1) / line_height;
|
||||||
|
|
||||||
|
buffer.shape_until(scroll + window_lines);
|
||||||
|
|
||||||
scroll = cmp::max(0, cmp::min(
|
scroll = cmp::max(0, cmp::min(
|
||||||
buffer.layout_lines().len() as i32 - (window_lines - 1),
|
buffer.layout_lines().len() as i32 - (window_lines - 1),
|
||||||
scroll
|
scroll
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue