Skip to content

Commit

Permalink
FontLoadingParams: use std::vector for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 3, 2024
1 parent 5bf521c commit d8156d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/hello_imgui/hello_imgui_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
#include "misc/freetype/imgui_freetype.h"
#endif



#ifdef IOS
#include "hello_imgui/internal/platform/getAppleBundleResourcePath.h"
#endif



// Patch ImGui font loading with sensible defaults
// - Do not require static
namespace ImGui_SensibleFont
{
static ImFontConfig* MakeStaticFontConfig(const ImFontConfig& font_cfg, const ImVector<ImWchar[2]> & glyph_ranges = {})
using ImWcharPair = std::array<ImWchar, 2>;

static ImFontConfig* MakeStaticFontConfig(const ImFontConfig& font_cfg, const ImVector<ImWcharPair> & glyph_ranges = {})
{
// Storage for pointers that will be used by ImGui::GetIO().Fonts->AddFontFromMemoryTTF
// (and which are required to persist during the application lifetime)
Expand Down Expand Up @@ -66,26 +67,26 @@ namespace ImGui_SensibleFont
return font_cfg_static;
}

ImFont* AddFontFromFileTTF(const char* filename, float font_size_pixels, const ImFontConfig& font_cfg = ImFontConfig(), const ImVector<ImWchar[2]> & glyph_ranges = {})
ImFont* AddFontFromFileTTF(const char* filename, float font_size_pixels, const ImFontConfig& font_cfg = ImFontConfig(), const ImVector<ImWcharPair> & glyph_ranges = {})
{
ImFontConfig* static_font_config = MakeStaticFontConfig(font_cfg, glyph_ranges);
static_font_config->FontDataOwnedByAtlas = false;
return ImGui::GetIO().Fonts->AddFontFromFileTTF(filename, font_size_pixels, static_font_config);
}

ImFont* AddFontFromMemoryTTF(void* font_data, int font_data_size, float font_size_pixels, const ImFontConfig& font_cfg = ImFontConfig(), const ImVector<ImWchar[2]> & glyph_ranges = {})
ImFont* AddFontFromMemoryTTF(void* font_data, int font_data_size, float font_size_pixels, const ImFontConfig& font_cfg = ImFontConfig(), const ImVector<ImWcharPair> & glyph_ranges = {})
{
ImFontConfig* static_font_config = MakeStaticFontConfig(font_cfg, glyph_ranges);
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(font_data, font_data_size, font_size_pixels, static_font_config);
}

ImFont* AddFontFromMemoryTTF_KeepOwnership(void* font_data, int font_data_size, float font_size_pixels, const ImFontConfig& font_cfg = ImFontConfig(), const ImVector<ImWchar[2]> & glyph_ranges = {})
ImFont* AddFontFromMemoryTTF_KeepOwnership(void* font_data, int font_data_size, float font_size_pixels, const ImFontConfig& font_cfg = ImFontConfig(), const ImVector<ImWcharPair> & glyph_ranges = {})
{
ImFontConfig* static_font_config = MakeStaticFontConfig(font_cfg, glyph_ranges);
static_font_config->FontDataOwnedByAtlas = false;
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(font_data, font_data_size, font_size_pixels, static_font_config);
}
}
} // namespace ImGui_SensibleFont


namespace HelloImGui
Expand Down Expand Up @@ -127,7 +128,6 @@ namespace HelloImGui
}



ImFont* LoadFont(const std::string & fontFilename, float fontSize_, const FontLoadingParams& params_)
{
gDidCallHelloImGuiLoadFontTTF = true;
Expand Down Expand Up @@ -163,17 +163,22 @@ namespace HelloImGui
params.fontConfig.MergeMode = params.mergeToLastFont;

ImFont* font = nullptr;

ImVector<ImWcharPair> glyphRangesImVector;
for(const auto& v: params.glyphRanges)
glyphRangesImVector.push_back(v);

if (params.insideAssets)
{
AssetFileData fontData = LoadAssetFileData(fontFilename.c_str());
font = ImGui_SensibleFont::AddFontFromMemoryTTF_KeepOwnership(
fontData.data, fontData.dataSize, fontSize, params.fontConfig, params.glyphRanges);
fontData.data, fontData.dataSize, fontSize, params.fontConfig, glyphRangesImVector);
FreeAssetFileData(&fontData);
}
else
{
font = ImGui_SensibleFont::AddFontFromFileTTF(
fontFilename.c_str(), fontSize, params.fontConfig, params.glyphRanges);
fontFilename.c_str(), fontSize, params.fontConfig, glyphRangesImVector);
}

if (params.mergeFontAwesome)
Expand Down
6 changes: 5 additions & 1 deletion src/hello_imgui/hello_imgui_font.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#pragma once
#include "imgui.h"
#include <string>
#include <vector>
#include <array>

namespace HelloImGui
{
using ImWcharPair = std::array<ImWchar, 2>;

// Font loading parameters: several options are available (color, merging, range, ...)
struct FontLoadingParams
{
Expand All @@ -29,7 +33,7 @@ namespace HelloImGui
// - if empty, the default glyph range will be used
// - you can specify several ranges
// (will be translated and stored as a static ImWChar* inside fontConfig)
ImVector<ImWchar[2]> glyphRanges = {};
std::vector<ImWcharPair> glyphRanges = {};

// ImGui native font config to use
ImFontConfig fontConfig = ImFontConfig();
Expand Down

0 comments on commit d8156d4

Please sign in to comment.