CacheGhost
CacheGhost

Reputation: 199

Embed font larger than char size in ImGui

Im developing translation on little script that uses ImGui as frontend. I need extended set of unicode characters to be available in font that will be used. Since this script is injecting via DLL there's no way (I think so. I have no experience with c++ at all.) to use:

io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels);

Adding font from ttf file resulted in error that data == NULL;

void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0);
if (!data)
{
    IM_ASSERT(0); // Could not load file.
    return NULL;
}

I've also tried to use io.Fonts->AddFontFromMemoryCompressedBase85TTF and compiling font by included binary_to_compressed_c but output is so big that I'm getting:

fatal error C1091: compiler limit: string exceeds 65535 bytes in length

But function is not accepting any types except char*. I was connecting chars into string and then re-assemble it by str() and c_str() but app was crashing after injection. Here is function handling base85 conversion from ImGui:

ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
{
    int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4;
    void* compressed_ttf = ImGui::MemAlloc((size_t)compressed_ttf_size);
    Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf);
    ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);
    ImGui::MemFree(compressed_ttf);
    return font;
}

How I can fix this problem ? I've tried everything and nothing is working. Only passing smaller chars into compile function is working (Tried with bundled Cousine_Regular.ttf).

Upvotes: 1

Views: 1929

Answers (1)

CacheGhost
CacheGhost

Reputation: 199

I've found workaround this problem. If you really need to use BASE85 there's still no answer but you can increase your size limit by converting to int type (Dont put -base85 in binary_to_compressed_c.exe) then insert resulting table to header file and use instrucions provided by ImGui like so:

Header file:

// File: 'DroidSans.ttf' (190044 bytes)
// Exported using binary_to_compressed_c.cpp
static const unsigned int droid_compressed_size = 134345;
static const unsigned int droid_compressed_data[134348 / 4] =

Your import / render file:

static const ImWchar ranges[] = { 0x0020, 0x00FF, 0x0100, 0x017F, 0 };
//Because I need extended characters im passing my array to function.

io.Fonts->AddFontFromMemoryCompressedTTF(droid_compressed_data, droid_compressed_size, 16.0f, NULL, ranges);

That's getting rid of the problem about converting from string to char and other stuff related to base85 importing.

Upvotes: 0

Related Questions