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

Fonts: Support for controling font rasterization density (aka. DPI) #6925

Closed
wants to merge 3 commits into from

Conversation

thedmd
Copy link
Contributor

@thedmd thedmd commented Oct 14, 2023

This is backport of an extension that adds an ability to control font rasterization density.

Rasterization density allow to increase size of the rendered glyphs aka. their size on the atlas texture while keeping metrics intact.

I found myself redoing this a few times now, so maybe people will find that useful too.

Earlier I was hijacking OversampleH/V for that purpose, but this time I went with adding separate variable that control this behavior.

// setup
ImFontConfig font_config;
font_config.OversampleH = 1; // FreeType does not support those, reset so stb_truetype will produce similar results
font_config.OversampleV = 1;

font_config.RasterizationDensity = 1.0f;
auto font_a = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f, &font_config);
font_a->Scale = 1.0f;

font_config.RasterizationDensity = 2.0f;
auto font_b = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f, &font_config);
font_b->Scale = 2.0f; // set font scale to match glyphs on atlas 1:1

font_config.RasterizationDensity = 4.0f;
auto font_c = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f, &font_config);
font_c->Scale = 4.0f;

// in loop
ImGui::Text("Font: 100%");

ImGui::PushFont(font_b);
ImGui::Text("Font: 200%");
ImGui::PopFont();

ImGui::PushFont(font_c);
ImGui::Text("Font: 400%");
ImGui::PopFont();

Works with both FreeType and built-in stb_truetype atlas generators.

FreeType stb_truetype
image image

This does aid with on form of DPI support when one utilize io.DisplayFramebufferScale, seting same scalle while regenerating font atlas in ImFontConfig remove need to touch global font scale and/or individual font scale.

@thedmd
Copy link
Contributor Author

thedmd commented Oct 20, 2023

Rebased on master, which does compile on Linux :)

@ocornut
Copy link
Owner

ocornut commented Nov 13, 2023

Merged as abfb926, ed29ff0, ade4d0e, along with minor amends 46843b6
Renamed to RasterizerDensity to match existing field.

One of the linked thread (thedmd/imgui-node-editor#261) gives an explanation as to how this is useful for e.g. smooth zooming:

Back to solution I use. I generate two fonts, one with 100% scaling, one with 400% scalling. I pick default font based on how much canvas is zoomed in. When it is zoomed-in > 200% I use 400% font. Otherwise I use 100%.

If you increase this it is expected that you increase font scale accordingly (directly or indirectly), otherwise quality may look lowered.

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants