From 9d64c60e011f33095a95f3620891e46fe19b63fc Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 4 Sep 2019 23:42:30 +0300 Subject: [PATCH] LibDraw: Introduce an Emoji class This class can locate and load emojis, which are expected to be stored as regular PNG images at /res/emoji/U+XXXX.png, where XXXX is the character codepoint. https://github.com/SerenityOS/serenity/issues/490 --- Libraries/LibDraw/Emoji.cpp | 27 +++++++++++++++++++++++++++ Libraries/LibDraw/Emoji.h | 19 +++++++++++++++++++ Libraries/LibDraw/Makefile | 3 ++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Libraries/LibDraw/Emoji.cpp create mode 100644 Libraries/LibDraw/Emoji.h diff --git a/Libraries/LibDraw/Emoji.cpp b/Libraries/LibDraw/Emoji.cpp new file mode 100644 index 00000000000..650c688ece0 --- /dev/null +++ b/Libraries/LibDraw/Emoji.cpp @@ -0,0 +1,27 @@ +#include "Emoji.h" +#include "GraphicsBitmap.h" +#include +#include + +static HashMap s_emojis; + +Emoji::Emoji(NonnullRefPtr bitmap) + : m_bitmap(move(bitmap)) +{ +} + +const Emoji* Emoji::emoji_for_codepoint(u32 codepoint) +{ + auto it = s_emojis.find(codepoint); + if (it != s_emojis.end()) + return &(*it).value; + + String path = String::format("/res/emoji/U+%X.png", codepoint); + + auto bitmap = GraphicsBitmap::load_from_file(path); + if (!bitmap) + return nullptr; + + s_emojis.set(codepoint, Emoji { bitmap.release_nonnull() }); + return &(*s_emojis.find(codepoint)).value; +} diff --git a/Libraries/LibDraw/Emoji.h b/Libraries/LibDraw/Emoji.h new file mode 100644 index 00000000000..7a810e4650b --- /dev/null +++ b/Libraries/LibDraw/Emoji.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +class GraphicsBitmap; + +class Emoji { + public: + ~Emoji() {} + + static const Emoji* emoji_for_codepoint(u32 codepoint); + const GraphicsBitmap& bitmap() const { return m_bitmap; } + +private: + explicit Emoji(NonnullRefPtr); + + NonnullRefPtr m_bitmap; +}; diff --git a/Libraries/LibDraw/Makefile b/Libraries/LibDraw/Makefile index c1330120f5f..653c372a2a5 100644 --- a/Libraries/LibDraw/Makefile +++ b/Libraries/LibDraw/Makefile @@ -9,7 +9,8 @@ OBJS = \ Painter.o \ PNGLoader.o \ Rect.o \ - StylePainter.o + StylePainter.o \ + Emoji.o LIBRARY = libdraw.a DEFINES += -DUSERLAND