Skip to content

Commit

Permalink
Added Kludgine::rebuild_font_system
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Mar 7, 2024
1 parent eff686f commit b057f2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Px>) {
self.text.update_scratch_buffer(text, self.scale, width);
}
Expand Down

0 comments on commit b057f2e

Please sign in to comment.