diff options
| author | 2023-09-09 11:21:32 +0200 | |
|---|---|---|
| committer | 2023-09-09 11:21:32 +0200 | |
| commit | 3450987355be7fe029db112474d06613929b54c7 (patch) | |
| tree | 1d2186d822add1becdd5c942cc6a6c67e5437b57 /graphics | |
| parent | 837529bc995a728300c61fc102474cc31f7a6500 (diff) | |
| download | iced-3450987355be7fe029db112474d06613929b54c7.tar.gz iced-3450987355be7fe029db112474d06613929b54c7.tar.bz2 iced-3450987355be7fe029db112474d06613929b54c7.zip  | |
Invalidate existing paragraphs when new fonts are loaded
Diffstat (limited to '')
| -rw-r--r-- | graphics/src/renderer.rs | 23 | ||||
| -rw-r--r-- | graphics/src/text.rs | 44 | ||||
| -rw-r--r-- | graphics/src/text/paragraph.rs | 15 | 
3 files changed, 69 insertions, 13 deletions
diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index f93f4a6d..d4df29a5 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -162,6 +162,29 @@ where          text::Paragraph::with_text(text, self.backend.font_system())      } +    fn update_paragraph( +        &self, +        paragraph: &mut Self::Paragraph, +        text: Text<'_, Self::Font>, +    ) { +        let font_system = self.backend.font_system(); + +        if paragraph.version() != font_system.version() { +            // The font system has changed, paragraph fonts may be outdated +            *paragraph = self.create_paragraph(text); +        } else { +            match core::text::compare(paragraph, text) { +                core::text::Difference::None => {} +                core::text::Difference::Bounds => { +                    self.resize_paragraph(paragraph, text.bounds); +                } +                core::text::Difference::Shape => { +                    *paragraph = self.create_paragraph(text); +                } +            } +        } +    } +      fn resize_paragraph(          &self,          paragraph: &mut Self::Paragraph, diff --git a/graphics/src/text.rs b/graphics/src/text.rs index bbe9d7cb..bc06aa3c 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -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>, Version) { +        (self.raw.write().expect("Write font system"), self.version)      } -    pub fn write(&self) -> sync::RwLockWriteGuard<'_, cosmic_text::FontSystem> { -        self.0.write().expect("Write font system") +    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() diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs index ee7c04c8..cd12bc8f 100644 --- a/graphics/src/text/paragraph.rs +++ b/graphics/src/text/paragraph.rs @@ -19,6 +19,7 @@ struct Internal {      vertical_alignment: alignment::Vertical,      bounds: Size,      min_bounds: Size, +    version: text::Version,  }  impl Paragraph { @@ -27,9 +28,9 @@ impl Paragraph {      }      pub fn with_text(text: Text<'_, Font>, font_system: &FontSystem) -> Self { -        log::trace!("\nAllocating paragraph: {}", text.content); +        log::trace!("Allocating paragraph: {}", text.content); -        let mut font_system = font_system.write(); +        let (mut font_system, version) = font_system.write();          let mut buffer = cosmic_text::Buffer::new(              &mut font_system, @@ -63,6 +64,7 @@ impl Paragraph {              shaping: text.shaping,              bounds: text.bounds,              min_bounds, +            version,          })))      } @@ -70,6 +72,10 @@ impl Paragraph {          &self.internal().buffer      } +    pub fn version(&self) -> text::Version { +        self.internal().version +    } +      pub fn downgrade(&self) -> Weak {          let paragraph = self.internal(); @@ -89,8 +95,10 @@ impl Paragraph {          match Arc::try_unwrap(paragraph) {              Ok(mut internal) => { +                let (mut font_system, _) = font_system.write(); +                  internal.buffer.set_size( -                    &mut font_system.write(), +                    &mut font_system,                      new_bounds.width,                      new_bounds.height,                  ); @@ -246,6 +254,7 @@ impl Default for Internal {              vertical_alignment: alignment::Vertical::Top,              bounds: Size::ZERO,              min_bounds: Size::ZERO, +            version: text::Version::default(),          }      }  }  | 
