Skip to content

Commit 1484d3d

Browse files
trflynn89linusg
authored andcommitted
LibUnicode: Add a method to check if a code point could start an emoji
1 parent 8c38d46 commit 1484d3d

File tree

4 files changed

+163
-2
lines changed

4 files changed

+163
-2
lines changed

Tests/LibUnicode/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(TEST_SOURCES
2+
TestEmoji.cpp
23
TestUnicodeCharacterTypes.cpp
34
TestUnicodeNormalization.cpp
45
)

Tests/LibUnicode/TestEmoji.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <AK/Array.h>
8+
#include <AK/CharacterTypes.h>
9+
#include <AK/String.h>
10+
#include <AK/Utf8View.h>
11+
#include <LibTest/TestCase.h>
12+
#include <LibUnicode/Emoji.h>
13+
14+
// These emojis are the first subgroup in each Unicode-defined group of emojis, plus some interesting
15+
// hand-picked test cases (such as keycap emoji, which begin with ASCII symbols, and country flags).
16+
static constexpr auto s_smileys_emotion = Array { "😀"sv, "😃"sv, "😄"sv, "😁"sv, "😆"sv, "😅"sv, "🤣"sv, "😂"sv, "🙂"sv, "🙃"sv, "🫠"sv, "😉"sv, "😊"sv, "😇"sv };
17+
static constexpr auto s_people_body = Array { "👋"sv, "🤚"sv, "🖐️"sv, "🖐"sv, ""sv, "🫱"sv, "🫲"sv, "🫳"sv, "🫴"sv, "🫷"sv, "🫸"sv };
18+
static constexpr auto s_animals_nature = Array { "🐶"sv, "🐕"sv, "🐕‍🦺"sv, "🐩"sv, "🦊"sv, "🦝"sv, "🐱"sv, "🐈"sv, "🐈‍⬛"sv, "🦁"sv, "🐯"sv, "🐴"sv, "🫎"sv, "🫏"sv, "🐎"sv, "🦄"sv, "🦓"sv, "🦌"sv, "🦬"sv, "🐮"sv, "🐷"sv, "🐖"sv, "🐗"sv, "🐽"sv, "🐑"sv, "🦙"sv, "🦒"sv, "🐘"sv, "🐭"sv, "🐁"sv, "🐀"sv, "🐰"sv, "🐇"sv, "🐿️"sv, "🐿"sv, "🦔"sv, "🦇"sv, "🐻"sv, "🐻‍❄️"sv, "🐻‍❄"sv, "🐨"sv, "🐼"sv, "🦥"sv, "🦘"sv, "🦡"sv, "🐾"sv };
19+
static constexpr auto s_food_drink = Array { "🍇"sv, "🍈"sv, "🍉"sv, "🍊"sv, "🍋"sv, "🍌"sv, "🍍"sv, "🥭"sv, "🍎"sv, "🍏"sv, "🍐"sv, "🍑"sv, "🍒"sv, "🍓"sv, "🫐"sv, "🥝"sv, "🍅"sv, "🫒"sv, "🥥"sv };
20+
static constexpr auto s_travel_places = Array { "🌍"sv, "🌎"sv, "🌏"sv, "🌐"sv, "🗺️"sv, "🗺"sv, "🗾"sv, "🧭"sv };
21+
static constexpr auto s_activities = Array { "🎃"sv, "🎄"sv, "🎆"sv, "🎇"sv, "🧨"sv, ""sv, "🎈"sv, "🎉"sv, "🎊"sv, "🎋"sv, "🎍"sv, "🎏"sv, "🎑"sv, "🎀"sv, "🎁"sv, "🎗️"sv, "🎗"sv, "🎟️"sv, "🎟"sv, "🎫"sv };
22+
static constexpr auto s_objects = Array { "👓"sv, "🕶️"sv, "🕶"sv, "🦺"sv, "👔"sv, "👖"sv, "🧦"sv, "👗"sv, "🥻"sv, "🩱"sv, "🩲"sv, "🩳"sv, "👙"sv, "🪭"sv, "👛"sv, "👜"sv, "🛍️"sv, "🛍"sv, "🩴"sv, "👡"sv, "👢"sv, "🪮"sv, "👑"sv, "🎩"sv, "🎓"sv, "🪖"sv, "⛑️"sv, ""sv, "💄"sv, "💍"sv, "💎"sv };
23+
static constexpr auto s_symbols = Array { "🚮"sv, "🚰"sv, ""sv, "🚹"sv, "🚺"sv, "🚾"sv, "🛂"sv, "🛃"sv, "🛄"sv, "🛅"sv, "#️⃣"sv, "#⃣"sv, "*️⃣"sv, "*⃣"sv, "0️⃣"sv, "0⃣"sv, "1️⃣"sv, "1⃣"sv, "2️⃣"sv, "2⃣"sv, "3️⃣"sv, "3⃣"sv, "4️⃣"sv, "4⃣"sv, "5️⃣"sv, "5⃣"sv, "6️⃣"sv, "6⃣"sv, "7️⃣"sv, "7⃣"sv, "8️⃣"sv, "8⃣"sv, "9️⃣"sv, "9⃣"sv, "🔟"sv };
24+
static constexpr auto s_flags = Array { "🏁"sv, "🚩"sv, "🎌"sv, "🏴"sv, "🏳️"sv, "🏳"sv, "🏳️‍🌈"sv, "🏳‍🌈"sv, "🏳️‍⚧️"sv, "🏳‍⚧️"sv, "🏳️‍⚧"sv, "🏳‍⚧"sv, "🏴‍☠️"sv, "🏴‍☠"sv, "🇦🇨"sv, "🇦🇩"sv, "🇦🇪"sv, "🇦🇫"sv, "🇦🇬"sv, "🇦🇮"sv, "🇦🇱"sv, "🇦🇲"sv, "🇦🇴"sv, "🇦🇶"sv, "🇦🇷"sv, "🇦🇸"sv, "🇦🇹"sv, "🇦🇺"sv, "🇦🇼"sv, "🇦🇽"sv, "🇦🇿"sv, "🇧🇦"sv, "🇧🇧"sv, "🇧🇩"sv, "🇧🇪"sv, "🇧🇫"sv, "🇧🇬"sv, "🇧🇭"sv, "🇧🇮"sv, "🇧🇯"sv, "🇧🇱"sv, "🇧🇲"sv, "🇧🇳"sv, "🇧🇴"sv, "🇧🇶"sv, "🇧🇷"sv, "🇧🇸"sv };
25+
26+
TEST_CASE(emoji)
27+
{
28+
auto test_emojis = [](auto const& emojis) {
29+
for (auto emoji : emojis) {
30+
Utf8View view { emoji };
31+
EXPECT(Unicode::could_be_start_of_emoji_sequence(view.begin()));
32+
}
33+
};
34+
35+
test_emojis(s_smileys_emotion);
36+
test_emojis(s_people_body);
37+
test_emojis(s_animals_nature);
38+
test_emojis(s_food_drink);
39+
test_emojis(s_travel_places);
40+
test_emojis(s_activities);
41+
test_emojis(s_objects);
42+
test_emojis(s_symbols);
43+
test_emojis(s_flags);
44+
}
45+
46+
TEST_CASE(ascii_is_not_emoji)
47+
{
48+
for (u32 code_point = 0u; is_ascii(code_point); ++code_point) {
49+
auto string = String::from_code_point(code_point);
50+
Utf8View view { string };
51+
52+
EXPECT(!Unicode::could_be_start_of_emoji_sequence(view.begin()));
53+
}
54+
}
Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,115 @@
11
/*
2-
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
2+
* Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

7+
#include <AK/CharacterTypes.h>
8+
#include <AK/Utf32View.h>
9+
#include <AK/Utf8View.h>
10+
#include <LibUnicode/CharacterTypes.h>
711
#include <LibUnicode/Emoji.h>
812

13+
#if ENABLE_UNICODE_DATA
14+
# include <LibUnicode/UnicodeData.h>
15+
#endif
16+
917
namespace Unicode {
1018

1119
Optional<Emoji> __attribute__((weak)) find_emoji_for_code_points(ReadonlySpan<u32>) { return {}; }
1220

21+
#if ENABLE_UNICODE_DATA
22+
23+
// https://unicode.org/reports/tr51/#def_emoji_core_sequence
24+
static bool could_be_start_of_emoji_core_sequence(u32 code_point, Optional<u32> const& next_code_point)
25+
{
26+
// emoji_core_sequence := emoji_character | emoji_presentation_sequence | emoji_keycap_sequence | emoji_modifier_sequence | emoji_flag_sequence
27+
28+
static constexpr auto emoji_presentation_selector = 0xFE0Fu;
29+
static constexpr auto combining_enclosing_keycap = 0x20E3u;
30+
31+
// https://unicode.org/reports/tr51/#def_emoji_keycap_sequence
32+
// emoji_keycap_sequence := [0-9#*] \x{FE0F 20E3}
33+
if (is_ascii_digit(code_point) || code_point == '#' || code_point == '*')
34+
return next_code_point == emoji_presentation_selector || next_code_point == combining_enclosing_keycap;
35+
36+
// A little non-standard, but all other ASCII code points are not the beginning of any emoji sequence.
37+
if (is_ascii(code_point))
38+
return false;
39+
40+
// https://unicode.org/reports/tr51/#def_emoji_character
41+
if (code_point_has_property(code_point, Property::Emoji))
42+
return true;
43+
44+
// https://unicode.org/reports/tr51/#def_emoji_presentation_sequence
45+
// emoji_presentation_sequence := emoji_character emoji_presentation_selector
46+
if (next_code_point == emoji_presentation_selector)
47+
return true;
48+
49+
// https://unicode.org/reports/tr51/#def_emoji_modifier_sequence
50+
// emoji_modifier_sequence := emoji_modifier_base emoji_modifier
51+
if (code_point_has_property(code_point, Property::Emoji_Modifier_Base))
52+
return true;
53+
54+
// https://unicode.org/reports/tr51/#def_emoji_flag_sequence
55+
// emoji_flag_sequence := regional_indicator regional_indicator
56+
if (code_point_has_property(code_point, Property::Regional_Indicator))
57+
return true;
58+
59+
return false;
60+
}
61+
62+
static bool could_be_start_of_serenity_emoji(u32 code_point)
63+
{
64+
// We use Supplementary Private Use Area-B for custom Serenity emoji, starting at U+10CD00.
65+
static constexpr auto first_custom_serenity_emoji_code_point = 0x10CD00u;
66+
67+
return code_point >= first_custom_serenity_emoji_code_point;
68+
}
69+
70+
#endif
71+
72+
// https://unicode.org/reports/tr51/#def_emoji_sequence
73+
template<typename CodePointIterator>
74+
static bool could_be_start_of_emoji_sequence_impl(CodePointIterator const& it)
75+
{
76+
// emoji_sequence := emoji_core_sequence | emoji_zwj_sequence | emoji_tag_sequence
77+
78+
if (it.done())
79+
return false;
80+
81+
#if ENABLE_UNICODE_DATA
82+
// The purpose of this method is to quickly filter out code points that cannot be the start of
83+
// an emoji. The emoji_core_sequence definition alone captures the start of all possible
84+
// emoji_zwj_sequence and emoji_tag_sequence emojis, because:
85+
//
86+
// * emoji_zwj_sequence must begin with emoji_zwj_element, which is:
87+
// emoji_zwj_sequence := emoji_core_sequence | emoji_tag_sequence
88+
//
89+
// * emoji_tag_sequence must begin with tag_base, which is:
90+
// tag_base := emoji_character | emoji_modifier_sequence | emoji_presentation_sequence
91+
// Note that this is a subset of emoji_core_sequence.
92+
auto code_point = *it;
93+
auto next_code_point = it.peek(1);
94+
95+
if (could_be_start_of_emoji_core_sequence(code_point, next_code_point))
96+
return true;
97+
if (could_be_start_of_serenity_emoji(code_point))
98+
return true;
99+
return false;
100+
#else
101+
return true;
102+
#endif
103+
}
104+
105+
bool could_be_start_of_emoji_sequence(Utf8CodePointIterator const& it)
106+
{
107+
return could_be_start_of_emoji_sequence_impl(it);
108+
}
109+
110+
bool could_be_start_of_emoji_sequence(Utf32CodePointIterator const& it)
111+
{
112+
return could_be_start_of_emoji_sequence_impl(it);
113+
}
114+
13115
}

Userland/Libraries/LibUnicode/Emoji.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
2+
* Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

77
#pragma once
88

9+
#include <AK/Forward.h>
910
#include <AK/Optional.h>
1011
#include <AK/StringView.h>
1112
#include <AK/Types.h>
@@ -46,6 +47,9 @@ Optional<Emoji> find_emoji_for_code_points(u32 const (&code_points)[Size])
4647
return find_emoji_for_code_points(ReadonlySpan<u32> { code_points });
4748
}
4849

50+
bool could_be_start_of_emoji_sequence(Utf8CodePointIterator const&);
51+
bool could_be_start_of_emoji_sequence(Utf32CodePointIterator const&);
52+
4953
constexpr StringView emoji_group_to_string(EmojiGroup group)
5054
{
5155
switch (group) {

0 commit comments

Comments
 (0)