Produce FontMatches inside of Buffer

This commit is contained in:
Jeremy Soller 2022-10-25 16:13:07 -06:00
parent 085231c153
commit 59a4f8a4c1
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
5 changed files with 20 additions and 24 deletions

View file

@ -23,7 +23,6 @@ use cosmic::{
}, },
}; };
use cosmic_text::{ use cosmic_text::{
FontMatches,
FontSystem, FontSystem,
SwashCache, SwashCache,
TextBuffer, TextBuffer,
@ -40,7 +39,7 @@ use self::text_box::text_box;
mod text_box; mod text_box;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref FONT_SYSTEM: FontSystem = FontSystem::new(); static ref FONT_SYSTEM: FontSystem<'static> = FontSystem::new();
} }
static FONT_SIZES: &'static [TextMetrics] = &[ static FONT_SIZES: &'static [TextMetrics] = &[
@ -102,11 +101,10 @@ impl Application for Window {
fn new(_flags: ()) -> (Self, Command<Self::Message>) { fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); 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 font_size_i = 1; // Body
let buffer = TextBuffer::new( let buffer = TextBuffer::new(
font_matches, &FONT_SYSTEM,
attrs,
FONT_SIZES[font_size_i], FONT_SIZES[font_size_i],
); );

View file

@ -30,9 +30,6 @@ fn main() {
) )
.unwrap(); .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 bg_color = Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let font_sizes = [ let font_sizes = [
@ -59,8 +56,10 @@ fn main() {
let mut swash_cache = SwashCache::new(); let mut swash_cache = SwashCache::new();
let line_x = 8 * display_scale; let line_x = 8 * display_scale;
let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono"));
let mut buffer = TextBuffer::new( let mut buffer = TextBuffer::new(
font_matches, &font_system,
attrs,
font_sizes[font_size_i] font_sizes[font_size_i]
); );
buffer.set_size( buffer.set_size(

View file

@ -45,9 +45,6 @@ fn main() {
) )
.unwrap(); .unwrap();
let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono"));
let font_matches = font_system.matches_attrs(attrs).unwrap();
let font_sizes = [ let font_sizes = [
TextMetrics::new(10, 14).scale(display_scale), // Caption TextMetrics::new(10, 14).scale(display_scale), // Caption
TextMetrics::new(14, 20).scale(display_scale), // Body TextMetrics::new(14, 20).scale(display_scale), // Body
@ -58,8 +55,10 @@ fn main() {
]; ];
let font_size_default = 1; // Body let font_size_default = 1; // Body
let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono"));
let mut buffer = TextBuffer::new( let mut buffer = TextBuffer::new(
font_matches, &font_system,
attrs,
font_sizes[font_size_default] font_sizes[font_size_default]
); );
buffer.set_size( buffer.set_size(

View file

@ -7,7 +7,7 @@ use std::{
}; };
use unicode_segmentation::UnicodeSegmentation; 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] /// An action to perform on a [TextBuffer]
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
@ -238,6 +238,7 @@ impl TextBufferLine {
/// A buffer of text that is shaped and laid out /// A buffer of text that is shaped and laid out
pub struct TextBuffer<'a> { pub struct TextBuffer<'a> {
font_matches: FontMatches<'a>, font_matches: FontMatches<'a>,
attrs: Attrs<'a>,
lines: Vec<TextBufferLine>, lines: Vec<TextBufferLine>,
metrics: TextMetrics, metrics: TextMetrics,
width: i32, width: i32,
@ -250,11 +251,14 @@ pub struct TextBuffer<'a> {
impl<'a> TextBuffer<'a> { impl<'a> TextBuffer<'a> {
pub fn new( pub fn new(
font_matches: FontMatches<'a>, font_system: &'a FontSystem<'a>,
attrs: Attrs<'a>,
metrics: TextMetrics, metrics: TextMetrics,
) -> Self { ) -> Self {
let font_matches = font_system.matches_attrs(&attrs);
let mut buffer = Self { let mut buffer = Self {
font_matches, font_matches,
attrs,
lines: Vec::new(), lines: Vec::new(),
metrics, metrics,
width: 0, width: 0,

View file

@ -80,7 +80,7 @@ impl<'a> FontSystem<'a> {
pub fn matches<F: Fn(&fontdb::FaceInfo) -> bool>( pub fn matches<F: Fn(&fontdb::FaceInfo) -> bool>(
&'a self, &'a self,
f: F, f: F,
) -> Option<FontMatches<'_>> { ) -> FontMatches<'_> {
let mut fonts = Vec::new(); let mut fonts = Vec::new();
for face in self.db.faces() { for face in self.db.faces() {
if !f(face) { if !f(face) {
@ -93,17 +93,13 @@ impl<'a> FontSystem<'a> {
} }
} }
if !fonts.is_empty() { FontMatches {
Some(FontMatches { locale: &self.locale,
locale: &self.locale, fonts
fonts
})
} else {
None
} }
} }
pub fn matches_attrs(&'a self, attrs: Attrs) -> Option<FontMatches<'_>> { pub fn matches_attrs(&'a self, attrs: &Attrs) -> FontMatches<'_> {
self.matches(|face| { self.matches(|face| {
let matched = attrs.matches(face); let matched = attrs.matches(face);