TextColorSource.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Text;
  2. #if MONOGAME || FNA
  3. using Microsoft.Xna.Framework;
  4. #elif STRIDE
  5. using Stride.Core.Mathematics;
  6. #else
  7. using Color = Veldrid.RgbaFloat;
  8. #endif
  9. namespace FontStashSharp
  10. {
  11. internal ref struct TextColorSource
  12. {
  13. public TextSource TextSource;
  14. public Color? SingleColor;
  15. public Color[] Colors;
  16. public int ColorPosition;
  17. public TextColorSource(string text, Color color)
  18. {
  19. TextSource = new TextSource(text);
  20. SingleColor = color;
  21. Colors = null;
  22. ColorPosition = 0;
  23. }
  24. public TextColorSource(string text, Color[] colors)
  25. {
  26. TextSource = new TextSource(text);
  27. SingleColor = null;
  28. Colors = colors;
  29. ColorPosition = 0;
  30. }
  31. public TextColorSource(StringSegment text, Color color)
  32. {
  33. TextSource = new TextSource(text);
  34. SingleColor = color;
  35. Colors = null;
  36. ColorPosition = 0;
  37. }
  38. public TextColorSource(StringSegment text, Color[] colors)
  39. {
  40. TextSource = new TextSource(text);
  41. SingleColor = null;
  42. Colors = colors;
  43. ColorPosition = 0;
  44. }
  45. public TextColorSource(StringBuilder text, Color color)
  46. {
  47. TextSource = new TextSource(text);
  48. SingleColor = color;
  49. Colors = null;
  50. ColorPosition = 0;
  51. }
  52. public TextColorSource(StringBuilder text, Color[] colors)
  53. {
  54. TextSource = new TextSource(text);
  55. SingleColor = null;
  56. Colors = colors;
  57. ColorPosition = 0;
  58. }
  59. public bool IsNull => TextSource.IsNull;
  60. [System.Obsolete("Possible phase out.")]
  61. public bool GetNextCodepoint(out int codepoint, out Color color)
  62. {
  63. color = Color.Clear;
  64. if (!TextSource.GetNextCodepoint(out codepoint))
  65. {
  66. return false;
  67. }
  68. if (SingleColor != null)
  69. {
  70. color = SingleColor.Value;
  71. }
  72. else
  73. {
  74. color = Colors[ColorPosition];
  75. ++ColorPosition;
  76. }
  77. return true;
  78. }
  79. public bool GetNextCodepoint(out int codepoint)
  80. {
  81. return TextSource.GetNextCodepoint(out codepoint);
  82. }
  83. public Color GetNextColor()
  84. {
  85. var color = Color.Clear;
  86. if (SingleColor != null)
  87. {
  88. color = SingleColor.Value;
  89. }
  90. else
  91. {
  92. color = Colors[ColorPosition % Colors.Length];
  93. ++ColorPosition;
  94. }
  95. return color;
  96. }
  97. }
  98. }