Serenity Operating System
1/*
2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#define AK_DONT_REPLACE_STD
9
10#include "FontPluginQt.h"
11#include <AK/DeprecatedString.h>
12#include <AK/String.h>
13#include <LibCore/StandardPaths.h>
14#include <LibGfx/Font/Emoji.h>
15#include <LibGfx/Font/FontDatabase.h>
16#include <QFont>
17#include <QFontInfo>
18
19extern DeprecatedString s_serenity_resource_root;
20
21namespace Ladybird {
22
23FontPluginQt::FontPluginQt()
24{
25 // Load the default SerenityOS fonts...
26 Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/res/fonts", s_serenity_resource_root));
27
28 // ...and also anything we can find in the system's font directories
29 for (auto const& path : Core::StandardPaths::font_directories().release_value_but_fixme_should_propagate_errors())
30 Gfx::FontDatabase::the().load_all_fonts_from_path(path.to_deprecated_string());
31
32 Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
33 Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
34
35 Gfx::Emoji::set_emoji_lookup_path(String::formatted("{}/res/emoji", s_serenity_resource_root).release_value_but_fixme_should_propagate_errors());
36
37 update_generic_fonts();
38
39 auto default_font_name = generic_font_name(Web::Platform::GenericFont::UiSansSerif);
40 m_default_font = Gfx::FontDatabase::the().get(default_font_name, 12.0, 400, Gfx::FontWidth::Normal, 0);
41 VERIFY(m_default_font);
42
43 auto default_fixed_width_font_name = generic_font_name(Web::Platform::GenericFont::UiMonospace);
44 m_default_fixed_width_font = Gfx::FontDatabase::the().get(default_fixed_width_font_name, 12.0, 400, Gfx::FontWidth::Normal, 0);
45 VERIFY(m_default_fixed_width_font);
46}
47
48FontPluginQt::~FontPluginQt() = default;
49
50Gfx::Font& FontPluginQt::default_font()
51{
52 return *m_default_font;
53}
54
55Gfx::Font& FontPluginQt::default_fixed_width_font()
56{
57 return *m_default_fixed_width_font;
58}
59
60void FontPluginQt::update_generic_fonts()
61{
62 // How we choose which system font to use for each CSS font:
63 // 1. Ask Qt via the QFont::StyleHint mechanism for the user's preferred font.
64 // 2. Try loading that font through Gfx::FontDatabase
65 // 3. If we don't support that font for whatever reason (e.g missing TrueType features in LibGfx)...
66 // 1. Try a list of known-suitable fallback fonts with their names hard-coded below
67 // 2. If that didn't work, fall back to Gfx::FontDatabase::default_font() (or default_fixed_width_font())
68
69 // This is rather weird, but it's how things work right now, as we can only draw with fonts loaded by LibGfx.
70
71 m_generic_font_names.resize(static_cast<size_t>(Web::Platform::GenericFont::__Count));
72
73 auto update_mapping = [&](Web::Platform::GenericFont generic_font, QFont::StyleHint qfont_style_hint, Vector<DeprecatedString> fallbacks = {}) {
74 QFont qt_font;
75 qt_font.setStyleHint(qfont_style_hint);
76 QFontInfo qt_info(qt_font);
77 auto qt_font_family = qt_info.family();
78
79 auto gfx_font = Gfx::FontDatabase::the().get(qt_font_family.toUtf8().data(), 16, 400, Gfx::FontWidth::Normal, 0, Gfx::Font::AllowInexactSizeMatch::Yes);
80 if (!gfx_font) {
81 for (auto& fallback : fallbacks) {
82 gfx_font = Gfx::FontDatabase::the().get(fallback, 16, 400, Gfx::FontWidth::Normal, 0, Gfx::Font::AllowInexactSizeMatch::Yes);
83 if (gfx_font)
84 break;
85 }
86 }
87
88 if (!gfx_font) {
89 if (generic_font == Web::Platform::GenericFont::Monospace || generic_font == Web::Platform::GenericFont::UiMonospace)
90 gfx_font = Gfx::FontDatabase::default_fixed_width_font();
91 else
92 gfx_font = Gfx::FontDatabase::default_font();
93 }
94
95 m_generic_font_names[static_cast<size_t>(generic_font)] = gfx_font->family();
96 };
97
98 // Fallback fonts to look for if Gfx::Font can't load the font suggested by Qt.
99 // The lists are basically arbitrary, taken from https://www.w3.org/Style/Examples/007/fonts.en.html
100 Vector<DeprecatedString> cursive_fallbacks { "Comic Sans MS", "Comic Sans", "Apple Chancery", "Bradley Hand", "Brush Script MT", "Snell Roundhand", "URW Chancery L" };
101 Vector<DeprecatedString> fantasy_fallbacks { "Impact", "Luminari", "Chalkduster", "Jazz LET", "Blippo", "Stencil Std", "Marker Felt", "Trattatello" };
102 Vector<DeprecatedString> monospace_fallbacks { "Andale Mono", "Courier New", "Courier", "FreeMono", "OCR A Std", "DejaVu Sans Mono", "Liberation Mono", "Csilla" };
103 Vector<DeprecatedString> sans_serif_fallbacks { "Arial", "Helvetica", "Verdana", "Trebuchet MS", "Gill Sans", "Noto Sans", "Avantgarde", "Optima", "Arial Narrow", "Liberation Sans", "Katica" };
104 Vector<DeprecatedString> serif_fallbacks { "Times", "Times New Roman", "Didot", "Georgia", "Palatino", "Bookman", "New Century Schoolbook", "American Typewriter", "Liberation Serif", "Roman" };
105
106 update_mapping(Web::Platform::GenericFont::Cursive, QFont::StyleHint::Cursive, cursive_fallbacks);
107 update_mapping(Web::Platform::GenericFont::Fantasy, QFont::StyleHint::Fantasy, fantasy_fallbacks);
108 update_mapping(Web::Platform::GenericFont::Monospace, QFont::StyleHint::Monospace, monospace_fallbacks);
109 update_mapping(Web::Platform::GenericFont::SansSerif, QFont::StyleHint::SansSerif, sans_serif_fallbacks);
110 update_mapping(Web::Platform::GenericFont::Serif, QFont::StyleHint::Serif, serif_fallbacks);
111 update_mapping(Web::Platform::GenericFont::UiMonospace, QFont::StyleHint::Monospace, monospace_fallbacks);
112 update_mapping(Web::Platform::GenericFont::UiRounded, QFont::StyleHint::SansSerif, sans_serif_fallbacks);
113 update_mapping(Web::Platform::GenericFont::UiSansSerif, QFont::StyleHint::SansSerif, sans_serif_fallbacks);
114 update_mapping(Web::Platform::GenericFont::UiSerif, QFont::StyleHint::Serif, serif_fallbacks);
115}
116
117DeprecatedString FontPluginQt::generic_font_name(Web::Platform::GenericFont generic_font)
118{
119 return m_generic_font_names[static_cast<size_t>(generic_font)];
120}
121
122}