perf(startup): build the font atlas once at the final ui.toml sizes

main() built the font atlas before App::init() loaded ui.toml, so the
first build used the hard-coded fallback sizes (H1 24 px, Body2 12 px).
App::init() then rebuilt for a saved font scale above 1.0, still before
the schema load and so still at the fallback sizes. Only the color-emoji
rebuild requested for the first frame reached the ui.toml sizes, so
color-emoji users built the atlas twice at font scale 1.0 and three
times at 1.5.

Users with chat_emoji_color=false never got that last rebuild. They
built the atlas once or twice and ran the whole session at the fallback
sizes, e.g. Body2 at 12 px instead of 15 and H1 at 24 px instead of 20.

Now:
- App::loadBaseUiSchema(), the ui.toml load moved out of App::init()
  unchanged, runs in main() before both InitImGui paths (DX11 and GL),
  together with the saved font scale and emoji style from the early
  settings read. It is idempotent, so the call left in App::init() is a
  no-op;
- the two rebuild triggers in App::init() are gone;
- Typography records what the atlas was built from (the 15 schema sizes,
  the schema's font scale, the effective emoji style and the schema
  generation), and preFrame() rebuilds once when they differ. While the
  schema generation and the emoji style are unchanged this costs two
  compares per frame.

Measured on the Lite build (color and mono emoji, font scale 1.0 and
1.5, English and Japanese): every run builds the atlas exactly once, at
the ui.toml sizes times the scale (H1 20 px and Body2 15 px at 1.0,
30 px and 22 px at 1.5), and ui.toml is loaded before the window and GL
context are created. The packaged Lite zip and AppImage log one atlas
build instead of two. Color-emoji screenshot sweeps match the previous
build within noise.

Visible changes:
- users with chat_emoji_color=false had been rendering the whole UI at
  the fallback font sizes; they now get the designed ui.toml sizes, so
  every mono-emoji screen differs from before and matches the
  color-emoji layout;
- a user skin whose [fonts] differ from ui.toml now rebuilds the atlas
  once when it is activated or left; on a runtime switch the atlas used
  to keep the old sizes until a restart. With a test skin that sets
  Body2 to 18 px, startup builds twice and ends at 18 px; switching to a
  bundled skin rebuilds once, switching among the 9 bundled skins
  rebuilds nothing (they carry an identical copy of [fonts]), and
  switching back rebuilds once;
- editing ui.toml while running still gives exactly one rebuild.

Not verifiable here: the Windows DX11 path with the schema loaded before
its first FreeType build, macOS, and the interactive font-scale slider,
Alt+scroll and emoji toggle, which were checked by reading the code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-11 06:05:07 -05:00
co-authored by Claude Opus 5.5
parent e0f9d36780
commit b50d5ac31f
5 changed files with 117 additions and 56 deletions
+54 -55
View File
@@ -348,6 +348,49 @@ private:
App& app_;
};
bool App::loadBaseUiSchema()
{
if (ui::schema::UISchema::instance().isLoaded()) return true;
std::string schemaPath = util::Platform::getExecutableDirectory() + "/res/themes/ui.toml";
bool loaded = false;
#if HAS_EMBEDDED_UI_TOML
// If on-disk ui.toml exists but differs in size from the embedded
// version, a newer wallet binary is running against stale theme
// files. Overwrite the on-disk copy so layout matches the binary.
if (std::filesystem::exists(schemaPath)) {
std::error_code ec;
auto diskSize = std::filesystem::file_size(schemaPath, ec);
if (!ec && diskSize != static_cast<std::uintmax_t>(embedded::ui_toml_size)) {
DEBUG_LOGF("[INFO] ui.toml on disk (%ju bytes) differs from embedded (%zu bytes) — updating\n",
(uintmax_t)diskSize, embedded::ui_toml_size);
std::ofstream ofs(schemaPath, std::ios::binary | std::ios::trunc);
if (ofs.is_open()) {
ofs.write(reinterpret_cast<const char*>(embedded::ui_toml_data),
embedded::ui_toml_size);
ofs.close();
}
}
}
#endif
if (std::filesystem::exists(schemaPath)) {
loaded = ui::schema::UISchema::instance().loadFromFile(schemaPath);
}
// Fallback: load from build-time embedded data when file not on disk
// (e.g., single-file Windows distribution without res/ directory)
if (!loaded) {
#if HAS_EMBEDDED_UI_TOML
std::string embedded(reinterpret_cast<const char*>(embedded::ui_toml_data),
embedded::ui_toml_size);
ui::schema::UISchema::instance().loadFromString(embedded, "embedded");
#else
DEBUG_LOGF("Warning: ui.toml not found at %s and no embedded fallback\n", schemaPath.c_str());
#endif
}
return ui::schema::UISchema::instance().isLoaded();
}
bool App::init()
{
DEBUG_LOGF("Initializing ObsidianDragon...\n");
@@ -363,9 +406,6 @@ bool App::init()
if (!settings_->load()) {
DEBUG_LOGF("Warning: Could not load settings, using defaults\n");
}
// The initial font atlas is built (in main, before App renders) with monochrome emoji. If the saved
// setting wants color, request a rebuild so the first preFrame switches to the FreeType color atlas.
if (settings_->getChatEmojiColor()) font_rebuild_requested_ = true;
// On upgrade (version mismatch), re-save to persist new defaults + current version
if (settings_->needsUpgradeSave()) {
DEBUG_LOGF("[INFO] Wallet upgraded — re-saving settings with new defaults\n");
@@ -392,21 +432,6 @@ bool App::init()
ui::ConsoleTab::s_line_text_color_enabled = settings_->getConsoleTextColor();
ui::ConsoleTab::s_console_zoom = settings_->getConsoleZoom();
// Apply saved user font scale so fonts are correct on first reload
{
float fs = settings_->getFontScale();
if (fs > 1.0f) {
ui::Layout::setUserFontScale(fs);
// Fonts were loaded at default scale in Init(); rebuild now.
auto& typo = ui::material::Typography::instance();
ImGuiIO& io = ImGui::GetIO();
typo.reload(io, typo.getDpiScale());
// Consume the flag so App::update() doesn't double-reload
ui::Layout::consumeUserFontReload();
DEBUG_LOGF("App: Applied saved font scale %.1fx\n", fs);
}
}
// Ensure ObsidianDragon config directory and template files exist
util::Platform::ensureObsidianDragonSetup();
@@ -442,43 +467,7 @@ bool App::init()
// Initialize unified UI schema (loads TOML, drives all layout values)
{
std::string schemaPath = util::Platform::getExecutableDirectory() + "/res/themes/ui.toml";
bool loaded = false;
#if HAS_EMBEDDED_UI_TOML
// If on-disk ui.toml exists but differs in size from the embedded
// version, a newer wallet binary is running against stale theme
// files. Overwrite the on-disk copy so layout matches the binary.
if (std::filesystem::exists(schemaPath)) {
std::error_code ec;
auto diskSize = std::filesystem::file_size(schemaPath, ec);
if (!ec && diskSize != static_cast<std::uintmax_t>(embedded::ui_toml_size)) {
DEBUG_LOGF("[INFO] ui.toml on disk (%ju bytes) differs from embedded (%zu bytes) — updating\n",
(uintmax_t)diskSize, embedded::ui_toml_size);
std::ofstream ofs(schemaPath, std::ios::binary | std::ios::trunc);
if (ofs.is_open()) {
ofs.write(reinterpret_cast<const char*>(embedded::ui_toml_data),
embedded::ui_toml_size);
ofs.close();
}
}
}
#endif
if (std::filesystem::exists(schemaPath)) {
loaded = ui::schema::UISchema::instance().loadFromFile(schemaPath);
}
// Fallback: load from build-time embedded data when file not on disk
// (e.g., single-file Windows distribution without res/ directory)
if (!loaded) {
#if HAS_EMBEDDED_UI_TOML
std::string embedded(reinterpret_cast<const char*>(embedded::ui_toml_data),
embedded::ui_toml_size);
ui::schema::UISchema::instance().loadFromString(embedded, "embedded");
#else
DEBUG_LOGF("Warning: ui.toml not found at %s and no embedded fallback\n", schemaPath.c_str());
#endif
}
loadBaseUiSchema(); // no-op when main() already loaded it before the first font build
// Initialize SkinManager and activate saved skin
auto& skinMgr = ui::schema::SkinManager::instance();
@@ -685,6 +674,16 @@ void App::preFrame()
typo.reload(io, typo.getDpiScale());
DEBUG_LOGF("App: Font atlas rebuilt on request (chat emoji style)\n");
}
// Safety net: schema font sizes (e.g. a user skin overlay defining [fonts]) or the emoji style differ from what the
// atlas was built with. Steady state is one integer compare; covers load-order skew without unconditional rebuilds.
// Build-expanded bundled skins carry an identical copy of [fonts] (expand_themes.py), so needsRebuild sees no size
// change; user skins with their own [fonts] trigger exactly one rebuild.
if (ui::material::Typography::instance().needsRebuild()) {
auto& typo = ui::material::Typography::instance();
typo.reload(io, typo.getDpiScale());
DEBUG_LOGF("App: Font atlas rebuilt (schema font sizes / emoji style changed)\n");
}
}
namespace {