Skip to content

Commit

Permalink
fix font fallback issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 14, 2023
1 parent 1d4e2a5 commit 27a9782
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 85 deletions.
1 change: 0 additions & 1 deletion include/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,4 @@ void TextEllipsis(const char* text, float maxWidth = 0);
void Hyperlink(const char* label, const char* url);
void HelpMarker(const char* desc);
ImTextureID LoadTexture(const char* path, int* width = nullptr, int* height = nullptr);
ImFont* codeFont();
} // namespace ImGui
148 changes: 71 additions & 77 deletions source/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,6 @@
#endif
#include "helpers.h"

void ImGui::HalignCenter(const char* text) {
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::CalcTextSize(text).x) * 0.5f);
}

void ImGui::TextCentered(const char* text, bool disabled) {
ImGui::HalignCenter(text);
if (disabled)
ImGui::TextDisabled("%s", text);
else
ImGui::Text("%s", text);
}

void ImGui::TextEllipsis(const char* text, float maxWidth) {
if (maxWidth == 0) maxWidth = ImGui::GetContentRegionAvail().x;
ImGuiStyle style = ImGui::GetStyle();
ImGuiWindow* window = ImGui::GetCurrentWindow();
ImVec2 textSize = ImGui::CalcTextSize(text);
ImVec2 min = ImGui::GetCursorScreenPos();
ImVec2 max = min + ImVec2(maxWidth - style.FramePadding.x, textSize.y);
ImRect textRect(min, max);
ImGui::ItemSize(textRect);
if (ImGui::ItemAdd(textRect, window->GetID(text)))
ImGui::RenderTextEllipsis(ImGui::GetWindowDrawList(), min, max, max.x, max.x, text, nullptr, &textSize);
}

void ImGui::Hyperlink(const char* label, const char* url) {
auto style = ImGui::GetStyle();
ImGui::PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_CheckMark]);
ImGui::Text("%s", label ? label : url);
if (ImGui::IsItemHovered()) ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
if (ImGui::IsItemClicked()) ImPlay::openUrl(url);
ImGui::PopStyleColor();
}

void ImGui::HelpMarker(const char* desc) {
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(35 * ImGui::GetFontSize());
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}

ImTextureID ImGui::LoadTexture(const char* path, int* width, int* height) {
int w, h;
auto icon = romfs::get(path);
auto data = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(icon.data()), icon.size(), &w, &h, NULL, 4);
if (data == nullptr) return nullptr;

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);

if (width != nullptr) *width = w;
if (height != nullptr) *height = h;

return reinterpret_cast<ImTextureID>(static_cast<intptr_t>(texture));
}

// return the second font
ImFont* ImGui::codeFont() {
auto fonts = ImGui::GetIO().Fonts->Fonts;
return fonts.size() > 1 ? fonts[1] : fonts[0];
}

void ImPlay::OptionParser::parse(int argc, char** argv) {
bool optEnd = false;
#ifdef _WIN32
Expand Down Expand Up @@ -266,4 +190,74 @@ std::vector<std::string> ImPlay::split(const std::string& str, const std::string
}
if (pos1 != str.length()) v.push_back(str.substr(pos1));
return v;
}
}

void ImGui::HalignCenter(const char* text) {
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::CalcTextSize(text).x) * 0.5f);
}

void ImGui::TextCentered(const char* text, bool disabled) {
ImGui::HalignCenter(text);
if (disabled)
ImGui::TextDisabled("%s", text);
else
ImGui::Text("%s", text);
}

void ImGui::TextEllipsis(const char* text, float maxWidth) {
if (maxWidth == 0) maxWidth = ImGui::GetContentRegionAvail().x;
ImGuiStyle style = ImGui::GetStyle();
ImGuiWindow* window = ImGui::GetCurrentWindow();
ImVec2 textSize = ImGui::CalcTextSize(text);
ImVec2 min = ImGui::GetCursorScreenPos();
ImVec2 max = min + ImVec2(maxWidth - style.FramePadding.x, textSize.y);
ImRect textRect(min, max);
ImGui::ItemSize(textRect);
if (ImGui::ItemAdd(textRect, window->GetID(text)))
ImGui::RenderTextEllipsis(ImGui::GetWindowDrawList(), min, max, max.x, max.x, text, nullptr, &textSize);
}

void ImGui::Hyperlink(const char* label, const char* url) {
auto style = ImGui::GetStyle();
ImGui::PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_CheckMark]);
ImGui::Text("%s", label ? label : url);
if (ImGui::IsItemHovered()) ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
if (ImGui::IsItemClicked()) ImPlay::openUrl(url);
ImGui::PopStyleColor();
}

void ImGui::HelpMarker(const char* desc) {
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(35 * ImGui::GetFontSize());
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}

ImTextureID ImGui::LoadTexture(const char* path, int* width, int* height) {
int w, h;
auto icon = romfs::get(path);
auto data = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(icon.data()), icon.size(), &w, &h, NULL, 4);
if (data == nullptr) return nullptr;

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);

if (width != nullptr) *width = w;
if (height != nullptr) *height = h;

return reinterpret_cast<ImTextureID>(static_cast<intptr_t>(texture));
}
2 changes: 0 additions & 2 deletions source/views/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ void Debug::Console::draw() {
// - Split them into same height items would be simpler and facilitate random-seeking into your list.
// - Consider using manual call to IsRectVisible() and skipping extraneous decoration from your items.
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing
ImGui::PushFont(ImGui::codeFont());
if (copy_to_clipboard) ImGui::LogToClipboard();
for (int i = 0; i < Items.Size; i++) {
auto item = Items[i];
Expand All @@ -454,7 +453,6 @@ void Debug::Console::draw() {
// Using a scrollbar or mouse-wheel will take away from the bottom edge.
if (ScrollToBottom || (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) ImGui::SetScrollHereY(1.0f);
ScrollToBottom = false;
ImGui::PopFont();
ImGui::PopStyleVar();
}
ImGui::EndChild();
Expand Down
7 changes: 2 additions & 5 deletions source/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,17 @@ void Window::loadFonts() {
io.Fonts->Clear();

ImFontConfig cfg;
cfg.OversampleH = 2.0f;
cfg.OversampleV = 1.0f;
cfg.SizePixels = fontSize;
ImWchar fa_range[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
const ImWchar* font_range = config.buildGlyphRanges();
io.Fonts->AddFontFromMemoryCompressedTTF(fa_compressed_data, fa_compressed_size, iconSize, &cfg, fa_range);
io.Fonts->AddFontFromMemoryCompressedTTF(source_code_pro_compressed_data, source_code_pro_compressed_size, 0, &cfg);
cfg.MergeMode = true;
io.Fonts->AddFontFromMemoryCompressedTTF(fa_compressed_data, fa_compressed_size, iconSize, &cfg, fa_range);
if (config.Data.Font.Path.empty())
io.Fonts->AddFontFromMemoryCompressedTTF(unifont_compressed_data, unifont_compressed_size, 0, &cfg, font_range);
else
io.Fonts->AddFontFromFileTTF(config.Data.Font.Path.c_str(), 0, &cfg, font_range);

io.Fonts->AddFontFromMemoryCompressedTTF(source_code_pro_compressed_data, source_code_pro_compressed_size, fontSize);

io.Fonts->Build();
}

Expand Down

0 comments on commit 27a9782

Please sign in to comment.