Skip to content

Commit

Permalink
fix font reload crash
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Nov 17, 2023
1 parent 04a12ef commit 20b5c9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/views/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Debug : public View {
struct LogItem {
char *Str;
const char *Lev;
ImFont *Font;
int FontIdx;
};

Mpv *mpv;
Expand Down
27 changes: 14 additions & 13 deletions source/views/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,19 @@ void Debug::Console::AddLog(const char* level, const char* fmt, ...) {
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
va_end(args);
Items.push_back({ImStrdup(buf), level, GetFont(buf)});

int fontIdx = 1; // mono
const char* p = buf;
auto mono = ImGui::GetIO().Fonts->Fonts[fontIdx];
while (*p) {
if (*p != '\n' && *p != '\r' && !mono->FindGlyphNoFallback((ImWchar)*p)) {
fontIdx = 0; // unicode
break;
}
p++;
}

Items.push_back({ImStrdup(buf), level, fontIdx});
if (Items.Size > LogLimit) {
int offset = Items.Size - LogLimit;
for (int i = 0; i < offset; i++) free(Items[i].Str);
Expand All @@ -374,17 +386,6 @@ ImVec4 Debug::Console::LogColor(const char* level) {
return logColors[level];
}

ImFont* Debug::Console::GetFont(const char* str) {
auto mono = ImGui::GetIO().Fonts->Fonts.back();
const char* p = str;
while (*p) {
if (*p != '\n' && *p != '\r' && mono->FindGlyphNoFallback((ImWchar)*p) == nullptr)
return ImGui::GetIO().Fonts->Fonts.front();
p++;
}
return mono;
}

void Debug::Console::draw() {
ImGui::BeginDisabled();
ImGui::TextUnformatted("views.debug.console.tip"_i18n);
Expand Down Expand Up @@ -432,7 +433,7 @@ void Debug::Console::draw() {
if (!Filter.PassFilter(item.Str)) continue;

ImGui::PushStyleColor(ImGuiCol_Text, LogColor(item.Lev));
ImGui::PushFont(item.Font);
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[item.FontIdx]);
ImGui::TextUnformatted(item.Str);
ImGui::PopFont();
ImGui::PopStyleColor();
Expand Down

0 comments on commit 20b5c9b

Please sign in to comment.