1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 08:05:07 +00:00

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
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()) if (parent_element && parent_element->computed_css_values())
font_metrics = parent_element->computed_css_values()->computed_font().pixel_metrics(); font_metrics = parent_element->computed_css_values()->computed_font().pixel_metrics();
else else
font_metrics = Gfx::FontDatabase::default_font().pixel_metrics(); font_metrics = Platform::FontPlugin::the().default_font().pixel_metrics();
auto parent_font_size = [&]() -> float { auto parent_font_size = [&]() -> float {
if (!parent_element || !parent_element->computed_css_values()) if (!parent_element || !parent_element->computed_css_values())

View file

@ -7,12 +7,12 @@
#include <AK/TypeCasts.h> #include <AK/TypeCasts.h>
#include <LibCore/DirIterator.h> #include <LibCore/DirIterator.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibWeb/CSS/Clip.h> #include <LibWeb/CSS/Clip.h>
#include <LibWeb/CSS/StyleProperties.h> #include <LibWeb/CSS/StyleProperties.h>
#include <LibWeb/FontCache.h> #include <LibWeb/FontCache.h>
#include <LibWeb/Layout/BlockContainer.h> #include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/Layout/Node.h> #include <LibWeb/Layout/Node.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::CSS { 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) NonnullRefPtr<Gfx::Font> StyleProperties::font_fallback(bool monospace, bool bold)
{ {
if (monospace && 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) if (monospace)
return Gfx::FontDatabase::default_fixed_width_font(); return Platform::FontPlugin::the().default_fixed_width_font();
if (bold) 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 float StyleProperties::line_height(Layout::Node const& layout_node) const

View file

@ -19,6 +19,7 @@
#include <LibWeb/HTML/TextMetrics.h> #include <LibWeb/HTML/TextMetrics.h>
#include <LibWeb/HTML/Window.h> #include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/TextNode.h> #include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::HTML { namespace Web::HTML {
@ -406,7 +407,7 @@ JS::NonnullGCPtr<TextMetrics> CanvasRenderingContext2D::measure_text(String cons
auto prepared_text = prepare_text(text); auto prepared_text = prepare_text(text);
auto metrics = TextMetrics::create(global_object()); auto metrics = TextMetrics::create(global_object());
// FIXME: Use the font that was used to create the glyphs in prepared_text. // 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.) // width attribute: The width of that inline box, in CSS pixels. (The text's advance width.)
metrics->set_width(prepared_text.bounding_box.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. // ...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: 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. // 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 width = 0;
size_t height = font.pixel_size(); size_t height = font.pixel_size();
for (auto c : Utf8View { replaced_text }) { for (auto c : Utf8View { replaced_text }) {

View file

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

View file

@ -5,7 +5,6 @@
*/ */
#include <AK/Demangle.h> #include <AK/Demangle.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/Dump.h> #include <LibWeb/Dump.h>
#include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/BrowsingContext.h>
@ -15,6 +14,7 @@
#include <LibWeb/Layout/InitialContainingBlock.h> #include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/Node.h> #include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/TextNode.h> #include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Platform/FontPlugin.h>
#include <typeinfo> #include <typeinfo>
namespace Web::Layout { namespace Web::Layout {
@ -198,7 +198,7 @@ NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::Comp
, m_computed_values(move(computed_values)) , m_computed_values(move(computed_values))
{ {
m_has_style = true; 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&) void NodeWithStyle::did_insert_into_layout_tree(CSS::StyleProperties const&)

View file

@ -9,6 +9,7 @@
#include <LibWeb/Layout/ImageBox.h> #include <LibWeb/Layout/ImageBox.h>
#include <LibWeb/Painting/BorderRadiusCornerClipper.h> #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
#include <LibWeb/Painting/ImagePaintable.h> #include <LibWeb/Painting/ImagePaintable.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::Painting { namespace Web::Painting {
@ -41,7 +42,7 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
if (phase == PaintPhase::Foreground) { if (phase == PaintPhase::Foreground) {
if (layout_box().renders_as_alt_text()) { if (layout_box().renders_as_alt_text()) {
auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node()); 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); 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(); auto alt = image_element.alt();
if (alt.is_empty()) if (alt.is_empty())

View file

@ -15,6 +15,7 @@
#include <LibWeb/Painting/PaintableBox.h> #include <LibWeb/Painting/PaintableBox.h>
#include <LibWeb/Painting/ShadowPainting.h> #include <LibWeb/Painting/ShadowPainting.h>
#include <LibWeb/Painting/StackingContext.h> #include <LibWeb/Painting/StackingContext.h>
#include <LibWeb/Platform/FontPlugin.h>
namespace Web::Painting { 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(border_rect, Color::Green);
paint_inspector_rect(content_rect, Color::Magenta); paint_inspector_rect(content_rect, Color::Magenta);
auto& font = Gfx::FontDatabase::default_font(); auto& font = Platform::FontPlugin::the().default_font();
StringBuilder builder; StringBuilder builder;
if (layout_box().dom_node()) if (layout_box().dom_node())

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/Forward.h> #include <AK/Forward.h>
#include <LibGfx/Forward.h>
namespace Web::Platform { namespace Web::Platform {
@ -30,6 +31,9 @@ public:
virtual ~FontPlugin(); virtual ~FontPlugin();
virtual Gfx::Font& default_font() = 0;
virtual Gfx::Font& default_fixed_width_font() = 0;
virtual String generic_font_name(GenericFont) = 0; virtual String generic_font_name(GenericFont) = 0;
}; };

View file

@ -16,6 +16,16 @@ FontPluginSerenity::FontPluginSerenity()
FontPluginSerenity::~FontPluginSerenity() = default; 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) String FontPluginSerenity::generic_font_name(Web::Platform::GenericFont generic_font)
{ {
// FIXME: Replace hard-coded font names with a relevant call to FontDatabase. // FIXME: Replace hard-coded font names with a relevant call to FontDatabase.

View file

@ -16,6 +16,8 @@ public:
FontPluginSerenity(); FontPluginSerenity();
virtual ~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; virtual String generic_font_name(Web::Platform::GenericFont) override;
}; };