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:
Linus Groh 2022-02-22 21:38:37 +00:00
parent 1752f7720e
commit 514f3e9c74
Notes: sideshowbarker 2024-07-17 18:21:08 +09:00
2 changed files with 22 additions and 7 deletions

View File

@ -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();
}

View File

@ -1,11 +1,13 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/Types.h>
namespace Gfx {
@ -14,7 +16,8 @@ class Bitmap;
class Emoji {
public:
static const Gfx::Bitmap* emoji_for_code_point(u32 code_point);
static Gfx::Bitmap const* emoji_for_code_point(u32 code_point);
static Gfx::Bitmap const* emoji_for_code_points(Span<u32> const&);
};
}