From c0cb84f7c7de62a4ce2b9cf59421cff9257eb078 Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Thu, 30 Nov 2023 01:46:54 +0100 Subject: [PATCH] browser/gui: Prime the font system w/ a 'monospace' font --- browser/gui/app.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- browser/gui/app.h | 10 ++-------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/browser/gui/app.cpp b/browser/gui/app.cpp index 20f0d260..b1ed07a6 100644 --- a/browser/gui/app.cpp +++ b/browser/gui/app.cpp @@ -8,7 +8,10 @@ #include "dom/dom.h" #include "gfx/color.h" #include "gfx/opengl_canvas.h" +#include "gfx/sfml_canvas.h" +#include "protocol/handler_factory.h" #include "render/render.h" +#include "type/sfml.h" #include "uri/uri.h" #include @@ -21,9 +24,11 @@ #include #include +#include #include #include #include +#include #include #include #include @@ -154,12 +159,44 @@ void window(char const *title, ImVec2 const &position, ImVec2 const &size, auto ImGui::End(); } } // namespace im + +std::unique_ptr create_font_system() { + static constexpr auto kMonospaceFontFileNames = std::to_array({ +#ifdef _WIN32 + "consola.ttf", +#else + "DejaVuSansMono.ttf", +#endif + }); + + auto type = std::make_unique(); + + for (auto const font_name : kMonospaceFontFileNames) { + if (auto font = type->font(font_name)) { + spdlog::info("Using '{}' as monospace font", font_name); + type->set_font("monospace", std::static_pointer_cast(*std::move(font))); + break; + } + } + + if (!type->font("monospace")) { + spdlog::warn("Unable to find a monospace font, looked for [{}], good luck", + fmt::join(kMonospaceFontFileNames, ", ")); + } + + return type; +} + } // namespace App::App(std::string browser_title, std::string start_page_hint, bool load_start_page) - : browser_title_{std::move(browser_title)}, window_{sf::VideoMode(kDefaultResolutionX, kDefaultResolutionY), + : engine_{protocol::HandlerFactory::create( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"), + create_font_system()}, + browser_title_{std::move(browser_title)}, window_{sf::VideoMode(kDefaultResolutionX, kDefaultResolutionY), browser_title_}, - url_buf_{std::move(start_page_hint)} { + url_buf_{std::move(start_page_hint)}, canvas_{std::make_unique( + window_, static_cast(engine_.font_system()))} { window_.setMouseCursor(cursor_); window_.setIcon(16, 16, kBrowserIcon.data()); if (!ImGui::SFML::Init(window_)) { diff --git a/browser/gui/app.h b/browser/gui/app.h index bf5582de..750045b5 100644 --- a/browser/gui/app.h +++ b/browser/gui/app.h @@ -8,10 +8,7 @@ #include "dom/dom.h" #include "engine/engine.h" #include "gfx/icanvas.h" -#include "gfx/sfml_canvas.h" #include "layout/layout_box.h" -#include "protocol/handler_factory.h" -#include "type/sfml.h" #include "uri/uri.h" #include "util/history.h" @@ -35,9 +32,7 @@ class App final { private: // Latest Firefox ESR user agent (on Windows). This matches what the Tor browser does. - engine::Engine engine_{protocol::HandlerFactory::create( - "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"), - std::make_unique()}; + engine::Engine engine_; bool page_loaded_{}; std::string browser_title_{}; @@ -58,8 +53,7 @@ class App final { }; Canvas selected_canvas_{Canvas::Sfml}; - std::unique_ptr canvas_{ - std::make_unique(window_, static_cast(engine_.font_system()))}; + std::unique_ptr canvas_; // The scroll offset is the opposite of the current translation of the web page. // When we scroll "down", the web page is translated "up".