From e72896e35e8516b9f71df18ff33de88a09ee47e6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 17 Sep 2022 21:25:50 +0200 Subject: [PATCH] LibWeb: Get default fonts via Platform::FontPlugin Instead of asking Gfx::FontDatabase for the "default font" and the "default fixed-width font", we now proxy those requests out via the Platform::FontPlugin. This will allow Ladybird to use other default fonts as fallback. --- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 2 +- Userland/Libraries/LibWeb/CSS/StyleProperties.cpp | 10 +++++----- .../Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp | 5 +++-- Userland/Libraries/LibWeb/Layout/ImageBox.cpp | 4 ++-- Userland/Libraries/LibWeb/Layout/Node.cpp | 4 ++-- Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp | 3 ++- Userland/Libraries/LibWeb/Painting/PaintableBox.cpp | 3 ++- Userland/Libraries/LibWeb/Platform/FontPlugin.h | 4 ++++ Userland/Services/WebContent/FontPluginSerenity.cpp | 10 ++++++++++ Userland/Services/WebContent/FontPluginSerenity.h | 2 ++ 10 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 88ad0a8685b..e020376dafb 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -953,7 +953,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele if (parent_element && parent_element->computed_css_values()) font_metrics = parent_element->computed_css_values()->computed_font().pixel_metrics(); else - font_metrics = Gfx::FontDatabase::default_font().pixel_metrics(); + font_metrics = Platform::FontPlugin::the().default_font().pixel_metrics(); auto parent_font_size = [&]() -> float { if (!parent_element || !parent_element->computed_css_values()) diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index fc64695d40f..107f678f8a6 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -7,12 +7,12 @@ #include #include -#include #include #include #include #include #include +#include namespace Web::CSS { @@ -99,15 +99,15 @@ Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithSty NonnullRefPtr StyleProperties::font_fallback(bool monospace, bool bold) { if (monospace && bold) - return Gfx::FontDatabase::default_fixed_width_font().bold_variant(); + return Platform::FontPlugin::the().default_fixed_width_font().bold_variant(); if (monospace) - return Gfx::FontDatabase::default_fixed_width_font(); + return Platform::FontPlugin::the().default_fixed_width_font(); if (bold) - return Gfx::FontDatabase::default_font().bold_variant(); + return Platform::FontPlugin::the().default_font().bold_variant(); - return Gfx::FontDatabase::default_font(); + return Platform::FontPlugin::the().default_font(); } float StyleProperties::line_height(Layout::Node const& layout_node) const diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 8e9fc7a126a..cad1a07bf8d 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace Web::HTML { @@ -406,7 +407,7 @@ JS::NonnullGCPtr CanvasRenderingContext2D::measure_text(String cons auto prepared_text = prepare_text(text); auto metrics = TextMetrics::create(global_object()); // FIXME: Use the font that was used to create the glyphs in prepared_text. - auto& font = Gfx::FontDatabase::default_font(); + auto& font = Platform::FontPlugin::the().default_font(); // width attribute: The width of that inline box, in CSS pixels. (The text's advance width.) metrics->set_width(prepared_text.bounding_box.width()); @@ -477,7 +478,7 @@ CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(St // ...and with all other properties set to their initial values. // FIXME: Actually use a LineBox here instead of, you know, using the default font and measuring its size (which is not the spec at all). // FIXME: Once we have CanvasTextDrawingStyles, add the CSS attributes. - auto& font = Gfx::FontDatabase::default_font(); + auto& font = Platform::FontPlugin::the().default_font(); size_t width = 0; size_t height = font.pixel_size(); for (auto c : Utf8View { replaced_text }) { diff --git a/Userland/Libraries/LibWeb/Layout/ImageBox.cpp b/Userland/Libraries/LibWeb/Layout/ImageBox.cpp index a67843c96b0..d263d0a6a03 100644 --- a/Userland/Libraries/LibWeb/Layout/ImageBox.cpp +++ b/Userland/Libraries/LibWeb/Layout/ImageBox.cpp @@ -4,10 +4,10 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include +#include namespace Web::Layout { @@ -55,7 +55,7 @@ void ImageBox::prepare_for_replaced_layout() if (renders_as_alt_text()) { auto& image_element = verify_cast(dom_node()); - auto& font = Gfx::FontDatabase::default_font(); + auto& font = Platform::FontPlugin::the().default_font(); auto alt = image_element.alt(); if (alt.is_empty()) alt = image_element.src(); diff --git a/Userland/Libraries/LibWeb/Layout/Node.cpp b/Userland/Libraries/LibWeb/Layout/Node.cpp index 69058d61ceb..aa9e0b74790 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.cpp +++ b/Userland/Libraries/LibWeb/Layout/Node.cpp @@ -5,7 +5,6 @@ */ #include -#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include #include namespace Web::Layout { @@ -198,7 +198,7 @@ NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::Comp , m_computed_values(move(computed_values)) { m_has_style = true; - m_font = Gfx::FontDatabase::default_font(); + m_font = Platform::FontPlugin::the().default_font(); } void NodeWithStyle::did_insert_into_layout_tree(CSS::StyleProperties const&) diff --git a/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp b/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp index 9b3feb84db8..31572e28074 100644 --- a/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace Web::Painting { @@ -41,7 +42,7 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const if (phase == PaintPhase::Foreground) { if (layout_box().renders_as_alt_text()) { auto& image_element = verify_cast(*dom_node()); - context.painter().set_font(Gfx::FontDatabase::default_font()); + context.painter().set_font(Platform::FontPlugin::the().default_font()); Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2); auto alt = image_element.alt(); if (alt.is_empty()) diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index 58d6e397f74..63d7e2239ee 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace Web::Painting { @@ -158,7 +159,7 @@ void PaintableBox::paint(PaintContext& context, PaintPhase phase) const paint_inspector_rect(border_rect, Color::Green); paint_inspector_rect(content_rect, Color::Magenta); - auto& font = Gfx::FontDatabase::default_font(); + auto& font = Platform::FontPlugin::the().default_font(); StringBuilder builder; if (layout_box().dom_node()) diff --git a/Userland/Libraries/LibWeb/Platform/FontPlugin.h b/Userland/Libraries/LibWeb/Platform/FontPlugin.h index 0b4b160c8d8..e696e982e84 100644 --- a/Userland/Libraries/LibWeb/Platform/FontPlugin.h +++ b/Userland/Libraries/LibWeb/Platform/FontPlugin.h @@ -7,6 +7,7 @@ #pragma once #include +#include namespace Web::Platform { @@ -30,6 +31,9 @@ public: virtual ~FontPlugin(); + virtual Gfx::Font& default_font() = 0; + virtual Gfx::Font& default_fixed_width_font() = 0; + virtual String generic_font_name(GenericFont) = 0; }; diff --git a/Userland/Services/WebContent/FontPluginSerenity.cpp b/Userland/Services/WebContent/FontPluginSerenity.cpp index d965778cc02..b9377b88435 100644 --- a/Userland/Services/WebContent/FontPluginSerenity.cpp +++ b/Userland/Services/WebContent/FontPluginSerenity.cpp @@ -16,6 +16,16 @@ FontPluginSerenity::FontPluginSerenity() FontPluginSerenity::~FontPluginSerenity() = default; +Gfx::Font& FontPluginSerenity::default_font() +{ + return Gfx::FontDatabase::default_font(); +} + +Gfx::Font& FontPluginSerenity::default_fixed_width_font() +{ + return Gfx::FontDatabase::default_fixed_width_font(); +} + String FontPluginSerenity::generic_font_name(Web::Platform::GenericFont generic_font) { // FIXME: Replace hard-coded font names with a relevant call to FontDatabase. diff --git a/Userland/Services/WebContent/FontPluginSerenity.h b/Userland/Services/WebContent/FontPluginSerenity.h index af779d4f5f8..371b0833a66 100644 --- a/Userland/Services/WebContent/FontPluginSerenity.h +++ b/Userland/Services/WebContent/FontPluginSerenity.h @@ -16,6 +16,8 @@ public: FontPluginSerenity(); virtual ~FontPluginSerenity(); + virtual Gfx::Font& default_font() override; + virtual Gfx::Font& default_fixed_width_font() override; virtual String generic_font_name(Web::Platform::GenericFont) override; };