From c88530260f3d3d2689cc9d92807e77c44654998c Mon Sep 17 00:00:00 2001 From: copygirl Date: Thu, 29 Dec 2022 18:45:10 +0100 Subject: [PATCH] Minor changes to AddFontFromResources --- src/gaemstone.Client/Systems/ImGuiManager.cs | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/gaemstone.Client/Systems/ImGuiManager.cs b/src/gaemstone.Client/Systems/ImGuiManager.cs index 2ca6ec9..f423405 100644 --- a/src/gaemstone.Client/Systems/ImGuiManager.cs +++ b/src/gaemstone.Client/Systems/ImGuiManager.cs @@ -37,19 +37,21 @@ public class ImGuiManager private unsafe static ImFontPtr AddFontFromResources( ImGuiIOPtr io, string name, int size, Action? cfgAction = null, - float offset = 0, float minAdvance = 0, - (int Min, int Max)[]? ranges = null, bool merge = false) + float? offset = null, float? advance = null, bool merge = false, + (int Min, int Max)[]? ranges = null) { // TODO: FontConfig can be freed at the end of this method. // Unfortunately, data has to stick around until font atlas is built. - var cfg = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig()) { - GlyphOffset = new(0, offset), - GlyphMinAdvanceX = minAdvance, - MergeMode = merge, - }; + var cfg = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig()); + + if (offset is float o) cfg.GlyphOffset = new(0, o); + if (advance is float a) cfg.GlyphMinAdvanceX = cfg.GlyphMaxAdvanceX = a; + if (merge) cfg.MergeMode = merge; + // Set cfg.Name so the font has a nice display name in ImGui. var fullName = $"{name.Replace('.', ' ')} {size}px"; Encoding.UTF8.GetBytes(fullName, new Span(cfg.Name.Data, cfg.Name.Count)); + // If glyph ranges are specified, allocate unmanaged heap memory for them. if (ranges != null) { var rangesSpan = GlobalHeapAllocator.Instance.Allocate(ranges.Length * 2 + 1); @@ -61,6 +63,7 @@ public class ImGuiManager fixed (void* rangesPtr = rangesSpan) cfg.GlyphRanges = (IntPtr)rangesPtr; } + // Use cfgAction to allow changing other font configs. cfgAction?.Invoke(cfg); @@ -89,8 +92,9 @@ public class ImGuiManager var style = ImGui.GetStyle(); var fontSize = 16; - void MergeIcons() => AddFontFromResources(io, "ForkAwesome", fontSize, - minAdvance: 18, ranges: new[]{ (ForkAwesome.Min, ForkAwesome.Max) }, merge: true); + void MergeIcons() => AddFontFromResources(io, + "ForkAwesome", fontSize, advance: fontSize, merge: true, + ranges: new[]{ (ForkAwesome.Min, ForkAwesome.Max) }); AddFontFromResources(io, "OpenSans" , fontSize, offset: -1); MergeIcons(); AddFontFromResources(io, "OpenSans.Bold" , fontSize, offset: -1); MergeIcons(); AddFontFromResources(io, "OpenSans.Italic", fontSize, offset: -1); MergeIcons();