diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-02-23 08:53:35 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-24 19:48:47 +0100 |
commit | 8be43cd3bf10e7f94418859a7f91e0a4a38717c7 (patch) | |
tree | 2e0940477d4e7e60b67dbaf2e2b7e88a412d7af4 /Userland | |
parent | 392c8c99aaf9e55d820b2f4de3678921729fea93 (diff) | |
download | serenity-8be43cd3bf10e7f94418859a7f91e0a4a38717c7.zip |
LibGfx: Use LibUnicode to filter code points that cannot start an emoji
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGfx/Font/Emoji.cpp | 27 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Painter.cpp | 17 |
2 files changed, 7 insertions, 37 deletions
diff --git a/Userland/Libraries/LibGfx/Font/Emoji.cpp b/Userland/Libraries/LibGfx/Font/Emoji.cpp index 055e45df60..51f6c6d85b 100644 --- a/Userland/Libraries/LibGfx/Font/Emoji.cpp +++ b/Userland/Libraries/LibGfx/Font/Emoji.cpp @@ -52,34 +52,9 @@ Bitmap const* Emoji::emoji_for_code_points(ReadonlySpan<u32> const& code_points) } template<typename CodePointIterator> -static bool could_be_emoji(CodePointIterator const& it) -{ - if (it.done()) - return false; - - static constexpr u32 supplementary_private_use_area_b_first_code_point = 0x100000; - if (*it >= supplementary_private_use_area_b_first_code_point) { - // We use Supplementary Private Use Area-B for custom Serenity emoji. - return true; - } - - static auto const emoji_property = Unicode::property_from_string("Emoji"sv); - if (!emoji_property.has_value()) { - // This means Unicode data generation is disabled. Always check the disk in that case. - return true; - } - - return Unicode::code_point_has_property(*it, *emoji_property); -} - -template<typename CodePointIterator> static Bitmap const* emoji_for_code_point_iterator_impl(CodePointIterator& 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). - if (!could_be_emoji(it)) + if (!Unicode::could_be_start_of_emoji_sequence(it)) return nullptr; constexpr size_t max_emoji_code_point_sequence_length = 10; diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index ee85d60752..745ca4187b 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -34,6 +34,7 @@ #include <LibGfx/TextDirection.h> #include <LibGfx/TextLayout.h> #include <LibUnicode/CharacterTypes.h> +#include <LibUnicode/Emoji.h> #include <stdio.h> #if defined(AK_COMPILER_GCC) @@ -1396,13 +1397,14 @@ void Painter::draw_glyph_or_emoji(FloatPoint point, u32 code_point, Font const& void Painter::draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font, Color color) { - static auto const emoji_component_property = Unicode::property_from_string("Emoji_Component"sv); - static auto const variation_selector = Unicode::property_from_string("Variation_Selector"sv); - u32 code_point = *it; auto next_code_point = it.peek(1); ScopeGuard consume_variation_selector = [&, initial_it = it] { + static auto const variation_selector = Unicode::property_from_string("Variation_Selector"sv); + if (!variation_selector.has_value()) + return; + // If we advanced the iterator to consume an emoji sequence, don't look for another variation selector. if (initial_it != it) return; @@ -1413,14 +1415,7 @@ void Painter::draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, F }; auto font_contains_glyph = font.contains_glyph(code_point); - auto check_for_emoji = false; - - if (emoji_component_property.has_value()) { - auto code_point_is_emoji_component = Unicode::code_point_has_property(code_point, *emoji_component_property); - auto next_code_point_is_emoji_component = next_code_point.has_value() && Unicode::code_point_has_property(*next_code_point, *emoji_component_property); - - check_for_emoji = code_point_is_emoji_component || next_code_point_is_emoji_component; - } + auto check_for_emoji = Unicode::could_be_start_of_emoji_sequence(it); // If the font contains the glyph, and we know it's not the start of an emoji, draw a text glyph. if (font_contains_glyph && !check_for_emoji) { |