Closed Durobot closed 2 days ago
Input 1: 'fontawesome-webfont.ttf, 13px', Oversample: (0, 0), PixelSnapH: 0, Offset: (0.0, 0.0)
ImFontConfig config = { 0 };
I believe that your problem and it may be due to your ImFontConfig
structure being zero initialized instead of constructed with our default values.
The default constructor does this:
ImFontConfig::ImFontConfig()
{
memset(this, 0, sizeof(*this));
FontDataOwnedByAtlas = true;
OversampleH = 2;
OversampleV = 1;
GlyphMaxAdvanceX = FLT_MAX;
RasterizerMultiply = 1.0f;
RasterizerDensity = 1.0f;
EllipsisChar = (ImWchar)-1;
}
I believe you need to look into how to initialize structures with cimgui.
Looks like that was indeed what was causing my problem.
After a cursory search for ImFontConfig
constructor equivalent in cimgui
and failing to find one - only found an allocator function:
CIMGUI_API ImFontConfig* ImFontConfig_ImFontConfig(void)
{
return IM_NEW(ImFontConfig)();
}
I have added initialization code into my example directly:
ImFontConfig config = { 0 };
// Dear ImGui standard init (from ImFontConfig::ImFontConfig() in imgui_draw.cpp)
config.FontDataOwnedByAtlas = true;
config.OversampleH = 2;
config.OversampleV = 1;
config.GlyphMaxAdvanceX = FLT_MAX;
config.RasterizerMultiply = 1.0f;
config.RasterizerDensity = 1.0f;
config.EllipsisChar = (ImWchar)-1;
config.FontData = NULL; // To be extra certain :)
config.MergeMode = true;
config.GlyphMinAdvanceX = 13.0f; // Use if you want to make the icon monospaced
ImFontAtlas_AddFontFromFileTTF(ioptr->Fonts, "/home/archie/projects/c-playground/cimgui-02/fonts/fontawesome-webfont.ttf", 13.0f, &config, icon_ranges);
and voila!
Thank you so much!
I'll keep my eyes open for Dear ImGui
constructors / default initialization that has to be done manually with cimgui
.
We noted it was an issue with dear bindings too and will try to address it. I think you may ask cimgui to provide a constructor function for some type. This one is clearly the most common one.
I have added an assert 6ce26ef to detect your issue in the future.
I'll keep my eyes open for
Dear ImGui
constructors / default initialization that has to be done manually withcimgui
.
you need ImFontConfig *config = ImFontConfig_ImFontConfig();
you need ImFontConfig *config = ImFontConfig_ImFontConfig();
As mentioned in https://github.com/cimgui/cimgui/issues/278, this seems to needlessly allocate on the heap. Whereas this solution wouldn't: https://github.com/cimgui/cimgui/issues/278#issuecomment-2356300849 And would be more idiomatic.
They have added a new parameter to cimgui
's generator.sh
/ generator.bat
scripts, "constructors".
So, for example, ./generator.sh -t "internal noimstrv constructors"
now outputs cimgui.h
and cimgui.cpp
files with *_Construct()
functions that call Dear ImGui
constructors.
Version/Branch of Dear ImGui:
Version 1.91.1, Branch: docking (via cimgui)
Back-ends:
imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Compiler, OS:
Arch Linux + gcc 14.2.1 20240910
Full config/build information:
Details:
I'm trying to merge an icon font into the standard built-in
ProggyClean.ttf
to be able to add icons to tree nodes / leaves, checkboxes, buttons, etc.Currently I don't plan on replacing
ProggyClean.ttf
, although at some point in the future I probably will have to think about my "font strategy" in the context of internationalization. For now, 13 pixelProggyClean.ttf
is fine.Also, I don't have any particular icon font in mind at the moment, I will probably have to make my own, but for now I am experimenting with
fontawesome-webfont.ttf
v4.7.0 I have downloaded from https://www.cdnpkg.com/font-awesome/file/fontawesome-webfont.ttf/.My test program below is written in C and I am using Dear Imgui via cimgui. My actual development takes place in Zig, also via
cimgui
, directly. I don't use any type of bindings, as Zig can compile, link C and C++ and also import C into Zig, translate C to Zig, so bindings are more of a syntactic sugar, not that this is relevant to my problem.I'm following this example, but when I merge
fontawesome-webfont.ttf
with the default font (?), my test control,shows up empty.
When I don't merge
fontawesome-webfont.ttf
and instead switch my font by callingigPushFont(p_fnt_fa); / igPopFont();
aroundigText(ICON_FA_GLASS ICON_FA_SEARCH " find a glass");
, the glass and search icons show up - see the screenshots.To switch between these behaviors, comment out / uncomment
#define MERGE_FONTS
in the test code below.When I merge
fontawesome-webfont.ttf
, in the Style Editor I can see one font, "ProggyClean.ttf, 13px" with two inputs:There are 227 glyphs: the U+0000..U+00FF region (224 glyphs), which looks fine, and U+F000..U+F0FF (3 glyphs), which looks empty. See the screenshot below.
When I don't merge
fontawesome-webfont.ttf
(by compiling the example code with#define MERGE_FONTS
commented out), I can see two fonts in the Style Editor:fontawesome-webfont.ttf
shows 3 icons, just as expected. See the screenshots below.I have tried calling
ImFontAtlas_Build(ioptr->Fonts);
after I load / mergefontawesome-webfont.ttf
, but it doesn't seem to matter.Screenshots/Video:
Merged fonts, test control doesn't show up:
Separate fonts, test control shows up (it doesn't show text, but shows the icons, as expected):
Merged fonts, U+F000..U+F0FF region is claimed to contain 3 glyphs, but they are empty:
Merged fonts, font atlas does not contain the expected icons:
Separate fonts, U+F000..U+F0FF region contains and shows 3 icon glyphs:
Separate fonts, font atlas contains the icons:
Minimal, Complete and Verifiable Example code: