ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.91k stars 307 forks source link

AddFontFromFileTTF not working #160

Open yafacex opened 4 years ago

yafacex commented 4 years ago

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!");

BlizzCrafter commented 4 years ago

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();
}
yafacex commented 4 years ago

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.....

shortgecko commented 3 years ago

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();

zaafar commented 3 years ago

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.