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:
parent
ff8c4c0d38
commit
1fc2510b3b
1 changed files with 13 additions and 10 deletions
|
@ -192,7 +192,8 @@ type FontIndex = usize;
|
||||||
pub struct Font {
|
pub struct Font {
|
||||||
text_style: TextStyle,
|
text_style: TextStyle,
|
||||||
fonts: Vec<Arc<FontImpl>>,
|
fonts: Vec<Arc<FontImpl>>,
|
||||||
characters: std::collections::BTreeSet<char>,
|
/// Lazily calculated.
|
||||||
|
characters: RwLock<Option<std::collections::BTreeSet<char>>>,
|
||||||
replacement_glyph: (FontIndex, GlyphInfo),
|
replacement_glyph: (FontIndex, GlyphInfo),
|
||||||
pixels_per_point: f32,
|
pixels_per_point: f32,
|
||||||
row_height: f32,
|
row_height: f32,
|
||||||
|
@ -201,16 +202,11 @@ pub struct Font {
|
||||||
|
|
||||||
impl Font {
|
impl Font {
|
||||||
pub fn new(text_style: TextStyle, fonts: Vec<Arc<FontImpl>>) -> Self {
|
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() {
|
if fonts.is_empty() {
|
||||||
return Self {
|
return Self {
|
||||||
text_style,
|
text_style,
|
||||||
fonts,
|
fonts,
|
||||||
characters,
|
characters: RwLock::new(None),
|
||||||
replacement_glyph: Default::default(),
|
replacement_glyph: Default::default(),
|
||||||
pixels_per_point: 0.0,
|
pixels_per_point: 0.0,
|
||||||
row_height: 0.0,
|
row_height: 0.0,
|
||||||
|
@ -224,7 +220,7 @@ impl Font {
|
||||||
let mut slf = Self {
|
let mut slf = Self {
|
||||||
text_style,
|
text_style,
|
||||||
fonts,
|
fonts,
|
||||||
characters,
|
characters: RwLock::new(None),
|
||||||
replacement_glyph: Default::default(),
|
replacement_glyph: Default::default(),
|
||||||
pixels_per_point,
|
pixels_per_point,
|
||||||
row_height,
|
row_height,
|
||||||
|
@ -258,8 +254,15 @@ impl Font {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// All supported characters
|
/// All supported characters
|
||||||
pub fn characters(&self) -> &BTreeSet<char> {
|
pub fn characters(&self) -> BTreeSet<char> {
|
||||||
&self.characters
|
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)]
|
#[inline(always)]
|
||||||
|
|
Loading…
Reference in a new issue