Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Assets/Editor/UIConfigEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine;
using FairyGUI;
using UnityEditor;
using FairyGUI;
using UnityEngine;

namespace FairyGUIEditor
{
Expand All @@ -14,7 +14,7 @@ public class UIConfigEditor : Editor
bool itemsFoldout;
bool packagesFoldOut;
int errorState;

static GUIStyle s_textAreaStyle;
private const float kButtonWidth = 18f;

void OnEnable()
Expand All @@ -31,7 +31,7 @@ public override void OnInspectorGUI()
serializedObject.Update();

DrawPropertiesExcluding(serializedObject, propertyToExclude);

UIConfig config = (UIConfig)target;

EditorGUILayout.BeginHorizontal();
Expand Down Expand Up @@ -141,6 +141,13 @@ public override void OnInspectorGUI()
if (EditorGUI.EndChangeCheck())
modified = true;
break;

case UIConfig.ConfigKey.SystemFontFamily:
value.s = EditorGUILayout.TextArea(value.s, s_textAreaStyle ??= new GUIStyle("TextField")
{
wordWrap = true,
});
break;
}

if (GUILayout.Button(new GUIContent("X", "Delete Item"), EditorStyles.miniButtonRight, GUILayout.Width(30)))
Expand Down
32 changes: 28 additions & 4 deletions Assets/Scripts/Core/Text/FontManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.TextCore.LowLevel;

namespace FairyGUI
{
Expand Down Expand Up @@ -61,6 +63,7 @@ static public BaseFont GetFont(string name)
return font;

object asset = Resources.Load(name);

if (asset == null)
asset = Resources.Load("Fonts/" + name);

Expand All @@ -82,21 +85,28 @@ static public BaseFont GetFont(string name)
if (asset == null)
return Fallback(name);

if (asset is Font)
if (asset is Font nativeFont)
{
font = new DynamicFont();
font.name = name;
sFontFactory.Add(name, font);

((DynamicFont)font).nativeFont = (Font)asset;
AppendSystemFontsFromUIConfig(nativeFont);

((DynamicFont)font).nativeFont = nativeFont;
}
#if FAIRYGUI_TMPRO
else if (asset is TMPro.TMP_FontAsset)
else if (asset is TMPro.TMP_FontAsset tmpFontAsset)
{
font = new TMPFont();
font.name = name;
sFontFactory.Add(name, font);
((TMPFont)font).fontAsset = (TMPro.TMP_FontAsset)asset;
// if (name == UIConfig.defaultFont) // apply to all may be better
{
; ((TMPFont)font).SetFallbackSystemFontFamily(UIConfig.systemFontFamily);
}

; ((TMPFont)font).fontAsset = tmpFontAsset;
}
#endif
else
Expand All @@ -116,6 +126,16 @@ static public BaseFont GetFont(string name)
return font;
}

static void AppendSystemFontsFromUIConfig(Font nativeFont)
{
if (UIConfig.systemFontFamily.Length == 0) return;

nativeFont.fontNames = nativeFont.fontNames
.Concat(UIConfig.systemFontFamily)
.Distinct()
.ToArray();
}

static BaseFont Fallback(string name)
{
if (name != UIConfig.defaultFont)
Expand All @@ -132,6 +152,8 @@ static BaseFont Fallback(string name)
if (asset == null)
throw new Exception("Failed to load font '" + name + "'");

AppendSystemFontsFromUIConfig(asset);

BaseFont font = new DynamicFont();
font.name = name;
((DynamicFont)font).nativeFont = asset;
Expand All @@ -149,6 +171,8 @@ static public void Clear()
kv.Value.Dispose();

sFontFactory.Clear();

SystemFontService.Clear();
}

#if UNITY_2019_3_OR_NEWER
Expand Down
28 changes: 28 additions & 0 deletions Assets/Scripts/Extensions/TextMeshPro/FontEngineNameResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if FAIRYGUI_TMPRO

using System.Collections.Generic;
using UnityEngine.TextCore.LowLevel;

namespace FairyGUI
{
// FontEngine loads all infos besides names and caches permanently. This implement will significantly increase memory unsage.
public class FontEngineNameResolver : IFontNameResolver
{
public void GetFontNames(string filePath, List<FontName> results)
{
var error = FontEngine.LoadFontFace(filePath);
if (error is not FontEngineError.Success) return;

var numFaces = FontEngine.GetFontFaces().Length; // a collection when it is ttc format
for (int i = 0; i < numFaces; i++)
{
// Dont worry: unity caches the result as TextCore:FontFaceCache
FontEngine.LoadFontFace(filePath, 0/*pointSize: default is 0*/, i);
var faceInfo = FontEngine.GetFaceInfo();
results.Add(FontName.Create(faceInfo.familyName, faceInfo.styleName, filePath));
}
}
}
}

#endif

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if FAIRYGUI_TMPRO
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore;
using UnityEngine.TextCore.LowLevel;

namespace FairyGUI
{
public class LightWeightFontNameResolver : IFontNameResolver
{
public void GetFontNames(string filePath, List<FontName> results)
{
LightWeightFontFaceImpl.Default.GetFontFamilyNames(filePath, results);
}
}
}

#endif

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

8 changes: 8 additions & 0 deletions Assets/Scripts/Extensions/TextMeshPro/LightWeightReader.meta

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

107 changes: 107 additions & 0 deletions Assets/Scripts/Extensions/TextMeshPro/LightWeightReader/BEReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#if FAIRYGUI_TMPRO
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace FairyGUI
{

internal unsafe class BEReader
{
[ThreadStatic] static byte[] tls_buffer = new byte[16];

private Stream m_stream;

public long Position
{
get => m_stream.Position;
set => m_stream.Position = value;
}

public BEReader(Stream stream)
{
this.m_stream = stream;
}

public string ReadString(int numBytes, Encoding encoding = null)
{
encoding ??= Encoding.UTF8;

if (numBytes <= 16)
{
tls_buffer ??= new byte[16];
m_stream.Read(tls_buffer, 0, numBytes);
return encoding.GetString(tls_buffer, 0, numBytes);
}
else
{
var largeBytes = ArrayPool<byte>.Shared.Rent(numBytes);
try
{
m_stream.Read(largeBytes, 0, numBytes);
return encoding.GetString(largeBytes, 0, numBytes);
}
finally
{
ArrayPool<byte>.Shared.Return(largeBytes);
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char ReadChar() => ReadPrimite<char>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte() => ReadPrimite<byte>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt32() => ReadPrimite<int>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadInt64() => ReadPrimite<long>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte ReadSByte() => ReadPrimite<sbyte>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadInt16() => ReadPrimite<short>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUInt16() => ReadPrimite<ushort>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt32() => ReadPrimite<uint>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadUInt64() => ReadPrimite<ulong>(m_stream);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static T ReadPrimite<T>(Stream stream) where T : unmanaged
{
tls_buffer ??= new byte[16];
stream.Read(tls_buffer, 0, sizeof(T));
fixed (byte* p = tls_buffer)
{
var raw = *(T*)p;
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < sizeof(T) >> 1; i++)
{
int j = sizeof(T) - 1 - i;
*(p + i) ^= *(p + j);
*(p + j) ^= *(p + i);
*(p + i) ^= *(p + j);
}
}

return *(T*)p;
}
}

}

}

#endif

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if FAIRYGUI_TMPRO
namespace FairyGUI
{
// see: https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
public enum EncodingCodePage : int
{
Unicode = 1200,
UTF8 = 65001,
UTF16LE = 1200,
UTF16BE = 1201,
GB2312 = 936,
KS_C_5601_1987 = 949,
Big5 = 950,
Macintosh = 10000,
}
}

#endif

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if FAIRYGUI_TMPRO
namespace FairyGUI
{
/*
FYR: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid
*/
internal enum LanguageID
{
English = 0x0409,
French = 0x040c,
German = 0x0407,
Italian = 0x0410,
Japanese = 0x0411,
Korean = 0x0412,
Spanish = 0x040a,
ChineseSimplified = 0x0804,
ChineseTraditional = 0x0404,
Russian = 0x0419,
Arabic = 0x0401,
Portuguese = 0x0816,
Hindi = 0x0439,
}
}

#endif

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

Loading