mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-05 04:28:47 +02:00
Default font encoded as base85 saves ~100 lines / 26 KB of source code (from @mmalex)
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
// ImGui - binary_to_compressed_c.cpp
|
||||
// Helper tool to turn a file into a C array.
|
||||
// The data is first compressed with stb_compress() to reduce source code size a little.
|
||||
// The data is first compressed with stb_compress() to reduce source code size.
|
||||
// Then encoded in Base85 to fit in a string so we can fit roughly 4 bytes of compressed data into 5 bytes of source code (suggested by @mmalex)
|
||||
// (If we used 32-bits constants it would require take 11 bytes of source code to encode 4 bytes.)
|
||||
// Useful if you want to embed fonts into your code.
|
||||
// Note that the output array is likely to be bigger than the binary file..
|
||||
// Note that even with compression, the output array is likely to be bigger than the binary file..
|
||||
// Load compressed TTF fonts with ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF()
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
@ -11,28 +13,44 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
const int COLUMNS = 12;
|
||||
|
||||
// stb_compress* from stb.h - declaration
|
||||
typedef unsigned int stb_uint;
|
||||
typedef unsigned char stb_uchar;
|
||||
stb_uint stb_compress(stb_uchar *out,stb_uchar *in,stb_uint len);
|
||||
|
||||
static bool binary_to_compressed_c(const char* filename, const char* symbol);
|
||||
static bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 3)
|
||||
{
|
||||
printf("Syntax: %s <inputfile> <symbolname>\n", argv[0]);
|
||||
printf("Syntax: %s [-base85] <inputfile> <symbolname>\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
binary_to_compressed_c(argv[1], argv[2]);
|
||||
int argn = 1;
|
||||
bool use_base85_encoding = false;
|
||||
if (argv[argn][0] == '-')
|
||||
{
|
||||
if (strcmp(argv[argn], "-base85") == 0) { use_base85_encoding = true; argn++; }
|
||||
else
|
||||
{
|
||||
printf("Unknown argument: '%s'\n", argv[argn]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
binary_to_compressed_c(argv[argn], argv[argn+1], use_base85_encoding);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool binary_to_compressed_c(const char* filename, const char* symbol)
|
||||
char Encode85Byte(unsigned int x)
|
||||
{
|
||||
x = (x % 85) + 35;
|
||||
return (x>='\\') ? x+1 : x;
|
||||
}
|
||||
|
||||
bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding)
|
||||
{
|
||||
// Read file
|
||||
FILE* f = fopen(filename, "rb");
|
||||
@ -50,22 +68,38 @@ bool binary_to_compressed_c(const char* filename, const char* symbol)
|
||||
int compressed_sz = stb_compress((stb_uchar*)compressed, (stb_uchar*)data, data_sz);
|
||||
memset(compressed + compressed_sz, 0, maxlen - compressed_sz);
|
||||
|
||||
// Output
|
||||
// Output as Base85 encoded
|
||||
FILE* out = stdout;
|
||||
fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz);
|
||||
fprintf(out, "// Exported using binary_to_compressed_c\n");
|
||||
fprintf(out, "static const unsigned int %s_compressed_size = %d;\n", symbol, (int)compressed_sz);
|
||||
fprintf(out, "static const unsigned int %s_compressed_data[%d/4] =\n{", symbol, (int)((compressed_sz+3)/4)*4);
|
||||
int column = 0;
|
||||
for (int i = 0; i < compressed_sz; i += 4)
|
||||
fprintf(out, "// Exported using binary_to_compressed_c.cpp\n");
|
||||
if (use_base85_encoding)
|
||||
{
|
||||
unsigned int d = *(unsigned int*)(compressed + i);
|
||||
if ((column++ % COLUMNS) == 0)
|
||||
fprintf(out, "\n 0x%08x, ", d);
|
||||
else
|
||||
fprintf(out, "0x%08x, ", d);
|
||||
fprintf(out, "static const char %s_compressed_data_base85[%d+1] =\n \"", symbol, (int)((compressed_sz+3)/4)*5);
|
||||
int column = 0;
|
||||
for (int i = 0; i < compressed_sz; i += 4)
|
||||
{
|
||||
unsigned int d = *(unsigned int*)(compressed + i);
|
||||
fprintf(out, "%c%c%c%c%c", Encode85Byte(d), Encode85Byte(d/85), Encode85Byte(d/7225), Encode85Byte(d/614125), Encode85Byte(d/52200625));
|
||||
if ((i % 112) == 112-4)
|
||||
fprintf(out, "\"\n \"");
|
||||
}
|
||||
fprintf(out, "\";\n\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "static const unsigned int %s_compressed_size = %d;\n", symbol, (int)compressed_sz);
|
||||
fprintf(out, "static const unsigned int %s_compressed_data[%d/4] =\n{", symbol, (int)((compressed_sz+3)/4)*4);
|
||||
int column = 0;
|
||||
for (int i = 0; i < compressed_sz; i += 4)
|
||||
{
|
||||
unsigned int d = *(unsigned int*)(compressed + i);
|
||||
if ((column++ % 12) == 0)
|
||||
fprintf(out, "\n 0x%08x, ", d);
|
||||
else
|
||||
fprintf(out, "0x%08x, ", d);
|
||||
}
|
||||
fprintf(out, "\n};\n\n");
|
||||
}
|
||||
fprintf(out, "\n};\n\n");
|
||||
|
||||
// Cleanup
|
||||
delete[] data;
|
||||
|
Reference in New Issue
Block a user