Skip to content

Commit

Permalink
Better Ubuntu font workaround (#274)
Browse files Browse the repository at this point in the history
* Suppress some noisy tracing logs.

* Better Ubuntu font workaround.
  • Loading branch information
SamRodri authored Jun 7, 2024
1 parent 4f25fc1 commit c709aab
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 45 deletions.
48 changes: 31 additions & 17 deletions crates/zng-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -1429,7 +1431,7 @@ pub fn print_tracing(max: tracing::Level) -> bool {
struct FilterLayer(tracing::Level);
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> 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<tracing::metadata::LevelFilter> {
Expand Down Expand Up @@ -1458,30 +1460,42 @@ impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> 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
Expand Down
34 changes: 9 additions & 25 deletions crates/zng-ext-font/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extern crate bitflags;

pub mod font_features;

mod match_util;

mod emoji_util;
pub use emoji_util::*;

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -2011,25 +2013,7 @@ impl FontFaceLoader {

fn get_system(font_name: &FontName, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option<font_kit::handle::Handle> {
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 {
Expand Down
93 changes: 93 additions & 0 deletions crates/zng-ext-font/src/match_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use crate::{FontName, FontStretch, FontStyle, FontWeight};

pub fn best(font_name: &FontName, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option<font_kit::handle::Handle> {
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<font_kit::handle::Handle> {
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
}
2 changes: 1 addition & 1 deletion crates/zng-ext-input/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/zng-view/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit c709aab

Please sign in to comment.