From c709aab7b4cbbed620d3140615a6a95177301645 Mon Sep 17 00:00:00 2001 From: Samuel Date: Thu, 6 Jun 2024 23:30:46 -0300 Subject: [PATCH] Better Ubuntu font workaround (#274) * Suppress some noisy tracing logs. * Better Ubuntu font workaround. --- crates/zng-app/src/lib.rs | 48 +++++++++----- crates/zng-ext-font/src/lib.rs | 34 +++------- crates/zng-ext-font/src/match_util.rs | 93 +++++++++++++++++++++++++++ crates/zng-ext-input/src/mouse.rs | 2 +- crates/zng-view/src/window.rs | 4 +- 5 files changed, 136 insertions(+), 45 deletions(-) create mode 100644 crates/zng-ext-font/src/match_util.rs diff --git a/crates/zng-app/src/lib.rs b/crates/zng-app/src/lib.rs index b05428759..795410a88 100644 --- a/crates/zng-app/src/lib.rs +++ b/crates/zng-app/src/lib.rs @@ -1415,6 +1415,8 @@ mod private { /// /// See also [`test_log`] to enable panicking for errors. /// +/// See also [`print_tracing_filter`] for the filter used by this. +/// /// [`tracing`]: https://docs.rs/tracing pub fn print_tracing(max: tracing::Level) -> bool { use tracing_subscriber::prelude::*; @@ -1429,7 +1431,7 @@ pub fn print_tracing(max: tracing::Level) -> bool { struct FilterLayer(tracing::Level); impl tracing_subscriber::Layer for FilterLayer { fn enabled(&self, metadata: &tracing::Metadata<'_>, _: tracing_subscriber::layer::Context<'_, S>) -> bool { - filter(&self.0, metadata) + print_tracing_filter(&self.0, metadata) } fn max_level_hint(&self) -> Option { @@ -1458,30 +1460,42 @@ impl tracing_subscriber::Layer for FilterLayer { } } } -fn filter(level: &tracing::Level, metadata: &tracing::Metadata) -> bool { +/// Filter used by [`print_tracing`], removes some log noise from dependencies. +pub fn print_tracing_filter(level: &tracing::Level, metadata: &tracing::Metadata) -> bool { if metadata.level() > level { return false; } - // suppress webrender warnings: - // - if metadata.target() == "webrender::device::gl" { - // Suppress "Cropping texture upload Box2D((0, 0), (0, 1)) to None" - // This happens when an empty frame is rendered. - if metadata.line() == Some(4661) { + if metadata.level() == &tracing::Level::INFO { + // suppress large info about texture cache + if metadata.target() == "zng_webrender::device::gl" { return false; } - } - - // suppress font-kit warnings: - // - if metadata.target() == "font_kit::loaders::freetype" { - // Suppress "$fn(): found invalid platform ID $n" - // This does not look fully implemented and generates a lot of warns - // with the default Ubuntu font set all with valid platform IDs. - if metadata.line() == Some(735) { + // suppress config dump + if metadata.target() == "zng_webrender::renderer::init" { return false; } + } else if metadata.level() == &tracing::Level::WARN { + // suppress webrender warnings: + // + if metadata.target() == "zng_webrender::device::gl" { + // Suppress "Cropping texture upload Box2D((0, 0), (0, 1)) to None" + // This happens when an empty frame is rendered. + if metadata.line() == Some(4661) { + return false; + } + } + + // suppress font-kit warnings: + // + if metadata.target() == "font_kit::loaders::freetype" { + // Suppress "$fn(): found invalid platform ID $n" + // This does not look fully implemented and generates a lot of warns + // with the default Ubuntu font set all with valid platform IDs. + if metadata.line() == Some(734) { + return false; + } + } } true diff --git a/crates/zng-ext-font/src/lib.rs b/crates/zng-ext-font/src/lib.rs index fa76ed263..bb9cb73e5 100644 --- a/crates/zng-ext-font/src/lib.rs +++ b/crates/zng-ext-font/src/lib.rs @@ -20,6 +20,8 @@ extern crate bitflags; pub mod font_features; +mod match_util; + mod emoji_util; pub use emoji_util::*; @@ -346,7 +348,7 @@ impl FontNames { if lang!("zh-Hans").matches(lang, true, false) { [ - "Ubuntu Regular", + "Ubuntu", "Droid Sans", "Source Han Sans SC", "Source Han Sans CN", @@ -357,7 +359,7 @@ impl FontNames { .into() } else if lang!("zh-Hant").matches(lang, true, false) { [ - "Ubuntu Regular", + "Ubuntu", "Droid Sans", "Source Han Sans TC", "Source Han Sans TW", @@ -369,7 +371,7 @@ impl FontNames { } else if lang!(ja).matches(lang, true, false) { [ "system-ui", - "Ubuntu Regular", + "Ubuntu", "Droid Sans", "Source Han Sans J", "Source Han Sans JP", @@ -381,7 +383,7 @@ impl FontNames { } else if lang!(ko).matches(lang, true, false) { [ "system-ui", - "Ubuntu Regular", + "Ubuntu", "Droid Sans", "Source Han Sans K", "Source Han Sans JR", @@ -393,7 +395,7 @@ impl FontNames { ] .into() } else { - ["system-ui", "Ubuntu Regular", "Droid Sans", "Noto Color Emoji", "sans-serif"].into() + ["system-ui", "Ubuntu", "Droid Sans", "Noto Color Emoji", "sans-serif"].into() } } @@ -1105,7 +1107,7 @@ impl FontFace { let metrics = font.metrics(); if metrics.units_per_em == 0 { // observed this in Noto Color Emoji - tracing::error!("font {:?} units_per_em 0", font.family_name()); + tracing::debug!("font {:?} units_per_em 0", font.family_name()); return Err(FontLoadingError::UnknownFormat); } @@ -2011,25 +2013,7 @@ impl FontFaceLoader { fn get_system(font_name: &FontName, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option { let _span = tracing::trace_span!("FontFaceLoader::get_system").entered(); - let family_name = font_kit::family_name::FamilyName::from(font_name.clone()); - match font_kit::source::SystemSource::new().select_best_match( - &[family_name], - &font_kit::properties::Properties { - style: style.into(), - weight: weight.into(), - stretch: stretch.into(), - }, - ) { - Ok(handle) => Some(handle), - Err(font_kit::error::SelectionError::NotFound) => { - tracing::debug!(target: "font_loading", "system font not found\nquery: {:?}", (font_name, style, weight, stretch)); - None - } - Err(e) => { - tracing::error!(target: "font_loading", "failed to select system font, {e}\nquery: {:?}", (font_name, style, weight, stretch)); - None - } - } + match_util::best(font_name, style, weight, stretch) } fn match_custom(faces: &[FontFace], style: FontStyle, weight: FontWeight, stretch: FontStretch) -> FontFace { diff --git a/crates/zng-ext-font/src/match_util.rs b/crates/zng-ext-font/src/match_util.rs new file mode 100644 index 000000000..e9f5712e0 --- /dev/null +++ b/crates/zng-ext-font/src/match_util.rs @@ -0,0 +1,93 @@ +use crate::{FontName, FontStretch, FontStyle, FontWeight}; + +pub fn best(font_name: &FontName, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option { + if font_name == "Ubuntu" { + if let Some(h) = workaround_ubuntu(style, weight, stretch) { + return Some(h); + } + } + + let family_name = font_kit::family_name::FamilyName::from(font_name.clone()); + match font_kit::source::SystemSource::new().select_best_match( + &[family_name], + &font_kit::properties::Properties { + style: style.into(), + weight: weight.into(), + stretch: stretch.into(), + }, + ) { + Ok(handle) => Some(handle), + Err(font_kit::error::SelectionError::NotFound) => { + tracing::debug!(target: "font_loading", "system font not found\nquery: {:?}", (font_name, style, weight, stretch)); + None + } + Err(e) => { + tracing::error!(target: "font_loading", "failed to select system font, {e}\nquery: {:?}", (font_name, style, weight, stretch)); + None + } + } +} + +// see https://github.com/servo/font-kit/issues/245 +fn workaround_ubuntu(style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option { + let source = font_kit::source::SystemSource::new(); + let ubuntu = source.select_family_by_name("Ubuntu").ok()?; + for handle in ubuntu.fonts() { + let font = handle.load().ok()?; + let name = font.postscript_name()?; + + // Ubuntu-ExtraBold + // Ubuntu-Condensed + // Ubuntu-CondensedLight + // Ubuntu-CondensedBold + // Ubuntu-CondensedMedium + // Ubuntu-CondensedExtraBold + // UbuntuItalic-CondensedLightItalic + // UbuntuItalic-CondensedItalic + // UbuntuItalic-CondensedMediumItalic + // UbuntuItalic-CondensedBoldItalic + // UbuntuItalic-CondensedExtraBoldItalic + // Ubuntu-Italic + // UbuntuItalic-ThinItalic + // UbuntuItalic-LightItalic + // UbuntuItalic-Italic + // UbuntuItalic-MediumItalic + // UbuntuItalic-BoldItalic + // UbuntuItalic-ExtraBoldItalic + // UbuntuItalic-CondensedThinItalic + // Ubuntu-Thin + // Ubuntu-Regular + // Ubuntu-Light + // Ubuntu-Bold + // Ubuntu-Medium + // Ubuntu-CondensedThin + + if (style == FontStyle::Italic) != name.contains("Italic") { + continue; + } + + if (FontWeight::MEDIUM..FontWeight::SEMIBOLD).contains(&weight) != name.contains("Medium") { + continue; + } + if (weight >= FontWeight::EXTRA_BOLD) != name.contains("ExtraBold") { + continue; + } + if (FontWeight::SEMIBOLD..FontWeight::EXTRA_BOLD).contains(&weight) != name.contains("Bold") { + continue; + } + + if (FontWeight::EXTRA_LIGHT..FontWeight::LIGHT).contains(&weight) != name.contains("Light") { + continue; + } + if (weight < FontWeight::EXTRA_LIGHT) != name.contains("Thin") { + continue; + } + + if (stretch <= FontStretch::CONDENSED) != name.contains("Condensed") { + continue; + } + + return Some(handle.clone()); + } + None +} diff --git a/crates/zng-ext-input/src/mouse.rs b/crates/zng-ext-input/src/mouse.rs index fe5c7e776..259dffaba 100644 --- a/crates/zng-ext-input/src/mouse.rs +++ b/crates/zng-ext-input/src/mouse.rs @@ -995,7 +995,7 @@ impl MouseManager { MOUSE_HOVERED_EVENT.notify(args); } } else if coalesced_pos.is_empty() { - tracing::warn!("RawCursorMoved did not actually move") + tracing::debug!("RawCursorMoved did not actually move") } } diff --git a/crates/zng-view/src/window.rs b/crates/zng-view/src/window.rs index ee4c22447..02eea0ad5 100644 --- a/crates/zng-view/src/window.rs +++ b/crates/zng-view/src/window.rs @@ -1114,7 +1114,7 @@ impl Window { return; } self.taskbar_visible = visible; - tracing::error!("`set_taskbar_visible` not implemented for {}", std::env::consts::OS); + tracing::warn!("`set_taskbar_visible` not implemented for {}", std::env::consts::OS); } #[cfg(windows)] @@ -1923,7 +1923,7 @@ impl Window { #[cfg(not(windows))] pub(crate) fn set_system_shutdown_warn(&mut self, reason: Txt) { if !reason.is_empty() { - tracing::error!("cannot set system shutdown warn, not implemented, requested warn reason was: {reason}"); + tracing::warn!("system shutdown warn not implemented on {}", std::env::consts::OS); } } }