| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "gui.h" |
| 21 |
#include <zlib.h> |
| 22 |
|
| 23 |
#define FONT_READ_SIZE 262144 |
| 24 |
|
| 25 |
struct InflateBlock { |
| 26 |
unsigned char* buf; |
| 27 |
size_t len; |
| 28 |
size_t blockSize; |
| 29 |
InflateBlock(size_t s) { |
| 30 |
buf=new unsigned char[s]; |
| 31 |
len=s; |
| 32 |
blockSize=0; |
| 33 |
} |
| 34 |
~InflateBlock() { |
| 35 |
delete[] buf; |
| 36 |
len=0; |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
ImFont* FurnaceGUI::addFontZlib(const void* data, size_t len, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) { |
| 41 |
// find font in cache |
| 42 |
logV("addFontZlib..."); |
| 43 |
for (FurnaceGUIUncompFont& i: fontCache) { |
| 44 |
if (i.origPtr==data && i.origLen==len) { |
| 45 |
logV("found in cache"); |
| 46 |
ImFontConfig fontConfig=(font_cfg==NULL)?ImFontConfig():(*font_cfg); |
| 47 |
fontConfig.FontDataOwnedByAtlas=false; |
| 48 |
|
| 49 |
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(i.data,i.len,size_pixels,&fontConfig,glyph_ranges); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
z_stream zl; |
| 54 |
memset(&zl,0,sizeof(z_stream)); |
| 55 |
logV("not found in cache - decompressing..."); |
| 56 |
|
| 57 |
zl.avail_in=len; |
| 58 |
zl.next_in=(Bytef*)data; |
| 59 |
zl.zalloc=NULL; |
| 60 |
zl.zfree=NULL; |
| 61 |
zl.opaque=NULL; |
| 62 |
|
| 63 |
int nextErr; |
| 64 |
nextErr=inflateInit(&zl); |
| 65 |
if (nextErr!=Z_OK) { |
| 66 |
if (zl.msg==NULL) { |
| 67 |
logD("zlib error: unknown! %d",nextErr); |
| 68 |
} else { |
| 69 |
logD("zlib error: %s",zl.msg); |
| 70 |
} |
| 71 |
inflateEnd(&zl); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
std::vector<InflateBlock*> blocks; |
| 76 |
while (true) { |
| 77 |
InflateBlock* ib=new InflateBlock(FONT_READ_SIZE); |
| 78 |
zl.next_out=ib->buf; |
| 79 |
zl.avail_out=ib->len; |
| 80 |
|
| 81 |
nextErr=inflate(&zl,Z_SYNC_FLUSH); |
| 82 |
if (nextErr!=Z_OK && nextErr!=Z_STREAM_END) { |
| 83 |
if (zl.msg==NULL) { |
| 84 |
logD("zlib error: unknown error! %d",nextErr); |
| 85 |
} else { |
| 86 |
logD("zlib inflate: %s",zl.msg); |
| 87 |
} |
| 88 |
for (InflateBlock* i: blocks) delete i; |
| 89 |
blocks.clear(); |
| 90 |
delete ib; |
| 91 |
inflateEnd(&zl); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
ib->blockSize=ib->len-zl.avail_out; |
| 95 |
blocks.push_back(ib); |
| 96 |
if (nextErr==Z_STREAM_END) { |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
nextErr=inflateEnd(&zl); |
| 101 |
if (nextErr!=Z_OK) { |
| 102 |
if (zl.msg==NULL) { |
| 103 |
logD("zlib end error: unknown error! %d",nextErr); |
| 104 |
} else { |
| 105 |
logD("zlib end: %s",zl.msg); |
| 106 |
} |
| 107 |
for (InflateBlock* i: blocks) delete i; |
| 108 |
blocks.clear(); |
| 109 |
return NULL; |
| 110 |
} |
| 111 |
|
| 112 |
size_t finalSize=0; |
| 113 |
size_t curSeek=0; |
| 114 |
for (InflateBlock* i: blocks) { |
| 115 |
finalSize+=i->blockSize; |
| 116 |
} |
| 117 |
if (finalSize<1) { |
| 118 |
logD("compressed too small!"); |
| 119 |
lastError="file too small"; |
| 120 |
for (InflateBlock* i: blocks) delete i; |
| 121 |
blocks.clear(); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
unsigned char* finalData=(unsigned char*)malloc(finalSize); |
| 125 |
for (InflateBlock* i: blocks) { |
| 126 |
memcpy(&finalData[curSeek],i->buf,i->blockSize); |
| 127 |
curSeek+=i->blockSize; |
| 128 |
delete i; |
| 129 |
} |
| 130 |
blocks.clear(); |
| 131 |
|
| 132 |
fontCache.push_back(FurnaceGUIUncompFont(data,len,finalData,finalSize)); |
| 133 |
|
| 134 |
ImFontConfig fontConfig=(font_cfg==NULL)?ImFontConfig():(*font_cfg); |
| 135 |
fontConfig.FontDataOwnedByAtlas=false; |
| 136 |
|
| 137 |
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(finalData,finalSize,size_pixels,&fontConfig,glyph_ranges); |
| 138 |
} |