BitmapFont.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class BitmapFont : BaseFont
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public class BMGlyph
  14. {
  15. public float x;
  16. public float y;
  17. public float width;
  18. public float height;
  19. public int advance;
  20. public int lineHeight;
  21. public Vector2[] uv = new Vector2[4];
  22. public int channel;//0-n/a, 1-r,2-g,3-b,4-alpha
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. public int size;
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. public bool resizable;
  32. /// <summary>
  33. /// Font generated by BMFont use channels.
  34. /// </summary>
  35. public bool hasChannel;
  36. protected Dictionary<int, BMGlyph> _dict;
  37. protected BMGlyph _glyph;
  38. float _scale;
  39. public BitmapFont()
  40. {
  41. this.canTint = true;
  42. this.hasChannel = false;
  43. this.customOutline = true;
  44. this.shader = ShaderConfig.bmFontShader;
  45. _dict = new Dictionary<int, BMGlyph>();
  46. _scale = 1;
  47. }
  48. public void AddChar(char ch, BMGlyph glyph)
  49. {
  50. _dict[ch] = glyph;
  51. }
  52. override public void SetFormat(TextFormat format, float fontSizeScale)
  53. {
  54. if (resizable)
  55. _scale = (float)format.size / size * fontSizeScale;
  56. else
  57. _scale = fontSizeScale;
  58. if (canTint)
  59. format.FillVertexColors(vertexColors);
  60. }
  61. override public bool GetGlyph(char ch, out float width, out float height, out float baseline)
  62. {
  63. if (ch == ' ')
  64. {
  65. width = Mathf.RoundToInt(size * _scale / 2);
  66. height = Mathf.RoundToInt(size * _scale);
  67. baseline = height;
  68. _glyph = null;
  69. return true;
  70. }
  71. else if (_dict.TryGetValue((int)ch, out _glyph))
  72. {
  73. width = Mathf.RoundToInt(_glyph.advance * _scale);
  74. height = Mathf.RoundToInt(_glyph.lineHeight * _scale);
  75. baseline = height;
  76. return true;
  77. }
  78. else
  79. {
  80. width = 0;
  81. height = 0;
  82. baseline = 0;
  83. return false;
  84. }
  85. }
  86. static Vector3 bottomLeft;
  87. static Vector3 topLeft;
  88. static Vector3 topRight;
  89. static Vector3 bottomRight;
  90. static Color32[] vertexColors = new Color32[4];
  91. override public int DrawGlyph(float x, float y,
  92. List<Vector3> vertList, List<Vector2> uvList, List<Vector2> uv2List, List<Color32> colList)
  93. {
  94. if (_glyph == null) //space
  95. return 0;
  96. topLeft.x = x + _glyph.x * _scale;
  97. topLeft.y = y + (_glyph.lineHeight - _glyph.y) * _scale;
  98. bottomRight.x = x + (_glyph.x + _glyph.width) * _scale;
  99. bottomRight.y = topLeft.y - _glyph.height * _scale;
  100. topRight.x = bottomRight.x;
  101. topRight.y = topLeft.y;
  102. bottomLeft.x = topLeft.x;
  103. bottomLeft.y = bottomRight.y;
  104. vertList.Add(bottomLeft);
  105. vertList.Add(topLeft);
  106. vertList.Add(topRight);
  107. vertList.Add(bottomRight);
  108. uvList.AddRange(_glyph.uv);
  109. if (hasChannel)
  110. {
  111. Vector2 channel = new Vector2(_glyph.channel, 0);
  112. uv2List.Add(channel);
  113. uv2List.Add(channel);
  114. uv2List.Add(channel);
  115. uv2List.Add(channel);
  116. }
  117. if (canTint)
  118. {
  119. colList.Add(vertexColors[0]);
  120. colList.Add(vertexColors[1]);
  121. colList.Add(vertexColors[2]);
  122. colList.Add(vertexColors[3]);
  123. }
  124. else
  125. {
  126. colList.Add(Color.white);
  127. colList.Add(Color.white);
  128. colList.Add(Color.white);
  129. colList.Add(Color.white);
  130. }
  131. return 4;
  132. }
  133. override public bool HasCharacter(char ch)
  134. {
  135. return ch == ' ' || _dict.ContainsKey((int)ch);
  136. }
  137. override public int GetLineHeight(int size)
  138. {
  139. if (_dict.Count > 0)
  140. {
  141. using (var et = _dict.GetEnumerator())
  142. {
  143. et.MoveNext();
  144. if (resizable)
  145. return Mathf.RoundToInt((float)et.Current.Value.lineHeight * size / this.size);
  146. else
  147. return et.Current.Value.lineHeight;
  148. }
  149. }
  150. else
  151. return 0;
  152. }
  153. }
  154. }