Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #393: no crash on textures past the GPU limit

aiongg9ba7e9e8c7e8
diff --cc crates/ui-egui/src/panels/glyphs.rs
index d30359543,f8fd8fdf4..de9d52d12
--- a/crates/ui-egui/src/panels/glyphs.rs
+++ b/crates/ui-egui/src/panels/glyphs.rs
@@@ -63,66 -71,21 +63,70 @@@ pub fn show(app: &mut DesignApp, ui: &m
}
});
}
- let cols = ((ui.available_width() / CELL).floor() as u32).max(1);
+ let (picked, count) = grid(ui, &db, scope, &st.family, &st.style, &q, 320.0);
+ insert = insert.or(picked);
+ if let Some(c) = insert {
+ match app.run("text.insert", json!({"text": c.to_string(), "raw": true})) {
+ Ok(_) => {
+ st.recent.retain(|x| *x != c);
+ st.recent.insert(0, c);
+ st.recent.truncate(12);
+ }
+ Err(e) => app.status(format!("Glyphs: {e}")),
+ }
+ }
+ crate::rtl::label(
+ ui,
+ egui::RichText::new(
+ crate::i18n::tr(&app.ui.language, "{count} glyphs — click to insert at the text cursor").replace("{count}", &count.to_string()),
+ )
+ .size(10.5)
+ .color(t.text_dim),
+ );
+ ui.data_mut(|d| d.insert_temp(id, st));
+}
+
+/// The characters of font `family` `style` in a grid (those matching `query`, a character or
+/// U+code, when it isn't empty), at most `max_height` tall and scrolling: the character clicked,
+/// and how many are shown.
+pub fn grid(
+ ui: &mut egui::Ui,
+ db: &designcraft_fonts::ScopedFonts<'_>,
+ scope: u32,
+ family: &str,
+ style: &str,
+ query: &str,
+ max_height: f32,
+) -> (Option<char>, usize) {
+ let t = Tokens::get(ui.ctx());
+ let face = db.face(family, style);
+ let q = query.trim();
+ let code = q.strip_prefix("U+").or_else(|| q.strip_prefix("u+")).and_then(|h| u32::from_str_radix(h, 16).ok());
+ let mut chars: Vec<char> = face.chars().into_iter().map(|(c, _)| c).filter(|c| !c.is_control()).collect();
+ chars.sort();
+ chars.dedup();
+ if !q.is_empty() {
+ chars.retain(|c| Some(*c as u32) == code || q.contains(*c) || format!("{:04X}", *c as u32).contains(&q.to_ascii_uppercase()));
+ }
+ chars.truncate(MAX);
+ // The scroll bar floats over the grid's right edge: leave it room.
+ let scroll = &ui.spacing().scroll;
+ let bar = scroll.bar_inner_margin + scroll.bar_width + scroll.bar_outer_margin;
+ let cols = (((ui.available_width() - bar) / CELL).floor() as u32).max(1);
let ppp = ui.ctx().pixels_per_point();
- let cell_px = (CELL * ppp).round() as u32;
+ let cell_px = (CELL * ppp).round().max(1.0) as u32;
+ // The grid is one texture: no more rows than the GPU's largest texture holds (a narrow
+ // panel of a large CJK font ran past it).
+ let max_rows = (ui.ctx().input(|i| i.max_texture_side) as u32 / cell_px).max(1);
+ chars.truncate((max_rows * cols) as usize);
let ink = t.text_strong;
- let key = egui::Id::new(("glyph_grid", &st.family, &st.style, scope, &q, cols, cell_px, ink.to_array()));
+ let key = egui::Id::new(("glyph_grid", family, style, scope, q, cols, cell_px, ink.to_array()));
let tex: egui::TextureHandle = match ui.data(|d| d.get_temp::<egui::TextureHandle>(key)) {
Some(t) => t,
None => {
- let img = designcraft_render::glyphs::glyph_grid(&db, &st.family, &st.style, &chars, cols, cell_px, ink.to_array());
+ let img = designcraft_render::glyphs::glyph_grid(db, family, style, &chars, cols, cell_px, ink.to_array());
let ci = egui::ColorImage::from_rgba_premultiplied([img.width as usize, img.height as usize], &img.pixels);
- let h = ui.ctx().load_texture("glyph_grid", ci, egui::TextureOptions::LINEAR);
+ let h = crate::widgets::load_texture(ui.ctx(), "glyph_grid", ci, egui::TextureOptions::LINEAR);
ui.data_mut(|d| d.insert_temp(key, h.clone()));
h
}