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.
This commit is contained in:
Andreas Kling 2022-09-17 21:25:50 +02:00
parent 07a0d9df30
commit e72896e35e
Notes: sideshowbarker 2024-07-17 06:53:30 +09:00
10 changed files with 33 additions and 14 deletions

View File

@ -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())

View File

@ -7,12 +7,12 @@
#include <AK/TypeCasts.h>
#include <LibCore/DirIterator.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibWeb/CSS/Clip.h>
#include <LibWeb/CSS/StyleProperties.h>
#include <LibWeb/FontCache.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::CSS {
@ -99,15 +99,15 @@ Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithSty
NonnullRefPtr<Gfx::Font> 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

View File

@ -19,6 +19,7 @@
#include <LibWeb/HTML/TextMetrics.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::HTML {
@ -406,7 +407,7 @@ JS::NonnullGCPtr<TextMetrics> 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 }) {

View File

@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Font/FontDatabase.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/Layout/ImageBox.h>
#include <LibWeb/Painting/ImagePaintable.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::Layout {
@ -55,7 +55,7 @@ void ImageBox::prepare_for_replaced_layout()
if (renders_as_alt_text()) {
auto& image_element = verify_cast<HTML::HTMLImageElement>(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();

View File

@ -5,7 +5,6 @@
*/
#include <AK/Demangle.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Dump.h>
#include <LibWeb/HTML/BrowsingContext.h>
@ -15,6 +14,7 @@
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Platform/FontPlugin.h>
#include <typeinfo>
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&)

View File

@ -9,6 +9,7 @@
#include <LibWeb/Layout/ImageBox.h>
#include <LibWeb/Painting/BorderRadiusCornerClipper.h>
#include <LibWeb/Painting/ImagePaintable.h>
#include <LibWeb/Platform/FontPlugin.h>
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<HTML::HTMLImageElement>(*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())

View File

@ -15,6 +15,7 @@
#include <LibWeb/Painting/PaintableBox.h>
#include <LibWeb/Painting/ShadowPainting.h>
#include <LibWeb/Painting/StackingContext.h>
#include <LibWeb/Platform/FontPlugin.h>
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())

View File

@ -7,6 +7,7 @@
#pragma once
#include <AK/Forward.h>
#include <LibGfx/Forward.h>
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;
};

View File

@ -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.

View File

@ -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;
};