12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904 |
- using System;
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Threading;
- using System.IO;
- using System.Text.RegularExpressions;
- using UnityEngine.TextCore;
- using UnityEngine.TextCore.LowLevel;
- using UnityEditor.TextCore.LowLevel;
- using Object = UnityEngine.Object;
- namespace TMPro.EditorUtilities
- {
- public class TMPro_FontAssetCreatorWindow : EditorWindow
- {
- [MenuItem("Window/TextMeshPro/Font Asset Creator", false, 2025)]
- public static void ShowFontAtlasCreatorWindow()
- {
- var window = GetWindow<TMPro_FontAssetCreatorWindow>();
- window.titleContent = new GUIContent("Font Asset Creator");
- window.Focus();
-
- window.CheckEssentialResources();
- }
- public static void ShowFontAtlasCreatorWindow(Font sourceFontFile)
- {
- var window = GetWindow<TMPro_FontAssetCreatorWindow>();
- window.titleContent = new GUIContent("Font Asset Creator");
- window.Focus();
- window.ClearGeneratedData();
- window.m_LegacyFontAsset = null;
- window.m_SelectedFontAsset = null;
-
- window.m_SourceFontFile = sourceFontFile;
-
- window.CheckEssentialResources();
- }
- public static void ShowFontAtlasCreatorWindow(TMP_FontAsset fontAsset)
- {
- var window = GetWindow<TMPro_FontAssetCreatorWindow>();
- window.titleContent = new GUIContent("Font Asset Creator");
- window.Focus();
-
- window.ClearGeneratedData();
- window.m_LegacyFontAsset = null;
-
- if (string.IsNullOrEmpty(fontAsset.creationSettings.sourceFontFileGUID) == false)
- {
- window.LoadFontCreationSettings(fontAsset.creationSettings);
-
- window.m_CharacterSetSelectionMode = 6;
- window.m_CharacterSequence = TMP_EditorUtility.GetUnicodeCharacterSequence(TMP_FontAsset.GetCharactersArray(fontAsset));
- window.m_ReferencedFontAsset = fontAsset;
- window.m_SavedFontAtlas = fontAsset.atlasTexture;
- }
- else
- {
- window.m_WarningMessage = "Font Asset [" + fontAsset.name + "] does not contain any previous \"Font Asset Creation Settings\". This usually means [" + fontAsset.name + "] was created before this new functionality was added.";
- window.m_SourceFontFile = null;
- window.m_LegacyFontAsset = fontAsset;
- }
-
- window.m_SelectedFontAsset = fontAsset;
-
- window.CheckEssentialResources();
- }
- [System.Serializable]
- class FontAssetCreationSettingsContainer
- {
- public List<FontAssetCreationSettings> fontAssetCreationSettings;
- }
- FontAssetCreationSettingsContainer m_FontAssetCreationSettingsContainer;
-
- int m_FontAssetCreationSettingsCurrentIndex = 0;
- const string k_FontAssetCreationSettingsContainerKey = "TextMeshPro.FontAssetCreator.RecentFontAssetCreationSettings.Container";
- const string k_FontAssetCreationSettingsCurrentIndexKey = "TextMeshPro.FontAssetCreator.RecentFontAssetCreationSettings.CurrentIndex";
- const float k_TwoColumnControlsWidth = 335f;
-
- System.Diagnostics.Stopwatch m_StopWatch;
- double m_GlyphPackingGenerationTime;
- double m_GlyphRenderingGenerationTime;
- string[] m_FontSizingOptions = { "Auto Sizing", "Custom Size" };
- int m_PointSizeSamplingMode;
- string[] m_FontResolutionLabels = { "8", "16","32", "64", "128", "256", "512", "1024", "2048", "4096", "8192" };
- int[] m_FontAtlasResolutions = { 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
- string[] m_FontCharacterSets = { "ASCII", "Extended ASCII", "ASCII Lowercase", "ASCII Uppercase", "Numbers + Symbols", "Custom Range", "Unicode Range (Hex)", "Custom Characters", "Characters from File" };
- enum FontPackingModes { Fast = 0, Optimum = 4 };
- FontPackingModes m_PackingMode = FontPackingModes.Fast;
- int m_CharacterSetSelectionMode;
- string m_CharacterSequence = "";
- string m_OutputFeedback = "";
- string m_WarningMessage;
- int m_CharacterCount;
- Vector2 m_ScrollPosition;
- Vector2 m_OutputScrollPosition;
- bool m_IsRepaintNeeded;
- float m_AtlasGenerationProgress;
- string m_AtlasGenerationProgressLabel = string.Empty;
- float m_RenderingProgress;
- bool m_IsGlyphPackingDone;
- bool m_IsGlyphRenderingDone;
- bool m_IsRenderingDone;
- bool m_IsProcessing;
- bool m_IsGenerationDisabled;
- bool m_IsGenerationCancelled;
- bool m_IsFontAtlasInvalid;
- Object m_SourceFontFile;
- TMP_FontAsset m_SelectedFontAsset;
- TMP_FontAsset m_LegacyFontAsset;
- TMP_FontAsset m_ReferencedFontAsset;
- TextAsset m_CharactersFromFile;
- int m_PointSize;
- int m_Padding = 5;
-
-
- GlyphRenderMode m_GlyphRenderMode = GlyphRenderMode.SDFAA;
- int m_AtlasWidth = 512;
- int m_AtlasHeight = 512;
- byte[] m_AtlasTextureBuffer;
- Texture2D m_FontAtlasTexture;
- Texture2D m_GlyphRectPreviewTexture;
- Texture2D m_SavedFontAtlas;
-
- List<Glyph> m_FontGlyphTable = new List<Glyph>();
- List<TMP_Character> m_FontCharacterTable = new List<TMP_Character>();
- Dictionary<uint, uint> m_CharacterLookupMap = new Dictionary<uint, uint>();
- Dictionary<uint, List<uint>> m_GlyphLookupMap = new Dictionary<uint, List<uint>>();
- List<Glyph> m_GlyphsToPack = new List<Glyph>();
- List<Glyph> m_GlyphsPacked = new List<Glyph>();
- List<GlyphRect> m_FreeGlyphRects = new List<GlyphRect>();
- List<GlyphRect> m_UsedGlyphRects = new List<GlyphRect>();
- List<Glyph> m_GlyphsToRender = new List<Glyph>();
- List<uint> m_AvailableGlyphsToAdd = new List<uint>();
- List<uint> m_MissingCharacters = new List<uint>();
- List<uint> m_ExcludedCharacters = new List<uint>();
- private FaceInfo m_FaceInfo;
- bool m_IncludeFontFeatures;
- public void OnEnable()
- {
-
- m_StopWatch = new System.Diagnostics.Stopwatch();
-
- minSize = new Vector2(315, minSize.y);
-
- ShaderUtilities.GetShaderPropertyIDs();
-
- if (EditorPrefs.HasKey(k_FontAssetCreationSettingsContainerKey))
- {
- if (m_FontAssetCreationSettingsContainer == null)
- m_FontAssetCreationSettingsContainer = JsonUtility.FromJson<FontAssetCreationSettingsContainer>(EditorPrefs.GetString(k_FontAssetCreationSettingsContainerKey));
- if (m_FontAssetCreationSettingsContainer.fontAssetCreationSettings != null && m_FontAssetCreationSettingsContainer.fontAssetCreationSettings.Count > 0)
- {
-
- if (EditorPrefs.HasKey(k_FontAssetCreationSettingsCurrentIndexKey))
- m_FontAssetCreationSettingsCurrentIndex = EditorPrefs.GetInt(k_FontAssetCreationSettingsCurrentIndexKey);
- LoadFontCreationSettings(m_FontAssetCreationSettingsContainer.fontAssetCreationSettings[m_FontAssetCreationSettingsCurrentIndex]);
- }
- }
- ClearGeneratedData();
- }
- public void OnDisable()
- {
-
-
- FontEngine.DestroyFontEngine();
- ClearGeneratedData();
-
- if (File.Exists("Assets/TextMesh Pro/Glyph Report.txt"))
- {
- File.Delete("Assets/TextMesh Pro/Glyph Report.txt");
- File.Delete("Assets/TextMesh Pro/Glyph Report.txt.meta");
- AssetDatabase.Refresh();
- }
-
- SaveCreationSettingsToEditorPrefs(SaveFontCreationSettings());
- EditorPrefs.SetInt(k_FontAssetCreationSettingsCurrentIndexKey, m_FontAssetCreationSettingsCurrentIndex);
-
- TMPro_EventManager.RESOURCE_LOAD_EVENT.Remove(ON_RESOURCES_LOADED);
- Resources.UnloadUnusedAssets();
- }
-
- void ON_RESOURCES_LOADED()
- {
- TMPro_EventManager.RESOURCE_LOAD_EVENT.Remove(ON_RESOURCES_LOADED);
- m_IsGenerationDisabled = false;
- }
-
- void CheckEssentialResources()
- {
- if (TMP_Settings.instance == null)
- {
- if (m_IsGenerationDisabled == false)
- TMPro_EventManager.RESOURCE_LOAD_EVENT.Add(ON_RESOURCES_LOADED);
- m_IsGenerationDisabled = true;
- }
- }
- public void OnGUI()
- {
- GUILayout.BeginHorizontal();
- DrawControls();
- if (position.width > position.height && position.width > k_TwoColumnControlsWidth)
- DrawPreview();
- GUILayout.EndHorizontal();
- }
- public void Update()
- {
- if (m_IsRepaintNeeded)
- {
-
- m_IsRepaintNeeded = false;
- Repaint();
- }
-
- if (m_IsProcessing)
- {
- m_AtlasGenerationProgress = FontEngine.generationProgress;
- m_IsRepaintNeeded = true;
- }
- if (m_IsGlyphPackingDone)
- {
- UpdateRenderFeedbackWindow();
- if (m_IsGenerationCancelled == false)
- {
- DrawGlyphRectPreviewTexture();
- Debug.Log("Glyph packing completed in: " + m_GlyphPackingGenerationTime.ToString("0.000 ms."));
- }
- m_IsGlyphPackingDone = false;
- }
- if (m_IsGlyphRenderingDone)
- {
- Debug.Log("Font Atlas generation completed in: " + m_GlyphRenderingGenerationTime.ToString("0.000 ms."));
- m_IsGlyphRenderingDone = false;
- }
-
- if (m_IsRenderingDone)
- {
- m_IsProcessing = false;
- m_IsRenderingDone = false;
- if (m_IsGenerationCancelled == false)
- {
- m_AtlasGenerationProgress = FontEngine.generationProgress;
- m_AtlasGenerationProgressLabel = "Generation completed in: " + (m_GlyphPackingGenerationTime + m_GlyphRenderingGenerationTime).ToString("0.00 ms.");
- UpdateRenderFeedbackWindow();
- CreateFontAtlasTexture();
-
- m_FontAtlasTexture.Apply(false, false);
- }
- Repaint();
- }
- }
-
-
-
-
-
- static uint[] ParseNumberSequence(string sequence)
- {
- List<uint> unicodeList = new List<uint>();
- string[] sequences = sequence.Split(',');
- foreach (string seq in sequences)
- {
- string[] s1 = seq.Split('-');
- if (s1.Length == 1)
- try
- {
- unicodeList.Add(uint.Parse(s1[0]));
- }
- catch
- {
- Debug.Log("No characters selected or invalid format.");
- }
- else
- {
- for (uint j = uint.Parse(s1[0]); j < uint.Parse(s1[1]) + 1; j++)
- {
- unicodeList.Add(j);
- }
- }
- }
- return unicodeList.ToArray();
- }
-
-
-
-
-
- static uint[] ParseHexNumberSequence(string sequence)
- {
- List<uint> unicodeList = new List<uint>();
- string[] sequences = sequence.Split(',');
- foreach (string seq in sequences)
- {
- string[] s1 = seq.Split('-');
- if (s1.Length == 1)
- try
- {
- unicodeList.Add(uint.Parse(s1[0], NumberStyles.AllowHexSpecifier));
- }
- catch
- {
- Debug.Log("No characters selected or invalid format.");
- }
- else
- {
- for (uint j = uint.Parse(s1[0], NumberStyles.AllowHexSpecifier); j < uint.Parse(s1[1], NumberStyles.AllowHexSpecifier) + 1; j++)
- {
- unicodeList.Add(j);
- }
- }
- }
- return unicodeList.ToArray();
- }
- void DrawControls()
- {
- GUILayout.Space(5f);
- if (position.width > position.height && position.width > k_TwoColumnControlsWidth)
- {
- m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition, GUILayout.Width(315));
- }
- else
- {
- m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition);
- }
- GUILayout.Space(5f);
- GUILayout.Label(m_SelectedFontAsset != null ? string.Format("Font Settings [{0}]", m_SelectedFontAsset.name) : "Font Settings", EditorStyles.boldLabel);
- EditorGUILayout.BeginVertical(EditorStyles.helpBox);
- EditorGUIUtility.labelWidth = 125f;
- EditorGUIUtility.fieldWidth = 5f;
-
- EditorGUI.BeginDisabledGroup(m_IsProcessing);
- {
-
- EditorGUI.BeginChangeCheck();
- m_SourceFontFile = EditorGUILayout.ObjectField("Source Font File", m_SourceFontFile, typeof(Font), false) as Font;
- if (EditorGUI.EndChangeCheck())
- {
- m_SelectedFontAsset = null;
- m_IsFontAtlasInvalid = true;
- }
-
- EditorGUI.BeginChangeCheck();
- if (m_PointSizeSamplingMode == 0)
- {
- m_PointSizeSamplingMode = EditorGUILayout.Popup("Sampling Point Size", m_PointSizeSamplingMode, m_FontSizingOptions);
- }
- else
- {
- GUILayout.BeginHorizontal();
- m_PointSizeSamplingMode = EditorGUILayout.Popup("Sampling Point Size", m_PointSizeSamplingMode, m_FontSizingOptions, GUILayout.Width(225));
- m_PointSize = EditorGUILayout.IntField(m_PointSize);
- GUILayout.EndHorizontal();
- }
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
-
- EditorGUI.BeginChangeCheck();
- m_Padding = EditorGUILayout.IntField("Padding", m_Padding);
- m_Padding = (int)Mathf.Clamp(m_Padding, 0f, 64f);
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
-
- EditorGUI.BeginChangeCheck();
- m_PackingMode = (FontPackingModes)EditorGUILayout.EnumPopup("Packing Method", m_PackingMode);
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
-
- GUILayout.BeginHorizontal();
- GUI.changed = false;
- EditorGUI.BeginChangeCheck();
- EditorGUILayout.PrefixLabel("Atlas Resolution");
- m_AtlasWidth = EditorGUILayout.IntPopup(m_AtlasWidth, m_FontResolutionLabels, m_FontAtlasResolutions);
- m_AtlasHeight = EditorGUILayout.IntPopup(m_AtlasHeight, m_FontResolutionLabels, m_FontAtlasResolutions);
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
- GUILayout.EndHorizontal();
-
- EditorGUI.BeginChangeCheck();
- bool hasSelectionChanged = false;
- m_CharacterSetSelectionMode = EditorGUILayout.Popup("Character Set", m_CharacterSetSelectionMode, m_FontCharacterSets);
- if (EditorGUI.EndChangeCheck())
- {
- m_CharacterSequence = "";
- hasSelectionChanged = true;
- m_IsFontAtlasInvalid = true;
- }
- switch (m_CharacterSetSelectionMode)
- {
- case 0:
-
- m_CharacterSequence = "32 - 126, 160, 8203, 8230, 9633";
- break;
- case 1:
- m_CharacterSequence = "32 - 126, 160 - 255, 8192 - 8303, 8364, 8482, 9633";
-
- break;
- case 2:
- m_CharacterSequence = "32 - 64, 91 - 126, 160";
- break;
- case 3:
- m_CharacterSequence = "32 - 96, 123 - 126, 160";
- break;
- case 4:
- m_CharacterSequence = "32 - 64, 91 - 96, 123 - 126, 160";
- break;
- case 5:
- EditorGUILayout.BeginVertical(EditorStyles.helpBox);
- GUILayout.Label("Enter a sequence of decimal values to define the characters to be included in the font asset or retrieve one from another font asset.", TMP_UIStyleManager.label);
- GUILayout.Space(10f);
- EditorGUI.BeginChangeCheck();
- m_ReferencedFontAsset = EditorGUILayout.ObjectField("Select Font Asset", m_ReferencedFontAsset, typeof(TMP_FontAsset), false) as TMP_FontAsset;
- if (EditorGUI.EndChangeCheck() || hasSelectionChanged)
- {
- if (m_ReferencedFontAsset != null)
- m_CharacterSequence = TMP_EditorUtility.GetDecimalCharacterSequence(TMP_FontAsset.GetCharactersArray(m_ReferencedFontAsset));
- m_IsFontAtlasInvalid = true;
- }
-
- char chr = Event.current.character;
- if ((chr < '0' || chr > '9') && (chr < ',' || chr > '-'))
- {
- Event.current.character = '\0';
- }
- GUILayout.Label("Character Sequence (Decimal)", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- m_CharacterSequence = EditorGUILayout.TextArea(m_CharacterSequence, TMP_UIStyleManager.textAreaBoxWindow, GUILayout.Height(120), GUILayout.ExpandWidth(true));
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
- EditorGUILayout.EndVertical();
- break;
- case 6:
- EditorGUILayout.BeginVertical(EditorStyles.helpBox);
- GUILayout.Label("Enter a sequence of Unicode (hex) values to define the characters to be included in the font asset or retrieve one from another font asset.", TMP_UIStyleManager.label);
- GUILayout.Space(10f);
- EditorGUI.BeginChangeCheck();
- m_ReferencedFontAsset = EditorGUILayout.ObjectField("Select Font Asset", m_ReferencedFontAsset, typeof(TMP_FontAsset), false) as TMP_FontAsset;
- if (EditorGUI.EndChangeCheck() || hasSelectionChanged)
- {
- if (m_ReferencedFontAsset != null)
- m_CharacterSequence = TMP_EditorUtility.GetUnicodeCharacterSequence(TMP_FontAsset.GetCharactersArray(m_ReferencedFontAsset));
- m_IsFontAtlasInvalid = true;
- }
-
- chr = Event.current.character;
- if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F') && (chr < ',' || chr > '-'))
- {
- Event.current.character = '\0';
- }
- GUILayout.Label("Character Sequence (Hex)", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- m_CharacterSequence = EditorGUILayout.TextArea(m_CharacterSequence, TMP_UIStyleManager.textAreaBoxWindow, GUILayout.Height(120), GUILayout.ExpandWidth(true));
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
- EditorGUILayout.EndVertical();
- break;
- case 7:
- EditorGUILayout.BeginVertical(EditorStyles.helpBox);
- GUILayout.Label("Type the characters to be included in the font asset or retrieve them from another font asset.", TMP_UIStyleManager.label);
- GUILayout.Space(10f);
- EditorGUI.BeginChangeCheck();
- m_ReferencedFontAsset = EditorGUILayout.ObjectField("Select Font Asset", m_ReferencedFontAsset, typeof(TMP_FontAsset), false) as TMP_FontAsset;
- if (EditorGUI.EndChangeCheck() || hasSelectionChanged)
- {
- if (m_ReferencedFontAsset != null)
- m_CharacterSequence = TMP_FontAsset.GetCharacters(m_ReferencedFontAsset);
- m_IsFontAtlasInvalid = true;
- }
- EditorGUI.indentLevel = 0;
- GUILayout.Label("Custom Character List", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- m_CharacterSequence = EditorGUILayout.TextArea(m_CharacterSequence, TMP_UIStyleManager.textAreaBoxWindow, GUILayout.Height(120), GUILayout.ExpandWidth(true));
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
- EditorGUILayout.EndVertical();
- break;
- case 8:
- EditorGUI.BeginChangeCheck();
- m_CharactersFromFile = EditorGUILayout.ObjectField("Character File", m_CharactersFromFile, typeof(TextAsset), false) as TextAsset;
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
- if (m_CharactersFromFile != null)
- {
- Regex rx = new Regex(@"(?<!\\)(?:\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})");
- m_CharacterSequence = rx.Replace(m_CharactersFromFile.text,
- match =>
- {
- if (match.Value.StartsWith("\\U"))
- return char.ConvertFromUtf32(int.Parse(match.Value.Replace("\\U", ""), NumberStyles.HexNumber));
- return char.ConvertFromUtf32(int.Parse(match.Value.Replace("\\u", ""), NumberStyles.HexNumber));
- });
- }
- break;
- }
-
-
-
-
-
-
-
-
-
-
-
- CheckForLegacyGlyphRenderMode();
- EditorGUI.BeginChangeCheck();
- m_GlyphRenderMode = (GlyphRenderMode)EditorGUILayout.EnumPopup("Render Mode", m_GlyphRenderMode);
- if (EditorGUI.EndChangeCheck())
- {
- m_IsFontAtlasInvalid = true;
- }
- m_IncludeFontFeatures = EditorGUILayout.Toggle("Get Kerning Pairs", m_IncludeFontFeatures);
- EditorGUILayout.Space();
- }
- EditorGUI.EndDisabledGroup();
- if (!string.IsNullOrEmpty(m_WarningMessage))
- {
- EditorGUILayout.HelpBox(m_WarningMessage, MessageType.Warning);
- }
- GUI.enabled = m_SourceFontFile != null && !m_IsProcessing && !m_IsGenerationDisabled;
- if (GUILayout.Button("Generate Font Atlas") && GUI.enabled)
- {
- if (!m_IsProcessing && m_SourceFontFile != null)
- {
- DestroyImmediate(m_FontAtlasTexture);
- DestroyImmediate(m_GlyphRectPreviewTexture);
- m_FontAtlasTexture = null;
- m_SavedFontAtlas = null;
- m_OutputFeedback = string.Empty;
-
- FontEngineError errorCode = FontEngine.InitializeFontEngine();
- if (errorCode != FontEngineError.Success)
- {
- Debug.Log("Font Asset Creator - Error [" + errorCode + "] has occurred while Initializing the FreeType Library.");
- }
-
- string fontPath = AssetDatabase.GetAssetPath(m_SourceFontFile);
- if (errorCode == FontEngineError.Success)
- {
- errorCode = FontEngine.LoadFontFace(fontPath);
- if (errorCode != FontEngineError.Success)
- {
- Debug.Log("Font Asset Creator - Error Code [" + errorCode + "] has occurred trying to load the [" + m_SourceFontFile.name + "] font file. This typically results from the use of an incompatible or corrupted font file.", m_SourceFontFile);
- }
- }
-
- if (errorCode == FontEngineError.Success)
- {
- uint[] characterSet = null;
-
- if (m_CharacterSetSelectionMode == 7 || m_CharacterSetSelectionMode == 8)
- {
- List<uint> char_List = new List<uint>();
- for (int i = 0; i < m_CharacterSequence.Length; i++)
- {
- uint unicode = m_CharacterSequence[i];
-
- if (i < m_CharacterSequence.Length - 1 && char.IsHighSurrogate((char)unicode) && char.IsLowSurrogate(m_CharacterSequence[i + 1]))
- {
- unicode = (uint)char.ConvertToUtf32(m_CharacterSequence[i], m_CharacterSequence[i + 1]);
- i += 1;
- }
-
- if (char_List.FindIndex(item => item == unicode) == -1)
- char_List.Add(unicode);
- }
- characterSet = char_List.ToArray();
- }
- else if (m_CharacterSetSelectionMode == 6)
- {
- characterSet = ParseHexNumberSequence(m_CharacterSequence);
- }
- else
- {
- characterSet = ParseNumberSequence(m_CharacterSequence);
- }
- m_CharacterCount = characterSet.Length;
- m_AtlasGenerationProgress = 0;
- m_IsProcessing = true;
- m_IsGenerationCancelled = false;
- GlyphLoadFlags glyphLoadFlags = ((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_HINTED) == GlyphRasterModes.RASTER_MODE_HINTED
- ? GlyphLoadFlags.LOAD_RENDER
- : GlyphLoadFlags.LOAD_RENDER | GlyphLoadFlags.LOAD_NO_HINTING;
- glyphLoadFlags = ((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_MONO) == GlyphRasterModes.RASTER_MODE_MONO
- ? glyphLoadFlags | GlyphLoadFlags.LOAD_MONOCHROME
- : glyphLoadFlags;
-
- AutoResetEvent autoEvent = new AutoResetEvent(false);
-
- ThreadPool.QueueUserWorkItem(PackGlyphs =>
- {
-
- m_StopWatch = System.Diagnostics.Stopwatch.StartNew();
-
- m_AvailableGlyphsToAdd.Clear();
- m_MissingCharacters.Clear();
- m_ExcludedCharacters.Clear();
- m_CharacterLookupMap.Clear();
- m_GlyphLookupMap.Clear();
- m_GlyphsToPack.Clear();
- m_GlyphsPacked.Clear();
-
- for (int i = 0; i < characterSet.Length; i++)
- {
- uint unicode = characterSet[i];
- uint glyphIndex;
- if (FontEngine.TryGetGlyphIndex(unicode, out glyphIndex))
- {
-
- if (m_CharacterLookupMap.ContainsKey(unicode))
- continue;
-
- m_CharacterLookupMap.Add(unicode, glyphIndex);
-
- if (m_GlyphLookupMap.ContainsKey(glyphIndex))
- {
-
- m_GlyphLookupMap[glyphIndex].Add(unicode);
- continue;
- }
-
- m_GlyphLookupMap.Add(glyphIndex, new List<uint>() { unicode });
-
- m_AvailableGlyphsToAdd.Add(glyphIndex);
- }
- else
- {
-
- m_MissingCharacters.Add(unicode);
- }
- }
-
- if (m_AvailableGlyphsToAdd.Count > 0)
- {
- int packingModifier = ((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
- if (m_PointSizeSamplingMode == 0)
- {
-
- int minPointSize = 0;
- int maxPointSize = (int)Mathf.Sqrt((m_AtlasWidth * m_AtlasHeight) / m_AvailableGlyphsToAdd.Count) * 3;
- m_PointSize = (maxPointSize + minPointSize) / 2;
- bool optimumPointSizeFound = false;
- for (int iteration = 0; iteration < 15 && optimumPointSizeFound == false; iteration++)
- {
- m_AtlasGenerationProgressLabel = "Packing glyphs - Pass (" + iteration + ")";
- FontEngine.SetFaceSize(m_PointSize);
- m_GlyphsToPack.Clear();
- m_GlyphsPacked.Clear();
- m_FreeGlyphRects.Clear();
- m_FreeGlyphRects.Add(new GlyphRect(0, 0, m_AtlasWidth - packingModifier, m_AtlasHeight - packingModifier));
- m_UsedGlyphRects.Clear();
- for (int i = 0; i < m_AvailableGlyphsToAdd.Count; i++)
- {
- uint glyphIndex = m_AvailableGlyphsToAdd[i];
- Glyph glyph;
- if (FontEngine.TryGetGlyphWithIndexValue(glyphIndex, glyphLoadFlags, out glyph))
- {
- if (glyph.glyphRect.width > 0 && glyph.glyphRect.height > 0)
- {
- m_GlyphsToPack.Add(glyph);
- }
- else
- {
- m_GlyphsPacked.Add(glyph);
- }
- }
- }
- FontEngine.TryPackGlyphsInAtlas(m_GlyphsToPack, m_GlyphsPacked, m_Padding, (GlyphPackingMode)m_PackingMode, m_GlyphRenderMode, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects);
- if (m_IsGenerationCancelled)
- {
- DestroyImmediate(m_FontAtlasTexture);
- m_FontAtlasTexture = null;
- return;
- }
-
- if (m_GlyphsToPack.Count > 0)
- {
- if (m_PointSize > minPointSize)
- {
- maxPointSize = m_PointSize;
- m_PointSize = (m_PointSize + minPointSize) / 2;
-
- }
- }
- else
- {
- if (maxPointSize - minPointSize > 1 && m_PointSize < maxPointSize)
- {
- minPointSize = m_PointSize;
- m_PointSize = (m_PointSize + maxPointSize) / 2;
-
- }
- else
- {
-
- optimumPointSizeFound = true;
- }
- }
- }
- }
- else
- {
- m_AtlasGenerationProgressLabel = "Packing glyphs...";
-
- FontEngine.SetFaceSize(m_PointSize);
- m_GlyphsToPack.Clear();
- m_GlyphsPacked.Clear();
- m_FreeGlyphRects.Clear();
- m_FreeGlyphRects.Add(new GlyphRect(0, 0, m_AtlasWidth - packingModifier, m_AtlasHeight - packingModifier));
- m_UsedGlyphRects.Clear();
- for (int i = 0; i < m_AvailableGlyphsToAdd.Count; i++)
- {
- uint glyphIndex = m_AvailableGlyphsToAdd[i];
- Glyph glyph;
- if (FontEngine.TryGetGlyphWithIndexValue(glyphIndex, glyphLoadFlags, out glyph))
- {
- if (glyph.glyphRect.width > 0 && glyph.glyphRect.height > 0)
- {
- m_GlyphsToPack.Add(glyph);
- }
- else
- {
- m_GlyphsPacked.Add(glyph);
- }
- }
- }
- FontEngine.TryPackGlyphsInAtlas(m_GlyphsToPack, m_GlyphsPacked, m_Padding, (GlyphPackingMode)m_PackingMode, m_GlyphRenderMode, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects);
- if (m_IsGenerationCancelled)
- {
- DestroyImmediate(m_FontAtlasTexture);
- m_FontAtlasTexture = null;
- return;
- }
-
- }
- }
- else
- {
- int packingModifier = ((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
- FontEngine.SetFaceSize(m_PointSize);
- m_GlyphsToPack.Clear();
- m_GlyphsPacked.Clear();
- m_FreeGlyphRects.Clear();
- m_FreeGlyphRects.Add(new GlyphRect(0, 0, m_AtlasWidth - packingModifier, m_AtlasHeight - packingModifier));
- m_UsedGlyphRects.Clear();
- }
-
- m_StopWatch.Stop();
- m_GlyphPackingGenerationTime = m_StopWatch.Elapsed.TotalMilliseconds;
- m_IsGlyphPackingDone = true;
- m_StopWatch.Reset();
- m_FontCharacterTable.Clear();
- m_FontGlyphTable.Clear();
- m_GlyphsToRender.Clear();
-
- if (m_GlyphRenderMode == GlyphRenderMode.SDF32 && m_PointSize > 512 || m_GlyphRenderMode == GlyphRenderMode.SDF16 && m_PointSize > 1024 || m_GlyphRenderMode == GlyphRenderMode.SDF8 && m_PointSize > 2048)
- {
- int upSampling = 1;
- switch (m_GlyphRenderMode)
- {
- case GlyphRenderMode.SDF8:
- upSampling = 8;
- break;
- case GlyphRenderMode.SDF16:
- upSampling = 16;
- break;
- case GlyphRenderMode.SDF32:
- upSampling = 32;
- break;
- }
- Debug.Log("Glyph rendering has been aborted due to sampling point size of [" + m_PointSize + "] x SDF [" + upSampling + "] up sampling exceeds 16,384 point size. Please revise your generation settings to make sure the sampling point size x SDF up sampling mode does not exceed 16,384.");
- m_IsRenderingDone = true;
- m_AtlasGenerationProgress = 0;
- m_IsGenerationCancelled = true;
- }
-
- foreach (Glyph glyph in m_GlyphsPacked)
- {
- uint glyphIndex = glyph.index;
- m_FontGlyphTable.Add(glyph);
-
- if (glyph.glyphRect.width > 0 && glyph.glyphRect.height > 0)
- m_GlyphsToRender.Add(glyph);
- foreach (uint unicode in m_GlyphLookupMap[glyphIndex])
- {
-
- m_FontCharacterTable.Add(new TMP_Character(unicode, glyph));
- }
- }
-
- foreach (Glyph glyph in m_GlyphsToPack)
- {
- foreach (uint unicode in m_GlyphLookupMap[glyph.index])
- {
- m_ExcludedCharacters.Add(unicode);
- }
- }
-
- m_FaceInfo = FontEngine.GetFaceInfo();
- autoEvent.Set();
- });
-
- ThreadPool.QueueUserWorkItem(RenderGlyphs =>
- {
- autoEvent.WaitOne();
- if (m_IsGenerationCancelled == false)
- {
-
- m_StopWatch = System.Diagnostics.Stopwatch.StartNew();
- m_IsRenderingDone = false;
-
- m_AtlasTextureBuffer = new byte[m_AtlasWidth * m_AtlasHeight];
- m_AtlasGenerationProgressLabel = "Rendering glyphs...";
-
- if (m_GlyphsToRender.Count > 0)
- {
- FontEngine.RenderGlyphsToTexture(m_GlyphsToRender, m_Padding, m_GlyphRenderMode, m_AtlasTextureBuffer, m_AtlasWidth, m_AtlasHeight);
- }
- m_IsRenderingDone = true;
-
- m_StopWatch.Stop();
- m_GlyphRenderingGenerationTime = m_StopWatch.Elapsed.TotalMilliseconds;
- m_IsGlyphRenderingDone = true;
- m_StopWatch.Reset();
- }
- });
- }
- SaveCreationSettingsToEditorPrefs(SaveFontCreationSettings());
- }
- }
-
- GUILayout.Space(1);
- Rect progressRect = EditorGUILayout.GetControlRect(false, 20);
- GUI.enabled = true;
- progressRect.width -= 22;
- EditorGUI.ProgressBar(progressRect, Mathf.Max(0.01f, m_AtlasGenerationProgress), m_AtlasGenerationProgressLabel);
- progressRect.x = progressRect.x + progressRect.width + 2;
- progressRect.y -= 1;
- progressRect.width = 20;
- progressRect.height = 20;
- GUI.enabled = m_IsProcessing;
- if (GUI.Button(progressRect, "X"))
- {
- FontEngine.SendCancellationRequest();
- m_AtlasGenerationProgress = 0;
- m_IsProcessing = false;
- m_IsGenerationCancelled = true;
- }
- GUILayout.Space(5);
-
- GUI.enabled = true;
- GUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.Height(200));
- m_OutputScrollPosition = EditorGUILayout.BeginScrollView(m_OutputScrollPosition);
- EditorGUILayout.LabelField(m_OutputFeedback, TMP_UIStyleManager.label);
- EditorGUILayout.EndScrollView();
- GUILayout.EndVertical();
-
- GUI.enabled = m_FontAtlasTexture != null && !m_IsProcessing;
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("Save") && GUI.enabled)
- {
- if (m_SelectedFontAsset == null)
- {
- if (m_LegacyFontAsset != null)
- SaveNewFontAssetWithSameName(m_LegacyFontAsset);
- else
- SaveNewFontAsset(m_SourceFontFile);
- }
- else
- {
-
- string filePath = Path.GetFullPath(AssetDatabase.GetAssetPath(m_SelectedFontAsset)).Replace('\\', '/');
- if (((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
- Save_Bitmap_FontAsset(filePath);
- else
- Save_SDF_FontAsset(filePath);
- }
- }
- if (GUILayout.Button("Save as...") && GUI.enabled)
- {
- if (m_SelectedFontAsset == null)
- {
- SaveNewFontAsset(m_SourceFontFile);
- }
- else
- {
- SaveNewFontAssetWithSameName(m_SelectedFontAsset);
- }
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- EditorGUILayout.EndVertical();
- GUI.enabled = true;
- if (position.height > position.width || position.width < k_TwoColumnControlsWidth)
- DrawPreview();
- EditorGUILayout.EndScrollView();
- if (m_IsFontAtlasInvalid)
- ClearGeneratedData();
- }
-
-
-
- void ClearGeneratedData()
- {
- m_IsFontAtlasInvalid = false;
- if (m_FontAtlasTexture != null && !EditorUtility.IsPersistent(m_FontAtlasTexture))
- {
- DestroyImmediate(m_FontAtlasTexture);
- m_FontAtlasTexture = null;
- }
- if (m_GlyphRectPreviewTexture != null)
- {
- DestroyImmediate(m_GlyphRectPreviewTexture);
- m_GlyphRectPreviewTexture = null;
- }
- m_AtlasGenerationProgressLabel = string.Empty;
- m_AtlasGenerationProgress = 0;
- m_SavedFontAtlas = null;
- m_OutputFeedback = string.Empty;
- m_WarningMessage = string.Empty;
- }
-
-
-
- void UpdateRenderFeedbackWindow()
- {
- m_PointSize = m_FaceInfo.pointSize;
- string missingGlyphReport = string.Empty;
-
- string colorTag2 = "<color=#C0ffff>";
- missingGlyphReport = "Font: <b>" + colorTag2 + m_FaceInfo.familyName + "</color></b> Style: <b>" + colorTag2 + m_FaceInfo.styleName + "</color></b>";
- missingGlyphReport += "\nPoint Size: <b>" + colorTag2 + m_FaceInfo.pointSize + "</color></b> SP/PD Ratio: <b>" + colorTag2 + ((float)m_Padding / m_FaceInfo.pointSize).ToString("0.0%" + "</color></b>");
- missingGlyphReport += "\n\nCharacters included: <color=#ffff00><b>" + m_FontCharacterTable.Count + "/" + m_CharacterCount + "</b></color>";
- missingGlyphReport += "\nMissing characters: <color=#ffff00><b>" + m_MissingCharacters.Count + "</b></color>";
- missingGlyphReport += "\nExcluded characters: <color=#ffff00><b>" + m_ExcludedCharacters.Count + "</b></color>";
-
- missingGlyphReport += "\n\n<b><color=#ffff00>Characters missing from font file:</color></b>";
- missingGlyphReport += "\n----------------------------------------";
- m_OutputFeedback = missingGlyphReport;
- for (int i = 0; i < m_MissingCharacters.Count; i++)
- {
- missingGlyphReport += "\nID: <color=#C0ffff>" + m_MissingCharacters[i] + "\t</color>Hex: <color=#C0ffff>" + m_MissingCharacters[i].ToString("X") + "\t</color>Char [<color=#C0ffff>" + (char)m_MissingCharacters[i] + "</color>]";
- if (missingGlyphReport.Length < 16300)
- m_OutputFeedback = missingGlyphReport;
- }
-
- missingGlyphReport += "\n\n<b><color=#ffff00>Characters excluded from packing:</color></b>";
- missingGlyphReport += "\n----------------------------------------";
- for (int i = 0; i < m_ExcludedCharacters.Count; i++)
- {
- missingGlyphReport += "\nID: <color=#C0ffff>" + m_ExcludedCharacters[i] + "\t</color>Hex: <color=#C0ffff>" + m_ExcludedCharacters[i].ToString("X") + "\t</color>Char [<color=#C0ffff>" + (char)m_ExcludedCharacters[i] + "</color>]";
- if (missingGlyphReport.Length < 16300)
- m_OutputFeedback = missingGlyphReport;
- }
- if (missingGlyphReport.Length > 16300)
- m_OutputFeedback += "\n\n<color=#ffff00>Report truncated.</color>\n<color=#c0ffff>See</color> \"TextMesh Pro\\Glyph Report.txt\"";
-
- if (Directory.Exists("Assets/TextMesh Pro"))
- {
- missingGlyphReport = System.Text.RegularExpressions.Regex.Replace(missingGlyphReport, @"<[^>]*>", string.Empty);
- File.WriteAllText("Assets/TextMesh Pro/Glyph Report.txt", missingGlyphReport);
- AssetDatabase.Refresh();
- }
- }
- void DrawGlyphRectPreviewTexture()
- {
- if (m_GlyphRectPreviewTexture != null)
- DestroyImmediate(m_GlyphRectPreviewTexture);
- m_GlyphRectPreviewTexture = new Texture2D(m_AtlasWidth, m_AtlasHeight, TextureFormat.RGBA32, false, true);
- FontEngine.ResetAtlasTexture(m_GlyphRectPreviewTexture);
- foreach (Glyph glyph in m_GlyphsPacked)
- {
- GlyphRect glyphRect = glyph.glyphRect;
- Color c = UnityEngine.Random.ColorHSV(0f, 1f, 0.5f, 0.5f, 1.0f, 1.0f);
- int x0 = glyphRect.x;
- int x1 = x0 + glyphRect.width;
- int y0 = glyphRect.y;
- int y1 = y0 + glyphRect.height;
-
- for (int x = x0; x < x1; x++)
- {
- for (int y = y0; y < y1; y++)
- m_GlyphRectPreviewTexture.SetPixel(x, y, c);
- }
- }
- m_GlyphRectPreviewTexture.Apply(false);
- }
- void CreateFontAtlasTexture()
- {
- if (m_FontAtlasTexture != null)
- DestroyImmediate(m_FontAtlasTexture);
- m_FontAtlasTexture = new Texture2D(m_AtlasWidth, m_AtlasHeight, TextureFormat.Alpha8, false, true);
- Color32[] colors = new Color32[m_AtlasWidth * m_AtlasHeight];
- for (int i = 0; i < colors.Length; i++)
- {
- byte c = m_AtlasTextureBuffer[i];
- colors[i] = new Color32(c, c, c, c);
- }
-
- m_AtlasTextureBuffer = null;
- if ((m_GlyphRenderMode & GlyphRenderMode.RASTER) == GlyphRenderMode.RASTER || (m_GlyphRenderMode & GlyphRenderMode.RASTER_HINTED) == GlyphRenderMode.RASTER_HINTED)
- m_FontAtlasTexture.filterMode = FilterMode.Point;
- m_FontAtlasTexture.SetPixels32(colors, 0);
- m_FontAtlasTexture.Apply(false, false);
-
-
-
- }
-
-
-
-
- void SaveNewFontAsset(Object sourceObject)
- {
- string filePath;
-
- string saveDirectory = new FileInfo(AssetDatabase.GetAssetPath(sourceObject)).DirectoryName;
- if (((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
- {
- filePath = EditorUtility.SaveFilePanel("Save TextMesh Pro! Font Asset File", saveDirectory, sourceObject.name, "asset");
- if (filePath.Length == 0)
- return;
- Save_Bitmap_FontAsset(filePath);
- }
- else
- {
- filePath = EditorUtility.SaveFilePanel("Save TextMesh Pro! Font Asset File", saveDirectory, sourceObject.name + " SDF", "asset");
- if (filePath.Length == 0)
- return;
- Save_SDF_FontAsset(filePath);
- }
- }
-
-
-
-
- void SaveNewFontAssetWithSameName(Object sourceObject)
- {
- string filePath;
-
- string saveDirectory = new FileInfo(AssetDatabase.GetAssetPath(sourceObject)).DirectoryName;
- filePath = EditorUtility.SaveFilePanel("Save TextMesh Pro! Font Asset File", saveDirectory, sourceObject.name, "asset");
- if (filePath.Length == 0)
- return;
- if (((GlyphRasterModes)m_GlyphRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
- {
- Save_Bitmap_FontAsset(filePath);
- }
- else
- {
- Save_SDF_FontAsset(filePath);
- }
- }
- void Save_Bitmap_FontAsset(string filePath)
- {
- filePath = filePath.Substring(0, filePath.Length - 6);
- string dataPath = Application.dataPath;
- if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1)
- {
- Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\"");
- return;
- }
- string relativeAssetPath = filePath.Substring(dataPath.Length - 6);
- string tex_DirName = Path.GetDirectoryName(relativeAssetPath);
- string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath);
- string tex_Path_NoExt = tex_DirName + "/" + tex_FileName;
-
- TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TMP_FontAsset)) as TMP_FontAsset;
- if (fontAsset == null)
- {
-
- fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
- AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset");
-
- fontAsset.version = "1.1.0";
-
- fontAsset.atlasRenderMode = m_GlyphRenderMode;
-
- fontAsset.m_SourceFontFile_EditorRef = (Font)m_SourceFontFile;
- fontAsset.m_SourceFontFileGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_SourceFontFile));
-
- fontAsset.faceInfo = m_FaceInfo;
-
- fontAsset.glyphTable = m_FontGlyphTable;
-
- fontAsset.characterTable = m_FontCharacterTable;
-
- fontAsset.SortAllTables();
-
- if (m_IncludeFontFeatures)
- fontAsset.fontFeatureTable = GetKerningTable();
-
- fontAsset.atlasTextures = new Texture2D[] { m_FontAtlasTexture };
- m_FontAtlasTexture.name = tex_FileName + " Atlas";
- fontAsset.atlasWidth = m_AtlasWidth;
- fontAsset.atlasHeight = m_AtlasHeight;
- fontAsset.atlasPadding = m_Padding;
- AssetDatabase.AddObjectToAsset(m_FontAtlasTexture, fontAsset);
-
- Shader default_Shader = Shader.Find("TextMeshPro/Bitmap");
- Material tmp_material = new Material(default_Shader);
- tmp_material.name = tex_FileName + " Material";
- tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlasTexture);
- fontAsset.material = tmp_material;
- AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
- }
- else
- {
-
- Material[] material_references = TMP_EditorUtility.FindMaterialReferences(fontAsset);
-
- fontAsset.version = "1.1.0";
-
- if (fontAsset.m_glyphInfoList != null && fontAsset.m_glyphInfoList.Count > 0)
- fontAsset.m_glyphInfoList = null;
-
- fontAsset.atlasRenderMode = m_GlyphRenderMode;
-
- fontAsset.faceInfo = m_FaceInfo;
-
- fontAsset.glyphTable = m_FontGlyphTable;
-
- fontAsset.characterTable = m_FontCharacterTable;
-
- fontAsset.SortAllTables();
-
- if (m_IncludeFontFeatures)
- fontAsset.fontFeatureTable = GetKerningTable();
-
- if (fontAsset.atlasTextures != null && fontAsset.atlasTextures.Length > 0)
- {
- for (int i = 1; i < fontAsset.atlasTextures.Length; i++)
- DestroyImmediate(fontAsset.atlasTextures[i], true);
- }
- fontAsset.m_AtlasTextureIndex = 0;
- fontAsset.atlasWidth = m_AtlasWidth;
- fontAsset.atlasHeight = m_AtlasHeight;
- fontAsset.atlasPadding = m_Padding;
-
- Texture2D tex = fontAsset.atlasTextures[0];
- tex.name = tex_FileName + " Atlas";
-
- bool isReadableState = tex.isReadable;
- if (isReadableState == false)
- FontEngineEditorUtilities.SetAtlasTextureIsReadable(tex, true);
- if (tex.width != m_AtlasWidth || tex.height != m_AtlasHeight)
- {
- tex.Resize(m_AtlasWidth, m_AtlasHeight);
- tex.Apply(false);
- }
-
- Graphics.CopyTexture(m_FontAtlasTexture, tex);
-
- tex.Apply(false);
-
- m_FontAtlasTexture.hideFlags = HideFlags.None;
- fontAsset.material.hideFlags = HideFlags.None;
-
-
-
-
-
-
-
-
-
-
- }
-
- FontEngineEditorUtilities.SetAtlasTextureIsReadable(fontAsset.atlasTexture, false);
-
- fontAsset.freeGlyphRects = m_FreeGlyphRects;
- fontAsset.usedGlyphRects = m_UsedGlyphRects;
-
- m_SelectedFontAsset = fontAsset;
- m_LegacyFontAsset = null;
- fontAsset.creationSettings = SaveFontCreationSettings();
- AssetDatabase.SaveAssets();
- AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset));
-
- fontAsset.ReadFontAssetDefinition();
- AssetDatabase.Refresh();
- m_FontAtlasTexture = null;
-
- TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
- }
- void Save_SDF_FontAsset(string filePath)
- {
- filePath = filePath.Substring(0, filePath.Length - 6);
- string dataPath = Application.dataPath;
- if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1)
- {
- Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\"");
- return;
- }
- string relativeAssetPath = filePath.Substring(dataPath.Length - 6);
- string tex_DirName = Path.GetDirectoryName(relativeAssetPath);
- string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath);
- string tex_Path_NoExt = tex_DirName + "/" + tex_FileName;
-
- TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(tex_Path_NoExt + ".asset");
- if (fontAsset == null)
- {
-
- fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
- AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset");
-
- fontAsset.version = "1.1.0";
-
- fontAsset.m_SourceFontFile_EditorRef = (Font)m_SourceFontFile;
- fontAsset.m_SourceFontFileGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_SourceFontFile));
-
- fontAsset.atlasRenderMode = m_GlyphRenderMode;
-
- fontAsset.faceInfo = m_FaceInfo;
-
- fontAsset.glyphTable = m_FontGlyphTable;
-
- fontAsset.characterTable = m_FontCharacterTable;
-
- fontAsset.SortAllTables();
-
- if (m_IncludeFontFeatures)
- fontAsset.fontFeatureTable = GetKerningTable();
-
- fontAsset.atlasTextures = new Texture2D[] { m_FontAtlasTexture };
- m_FontAtlasTexture.name = tex_FileName + " Atlas";
- fontAsset.atlasWidth = m_AtlasWidth;
- fontAsset.atlasHeight = m_AtlasHeight;
- fontAsset.atlasPadding = m_Padding;
- AssetDatabase.AddObjectToAsset(m_FontAtlasTexture, fontAsset);
-
- Shader default_Shader = Shader.Find("TextMeshPro/Distance Field");
- Material tmp_material = new Material(default_Shader);
- tmp_material.name = tex_FileName + " Material";
- tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlasTexture);
- tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlasTexture.width);
- tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlasTexture.height);
- int spread = m_Padding + 1;
- tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread);
- tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
- tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
- fontAsset.material = tmp_material;
- AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
- }
- else
- {
-
- Material[] material_references = TMP_EditorUtility.FindMaterialReferences(fontAsset);
-
- fontAsset.version = "1.1.0";
-
- if (fontAsset.m_glyphInfoList != null && fontAsset.m_glyphInfoList.Count > 0)
- fontAsset.m_glyphInfoList = null;
-
- fontAsset.atlasRenderMode = m_GlyphRenderMode;
-
- fontAsset.faceInfo = m_FaceInfo;
-
- fontAsset.glyphTable = m_FontGlyphTable;
-
- fontAsset.characterTable = m_FontCharacterTable;
-
- fontAsset.SortAllTables();
-
-
- if (m_IncludeFontFeatures)
- fontAsset.fontFeatureTable = GetKerningTable();
-
- if (fontAsset.atlasTextures != null && fontAsset.atlasTextures.Length > 0)
- {
- for (int i = 1; i < fontAsset.atlasTextures.Length; i++)
- DestroyImmediate(fontAsset.atlasTextures[i], true);
- }
- fontAsset.m_AtlasTextureIndex = 0;
- fontAsset.atlasWidth = m_AtlasWidth;
- fontAsset.atlasHeight = m_AtlasHeight;
- fontAsset.atlasPadding = m_Padding;
-
- Texture2D tex = fontAsset.atlasTextures[0];
- tex.name = tex_FileName + " Atlas";
-
- bool isReadableState = tex.isReadable;
- if (isReadableState == false)
- FontEngineEditorUtilities.SetAtlasTextureIsReadable(tex, true);
- if (tex.width != m_AtlasWidth || tex.height != m_AtlasHeight)
- {
- tex.Resize(m_AtlasWidth, m_AtlasHeight);
- tex.Apply(false);
- }
-
- Graphics.CopyTexture(m_FontAtlasTexture, tex);
-
- tex.Apply(false);
-
- m_FontAtlasTexture.hideFlags = HideFlags.None;
- fontAsset.material.hideFlags = HideFlags.None;
-
- for (int i = 0; i < material_references.Length; i++)
- {
- material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, tex.width);
- material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, tex.height);
- int spread = m_Padding + 1;
- material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread);
- material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
- material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
- }
- }
-
-
-
-
- FontEngineEditorUtilities.SetAtlasTextureIsReadable(fontAsset.atlasTexture, false);
-
- fontAsset.freeGlyphRects = m_FreeGlyphRects;
- fontAsset.usedGlyphRects = m_UsedGlyphRects;
-
- m_SelectedFontAsset = fontAsset;
- m_LegacyFontAsset = null;
- fontAsset.creationSettings = SaveFontCreationSettings();
- AssetDatabase.SaveAssets();
- AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset));
- fontAsset.ReadFontAssetDefinition();
- AssetDatabase.Refresh();
- m_FontAtlasTexture = null;
-
- TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
- }
-
-
-
-
- FontAssetCreationSettings SaveFontCreationSettings()
- {
- FontAssetCreationSettings settings = new FontAssetCreationSettings();
-
- settings.sourceFontFileGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_SourceFontFile));
- settings.pointSizeSamplingMode = m_PointSizeSamplingMode;
- settings.pointSize = m_PointSize;
- settings.padding = m_Padding;
- settings.packingMode = (int)m_PackingMode;
- settings.atlasWidth = m_AtlasWidth;
- settings.atlasHeight = m_AtlasHeight;
- settings.characterSetSelectionMode = m_CharacterSetSelectionMode;
- settings.characterSequence = m_CharacterSequence;
- settings.referencedFontAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_ReferencedFontAsset));
- settings.referencedTextAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_CharactersFromFile));
-
-
- settings.renderMode = (int)m_GlyphRenderMode;
- settings.includeFontFeatures = m_IncludeFontFeatures;
- return settings;
- }
-
-
-
-
- void LoadFontCreationSettings(FontAssetCreationSettings settings)
- {
- m_SourceFontFile = AssetDatabase.LoadAssetAtPath<Font>(AssetDatabase.GUIDToAssetPath(settings.sourceFontFileGUID));
- m_PointSizeSamplingMode = settings.pointSizeSamplingMode;
- m_PointSize = settings.pointSize;
- m_Padding = settings.padding;
- m_PackingMode = (FontPackingModes)settings.packingMode;
- m_AtlasWidth = settings.atlasWidth;
- m_AtlasHeight = settings.atlasHeight;
- m_CharacterSetSelectionMode = settings.characterSetSelectionMode;
- m_CharacterSequence = settings.characterSequence;
- m_ReferencedFontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(AssetDatabase.GUIDToAssetPath(settings.referencedFontAssetGUID));
- m_CharactersFromFile = AssetDatabase.LoadAssetAtPath<TextAsset>(AssetDatabase.GUIDToAssetPath(settings.referencedTextAssetGUID));
-
-
- m_GlyphRenderMode = (GlyphRenderMode)settings.renderMode;
- m_IncludeFontFeatures = settings.includeFontFeatures;
- }
-
-
-
-
- void SaveCreationSettingsToEditorPrefs(FontAssetCreationSettings settings)
- {
-
- if (m_FontAssetCreationSettingsContainer == null)
- {
- m_FontAssetCreationSettingsContainer = new FontAssetCreationSettingsContainer();
- m_FontAssetCreationSettingsContainer.fontAssetCreationSettings = new List<FontAssetCreationSettings>();
- }
-
- m_FontAssetCreationSettingsContainer.fontAssetCreationSettings.Add(settings);
-
- if (m_FontAssetCreationSettingsContainer.fontAssetCreationSettings.Count > 4)
- m_FontAssetCreationSettingsContainer.fontAssetCreationSettings.RemoveAt(0);
- m_FontAssetCreationSettingsCurrentIndex = m_FontAssetCreationSettingsContainer.fontAssetCreationSettings.Count - 1;
-
- string serializedSettings = JsonUtility.ToJson(m_FontAssetCreationSettingsContainer, true);
- EditorPrefs.SetString(k_FontAssetCreationSettingsContainerKey, serializedSettings);
- }
- void DrawPreview()
- {
- Rect pixelRect;
- float ratioX = (position.width - k_TwoColumnControlsWidth) / m_AtlasWidth;
- float ratioY = (position.height - 15) / m_AtlasHeight;
- if (position.width < position.height)
- {
- ratioX = (position.width - 15) / m_AtlasWidth;
- ratioY = (position.height - 485) / m_AtlasHeight;
- }
- if (ratioX < ratioY)
- {
- float width = m_AtlasWidth * ratioX;
- float height = m_AtlasHeight * ratioX;
- EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.MaxWidth(width), GUILayout.MaxHeight(height));
- pixelRect = GUILayoutUtility.GetRect(width - 5, height, GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(false));
- }
- else
- {
- float width = m_AtlasWidth * ratioY;
- float height = m_AtlasHeight * ratioY;
- EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.MaxWidth(width), GUILayout.MaxHeight(height));
- pixelRect = GUILayoutUtility.GetRect(width - 5, height, GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(false));
- }
- if (m_FontAtlasTexture != null)
- {
- EditorGUI.DrawTextureAlpha(pixelRect, m_FontAtlasTexture, ScaleMode.StretchToFill);
-
- if (m_GlyphRectPreviewTexture != null)
- {
- DestroyImmediate(m_GlyphRectPreviewTexture);
- m_GlyphRectPreviewTexture = null;
- }
- }
- else if (m_GlyphRectPreviewTexture != null)
- {
- EditorGUI.DrawPreviewTexture(pixelRect, m_GlyphRectPreviewTexture, null, ScaleMode.StretchToFill);
- }
- else if (m_SavedFontAtlas != null)
- {
- EditorGUI.DrawTextureAlpha(pixelRect, m_SavedFontAtlas, ScaleMode.StretchToFill);
- }
- EditorGUILayout.EndVertical();
- }
- void CheckForLegacyGlyphRenderMode()
- {
-
- if ((int)m_GlyphRenderMode < 0x100)
- {
- switch ((int)m_GlyphRenderMode)
- {
- case 0:
- m_GlyphRenderMode = GlyphRenderMode.SMOOTH_HINTED;
- break;
- case 1:
- m_GlyphRenderMode = GlyphRenderMode.SMOOTH;
- break;
- case 2:
- m_GlyphRenderMode = GlyphRenderMode.RASTER_HINTED;
- break;
- case 3:
- m_GlyphRenderMode = GlyphRenderMode.RASTER;
- break;
- case 6:
- case 7:
- m_GlyphRenderMode = GlyphRenderMode.SDFAA;
- break;
- }
- }
- }
-
- public TMP_FontFeatureTable GetKerningTable()
- {
- GlyphPairAdjustmentRecord[] adjustmentRecords = FontEngine.GetGlyphPairAdjustmentTable(m_AvailableGlyphsToAdd.ToArray());
- if (adjustmentRecords == null)
- return null;
- TMP_FontFeatureTable fontFeatureTable = new TMP_FontFeatureTable();
- for (int i = 0; i < adjustmentRecords.Length && adjustmentRecords[i].firstAdjustmentRecord.glyphIndex != 0; i++)
- {
- fontFeatureTable.glyphPairAdjustmentRecords.Add(new TMP_GlyphPairAdjustmentRecord(adjustmentRecords[i]));
- }
- fontFeatureTable.SortGlyphPairAdjustmentRecords();
- return fontFeatureTable;
- }
- }
- }
|