TextLine.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections.Generic;
  2. #if MONOGAME || FNA
  3. using Microsoft.Xna.Framework;
  4. #elif STRIDE
  5. using Stride.Core.Mathematics;
  6. #else
  7. using System.Drawing;
  8. #endif
  9. namespace FontStashSharp.RichText
  10. {
  11. public class TextLine
  12. {
  13. public int Count { get; internal set; }
  14. public Point Size;
  15. public int LineIndex { get; internal set; }
  16. public int TextStartIndex { get; internal set; }
  17. public List<BaseChunk> Chunks { get; } = new List<BaseChunk>();
  18. public TextChunkGlyph? GetGlyphInfoByIndex(int index)
  19. {
  20. foreach (var chunk in Chunks)
  21. {
  22. var textChunk = chunk as TextChunk;
  23. if (textChunk == null) continue;
  24. if (index >= textChunk.Count)
  25. {
  26. index -= textChunk.Count;
  27. }
  28. else
  29. {
  30. return textChunk.GetGlyphInfoByIndex(index);
  31. }
  32. }
  33. return null;
  34. }
  35. public int? GetGlyphIndexByX(int startX)
  36. {
  37. if (Chunks.Count == 0)
  38. {
  39. return null;
  40. }
  41. var x = startX;
  42. for (var i = 0; i < Chunks.Count; ++i)
  43. {
  44. var chunk = (TextChunk)Chunks[i];
  45. if (x >= chunk.Size.X)
  46. {
  47. x -= chunk.Size.X;
  48. }
  49. else
  50. {
  51. if (chunk.Glyphs.Count > 0 && x < chunk.Glyphs[0].Bounds.X)
  52. {
  53. // Before first glyph
  54. return 0;
  55. }
  56. return chunk.GetGlyphIndexByX(x);
  57. }
  58. }
  59. // Use last chunk
  60. x = startX;
  61. return ((TextChunk)Chunks[Chunks.Count - 1]).GetGlyphIndexByX(startX);
  62. }
  63. }
  64. }