Xbox Cloud Gaming app for PS5 Homebrew
C
ui::Font: a font that didn't load draws nothing instead of crashing
Rafael Abud Ferreira3f58cfc07366
diff --git a/src/ui/font.cpp b/src/ui/font.cppindex 0e3ca49..3eb21dd 100644--- a/src/ui/font.cpp+++ b/src/ui/font.cpp@@ -11,6 +11,7 @@#define STBTT_STATIC#include "stb_truetype.h"+#include <atomic>#include <cmath>namespace xc::ui {@@ -24,6 +25,7 @@ Font::Font() : impl_(std::make_unique<Impl>()) {}Font::~Font() = default;bool Font::load(const std::string& path) {+ loaded_ = false;std::string bytes;if (!platform::readFile(path, bytes)) {XC_LOGE("font %s: cannot read", path.c_str());@@ -35,10 +37,23 @@ bool Font::load(const std::string& path) {return false;}stbtt_GetFontVMetrics(&impl_->info, &impl_->ascent, &impl_->descent, &impl_->lineGap);+ {+ std::lock_guard<std::mutex> lock(mutex_);+ cache_.clear();+ }+ loaded_ = true;return true;}+bool Font::usable() const {+ if (loaded_) return true;+ static std::atomic<bool> told{false};+ if (!told.load(std::memory_order_relaxed) && !told.exchange(true)) XC_LOGE("a font is missing: its text isn't drawn");+ return false;+}+int Font::centeredY(int boxY, int boxH, int px) const {+ if (!usable()) return boxY;// Cap height from the "H" glyph's box, in font units.int x0, y0, x1, y1;float capH = stbtt_GetCodepointBox(&impl_->info, 'H', &x0, &y0, &x1, &y1) ? y1 * scale(px) : 0.7f * px;@@ -50,6 +65,7 @@ int Font::centeredY(int boxY, int boxH, int px) const {float Font::scale(int px) const { return stbtt_ScaleForPixelHeight(&impl_->info, static_cast<float>(px)); }int Font::lineHeight(int px) const {+ if (!usable()) return px;return static_cast<int>(std::ceil((impl_->ascent - impl_->descent) * scale(px)));}@@ -67,6 +83,8 @@ uint32_t nextCodepoint(std::string_view s, size_t& i) {}const Font::Glyph& Font::glyph(uint32_t cp, int px) const {+ static const Glyph kNone;+ if (!usable()) return kNone;std::lock_guard<std::mutex> lock(mutex_);auto key = std::make_pair(cp, px);auto it = cache_.find(key);@@ -95,6 +113,7 @@ int Font::measure(std::string_view text, int px) const {}int Font::draw(Canvas& c, std::string_view text, int x, int y, int px, Color color) const {+ if (!usable()) return x;int baseline = y + static_cast<int>(std::lround(impl_->ascent * scale(px)));for (size_t i = 0; i < text.size();) {const Glyph& g = glyph(nextCodepoint(text, i), px);diff --git a/src/ui/font.h b/src/ui/font.hindex 6ca60dc..bd76583 100644--- a/src/ui/font.h+++ b/src/ui/font.h@@ -22,7 +22,12 @@ public:Font(const Font&) = delete;Font& operator=(const Font&) = delete;+ // False (logged) when the file can't be read or isn't a font. A font+ // that never loaded measures and draws as nothing (said once in the log)+ // instead of reading an empty font: the app still runs without its+ // assets folder.bool load(const std::string& path);+ bool loaded() const { return loaded_; }int lineHeight(int px) const;// The `y` to draw at so that capital letters sit centred in a box of@@ -44,10 +49,13 @@ private:};const Glyph& glyph(uint32_t codepoint, int px) const;float scale(int px) const;+ // loaded_, else false and a line in the log (the first time).+ bool usable() const;struct Impl;std::unique_ptr<Impl> impl_;std::vector<unsigned char> data_;+ bool loaded_ = false;mutable std::mutex mutex_;mutable std::map<std::pair<uint32_t, int>, Glyph> cache_;};diff --git a/tests/test_main.cpp b/tests/test_main.cppindex 4ee59de..d276d1d 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -1139,6 +1139,21 @@ static void testGyro() {// The desktop's stand-ins for the PS5 backends: nothing connected, the// settings kept for their getters.+// A font that didn't load (no assets folder) draws and measures nothing+// instead of crashing in stb_truetype.+static void testMissingFont() {+ ui::Font font;+ CHECK(!font.load("/nonexistent/Inter-Regular.ttf") && !font.loaded());+ ui::Canvas c(64, 32);+ c.clear(ui::rgba(1, 2, 3));+ CHECK(font.measure("Hello", 24) == 0);+ CHECK(font.draw(c, "Hello", 5, 4, 24, ui::rgba(255, 255, 255)) == 5);+ CHECK(c.data()[5 * 64 + 10] == ui::rgba(1, 2, 3));+ CHECK(font.centeredY(10, 40, 24) == 10 && font.lineHeight(24) == 24);+ CHECK(font.fit("Hello", 24, 100) == "Hello");+ CHECK(font.wrap("Hello world", 24, 100, 2).size() == 1);+}+// The desktop app's in-app notices (platform/toasts.h).static void testToasts() {using platform::ToastQueue;@@ -1199,6 +1214,7 @@ static void testNullBackends() {int main() {testNullBackends();testToasts();+ testMissingFont();testLoginRetry();testGyro();testReport();