Skip to content

Commit

Permalink
Update font_book.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustbasic authored Sep 20, 2024
1 parent 06f7094 commit 713f71a
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions crates/egui_demo_lib/src/demo/font_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ impl crate::View for FontBook {
ui.add(crate::egui_github_link_file!());
});

let available_glyphs = self
.available_glyphs
.entry(self.font_id.family.clone())
.or_insert_with(|| available_characters(ui, self.font_id.clone()));

ui.label(format!(
"The selected font supports {} characters.",
self.available_glyphs
.get(&self.font_id.family)
.map(|map| map.len())
.unwrap_or_default()
available_glyphs.len()
));

ui.horizontal_wrapped(|ui| {
Expand All @@ -74,10 +76,6 @@ impl crate::View for FontBook {
});

let filter = &self.filter;
let available_glyphs = self
.available_glyphs
.entry(self.font_id.family.clone())
.or_insert_with(|| available_characters(ui, self.font_id.family.clone()));

ui.separator();

Expand Down Expand Up @@ -105,7 +103,7 @@ impl crate::View for FontBook {
));
};

if ui.add(button).on_hover_ui(tooltip_ui).clicked() {
if ui.add(button).on_hover_ui_at_pointer(tooltip_ui).clicked() {
ui.ctx().copy_text(chr.to_string());
}
}
Expand All @@ -115,11 +113,11 @@ impl crate::View for FontBook {
}
}

fn available_characters(ui: &egui::Ui, family: egui::FontFamily) -> BTreeMap<char, GlyphInfo> {
fn available_characters(ui: &egui::Ui, font_id: egui::FontId) -> BTreeMap<char, GlyphInfo> {
ui.fonts(|f| {
f.lock()
.fonts
.font(&egui::FontId::new(10.0, family)) // size is arbitrary for getting the characters
.font(&font_id)
.characters()
.iter()
.filter(|(chr, _fonts)| !chr.is_whitespace() && !chr.is_ascii_control())
Expand Down

0 comments on commit 713f71a

Please sign in to comment.