From f82094703e93110689a02ffd07b5f11a3db6d563 Mon Sep 17 00:00:00 2001 From: tamewild <138316124+tamewild@users.noreply.github.com> Date: Sun, 28 Jul 2024 18:34:16 +0700 Subject: [PATCH] iterate on visible runs within textbounds (#108) --- src/text_render.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/text_render.rs b/src/text_render.rs index ce5549b..f73da12 100644 --- a/src/text_render.rs +++ b/src/text_render.rs @@ -60,7 +60,18 @@ impl TextRenderer { let resolution = viewport.resolution(); for text_area in text_areas { - for run in text_area.buffer.layout_runs() { + let is_run_visible = |run: &cosmic_text::LayoutRun| { + let start_y = (text_area.top + run.line_top) as i32; + let end_y = (text_area.top + run.line_top + run.line_height) as i32; + + start_y <= text_area.bounds.bottom && text_area.bounds.top <= end_y + }; + + let layout_runs = text_area.buffer.layout_runs() + .skip_while(|run| !is_run_visible(run)) + .take_while(is_run_visible); + + for run in layout_runs { for glyph in run.glyphs.iter() { let physical_glyph = glyph.physical((text_area.left, text_area.top), text_area.scale);