SpriteFontBase.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using System;
  4. using FontStashSharp.Interfaces;
  5. using System.Linq;
  6. #if MONOGAME || FNA
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. #elif STRIDE
  10. using Stride.Core.Mathematics;
  11. using Stride.Graphics;
  12. using Texture2D = Stride.Graphics.Texture;
  13. #else
  14. using System.Drawing;
  15. using System.Numerics;
  16. using Matrix = System.Numerics.Matrix3x2;
  17. using Texture2D = Veldrid.Texture;
  18. #endif
  19. namespace FontStashSharp
  20. {
  21. public abstract partial class SpriteFontBase
  22. {
  23. private static Texture2D _white;
  24. /// <summary>
  25. /// Font Size
  26. /// </summary>
  27. public float FontSize { get; private set; }
  28. /// <summary>
  29. /// Line Height in pixels
  30. /// </summary>
  31. public int LineHeight { get; private set; }
  32. protected float RenderFontSizeMultiplicator { get; set; } = 1f;
  33. protected SpriteFontBase(float fontSize, int lineHeight)
  34. {
  35. FontSize = fontSize;
  36. LineHeight = lineHeight;
  37. }
  38. #if MONOGAME || FNA || STRIDE
  39. protected internal abstract FontGlyph GetGlyph(GraphicsDevice device, int codepoint);
  40. #else
  41. protected internal abstract FontGlyph GetGlyph(ITexture2DManager device, int codepoint);
  42. #endif
  43. internal abstract void PreDraw(TextSource str, out int ascent, out int lineHeight);
  44. private void Prepare(Vector2 position, ref Vector2 scale, float rotation, Vector2 origin, out Matrix transformation)
  45. {
  46. scale /= RenderFontSizeMultiplicator;
  47. Utility.BuildTransform(position, scale, rotation, origin, out transformation);
  48. }
  49. internal virtual Bounds InternalTextBounds(TextSource source, Vector2 position, float characterSpacing, float lineSpacing)
  50. {
  51. if (source.IsNull) return Bounds.Empty;
  52. int ascent, lineHeight;
  53. PreDraw(source, out ascent, out lineHeight);
  54. var x = position.X;
  55. var y = position.Y;
  56. y += ascent;
  57. float minx, maxx, miny, maxy;
  58. minx = maxx = x;
  59. miny = maxy = y;
  60. float startx = x;
  61. FontGlyph prevGlyph = null;
  62. while (true)
  63. {
  64. int codepoint;
  65. if (!source.GetNextCodepoint(out codepoint))
  66. break;
  67. if (codepoint == '\n')
  68. {
  69. x = startx;
  70. y += lineHeight + lineSpacing;
  71. prevGlyph = null;
  72. continue;
  73. }
  74. var glyph = GetGlyph(null, codepoint);
  75. if (glyph == null)
  76. {
  77. continue;
  78. }
  79. if (prevGlyph != null)
  80. {
  81. x += characterSpacing;
  82. x += GetKerning(glyph, prevGlyph);
  83. }
  84. var x0 = x + glyph.RenderOffset.X;
  85. if (x0 < minx)
  86. minx = x0;
  87. x += glyph.XAdvance;
  88. if (x > maxx)
  89. maxx = x;
  90. var y0 = y + glyph.RenderOffset.Y;
  91. var y1 = y0 + glyph.Size.Y;
  92. if (y0 < miny)
  93. miny = y0;
  94. if (y1 > maxy)
  95. maxy = y1;
  96. prevGlyph = glyph;
  97. }
  98. return new Bounds(minx, miny, maxx, maxy);
  99. }
  100. public Bounds TextBounds(string text, Vector2 position, Vector2? scale = null, float characterSpacing = 0.0f, float lineSpacing = 0.0f)
  101. {
  102. var bounds = InternalTextBounds(new TextSource(text), position, characterSpacing, lineSpacing);
  103. var realScale = scale ?? Utility.DefaultScale;
  104. bounds.ApplyScale(realScale / RenderFontSizeMultiplicator);
  105. return bounds;
  106. }
  107. public Bounds TextBounds(StringBuilder text, Vector2 position, Vector2? scale = null, float characterSpacing = 0.0f, float lineSpacing = 0.0f)
  108. {
  109. var bounds = InternalTextBounds(new TextSource(text), position, characterSpacing, lineSpacing);
  110. var realScale = scale ?? Utility.DefaultScale;
  111. bounds.ApplyScale(realScale / RenderFontSizeMultiplicator);
  112. return bounds;
  113. }
  114. private List<Glyph> GetGlyphs(TextSource source, Vector2 position, Vector2 origin, Vector2? sourceScale, float characterSpacing, float lineSpacing)
  115. {
  116. List<Glyph> result = new List<Glyph>();
  117. if (source.IsNull) return result;
  118. Matrix transformation;
  119. var scale = sourceScale ?? Utility.DefaultScale;
  120. Prepare(position, ref scale, 0, origin, out transformation);
  121. int ascent, lineHeight;
  122. PreDraw(source, out ascent, out lineHeight);
  123. var pos = new Vector2(0, ascent);
  124. FontGlyph prevGlyph = null;
  125. var i = 0;
  126. while (true)
  127. {
  128. int codepoint;
  129. if (!source.GetNextCodepoint(out codepoint))
  130. {
  131. break;
  132. }
  133. var rect = new Rectangle((int)pos.X, (int)pos.Y - LineHeight, 0, LineHeight);
  134. var xAdvance = 0;
  135. if (codepoint == '\n')
  136. {
  137. pos.X = 0;
  138. pos.Y += lineHeight + lineSpacing;
  139. prevGlyph = null;
  140. }
  141. else
  142. {
  143. var glyph = GetGlyph(null, codepoint);
  144. if (glyph != null)
  145. {
  146. if (prevGlyph != null)
  147. {
  148. pos.X += characterSpacing;
  149. pos.X += GetKerning(glyph, prevGlyph);
  150. }
  151. rect = glyph.RenderRectangle;
  152. rect.Offset((int)pos.X, (int)pos.Y);
  153. xAdvance = glyph.XAdvance;
  154. pos.X += xAdvance;
  155. prevGlyph = glyph;
  156. }
  157. }
  158. // Apply transformation to rect
  159. var p = new Vector2(rect.X, rect.Y);
  160. p = p.Transform(ref transformation);
  161. var s = new Vector2(rect.Width * scale.X, rect.Height * scale.Y);
  162. var glyphInfo = new Glyph
  163. {
  164. Index = i,
  165. Codepoint = codepoint,
  166. Bounds = new Rectangle((int)p.X, (int)p.Y, (int)s.X, (int)s.Y),
  167. XAdvance = xAdvance
  168. };
  169. // Add to the result
  170. result.Add(glyphInfo);
  171. ++i;
  172. }
  173. return result;
  174. }
  175. public List<Glyph> GetGlyphs(string text, Vector2 position,
  176. Vector2 origin = default(Vector2), Vector2? scale = null,
  177. float characterSpacing = 0.0f, float lineSpacing = 0.0f) =>
  178. GetGlyphs(new TextSource(text), position, origin, scale, characterSpacing, lineSpacing);
  179. public List<Glyph> GetGlyphs(StringBuilder text, Vector2 position,
  180. Vector2 origin = default(Vector2), Vector2? scale = null,
  181. float characterSpacing = 0.0f, float lineSpacing = 0.0f) =>
  182. GetGlyphs(new TextSource(text), position, origin, scale, characterSpacing, lineSpacing);
  183. [Obsolete("Use GetGlyphs")]
  184. public List<Rectangle> GetGlyphRects(string text, Vector2 position,
  185. Vector2 origin = default(Vector2), Vector2? scale = null,
  186. float characterSpacing = 0.0f, float lineSpacing = 0.0f)
  187. {
  188. var glyphs = GetGlyphs(text, position, origin, scale, characterSpacing, lineSpacing);
  189. return (from g in glyphs select g.Bounds).ToList();
  190. }
  191. [Obsolete("Use GetGlyphs")]
  192. public List<Rectangle> GetGlyphRects(StringBuilder text, Vector2 position,
  193. Vector2 origin = default(Vector2), Vector2? scale = null,
  194. float characterSpacing = 0.0f, float lineSpacing = 0.0f)
  195. {
  196. var glyphs = GetGlyphs(text, position, origin, scale, characterSpacing, lineSpacing);
  197. return (from g in glyphs select g.Bounds).ToList();
  198. }
  199. public Vector2 MeasureString(string text, Vector2? scale = null, float characterSpacing = 0.0f, float lineSpacing = 0.0f)
  200. {
  201. var bounds = TextBounds(text, Utility.Vector2Zero, scale, characterSpacing, lineSpacing);
  202. return new Vector2(bounds.X2-bounds.X, bounds.Y2-bounds.Y);
  203. }
  204. public Vector2 MeasureString(StringBuilder text, Vector2? scale = null, float characterSpacing = 0.0f, float lineSpacing = 0.0f)
  205. {
  206. var bounds = TextBounds(text, Utility.Vector2Zero, scale, characterSpacing, lineSpacing);
  207. return new Vector2(bounds.X2-bounds.X, bounds.Y2-bounds.Y);
  208. }
  209. internal abstract float GetKerning(FontGlyph glyph, FontGlyph prevGlyph);
  210. #if MONOGAME || FNA || STRIDE
  211. public static Texture2D GetWhite(GraphicsDevice graphicsDevice)
  212. #else
  213. public static Texture2D GetWhite(ITexture2DManager textureManager)
  214. #endif
  215. {
  216. if (_white != null)
  217. {
  218. return _white;
  219. }
  220. #if MONOGAME || FNA || STRIDE
  221. _white = Texture2DManager.CreateTexture(graphicsDevice, 1, 1);
  222. Texture2DManager.SetTextureData(_white, new Rectangle(0, 0, 1, 1), new byte[] { 255, 255, 255, 255 });
  223. #else
  224. _white = textureManager.CreateTexture(1, 1);
  225. textureManager.SetTextureData(_white, new Rectangle(0, 0, 1, 1), new byte[] { 255, 255, 255, 255 });
  226. #endif
  227. return _white;
  228. }
  229. }
  230. }