Invalidate existing paragraphs when new fonts are loaded

This commit is contained in:
Héctor Ramón Jiménez 2023-09-09 11:21:32 +02:00
parent 837529bc99
commit 3450987355
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
6 changed files with 127 additions and 33 deletions

View file

@ -10,30 +10,54 @@ use crate::core::font::{self, Font};
use crate::core::text::Shaping;
use crate::core::Size;
use std::borrow::Cow;
use std::sync::{self, Arc, RwLock};
#[allow(missing_debug_implementations)]
pub struct FontSystem(RwLock<cosmic_text::FontSystem>);
pub struct FontSystem {
raw: RwLock<cosmic_text::FontSystem>,
version: Version,
}
impl FontSystem {
pub fn new() -> Self {
FontSystem(RwLock::new(cosmic_text::FontSystem::new_with_fonts(
[cosmic_text::fontdb::Source::Binary(Arc::new(
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
))]
.into_iter(),
)))
FontSystem {
raw: RwLock::new(cosmic_text::FontSystem::new_with_fonts(
[cosmic_text::fontdb::Source::Binary(Arc::new(
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
))]
.into_iter(),
)),
version: Version::default(),
}
}
pub fn get_mut(&mut self) -> &mut cosmic_text::FontSystem {
self.0.get_mut().expect("Lock font system")
self.raw.get_mut().expect("Lock font system")
}
pub fn write(&self) -> sync::RwLockWriteGuard<'_, cosmic_text::FontSystem> {
self.0.write().expect("Write font system")
pub fn write(
&self,
) -> (sync::RwLockWriteGuard<'_, cosmic_text::FontSystem>, Version) {
(self.raw.write().expect("Write font system"), self.version)
}
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
let _ = self.get_mut().db_mut().load_font_source(
cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
);
self.version = Version(self.version.0 + 1);
}
pub fn version(&self) -> Version {
self.version
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Version(u32);
impl Default for FontSystem {
fn default() -> Self {
Self::new()