Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chinese font problem #1434

Closed
ghost opened this issue Nov 14, 2017 · 6 comments
Closed

Chinese font problem #1434

ghost opened this issue Nov 14, 2017 · 6 comments

Comments

@ghost
Copy link

ghost commented Nov 14, 2017

ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\simhei.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesChinese());

This takes up a lot of memory(300MB)
it just a font...
tim 20171114085133

@ratchetfreak
Copy link

The font file is 9 MB. Which is compressed. To make efficient use out of the font it needs to be decompressed.

IMO it is not unsurprising that a font file converted to usable data structure explodes 30-fold especially when the render method is bitmap based.

@ocornut
Copy link
Owner

ocornut commented Nov 14, 2017

With the current system of baking full ranges into a texture atlas, if I load a test font at 18 px size with full Chinese range + default oversampling setting, I get a 4096x4096 texture. You can see it in the Demo Window under Windows Options > Font.

A 4096x4096 texture in RGBA with will be 64 MB of RAM + Alpha texture currently stays stored that's 16 MB. Total 80 MB. It's way too much but it's not 300 MB.
(will fix the fact that we keep the Alpha texture stored)

image

The four solutions are:

  • You can reduce the oversampling.
    ImFontConfig cfg;
    cfg.OversampleH = cfg.OversampleV = 1;
    ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Omar\\WB\\work\\game\\data\\font\\NotoSansCJKsc-Medium.otf", 18.0f, &cfg, io.Fonts->GetGlyphRangesChinese());

Texture becomes 4096x1024 so 4 MB alpha and 16 MB RGBA.

  • You can request the Alpha (not RGBA) texture and upload that to your GPU. This is the recommended thing and will divide your GPU memory consumption by 4 and CPU ram by 5.

  • You can reduce the glyphs range by computing your own ranges for the characters you actually need to display (this work if you text is known). This also will be much faster to build.
    The ImFontAtlas::GlyphRangesBuilder class is a helper to build ranges given input tetxt.

  • You can wait until the atlas system gets support for dynamically adding characters as they are needed (probably not before several months).

@ghost
Copy link
Author

ghost commented Nov 15, 2017

@ocornut i use 1nd solution, thanks for answers

@ghost
Copy link
Author

ghost commented Nov 15, 2017

@ratchetfreak how to do i uncompress it?

@ghost ghost closed this as completed Nov 15, 2017
@ratchetfreak
Copy link

@jingjinghack that's what AddFontFromFileTTF does, it decompresses the file into a usable format and then adds the required data structures to the text renderer.

@ocornut
Copy link
Owner

ocornut commented Mar 6, 2025

FYI, work on dynamic_fonts branch aimed to be merged in 1.92 (#8465) should solve this, as glyphs are rasterized and packed on demand when they are used.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants