Comments (2)
(1) Merged your fix as b475309.
(I also confirmed that the output for your specific case matches the alignment of when using imgui_freetype)
(2)
Bonus issue (related, but less important): a second inconvenience I ran into is merging with another font. Differences in ascent and descent to the merged into font will be visible unless compensated with a glyph offset. But that offset can be calculated only if the ascent of the other font is known in advance, since those metrics are not filled in before the atlas is done building. If the font is configurable by the user, this value is not known and I don't have a good solution yet. Building the atlas just to extract some metrics, discarding it again and then building it again with merged icons would work, but is far from a good solution. I need to think about this one a bit more or maybe discard custom font selection for users.
I haven't dug into this further but I wonder if merged fonts could be trying to align to e.g. descent line. I'll keep this issue in mind for reference. I think when we rewrite font building and packing we may reinvestigate this in more details.
Thank you for your investigation!
from imgui.
Made a pull request for the simple fix. Independent of that, I found a suitable solution for my second problem and put it into a wrapper function in my custom extensions:
#include <imgui.h>
#include <imgui_internal.h>
#include <imstb_truetype.h>
namespace ImGuiEx
{
bool GetOrCalculateFontMetrics( const ImFont* font, float* size, float* ascent, float* descent, float* offsetY )
{
if ( font->FontSize != 0.0f )
{
if ( size != nullptr )
*size = font->FontSize;
if ( ascent != nullptr )
*ascent = font->Ascent;
if ( descent != nullptr )
*descent = font->Descent;
}
else
{
if ( font->ConfigData == nullptr )
return false;
const ImFontConfig& config = *font->ConfigData;
if ( size != nullptr )
*size = config.SizePixels;
if ( ( ascent != nullptr ) || ( descent != nullptr ) )
{
const unsigned char* fontData = (const unsigned char*)config.FontData;
if ( fontData == nullptr )
return false;
const int fontOffset = stbtt_GetFontOffsetForIndex( fontData, config.FontNo );
stbtt_fontinfo fontInfo;
if ( !stbtt_InitFont( &fontInfo, (unsigned char*)font->ConfigData->FontData, fontOffset ) )
return false;
const float fontScale = stbtt_ScaleForPixelHeight( &fontInfo, config.SizePixels );
int unscaledAscent, unscaledDescent, unscaledLineGap;
stbtt_GetFontVMetrics( &fontInfo, &unscaledAscent, &unscaledDescent, &unscaledLineGap );
#if 0
// off by one calculation
if ( ascent != nullptr )
*ascent = ImTrunc( unscaledAscent * fontScale + ( ( unscaledAscent > 0.0f ) ? +1 : -1 ) );
if ( descent != nullptr )
*descent = ImTrunc( unscaledDescent * fontScale + ( ( unscaledDescent > 0.0f ) ? +1 : -1 ) );
#else
// fixed calculation
if ( ascent != nullptr )
*ascent = ImCeil( unscaledAscent * fontScale );
if ( descent != nullptr )
*descent = ImFloor( unscaledDescent * fontScale );
#endif
}
}
if ( offsetY != nullptr )
{
if ( font->ConfigData == nullptr )
return false;
*offsetY = font->ConfigData->GlyphOffset.y;
}
return true;
}
} // namespace ImGuiEx
Doesn't replicate too much code, only parses a little of the font data and will get me the values I need to merge my icon font with perfect pixel precision regardless of font or font size it is merged into. There is just a single #if
that must be set depending on whether the metrics fix has been applied or not. Maybe this is useful for someone else as well.
from imgui.
Related Issues (20)
- SDL3 SDL_KeyCode no longer an enum HOT 2
- Frame hover glitch when moving on adjacent item border, with SetNextItemAllowOverlap HOT 2
- Frame hover glitch when moving on overlapping item border, with SetNextItemAllowOverlap HOT 2
- Dummy does not respect SetNextItemAllowOverlap HOT 2
- Cancelling of touchpad double-tap dragging by keyboard gets lost HOT 4
- Edit ImGui's Window Title to add a button HOT 1
- HiDPI with Emscripten and GLFW
- ImGui not using the entire screen surface with Android NDK HOT 2
- Windows + V doesn't paste value using SDL HOT 4
- DragFloat: incorrect work of clamp value, when we set it by ctrl+click HOT 1
- Strange endian behaviour causing ImU32 colors to be flipped HOT 5
- How to know why glad failed to load. HOT 1
- Can't hide "Hide tab bar" button when window is in dock HOT 8
- implot breaks on IMGUI_DEFINE_MATH_OPERATORS HOT 4
- Dealing with the glfw window HOT 4
- Issue with ImGui_ImplMetal_Shutdown - Crashes inside ImGui_ImplMetal_DestroyFontsTexture HOT 2
- no payload received for std::string HOT 7
- OpenGL backend + OpenCascades: black screen is shown. HOT 4
- Dynamic Error Modals - spawning them with runtime errors - handling their return values HOT 8
- Mouse position issue HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imgui.