UnicodeRange.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #nullable enable
  2. using System.Reflection;
  3. using System.Text.Unicode;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Represents all of the Uniicode ranges.from System.Text.Unicode.UnicodeRange plus
  7. /// the non-BMP ranges not included.
  8. /// </summary>
  9. public class UnicodeRange (int start, int end, string category)
  10. {
  11. /// <summary>
  12. /// Gets the list of all ranges.
  13. /// </summary>
  14. public static List<UnicodeRange> Ranges => GetRanges ();
  15. /// <summary>
  16. /// The category.
  17. /// </summary>
  18. public string Category { get; set; } = category;
  19. /// <summary>
  20. /// Te codepoint at the start of the range.
  21. /// </summary>
  22. public int Start { get; set; } = start;
  23. /// <summary>
  24. /// The codepoint at the end of the range.
  25. /// </summary>
  26. public int End { get; set; } = end;
  27. /// <summary>
  28. /// Gets the list of all ranges..
  29. /// </summary>
  30. /// <returns></returns>
  31. public static List<UnicodeRange> GetRanges ()
  32. {
  33. IEnumerable<UnicodeRange> ranges =
  34. from r in typeof (UnicodeRanges).GetProperties (BindingFlags.Static | BindingFlags.Public)
  35. let urange = r.GetValue (null) as System.Text.Unicode.UnicodeRange
  36. let name = string.IsNullOrEmpty (r.Name)
  37. ? $"U+{urange.FirstCodePoint:x5}-U+{urange.FirstCodePoint + urange.Length:x5}"
  38. : r.Name
  39. where name != "None" && name != "All"
  40. select new UnicodeRange (urange.FirstCodePoint, urange.FirstCodePoint + urange.Length, name);
  41. // .NET 8.0 only supports BMP in UnicodeRanges: https://learn.microsoft.com/en-us/dotnet/api/system.text.unicode.unicoderanges?view=net-8.0
  42. List<UnicodeRange> nonBmpRanges = new ()
  43. {
  44. new (
  45. 0x1F130,
  46. 0x1F149,
  47. "Squared Latin Capital Letters"
  48. ),
  49. new (
  50. 0x12400,
  51. 0x1240f,
  52. "Cuneiform Numbers and Punctuation"
  53. ),
  54. new (0x10000, 0x1007F, "Linear B Syllabary"),
  55. new (0x10080, 0x100FF, "Linear B Ideograms"),
  56. new (0x10100, 0x1013F, "Aegean Numbers"),
  57. new (0x10300, 0x1032F, "Old Italic"),
  58. new (0x10330, 0x1034F, "Gothic"),
  59. new (0x10380, 0x1039F, "Ugaritic"),
  60. new (0x10400, 0x1044F, "Deseret"),
  61. new (0x10450, 0x1047F, "Shavian"),
  62. new (0x10480, 0x104AF, "Osmanya"),
  63. new (0x10800, 0x1083F, "Cypriot Syllabary"),
  64. new (
  65. 0x1D000,
  66. 0x1D0FF,
  67. "Byzantine Musical Symbols"
  68. ),
  69. new (0x1D100, 0x1D1FF, "Musical Symbols"),
  70. new (0x1D300, 0x1D35F, "Tai Xuan Jing Symbols"),
  71. new (
  72. 0x1D400,
  73. 0x1D7FF,
  74. "Mathematical Alphanumeric Symbols"
  75. ),
  76. new (0x1F600, 0x1F532, "Emojis Symbols"),
  77. new (
  78. 0x20000,
  79. 0x2A6DF,
  80. "CJK Unified Ideographs Extension B"
  81. ),
  82. new (
  83. 0x2F800,
  84. 0x2FA1F,
  85. "CJK Compatibility Ideographs Supplement"
  86. ),
  87. new (0xE0000, 0xE007F, "Tags")
  88. };
  89. return ranges.Concat (nonBmpRanges).OrderBy (r => r.Category).ToList ();
  90. }
  91. }