mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:17:35 +00:00
LibGfx: Add Emoji::emoji_for_code_points(Span<u32> const&)
Not all emojis are just one code point, so the existing API is not sufficient: Emoji::emoji_for_code_point(u32). The file name for such emojis is simply each U+XXXX separated by an underscore.
This commit is contained in:
parent
1752f7720e
commit
514f3e9c74
2 changed files with 22 additions and 7 deletions
|
@ -1,31 +1,43 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Emoji.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
static HashMap<u32, RefPtr<Gfx::Bitmap>> s_emojis;
|
||||
// https://unicode.org/reports/tr51/
|
||||
// https://unicode.org/emoji/charts/emoji-list.html
|
||||
// https://unicode.org/emoji/charts/emoji-zwj-sequences.html
|
||||
|
||||
const Bitmap* Emoji::emoji_for_code_point(u32 code_point)
|
||||
static HashMap<Span<u32>, RefPtr<Gfx::Bitmap>> s_emojis;
|
||||
|
||||
Bitmap const* Emoji::emoji_for_code_point(u32 code_point)
|
||||
{
|
||||
auto it = s_emojis.find(code_point);
|
||||
return emoji_for_code_points(Array { code_point });
|
||||
}
|
||||
|
||||
Bitmap const* Emoji::emoji_for_code_points(Span<u32> const& code_points)
|
||||
{
|
||||
auto it = s_emojis.find(code_points);
|
||||
if (it != s_emojis.end())
|
||||
return (*it).value.ptr();
|
||||
|
||||
auto bitmap_or_error = Bitmap::try_load_from_file(String::formatted("/res/emoji/U+{:X}.png", code_point));
|
||||
auto basename = String::join('_', code_points, "U+{:X}");
|
||||
auto bitmap_or_error = Bitmap::try_load_from_file(String::formatted("/res/emoji/{}.png", basename));
|
||||
if (bitmap_or_error.is_error()) {
|
||||
s_emojis.set(code_point, nullptr);
|
||||
s_emojis.set(code_points, nullptr);
|
||||
return nullptr;
|
||||
}
|
||||
auto bitmap = bitmap_or_error.release_value();
|
||||
s_emojis.set(code_point, bitmap);
|
||||
s_emojis.set(code_points, bitmap);
|
||||
return bitmap.ptr();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue