mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +00:00
LibWeb+WebContent: Add abstraction layer for generic font families
Instead of hard-coding the names of system fonts to use for the CSS generic fonts (like "sans-serif", "monospace", etc.) we now call out to a Platform::FontPlugin and ask for the generic names.
This commit is contained in:
parent
26544f559c
commit
5c2e3d1637
8 changed files with 153 additions and 8 deletions
|
@ -352,6 +352,7 @@ set(SOURCES
|
||||||
Painting/StackingContext.cpp
|
Painting/StackingContext.cpp
|
||||||
Painting/TextPaintable.cpp
|
Painting/TextPaintable.cpp
|
||||||
Platform/EventLoopPlugin.cpp
|
Platform/EventLoopPlugin.cpp
|
||||||
|
Platform/FontPlugin.cpp
|
||||||
Platform/Timer.cpp
|
Platform/Timer.cpp
|
||||||
RequestIdleCallback/IdleDeadline.cpp
|
RequestIdleCallback/IdleDeadline.cpp
|
||||||
ResizeObserver/ResizeObserver.cpp
|
ResizeObserver/ResizeObserver.cpp
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include <LibWeb/FontCache.h>
|
#include <LibWeb/FontCache.h>
|
||||||
#include <LibWeb/HTML/HTMLHtmlElement.h>
|
#include <LibWeb/HTML/HTMLHtmlElement.h>
|
||||||
#include <LibWeb/Loader/ResourceLoader.h>
|
#include <LibWeb/Loader/ResourceLoader.h>
|
||||||
|
#include <LibWeb/Platform/FontPlugin.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
namespace Web::CSS {
|
namespace Web::CSS {
|
||||||
|
@ -1029,28 +1030,39 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
|
||||||
return {};
|
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> {
|
auto find_generic_font = [&](ValueID font_id) -> RefPtr<Gfx::Font> {
|
||||||
|
Platform::GenericFont generic_font {};
|
||||||
switch (font_id) {
|
switch (font_id) {
|
||||||
case ValueID::Monospace:
|
case ValueID::Monospace:
|
||||||
case ValueID::UiMonospace:
|
case ValueID::UiMonospace:
|
||||||
|
generic_font = Platform::GenericFont::Monospace;
|
||||||
monospace = true;
|
monospace = true;
|
||||||
return find_font("Csilla");
|
break;
|
||||||
case ValueID::Serif:
|
case ValueID::Serif:
|
||||||
return find_font("Roman");
|
generic_font = Platform::GenericFont::Serif;
|
||||||
|
break;
|
||||||
case ValueID::Fantasy:
|
case ValueID::Fantasy:
|
||||||
return find_font("Comic Book");
|
generic_font = Platform::GenericFont::Fantasy;
|
||||||
|
break;
|
||||||
case ValueID::SansSerif:
|
case ValueID::SansSerif:
|
||||||
|
generic_font = Platform::GenericFont::SansSerif;
|
||||||
|
break;
|
||||||
case ValueID::Cursive:
|
case ValueID::Cursive:
|
||||||
|
generic_font = Platform::GenericFont::Cursive;
|
||||||
|
break;
|
||||||
case ValueID::UiSerif:
|
case ValueID::UiSerif:
|
||||||
|
generic_font = Platform::GenericFont::UiSerif;
|
||||||
|
break;
|
||||||
case ValueID::UiSansSerif:
|
case ValueID::UiSansSerif:
|
||||||
|
generic_font = Platform::GenericFont::UiSansSerif;
|
||||||
|
break;
|
||||||
case ValueID::UiRounded:
|
case ValueID::UiRounded:
|
||||||
return find_font("Katica");
|
generic_font = Platform::GenericFont::UiRounded;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
return find_font(Platform::FontPlugin::the().generic_font_name(generic_font));
|
||||||
};
|
};
|
||||||
|
|
||||||
RefPtr<Gfx::Font> found_font;
|
RefPtr<Gfx::Font> found_font;
|
||||||
|
|
27
Userland/Libraries/LibWeb/Platform/FontPlugin.cpp
Normal file
27
Userland/Libraries/LibWeb/Platform/FontPlugin.cpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibWeb/Platform/FontPlugin.h>
|
||||||
|
|
||||||
|
namespace Web::Platform {
|
||||||
|
|
||||||
|
static FontPlugin* s_the;
|
||||||
|
|
||||||
|
FontPlugin& FontPlugin::the()
|
||||||
|
{
|
||||||
|
VERIFY(s_the);
|
||||||
|
return *s_the;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontPlugin::install(FontPlugin& plugin)
|
||||||
|
{
|
||||||
|
VERIFY(!s_the);
|
||||||
|
s_the = &plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
FontPlugin::~FontPlugin() = default;
|
||||||
|
|
||||||
|
}
|
36
Userland/Libraries/LibWeb/Platform/FontPlugin.h
Normal file
36
Userland/Libraries/LibWeb/Platform/FontPlugin.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Forward.h>
|
||||||
|
|
||||||
|
namespace Web::Platform {
|
||||||
|
|
||||||
|
enum class GenericFont {
|
||||||
|
Cursive,
|
||||||
|
Fantasy,
|
||||||
|
Monospace,
|
||||||
|
SansSerif,
|
||||||
|
Serif,
|
||||||
|
UiMonospace,
|
||||||
|
UiRounded,
|
||||||
|
UiSansSerif,
|
||||||
|
UiSerif,
|
||||||
|
__Count,
|
||||||
|
};
|
||||||
|
|
||||||
|
class FontPlugin {
|
||||||
|
public:
|
||||||
|
static FontPlugin& the();
|
||||||
|
static void install(FontPlugin&);
|
||||||
|
|
||||||
|
virtual ~FontPlugin();
|
||||||
|
|
||||||
|
virtual String generic_font_name(GenericFont) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ set(SOURCES
|
||||||
ConnectionFromClient.cpp
|
ConnectionFromClient.cpp
|
||||||
ConsoleGlobalObject.cpp
|
ConsoleGlobalObject.cpp
|
||||||
EventLoopPluginSerenity.cpp
|
EventLoopPluginSerenity.cpp
|
||||||
|
FontPluginSerenity.cpp
|
||||||
PageHost.cpp
|
PageHost.cpp
|
||||||
TimerSerenity.cpp
|
TimerSerenity.cpp
|
||||||
WebContentClientEndpoint.h
|
WebContentClientEndpoint.h
|
||||||
|
|
44
Userland/Services/WebContent/FontPluginSerenity.cpp
Normal file
44
Userland/Services/WebContent/FontPluginSerenity.cpp
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "FontPluginSerenity.h"
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <LibGfx/Font/FontDatabase.h>
|
||||||
|
|
||||||
|
namespace WebContent {
|
||||||
|
|
||||||
|
FontPluginSerenity::FontPluginSerenity()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FontPluginSerenity::~FontPluginSerenity() = default;
|
||||||
|
|
||||||
|
String FontPluginSerenity::generic_font_name(Web::Platform::GenericFont generic_font)
|
||||||
|
{
|
||||||
|
// 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.
|
||||||
|
switch (generic_font) {
|
||||||
|
case Web::Platform::GenericFont::SansSerif:
|
||||||
|
case Web::Platform::GenericFont::UiSansSerif:
|
||||||
|
case Web::Platform::GenericFont::Cursive:
|
||||||
|
case Web::Platform::GenericFont::UiRounded:
|
||||||
|
return "Katica";
|
||||||
|
case Web::Platform::GenericFont::Monospace:
|
||||||
|
case Web::Platform::GenericFont::UiMonospace:
|
||||||
|
return "Csilla";
|
||||||
|
case Web::Platform::GenericFont::Serif:
|
||||||
|
case Web::Platform::GenericFont::UiSerif:
|
||||||
|
return "Roman";
|
||||||
|
case Web::Platform::GenericFont::Fantasy:
|
||||||
|
return "Comic Book";
|
||||||
|
case Web::Platform::GenericFont::__Count:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
22
Userland/Services/WebContent/FontPluginSerenity.h
Normal file
22
Userland/Services/WebContent/FontPluginSerenity.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
#include <LibWeb/Platform/FontPlugin.h>
|
||||||
|
|
||||||
|
namespace WebContent {
|
||||||
|
|
||||||
|
class FontPluginSerenity final : public Web::Platform::FontPlugin {
|
||||||
|
public:
|
||||||
|
FontPluginSerenity();
|
||||||
|
virtual ~FontPluginSerenity();
|
||||||
|
|
||||||
|
virtual String generic_font_name(Web::Platform::GenericFont) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -1,10 +1,11 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "EventLoopPluginSerenity.h"
|
#include "EventLoopPluginSerenity.h"
|
||||||
|
#include "FontPluginSerenity.h"
|
||||||
#include <LibCore/EventLoop.h>
|
#include <LibCore/EventLoop.h>
|
||||||
#include <LibCore/LocalServer.h>
|
#include <LibCore/LocalServer.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
|
@ -31,6 +32,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
TRY(Core::System::unveil(nullptr, nullptr));
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
|
|
||||||
Web::Platform::EventLoopPlugin::install(*new WebContent::EventLoopPluginSerenity);
|
Web::Platform::EventLoopPlugin::install(*new WebContent::EventLoopPluginSerenity);
|
||||||
|
Web::Platform::FontPlugin::install(*new WebContent::FontPluginSerenity);
|
||||||
|
|
||||||
Web::ImageDecoding::Decoder::initialize(WebView::ImageDecoderClientAdapter::create());
|
Web::ImageDecoding::Decoder::initialize(WebView::ImageDecoderClientAdapter::create());
|
||||||
Web::WebSockets::WebSocketClientManager::initialize(TRY(WebView::WebSocketClientManagerAdapter::try_create()));
|
Web::WebSockets::WebSocketClientManager::initialize(TRY(WebView::WebSocketClientManagerAdapter::try_create()));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue