UnicodeData.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. namespace Avalonia.Media.TextFormatting.Unicode
  2. {
  3. /// <summary>
  4. /// Helper for looking up unicode character class information
  5. /// </summary>
  6. internal static class UnicodeData
  7. {
  8. internal const int CATEGORY_BITS = 6;
  9. internal const int SCRIPT_BITS = 8;
  10. internal const int BIDI_BITS = 5;
  11. internal const int LINEBREAK_BITS = 6;
  12. internal const int SCRIPT_SHIFT = CATEGORY_BITS;
  13. internal const int BIDI_SHIFT = CATEGORY_BITS + SCRIPT_BITS;
  14. internal const int LINEBREAK_SHIFT = CATEGORY_BITS + SCRIPT_BITS + BIDI_BITS;
  15. internal const int CATEGORY_MASK = (1 << CATEGORY_BITS) - 1;
  16. internal const int SCRIPT_MASK = (1 << SCRIPT_BITS) - 1;
  17. internal const int BIDI_MASK = (1 << BIDI_BITS) - 1;
  18. internal const int LINEBREAK_MASK = (1 << LINEBREAK_BITS) - 1;
  19. private static readonly UnicodeTrie s_unicodeDataTrie;
  20. private static readonly UnicodeTrie s_graphemeBreakTrie;
  21. static UnicodeData()
  22. {
  23. s_unicodeDataTrie = new UnicodeTrie(typeof(UnicodeData).Assembly.GetManifestResourceStream("Avalonia.Assets.UnicodeData.trie"));
  24. s_graphemeBreakTrie = new UnicodeTrie(typeof(UnicodeData).Assembly.GetManifestResourceStream("Avalonia.Assets.GraphemeBreak.trie"));
  25. }
  26. /// <summary>
  27. /// Gets the <see cref="GeneralCategory"/> for a Unicode codepoint.
  28. /// </summary>
  29. /// <param name="codepoint">The codepoint in question.</param>
  30. /// <returns>The code point's general category.</returns>
  31. public static GeneralCategory GetGeneralCategory(int codepoint)
  32. {
  33. var value = s_unicodeDataTrie.Get(codepoint);
  34. return (GeneralCategory)(value & CATEGORY_MASK);
  35. }
  36. /// <summary>
  37. /// Gets the <see cref="Script"/> for a Unicode codepoint.
  38. /// </summary>
  39. /// <param name="codepoint">The codepoint in question.</param>
  40. /// <returns>The code point's script.</returns>
  41. public static Script GetScript(int codepoint)
  42. {
  43. var value = s_unicodeDataTrie.Get(codepoint);
  44. return (Script)((value >> SCRIPT_SHIFT) & SCRIPT_MASK);
  45. }
  46. /// <summary>
  47. /// Gets the <see cref="BiDiClass"/> for a Unicode codepoint.
  48. /// </summary>
  49. /// <param name="codepoint">The codepoint in question.</param>
  50. /// <returns>The code point's biDi class.</returns>
  51. public static BiDiClass GetBiDiClass(int codepoint)
  52. {
  53. var value = s_unicodeDataTrie.Get(codepoint);
  54. return (BiDiClass)((value >> BIDI_SHIFT) & BIDI_MASK);
  55. }
  56. /// <summary>
  57. /// Gets the line break class for a Unicode codepoint.
  58. /// </summary>
  59. /// <param name="codepoint">The codepoint in question.</param>
  60. /// <returns>The code point's line break class.</returns>
  61. public static LineBreakClass GetLineBreakClass(int codepoint)
  62. {
  63. var value = s_unicodeDataTrie.Get(codepoint);
  64. return (LineBreakClass)((value >> LINEBREAK_SHIFT) & LINEBREAK_MASK);
  65. }
  66. /// <summary>
  67. /// Gets the grapheme break type for the Unicode codepoint.
  68. /// </summary>
  69. /// <param name="codepoint">The codepoint in question.</param>
  70. /// <returns>The code point's grapheme break type.</returns>
  71. public static GraphemeBreakClass GetGraphemeClusterBreak(int codepoint)
  72. {
  73. return (GraphemeBreakClass)s_graphemeBreakTrie.Get(codepoint);
  74. }
  75. }
  76. }