mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-08 19:02:56 +00:00
Font atlas builder is off by one in some font configurations #7399
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#7399
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Version/Branch of Dear ImGui:
Version 1.90.5 WIP ()19045), Branch: master/docking
Back-ends:
imgui_impl_win32.cpp + imgui_impl_dx11/dx12.cpp
Compiler, OS:
Windows 10/11 + MSVC 2019/2022
Full config/build information:
Details:
I have seen the notice to use the discussions for font loading issues. This is not a font loading question, just a problem with the font atlas builder with a fix included.
I just started looking into custom icon fonts (and UI scaling, but that's not the issue) to improve our internal tools and promptly ran into an imprecision. I made a test font with a single glyph for now. It has an ascent of 1024 and a descent of 0 in a 1024 grid, the gear icon touches the limits exactly but doesn't exceed them. So the expected behavior is to load an icon font at a specific size and get that exact size. If loading at 32 pixels, the font should have an ascent of 32 pixels and a descent of 0, but it actually gets 33 and -1. The ascent being too big then results in the glyphs being offset vertically. Since I am kind of a pixel perfectionist, I investigated further.
I have identified the problem to be here:
ocornut/imgui@da29b776ee/imgui_draw.cpp (L2986-L2987)As long as the ascent is > 0 and the descent is <= 0, these lines are just overly complicated versions of
ceilfandfloorfthat are off by one if the calculated float precisely hits an integer value. If ascent is <= 0 or descent is > 0 (I don't know if that can ever be the case), these lines would even break a bit, the calculated ascent and descent would then no longer be guaranteed to cover the glyphs.Replacing the lines with these simpler versions fixes the issue:
With this, the calculated ascent and descent are correct and the glyph is vertically centered on the button. The default font is not affected at all since it doesn't hit exact integer values, so no side effects except other custom fonts and font scalings potentially being a bit more precise. I know that the placement could also be adjusted with a glyph offset in the font config, but that would fix the rendering only and leave the font metrics with their slightly off values, making them less useful for other stuff.
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.
Screenshots/Video:
When looking closely, the glyph is placed one pixel too low.
Minimal, Complete and Verifiable Example code:
Here is the font file in case you want to test it yourself (must be unzipped and placed in the working directory):
GmNavIcons.zip