From 7ff41a78f956cf3388db9e0c8edfa1f0257e5c1f Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 15 Feb 2022 22:16:55 +0300 Subject: [PATCH] Fonts: binary_to_compressed_c.cpp gained -nostatic option (#5021) This allow to use added variables in other source files via extern declaration. --- docs/CHANGELOG.txt | 1 + misc/fonts/binary_to_compressed_c.cpp | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index ce2ff4dcc..8b1fadd35 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -41,6 +41,7 @@ Other Changes: - Stack Tool: Added option to copy item path to clipboard. (#4631) - Misc: Added constexpr to ImVec2/ImVec4 inline constructors. (#4995) [@Myriachan] +- Misc: binary_to_compressed_c tool: Added -nostatic option. (#5021) [@podsvirov] - ImVector: Fixed erase() with empty range. (#5009) [@thedmd] - Drawlist: Fixed PathArcTo() emitting terminating vertices too close to arc vertices. (#4993) [@thedmd] diff --git a/misc/fonts/binary_to_compressed_c.cpp b/misc/fonts/binary_to_compressed_c.cpp index 441c8f67c..284f039a2 100644 --- a/misc/fonts/binary_to_compressed_c.cpp +++ b/misc/fonts/binary_to_compressed_c.cpp @@ -15,7 +15,7 @@ // You can also find a precompiled Windows binary in the binary/demo package available from https://github.com/ocornut/imgui // Usage: -// binary_to_compressed_c.exe [-base85] [-nocompress] +// binary_to_compressed_c.exe [-base85] [-nocompress] [-nostatic] // Usage example: // # binary_to_compressed_c.exe myfont.ttf MyFont > myfont.cpp // # binary_to_compressed_c.exe -base85 myfont.ttf MyFont > myfont.cpp @@ -31,23 +31,25 @@ 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, bool use_base85_encoding, bool use_compression); +static bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static); int main(int argc, char** argv) { if (argc < 3) { - printf("Syntax: %s [-base85] [-nocompress] \n", argv[0]); + printf("Syntax: %s [-base85] [-nocompress] [-nostatic] \n", argv[0]); return 0; } int argn = 1; bool use_base85_encoding = false; bool use_compression = true; - if (argv[argn][0] == '-') + bool use_static = true; + while (argn < (argc - 2) && argv[argn][0] == '-') { if (strcmp(argv[argn], "-base85") == 0) { use_base85_encoding = true; argn++; } else if (strcmp(argv[argn], "-nocompress") == 0) { use_compression = false; argn++; } + else if (strcmp(argv[argn], "-nostatic") == 0) { use_static = false; argn++; } else { fprintf(stderr, "Unknown argument: '%s'\n", argv[argn]); @@ -55,7 +57,7 @@ int main(int argc, char** argv) } } - bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], use_base85_encoding, use_compression); + bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], use_base85_encoding, use_compression, use_static); if (!ret) fprintf(stderr, "Error opening or reading file: '%s'\n", argv[argn]); return ret ? 0 : 1; @@ -67,7 +69,7 @@ char Encode85Byte(unsigned int x) return (x >= '\\') ? x + 1 : x; } -bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression) +bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static) { // Read file FILE* f = fopen(filename, "rb"); @@ -90,10 +92,11 @@ bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_b FILE* out = stdout; fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz); fprintf(out, "// Exported using binary_to_compressed_c.cpp\n"); + const char* static_str = use_static ? "static " : ""; const char* compressed_str = use_compression ? "compressed_" : ""; if (use_base85_encoding) { - fprintf(out, "static const char %s_%sdata_base85[%d+1] =\n \"", symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5); + fprintf(out, "%sconst char %s_%sdata_base85[%d+1] =\n \"", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5); char prev_c = 0; for (int src_i = 0; src_i < compressed_sz; src_i += 4) { @@ -112,8 +115,8 @@ bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_b } else { - fprintf(out, "static const unsigned int %s_%ssize = %d;\n", symbol, compressed_str, (int)compressed_sz); - fprintf(out, "static const unsigned int %s_%sdata[%d/4] =\n{", symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4); + fprintf(out, "%sconst unsigned int %s_%ssize = %d;\n", static_str, symbol, compressed_str, (int)compressed_sz); + fprintf(out, "%sconst unsigned int %s_%sdata[%d/4] =\n{", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4); int column = 0; for (int i = 0; i < compressed_sz; i += 4) {