summaryrefslogtreecommitdiffstats
path: root/graphics/src/text.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-11 02:47:24 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-11 02:47:24 +0200
commit346af3f8b0baa418fd37b878bc2930ff0bd57cc0 (patch)
treeb11cde03595fb28bddb055507d272dd973634ab5 /graphics/src/text.rs
parent9245423c5d82f88c99adecaaf5dd2ac3559a05a8 (diff)
downloadiced-346af3f8b0baa418fd37b878bc2930ff0bd57cc0.tar.gz
iced-346af3f8b0baa418fd37b878bc2930ff0bd57cc0.tar.bz2
iced-346af3f8b0baa418fd37b878bc2930ff0bd57cc0.zip
Make `FontSystem` global and simplify `Paragraph` API
Diffstat (limited to '')
-rw-r--r--graphics/src/text.rs47
1 files changed, 20 insertions, 27 deletions
diff --git a/graphics/src/text.rs b/graphics/src/text.rs
index bc06aa3c..f5ccaf52 100644
--- a/graphics/src/text.rs
+++ b/graphics/src/text.rs
@@ -10,40 +10,39 @@ use crate::core::font::{self, Font};
use crate::core::text::Shaping;
use crate::core::Size;
+use once_cell::sync::OnceCell;
use std::borrow::Cow;
-use std::sync::{self, Arc, RwLock};
+use std::sync::{Arc, RwLock};
-#[allow(missing_debug_implementations)]
-pub struct FontSystem {
- raw: RwLock<cosmic_text::FontSystem>,
- version: Version,
-}
+pub fn font_system() -> &'static RwLock<FontSystem> {
+ static FONT_SYSTEM: OnceCell<RwLock<FontSystem>> = OnceCell::new();
-impl FontSystem {
- pub fn new() -> Self {
- FontSystem {
- raw: RwLock::new(cosmic_text::FontSystem::new_with_fonts(
+ FONT_SYSTEM.get_or_init(|| {
+ RwLock::new(FontSystem {
+ raw: 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.raw.get_mut().expect("Lock font system")
- }
+#[allow(missing_debug_implementations)]
+pub struct FontSystem {
+ raw: cosmic_text::FontSystem,
+ version: Version,
+}
- pub fn write(
- &self,
- ) -> (sync::RwLockWriteGuard<'_, cosmic_text::FontSystem>, Version) {
- (self.raw.write().expect("Write font system"), self.version)
+impl FontSystem {
+ pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
+ &mut self.raw
}
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
- let _ = self.get_mut().db_mut().load_font_source(
+ let _ = self.raw.db_mut().load_font_source(
cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
);
@@ -58,12 +57,6 @@ impl FontSystem {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Version(u32);
-impl Default for FontSystem {
- fn default() -> Self {
- Self::new()
- }
-}
-
pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
let (width, total_lines) = buffer
.layout_runs()