SpriteFontBase.IFontStashRenderer2.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using FontStashSharp.Interfaces;
  2. using System.Text;
  3. using System;
  4. #if MONOGAME || FNA
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. #elif STRIDE
  8. using Stride.Core.Mathematics;
  9. using Stride.Graphics;
  10. #else
  11. using System.Numerics;
  12. using System.Drawing;
  13. using Matrix = System.Numerics.Matrix3x2;
  14. using Color = Veldrid.RgbaFloat;
  15. #endif
  16. namespace FontStashSharp
  17. {
  18. public partial class SpriteFontBase
  19. {
  20. private void RenderStyle(IFontStashRenderer2 renderer, TextStyle textStyle, Vector2 pos,
  21. int lineHeight, int ascent, Color color, ref Matrix transformation, float layerDepth,
  22. ref VertexPositionColorTexture topLeft, ref VertexPositionColorTexture topRight,
  23. ref VertexPositionColorTexture bottomLeft, ref VertexPositionColorTexture bottomRight)
  24. {
  25. if (textStyle == TextStyle.None || pos.X == 0)
  26. {
  27. return;
  28. }
  29. #if MONOGAME || FNA || STRIDE
  30. var white = GetWhite(renderer.GraphicsDevice);
  31. #else
  32. var white = GetWhite(renderer.TextureManager);
  33. #endif
  34. var start = Vector2.Zero;
  35. if (textStyle == TextStyle.Strikethrough)
  36. {
  37. start.Y = pos.Y - ascent - FontSystemDefaults.TextStyleLineHeight / 2 + lineHeight / 2;
  38. }
  39. else
  40. {
  41. start.Y = pos.Y + 1;
  42. }
  43. var size = new Vector2(pos.X, FontSystemDefaults.TextStyleLineHeight);
  44. renderer.DrawQuad(white, color, start, ref transformation,
  45. layerDepth, size, new Rectangle(0, 0, 1, 1),
  46. ref topLeft, ref topRight, ref bottomLeft, ref bottomRight);
  47. }
  48. private float DrawText(IFontStashRenderer2 renderer, TextColorSource source,
  49. Vector2 position, Vector2? sourceScale, float rotation, Vector2 origin,
  50. float layerDepth, float characterSpacing, float lineSpacing, TextStyle textStyle)
  51. {
  52. if (renderer == null)
  53. {
  54. throw new ArgumentNullException(nameof(renderer));
  55. }
  56. #if MONOGAME || FNA || STRIDE
  57. if (renderer.GraphicsDevice == null)
  58. {
  59. throw new ArgumentNullException("renderer.GraphicsDevice can't be null.");
  60. }
  61. #else
  62. if (renderer.TextureManager == null)
  63. {
  64. throw new ArgumentNullException("renderer.TextureManager can't be null.");
  65. }
  66. #endif
  67. if (source.IsNull) return 0.0f;
  68. Matrix transformation;
  69. var scale = sourceScale ?? Utility.DefaultScale;
  70. Prepare(position, ref scale, rotation, origin, out transformation);
  71. int ascent, lineHeight;
  72. PreDraw(source.TextSource, out ascent, out lineHeight);
  73. var pos = new Vector2(0, ascent);
  74. FontGlyph prevGlyph = null;
  75. var topLeft = new VertexPositionColorTexture();
  76. var topRight = new VertexPositionColorTexture();
  77. var bottomLeft = new VertexPositionColorTexture();
  78. var bottomRight = new VertexPositionColorTexture();
  79. Color? firstColor = null;
  80. while (true)
  81. {
  82. int codepoint;
  83. Color color;
  84. if (!source.GetNextCodepoint(out codepoint))
  85. break;
  86. if (codepoint == '\n')
  87. {
  88. if (textStyle != TextStyle.None && firstColor != null)
  89. {
  90. RenderStyle(renderer, textStyle, pos,
  91. lineHeight, ascent, firstColor.Value, ref transformation, layerDepth,
  92. ref topLeft, ref topRight, ref bottomLeft, ref bottomRight);
  93. }
  94. pos.X = 0.0f;
  95. pos.Y += lineHeight;
  96. prevGlyph = null;
  97. continue;
  98. }
  99. #if MONOGAME || FNA || STRIDE
  100. var glyph = GetGlyph(renderer.GraphicsDevice, codepoint);
  101. #else
  102. var glyph = GetGlyph(renderer.TextureManager, codepoint);
  103. #endif
  104. if (glyph == null)
  105. {
  106. continue;
  107. }
  108. if (prevGlyph != null)
  109. {
  110. pos.X += characterSpacing;
  111. pos.X += GetKerning(glyph, prevGlyph);
  112. }
  113. if (!glyph.IsEmpty)
  114. {
  115. color = source.GetNextColor();
  116. firstColor = color;
  117. var baseOffset = new Vector2(glyph.RenderOffset.X, glyph.RenderOffset.Y) + pos;
  118. var size = new Vector2(glyph.Size.X, glyph.Size.Y);
  119. renderer.DrawQuad(glyph.Texture, color, baseOffset, ref transformation,
  120. layerDepth, size, glyph.TextureRectangle,
  121. ref topLeft, ref topRight, ref bottomLeft, ref bottomRight);
  122. }
  123. pos.X += glyph.XAdvance;
  124. prevGlyph = glyph;
  125. }
  126. if (textStyle != TextStyle.None && firstColor != null)
  127. {
  128. RenderStyle(renderer, textStyle, pos,
  129. lineHeight, ascent, firstColor.Value, ref transformation, layerDepth,
  130. ref topLeft, ref topRight, ref bottomLeft, ref bottomRight);
  131. }
  132. return position.X + position.X;
  133. }
  134. /// <summary>
  135. /// Draws a text
  136. /// </summary>
  137. /// <param name="renderer">A renderer</param>
  138. /// <param name="text">The text which will be drawn</param>
  139. /// <param name="position">The drawing location on screen</param>
  140. /// <param name="color">A color mask</param>
  141. /// <param name="rotation">A rotation of this text in radians</param>
  142. /// <param name="origin">Center of the rotation</param>
  143. /// <param name="scale">A scaling of this text. Null means the scaling is (1, 1)</param>
  144. /// <param name="layerDepth">A depth of the layer of this string</param>
  145. /// <param name="characterSpacing">A character spacing</param>
  146. /// <param name="lineSpacing">A line spacing</param>
  147. public float DrawText(IFontStashRenderer2 renderer, string text, Vector2 position, Color color,
  148. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  149. float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
  150. TextStyle textStyle = TextStyle.None) =>
  151. DrawText(renderer, new TextColorSource(text, color), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
  152. /// <summary>
  153. /// Draws a text
  154. /// </summary>
  155. /// <param name="renderer">A renderer</param>
  156. /// <param name="text">The text which will be drawn</param>
  157. /// <param name="position">The drawing location on screen</param>
  158. /// <param name="colors">Colors of glyphs</param>
  159. /// <param name="rotation">A rotation of this text in radians</param>
  160. /// <param name="origin">Center of the rotation</param>
  161. /// <param name="scale">A scaling of this text. Null means the scaling is (1, 1)</param>
  162. /// <param name="layerDepth">A depth of the layer of this string</param>
  163. /// <param name="characterSpacing">A character spacing</param>
  164. /// <param name="lineSpacing">A line spacing</param>
  165. public float DrawText(IFontStashRenderer2 renderer, string text, Vector2 position, Color[] colors,
  166. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  167. float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
  168. TextStyle textStyle = TextStyle.None) =>
  169. DrawText(renderer, new TextColorSource(text, colors), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
  170. /// <summary>
  171. /// Draws a text
  172. /// </summary>
  173. /// <param name="renderer">A renderer</param>
  174. /// <param name="text">The text which will be drawn</param>
  175. /// <param name="position">The drawing location on screen</param>
  176. /// <param name="color">A color mask</param>
  177. /// <param name="rotation">A rotation of this text in radians</param>
  178. /// <param name="origin">Center of the rotation</param>
  179. /// <param name="scale">A scaling of this text. Null means the scaling is (1, 1)</param>
  180. /// <param name="layerDepth">A depth of the layer of this string</param>
  181. /// <param name="characterSpacing">A character spacing</param>
  182. /// <param name="lineSpacing">A line spacing</param>
  183. public float DrawText(IFontStashRenderer2 renderer, StringSegment text, Vector2 position, Color color,
  184. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  185. float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
  186. TextStyle textStyle = TextStyle.None) =>
  187. DrawText(renderer, new TextColorSource(text, color), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
  188. /// <summary>
  189. /// Draws a text
  190. /// </summary>
  191. /// <param name="renderer">A renderer</param>
  192. /// <param name="text">The text which will be drawn</param>
  193. /// <param name="position">The drawing location on screen</param>
  194. /// <param name="colors">Colors of glyphs</param>
  195. /// <param name="rotation">A rotation of this text in radians</param>
  196. /// <param name="origin">Center of the rotation</param>
  197. /// <param name="scale">A scaling of this text. Null means the scaling is (1, 1)</param>
  198. /// <param name="layerDepth">A depth of the layer of this string</param>
  199. /// <param name="characterSpacing">A character spacing</param>
  200. /// <param name="lineSpacing">A line spacing</param>
  201. public float DrawText(IFontStashRenderer2 renderer, StringSegment text, Vector2 position, Color[] colors,
  202. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  203. float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
  204. TextStyle textStyle = TextStyle.None) =>
  205. DrawText(renderer, new TextColorSource(text, colors), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
  206. /// <summary>
  207. /// Draws a text
  208. /// </summary>
  209. /// <param name="renderer">A renderer</param>
  210. /// <param name="text">The text which will be drawn</param>
  211. /// <param name="position">The drawing location on screen</param>
  212. /// <param name="color">A color mask</param>
  213. /// <param name="rotation">A rotation of this text in radians</param>
  214. /// <param name="origin">Center of the rotation</param>
  215. /// <param name="scale">A scaling of this text. Null means the scaling is (1, 1)</param>
  216. /// <param name="layerDepth">A depth of the layer of this string</param>
  217. /// <param name="characterSpacing">A character spacing</param>
  218. /// <param name="lineSpacing">A line spacing</param>
  219. public float DrawText(IFontStashRenderer2 renderer, StringBuilder text, Vector2 position, Color color,
  220. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  221. float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
  222. TextStyle textStyle = TextStyle.None) =>
  223. DrawText(renderer, new TextColorSource(text, color), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
  224. /// <summary>
  225. /// Draws a text
  226. /// </summary>
  227. /// <param name="renderer">A renderer</param>
  228. /// <param name="text">The text which will be drawn</param>
  229. /// <param name="position">The drawing location on screen</param>
  230. /// <param name="colors">Colors of glyphs</param>
  231. /// <param name="rotation">A rotation of this text in radians</param>
  232. /// <param name="origin">Center of the rotation</param>
  233. /// <param name="scale">A scaling of this text. Null means the scaling is (1, 1)</param>
  234. /// <param name="layerDepth">A depth of the layer of this string</param>
  235. /// <param name="characterSpacing">A character spacing</param>
  236. /// <param name="lineSpacing">A line spacing</param>
  237. public float DrawText(IFontStashRenderer2 renderer, StringBuilder text, Vector2 position, Color[] colors,
  238. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  239. float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
  240. TextStyle textStyle = TextStyle.None) =>
  241. DrawText(renderer, new TextColorSource(text, colors), position, scale, rotation, origin, layerDepth, characterSpacing, lineSpacing, textStyle);
  242. }
  243. }