TMP_GlyphPairAdjustmentRecordPropertyDrawer.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using UnityEngine.TextCore.LowLevel;
  4. using UnityEditor;
  5. using System.Collections;
  6. using System.Text.RegularExpressions;
  7. namespace TMPro.EditorUtilities
  8. {
  9. [CustomPropertyDrawer(typeof(TMP_GlyphPairAdjustmentRecord))]
  10. public class TMP_GlyphPairAdjustmentRecordPropertyDrawer : PropertyDrawer
  11. {
  12. private bool isEditingEnabled = false;
  13. private bool isSelectable = false;
  14. private string m_FirstCharacter = string.Empty;
  15. private string m_SecondCharacter = string.Empty;
  16. private string m_PreviousInput;
  17. static GUIContent s_CharacterTextFieldLabel = new GUIContent("Char:", "Enter the character or its UTF16 or UTF32 Unicode character escape sequence. For UTF16 use \"\\uFF00\" and for UTF32 use \"\\UFF00FF00\" representation.");
  18. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  19. {
  20. SerializedProperty prop_FirstAdjustmentRecord = property.FindPropertyRelative("m_FirstAdjustmentRecord");
  21. SerializedProperty prop_SecondAdjustmentRecord = property.FindPropertyRelative("m_SecondAdjustmentRecord");
  22. SerializedProperty prop_FirstGlyphIndex = prop_FirstAdjustmentRecord.FindPropertyRelative("m_GlyphIndex");
  23. SerializedProperty prop_FirstGlyphValueRecord = prop_FirstAdjustmentRecord.FindPropertyRelative("m_GlyphValueRecord");
  24. SerializedProperty prop_SecondGlyphIndex = prop_SecondAdjustmentRecord.FindPropertyRelative("m_GlyphIndex");
  25. SerializedProperty prop_SecondGlyphValueRecord = prop_SecondAdjustmentRecord.FindPropertyRelative("m_GlyphValueRecord");
  26. SerializedProperty prop_FontFeatureLookupFlags = property.FindPropertyRelative("m_FeatureLookupFlags");
  27. position.yMin += 2;
  28. float width = position.width / 2;
  29. float padding = 5.0f;
  30. Rect rect;
  31. isEditingEnabled = GUI.enabled;
  32. isSelectable = label.text == "Selectable" ? true : false;
  33. if (isSelectable)
  34. GUILayoutUtility.GetRect(position.width, 75);
  35. else
  36. GUILayoutUtility.GetRect(position.width, 55);
  37. GUIStyle style = new GUIStyle(EditorStyles.label);
  38. style.richText = true;
  39. // First Glyph
  40. GUI.enabled = isEditingEnabled;
  41. if (isSelectable)
  42. {
  43. rect = new Rect(position.x + 70, position.y, position.width, 49);
  44. float labelWidth = GUI.skin.label.CalcSize(new GUIContent("ID: " + prop_FirstGlyphIndex.intValue)).x;
  45. EditorGUI.LabelField(new Rect(position.x + (64 - labelWidth) / 2, position.y + 60, 64f, 18f), new GUIContent("ID: <color=#FFFF80>" + prop_FirstGlyphIndex.intValue + "</color>"), style);
  46. GUI.enabled = isEditingEnabled;
  47. EditorGUIUtility.labelWidth = 30f;
  48. rect = new Rect(position.x + 70, position.y + 10, (width - 70) - padding, 18);
  49. EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_XPlacement"), new GUIContent("OX:"));
  50. rect.y += 20;
  51. EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_YPlacement"), new GUIContent("OY:"));
  52. rect.y += 20;
  53. EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_XAdvance"), new GUIContent("AX:"));
  54. //rect.y += 20;
  55. //EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_YAdvance"), new GUIContent("AY:"));
  56. DrawGlyph((uint)prop_FirstGlyphIndex.intValue, new Rect(position.x, position.y, position.width, position.height), property);
  57. }
  58. else
  59. {
  60. rect = new Rect(position.x, position.y, width / 2 * 0.8f - padding, 18);
  61. EditorGUIUtility.labelWidth = 40f;
  62. // First Character Lookup
  63. GUI.SetNextControlName("FirstCharacterField");
  64. EditorGUI.BeginChangeCheck();
  65. string firstCharacter = EditorGUI.TextField(rect, s_CharacterTextFieldLabel, m_FirstCharacter);
  66. if (GUI.GetNameOfFocusedControl() == "FirstCharacterField")
  67. {
  68. if (ValidateInput(firstCharacter))
  69. {
  70. //Debug.Log("1st Unicode value: [" + firstCharacter + "]");
  71. uint unicode = GetUnicodeCharacter(firstCharacter);
  72. // Lookup glyph index
  73. TMP_SerializedPropertyHolder propertyHolder = property.serializedObject.targetObject as TMP_SerializedPropertyHolder;
  74. TMP_FontAsset fontAsset = propertyHolder.fontAsset;
  75. if (fontAsset != null)
  76. {
  77. prop_FirstGlyphIndex.intValue = (int)fontAsset.GetGlyphIndex(unicode);
  78. propertyHolder.firstCharacter = unicode;
  79. }
  80. }
  81. }
  82. if (EditorGUI.EndChangeCheck())
  83. m_FirstCharacter = firstCharacter;
  84. // First Glyph Index
  85. rect.x += width / 2 * 0.8f;
  86. EditorGUIUtility.labelWidth = 25f;
  87. EditorGUI.BeginChangeCheck();
  88. EditorGUI.PropertyField(rect, prop_FirstGlyphIndex, new GUIContent("ID:"));
  89. if (EditorGUI.EndChangeCheck())
  90. {
  91. }
  92. GUI.enabled = isEditingEnabled;
  93. EditorGUIUtility.labelWidth = 25f;
  94. rect = new Rect(position.x, position.y + 20, width * 0.5f - padding, 18);
  95. EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_XPlacement"), new GUIContent("OX"));
  96. rect.x += width * 0.5f;
  97. EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_YPlacement"), new GUIContent("OY"));
  98. rect.x = position.x;
  99. rect.y += 20;
  100. EditorGUI.PropertyField(rect, prop_FirstGlyphValueRecord.FindPropertyRelative("m_XAdvance"), new GUIContent("AX"));
  101. //rect.x += width * 0.5f;
  102. //EditorGUI.PropertyField(rect, prop_FirstGlyphAdjustment.FindPropertyRelative("m_YAdvance"), new GUIContent("AY"));
  103. }
  104. // Second Glyph
  105. GUI.enabled = isEditingEnabled;
  106. if (isSelectable)
  107. {
  108. float labelWidth = GUI.skin.label.CalcSize(new GUIContent("ID: " + prop_SecondGlyphIndex.intValue)).x;
  109. EditorGUI.LabelField(new Rect(position.width / 2 + 20 + (64 - labelWidth) / 2, position.y + 60, 64f, 18f), new GUIContent("ID: <color=#FFFF80>" + prop_SecondGlyphIndex.intValue + "</color>"), style);
  110. GUI.enabled = isEditingEnabled;
  111. EditorGUIUtility.labelWidth = 30f;
  112. rect = new Rect(position.width / 2 + 20 + 70, position.y + 10, (width - 70) - padding, 18);
  113. EditorGUI.PropertyField(rect, prop_SecondGlyphValueRecord.FindPropertyRelative("m_XPlacement"), new GUIContent("OX:"));
  114. rect.y += 20;
  115. EditorGUI.PropertyField(rect, prop_SecondGlyphValueRecord.FindPropertyRelative("m_YPlacement"), new GUIContent("OY:"));
  116. rect.y += 20;
  117. EditorGUI.PropertyField(rect, prop_SecondGlyphValueRecord.FindPropertyRelative("m_XAdvance"), new GUIContent("AX:"));
  118. //rect.y += 20;
  119. //EditorGUI.PropertyField(rect, prop_SecondGlyphAdjustment.FindPropertyRelative("m_YAdvance"), new GUIContent("AY"));
  120. DrawGlyph((uint)prop_SecondGlyphIndex.intValue, new Rect(position.width / 2 + 20, position.y, position.width, position.height), property);
  121. }
  122. else
  123. {
  124. rect = new Rect(position.width / 2 + 20, position.y, width / 2 * 0.8f - padding, 18);
  125. EditorGUIUtility.labelWidth = 40f;
  126. // Second Character Lookup
  127. GUI.SetNextControlName("SecondCharacterField");
  128. EditorGUI.BeginChangeCheck();
  129. string secondCharacter = EditorGUI.TextField(rect, s_CharacterTextFieldLabel, m_SecondCharacter);
  130. if (GUI.GetNameOfFocusedControl() == "SecondCharacterField")
  131. {
  132. if (ValidateInput(secondCharacter))
  133. {
  134. //Debug.Log("2nd Unicode value: [" + secondCharacter + "]");
  135. uint unicode = GetUnicodeCharacter(secondCharacter);
  136. // Lookup glyph index
  137. TMP_SerializedPropertyHolder propertyHolder = property.serializedObject.targetObject as TMP_SerializedPropertyHolder;
  138. TMP_FontAsset fontAsset = propertyHolder.fontAsset;
  139. if (fontAsset != null)
  140. {
  141. prop_SecondGlyphIndex.intValue = (int)fontAsset.GetGlyphIndex(unicode);
  142. propertyHolder.secondCharacter = unicode;
  143. }
  144. }
  145. }
  146. if (EditorGUI.EndChangeCheck())
  147. m_SecondCharacter = secondCharacter;
  148. // Second Glyph Index
  149. rect.x += width / 2 * 0.8f;
  150. EditorGUIUtility.labelWidth = 25f;
  151. EditorGUI.BeginChangeCheck();
  152. EditorGUI.PropertyField(rect, prop_SecondGlyphIndex, new GUIContent("ID:"));
  153. if (EditorGUI.EndChangeCheck())
  154. {
  155. }
  156. GUI.enabled = isEditingEnabled;
  157. EditorGUIUtility.labelWidth = 25f;
  158. rect = new Rect(position.width / 2 + 20, position.y + 20, width * 0.5f - padding, 18);
  159. EditorGUI.PropertyField(rect, prop_SecondGlyphValueRecord.FindPropertyRelative("m_XPlacement"), new GUIContent("OX"));
  160. rect.x += width * 0.5f;
  161. EditorGUI.PropertyField(rect, prop_SecondGlyphValueRecord.FindPropertyRelative("m_YPlacement"), new GUIContent("OY"));
  162. rect.x = position.width / 2 + 20;
  163. rect.y += 20;
  164. EditorGUI.PropertyField(rect, prop_SecondGlyphValueRecord.FindPropertyRelative("m_XAdvance"), new GUIContent("AX"));
  165. //rect.x += width * 0.5f;
  166. //EditorGUI.PropertyField(rect, prop_SecondGlyphAdjustment.FindPropertyRelative("m_YAdvance"), new GUIContent("AY"));
  167. }
  168. // Font Feature Lookup Flags
  169. if (isSelectable)
  170. {
  171. EditorGUIUtility.labelWidth = 55f;
  172. rect.x = position.width - 255;
  173. rect.y += 23;
  174. rect.width = 270; // width - 70 - padding;
  175. FontFeatureLookupFlags flags = (FontFeatureLookupFlags)prop_FontFeatureLookupFlags.intValue;
  176. EditorGUI.BeginChangeCheck();
  177. flags = (FontFeatureLookupFlags)EditorGUI.EnumFlagsField(rect, new GUIContent("Options:"), flags);
  178. if (EditorGUI.EndChangeCheck())
  179. {
  180. prop_FontFeatureLookupFlags.intValue = (int)flags;
  181. }
  182. }
  183. }
  184. bool ValidateInput(string source)
  185. {
  186. int length = string.IsNullOrEmpty(source) ? 0 : source.Length;
  187. ////Filter out unwanted characters.
  188. Event evt = Event.current;
  189. char c = evt.character;
  190. if (c != '\0')
  191. {
  192. switch (length)
  193. {
  194. case 0:
  195. break;
  196. case 1:
  197. if (source != m_PreviousInput)
  198. return true;
  199. if ((source[0] == '\\' && (c == 'u' || c == 'U')) == false)
  200. evt.character = '\0';
  201. break;
  202. case 2:
  203. case 3:
  204. case 4:
  205. case 5:
  206. if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F'))
  207. evt.character = '\0';
  208. break;
  209. case 6:
  210. case 7:
  211. case 8:
  212. case 9:
  213. if (source[1] == 'u' || (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F'))
  214. evt.character = '\0';
  215. // Validate input
  216. if (length == 6 && source[1] == 'u' && source != m_PreviousInput)
  217. return true;
  218. break;
  219. case 10:
  220. if (source != m_PreviousInput)
  221. return true;
  222. evt.character = '\0';
  223. break;
  224. }
  225. }
  226. m_PreviousInput = source;
  227. return false;
  228. }
  229. uint GetUnicodeCharacter (string source)
  230. {
  231. uint unicode;
  232. if (source.Length == 1)
  233. unicode = source[0];
  234. else if (source.Length == 6)
  235. unicode = (uint)TMP_TextUtilities.StringHexToInt(source.Replace("\\u", ""));
  236. else
  237. unicode = (uint)TMP_TextUtilities.StringHexToInt(source.Replace("\\U", ""));
  238. return unicode;
  239. }
  240. void DrawGlyph(uint glyphIndex, Rect position, SerializedProperty property)
  241. {
  242. // Get a reference to the font asset
  243. TMP_FontAsset fontAsset = property.serializedObject.targetObject as TMP_FontAsset;
  244. if (fontAsset == null)
  245. return;
  246. Glyph glyph;
  247. // Check if glyph is present in the atlas texture.
  248. if (!fontAsset.glyphLookupTable.TryGetValue(glyphIndex, out glyph))
  249. return;
  250. // Get the atlas index of the glyph and lookup its atlas texture
  251. int atlasIndex = glyph.atlasIndex;
  252. Texture2D atlasTexture = fontAsset.atlasTextures.Length > atlasIndex ? fontAsset.atlasTextures[atlasIndex] : null;
  253. if (atlasTexture == null)
  254. return;
  255. Material mat;
  256. if (((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
  257. {
  258. mat = TMP_FontAssetEditor.internalBitmapMaterial;
  259. if (mat == null)
  260. return;
  261. mat.mainTexture = atlasTexture;
  262. }
  263. else
  264. {
  265. mat = TMP_FontAssetEditor.internalSDFMaterial;
  266. if (mat == null)
  267. return;
  268. mat.mainTexture = atlasTexture;
  269. mat.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1);
  270. }
  271. // Draw glyph from atlas texture.
  272. Rect glyphDrawPosition = new Rect(position.x, position.y + 2, 64, 60);
  273. GlyphRect glyphRect = glyph.glyphRect;
  274. int padding = fontAsset.atlasPadding;
  275. int glyphOriginX = glyphRect.x - padding;
  276. int glyphOriginY = glyphRect.y - padding;
  277. int glyphWidth = glyphRect.width + padding * 2;
  278. int glyphHeight = glyphRect.height + padding * 2;
  279. float normalizedHeight = fontAsset.faceInfo.ascentLine - fontAsset.faceInfo.descentLine;
  280. float scale = glyphDrawPosition.width / normalizedHeight;
  281. // Compute the normalized texture coordinates
  282. Rect texCoords = new Rect((float)glyphOriginX / atlasTexture.width, (float)glyphOriginY / atlasTexture.height, (float)glyphWidth / atlasTexture.width, (float)glyphHeight / atlasTexture.height);
  283. if (Event.current.type == EventType.Repaint)
  284. {
  285. glyphDrawPosition.x += (glyphDrawPosition.width - glyphWidth * scale) / 2;
  286. glyphDrawPosition.y += (glyphDrawPosition.height - glyphHeight * scale) / 2;
  287. glyphDrawPosition.width = glyphWidth * scale;
  288. glyphDrawPosition.height = glyphHeight * scale;
  289. // Could switch to using the default material of the font asset which would require passing scale to the shader.
  290. Graphics.DrawTexture(glyphDrawPosition, atlasTexture, texCoords, 0, 0, 0, 0, new Color(1f, 1f, 1f), mat);
  291. }
  292. }
  293. }
  294. }