mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +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) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||||
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/Span.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibGfx/Bitmap.h>
|
#include <LibGfx/Bitmap.h>
|
||||||
#include <LibGfx/Emoji.h>
|
#include <LibGfx/Emoji.h>
|
||||||
|
|
||||||
namespace Gfx {
|
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())
|
if (it != s_emojis.end())
|
||||||
return (*it).value.ptr();
|
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()) {
|
if (bitmap_or_error.is_error()) {
|
||||||
s_emojis.set(code_point, nullptr);
|
s_emojis.set(code_points, nullptr);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto bitmap = bitmap_or_error.release_value();
|
auto bitmap = bitmap_or_error.release_value();
|
||||||
s_emojis.set(code_point, bitmap);
|
s_emojis.set(code_points, bitmap);
|
||||||
return bitmap.ptr();
|
return bitmap.ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||||
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Forward.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
@ -14,7 +16,8 @@ class Bitmap;
|
||||||
|
|
||||||
class Emoji {
|
class Emoji {
|
||||||
public:
|
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&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue