Fix blocking when using custom large font files (#594)

* Fix blocking when using custom large font files

* Add docstring explaining laziness

* Put characters behind a epaint::RwLock

* cargo fmt font.rs

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
zu1k 2021-08-17 04:17:31 +08:00 committed by GitHub
parent ff8c4c0d38
commit 1fc2510b3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -192,7 +192,8 @@ type FontIndex = usize;
pub struct Font {
text_style: TextStyle,
fonts: Vec<Arc<FontImpl>>,
characters: std::collections::BTreeSet<char>,
/// Lazily calculated.
characters: RwLock<Option<std::collections::BTreeSet<char>>>,
replacement_glyph: (FontIndex, GlyphInfo),
pixels_per_point: f32,
row_height: f32,
@ -201,16 +202,11 @@ pub struct Font {
impl Font {
pub fn new(text_style: TextStyle, fonts: Vec<Arc<FontImpl>>) -> Self {
let mut characters = BTreeSet::new();
for font in &fonts {
characters.extend(font.characters());
}
if fonts.is_empty() {
return Self {
text_style,
fonts,
characters,
characters: RwLock::new(None),
replacement_glyph: Default::default(),
pixels_per_point: 0.0,
row_height: 0.0,
@ -224,7 +220,7 @@ impl Font {
let mut slf = Self {
text_style,
fonts,
characters,
characters: RwLock::new(None),
replacement_glyph: Default::default(),
pixels_per_point,
row_height,
@ -258,8 +254,15 @@ impl Font {
}
/// All supported characters
pub fn characters(&self) -> &BTreeSet<char> {
&self.characters
pub fn characters(&self) -> BTreeSet<char> {
if self.characters.read().is_none() {
let mut characters = BTreeSet::new();
for font in &self.fonts {
characters.extend(font.characters());
}
self.characters.write().replace(characters);
}
self.characters.read().clone().unwrap()
}
#[inline(always)]