Skip to content

Commit 3aca446

Browse files
committed
Added ImFontAtlas::AddFontFromMemoryCompressedBase85TTF()
1 parent da3baeb commit 3aca446

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

imgui.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9895,33 +9895,26 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
98959895
return Fonts.back();
98969896
}
98979897

9898+
// Default font ttf is compressed with stb_compress then base85 encoded (see extra_fonts/binary_to_compressed_c.cpp for encoder)
98989899
static unsigned int stb_decompress_length(unsigned char *input);
98999900
static unsigned int stb_decompress(unsigned char *output, unsigned char *i, unsigned int length);
9900-
9901-
// Default font ttf is compressed with stb_compress then base85 encoded (see extra_fonts/binary_to_compressed_c.cpp for encoder)
99029901
static const char* GetDefaultCompressedFontDataTTFBase85();
99039902
static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; }
99049903
static void Decode85(const unsigned char* src, unsigned int* dst) { for (; *src; src += 5) *dst++ = Decode85Byte(src[0]) + 85*(Decode85Byte(src[1]) + 85*(Decode85Byte(src[2]) + 85*(Decode85Byte(src[3]) + 85*Decode85Byte(src[4])))); }
99059904

99069905
// Load embedded ProggyClean.ttf at size 13
99079906
ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
99089907
{
9909-
const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85();
9910-
int ttf_compressed_size = (((int)strlen(ttf_compressed_base85) + 4) / 5) * 4;
9911-
void* ttf_compressed = ImGui::MemAlloc(ttf_compressed_size);
9912-
Decode85((const unsigned char*)ttf_compressed_base85, (unsigned int*)ttf_compressed);
9913-
9914-
ImFontConfig font_cfg;
9915-
if (font_cfg_template)
9916-
font_cfg = *font_cfg_template;
9917-
else
9908+
ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
9909+
if (!font_cfg_template)
99189910
{
99199911
font_cfg.OversampleH = font_cfg.OversampleV = 1;
99209912
font_cfg.PixelSnapH = true;
99219913
}
99229914
if (font_cfg.Name[0] == '\0') strcpy(font_cfg.Name, "<default>");
9923-
ImFont* font = AddFontFromMemoryCompressedTTF(ttf_compressed, ttf_compressed_size, 13.0f, &font_cfg, GetGlyphRangesDefault());
9924-
ImGui::MemFree(ttf_compressed);
9915+
9916+
const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85();
9917+
ImFont* font = AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, 13.0f, &font_cfg, GetGlyphRangesDefault());
99259918
return font;
99269919
}
99279920

@@ -9969,6 +9962,16 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d
99699962
return AddFontFromMemoryTTF(buf_decompressed_data, (int)buf_decompressed_size, size_pixels, font_cfg_template, glyph_ranges);
99709963
}
99719964

9965+
ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
9966+
{
9967+
int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4;
9968+
void* compressed_ttf = ImGui::MemAlloc(compressed_ttf_size);
9969+
Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned int*)compressed_ttf);
9970+
ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);
9971+
ImGui::MemFree(compressed_ttf);
9972+
return font;
9973+
}
9974+
99729975
bool ImFontAtlas::Build()
99739976
{
99749977
IM_ASSERT(ConfigData.Size > 0);

imgui.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,8 @@ struct ImFontAtlas
11551155
IMGUI_API ImFont* AddFontDefault(const ImFontConfig* font_cfg = NULL);
11561156
IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL);
11571157
IMGUI_API ImFont* AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Transfer ownership of 'ttf_data' to ImFontAtlas, will be deleted after Build()
1158-
IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_ttf_data' untouched and still owned by caller. Compress with binary_to_compressed_c.cpp
1158+
IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_ttf_data' still owned by caller. Compress with binary_to_compressed_c.cpp
1159+
IMGUI_API ImFont* AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_ttf_data_base85' still owned by caller. Compress with binary_to_compressed_c.cpp with -base85 paramaeter
11591160
IMGUI_API void ClearTexData(); // Clear the CPU-side texture data. Saves RAM once the texture has been copied to graphics memory.
11601161
IMGUI_API void ClearInputData(); // Clear the input TTF data (inc sizes, glyph ranges)
11611162
IMGUI_API void ClearFonts(); // Clear the ImGui-side font data (glyphs storage, UV coordinates)

0 commit comments

Comments
 (0)