Minor changes to AddFontFromResources

wip/source-generators
copygirl 1 year ago
parent 5268d97828
commit c88530260f
  1. 22
      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<ImFontConfigPtr>? 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<byte>(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<char>(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();

Loading…
Cancel
Save