From 6e799005ad3ddaa56dbb9a5499c493ecd97f424b Mon Sep 17 00:00:00 2001 From: CloudJ Date: Wed, 5 Jul 2023 10:20:51 +0800 Subject: [PATCH] thai --- .../BFResourceTools/FontTools/TMPTools.cs | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/Assets/Editor/BFResourceTools/FontTools/TMPTools.cs b/Assets/Editor/BFResourceTools/FontTools/TMPTools.cs index 51177e240..c876e8b5e 100644 --- a/Assets/Editor/BFResourceTools/FontTools/TMPTools.cs +++ b/Assets/Editor/BFResourceTools/FontTools/TMPTools.cs @@ -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("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()), AssetThaiName, isdevelop); + // 日语 + var jaFont = AssetDatabase.LoadAssetAtPath("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()), AssetJaName, isdevelop); + // 战斗用 // var battleFont = AssetDatabase.LoadAssetAtPath("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()), AssetBattleName, isdevelop); @@ -237,6 +243,30 @@ namespace BFEditor.Resource } } } + + static private void GetThaiWords(List luaFolderPaths, out string words) + { + words = string.Empty; + //是否开发模式 + var text = LocalizationMenuTools.CatWordsFromLuaConfig(new List(){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) { @@ -290,6 +320,20 @@ namespace BFEditor.Resource EditorUtility.SetDirty(fontAsset); } + // 泰语使用静态并特殊处理 + if (assetName == AssetThaiName) + { + var luaFolderPaths = new List(); + 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(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(fontAsset.fontFeatureTable.glyphPairAdjustmentRecords); + var lookupTable = fontAsset.characterLookupTable; + + var glyphPairAdjustmentRecordLookupDictionary = + (Dictionary) 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 : " + fontAsset.name + "" + + " Height offset : " + wannayukHeight + "" + + " Number of adjustment add : " + recordAdd + ""); + } + + static void _Clear(TMP_FontAsset fontAsset) + { + fontAsset.fontFeatureTable.glyphPairAdjustmentRecords = new List(); + 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; + } + } }