Skip to content

Commit

Permalink
browser/gui: Prime the font system w/ a 'monospace' font
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Dec 2, 2023
1 parent c4e2ec9 commit c0cb84f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
41 changes: 39 additions & 2 deletions browser/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SFML/Graphics/Image.hpp>
Expand All @@ -21,9 +24,11 @@
#include <spdlog/spdlog.h>

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <memory>
#include <optional>
#include <sstream>
#include <string_view>
Expand Down Expand Up @@ -154,12 +159,44 @@ void window(char const *title, ImVec2 const &position, ImVec2 const &size, auto
ImGui::End();
}
} // namespace im

std::unique_ptr<type::IType> create_font_system() {
static constexpr auto kMonospaceFontFileNames = std::to_array<std::string_view>({
#ifdef _WIN32
"consola.ttf",
#else
"DejaVuSansMono.ttf",
#endif
});

auto type = std::make_unique<type::SfmlType>();

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<type::SfmlFont const>(*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<gfx::SfmlCanvas>(
window_, static_cast<type::SfmlType &>(engine_.font_system()))} {
window_.setMouseCursor(cursor_);
window_.setIcon(16, 16, kBrowserIcon.data());
if (!ImGui::SFML::Init(window_)) {
Expand Down
10 changes: 2 additions & 8 deletions browser/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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<type::SfmlType>()};
engine::Engine engine_;
bool page_loaded_{};

std::string browser_title_{};
Expand All @@ -58,8 +53,7 @@ class App final {
};

Canvas selected_canvas_{Canvas::Sfml};
std::unique_ptr<gfx::ICanvas> canvas_{
std::make_unique<gfx::SfmlCanvas>(window_, static_cast<type::SfmlType &>(engine_.font_system()))};
std::unique_ptr<gfx::ICanvas> 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".
Expand Down

0 comments on commit c0cb84f

Please sign in to comment.