Invalidate existing paragraphs when new fonts are loaded
This commit is contained in:
parent
837529bc99
commit
3450987355
6 changed files with 127 additions and 33 deletions
|
|
@ -172,18 +172,14 @@ pub trait Renderer: crate::Renderer {
|
||||||
paragraph: &mut Self::Paragraph,
|
paragraph: &mut Self::Paragraph,
|
||||||
text: Text<'_, Self::Font>,
|
text: Text<'_, Self::Font>,
|
||||||
) {
|
) {
|
||||||
if paragraph.content() != text.content
|
match compare(paragraph, text) {
|
||||||
|| paragraph.text_size() != text.size
|
Difference::None => {}
|
||||||
|| paragraph.line_height().to_absolute(text.size)
|
Difference::Bounds => {
|
||||||
!= text.line_height.to_absolute(text.size)
|
self.resize_paragraph(paragraph, text.bounds);
|
||||||
|| paragraph.font() != text.font
|
}
|
||||||
|| paragraph.shaping() != text.shaping
|
Difference::Shape => {
|
||||||
|| paragraph.horizontal_alignment() != text.horizontal_alignment
|
*paragraph = self.create_paragraph(text);
|
||||||
|| paragraph.vertical_alignment() != text.vertical_alignment
|
}
|
||||||
{
|
|
||||||
*paragraph = self.create_paragraph(text);
|
|
||||||
} else if paragraph.bounds() != text.bounds {
|
|
||||||
self.resize_paragraph(paragraph, text.bounds);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -255,3 +251,51 @@ pub trait Paragraph: Default {
|
||||||
self.min_bounds().height
|
self.min_bounds().height
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The difference detected in some text.
|
||||||
|
///
|
||||||
|
/// You will obtain a [`Difference`] when you [`compare`] a [`Paragraph`] with some
|
||||||
|
/// [`Text`].
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Difference {
|
||||||
|
/// No difference.
|
||||||
|
///
|
||||||
|
/// The text can be reused as it is!
|
||||||
|
None,
|
||||||
|
|
||||||
|
/// A bounds difference.
|
||||||
|
///
|
||||||
|
/// This normally means a relayout is necessary, but the shape of the text can
|
||||||
|
/// be reused.
|
||||||
|
Bounds,
|
||||||
|
|
||||||
|
/// A shape difference.
|
||||||
|
///
|
||||||
|
/// The contents, alignment, sizes, fonts, or any other essential attributes
|
||||||
|
/// of the shape of the text have changed. A complete reshape and relayout of
|
||||||
|
/// the text is necessary.
|
||||||
|
Shape,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compares a [`Paragraph`] with some desired [`Text`] and returns the
|
||||||
|
/// [`Difference`].
|
||||||
|
pub fn compare<Font: PartialEq>(
|
||||||
|
paragraph: &impl Paragraph<Font = Font>,
|
||||||
|
text: Text<'_, Font>,
|
||||||
|
) -> Difference {
|
||||||
|
if paragraph.content() != text.content
|
||||||
|
|| paragraph.text_size() != text.size
|
||||||
|
|| paragraph.line_height().to_absolute(text.size)
|
||||||
|
!= text.line_height.to_absolute(text.size)
|
||||||
|
|| paragraph.font() != text.font
|
||||||
|
|| paragraph.shaping() != text.shaping
|
||||||
|
|| paragraph.horizontal_alignment() != text.horizontal_alignment
|
||||||
|
|| paragraph.vertical_alignment() != text.vertical_alignment
|
||||||
|
{
|
||||||
|
Difference::Shape
|
||||||
|
} else if paragraph.bounds() != text.bounds {
|
||||||
|
Difference::Bounds
|
||||||
|
} else {
|
||||||
|
Difference::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -162,6 +162,29 @@ where
|
||||||
text::Paragraph::with_text(text, self.backend.font_system())
|
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(
|
fn resize_paragraph(
|
||||||
&self,
|
&self,
|
||||||
paragraph: &mut Self::Paragraph,
|
paragraph: &mut Self::Paragraph,
|
||||||
|
|
|
||||||
|
|
@ -10,30 +10,54 @@ use crate::core::font::{self, Font};
|
||||||
use crate::core::text::Shaping;
|
use crate::core::text::Shaping;
|
||||||
use crate::core::Size;
|
use crate::core::Size;
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::sync::{self, Arc, RwLock};
|
use std::sync::{self, Arc, RwLock};
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct FontSystem(RwLock<cosmic_text::FontSystem>);
|
pub struct FontSystem {
|
||||||
|
raw: RwLock<cosmic_text::FontSystem>,
|
||||||
|
version: Version,
|
||||||
|
}
|
||||||
|
|
||||||
impl FontSystem {
|
impl FontSystem {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
FontSystem(RwLock::new(cosmic_text::FontSystem::new_with_fonts(
|
FontSystem {
|
||||||
[cosmic_text::fontdb::Source::Binary(Arc::new(
|
raw: RwLock::new(cosmic_text::FontSystem::new_with_fonts(
|
||||||
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
|
[cosmic_text::fontdb::Source::Binary(Arc::new(
|
||||||
))]
|
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
|
||||||
.into_iter(),
|
))]
|
||||||
)))
|
.into_iter(),
|
||||||
|
)),
|
||||||
|
version: Version::default(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut(&mut self) -> &mut cosmic_text::FontSystem {
|
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> {
|
pub fn write(
|
||||||
self.0.write().expect("Write font system")
|
&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 {
|
impl Default for FontSystem {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ struct Internal {
|
||||||
vertical_alignment: alignment::Vertical,
|
vertical_alignment: alignment::Vertical,
|
||||||
bounds: Size,
|
bounds: Size,
|
||||||
min_bounds: Size,
|
min_bounds: Size,
|
||||||
|
version: text::Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Paragraph {
|
impl Paragraph {
|
||||||
|
|
@ -27,9 +28,9 @@ impl Paragraph {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_text(text: Text<'_, Font>, font_system: &FontSystem) -> Self {
|
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(
|
let mut buffer = cosmic_text::Buffer::new(
|
||||||
&mut font_system,
|
&mut font_system,
|
||||||
|
|
@ -63,6 +64,7 @@ impl Paragraph {
|
||||||
shaping: text.shaping,
|
shaping: text.shaping,
|
||||||
bounds: text.bounds,
|
bounds: text.bounds,
|
||||||
min_bounds,
|
min_bounds,
|
||||||
|
version,
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,6 +72,10 @@ impl Paragraph {
|
||||||
&self.internal().buffer
|
&self.internal().buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn version(&self) -> text::Version {
|
||||||
|
self.internal().version
|
||||||
|
}
|
||||||
|
|
||||||
pub fn downgrade(&self) -> Weak {
|
pub fn downgrade(&self) -> Weak {
|
||||||
let paragraph = self.internal();
|
let paragraph = self.internal();
|
||||||
|
|
||||||
|
|
@ -89,8 +95,10 @@ impl Paragraph {
|
||||||
|
|
||||||
match Arc::try_unwrap(paragraph) {
|
match Arc::try_unwrap(paragraph) {
|
||||||
Ok(mut internal) => {
|
Ok(mut internal) => {
|
||||||
|
let (mut font_system, _) = font_system.write();
|
||||||
|
|
||||||
internal.buffer.set_size(
|
internal.buffer.set_size(
|
||||||
&mut font_system.write(),
|
&mut font_system,
|
||||||
new_bounds.width,
|
new_bounds.width,
|
||||||
new_bounds.height,
|
new_bounds.height,
|
||||||
);
|
);
|
||||||
|
|
@ -246,6 +254,7 @@ impl Default for Internal {
|
||||||
vertical_alignment: alignment::Vertical::Top,
|
vertical_alignment: alignment::Vertical::Top,
|
||||||
bounds: Size::ZERO,
|
bounds: Size::ZERO,
|
||||||
min_bounds: Size::ZERO,
|
min_bounds: Size::ZERO,
|
||||||
|
version: text::Version::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::hash_map;
|
use std::collections::hash_map;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
|
|
@ -32,9 +31,7 @@ impl Pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
|
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
|
||||||
self.font_system.get_mut().db_mut().load_font_source(
|
self.font_system.load_font(bytes);
|
||||||
cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
|
|
||||||
);
|
|
||||||
|
|
||||||
self.cache = RefCell::new(Cache::new());
|
self.cache = RefCell::new(Cache::new());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ use crate::layer::Text;
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
|
|
@ -47,9 +46,7 @@ impl Pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
|
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
|
||||||
let _ = self.font_system.get_mut().db_mut().load_font_source(
|
self.font_system.load_font(bytes);
|
||||||
glyphon::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
|
|
||||||
);
|
|
||||||
|
|
||||||
self.cache = RefCell::new(Cache::new());
|
self.cache = RefCell::new(Cache::new());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue