diff --git a/Userland/Libraries/LibGfx/Emoji.cpp b/Userland/Libraries/LibGfx/Emoji.cpp index 8591b96833..a69d338d6b 100644 --- a/Userland/Libraries/LibGfx/Emoji.cpp +++ b/Userland/Libraries/LibGfx/Emoji.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -41,4 +42,48 @@ Bitmap const* Emoji::emoji_for_code_points(Span const& code_points) return bitmap.ptr(); } +Bitmap const* Emoji::emoji_for_code_point_iterator(Utf8CodePointIterator& it) +{ + // NOTE: I'm sure this could be more efficient, e.g. by checking if each code point falls + // into a certain range in the loop below (emojis, modifiers, variation selectors, ZWJ), + // and bailing out early if not. Current worst case is 10 file lookups for any sequence of + // code points (if the first glyph isn't part of the font in regular text rendering). + + constexpr size_t max_emoji_code_point_sequence_length = 10; + + Vector code_points; + + struct EmojiAndCodePoints { + Bitmap const* emoji; + Span code_points; + }; + Vector possible_emojis; + + // Determine all existing emojis for the longest possible ZWJ emoji sequence, + // or until we run out of code points in the iterator. + for (size_t i = 0; i < max_emoji_code_point_sequence_length; ++i) { + auto code_point = it.peek(i); + if (!code_point.has_value()) + break; + code_points.append(*code_point); + if (auto const* emoji = emoji_for_code_points(code_points)) + possible_emojis.empend(emoji, code_points); + } + + if (possible_emojis.is_empty()) + return nullptr; + + // If we found one or more matches, return the longest, i.e. last. For example: + // U+1F3F3 - white flag + // U+1F3F3 U+FE0F U+200D U+1F308 - rainbow flag + auto& [emoji, emoji_code_points] = possible_emojis.last(); + + // Advance the iterator, so it's on the last code point of our found emoji and + // whoever is iterating will advance to the next new code point. + for (size_t i = 0; i < emoji_code_points.size() - 1; ++i) + ++it; + + return emoji; +} + } diff --git a/Userland/Libraries/LibGfx/Emoji.h b/Userland/Libraries/LibGfx/Emoji.h index 9f706b451b..804d7e4680 100644 --- a/Userland/Libraries/LibGfx/Emoji.h +++ b/Userland/Libraries/LibGfx/Emoji.h @@ -18,6 +18,7 @@ class Emoji { public: static Gfx::Bitmap const* emoji_for_code_point(u32 code_point); static Gfx::Bitmap const* emoji_for_code_points(Span const&); + static Gfx::Bitmap const* emoji_for_code_point_iterator(Utf8CodePointIterator&); }; }