This commit is contained in:
CloudJ 2023-07-05 10:20:51 +08:00
parent c5ec2e1853
commit 6e799005ad

View File

@ -7,6 +7,7 @@ using System;
using UnityEngine.TextCore.LowLevel;
using TMPro.EditorUtilities;
using Object = UnityEngine.Object;
using System.Reflection;
namespace BFEditor.Resource
{
@ -28,6 +29,7 @@ namespace BFEditor.Resource
private const string AssetNumberName = "font_number_sdf.asset";
private const string AssetBattleName = "font_battle_sdf.asset";
private const string AssetThaiName = "font_thai_sdf.asset";
private const string AssetJaName = "font_ja_sdf.asset";
private const string DevStrPath = "Assets/Editor/BFResourceTools/FontTools/cn_3900.txt";
private const string CommonCNWordsPath = "Assets/Editor/BFResourceTools/FontTools/cn_1200.txt";
@ -158,6 +160,10 @@ namespace BFEditor.Resource
var thaiFont = AssetDatabase.LoadAssetAtPath<Font>("Assets/arts/fonts/tmpfonts/default/tmpfont/font_thai.ttf");
GenTMPFontAsset(thaiFont, new TTFInfo("cn", "Assets/arts/fonts/tmpfonts/default/tmpfont", "Assets/arts/fonts/tmpfonts/default/tmpfont", 512, new List<string>()), AssetThaiName, isdevelop);
// 日语
var jaFont = AssetDatabase.LoadAssetAtPath<Font>("Assets/arts/fonts/tmpfonts/default/tmpfont/font_ja.ttf");
GenTMPFontAsset(jaFont, new TTFInfo("cn", "Assets/arts/fonts/tmpfonts/default/tmpfont", "Assets/arts/fonts/tmpfonts/default/tmpfont", 512, new List<string>()), AssetJaName, isdevelop);
// 战斗用
// var battleFont = AssetDatabase.LoadAssetAtPath<Font>("Assets/arts/fonts/tmpfonts/battle/font_battle.ttf");
// GenTMPFontAsset(battleFont, new TTFInfo("cn", "Assets/arts/fonts/tmpfonts/battle", "Assets/arts/fonts/tmpfonts/battle", 1024, new List<string>()), AssetBattleName, isdevelop);
@ -238,6 +244,30 @@ namespace BFEditor.Resource
}
}
static private void GetThaiWords(List<string> luaFolderPaths, out string words)
{
words = string.Empty;
//是否开发模式
var text = LocalizationMenuTools.CatWordsFromLuaConfig(new List<string>(){Application.dataPath + "/Developer/lua/app/config/strings/th"});
text += "ิ";
text += "ี";
text += "ึ";
text += "ื";
text += "ำ";
text += "ั";
text += "ํ";
text += "่";
text += "้";
text += "๊";
text += "๋";
foreach (var w in text)
{
if (!words.Contains(w.ToString()))
words += w;
}
}
static private void GenTMPFontAsset(Font font, TTFInfo ttfInfo, string assetName, bool isdevelop)
{
EditorUtility.DisplayProgressBar("生成TMP字体", "正在生成中...", 0f);
@ -290,6 +320,20 @@ namespace BFEditor.Resource
EditorUtility.SetDirty(fontAsset);
}
// 泰语使用静态并特殊处理
if (assetName == AssetThaiName)
{
var luaFolderPaths = new List<string>();
luaFolderPaths.Add(Application.dataPath + "/Developer/lua/app/config/strings/th");
var words = string.Empty;
GetThaiWords(luaFolderPaths, out words);
Debug.Log("泰语字符集:" + words);
fontAsset.TryAddCharacters(words);
fontAsset.atlasPopulationMode = AtlasPopulationMode.Static;
// 修订部分重音位置
_Adjust(fontAsset);
}
if (null != fontAsset)
{
// DealWithMetric(fontAsset);
@ -306,6 +350,11 @@ namespace BFEditor.Resource
{
fontAsset.fallbackFontAssetTable.Add(fallbackAsset);
}
fallbackAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(Path.Combine(assetDirPath, AssetJaName));
if (fallbackAsset != null)
{
fontAsset.fallbackFontAssetTable.Add(fallbackAsset);
}
}
}
EditorUtility.ClearProgressBar();
@ -562,6 +611,147 @@ namespace BFEditor.Resource
#endregion
// 针对泰语处理
static bool overrideAll = true;
static float wannayukHeight = 10f;
static float aumXPlacementAfter = -35f;
static float aumXPlacementBefore = 10f;
static void _Adjust(TMP_FontAsset fontAsset)
{
if (fontAsset == null)
{
Debug.Log("No FontAsset selected");
return;
}
var glyphPairAdjustmentRecords = new List<TMP_GlyphPairAdjustmentRecord>(fontAsset.fontFeatureTable.glyphPairAdjustmentRecords);
var lookupTable = fontAsset.characterLookupTable;
var glyphPairAdjustmentRecordLookupDictionary =
(Dictionary<uint, TMP_GlyphPairAdjustmentRecord>) fontAsset.fontFeatureTable
.GetType()
.GetField("m_GlyphPairAdjustmentRecordLookupDictionary", BindingFlags.NonPublic | BindingFlags.Instance)?
.GetValue(fontAsset.fontFeatureTable);
int[] saras = new int[7];
int[] wannayuks = new int[4];
//get sara
saras[0] = (int) lookupTable[GetUnicodeCharacter("ิ")].glyphIndex; // อิ
saras[1] = (int) lookupTable[GetUnicodeCharacter("ี")].glyphIndex; // อี
saras[2] = (int) lookupTable[GetUnicodeCharacter("ึ")].glyphIndex; // อึ
saras[3] = (int) lookupTable[GetUnicodeCharacter("ื")].glyphIndex; // อื
saras[4] = (int) lookupTable[GetUnicodeCharacter("ำ")].glyphIndex; // ำ
saras[5] = (int) lookupTable[GetUnicodeCharacter("ั")].glyphIndex; // ั
saras[6] = (int) lookupTable[GetUnicodeCharacter("ํ")].glyphIndex; // ํ
//get wanna yuk
wannayuks[0] = (int) lookupTable[GetUnicodeCharacter("่")].glyphIndex; //เอก
wannayuks[1] = (int) lookupTable[GetUnicodeCharacter("้")].glyphIndex; //โท
wannayuks[2] = (int) lookupTable[GetUnicodeCharacter("๊")].glyphIndex; //ตรี
wannayuks[3] = (int) lookupTable[GetUnicodeCharacter("๋")].glyphIndex; //จัตวา
int recordAdd = 0;
foreach (var sara in saras)
{
foreach (var wannayuk in wannayuks)
{
float xPlacement = sara == saras[4] || sara == saras[6] ? aumXPlacementAfter : 0;
TMP_GlyphValueRecord saraPosition = new TMP_GlyphValueRecord(0, 0, 0, 0);
TMP_GlyphAdjustmentRecord saraGlyph = new TMP_GlyphAdjustmentRecord((uint) sara, saraPosition);
TMP_GlyphValueRecord wannayukPosition = new TMP_GlyphValueRecord(xPlacement, wannayukHeight, 0, 0);
TMP_GlyphAdjustmentRecord wannayukGlyph = new TMP_GlyphAdjustmentRecord((uint) wannayuk, wannayukPosition);
var saraThenWannayukGlyphPair = new TMP_GlyphPairAdjustmentRecord(saraGlyph, wannayukGlyph);
if (sara == saras[4] || sara == saras[6])
{
xPlacement = aumXPlacementBefore;
wannayukPosition = new TMP_GlyphValueRecord(xPlacement, wannayukHeight, 0, 0);
wannayukGlyph = new TMP_GlyphAdjustmentRecord((uint) wannayuk, wannayukPosition);
}
var wannayukThenSaraGlyphPair = new TMP_GlyphPairAdjustmentRecord(wannayukGlyph, saraGlyph);
uint firstPairKey = saraThenWannayukGlyphPair.firstAdjustmentRecord.glyphIndex << 16 | saraThenWannayukGlyphPair.secondAdjustmentRecord.glyphIndex;
uint secondPairKey = wannayukThenSaraGlyphPair.firstAdjustmentRecord.glyphIndex << 16 | wannayukThenSaraGlyphPair.secondAdjustmentRecord.glyphIndex;
if (overrideAll)
{
glyphPairAdjustmentRecords.RemoveAll(record => IsGlyphPairEqual(record, saraThenWannayukGlyphPair) ||
IsGlyphPairEqual(record, wannayukThenSaraGlyphPair));
glyphPairAdjustmentRecords.Add(saraThenWannayukGlyphPair);
glyphPairAdjustmentRecords.Add(wannayukThenSaraGlyphPair);
if (glyphPairAdjustmentRecordLookupDictionary != null && !glyphPairAdjustmentRecordLookupDictionary.ContainsKey(firstPairKey))
{
glyphPairAdjustmentRecordLookupDictionary.Add(firstPairKey, saraThenWannayukGlyphPair);
}
recordAdd += 2;
}
else if (glyphPairAdjustmentRecordLookupDictionary != null)
{
if (!glyphPairAdjustmentRecordLookupDictionary.ContainsKey(firstPairKey))
{
glyphPairAdjustmentRecords.Add(saraThenWannayukGlyphPair);
recordAdd++;
}
if (!glyphPairAdjustmentRecordLookupDictionary.ContainsKey(secondPairKey))
{
glyphPairAdjustmentRecords.Add(wannayukThenSaraGlyphPair);
recordAdd++;
}
}
}
}
if (recordAdd > 0)
{
fontAsset.fontFeatureTable.glyphPairAdjustmentRecords = glyphPairAdjustmentRecords;
fontAsset.fontFeatureTable.SortGlyphPairAdjustmentRecords();
EditorUtility.SetDirty(fontAsset);
AssetDatabase.SaveAssets();
Canvas.ForceUpdateCanvases();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
}
Debug.Log("Adjust font : <color=#2bcaff>" + fontAsset.name + "</color>" +
" Height offset : <color=#d8ff2b>" + wannayukHeight + "</color>" +
" Number of adjustment add : <color=#5dfa41>" + recordAdd + "</color>");
}
static void _Clear(TMP_FontAsset fontAsset)
{
fontAsset.fontFeatureTable.glyphPairAdjustmentRecords = new List<TMP_GlyphPairAdjustmentRecord>();
EditorUtility.SetDirty(fontAsset);
AssetDatabase.SaveAssets();
Canvas.ForceUpdateCanvases();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
}
static uint GetUnicodeCharacter (string source)
{
uint unicode;
if (source.Length == 1)
unicode = source[0];
else if (source.Length == 6)
unicode = (uint)TMP_TextUtilities.StringHexToInt(source.Replace("\\u", ""));
else
unicode = (uint)TMP_TextUtilities.StringHexToInt(source.Replace("\\U", ""));
return unicode;
}
static bool IsGlyphPairEqual(TMP_GlyphPairAdjustmentRecord a, TMP_GlyphPairAdjustmentRecord b)
{
return a.firstAdjustmentRecord.glyphIndex == b.firstAdjustmentRecord.glyphIndex &&
a.secondAdjustmentRecord.glyphIndex == b.secondAdjustmentRecord.glyphIndex;
}
}
}