Small optimization of tesselate_text

This commit is contained in:
Emil Ernerfeldt 2021-04-01 23:06:39 +02:00
parent 1068750bbc
commit fe0d31204e
2 changed files with 24 additions and 6 deletions

View file

@ -638,24 +638,28 @@ impl Tessellator {
fake_italics: bool, fake_italics: bool,
out: &mut Mesh, out: &mut Mesh,
) { ) {
if color == Color32::TRANSPARENT { if color == Color32::TRANSPARENT || galley.is_empty() {
return; return;
} }
galley.sanity_check(); if cfg!(debug_assertions) {
galley.sanity_check();
}
let num_chars = galley.text.chars().count(); let num_chars = galley.char_count_excluding_newlines();
out.reserve_triangles(num_chars * 2); out.reserve_triangles(num_chars * 2);
out.reserve_vertices(num_chars * 4); out.reserve_vertices(num_chars * 4);
let inv_tex_w = 1.0 / tex_size[0] as f32; let inv_tex_w = 1.0 / tex_size[0] as f32;
let inv_tex_h = 1.0 / tex_size[1] as f32; let inv_tex_h = 1.0 / tex_size[1] as f32;
let clip_rect = self.clip_rect.expand(2.0); // Some fudge to handle letters that are slightly larger than expected. let clip_slack = 2.0; // Some fudge to handle letters that are slightly larger than expected.
let clip_rect_min_y = self.clip_rect.min.y - clip_slack;
let clip_rect_max_y = self.clip_rect.max.y + clip_slack;
for row in &galley.rows { for row in &galley.rows {
let row_min_y = pos.y + row.y_min; let row_min_y = pos.y + row.y_min;
let row_max_y = pos.y + row.y_max; let row_max_y = pos.y + row.y_max;
let is_line_visible = row_max_y >= clip_rect.min.y && row_min_y <= clip_rect.max.y; let is_line_visible = clip_rect_min_y <= row_max_y && row_min_y <= clip_rect_max_y;
if self.options.coarse_tessellation_culling && !is_line_visible { if self.options.coarse_tessellation_culling && !is_line_visible {
// culling individual lines of text is important, since a single `Shape::Text` // culling individual lines of text is important, since a single `Shape::Text`

View file

@ -138,13 +138,27 @@ impl Row {
} }
impl Galley { impl Galley {
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
#[inline(always)]
pub(crate) fn char_count_excluding_newlines(&self) -> usize {
let mut char_count = 0;
for row in &self.rows {
char_count += row.char_count_excluding_newline();
}
char_count
}
pub fn sanity_check(&self) { pub fn sanity_check(&self) {
let mut char_count = 0; let mut char_count = 0;
for row in &self.rows { for row in &self.rows {
row.sanity_check(); row.sanity_check();
char_count += row.char_count_including_newline(); char_count += row.char_count_including_newline();
} }
assert_eq!(char_count, self.text.chars().count()); debug_assert_eq!(char_count, self.text.chars().count());
if let Some(last_row) = self.rows.last() { if let Some(last_row) = self.rows.last() {
debug_assert!( debug_assert!(
!last_row.ends_with_newline, !last_row.ends_with_newline,