Skip to content

Commit

Permalink
feat: add font configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Rofli Sanches committed May 14, 2021
1 parent 9299843 commit ae6e249
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Source/Data/Font.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

184 changes: 184 additions & 0 deletions Source/Data/Font/FontConfig.cs
Original file line number Diff line number Diff line change
@@ -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<ushort> BuildRanges()
{
ImFontAtlas* atlas = null;
List<ushort> 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;
}
}
}
11 changes: 11 additions & 0 deletions Source/Data/Font/FontConfig.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Source/Data/Font/FontDefinition.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions Source/Data/Font/FontDefinition.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Source/Data/Font/FontRasterizerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UImGui
{
internal enum FontRasterizerType
{
StbTrueType,
FreeType,
}
}
11 changes: 11 additions & 0 deletions Source/Data/Font/FontRasterizerType.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Source/Data/Font/ScriptGlyphRanges.cs
Original file line number Diff line number Diff line change
@@ -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,
}
}
11 changes: 11 additions & 0 deletions Source/Data/Font/ScriptGlyphRanges.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ae6e249

Please sign in to comment.