GUI: add a compressed font cache
reduce memory usage by not loading the same font twice
This commit is contained in:
parent
c75f363453
commit
fb35e6f993
|
@ -38,9 +38,21 @@ struct InflateBlock {
|
|||
};
|
||||
|
||||
ImFont* FurnaceGUI::addFontZlib(const void* data, size_t len, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) {
|
||||
// find font in cache
|
||||
logV("addFontZlib...");
|
||||
for (FurnaceGUIUncompFont& i: fontCache) {
|
||||
if (i.origPtr==data && i.origLen==len) {
|
||||
logV("found in cache");
|
||||
ImFontConfig fontConfig=(font_cfg==NULL)?ImFontConfig():(*font_cfg);
|
||||
fontConfig.FontDataOwnedByAtlas=false;
|
||||
|
||||
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(i.data,i.len,size_pixels,&fontConfig,glyph_ranges);
|
||||
}
|
||||
}
|
||||
|
||||
z_stream zl;
|
||||
memset(&zl,0,sizeof(z_stream));
|
||||
logV("addFontZlib...");
|
||||
logV("not found in cache - decompressing...");
|
||||
|
||||
zl.avail_in=len;
|
||||
zl.next_in=(Bytef*)data;
|
||||
|
@ -116,10 +128,11 @@ ImFont* FurnaceGUI::addFontZlib(const void* data, size_t len, float size_pixels,
|
|||
delete i;
|
||||
}
|
||||
blocks.clear();
|
||||
len=finalSize;
|
||||
|
||||
fontCache.push_back(FurnaceGUIUncompFont(data,len,finalData,finalSize));
|
||||
|
||||
ImFontConfig fontConfig=(font_cfg==NULL)?ImFontConfig():(*font_cfg);
|
||||
fontConfig.FontDataOwnedByAtlas=true;
|
||||
fontConfig.FontDataOwnedByAtlas=false;
|
||||
|
||||
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(finalData,finalSize,size_pixels,&fontConfig,glyph_ranges);
|
||||
}
|
||||
|
|
|
@ -1546,6 +1546,18 @@ struct FurnaceGUIPerfMetric {
|
|||
elapsed(0) {}
|
||||
};
|
||||
|
||||
struct FurnaceGUIUncompFont {
|
||||
const void* origPtr;
|
||||
size_t origLen;
|
||||
void* data;
|
||||
size_t len;
|
||||
FurnaceGUIUncompFont(const void* ptr, size_t len, void* d, size_t l):
|
||||
origPtr(ptr),
|
||||
origLen(len),
|
||||
data(d),
|
||||
len(l) {}
|
||||
};
|
||||
|
||||
struct FurnaceGUIBackupEntry {
|
||||
String name;
|
||||
uint64_t size;
|
||||
|
@ -1778,6 +1790,7 @@ class FurnaceGUI {
|
|||
MIDIMap midiMap;
|
||||
int learning;
|
||||
|
||||
std::vector<FurnaceGUIUncompFont> fontCache;
|
||||
ImFont* mainFont;
|
||||
ImFont* iconFont;
|
||||
ImFont* furIconFont;
|
||||
|
|
Loading…
Reference in a new issue