diff options
author | Sergey Bugaev <bugaevc@gmail.com> | 2019-09-04 23:45:55 +0300 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-05 16:37:39 +0200 |
commit | 27380b9d2b8409c091ce66aad3bcacc3543cc8ff (patch) | |
tree | 0112f91a1206c3912d72396eaebecfbfb2395692 /Libraries/LibDraw | |
parent | 9d64c60e011f33095a95f3620891e46fe19b63fc (diff) | |
download | serenity-27380b9d2b8409c091ce66aad3bcacc3543cc8ff.zip |
LibDraw: Add emoji support to the Font class
https://github.com/SerenityOS/serenity/issues/490
Diffstat (limited to 'Libraries/LibDraw')
-rw-r--r-- | Libraries/LibDraw/Font.cpp | 37 | ||||
-rw-r--r-- | Libraries/LibDraw/Font.h | 5 |
2 files changed, 34 insertions, 8 deletions
diff --git a/Libraries/LibDraw/Font.cpp b/Libraries/LibDraw/Font.cpp index 755400ab32..be6bf6e462 100644 --- a/Libraries/LibDraw/Font.cpp +++ b/Libraries/LibDraw/Font.cpp @@ -8,6 +8,9 @@ #include <LibC/mman.h> #include <LibC/stdio.h> #include <LibC/unistd.h> +#include <AK/Utf8View.h> +#include "Emoji.h" +#include "GraphicsBitmap.h" struct [[gnu::packed]] FontFileHeader { @@ -170,17 +173,37 @@ bool Font::write_to_file(const StringView& path) return true; } -int Font::width(const StringView& string) const +int Font::glyph_or_emoji_width(u32 codepoint) const { - if (!string.length()) - return 0; + if (codepoint < 256) + return glyph_width((char)codepoint); if (m_fixed_width) - return string.length() * m_glyph_width; + return m_glyph_width; + + auto emoji = Emoji::emoji_for_codepoint(codepoint); + if (emoji == nullptr) + return glyph_width('?'); + return emoji->bitmap().size().width(); +} + +int Font::width(const StringView& string) const +{ + Utf8View utf8 { string }; + return width(utf8); +} +int Font::width(const Utf8View& utf8) const +{ + bool first = true; int width = 0; - for (int i = 0; i < string.length(); ++i) - width += glyph_width(string.characters_without_null_termination()[i]) + 1; - return width - 1; + for (u32 codepoint : utf8) { + if (!first) + width += glyph_spacing(); + first = false; + width += glyph_or_emoji_width(codepoint); + } + + return width; } diff --git a/Libraries/LibDraw/Font.h b/Libraries/LibDraw/Font.h index bdc2e05846..c22b17c55f 100644 --- a/Libraries/LibDraw/Font.h +++ b/Libraries/LibDraw/Font.h @@ -6,6 +6,7 @@ #include <AK/RefCounted.h> #include <AK/Types.h> #include <LibDraw/Rect.h> +#include <AK/Utf8View.h> // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead? class GlyphBitmap { @@ -58,11 +59,13 @@ public: GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(u8)ch * m_glyph_height], { glyph_width(ch), m_glyph_height }); } u8 glyph_width(char ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[(u8)ch]; } + int glyph_or_emoji_width(u32 codepoint) const; u8 glyph_height() const { return m_glyph_height; } u8 min_glyph_width() const { return m_min_glyph_width; } u8 max_glyph_width() const { return m_max_glyph_width; } u8 glyph_spacing() const { return m_fixed_width ? 0 : 1; } - int width(const StringView& string) const; + int width(const StringView&) const; + int width(const Utf8View&) const; String name() const { return m_name; } void set_name(const StringView& name) { m_name = name; } |