Emoji.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/Utf32View.h>
  8. #include <AK/Utf8View.h>
  9. #include <LibUnicode/CharacterTypes.h>
  10. #include <LibUnicode/Emoji.h>
  11. namespace Unicode {
  12. Optional<Emoji> __attribute__((weak)) find_emoji_for_code_points(ReadonlySpan<u32>) { return {}; }
  13. // https://unicode.org/reports/tr51/#def_emoji_core_sequence
  14. static bool could_be_start_of_emoji_core_sequence(u32 code_point, Optional<u32> const& next_code_point, SequenceType type)
  15. {
  16. // emoji_core_sequence := emoji_character | emoji_presentation_sequence | emoji_keycap_sequence | emoji_modifier_sequence | emoji_flag_sequence
  17. static constexpr auto emoji_presentation_selector = 0xFE0Fu;
  18. static constexpr auto combining_enclosing_keycap = 0x20E3u;
  19. static constexpr auto zero_width_joiner = 0x200Du;
  20. // https://unicode.org/reports/tr51/#def_emoji_keycap_sequence
  21. // emoji_keycap_sequence := [0-9#*] \x{FE0F 20E3}
  22. if (is_ascii_digit(code_point) || code_point == '#' || code_point == '*')
  23. return next_code_point == emoji_presentation_selector || next_code_point == combining_enclosing_keycap;
  24. // A little non-standard, but all other ASCII code points are not the beginning of any emoji sequence.
  25. if (is_ascii(code_point))
  26. return false;
  27. // https://unicode.org/reports/tr51/#def_emoji_character
  28. switch (type) {
  29. case SequenceType::Any:
  30. if (code_point_has_emoji_property(code_point))
  31. return true;
  32. break;
  33. case SequenceType::EmojiPresentation:
  34. if (code_point_has_emoji_presentation_property(code_point))
  35. return true;
  36. if (next_code_point == zero_width_joiner && code_point_has_emoji_property(code_point))
  37. return true;
  38. break;
  39. }
  40. // https://unicode.org/reports/tr51/#def_emoji_presentation_sequence
  41. // emoji_presentation_sequence := emoji_character emoji_presentation_selector
  42. if (next_code_point == emoji_presentation_selector)
  43. return true;
  44. // https://unicode.org/reports/tr51/#def_emoji_modifier_sequence
  45. // emoji_modifier_sequence := emoji_modifier_base emoji_modifier
  46. if (code_point_has_emoji_modifier_base_property(code_point))
  47. return true;
  48. // https://unicode.org/reports/tr51/#def_emoji_flag_sequence
  49. // emoji_flag_sequence := regional_indicator regional_indicator
  50. if (code_point_has_regional_indicator_property(code_point))
  51. return true;
  52. return false;
  53. }
  54. static bool could_be_start_of_serenity_emoji(u32 code_point)
  55. {
  56. // We use Supplementary Private Use Area-B for custom Serenity emoji, starting at U+10CD00.
  57. static constexpr auto first_custom_serenity_emoji_code_point = 0x10CD00u;
  58. return code_point >= first_custom_serenity_emoji_code_point;
  59. }
  60. // https://unicode.org/reports/tr51/#def_emoji_sequence
  61. template<typename CodePointIterator>
  62. static bool could_be_start_of_emoji_sequence_impl(CodePointIterator const& it, SequenceType type)
  63. {
  64. // emoji_sequence := emoji_core_sequence | emoji_zwj_sequence | emoji_tag_sequence
  65. if (it.done())
  66. return false;
  67. // The purpose of this method is to quickly filter out code points that cannot be the start of
  68. // an emoji. The emoji_core_sequence definition alone captures the start of all possible
  69. // emoji_zwj_sequence and emoji_tag_sequence emojis, because:
  70. //
  71. // * emoji_zwj_sequence must begin with emoji_zwj_element, which is:
  72. // emoji_zwj_element := emoji_core_sequence | emoji_tag_sequence
  73. //
  74. // * emoji_tag_sequence must begin with tag_base, which is:
  75. // tag_base := emoji_character | emoji_modifier_sequence | emoji_presentation_sequence
  76. // Note that this is a subset of emoji_core_sequence.
  77. auto code_point = *it;
  78. auto next_code_point = it.peek(1);
  79. if (could_be_start_of_emoji_core_sequence(code_point, next_code_point, type))
  80. return true;
  81. if (could_be_start_of_serenity_emoji(code_point))
  82. return true;
  83. return false;
  84. }
  85. bool could_be_start_of_emoji_sequence(Utf8CodePointIterator const& it, SequenceType type)
  86. {
  87. return could_be_start_of_emoji_sequence_impl(it, type);
  88. }
  89. bool could_be_start_of_emoji_sequence(Utf32CodePointIterator const& it, SequenceType type)
  90. {
  91. return could_be_start_of_emoji_sequence_impl(it, type);
  92. }
  93. }