diff --git a/Source/Data/Font.meta b/Source/Data/Font.meta new file mode 100644 index 0000000..65e99cf --- /dev/null +++ b/Source/Data/Font.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: eab2da094dda091438bc71dad7da4dbf +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Data/Font/FontConfig.cs b/Source/Data/Font/FontConfig.cs new file mode 100644 index 0000000..fc46325 --- /dev/null +++ b/Source/Data/Font/FontConfig.cs @@ -0,0 +1,184 @@ +using ImGuiNET; +using NumericsConverter; +using System.Collections.Generic; +using UnityEngine; + +namespace UImGui +{ + [System.Serializable] + internal struct FontConfig + { + [Tooltip("TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself). (default=true)")] + public bool FontDataOwnedByAtlas; + + [Tooltip("Index of font within TTF/OTF file. (default=0)")] + public int FontNo; + + [Tooltip("Size in pixels for rasterizer (more or less maps to the resulting font height).")] + public float SizeInPixels; + + [Tooltip("Rasterize at higher quality for sub-pixel positioning. " + + "Note the difference between 2 and 3 is minimal so you can reduce this to 2 to save memory. " + + "Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details. (default=3)")] + public int OversampleH; + + [Tooltip("Rasterize at higher quality for sub-pixel positioning. " + + "This is not really useful as we don't use sub-pixel positions on the Y axis. (default=1)")] + public int OversampleV; + + [Tooltip("Align every glyph to pixel boundary." + + " Useful e.g. if you are merging a non-pixel aligned font with the default font. (default=false)" + + "If enabled, you can set OversampleH/V to 1.")] + public bool PixelSnapH; + + [Tooltip("Extra spacing (in pixels) between glyphs. Only X axis is supported for now. (default=0, 0)")] + public Vector2 GlyphExtraSpacing; + + [Tooltip("Offest all glyphs from this font input. (default=0, 0)")] + public Vector2 GlyphOffset; + + [Tooltip("Glyph ranges for different writing systems.")] + public ScriptGlyphRanges GlyphRanges; + + [Tooltip("Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font. (default=0, 0)")] + public float GlyphMinAdvanceX; + + [Tooltip("Maximum AdvanceX for glyphs. (default=float_max)")] + public float GlyphMaxAdvanceX; + + [Tooltip("Merge into previous ImFont, so you can combine multiple " + + "inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). " + + "You may want to use GlyphOffset.y when merge font of different heights. (default=false)")] + public bool MergeMode; + + [Tooltip("Settings for custom font builder. THIS IS BUILDER IMPLEMENTATION DEPENDENT. Leave as zero if unsure. (default=0)")] + public uint FontBuilderFlags; + + [Tooltip("Brighten (>1.0f) or darken (<1.0f) font output. " + + "Brightening small fonts may be a good workaround to make them more readable. (default=1.0f)")] + public float RasterizerMultiply; + + [Tooltip("Explicitly specify unicode codepoint of ellipsis character. " + + "When fonts are being merged first specified ellipsis will be used. (default=-1)")] + public char EllipsisChar; + + [Tooltip("User-provided list of Unicode range (2 value per range, values are inclusive).")] + public Range[] CustomGlyphRanges; + + public void SetDefaults() + { + unsafe + { + ImFontConfig* imFontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); + SetFrom(imFontConfig); + ImGuiNative.ImFontConfig_destroy(imFontConfig); + } + } + + public void ApplyTo(ImFontConfigPtr im) + { + im.FontDataOwnedByAtlas = FontDataOwnedByAtlas; + im.FontNo = FontNo; + im.SizePixels = SizeInPixels; + im.OversampleH = OversampleH; + im.OversampleV = OversampleV; + im.PixelSnapH = PixelSnapH; + im.GlyphExtraSpacing = GlyphExtraSpacing.ToSystem(); + im.GlyphOffset = GlyphOffset.ToSystem(); + im.GlyphMinAdvanceX = GlyphMinAdvanceX; + im.GlyphMaxAdvanceX = GlyphMaxAdvanceX; + im.MergeMode = MergeMode; + im.FontBuilderFlags = FontBuilderFlags; + im.RasterizerMultiply = RasterizerMultiply; + im.EllipsisChar = EllipsisChar; + + // setting GlyphRanges requires allocating memory so it is not done here + // use BuildRanges to get a List with the values, then allocate memory and copy + // (see TextureManager) + } + + public void SetFrom(ImFontConfigPtr im) + { + FontDataOwnedByAtlas = im.FontDataOwnedByAtlas; + FontNo = im.FontNo; + SizeInPixels = im.SizePixels; + OversampleH = im.OversampleH; + OversampleV = im.OversampleV; + PixelSnapH = im.PixelSnapH; + GlyphExtraSpacing = im.GlyphExtraSpacing.ToUnity(); + GlyphOffset = im.GlyphOffset.ToUnity(); + GlyphMinAdvanceX = im.GlyphMinAdvanceX; + GlyphMaxAdvanceX = im.GlyphMaxAdvanceX; + MergeMode = im.MergeMode; + FontBuilderFlags = im.FontBuilderFlags; + RasterizerMultiply = im.RasterizerMultiply; + EllipsisChar = (char)im.EllipsisChar; + + // no good way to set GlyphRanges, do manually + } + + public unsafe List BuildRanges() + { + ImFontAtlas* atlas = null; + List ranges = default; + + void AddRangePtr(ushort* r) + { + while (*r != 0) + { + ranges.Add(*r++); + } + }; + + if ((GlyphRanges & ScriptGlyphRanges.Default) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesDefault(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.Cyrillic) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesCyrillic(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.Japanese) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesJapanese(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.Korean) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesKorean(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.Thai) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesThai(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.Vietnamese) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesVietnamese(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.ChineseSimplified) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesChineseSimplifiedCommon(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.ChineseFull) != 0) + { + AddRangePtr(ImGuiNative.ImFontAtlas_GetGlyphRangesChineseFull(atlas)); + } + + if ((GlyphRanges & ScriptGlyphRanges.Custom) != 0) + { + foreach (Range range in CustomGlyphRanges) + { + ranges.AddRange(new[] { range.Start, range.End }); + } + } + + return ranges; + } + } +} diff --git a/Source/Data/Font/FontConfig.cs.meta b/Source/Data/Font/FontConfig.cs.meta new file mode 100644 index 0000000..77ef510 --- /dev/null +++ b/Source/Data/Font/FontConfig.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5089ec6fba00e46409ac050869a21be5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Data/Font/FontDefinition.cs b/Source/Data/Font/FontDefinition.cs new file mode 100644 index 0000000..20f1e4a --- /dev/null +++ b/Source/Data/Font/FontDefinition.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +namespace UImGui +{ + [System.Serializable] + internal struct FontDefinition + { + [Tooltip("Path relative to Application.streamingAssetsPath")] + public string Path; + public FontConfig Config; + + [SerializeField] + private Object _fontAsset; + } +} diff --git a/Source/Data/Font/FontDefinition.cs.meta b/Source/Data/Font/FontDefinition.cs.meta new file mode 100644 index 0000000..496931e --- /dev/null +++ b/Source/Data/Font/FontDefinition.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 40b8deff9b26abf4ea8b8bf41082d396 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Data/Font/FontRasterizerType.cs b/Source/Data/Font/FontRasterizerType.cs new file mode 100644 index 0000000..b5dd61b --- /dev/null +++ b/Source/Data/Font/FontRasterizerType.cs @@ -0,0 +1,8 @@ +namespace UImGui +{ + internal enum FontRasterizerType + { + StbTrueType, + FreeType, + } +} diff --git a/Source/Data/Font/FontRasterizerType.cs.meta b/Source/Data/Font/FontRasterizerType.cs.meta new file mode 100644 index 0000000..5f3915f --- /dev/null +++ b/Source/Data/Font/FontRasterizerType.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fd44d44ecc6111941b4ee71ef2e70a5a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Data/Font/ScriptGlyphRanges.cs b/Source/Data/Font/ScriptGlyphRanges.cs new file mode 100644 index 0000000..9b6438f --- /dev/null +++ b/Source/Data/Font/ScriptGlyphRanges.cs @@ -0,0 +1,18 @@ +using System; + +namespace UImGui +{ + [Flags] + internal enum ScriptGlyphRanges + { + Default = 1 << 0, + Cyrillic = 1 << 1, + Japanese = 1 << 2, + Korean = 1 << 3, + Thai = 1 << 4, + Vietnamese = 1 << 5, + ChineseSimplified = 1 << 6, + ChineseFull = 1 << 7, + Custom = 1 << 8, + } +} diff --git a/Source/Data/Font/ScriptGlyphRanges.cs.meta b/Source/Data/Font/ScriptGlyphRanges.cs.meta new file mode 100644 index 0000000..39716d2 --- /dev/null +++ b/Source/Data/Font/ScriptGlyphRanges.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 26d8c6f671d23d044ae42b375d5e901b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: