From d70c80569fcba2730fc222543c17860c606d447f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 22 Jan 2022 14:03:29 +0100 Subject: [PATCH] Replace indexing in Fonts with member function --- egui_demo_lib/src/apps/demo/code_example.rs | 2 +- egui_demo_lib/src/apps/demo/font_book.rs | 3 ++- egui_demo_lib/src/apps/demo/misc_demo_window.rs | 2 +- egui_demo_lib/src/apps/demo/scrolling.rs | 6 +++--- egui_demo_lib/src/easy_mark/easy_mark_viewer.rs | 8 ++++---- epaint/src/text/fonts.rs | 14 +++++--------- epaint/src/text/text_layout.rs | 2 +- 7 files changed, 17 insertions(+), 20 deletions(-) diff --git a/egui_demo_lib/src/apps/demo/code_example.rs b/egui_demo_lib/src/apps/demo/code_example.rs index 783e1ed4..650c80c4 100644 --- a/egui_demo_lib/src/apps/demo/code_example.rs +++ b/egui_demo_lib/src/apps/demo/code_example.rs @@ -98,7 +98,7 @@ impl CodeExample { ); ui.horizontal(|ui| { - let indentation = 8.0 * ui.fonts()[egui::TextStyle::Monospace].glyph_width(' '); + let indentation = 8.0 * ui.fonts().glyph_width(egui::TextStyle::Monospace, ' '); let item_spacing = ui.spacing_mut().item_spacing; ui.add_space(indentation - item_spacing.x); diff --git a/egui_demo_lib/src/apps/demo/font_book.rs b/egui_demo_lib/src/apps/demo/font_book.rs index fe1ce499..e3573a8b 100644 --- a/egui_demo_lib/src/apps/demo/font_book.rs +++ b/egui_demo_lib/src/apps/demo/font_book.rs @@ -71,7 +71,8 @@ impl super::View for FontBook { let text_style = self.text_style; let filter = &self.filter; let named_chars = self.named_chars.entry(text_style).or_insert_with(|| { - ui.fonts()[text_style] + ui.fonts() + .font(text_style) .characters() .iter() .filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control()) diff --git a/egui_demo_lib/src/apps/demo/misc_demo_window.rs b/egui_demo_lib/src/apps/demo/misc_demo_window.rs index 9308d335..e639c91f 100644 --- a/egui_demo_lib/src/apps/demo/misc_demo_window.rs +++ b/egui_demo_lib/src/apps/demo/misc_demo_window.rs @@ -140,7 +140,7 @@ impl Widgets { ui.horizontal_wrapped(|ui| { // Trick so we don't have to add spaces in the text below: - let width = ui.fonts()[TextStyle::Body].glyph_width(' '); + let width = ui.fonts().glyph_width(TextStyle::Body, ' '); ui.spacing_mut().item_spacing.x = width; ui.label(RichText::new("Text can have").color(Color32::from_rgb(110, 255, 110))); diff --git a/egui_demo_lib/src/apps/demo/scrolling.rs b/egui_demo_lib/src/apps/demo/scrolling.rs index 7c69a726..f4d2de9a 100644 --- a/egui_demo_lib/src/apps/demo/scrolling.rs +++ b/egui_demo_lib/src/apps/demo/scrolling.rs @@ -81,7 +81,7 @@ fn huge_content_lines(ui: &mut egui::Ui) { ui.add_space(4.0); let text_style = TextStyle::Body; - let row_height = ui.fonts()[text_style].row_height(); + let row_height = ui.fonts().row_height(text_style); let num_rows = 10_000; ScrollArea::vertical().auto_shrink([false; 2]).show_rows( ui, @@ -102,7 +102,7 @@ fn huge_content_painter(ui: &mut egui::Ui) { ui.add_space(4.0); let text_style = TextStyle::Body; - let row_height = ui.fonts()[text_style].row_height() + ui.spacing().item_spacing.y; + let row_height = ui.fonts().row_height(text_style) + ui.spacing().item_spacing.y; let num_rows = 10_000; ScrollArea::vertical() @@ -265,7 +265,7 @@ impl super::View for ScrollStickTo { ui.add_space(4.0); let text_style = TextStyle::Body; - let row_height = ui.fonts()[text_style].row_height(); + let row_height = ui.fonts().row_height(text_style); ScrollArea::vertical().stick_to_bottom().show_rows( ui, row_height, diff --git a/egui_demo_lib/src/easy_mark/easy_mark_viewer.rs b/egui_demo_lib/src/easy_mark/easy_mark_viewer.rs index 56b379f1..0dfac75d 100644 --- a/egui_demo_lib/src/easy_mark/easy_mark_viewer.rs +++ b/egui_demo_lib/src/easy_mark/easy_mark_viewer.rs @@ -18,7 +18,7 @@ pub fn easy_mark_it<'em>(ui: &mut Ui, items: impl Iterator(ui: &mut Ui, items: impl Iterator) { - let row_height = ui.fonts()[TextStyle::Body].row_height(); + let row_height = ui.fonts().row_height(TextStyle::Body); let one_indent = row_height / 2.0; match item { @@ -134,7 +134,7 @@ fn rich_text_from_style(text: &str, style: &easy_mark::Style) -> RichText { } fn bullet_point(ui: &mut Ui, width: f32) -> Response { - let row_height = ui.fonts()[TextStyle::Body].row_height(); + let row_height = ui.fonts().row_height(TextStyle::Body); let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover()); ui.painter().circle_filled( rect.center(), @@ -145,7 +145,7 @@ fn bullet_point(ui: &mut Ui, width: f32) -> Response { } fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response { - let row_height = ui.fonts()[TextStyle::Body].row_height(); + let row_height = ui.fonts().row_height(TextStyle::Body); let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover()); let text = format!("{}.", number); let text_color = ui.visuals().strong_text_color(); diff --git a/epaint/src/text/fonts.rs b/epaint/src/text/fonts.rs index f251fb1a..eb1df31e 100644 --- a/epaint/src/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -298,6 +298,11 @@ impl Fonts { &self.definitions } + #[inline] + pub fn font(&self, text_style: TextStyle) -> &Font { + &self.fonts[&text_style] + } + /// Call each frame to get the change to the font texture since last call. pub fn font_image_delta(&self) -> Option { self.atlas.lock().take_delta() @@ -382,15 +387,6 @@ impl Fonts { } } -impl std::ops::Index for Fonts { - type Output = Font; - - #[inline(always)] - fn index(&self, text_style: TextStyle) -> &Font { - &self.fonts[&text_style] - } -} - // ---------------------------------------------------------------------------- struct CachedGalley { diff --git a/epaint/src/text/text_layout.rs b/epaint/src/text/text_layout.rs index 6b755fc8..574e4619 100644 --- a/epaint/src/text/text_layout.rs +++ b/epaint/src/text/text_layout.rs @@ -86,7 +86,7 @@ fn layout_section( byte_range, format, } = section; - let font = &fonts[format.style]; + let font = fonts.font(format.style); let font_height = font.row_height(); let mut paragraph = out_paragraphs.last_mut().unwrap();