diff --git a/egui_demo_lib/src/apps/demo/font_book.rs b/egui_demo_lib/src/apps/demo/font_book.rs index 08fe47a5..d8d901b0 100644 --- a/egui_demo_lib/src/apps/demo/font_book.rs +++ b/egui_demo_lib/src/apps/demo/font_book.rs @@ -73,7 +73,7 @@ impl super::View for FontBook { let named_chars = self.named_chars.entry(text_style).or_insert_with(|| { ui.fonts() .lock() - .font(text_style) + .font_mut(text_style) .characters() .iter() .filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control()) diff --git a/epaint/src/text/font.rs b/epaint/src/text/font.rs index 1594f2fc..6e7fe987 100644 --- a/epaint/src/text/font.rs +++ b/epaint/src/text/font.rs @@ -205,7 +205,7 @@ pub struct Font { text_style: TextStyle, fonts: Vec>, /// Lazily calculated. - characters: RwLock>>, + characters: Option>, replacement_glyph: (FontIndex, GlyphInfo), pixels_per_point: f32, row_height: f32, @@ -218,7 +218,7 @@ impl Font { return Self { text_style, fonts, - characters: RwLock::new(None), + characters: None, replacement_glyph: Default::default(), pixels_per_point: 1.0, row_height: 0.0, @@ -232,7 +232,7 @@ impl Font { let mut slf = Self { text_style, fonts, - characters: RwLock::new(None), + characters: None, replacement_glyph: Default::default(), pixels_per_point, row_height, @@ -266,15 +266,14 @@ impl Font { } /// All supported characters - pub fn characters(&self) -> BTreeSet { - if self.characters.read().is_none() { + pub fn characters(&mut self) -> &BTreeSet { + self.characters.get_or_insert_with(|| { let mut characters = BTreeSet::new(); for font in &self.fonts { characters.extend(font.characters()); } - self.characters.write().replace(characters); - } - self.characters.read().clone().unwrap() + characters + }) } #[inline(always)] diff --git a/epaint/src/text/fonts.rs b/epaint/src/text/fonts.rs index 059e37ac..e3332904 100644 --- a/epaint/src/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -416,11 +416,6 @@ impl FontsImpl { &self.definitions } - #[inline] - pub fn font(&self, text_style: TextStyle) -> &Font { - &self.fonts[&text_style] - } - #[inline] pub fn font_mut(&mut self, text_style: TextStyle) -> &mut Font { self.fonts.get_mut(&text_style).unwrap()