From 90408fad2a6f0ceab7fe0d191a1739a073f49a72 Mon Sep 17 00:00:00 2001 From: Espaye <233430470+Espaye@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:01:11 +0100 Subject: [PATCH 1/2] Draw a growing lyric line from two sizes of letters, so the font atlas stops filling In full screen the line being sung grows over 0.3 s, and it was laid out at a font size of its own on every frame of that. egui keeps each letter at each size it was asked for and evicts nothing, so every line change added letters used once. The atlas is as wide as the largest texture and doubles in height when full, up to 16384 x 16384: a gigabyte in the process and as much again in the graphics driver, in steps that double (#954). A line is now laid out at the quiet or the sung size, whichever is nearer, and that picture is scaled the rest of the way. --- src/ui/lyrics.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/src/ui/lyrics.rs b/src/ui/lyrics.rs index 829bca046..3c9e0d96b 100644 --- a/src/ui/lyrics.rs +++ b/src/ui/lyrics.rs @@ -36,6 +36,31 @@ fn blend(from: egui::Color32, to: egui::Color32, t: f32) -> egui::Color32 { egui::Color32::from(egui::Rgba::from(from) * (1.0 - t) + egui::Rgba::from(to) * t) } +/// What a line `grown` times its quiet size is drawn from: the size its +/// letters are laid out at, as a multiple of the quiet size, and how far +/// that picture is then scaled. Letters are only ever drawn at the quiet +/// and the sung size. The font atlas keeps every letter at every size it +/// was asked for until it is full, so a line growing through a size of its +/// own each frame fills it with letters used once, up to a gigabyte (#954). +fn drawn_from(grown: f32) -> (f32, f32) { + if grown < (1.0 + SUNG_LINE_GROWTH) / 2.0 { + (1.0, grown) + } else { + (SUNG_LINE_GROWTH, grown / SUNG_LINE_GROWTH) + } +} + +/// `galley` drawn `scale` times as large, from the letters it already has +/// in the font atlas. +fn scaled(galley: std::sync::Arc, scale: f32) -> std::sync::Arc { + if scale == 1.0 { + return galley; + } + let mut shape = egui::epaint::TextShape::new(pos2(0.0, 0.0), galley, Color32::TRANSPARENT); + shape.transform(egui::emath::TSTransform::from_scaling(scale)); + shape.galley +} + pub fn side_panel(app: &mut App, ui: &mut egui::Ui) { let palette = app.palette; let fit = super::yielding_panel( @@ -929,8 +954,11 @@ fn fullscreen_contents(app: &mut App, ui: &mut egui::Ui) { } else { 1.0 }; - let wrap = ui.available_width() / SUNG_LINE_GROWTH * grown; - let font = theme::bold(size * grown); + // On its way between quiet and sung, a line is the nearer + // size's letters scaled, never a size of its own. + let (drawn, scale) = drawn_from(grown); + let wrap = ui.available_width() / SUNG_LINE_GROWTH * drawn; + let font = theme::bold(size * drawn); // A timed line with no words is the band playing on. let text = if line.text.is_empty() && lyrics.synced { "\u{266a}" @@ -942,6 +970,7 @@ fn fullscreen_contents(app: &mut App, ui: &mut egui::Ui) { let sense = Sense::click(); let galley = crate::bidi::layout(ui.painter(), text, font, color, wrap, usize::MAX, None); + let galley = scaled(galley, scale); let center = ui.cursor().top() + galley.size().y * 0.5; let edge = ((center - viewport.top()).min(viewport.bottom() - center) / (size * 2.0)) @@ -1007,6 +1036,73 @@ fn fullscreen_contents(app: &mut App, ui: &mut egui::Ui) { mod tests { use super::{fullscreen_content_width, preferred_backdrop_art}; + /// A line growing into the sung line is laid out at one of two sizes all + /// the way, and scaled to the rest, so the font atlas is asked for two + /// sizes however long the songs go on. + #[test] + fn a_growing_line_is_drawn_from_the_quiet_or_the_sung_letters() { + let sung = super::SUNG_LINE_GROWTH; + for step in 0..=300 { + let grown = 1.0 + (sung - 1.0) * step as f32 / 300.0; + let (drawn, scale) = super::drawn_from(grown); + assert!(drawn == 1.0 || drawn == sung, "step {step}"); + assert!((drawn * scale - grown).abs() < 1e-6, "step {step}"); + } + // At rest a line is its letters as they were drawn. + assert_eq!(super::drawn_from(1.0), (1.0, 1.0)); + assert_eq!(super::drawn_from(sung), (sung, 1.0)); + } + + /// A lyric line laid out at `size` points. + fn line(ui: &egui::Ui, size: f32) -> std::sync::Arc { + crate::bidi::layout( + ui.painter(), + "Words of a song", + crate::theme::bold(size), + egui::Color32::WHITE, + f32::INFINITY, + usize::MAX, + None, + ) + } + + /// What the font atlas holds after a pass that draws `pass`. + fn atlas_after(ctx: &egui::Context, pass: impl Fn(&egui::Ui)) -> egui::ColorImage { + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| pass(ui)); + output.textures_delta.clear(); + ctx.fonts(|fonts| fonts.image()) + } + + /// A scaled line takes the room of its size and is made of the letters + /// already in the font atlas, where a line laid out at a size of its own + /// adds its letters to it. + #[test] + fn a_scaled_line_adds_no_letters_to_the_font_atlas() { + let ctx = egui::Context::default(); + crate::theme::install(&ctx); + // Fonts set on a context take effect from its next pass. + ctx.run_ui(egui::RawInput::default(), |_| {}) + .textures_delta + .clear(); + let before = atlas_after(&ctx, |ui| { + let _galley = line(ui, 30.0); + }); + let after = atlas_after(&ctx, |ui| { + let size = line(ui, 30.0).size(); + for step in 1..=20 { + let scale = 1.0 + 0.15 * step as f32 / 20.0; + let grown = super::scaled(line(ui, 30.0), scale).size(); + assert!((grown.x - size.x * scale).abs() < 0.01, "step {step}"); + assert!((grown.y - size.y * scale).abs() < 0.01, "step {step}"); + } + }); + assert!(before == after, "a scaled line added letters"); + let resized = atlas_after(&ctx, |ui| { + let _galley = line(ui, 31.5); + }); + assert!(after != resized, "a size of its own adds its letters"); + } + #[test] fn fullscreen_backdrop_prefers_small_art_with_large_art_as_fallback() { let small = "small".to_string(); From 8dbd463f8d61c84b1defad4e53750e74f7ee57e9 Mon Sep 17 00:00:00 2001 From: Espaye <233430470+Espaye@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:58:24 +0100 Subject: [PATCH 2/2] Move a scaled lyric line's glyphs with its letters, so an emoji stays on it TextShape::transform scales a galley's rows and meshes and leaves each glyph's position and advance as laid out. The emoji plugin paints an emoji's picture from its glyph, so on a growing line the picture stayed where the emoji had been laid out while the letters around it moved: off by up to 7.5% of its distance along the row, and jumping at the midpoint where the line changes from the quiet letters to the sung ones. scaled() now scales the glyphs too. Found by CodeRabbit on #1357. --- src/ui/lyrics.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/ui/lyrics.rs b/src/ui/lyrics.rs index 3c9e0d96b..0fb9da8f3 100644 --- a/src/ui/lyrics.rs +++ b/src/ui/lyrics.rs @@ -58,6 +58,22 @@ fn scaled(galley: std::sync::Arc, scale: f32) -> std::sync::Arc std::sync::Arc { + line_of(ui, "Words of a song", size) + } + + /// `text` as a lyric line laid out at `size` points. + fn line_of(ui: &egui::Ui, text: &str, size: f32) -> std::sync::Arc { crate::bidi::layout( ui.painter(), - "Words of a song", + text, crate::theme::bold(size), egui::Color32::WHITE, f32::INFINITY, @@ -1103,6 +1124,47 @@ mod tests { assert!(after != resized, "a size of its own adds its letters"); } + /// Whether every letter `galley` draws has its middle inside the span + /// its glyph says it takes. + fn letters_in_place(galley: &egui::Galley) -> bool { + galley.rows.iter().all(|placed| { + let row = &placed.row; + row.glyphs.iter().all(|glyph| { + if glyph.uv_rect.is_nothing() { + return true; + } + let first = glyph.first_vertex as usize; + let quad = &row.visuals.mesh.vertices[first..first + 4]; + let middle = quad.iter().map(|vertex| vertex.pos.x).sum::() / 4.0; + (glyph.pos.x..=glyph.max_x()).contains(&middle) + }) + }) + } + + /// A scaled line's glyphs say where its letters now are. An emoji's + /// picture is painted from its glyph, so on a growing line it would + /// otherwise stay where the emoji was laid out, off its place by more + /// the further along the line it is. + #[test] + fn a_scaled_line_keeps_each_glyph_under_its_letter() { + let ctx = egui::Context::default(); + crate::theme::install(&ctx); + // Fonts set on a context take effect from its next pass. + ctx.run_ui(egui::RawInput::default(), |_| {}) + .textures_delta + .clear(); + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| { + let text = "Words of a song that goes on \u{1F600} and on"; + let laid_out = line_of(ui, text, 30.0); + assert!(letters_in_place(&laid_out), "as laid out"); + for scale in [0.935, 1.05, 1.075] { + let scaled = super::scaled(laid_out.clone(), scale); + assert!(letters_in_place(&scaled), "scaled by {scale}"); + } + }); + output.textures_delta.clear(); + } + #[test] fn fullscreen_backdrop_prefers_small_art_with_large_art_as_fallback() { let small = "small".to_string();