LibWeb: Remove on-demand font resolution

Fonts are now resolved as part of the CSS cascade.
This commit is contained in:
Andreas Kling 2021-09-23 19:51:45 +02:00
parent 1ca33598da
commit 785ace4fc2
Notes: sideshowbarker 2024-07-18 03:28:46 +09:00
4 changed files with 3 additions and 166 deletions

View file

@ -85,159 +85,6 @@ Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithSty
return value.value()->to_color(node);
}
void StyleProperties::load_font(Layout::Node const& node) const
{
auto font_size = property(CSS::PropertyID::FontSize).value_or(IdentifierStyleValue::create(CSS::ValueID::Medium));
auto font_weight = property(CSS::PropertyID::FontWeight).value_or(IdentifierStyleValue::create(CSS::ValueID::Normal));
int weight = Gfx::FontWeight::Regular;
if (font_weight->has_identifier()) {
switch (font_weight->to_identifier()) {
case CSS::ValueID::Normal:
weight = Gfx::FontWeight::Regular;
break;
case CSS::ValueID::Bold:
weight = Gfx::FontWeight::Bold;
break;
case CSS::ValueID::Lighter:
// FIXME: This should be relative to the parent.
weight = Gfx::FontWeight::Regular;
break;
case CSS::ValueID::Bolder:
// FIXME: This should be relative to the parent.
weight = Gfx::FontWeight::Bold;
break;
default:
break;
}
} else if (font_weight->is_numeric()) {
int font_weight_integer = font_weight->as_numeric().int_value();
if (font_weight_integer <= Gfx::FontWeight::Regular)
weight = Gfx::FontWeight::Regular;
else if (font_weight_integer <= Gfx::FontWeight::Bold)
weight = Gfx::FontWeight::Bold;
else
weight = Gfx::FontWeight::Black;
}
// FIXME: calc() for font-weight
bool bold = weight > Gfx::FontWeight::Regular;
int size = 10;
auto parent_font_size = node.parent() == nullptr ? size : node.parent()->font_size();
constexpr float font_size_ratio = 1.2f;
if (font_size->has_identifier()) {
switch (font_size->to_identifier()) {
case CSS::ValueID::XxSmall:
case CSS::ValueID::XSmall:
case CSS::ValueID::Small:
case CSS::ValueID::Medium:
// FIXME: Should be based on "user's default font size"
size = 10;
break;
case CSS::ValueID::Large:
case CSS::ValueID::XLarge:
case CSS::ValueID::XxLarge:
case CSS::ValueID::XxxLarge:
// FIXME: Should be based on "user's default font size"
size = 12;
break;
case CSS::ValueID::Smaller:
size = roundf(parent_font_size / font_size_ratio);
break;
case CSS::ValueID::Larger:
size = roundf(parent_font_size * font_size_ratio);
break;
default:
break;
}
} else {
Optional<Length> maybe_length;
if (font_size->is_length()) {
maybe_length = font_size->to_length();
} else if (font_size->is_calculated()) {
Length length = Length(0, Length::Type::Calculated);
length.set_calculated_style(&font_size->as_calculated());
maybe_length = length;
}
if (maybe_length.has_value()) {
// FIXME: em sizes return 0 here, for some reason
auto calculated_size = maybe_length.value().resolved_or_zero(node, parent_font_size).to_px(node);
if (calculated_size != 0)
size = calculated_size;
}
}
// FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
// Note: This is modified by the find_font() lambda
FontSelector font_selector;
bool monospace = false;
auto find_font = [&](String const& family) -> RefPtr<Gfx::Font> {
font_selector = { family, size, weight };
if (auto found_font = FontCache::the().get(font_selector))
return found_font;
if (auto found_font = Gfx::FontDatabase::the().get(family, size, weight))
return found_font;
return {};
};
// FIXME: Replace hard-coded font names with a relevant call to FontDatabase.
// Currently, we cannot request the default font's name, or request it at a specific size and weight.
// So, hard-coded font names it is.
auto find_generic_font = [&](ValueID font_id) -> RefPtr<Gfx::Font> {
switch (font_id) {
case ValueID::Monospace:
case ValueID::UiMonospace:
monospace = true;
return find_font("Csilla");
case ValueID::Serif:
case ValueID::SansSerif:
case ValueID::Cursive:
case ValueID::Fantasy:
case ValueID::UiSerif:
case ValueID::UiSansSerif:
case ValueID::UiRounded:
return find_font("Katica");
default:
return {};
}
};
RefPtr<Gfx::Font> found_font {};
auto family_value = property(PropertyID::FontFamily).value_or(StringStyleValue::create("Katica"));
if (family_value->is_value_list()) {
auto& family_list = family_value->as_value_list().values();
for (auto& family : family_list) {
if (family.is_identifier()) {
found_font = find_generic_font(family.to_identifier());
} else if (family.is_string()) {
found_font = find_font(family.to_string());
}
if (found_font)
break;
}
} else if (family_value->is_identifier()) {
found_font = find_generic_font(family_value->to_identifier());
} else if (family_value->is_string()) {
found_font = find_font(family_value->to_string());
}
if (!found_font) {
found_font = font_fallback(monospace, bold);
}
m_font = move(found_font);
FontCache::the().set(font_selector, *m_font);
}
NonnullRefPtr<Gfx::Font> StyleProperties::font_fallback(bool monospace, bool bold)
{
if (monospace && bold)
@ -257,7 +104,7 @@ float StyleProperties::line_height(const Layout::Node& layout_node) const
auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, Length::make_auto());
if (line_height_length.is_absolute())
return (float)line_height_length.to_px(layout_node);
return (float)font(layout_node).glyph_height() * 1.4f;
return (float)computed_font().glyph_height() * 1.4f;
}
Optional<int> StyleProperties::z_index() const

View file

@ -69,13 +69,6 @@ public:
Vector<CSS::Transformation> transformations() const;
const Gfx::Font& font(Layout::Node const& node) const
{
if (!m_font)
load_font(node);
return *m_font;
}
Gfx::Font const& computed_font() const
{
VERIFY(m_font);
@ -103,8 +96,6 @@ private:
HashMap<CSS::PropertyID, NonnullRefPtr<StyleValue>> m_property_values;
Optional<CSS::Overflow> overflow(CSS::PropertyID) const;
void load_font(Layout::Node const&) const;
mutable RefPtr<Gfx::Font> m_font;
};

View file

@ -704,9 +704,8 @@ void StyleResolver::compute_font(StyleProperties& style, DOM::Element const* ele
break;
}
} else {
// FIXME: Get the root element font.
float root_font_size = 10;
if (element->document().document_element() != element)
root_font_size = element->document().document_element()->layout_node()->font().presentation_size();
Gfx::FontMetrics font_metrics;
if (element && element->parent_element())

View file

@ -205,7 +205,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
{
auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
m_font = specified_style.font(*this);
m_font = specified_style.computed_font();
m_line_height = specified_style.line_height(*this);
{