diff options
author | Simon Danner <danners@users.noreply.github.com> | 2021-04-22 20:47:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-22 20:47:47 +0200 |
commit | c8d56ee4f49c7d7d0946813bb17def6ea2e8b42b (patch) | |
tree | 9102e9af2235a0c012096467c22f41f872e004e2 /Userland/Libraries | |
parent | 7ab8be9e0bcecdfb5e994322a4958b0d11ff10f6 (diff) | |
download | serenity-c8d56ee4f49c7d7d0946813bb17def6ea2e8b42b.zip |
LibWeb: Improve fallback font selection
Try to find a font that has at least some of the requested properties.
This makes e.g. rfcs on tools.ietf.org easier to read since their
headers are now bold.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleProperties.cpp | 27 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleProperties.h | 1 |
2 files changed, 25 insertions, 3 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index 09db03d658..739ace583e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -92,10 +92,15 @@ void StyleProperties::load_font() const auto family_parts = family_value.split(','); auto family = family_parts[0]; - if (family.is_one_of("monospace", "ui-monospace")) + auto monospace = false; + auto bold = false; + + if (family.is_one_of("monospace", "ui-monospace")) { + monospace = true; family = "Csilla"; - else if (family.is_one_of("serif", "sans-serif", "cursive", "fantasy", "ui-serif", "ui-sans-serif", "ui-rounded")) + } else if (family.is_one_of("serif", "sans-serif", "cursive", "fantasy", "ui-serif", "ui-sans-serif", "ui-rounded")) { family = "Katica"; + } int weight = 400; if (font_weight->is_identifier()) { @@ -127,6 +132,8 @@ void StyleProperties::load_font() const weight = 900; } + bold = weight > 400; + int size = 10; if (font_size->is_identifier()) { switch (static_cast<const IdentifierStyleValue&>(*font_size).id()) { @@ -177,13 +184,27 @@ void StyleProperties::load_font() const if (!found_font) { dbgln("Font not found: '{}' {} {}", family, size, weight); - found_font = Gfx::FontDatabase::default_font(); + found_font = font_fallback(monospace, bold); } m_font = found_font; FontCache::the().set(font_selector, *m_font); } +RefPtr<Gfx::Font> StyleProperties::font_fallback(bool monospace, bool bold) const +{ + if (monospace && bold) + return Gfx::FontDatabase::default_bold_fixed_width_font(); + + if (monospace) + return Gfx::FontDatabase::default_fixed_width_font(); + + if (bold) + return Gfx::FontDatabase::default_bold_font(); + + return Gfx::FontDatabase::default_font(); +} + float StyleProperties::line_height(const Layout::Node& layout_node) const { auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, Length::make_auto()); diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.h b/Userland/Libraries/LibWeb/CSS/StyleProperties.h index 2971eea526..1b493d5063 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.h +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.h @@ -76,6 +76,7 @@ private: Optional<CSS::Overflow> overflow(CSS::PropertyID) const; void load_font() const; + RefPtr<Gfx::Font> font_fallback(bool monospace, bool bold) const; mutable RefPtr<Gfx::Font> m_font; }; |