ImGui.NET icon indicating copy to clipboard operation
ImGui.NET copied to clipboard

AddFontFromFileTTF not working

Open yafacex opened this issue 5 years ago • 4 comments

In project “ImGui.NET.SampleProgram.XNA” at master branch. SampleGame.cs

protected override void Initialize() { _imGuiRenderer = new ImGuiRenderer(this); _imGuiRenderer.RebuildFontAtlas(); base.Initialize(); ImGuiIOPtr io = ImGui.GetIO(); //io.Fonts.AddFontDefault(); io.Fonts.AddFontFromFileTTF(@"C:\Windows\Fonts\simhei.ttf", 20.0f, null, io.Fonts.GetGlyphRangesChineseSimplifiedCommon()); _imGuiRenderer.RebuildFontAtlas(); }

I have tried many ways such as use Arial.ttf and call RedbuildFontAtlas again.The result never changed. image

ImGui.Text(@"Hello,你好 world!"); ImGui.Text("Hello,你好 world!");

yafacex avatar Apr 02 '20 10:04 yafacex

This is how I'm doing it:

//Method
private unsafe void CreateFont(string fontFile, float fontSize, byte mergeMode, ushort[] charRange)
{
    // create the object on the native side
    var nativeConfig = ImGuiNative.ImFontConfig_ImFontConfig();

    // fill with data
    (*nativeConfig).OversampleH = 3;
    (*nativeConfig).OversampleV = 3;
    (*nativeConfig).RasterizerMultiply = 1f;
    (*nativeConfig).GlyphExtraSpacing = new System.Numerics.Vector2(0, 0);
    (*nativeConfig).MergeMode = mergeMode;

    GCHandle rangeHandle = GCHandle.Alloc(charRange, GCHandleType.Pinned);
    try
    {
        ImGui.GetIO().Fonts.AddFontFromFileTTF(fontFile, fontSize, nativeConfig, rangeHandle.AddrOfPinnedObject());
    }
    finally
    {
        if (rangeHandle.IsAllocated)
            rangeHandle.Free();
    }

    // delete the reference. ImGui copies it
    ImGuiNative.ImFontConfig_destroy(nativeConfig);
}

//Usage
protected override void Initialize()
{
    base.Initialize();

    string fontsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

    try
    {
        CreateFont(Path.Combine(fontsFolder, "micross.ttf"), 20f, 0, new ushort[] { 0x0021, 0x0375, 0 });
    }
    catch { ImGui.GetIO().Fonts.AddFontDefault(); }

    CreateFont(@Path.Combine(Settings.FormsDirectory.FullName, "Font-Awesome-5-Free-Solid-900.otf"), 20f, 1, new ushort[] { 0xf000, 0xf83e, 0 });
    CreateFont(@Path.Combine(Settings.FormsDirectory.FullName, "Font-Awesome-5-Brands-Regular-400.otf"), 20f, 1, new ushort[] { 0xf081, 0xf840, 0 });

    ImGuiRenderer.RebuildFontAtlas();
}

BlizzCrafter avatar Apr 02 '20 14:04 BlizzCrafter

This is how I'm doing it:

//Method
private unsafe void CreateFont(string fontFile, float fontSize, byte mergeMode, ushort[] charRange)
{
    // create the object on the native side
    var nativeConfig = ImGuiNative.ImFontConfig_ImFontConfig();

    // fill with data
    (*nativeConfig).OversampleH = 3;
    (*nativeConfig).OversampleV = 3;
    (*nativeConfig).RasterizerMultiply = 1f;
    (*nativeConfig).GlyphExtraSpacing = new System.Numerics.Vector2(0, 0);
    (*nativeConfig).MergeMode = mergeMode;

    GCHandle rangeHandle = GCHandle.Alloc(charRange, GCHandleType.Pinned);
    try
    {
        ImGui.GetIO().Fonts.AddFontFromFileTTF(fontFile, fontSize, nativeConfig, rangeHandle.AddrOfPinnedObject());
    }
    finally
    {
        if (rangeHandle.IsAllocated)
            rangeHandle.Free();
    }

    // delete the reference. ImGui copies it
    ImGuiNative.ImFontConfig_destroy(nativeConfig);
}

//Usage
protected override void Initialize()
{
    base.Initialize();

    string fontsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

    try
    {
        CreateFont(Path.Combine(fontsFolder, "micross.ttf"), 20f, 0, new ushort[] { 0x0021, 0x0375, 0 });
    }
    catch { ImGui.GetIO().Fonts.AddFontDefault(); }

    CreateFont(@Path.Combine(Settings.FormsDirectory.FullName, "Font-Awesome-5-Free-Solid-900.otf"), 20f, 1, new ushort[] { 0xf000, 0xf83e, 0 });
    CreateFont(@Path.Combine(Settings.FormsDirectory.FullName, "Font-Awesome-5-Brands-Regular-400.otf"), 20f, 1, new ushort[] { 0xf081, 0xf840, 0 });

    ImGuiRenderer.RebuildFontAtlas();
}

Thanks for reply.I found I forgot font push.....

yafacex avatar Apr 03 '20 02:04 yafacex

maybe its because you called ImGui.RebuildFontAtlas() twice, though I dont see how that could be the issue, anywayds heres my code, its a lot simple since Im pretty fine with my default font settings.

_imGuiRenderer = new ImGuiRenderer(Game); io = ImGui.GetIO(); io.Fonts.AddFontFromFileTTF(Font, fontSize); _imGuiRenderer.RebuildFontAtlas();

shortgecko avatar Jan 10 '21 09:01 shortgecko

feel free to use this lib as reference. https://github.com/zaafar/ClickableTransparentOverlay/blob/master/Examples/SingleThreadedOverlayWithCoroutines/SampleOverlay.cs#L86 it can load/change any font (english, non english) of any size at run time.

zaafar avatar Nov 20 '21 17:11 zaafar