TMPFont.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #if FAIRYGUI_TMPRO
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.TextCore;
  6. using TMPro;
  7. namespace FairyGUI
  8. {
  9. /// <summary>
  10. /// TextMeshPro text adapter for FairyGUI. Most of codes were taken from TextMeshPro!
  11. /// Note that not all of TextMeshPro features are supported.
  12. /// </summary>
  13. public class TMPFont : BaseFont
  14. {
  15. protected TMP_FontAsset _fontAsset;
  16. FontStyles _style;
  17. float _scale;
  18. float _padding;
  19. float _stylePadding;
  20. float _ascent;
  21. float _lineHeight;
  22. float _boldMultiplier;
  23. FontWeight _defaultFontWeight;
  24. FontWeight _fontWeight;
  25. TextFormat _format;
  26. TMP_Character _char;
  27. TMP_Character _lineChar;
  28. Material _material;
  29. MaterialManager _manager;
  30. public TMPFont()
  31. {
  32. this.canTint = true;
  33. this.shader = "FairyGUI/TextMeshPro/Distance Field";
  34. this.keepCrisp = true;
  35. _defaultFontWeight = FontWeight.Medium;
  36. }
  37. override public void Dispose()
  38. {
  39. Release();
  40. }
  41. public TMP_FontAsset fontAsset
  42. {
  43. get { return _fontAsset; }
  44. set
  45. {
  46. _fontAsset = value;
  47. Init();
  48. }
  49. }
  50. public FontWeight fontWeight
  51. {
  52. get { return _defaultFontWeight; }
  53. set { _defaultFontWeight = value; }
  54. }
  55. void Release()
  56. {
  57. if (_manager != null)
  58. {
  59. _manager.onCreateNewMaterial -= OnCreateNewMaterial;
  60. _manager = null;
  61. }
  62. if (mainTexture != null)
  63. {
  64. mainTexture.Dispose();
  65. mainTexture = null;
  66. }
  67. if (_material != null)
  68. {
  69. Material.DestroyImmediate(_material);
  70. _material = null;
  71. }
  72. }
  73. void Init()
  74. {
  75. Release();
  76. mainTexture = new NTexture(_fontAsset.atlasTexture);
  77. mainTexture.destroyMethod = DestroyMethod.None;
  78. _manager = mainTexture.GetMaterialManager(this.shader);
  79. _manager.onCreateNewMaterial += OnCreateNewMaterial;
  80. _material = new Material(_fontAsset.material); //copy
  81. _material.SetFloat(ShaderUtilities.ID_TextureWidth, mainTexture.width);
  82. _material.SetFloat(ShaderUtilities.ID_TextureHeight, mainTexture.height);
  83. _material.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1);
  84. _material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  85. _material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  86. // _ascent = _fontAsset.faceInfo.ascentLine;
  87. // _lineHeight = _fontAsset.faceInfo.lineHeight;
  88. _ascent = _fontAsset.faceInfo.pointSize;
  89. _lineHeight = _fontAsset.faceInfo.pointSize * 1.25f;
  90. _lineChar = GetCharacterFromFontAsset('_', FontStyles.Normal);
  91. }
  92. void OnCreateNewMaterial(Material mat)
  93. {
  94. mat.SetFloat(ShaderUtilities.ID_TextureWidth, mainTexture.width);
  95. mat.SetFloat(ShaderUtilities.ID_TextureHeight, mainTexture.height);
  96. mat.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1);
  97. mat.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  98. mat.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  99. }
  100. override public void UpdateGraphics(NGraphics graphics)
  101. {
  102. MaterialPropertyBlock block = graphics.materialPropertyBlock;
  103. if (_format.outline > 0)
  104. {
  105. graphics.ToggleKeyword("OUTLINE_ON", true);
  106. block.SetFloat(ShaderUtilities.ID_OutlineWidth, _format.outline);
  107. block.SetColor(ShaderUtilities.ID_OutlineColor, _format.outlineColor);
  108. }
  109. else
  110. {
  111. graphics.ToggleKeyword("OUTLINE_ON", false);
  112. block.SetFloat(ShaderUtilities.ID_OutlineWidth, 0);
  113. }
  114. if (_format.shadowOffset.x != 0 || _format.shadowOffset.y != 0)
  115. {
  116. graphics.ToggleKeyword("UNDERLAY_ON", true);
  117. block.SetColor(ShaderUtilities.ID_UnderlayColor, _format.shadowColor);
  118. block.SetFloat(ShaderUtilities.ID_UnderlayOffsetX, _format.shadowOffset.x);
  119. block.SetFloat(ShaderUtilities.ID_UnderlayOffsetY, -_format.shadowOffset.y);
  120. block.SetFloat(ShaderUtilities.ID_UnderlaySoftness, _format.underlaySoftness);
  121. }
  122. else
  123. {
  124. graphics.ToggleKeyword("UNDERLAY_ON", false);
  125. block.SetFloat(ShaderUtilities.ID_UnderlayOffsetX, 0);
  126. block.SetFloat(ShaderUtilities.ID_UnderlayOffsetY, 0);
  127. block.SetFloat(ShaderUtilities.ID_UnderlaySoftness, 0);
  128. }
  129. block.SetFloat(ShaderUtilities.ID_FaceDilate, _format.faceDilate);
  130. block.SetFloat(ShaderUtilities.ID_OutlineSoftness, _format.outlineSoftness);
  131. if (_material.HasProperty(ShaderUtilities.ID_ScaleRatio_A))
  132. {
  133. //ShaderUtilities.GetPadding does not support handle materialproperyblock, we have to use a temp material
  134. _material.SetFloat(ShaderUtilities.ID_OutlineWidth, block.GetFloat(ShaderUtilities.ID_OutlineWidth));
  135. _material.SetFloat(ShaderUtilities.ID_UnderlayOffsetX, block.GetFloat(ShaderUtilities.ID_UnderlayOffsetX));
  136. _material.SetFloat(ShaderUtilities.ID_UnderlayOffsetY, block.GetFloat(ShaderUtilities.ID_UnderlayOffsetY));
  137. _material.SetFloat(ShaderUtilities.ID_UnderlaySoftness, block.GetFloat(ShaderUtilities.ID_UnderlaySoftness));
  138. _material.SetFloat(ShaderUtilities.ID_FaceDilate, block.GetFloat(ShaderUtilities.ID_FaceDilate));
  139. _material.SetFloat(ShaderUtilities.ID_OutlineSoftness, block.GetFloat(ShaderUtilities.ID_OutlineSoftness));
  140. _padding = ShaderUtilities.GetPadding(_material, false, false);
  141. //and then set back the properteis
  142. block.SetFloat(ShaderUtilities.ID_ScaleRatio_A, _material.GetFloat(ShaderUtilities.ID_ScaleRatio_A));
  143. block.SetFloat(ShaderUtilities.ID_ScaleRatio_B, _material.GetFloat(ShaderUtilities.ID_ScaleRatio_B));
  144. block.SetFloat(ShaderUtilities.ID_ScaleRatio_C, _material.GetFloat(ShaderUtilities.ID_ScaleRatio_C));
  145. }
  146. // Set Padding based on selected font style
  147. #region Handle Style Padding
  148. if (((_style & FontStyles.Bold) == FontStyles.Bold)) // Checks for any combination of Bold Style.
  149. {
  150. if (_material.HasProperty(ShaderUtilities.ID_GradientScale))
  151. {
  152. float gradientScale = _material.GetFloat(ShaderUtilities.ID_GradientScale);
  153. _stylePadding = _fontAsset.boldStyle / 4.0f * gradientScale * _material.GetFloat(ShaderUtilities.ID_ScaleRatio_A);
  154. // Clamp overall padding to Gradient Scale size.
  155. if (_stylePadding + _padding > gradientScale)
  156. _padding = gradientScale - _stylePadding;
  157. }
  158. else
  159. _stylePadding = 0;
  160. }
  161. else
  162. {
  163. if (_material.HasProperty(ShaderUtilities.ID_GradientScale))
  164. {
  165. float gradientScale = _material.GetFloat(ShaderUtilities.ID_GradientScale);
  166. _stylePadding = _fontAsset.normalStyle / 4.0f * gradientScale * _material.GetFloat(ShaderUtilities.ID_ScaleRatio_A);
  167. // Clamp overall padding to Gradient Scale size.
  168. if (_stylePadding + _padding > gradientScale)
  169. _padding = gradientScale - _stylePadding;
  170. }
  171. else
  172. _stylePadding = 0;
  173. }
  174. #endregion Handle Style Padding
  175. }
  176. override public void SetFormat(TextFormat format, float fontSizeScale)
  177. {
  178. _format = format;
  179. float size = format.size * fontSizeScale;
  180. if (_format.specialStyle == TextFormat.SpecialStyle.Subscript || _format.specialStyle == TextFormat.SpecialStyle.Superscript)
  181. size *= SupScale;
  182. _scale = size / _fontAsset.faceInfo.pointSize * _fontAsset.faceInfo.scale;
  183. _style = FontStyles.Normal;
  184. if (format.bold)
  185. {
  186. _style |= FontStyles.Bold;
  187. _fontWeight = FontWeight.Bold;
  188. _boldMultiplier = 1 + _fontAsset.boldSpacing * 0.01f;
  189. }
  190. else
  191. {
  192. _fontWeight = _defaultFontWeight;
  193. _boldMultiplier = 1.0f;
  194. }
  195. if (format.italic)
  196. _style |= FontStyles.Italic;
  197. format.FillVertexColors(vertexColors);
  198. }
  199. override public bool GetGlyph(char ch, out float width, out float height, out float baseline)
  200. {
  201. _char = GetCharacterFromFontAsset(ch, _style);
  202. if (_char != null)
  203. {
  204. width = _char.glyph.metrics.horizontalAdvance * _boldMultiplier * _scale;
  205. height = _lineHeight * _scale;
  206. baseline = _ascent * _scale;
  207. if (_format.specialStyle == TextFormat.SpecialStyle.Subscript)
  208. {
  209. height /= SupScale;
  210. baseline /= SupScale;
  211. }
  212. else if (_format.specialStyle == TextFormat.SpecialStyle.Superscript)
  213. {
  214. height = height / SupScale + baseline * SupOffset;
  215. baseline *= (SupOffset + 1 / SupScale);
  216. }
  217. height = Mathf.RoundToInt(height);
  218. baseline = Mathf.RoundToInt(baseline);
  219. return true;
  220. }
  221. else
  222. {
  223. width = 0;
  224. height = 0;
  225. baseline = 0;
  226. return false;
  227. }
  228. }
  229. TMP_Character GetCharacterFromFontAsset(uint unicode, FontStyles fontStyle)
  230. {
  231. bool isAlternativeTypeface;
  232. #pragma warning disable
  233. TMP_FontAsset actualAsset;
  234. #pragma warning restore
  235. return TMP_FontAssetUtilities.GetCharacterFromFontAsset(unicode, _fontAsset, true, fontStyle, _fontWeight,
  236. out isAlternativeTypeface
  237. //,out actualAsset //old TMP version need this line
  238. );
  239. }
  240. static Vector3 bottomLeft;
  241. static Vector3 topLeft;
  242. static Vector3 topRight;
  243. static Vector3 bottomRight;
  244. static Vector4 uvBottomLeft;
  245. static Vector4 uvTopLeft;
  246. static Vector4 uvTopRight;
  247. static Vector4 uvBottomRight;
  248. static Vector4 uv2BottomLeft;
  249. static Vector4 uv2TopLeft;
  250. static Vector4 uv2TopRight;
  251. static Vector4 uv2BottomRight;
  252. static Color32[] vertexColors = new Color32[4];
  253. override public int DrawGlyph(float x, float y,
  254. List<Vector3> vertList, List<Vector2> uvList, List<Vector2> uv2List, List<Color32> colList)
  255. {
  256. GlyphMetrics metrics = _char.glyph.metrics;
  257. GlyphRect rect = _char.glyph.glyphRect;
  258. if (_format.specialStyle == TextFormat.SpecialStyle.Subscript)
  259. y = y - Mathf.RoundToInt(_ascent * _scale * SupOffset);
  260. else if (_format.specialStyle == TextFormat.SpecialStyle.Superscript)
  261. y = y + Mathf.RoundToInt(_ascent * _scale * (1 / SupScale - 1 + SupOffset));
  262. topLeft.x = x + (metrics.horizontalBearingX - _padding - _stylePadding) * _scale;
  263. topLeft.y = y + (metrics.horizontalBearingY + _padding) * _scale;
  264. bottomRight.x = topLeft.x + (metrics.width + _padding * 2 + _stylePadding * 2) * _scale;
  265. bottomRight.y = topLeft.y - (metrics.height + _padding * 2) * _scale;
  266. topRight.x = bottomRight.x;
  267. topRight.y = topLeft.y;
  268. bottomLeft.x = topLeft.x;
  269. bottomLeft.y = bottomRight.y;
  270. #region Handle Italic & Shearing
  271. if (((_style & FontStyles.Italic) == FontStyles.Italic))
  272. {
  273. // Shift Top vertices forward by half (Shear Value * height of character) and Bottom vertices back by same amount.
  274. float shear_value = _fontAsset.italicStyle * 0.01f;
  275. Vector3 topShear = new Vector3(shear_value * ((metrics.horizontalBearingY + _padding + _stylePadding) * _scale), 0, 0);
  276. Vector3 bottomShear = new Vector3(shear_value * (((metrics.horizontalBearingY - metrics.height - _padding - _stylePadding)) * _scale), 0, 0);
  277. topLeft += topShear;
  278. bottomLeft += bottomShear;
  279. topRight += topShear;
  280. bottomRight += bottomShear;
  281. }
  282. #endregion Handle Italics & Shearing
  283. vertList.Add(bottomLeft);
  284. vertList.Add(topLeft);
  285. vertList.Add(topRight);
  286. vertList.Add(bottomRight);
  287. float u = (rect.x - _padding - _stylePadding) / _fontAsset.atlasWidth;
  288. float v = (rect.y - _padding - _stylePadding) / _fontAsset.atlasHeight;
  289. float uw = (rect.width + _padding * 2 + _stylePadding * 2) / _fontAsset.atlasWidth;
  290. float vw = (rect.height + _padding * 2 + _stylePadding * 2) / _fontAsset.atlasHeight;
  291. uvBottomLeft = new Vector2(u, v);
  292. uvTopLeft = new Vector2(u, v + vw);
  293. uvTopRight = new Vector2(u + uw, v + vw);
  294. uvBottomRight = new Vector2(u + uw, v);
  295. float xScale = _scale * 0.01f;
  296. if (_format.bold)
  297. xScale *= -1;
  298. uv2BottomLeft = new Vector2(0, xScale);
  299. uv2TopLeft = new Vector2(511, xScale);
  300. uv2TopRight = new Vector2(2093567, xScale);
  301. uv2BottomRight = new Vector2(2093056, xScale);
  302. uvList.Add(uvBottomLeft);
  303. uvList.Add(uvTopLeft);
  304. uvList.Add(uvTopRight);
  305. uvList.Add(uvBottomRight);
  306. uv2List.Add(uv2BottomLeft);
  307. uv2List.Add(uv2TopLeft);
  308. uv2List.Add(uv2TopRight);
  309. uv2List.Add(uv2BottomRight);
  310. colList.Add(vertexColors[0]);
  311. colList.Add(vertexColors[1]);
  312. colList.Add(vertexColors[2]);
  313. colList.Add(vertexColors[3]);
  314. return 4;
  315. }
  316. override public int DrawLine(float x, float y, float width, int fontSize, int type,
  317. List<Vector3> vertList, List<Vector2> uvList, List<Vector2> uv2List, List<Color32> colList)
  318. {
  319. if (_lineChar == null)
  320. return 0;
  321. float thickness;
  322. float offset;
  323. if (type == 0)
  324. {
  325. thickness = _fontAsset.faceInfo.underlineThickness;
  326. offset = _fontAsset.faceInfo.underlineOffset;
  327. }
  328. else
  329. {
  330. thickness = _fontAsset.faceInfo.strikethroughThickness;
  331. offset = _fontAsset.faceInfo.strikethroughOffset;
  332. }
  333. float scale = (float)fontSize / _fontAsset.faceInfo.pointSize * _fontAsset.faceInfo.scale;
  334. float segmentWidth = _lineChar.glyph.metrics.width / 2 * scale;
  335. if (width < _lineChar.glyph.metrics.width * scale)
  336. segmentWidth = width / 2f;
  337. // UNDERLINE VERTICES FOR (3) LINE SEGMENTS
  338. #region UNDERLINE VERTICES
  339. thickness = thickness * scale;
  340. if (thickness < 1)
  341. thickness = 1;
  342. offset = Mathf.RoundToInt(offset * scale);
  343. // Front Part of the Underline
  344. y += offset;
  345. topLeft.x = x;
  346. topLeft.y = y + _padding * scale;
  347. bottomRight.x = x + segmentWidth;
  348. bottomRight.y = y - thickness - _padding * scale;
  349. topRight.x = bottomRight.x;
  350. topRight.y = topLeft.y;
  351. bottomLeft.x = topLeft.x;
  352. bottomLeft.y = bottomRight.y;
  353. vertList.Add(bottomLeft);
  354. vertList.Add(topLeft);
  355. vertList.Add(topRight);
  356. vertList.Add(bottomRight);
  357. // Middle Part of the Underline
  358. topLeft = topRight;
  359. bottomLeft = bottomRight;
  360. topRight.x = x + width - segmentWidth;
  361. bottomRight.x = topRight.x;
  362. vertList.Add(bottomLeft);
  363. vertList.Add(topLeft);
  364. vertList.Add(topRight);
  365. vertList.Add(bottomRight);
  366. // End Part of the Underline
  367. topLeft = topRight;
  368. bottomLeft = bottomRight;
  369. topRight.x = x + width;
  370. bottomRight.x = topRight.x;
  371. vertList.Add(bottomLeft);
  372. vertList.Add(topLeft);
  373. vertList.Add(topRight);
  374. vertList.Add(bottomRight);
  375. #endregion
  376. // UNDERLINE UV0
  377. #region HANDLE UV0
  378. // Calculate UV required to setup the 3 Quads for the Underline.
  379. Vector2 uv0 = new Vector2((_lineChar.glyph.glyphRect.x - _padding) / _fontAsset.atlasWidth, (_lineChar.glyph.glyphRect.y - _padding) / _fontAsset.atlasHeight); // bottom left
  380. Vector2 uv1 = new Vector2(uv0.x, (_lineChar.glyph.glyphRect.y + _lineChar.glyph.glyphRect.height + _padding) / _fontAsset.atlasHeight); // top left
  381. Vector2 uv2 = new Vector2((_lineChar.glyph.glyphRect.x - _padding + (float)_lineChar.glyph.glyphRect.width / 2) / _fontAsset.atlasWidth, uv1.y); // Mid Top Left
  382. Vector2 uv3 = new Vector2(uv2.x, uv0.y); // Mid Bottom Left
  383. Vector2 uv4 = new Vector2((_lineChar.glyph.glyphRect.x + _padding + (float)_lineChar.glyph.glyphRect.width / 2) / _fontAsset.atlasWidth, uv1.y); // Mid Top Right
  384. Vector2 uv5 = new Vector2(uv4.x, uv0.y); // Mid Bottom right
  385. Vector2 uv6 = new Vector2((_lineChar.glyph.glyphRect.x + _padding + _lineChar.glyph.glyphRect.width) / _fontAsset.atlasWidth, uv1.y); // End Part - Bottom Right
  386. Vector2 uv7 = new Vector2(uv6.x, uv0.y); // End Part - Top Right
  387. uvList.Add(uv0);
  388. uvList.Add(uv1);
  389. uvList.Add(uv2);
  390. uvList.Add(uv3);
  391. // Middle Part of the Underline
  392. uvList.Add(new Vector2(uv2.x - uv2.x * 0.001f, uv0.y));
  393. uvList.Add(new Vector2(uv2.x - uv2.x * 0.001f, uv1.y));
  394. uvList.Add(new Vector2(uv2.x + uv2.x * 0.001f, uv1.y));
  395. uvList.Add(new Vector2(uv2.x + uv2.x * 0.001f, uv0.y));
  396. // Right Part of the Underline
  397. uvList.Add(uv5);
  398. uvList.Add(uv4);
  399. uvList.Add(uv6);
  400. uvList.Add(uv7);
  401. #endregion
  402. // UNDERLINE UV2
  403. #region HANDLE UV2 - SDF SCALE
  404. // UV1 contains Face / Border UV layout.
  405. float segUv1 = segmentWidth / width;
  406. float segUv2 = 1 - segUv1;
  407. //Calculate the xScale or how much the UV's are getting stretched on the X axis for the middle section of the underline.
  408. float xScale = scale * 0.01f;
  409. uv2List.Add(PackUV(0, 0, xScale));
  410. uv2List.Add(PackUV(0, 1, xScale));
  411. uv2List.Add(PackUV(segUv1, 1, xScale));
  412. uv2List.Add(PackUV(segUv1, 0, xScale));
  413. uv2List.Add(PackUV(segUv1, 0, xScale));
  414. uv2List.Add(PackUV(segUv1, 1, xScale));
  415. uv2List.Add(PackUV(segUv2, 1, xScale));
  416. uv2List.Add(PackUV(segUv2, 0, xScale));
  417. uv2List.Add(PackUV(segUv2, 0, xScale));
  418. uv2List.Add(PackUV(segUv2, 1, xScale));
  419. uv2List.Add(PackUV(1, 1, xScale));
  420. uv2List.Add(PackUV(1, 0, xScale));
  421. #endregion
  422. // UNDERLINE VERTEX COLORS
  423. #region
  424. // Alpha is the lower of the vertex color or tag color alpha used.
  425. for (int i = 0; i < 12; i++)
  426. colList.Add(vertexColors[0]);
  427. #endregion
  428. return 12;
  429. }
  430. Vector2 PackUV(float x, float y, float xScale)
  431. {
  432. double x0 = (int)(x * 511);
  433. double y0 = (int)(y * 511);
  434. return new Vector2((float)((x0 * 4096) + y0), xScale);
  435. }
  436. override public bool HasCharacter(char ch)
  437. {
  438. return _fontAsset.HasCharacter(ch);
  439. }
  440. override public int GetLineHeight(int size)
  441. {
  442. return Mathf.RoundToInt(_lineHeight * ((float)size / _fontAsset.faceInfo.pointSize * _fontAsset.faceInfo.scale));
  443. }
  444. }
  445. }
  446. #endif