Read the system CJK font once, and hold fonts as ImFont pointers

Each of the four fonts merged the system CJK font from a fresh read of its file, twice, and never freed it.
It is now read once and shared. Startup memory with a document open went from 412 MB to 269 MB.
This commit is contained in:
shweet committed 2026-11-22 17:44:07 -05:00
1 parent 2f1e75da37
commit 20209e655e
12 files changed
+58 -106

No files matched your search

+2 -2
View File
@@ -272,7 +272,7 @@ static void friend_canvas_draw(About::FriendState& state, Resources& resources)
// The application name, as large as fits its row.
static void title_draw(About& about, Resources& resources, const char* title, float width)
{
auto font = resources.fonts[FONT_BOLD].get();
auto font = resources.fonts[FONT_BOLD];
auto fontSize = (float)FONT_SIZE_LARGE * TITLE_SCALE;
fontSize *= std::min(1.0f, width / font->CalcTextSizeA(fontSize, FLT_MAX, 0.0f, title).x);
auto textSize = font->CalcTextSizeA(fontSize, FLT_MAX, 0.0f, title);
@@ -366,7 +366,7 @@ static void roll_text_draw(ImDrawList* drawList, Resources& resources, const Abo
if (!*credit.string || y + lineHeight < min.y || y > max.y)
continue;
auto font = resources.fonts[credit.font].get();
auto font = resources.fonts[credit.font];
auto x =
centerX - font->CalcTextSizeA(lineHeight, FLT_MAX, 0.0f, credit.string).x * 0.5f + frame.offsetX * scale;
if (frame.effect == EFFECT_WAVE)
+1 -1
View File
@@ -891,7 +891,7 @@ void AnimationPreview::update(Manager& manager, Settings& settings, Resources& r
auto textPos = to_imvec2(to_vec2(cursorScreenPos) + to_vec2(ImGui::GetStyle().WindowPadding));
drawList->PushClipRect(clipMin, clipMax);
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE_LARGE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE_LARGE);
auto triggerTextColor = isLightTheme ? TRIGGER_TEXT_COLOR_LIGHT : TRIGGER_TEXT_COLOR_DARK;
drawList->AddText(textPos, ImGui::GetColorU32(triggerTextColor), names.c_str());
ImGui::PopFont();
+1 -1
View File
@@ -720,7 +720,7 @@ void animations_update(Panel& panel, AnimationsPanel& animations)
tooltip_name_draw(resources, animation.name);
if (model.animations.defaultAnimation == animation.name)
{
ImGui::PushFont(resources.fonts[FONT_ITALICS].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_ITALICS], FONT_SIZE);
ImGui::TextUnformatted(localize.get(BASIC_DEFAULT));
ImGui::PopFont();
}
+1 -1
View File
@@ -212,7 +212,7 @@ void Documents::update(Taskbar& taskbar, Manager& manager, Settings& settings, R
if (isRequested)
flags |= ImGuiTabItemFlags_SetSelected;
ImGui::PushFont(resources.fonts[font].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[font], FONT_SIZE);
if (ImGui::BeginTabItem(label.c_str(), &document.isOpen, flags))
{
if (manager.selected != i)
+3 -3
View File
@@ -130,7 +130,7 @@ bool row_draw(Panel& panel, std::set<int>& selection, int& focus, const ListRows
ImGui::PushID(key);
ImGui::SetNextItemSelectionUserData(index);
if (font != FONT_REGULAR)
ImGui::PushFont(panel.resources.fonts[font].get(), FONT_SIZE);
ImGui::PushFont(panel.resources.fonts[font], FONT_SIZE);
if (isInvalid)
ImGui::PushStyleColor(ImGuiCol_Text, CARD_TINT_INVALID);
if (arrowKey == key)
@@ -244,7 +244,7 @@ static void card_draw(Panel& panel, std::set<int>& selection, int& focus, const
ImGui::SetCursorPos(
ImVec2(cardSize.y + style.ItemSpacing.x, cardSize.y - cardSize.y / 2 - ImGui::GetTextLineHeight() / 2));
if (isFocused)
ImGui::PushFont(panel.resources.fonts[FONT_ITALICS].get(), FONT_SIZE);
ImGui::PushFont(panel.resources.fonts[FONT_ITALICS], FONT_SIZE);
ImGui::TextUnformatted(label.c_str());
if (isFocused)
ImGui::PopFont();
@@ -476,7 +476,7 @@ void list_panel_draw(Panel& panel, Actions& actions, const Footer& footer, const
void tooltip_name_draw(Resources& resources, const std::string& name)
{
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(name.c_str());
ImGui::PopFont();
}
+1 -1
View File
@@ -402,7 +402,7 @@ void SpritesheetEditor::update(Manager& manager, Settings& settings, Resources&
if (tool == TOOL_PAN && hoveredRegionId != -1 && spritesheet)
if (auto region = region_get(hoveredRegionId); region && ImGui::BeginTooltip())
{
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(region->name.c_str());
ImGui::PopFont();
auto originLabel = localize.get(region->origin == Origin::TOP_LEFT ? LABEL_REGION_ORIGIN_TOP_LEFT
+6 -6
View File
@@ -155,7 +155,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index)
if (isGroupTooltipDelayed && !isVisibleButtonHovered)
{
ImGui::BeginTooltip();
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(label.c_str());
ImGui::PopFont();
ImGui::TextUnformatted(std::vformat(localize.get(FORMAT_ID), std::make_format_args(group->id)).c_str());
@@ -307,7 +307,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index)
{
case ROOT:
{
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(localize.get(BASIC_ROOT));
ImGui::PopFont();
@@ -328,7 +328,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index)
auto layer = ::item_get(model.content.layers, id);
if (!layer)
break;
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(layer->name.c_str());
ImGui::PopFont();
@@ -348,7 +348,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index)
auto nullInfo = ::item_get(model.content.nulls, id);
if (!nullInfo)
break;
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(nullInfo->name.c_str());
ImGui::PopFont();
@@ -367,7 +367,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index)
}
case TRIGGER:
{
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE);
ImGui::TextUnformatted(localize.get(BASIC_TRIGGERS));
ImGui::PopFont();
@@ -413,7 +413,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index)
overlay_icon(resources.icon_id_get(icon), iconTintCurrent, false);
ImGui::SameLine();
if (isReferenced)
ImGui::PushFont(resources.fonts[FONT_ITALICS].get(), FONT_SIZE);
ImGui::PushFont(resources.fonts[FONT_ITALICS], FONT_SIZE);
ImGui::PushStyleColor(ImGuiCol_Text, itemTextColor);
ImGui::TextUnformatted(label.c_str());
ImGui::PopStyleColor();
+1 -1
View File
@@ -26,7 +26,7 @@ void welcome_update(Manager& manager, Resources& resources, Dialog& dialog, Task
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse))
{
ImGui::PushFont(resources.fonts[FONT_BOLD].get(), FONT_SIZE_LARGE);
ImGui::PushFont(resources.fonts[FONT_BOLD], FONT_SIZE_LARGE);
ImGui::TextUnformatted(localize.get(LABEL_APPLICATION_NAME));
ImGui::PopFont();