From b057f2e8adcae7b347d9bf81f03c51866fe1e692 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Wed, 6 Mar 2024 16:15:50 -0800 Subject: [PATCH] Added Kludgine::rebuild_font_system --- CHANGELOG.md | 3 +++ src/text.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c696d0bd7..62e94df4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A new feature `plotters` enables integration with the excellent [plotters][plotters] crate. `Renderer::as_plot_area()` is a new function that returns a `plotters::DrawingArea`. +- `Kludgine::rebuild_font_system()` is a new function that recreates the + `cosmic_text::FontSystem`, which has the net effect of clearing font-database + related caches. [plotters]: https://github.com/plotters-rs/plotters diff --git a/src/text.rs b/src/text.rs index 7a3a9135d..3a7d85054 100644 --- a/src/text.rs +++ b/src/text.rs @@ -24,6 +24,20 @@ impl Kludgine { &mut self.text.fonts } + /// Rebuilds the font system, invalidating font database caches. + /// + /// This function can be invoked after loading fonts into the font database + /// to ensure that all future text rendering considers the newly loaded + /// fonts. + pub fn rebuild_font_system(&mut self) { + let existing_system = std::mem::replace( + &mut self.text.fonts, + cosmic_text::FontSystem::new_with_fonts([]), + ); + let (locale, db) = existing_system.into_locale_and_db(); + self.text.fonts = cosmic_text::FontSystem::new_with_locale_and_db(locale, db); + } + pub(crate) fn update_scratch_buffer(&mut self, text: &str, width: Option) { self.text.update_scratch_buffer(text, self.scale, width); }