RichTextLayout.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System.Collections.Generic;
  2. using FontStashSharp.Interfaces;
  3. using Hebron.Runtime;
  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.Drawing;
  12. using System.Numerics;
  13. using Color = Veldrid.RgbaFloat;
  14. #endif
  15. namespace FontStashSharp.RichText
  16. {
  17. public class RichTextLayout
  18. {
  19. private SpriteFontBase _font;
  20. private string _text = string.Empty;
  21. private int? _width;
  22. private Point _size;
  23. private bool _dirty = true;
  24. private readonly Dictionary<int, Point> _measures = new Dictionary<int, Point>();
  25. private readonly LayoutBuilder _layoutBuilder = new LayoutBuilder();
  26. private readonly FSRenderContext _renderContext = new FSRenderContext();
  27. public SpriteFontBase Font
  28. {
  29. get
  30. {
  31. return _font;
  32. }
  33. set
  34. {
  35. if (value == _font)
  36. {
  37. return;
  38. }
  39. _font = value;
  40. InvalidateLayout();
  41. InvalidateMeasures();
  42. }
  43. }
  44. public string Text
  45. {
  46. get
  47. {
  48. return _text;
  49. }
  50. set
  51. {
  52. if (value == _text)
  53. {
  54. return;
  55. }
  56. _text = value;
  57. InvalidateLayout();
  58. InvalidateMeasures();
  59. }
  60. }
  61. public int VerticalSpacing
  62. {
  63. get
  64. {
  65. return _layoutBuilder.VerticalSpacing;
  66. }
  67. set
  68. {
  69. if (value == _layoutBuilder.VerticalSpacing)
  70. {
  71. return;
  72. }
  73. _layoutBuilder.VerticalSpacing = value;
  74. InvalidateLayout();
  75. InvalidateMeasures();
  76. }
  77. }
  78. public int? Width
  79. {
  80. get
  81. {
  82. return _width;
  83. }
  84. set
  85. {
  86. if (value == _width)
  87. {
  88. return;
  89. }
  90. _width = value;
  91. InvalidateLayout();
  92. }
  93. }
  94. public List<TextLine> Lines
  95. {
  96. get
  97. {
  98. Update();
  99. return _layoutBuilder.Lines;
  100. }
  101. }
  102. public Point Size
  103. {
  104. get
  105. {
  106. Update();
  107. return _size;
  108. }
  109. }
  110. public bool CalculateGlyphs
  111. {
  112. get
  113. {
  114. return _layoutBuilder.CalculateGlyphs;
  115. }
  116. set
  117. {
  118. if (value == _layoutBuilder.CalculateGlyphs)
  119. {
  120. return;
  121. }
  122. _layoutBuilder.CalculateGlyphs = value;
  123. InvalidateLayout();
  124. InvalidateMeasures();
  125. }
  126. }
  127. public bool SupportsCommands
  128. {
  129. get
  130. {
  131. return _layoutBuilder.SupportsCommands;
  132. }
  133. set
  134. {
  135. if (value == _layoutBuilder.SupportsCommands)
  136. {
  137. return;
  138. }
  139. _layoutBuilder.SupportsCommands = value;
  140. InvalidateLayout();
  141. InvalidateMeasures();
  142. }
  143. }
  144. public bool IgnoreColorCommand { get; set; } = false;
  145. public char CommandPrefix
  146. {
  147. get => _layoutBuilder.CommandPrefix;
  148. set
  149. {
  150. if (value == _layoutBuilder.CommandPrefix)
  151. {
  152. return;
  153. }
  154. _layoutBuilder.CommandPrefix = value;
  155. InvalidateLayout();
  156. InvalidateMeasures();
  157. }
  158. }
  159. private static int GetMeasureKey(int? width)
  160. {
  161. return width != null ? width.Value : -1;
  162. }
  163. private void Update()
  164. {
  165. if (!_dirty)
  166. {
  167. return;
  168. }
  169. _size = _layoutBuilder.Layout(Text, Font, Width);
  170. var key = GetMeasureKey(Width);
  171. _measures[key] = _size;
  172. _dirty = false;
  173. }
  174. public Point Measure(int? width)
  175. {
  176. var result =Utility.PointZero;
  177. var key = GetMeasureKey(width);
  178. if (_measures.TryGetValue(key, out result))
  179. {
  180. return result;
  181. }
  182. result = _layoutBuilder.Layout(Text, Font, width, true);
  183. _measures[key] = result;
  184. return result;
  185. }
  186. public TextLine GetLineByCursorPosition(int cursorPosition)
  187. {
  188. Update();
  189. if (Lines.Count == 0)
  190. {
  191. return null;
  192. }
  193. if (cursorPosition < 0)
  194. {
  195. return Lines[0];
  196. }
  197. for (var i = 0; i < Lines.Count; ++i)
  198. {
  199. var s = Lines[i];
  200. if (s.TextStartIndex <= cursorPosition && cursorPosition < s.TextStartIndex + s.Count)
  201. {
  202. return s;
  203. }
  204. }
  205. return Lines[Lines.Count - 1];
  206. }
  207. public TextLine GetLineByY(int y)
  208. {
  209. if (string.IsNullOrEmpty(_text) || y < 0)
  210. {
  211. return null;
  212. }
  213. Update();
  214. var py = 0;
  215. for (var i = 0; i < Lines.Count; ++i)
  216. {
  217. var s = Lines[i];
  218. if (py <= y && y < py + s.Size.Y)
  219. {
  220. return s;
  221. }
  222. py += s.Size.Y;
  223. py += VerticalSpacing;
  224. }
  225. return null;
  226. }
  227. public TextChunkGlyph? GetGlyphInfoByIndex(int charIndex)
  228. {
  229. var strings = Lines;
  230. foreach (var si in strings)
  231. {
  232. if (charIndex >= si.Count)
  233. {
  234. charIndex -= si.Count;
  235. }
  236. else
  237. {
  238. return si.GetGlyphInfoByIndex(charIndex);
  239. }
  240. }
  241. return null;
  242. }
  243. private void Draw(Vector2 position, Color color, Vector2? sourceScale,
  244. float rotation, Vector2 origin, float layerDepth, TextHorizontalAlignment horizontalAlignment)
  245. {
  246. Update();
  247. var scale = sourceScale ?? Utility.DefaultScale;
  248. _renderContext.Prepare(position, scale, rotation, origin, layerDepth);
  249. var pos = Utility.Vector2Zero;
  250. foreach (var line in Lines)
  251. {
  252. pos.X = 0;
  253. if (horizontalAlignment == TextHorizontalAlignment.Center)
  254. {
  255. pos.X -= line.Size.X / 2;
  256. }
  257. else if (horizontalAlignment == TextHorizontalAlignment.Right)
  258. {
  259. pos.X -= line.Size.X;
  260. }
  261. foreach (var chunk in line.Chunks)
  262. {
  263. var chunkColor = color;
  264. if (!IgnoreColorCommand && chunk.Color != null)
  265. {
  266. chunkColor = chunk.Color.Value;
  267. }
  268. chunk.Draw(_renderContext, pos + new Vector2(0, chunk.VerticalOffset), chunkColor);
  269. pos.X += chunk.Size.X;
  270. }
  271. pos.Y += line.Size.Y;
  272. pos.Y += VerticalSpacing;
  273. }
  274. }
  275. public void Draw(IFontStashRenderer renderer, Vector2 position, Color color,
  276. Vector2? sourceScale = null, float rotation = 0,
  277. Vector2 origin = default(Vector2), float layerDepth = 0.0f,
  278. TextHorizontalAlignment horizontalAlignment = TextHorizontalAlignment.Left)
  279. {
  280. _renderContext.SetRenderer(renderer);
  281. Draw(position, color, sourceScale, rotation, origin, layerDepth, horizontalAlignment);
  282. }
  283. public void Draw(IFontStashRenderer2 renderer, Vector2 position, Color color,
  284. Vector2? sourceScale = null, float rotation = 0,
  285. Vector2 origin = default(Vector2), float layerDepth = 0.0f,
  286. TextHorizontalAlignment horizontalAlignment = TextHorizontalAlignment.Left)
  287. {
  288. _renderContext.SetRenderer(renderer);
  289. Draw(position, color, sourceScale, rotation, origin, layerDepth, horizontalAlignment);
  290. }
  291. #if MONOGAME || FNA || STRIDE
  292. public void Draw(SpriteBatch batch, Vector2 position, Color color,
  293. Vector2? scale = null, float rotation = 0, Vector2 origin = default(Vector2),
  294. float layerDepth = 0.0f, TextHorizontalAlignment horizontalAlignment = TextHorizontalAlignment.Left)
  295. {
  296. var renderer = SpriteBatchRenderer.Instance;
  297. renderer.Batch = batch;
  298. Draw(renderer, position, color, scale, rotation, origin, layerDepth, horizontalAlignment);
  299. }
  300. #endif
  301. private void InvalidateLayout()
  302. {
  303. _dirty = true;
  304. }
  305. private void InvalidateMeasures()
  306. {
  307. _measures.Clear();
  308. }
  309. }
  310. }