diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 3c28107..6123df8 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -23,7 +23,6 @@ use cosmic::{ }, }; use cosmic_text::{ - FontMatches, FontSystem, SwashCache, TextBuffer, @@ -40,7 +39,7 @@ use self::text_box::text_box; mod text_box; lazy_static::lazy_static! { - static ref FONT_SYSTEM: FontSystem = FontSystem::new(); + static ref FONT_SYSTEM: FontSystem<'static> = FontSystem::new(); } static FONT_SIZES: &'static [TextMetrics] = &[ @@ -102,11 +101,10 @@ impl Application for Window { fn new(_flags: ()) -> (Self, Command) { let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); - let font_matches = FONT_SYSTEM.matches_attrs(attrs).unwrap(); - let font_size_i = 1; // Body let buffer = TextBuffer::new( - font_matches, + &FONT_SYSTEM, + attrs, FONT_SIZES[font_size_i], ); diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index debcbe9..8dc8f8f 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -30,9 +30,6 @@ fn main() { ) .unwrap(); - let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); - let font_matches = font_system.matches_attrs(attrs).unwrap(); - let bg_color = Color::rgb(0x34, 0x34, 0x34); let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let font_sizes = [ @@ -59,8 +56,10 @@ fn main() { let mut swash_cache = SwashCache::new(); let line_x = 8 * display_scale; + let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); let mut buffer = TextBuffer::new( - font_matches, + &font_system, + attrs, font_sizes[font_size_i] ); buffer.set_size( diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 760ad16..a51d8c5 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -45,9 +45,6 @@ fn main() { ) .unwrap(); - let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); - let font_matches = font_system.matches_attrs(attrs).unwrap(); - let font_sizes = [ TextMetrics::new(10, 14).scale(display_scale), // Caption TextMetrics::new(14, 20).scale(display_scale), // Body @@ -58,8 +55,10 @@ fn main() { ]; let font_size_default = 1; // Body + let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); let mut buffer = TextBuffer::new( - font_matches, + &font_system, + attrs, font_sizes[font_size_default] ); buffer.set_size( diff --git a/src/buffer.rs b/src/buffer.rs index 8ca14ca..19eee75 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -7,7 +7,7 @@ use std::{ }; use unicode_segmentation::UnicodeSegmentation; -use crate::{LayoutGlyph, LayoutLine, FontMatches, FontShapeLine}; +use crate::{Attrs, LayoutGlyph, LayoutLine, FontMatches, FontShapeLine, FontSystem}; /// An action to perform on a [TextBuffer] #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -238,6 +238,7 @@ impl TextBufferLine { /// A buffer of text that is shaped and laid out pub struct TextBuffer<'a> { font_matches: FontMatches<'a>, + attrs: Attrs<'a>, lines: Vec, metrics: TextMetrics, width: i32, @@ -250,11 +251,14 @@ pub struct TextBuffer<'a> { impl<'a> TextBuffer<'a> { pub fn new( - font_matches: FontMatches<'a>, + font_system: &'a FontSystem<'a>, + attrs: Attrs<'a>, metrics: TextMetrics, ) -> Self { + let font_matches = font_system.matches_attrs(&attrs); let mut buffer = Self { font_matches, + attrs, lines: Vec::new(), metrics, width: 0, diff --git a/src/font/system.rs b/src/font/system.rs index ff2e652..24a674c 100644 --- a/src/font/system.rs +++ b/src/font/system.rs @@ -80,7 +80,7 @@ impl<'a> FontSystem<'a> { pub fn matches bool>( &'a self, f: F, - ) -> Option> { + ) -> FontMatches<'_> { let mut fonts = Vec::new(); for face in self.db.faces() { if !f(face) { @@ -93,17 +93,13 @@ impl<'a> FontSystem<'a> { } } - if !fonts.is_empty() { - Some(FontMatches { - locale: &self.locale, - fonts - }) - } else { - None + FontMatches { + locale: &self.locale, + fonts } } - pub fn matches_attrs(&'a self, attrs: Attrs) -> Option> { + pub fn matches_attrs(&'a self, attrs: &Attrs) -> FontMatches<'_> { self.matches(|face| { let matched = attrs.matches(face);