Skip to content

Commit fd4c317

Browse files
committed
refactor kernings
1 parent 0b57b07 commit fd4c317

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

Assets/BitmapFontImporter/Editor/BFImporter.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ public static void DoImportBitmapFont(string fntPatn)
6161
so.Update();
6262
so.FindProperty("m_FontSize").floatValue = parse.fontSize;
6363
so.FindProperty("m_LineSpacing").floatValue = parse.lineHeight;
64-
SerializedProperty kerningsProp = so.FindProperty("m_KerningValues");
65-
// Debug.Log("arraySize " + kerningsProp.arraySize);
66-
// Clear kernings info, reset kernings on end.
67-
kerningsProp.ClearArray();
68-
64+
UpdateKernings(so, parse.kernings);
6965
so.ApplyModifiedProperties();
7066
so.SetIsDifferentCacheDirty();
7167

@@ -87,37 +83,43 @@ public static void DoImportBitmapFont(string fntPatn)
8783

8884
font.characterInfo = parse.charInfos;
8985

90-
AssetDatabase.SaveAssets();
91-
92-
// reset kernings by modify *.fontsettings file.
93-
string kerningValues = GetKerningsStr(parse.kernings);
94-
if (kerningValues != null)
95-
{
96-
string fontText = File.ReadAllText(fontPath);
97-
fontText = fontText.Replace("m_KerningValues: []", kerningValues);
98-
File.WriteAllText(fontPath, fontText);
99-
//AssetDatabase.ImportAsset(fontText, ImportAssetOptions.ForceUpdate);
100-
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
101-
}
102-
86+
AssetDatabase.SaveAssets();
10387
}
10488

105-
private static string GetKerningsStr(Kerning[] kernings)
89+
private static void UpdateKernings(SerializedObject so, Kerning[] kernings)
10690
{
107-
if (kernings == null || kernings.Length == 0)
108-
return null;
91+
int len = kernings != null ? kernings.Length : 0;
92+
SerializedProperty kerningsProp = so.FindProperty("m_KerningValues");
93+
so.Update();
10994

110-
const string format = " - first:\n {0}: {1}\n second: {2}\n";
111-
StringBuilder builder = new StringBuilder("m_KerningValues:\n", kernings.Length * 20);
112-
for (int i = 0; i < kernings.Length; i++)
95+
if (len == 0)
11396
{
114-
builder.AppendFormat(format, kernings[i].first, kernings[i].second, kernings[i].amount);
97+
kerningsProp.ClearArray();
98+
return;
11599
}
116-
builder.Remove(builder.Length - 1, 1);
117-
return builder.ToString();
118-
}
119-
120100

101+
int propLen = kerningsProp.arraySize;
102+
for (int i = 0; i < len; i++)
103+
{
104+
if (propLen <= i)
105+
{
106+
kerningsProp.InsertArrayElementAtIndex(i);
107+
}
108+
109+
SerializedProperty kerningProp = kerningsProp.GetArrayElementAtIndex(i);
110+
kerningProp.FindPropertyRelative("second").floatValue = kernings[i].amount;
111+
SerializedProperty pairProp = kerningProp.FindPropertyRelative("first");
112+
pairProp.Next(true);
113+
pairProp.intValue = kernings[i].first;
114+
pairProp.Next(false);
115+
pairProp.intValue = kernings[i].second;
116+
}
117+
for (int i = propLen - 1; i >= len; i--)
118+
{
119+
kerningsProp.DeleteArrayElementAtIndex(i);
120+
}
121+
}
122+
121123
private static void DelBitmapFont(string fntPath)
122124
{
123125
if (!IsFnt(fntPath)) return;

0 commit comments

Comments
 (0)