Have TextBuffer own FontMatches

This commit is contained in:
Jeremy Soller 2022-10-25 11:10:44 -06:00
parent 1c341f3126
commit 0f446368ca
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
5 changed files with 42 additions and 49 deletions

View file

@ -42,9 +42,6 @@ lazy_static::lazy_static! {
static ref FONT_SYSTEM: FontSystem = FontSystem::new();
}
//TODO: find out how to do this!
static mut FONT_MATCHES: Option<FontMatches<'static>> = None;
static FONT_SIZES: &'static [TextMetrics] = &[
TextMetrics::new(10, 14), // Caption
TextMetrics::new(14, 20), // Body
@ -57,38 +54,6 @@ static FONT_SIZES: &'static [TextMetrics] = &[
fn main() -> cosmic::iced::Result {
env_logger::init();
let font_matches: FontMatches<'static> = FONT_SYSTEM.matches(|info| -> bool {
#[cfg(feature = "mono")]
let monospaced = true;
#[cfg(not(feature = "mono"))]
let monospaced = false;
let matched = {
info.style == fontdb::Style::Normal &&
info.weight == fontdb::Weight::NORMAL &&
info.stretch == fontdb::Stretch::Normal &&
(info.monospaced == monospaced || info.post_script_name.contains("Emoji"))
};
if matched {
log::debug!(
"{:?}: family '{}' postscript name '{}' style {:?} weight {:?} stretch {:?} monospaced {:?}",
info.id,
info.family,
info.post_script_name,
info.style,
info.weight,
info.stretch,
info.monospaced
);
}
matched
}).unwrap();
unsafe { FONT_MATCHES = Some(font_matches); }
let mut settings = settings();
settings.window.min_size = Some((400, 100));
Window::run(settings)
@ -134,9 +99,39 @@ impl Application for Window {
type Theme = Theme;
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let font_matches: FontMatches<'static> = FONT_SYSTEM.matches(|info| -> bool {
#[cfg(feature = "mono")]
let monospaced = true;
#[cfg(not(feature = "mono"))]
let monospaced = false;
let matched = {
info.style == fontdb::Style::Normal &&
info.weight == fontdb::Weight::NORMAL &&
info.stretch == fontdb::Stretch::Normal &&
(info.monospaced == monospaced || info.post_script_name.contains("Emoji"))
};
if matched {
log::debug!(
"{:?}: family '{}' postscript name '{}' style {:?} weight {:?} stretch {:?} monospaced {:?}",
info.id,
info.family,
info.post_script_name,
info.style,
info.weight,
info.stretch,
info.monospaced
);
}
matched
}).unwrap();
let font_size_i = 1; // Body
let buffer = TextBuffer::new(
unsafe { FONT_MATCHES.as_ref().unwrap() },
font_matches,
FONT_SIZES[font_size_i],
);