Improve performance of layout lines

This commit is contained in:
Jeremy Soller 2022-10-05 11:28:05 -06:00
parent 682407d15a
commit 3227efef89
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
5 changed files with 85 additions and 32 deletions

View file

@ -1,7 +1,8 @@
use core::marker::PhantomData;
use super::FontLineIndex;
pub struct FontLayoutGlyph<'a, T: 'a> {
pub line_i: usize,
pub start: usize,
pub end: usize,
pub x: f32,
@ -14,6 +15,7 @@ pub struct FontLayoutGlyph<'a, T: 'a> {
}
pub struct FontLayoutLine<'a> {
pub line_i: FontLineIndex,
pub glyphs: Vec<FontLayoutGlyph<'a, ()>>,
}

View file

@ -1,11 +1,11 @@
use super::{Font, FontShapeGlyph, FontShapeLine, FontShapeSpan};
use super::{Font, FontLineIndex, FontShapeGlyph, FontShapeLine, FontShapeSpan};
pub struct FontMatches<'a> {
pub fonts: Vec<&'a Font<'a>>,
}
impl<'a> FontMatches<'a> {
fn shape_span(&self, line_i: usize, line: &str, start_span: usize, end_span: usize) -> FontShapeSpan {
fn shape_span(&self, line: &str, start_span: usize, end_span: usize) -> FontShapeSpan {
let span = &line[start_span..end_span];
let mut spans_by_font = Vec::with_capacity(self.fonts.len());
@ -44,7 +44,6 @@ impl<'a> FontMatches<'a> {
let inner = font.rusttype.glyph(rusttype::GlyphId(info.glyph_id as u16));
glyphs.push(FontShapeGlyph {
line_i,
start: start_span + info.cluster as usize,
end: end_span, // Set later
x_advance,
@ -114,7 +113,7 @@ impl<'a> FontMatches<'a> {
spans_by_font.remove(least_misses_i).1
}
pub fn shape_line(&self, line_i: usize, line: &str) -> FontShapeLine {
pub fn shape_line(&self, line_i: FontLineIndex, line: &str) -> FontShapeLine {
use unicode_script::{Script, UnicodeScript};
//TODO: more special handling of characters
@ -130,7 +129,7 @@ impl<'a> FontMatches<'a> {
let cur = c.script();
if prev != cur && prev != Script::Unknown {
// No combination, start new span
spans.push(self.shape_span(line_i, line, start, i));
spans.push(self.shape_span(line, start, i));
start = i;
prev = Script::Unknown;
} else {
@ -138,7 +137,7 @@ impl<'a> FontMatches<'a> {
}
}
spans.push(self.shape_span(line_i, line, start, line.len()));
spans.push(self.shape_span(line, start, line.len()));
let bidi = unicode_bidi::BidiInfo::new(line, None);
let rtl = if bidi.paragraphs.is_empty() {
@ -149,6 +148,7 @@ impl<'a> FontMatches<'a> {
};
FontShapeLine {
line_i,
rtl,
spans,
}

View file

@ -10,6 +10,19 @@ mod shape;
pub use self::system::*;
mod system;
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct FontLineIndex(usize);
impl FontLineIndex {
pub fn new(index: usize) -> Self {
Self(index)
}
pub fn get(&self) -> usize {
self.0
}
}
pub struct Font<'a> {
data: &'a [u8],
pub rustybuzz: rustybuzz::Face<'a>,

View file

@ -2,10 +2,9 @@
use ab_glyph::Font;
use core::marker::PhantomData;
use super::{FontLayoutGlyph, FontLayoutLine};
use super::{FontLayoutGlyph, FontLayoutLine, FontLineIndex};
pub struct FontShapeGlyph<'a> {
pub line_i: usize,
pub start: usize,
pub end: usize,
pub x_advance: f32,
@ -26,12 +25,13 @@ pub struct FontShapeSpan<'a> {
}
pub struct FontShapeLine<'a> {
pub line_i: FontLineIndex,
pub rtl: bool,
pub spans: Vec<FontShapeSpan<'a>>,
}
impl<'a> FontShapeLine<'a> {
pub fn layout(&self, font_size: i32, line_width: i32, lines: &mut Vec<FontLayoutLine<'a>>) {
pub fn layout(&self, font_size: i32, line_width: i32, layout_lines: &mut Vec<FontLayoutLine<'a>>, mut layout_i: usize) {
let mut push_line = true;
let mut glyphs = Vec::new();
@ -62,7 +62,10 @@ impl<'a> FontShapeLine<'a> {
if x < 0.0 {
let mut glyphs_swap = Vec::new();
std::mem::swap(&mut glyphs, &mut glyphs_swap);
lines.push(FontLayoutLine { glyphs: glyphs_swap });
layout_lines.push(FontLayoutLine {
line_i: self.line_i,
glyphs: glyphs_swap
});
x = line_width as f32 - x_advance;
y = 0.0;
@ -71,8 +74,11 @@ impl<'a> FontShapeLine<'a> {
if x + x_advance > line_width as f32 {
let mut glyphs_swap = Vec::new();
std::mem::swap(&mut glyphs, &mut glyphs_swap);
lines.push(FontLayoutLine { glyphs: glyphs_swap });
push_line = false;
layout_lines.insert(layout_i, FontLayoutLine {
line_i: self.line_i,
glyphs: glyphs_swap
});
layout_i += 1;
x = 0.0;
y = 0.0;
@ -99,7 +105,6 @@ impl<'a> FontShapeLine<'a> {
));
glyphs.push(FontLayoutGlyph {
line_i: glyph.line_i,
start: glyph.start,
end: glyph.end,
x,
@ -119,7 +124,11 @@ impl<'a> FontShapeLine<'a> {
}
if push_line {
lines.push(FontLayoutLine { glyphs });
layout_lines.insert(layout_i, FontLayoutLine {
line_i: self.line_i,
glyphs
});
layout_i += 1;
}
}
}