Skip to content

Commit

Permalink
add range param for Font.
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaochen910 committed Mar 5, 2024
1 parent fd5d0e2 commit 3bbf346
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 13 additions & 5 deletions addons/imgui-godot/ImGuiGodot/Internal/Fonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private sealed class FontParams
public FontFile? Font { get; init; }
public int FontSize { get; init; }
public bool Merge { get; init; }
public ushort[] Ranges { get; init; }
}
private readonly List<FontParams> _fontConfiguration = new();

Expand All @@ -32,12 +33,12 @@ public void ResetFonts()
_fontConfiguration.Clear();
}

public void AddFont(FontFile? fontData, int fontSize, bool merge)
public void AddFont(FontFile? fontData, int fontSize, bool merge, ushort[] predefinedRanges = null)
{
_fontConfiguration.Add(new FontParams { Font = fontData, FontSize = fontSize, Merge = merge });
_fontConfiguration.Add(new FontParams { Font = fontData, FontSize = fontSize, Merge = merge, Ranges = predefinedRanges});
}

private static unsafe void AddFontToAtlas(FontFile? fontData, int fontSize, bool merge)
private static unsafe void AddFontToAtlas(FontFile? fontData, int fontSize, bool merge, ushort[] predefinedRanges)
{
ImFontConfig* fc = ImGuiNative.ImFontConfig_ImFontConfig();
if (merge)
Expand Down Expand Up @@ -70,7 +71,14 @@ private static unsafe void AddFontToAtlas(FontFile? fontData, int fontSize, bool
// let ImGui manage this memory
IntPtr p = ImGui.MemAlloc((uint)len);
Marshal.Copy(fontData.Data, 0, p, len);
ImGui.GetIO().Fonts.AddFontFromMemoryTTF(p, len, fontSize, fc, ranges.Data);
if ( predefinedRanges == null ) {
ImGui.GetIO().Fonts.AddFontFromMemoryTTF(p, len, fontSize, fc, ranges.Data);
}
else {
fixed ( ushort* rangesPtr = predefinedRanges ) {
ImGui.GetIO().Fonts.AddFontFromMemoryTTF(p, len, fontSize, fc, ( IntPtr )rangesPtr);
}
}
}

if (merge)
Expand Down Expand Up @@ -143,7 +151,7 @@ public unsafe void RebuildFontAtlas(float scale)

foreach (var fontParams in _fontConfiguration)
{
AddFontToAtlas(fontParams.Font, (int)(fontParams.FontSize * scale), fontParams.Merge);
AddFontToAtlas(fontParams.Font, (int)(fontParams.FontSize * scale), fontParams.Merge, fontParams.Ranges);
}

io.Fonts.GetTexDataAsRGBA32(out byte* pixelData, out int width, out int height, out int bytesPerPixel);
Expand Down
1 change: 1 addition & 0 deletions addons/imgui-godot/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ description="Dear ImGui for Godot"
author="Patrick Dawson"
version="4.1.0"
script="scripts/ImGuiPlugin.gd"
version="4.1.1"

0 comments on commit 3bbf346

Please sign in to comment.