Skip to content

Commit

Permalink
Add dpi params: fontOversampleH / fontOversampleV + add hello_imgui_e…
Browse files Browse the repository at this point in the history
…xample.ini
  • Loading branch information
pthom committed Jul 6, 2024
1 parent 876875c commit 19af0b4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 14 deletions.
58 changes: 58 additions & 0 deletions hello_imgui_example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
; This is an example configuration file for hello_imgui
; To use it:
; - Rename this file to hello_imgui.ini
; - Edit the values to match your needs
; - Place it in the current working directory of your application
; - Or any parent directory of the current working directory, to apply the settings to multiple applications.



[DpiAwareParams]
; - `dpiWindowSizeFactor`:factor by which window size should be multiplied
; - `fontRenderingScale`:
; factor by which fonts glyphs should be scaled at rendering time
; (typically 1 on windows, and 0.5 on macOS retina screens)
;
; By default, Hello ImGui will compute them automatically
;dpiWindowSizeFactor=2
;fontRenderingScale=0.5


; `fontOversampleH` and `fontOversampleV` : Font oversampling parameters
; Rasterize at higher quality for sub-pixel positioning. Probably unused if freeType is used.
; If not zero, these values will be used to set the oversampling factor when loading fonts.
; (i.e. they will be set in ImFontConfig::OversampleH and ImFontConfig::OversampleV)
; fontOversampleH: The difference between 2 and 3 for OversampleH is minimal.
; You can reduce this to 1 for large glyphs save memory.
; fontOversampleV: This is not really useful as we don't use sub-pixel positions on the Y axis.
; fontOversampleH=2
; fontOversampleV=1



[OpenGlOptions]
; OpenGlOptions contains advanced options used at the startup of OpenGL.
; These parameters are reserved for advanced users.
; By default, Hello ImGui will select reasonable default values, and these parameters are not used.
; Use at your own risk, as they make break the multi-platform compatibility of your application!
; All these parameters are platform dependent.
; For real multiplatform examples, see
; hello_imgui/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp
; and
; hello_imgui/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp

; GlslVersion = 130
; MajorVersion = 3
; MinorVersion = 2
; UseCoreProfile = true
; UseForwardCompat = false

; `AntiAliasingSamples`
; If not zero, this value will be used to reduce the number of samples used for anti-aliasing.
; This is used only when running with Glfw + OpenGL (which is the default)
; Notes:
; - by default, we query the maximum number of samples supported by the hardwareand we use this value
; - if you set this value to a non-zero value, it will be used instead of the maximum value
; (except if it is greater than the maximum value, in which case a warning will be issued)
; (it will be via `glfwWindowHint(GLFW_SAMPLES, AntiAliasingSamples);`)
; AntiAliasingSamples=16
39 changes: 26 additions & 13 deletions src/hello_imgui/dpi_aware.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace HelloImGui

//
// Hello ImGui will try its best to automatically handle DPI scaling for you.
// It does this by setting two values:
//
// Parameters to change the scaling behavior:
// ------------------------------------------
// - `dpiWindowSizeFactor`:
// factor by which window size should be multiplied
//
Expand All @@ -19,22 +20,22 @@ namespace HelloImGui
// By default, Hello ImGui will compute them automatically,
// when dpiWindowSizeFactor and fontRenderingScale are set to 0.
//
// Parameters to improve font rendering quality:
// ---------------------------------------------
// - `fontOversampleH` and `fontOversampleV` : Font oversampling parameters
// Rasterize at higher quality for sub-pixel positioning. Probably unused if freeType is used.
// If not zero, these values will be used to set the oversampling factor when loading fonts.
//
//
// How to set those values manually:
// ---------------------------------
// If it fails (i.e. your window and/or fonts are too big or too small),
// you may set them manually:
// (1) Either by setting them programmatically in your application
// (set their values in `runnerParams.dpiAwareParams`)
// (2) Either by setting them in a `hello_imgui.ini` file in the current folder, or any of its parent folders.
// (this is useful when you want to set them for a specific app or set of apps, without modifying the app code)
// (2) Either by setting them in a `hello_imgui.ini` file. See hello_imgui/hello_imgui_example.ini for more info
// Note: if several methods are used, the order of priority is (1) > (2)
//
// Example content of a ini file:
// ------------------------------
// [DpiAwareParams]
// dpiWindowSizeFactor=2
// fontRenderingScale=0.5
//
// For more information, see the documentation on DPI handling, here: https://pthom.github.io/hello_imgui/book/doc_api.html#handling-screens-with-high-dpi
//
struct DpiAwareParams
Expand Down Expand Up @@ -68,10 +69,22 @@ struct DpiAwareParams
// (This parameter will be used to set ImGui::GetIO().FontGlobalScale at startup)
float fontRenderingScale = 0.0f;

// `onlyUseFontDpiResponsive`
// If true, guarantees that only HelloImGui::LoadDpiResponsiveFont will be used to load fonts.
// (also for the default font)
bool onlyUseFontDpiResponsive = false;
// `onlyUseFontDpiResponsive`
// If true, guarantees that only HelloImGui::LoadDpiResponsiveFont will be used to load fonts.
// (also for the default font)
bool onlyUseFontDpiResponsive = false;

// `fontOversampleH` and `fontOversampleV` : Font oversampling parameters
// Rasterize at higher quality for sub-pixel positioning. Probably unused if freeType is used.
// If not zero, these values will be used to set the oversampling factor when loading fonts.
// (i.e. they will be set in ImFontConfig::OversampleH and ImFontConfig::OversampleV)
// OversampleH: The difference between 2 and 3 for OversampleH is minimal.
// You can reduce this to 1 for large glyphs save memory.
// OversampleV: This is not really useful as we don't use sub-pixel positions on the Y axis.
// Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details.
int fontOversampleH = 0; // Default is 2 in ImFontConfig
int fontOversampleV = 0; // Default is 1 in ImFontConfig


// `dpiFontLoadingFactor`
// factor by which font size should be multiplied at loading time to get a similar
Expand Down
11 changes: 10 additions & 1 deletion src/hello_imgui/impl/hello_imgui_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ namespace HelloImGui

FontLoadingParams params = params_;

// Font oversampling (set by dpiAwareParams)
{
const auto& dpiAwareParams = HelloImGui::GetRunnerParams()->dpiAwareParams;
if (dpiAwareParams.fontOversampleH > 0)
params.fontConfig.OversampleH = dpiAwareParams.fontOversampleH;
if (dpiAwareParams.fontOversampleV > 0)
params.fontConfig.OversampleV = dpiAwareParams.fontOversampleV;
}

float fontSize = fontSize_;
if (params.adjustSizeToDpi)
fontSize *= HelloImGui::DpiFontLoadingFactor();
Expand Down Expand Up @@ -271,7 +280,7 @@ namespace HelloImGui
}


ImFont* LoadFontTTF(const std::string & fontFilename, float fontSize, bool useFullGlyphRange, ImFontConfig config)
ImFont* LoadFontTTF(const std::string & fontFilename, float fontSize, bool useFullGlyphRange, ImFontConfig config)
{
FontLoadingParams fontLoadingParams;
if (useFullGlyphRange)
Expand Down
12 changes: 12 additions & 0 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,19 @@ void ReadDpiAwareParams(DpiAwareParams* dpiAwareParams)
auto fontRenderingScale = HelloImGuiIniAnyParentFolder::readFloatValue("DpiAwareParams", "fontRenderingScale");
if (fontRenderingScale.has_value())
dpiAwareParams->fontRenderingScale = fontRenderingScale.value();
}

if (dpiAwareParams->fontOversampleH == 0)
{
auto fontOversampleH = HelloImGuiIniAnyParentFolder::readIntValue("DpiAwareParams", "fontOversampleH");
if (fontOversampleH.has_value())
dpiAwareParams->fontOversampleH = fontOversampleH.value();
}
if (dpiAwareParams->fontOversampleV == 0)
{
auto fontOversampleV = HelloImGuiIniAnyParentFolder::readIntValue("DpiAwareParams", "fontOversampleV");
if (fontOversampleV.has_value())
dpiAwareParams->fontOversampleV = fontOversampleV.value();
}
}

Expand Down

0 comments on commit 19af0b4

Please sign in to comment.