Repository navigation
Stop full screen lyrics from climbing to gigabytes of memory #1357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,47 @@ 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<egui::Galley>, scale: f32) -> std::sync::Arc<egui::Galley> { | ||
| 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)); | ||
| // That scales the rows and their letters, and leaves each glyph where | ||
| // and as large as it was laid out. The emoji plugin paints its pictures | ||
| // from the glyphs, so they go along. | ||
| for placed in &mut std::sync::Arc::make_mut(&mut shape.galley).rows { | ||
| for glyph in &mut std::sync::Arc::make_mut(&mut placed.row).glyphs { | ||
| glyph.pos *= scale; | ||
| glyph.advance_width *= scale; | ||
| glyph.line_height *= scale; | ||
| glyph.font_ascent *= scale; | ||
| glyph.font_height *= scale; | ||
| glyph.font_face_ascent *= scale; | ||
| glyph.font_face_height *= scale; | ||
| glyph.uv_rect.offset *= scale; | ||
| glyph.uv_rect.size *= 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 +970,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 +986,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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: AGENTS.md (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added to the description, under Visual review: matching recordings at 800x600 and 1280x800, Before and After side by side, in dark and light, with the line change also four times slower. One difference at rest is stated there too: at 1280x800 the block of lyrics rests 1 px lower, and the description says where that pixel comes from.
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| let center = ui.cursor().top() + galley.size().y * 0.5; | ||
| let edge = ((center - viewport.top()).min(viewport.bottom() - center) | ||
| / (size * 2.0)) | ||
|
|
@@ -1007,6 +1052,119 @@ 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<egui::Galley> { | ||
| 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<egui::Galley> { | ||
| crate::bidi::layout( | ||
| ui.painter(), | ||
| text, | ||
| 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"); | ||
| } | ||
|
|
||
| /// 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::<f32>() / 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(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.