|
|
@ -37,19 +37,21 @@ public class ImGuiManager |
|
|
|
|
|
|
|
|
|
|
|
private unsafe static ImFontPtr AddFontFromResources( |
|
|
|
private unsafe static ImFontPtr AddFontFromResources( |
|
|
|
ImGuiIOPtr io, string name, int size, Action<ImFontConfigPtr>? cfgAction = null, |
|
|
|
ImGuiIOPtr io, string name, int size, Action<ImFontConfigPtr>? cfgAction = null, |
|
|
|
float offset = 0, float minAdvance = 0, |
|
|
|
float? offset = null, float? advance = null, bool merge = false, |
|
|
|
(int Min, int Max)[]? ranges = null, bool merge = false) |
|
|
|
(int Min, int Max)[]? ranges = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// TODO: FontConfig can be freed at the end of this method. |
|
|
|
// TODO: FontConfig can be freed at the end of this method. |
|
|
|
// Unfortunately, data has to stick around until font atlas is built. |
|
|
|
// Unfortunately, data has to stick around until font atlas is built. |
|
|
|
var cfg = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig()) { |
|
|
|
var cfg = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig()); |
|
|
|
GlyphOffset = new(0, offset), |
|
|
|
|
|
|
|
GlyphMinAdvanceX = minAdvance, |
|
|
|
if (offset is float o) cfg.GlyphOffset = new(0, o); |
|
|
|
MergeMode = merge, |
|
|
|
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. |
|
|
|
// Set cfg.Name so the font has a nice display name in ImGui. |
|
|
|
var fullName = $"{name.Replace('.', ' ')} {size}px"; |
|
|
|
var fullName = $"{name.Replace('.', ' ')} {size}px"; |
|
|
|
Encoding.UTF8.GetBytes(fullName, new Span<byte>(cfg.Name.Data, cfg.Name.Count)); |
|
|
|
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 glyph ranges are specified, allocate unmanaged heap memory for them. |
|
|
|
if (ranges != null) { |
|
|
|
if (ranges != null) { |
|
|
|
var rangesSpan = GlobalHeapAllocator.Instance.Allocate<char>(ranges.Length * 2 + 1); |
|
|
|
var rangesSpan = GlobalHeapAllocator.Instance.Allocate<char>(ranges.Length * 2 + 1); |
|
|
@ -61,6 +63,7 @@ public class ImGuiManager |
|
|
|
fixed (void* rangesPtr = rangesSpan) |
|
|
|
fixed (void* rangesPtr = rangesSpan) |
|
|
|
cfg.GlyphRanges = (IntPtr)rangesPtr; |
|
|
|
cfg.GlyphRanges = (IntPtr)rangesPtr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Use cfgAction to allow changing other font configs. |
|
|
|
// Use cfgAction to allow changing other font configs. |
|
|
|
cfgAction?.Invoke(cfg); |
|
|
|
cfgAction?.Invoke(cfg); |
|
|
|
|
|
|
|
|
|
|
@ -89,8 +92,9 @@ public class ImGuiManager |
|
|
|
var style = ImGui.GetStyle(); |
|
|
|
var style = ImGui.GetStyle(); |
|
|
|
|
|
|
|
|
|
|
|
var fontSize = 16; |
|
|
|
var fontSize = 16; |
|
|
|
void MergeIcons() => AddFontFromResources(io, "ForkAwesome", fontSize, |
|
|
|
void MergeIcons() => AddFontFromResources(io, |
|
|
|
minAdvance: 18, ranges: new[]{ (ForkAwesome.Min, ForkAwesome.Max) }, merge: true); |
|
|
|
"ForkAwesome", fontSize, advance: fontSize, merge: true, |
|
|
|
|
|
|
|
ranges: new[]{ (ForkAwesome.Min, ForkAwesome.Max) }); |
|
|
|
AddFontFromResources(io, "OpenSans" , fontSize, offset: -1); MergeIcons(); |
|
|
|
AddFontFromResources(io, "OpenSans" , fontSize, offset: -1); MergeIcons(); |
|
|
|
AddFontFromResources(io, "OpenSans.Bold" , fontSize, offset: -1); MergeIcons(); |
|
|
|
AddFontFromResources(io, "OpenSans.Bold" , fontSize, offset: -1); MergeIcons(); |
|
|
|
AddFontFromResources(io, "OpenSans.Italic", fontSize, offset: -1); MergeIcons(); |
|
|
|
AddFontFromResources(io, "OpenSans.Italic", fontSize, offset: -1); MergeIcons(); |
|
|
|