Skip to content

Commit

Permalink
Add TranslateCommonGlyphRanges
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jul 6, 2024
1 parent c6abee0 commit c8aad88
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/hello_imgui/doc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ See [hello_imgui_font.h](https://github.com/pthom/hello_imgui/blob/master/src/he
// Otherwise, it will be loaded from the filesystem
bool insideAssets = true;
// the ranges of glyphs to load:
// the ranges of glyphs to load, as a list of pairs of ImWchar
// - if empty, the default glyph range will be used
// - you can specify several ranges
// - intervals bounds are inclusive
// (will be translated and stored as a static ImWChar* inside fontConfig)
// Note: in order to use common ranges defined by ImGui (GetGlyphRangesJapanese, GetGlyphRangesChinese, ...)
// use TranslateCommonGlyphRanges (or translate_common_glyph_ranges in Python)
std::vector<ImWcharPair> glyphRanges = {};
// ImGui native font config to use
Expand Down
9 changes: 7 additions & 2 deletions src/hello_imgui/hello_imgui_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace HelloImGui
{
using ImWcharPair = std::array<ImWchar, 2>;

// Utility to translate DearImGui common Unicode ranges to ImWcharPair
// (GetGlyphRangesJapanese, GetGlyphRangesChinese, GetGlyphRangesCyrillic, ...)
std::vector<ImWcharPair> TranslateCommonGlyphRanges(const std::vector<ImWchar> & glyphRanges);

// @@md#Fonts

// When loading fonts, use
Expand Down Expand Up @@ -44,11 +48,12 @@ namespace HelloImGui
// Otherwise, it will be loaded from the filesystem
bool insideAssets = true;

// the ranges of glyphs to load:
// the ranges of glyphs to load, as a list of pairs of ImWchar
// - if empty, the default glyph range will be used
// - you can specify several ranges
// - intervals bounds are inclusive
// (will be translated and stored as a static ImWChar* inside fontConfig)
// Note: in order to use common ranges defined by ImGui (GetGlyphRangesJapanese, GetGlyphRangesChinese, ...)
// use TranslateCommonGlyphRanges (or translate_common_glyph_ranges in Python)
std::vector<ImWcharPair> glyphRanges = {};

// ImGui native font config to use
Expand Down
13 changes: 13 additions & 0 deletions src/hello_imgui/impl/hello_imgui_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,17 @@ namespace HelloImGui
return HelloImGui::gDidCallHelloImGuiLoadFontTTF;
}

// Utility to translate DearImGui common Unicode ranges to ImWcharPair
// (GetGlyphRangesJapanese, GetGlyphRangesChinese, GetGlyphRangesCyrillic, ...)
std::vector<ImWcharPair> TranslateCommonGlyphRanges(const std::vector<ImWchar> & glyphRanges)
{
std::vector<ImWcharPair> glyphRangesPairs;
for (size_t i = 0; i < glyphRanges.size(); i += 2)
{
ImWcharPair glyphRangePair = { glyphRanges[i], glyphRanges[i + 1] };
glyphRangesPairs.push_back(glyphRangePair);
}
return glyphRangesPairs;
}

}

0 comments on commit c8aad88

Please sign in to comment.