From 9bc8f531178afecfeb5b9de35118b8574b10f488 Mon Sep 17 00:00:00 2001 From: yirui Date: Mon, 12 Sep 2016 10:37:11 +0900 Subject: [PATCH] Parse Emoji subtag and store it to FontLanguage Parse Emoji subtag and store into 4 different styles: default, text, color and empty. Replace hasEmojiFlag function with getEmojiStyle to get effective status according to script and subtag. However, score calculation for the font selection needs to be completed in the next stage. No performance regression is observed with this CL. Bug: 31608997 Test: Done by unittests. Change-Id: I923243641c946248dd5a0aa9fb9c940515310d34 --- .../flutter/libs/minikin/FontCollection.cpp | 2 +- .../src/flutter/libs/minikin/FontFamily.cpp | 2 +- .../src/flutter/libs/minikin/FontLanguage.cpp | 45 ++++++- .../src/flutter/libs/minikin/FontLanguage.h | 40 ++++-- .../flutter/tests/perftests/FontLanguage.cpp | 7 ++ .../flutter/tests/unittest/FontFamilyTest.cpp | 119 +++++++++++++++++- .../src/flutter/tests/unittest/how_to_run.txt | 2 +- 7 files changed, 194 insertions(+), 23 deletions(-) diff --git a/engine/src/flutter/libs/minikin/FontCollection.cpp b/engine/src/flutter/libs/minikin/FontCollection.cpp index 19ad7523f7..5bc3634e82 100644 --- a/engine/src/flutter/libs/minikin/FontCollection.cpp +++ b/engine/src/flutter/libs/minikin/FontCollection.cpp @@ -219,7 +219,7 @@ uint32_t FontCollection::calcCoverageScore(uint32_t ch, uint32_t vs, FontFamily* const FontLanguages& langs = FontLanguageListCache::getById(fontFamily->langId()); bool hasEmojiFlag = false; for (size_t i = 0; i < langs.size(); ++i) { - if (langs[i].hasEmojiFlag()) { + if (langs[i].getEmojiStyle() == FontLanguage::EMSTYLE_EMOJI) { hasEmojiFlag = true; break; } diff --git a/engine/src/flutter/libs/minikin/FontFamily.cpp b/engine/src/flutter/libs/minikin/FontFamily.cpp index f41a20a4f9..f5f397681a 100644 --- a/engine/src/flutter/libs/minikin/FontFamily.cpp +++ b/engine/src/flutter/libs/minikin/FontFamily.cpp @@ -161,7 +161,7 @@ FontStyle FontFamily::getStyle(size_t index) const { bool FontFamily::isColorEmojiFamily() const { const FontLanguages& languageList = FontLanguageListCache::getById(mLangId); for (size_t i = 0; i < languageList.size(); ++i) { - if (languageList[i].hasEmojiFlag()) { + if (languageList[i].getEmojiStyle() == FontLanguage::EMSTYLE_EMOJI) { return true; } } diff --git a/engine/src/flutter/libs/minikin/FontLanguage.cpp b/engine/src/flutter/libs/minikin/FontLanguage.cpp index 9cfa0aa664..fca4cdbfcd 100644 --- a/engine/src/flutter/libs/minikin/FontLanguage.cpp +++ b/engine/src/flutter/libs/minikin/FontLanguage.cpp @@ -18,7 +18,9 @@ #include "FontLanguage.h" +#include #include +#include #include namespace minikin { @@ -27,6 +29,18 @@ namespace minikin { (((uint32_t)(c1)) << 24 | ((uint32_t)(c2)) << 16 | ((uint32_t)(c3)) << 8 | \ ((uint32_t)(c4))) +// Check if a language code supports emoji according to its subtag +static bool isEmojiSubtag(const char* buf, size_t bufLen, const char* subtag, size_t subtagLen) { + if (bufLen < subtagLen) { + return false; + } + if (strncmp(buf, subtag, subtagLen) != 0) { + return false; // no match between two strings + } + return (bufLen == subtagLen || buf[subtagLen] == '\0' || + buf[subtagLen] == '-' || buf[subtagLen] == '_'); +} + // Parse BCP 47 language identifier into internal structure FontLanguage::FontLanguage(const char* buf, size_t length) : FontLanguage() { size_t i; @@ -53,8 +67,34 @@ FontLanguage::FontLanguage(const char* buf, size_t length) : FontLanguage() { mScript = SCRIPT_TAG(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]); } } - mSubScriptBits = scriptToSubScriptBits(mScript); + + if (mScript == SCRIPT_TAG('Z', 's', 'y', 'e')) { + mEmojiStyle = EMSTYLE_EMOJI; + } else if (mScript == SCRIPT_TAG('Z', 's', 'y', 'm')) { + mEmojiStyle = EMSTYLE_TEXT; + } + // 10 is the length of "-u-em-text", which is the shortest emoji subtag, + // unnecessary comparison can be avoided if total length is smaller than 10. + const size_t kMinSubtagLength = 10; + if (length < kMinSubtagLength) { + return; + } + + static const char kPrefix[] = "-u-em-"; + const char *pos = std::search(buf, buf + length, kPrefix, kPrefix + strlen(kPrefix)); + if (pos == buf + length) { + return; + } + pos += strlen(kPrefix); + const size_t remainingLength = length - (pos - buf); + if (isEmojiSubtag(pos, remainingLength, "emoji", 5)){ + mEmojiStyle = EMSTYLE_EMOJI; + } else if (isEmojiSubtag(pos, remainingLength, "text", 4)){ + mEmojiStyle = EMSTYLE_TEXT; + } else if (isEmojiSubtag(pos, remainingLength, "default", 7)){ + mEmojiStyle = EMSTYLE_DEFAULT; + } } //static @@ -95,9 +135,6 @@ uint8_t FontLanguage::scriptToSubScriptBits(uint32_t script) { case SCRIPT_TAG('K', 'o', 'r', 'e'): subScriptBits = kHanFlag | kHangulFlag; break; - case SCRIPT_TAG('Z', 's', 'y', 'e'): - subScriptBits = kEmojiFlag; - break; } return subScriptBits; } diff --git a/engine/src/flutter/libs/minikin/FontLanguage.h b/engine/src/flutter/libs/minikin/FontLanguage.h index b1bb6eb5f9..25a5cc3dfa 100644 --- a/engine/src/flutter/libs/minikin/FontLanguage.h +++ b/engine/src/flutter/libs/minikin/FontLanguage.h @@ -34,14 +34,25 @@ class FontLanguages; // font rendering. struct FontLanguage { public: + enum EmojiStyle : uint8_t { + EMSTYLE_EMPTY = 0, + EMSTYLE_DEFAULT = 1, + EMSTYLE_EMOJI = 2, + EMSTYLE_TEXT = 3, + }; // Default constructor creates the unsupported language. - FontLanguage() : mScript(0ul), mLanguage(0ul), mSubScriptBits(0ul) {} + FontLanguage() + : mScript(0ul), + mLanguage(0ul), + mSubScriptBits(0ul), + mEmojiStyle(EMSTYLE_EMPTY) {} // Parse from string FontLanguage(const char* buf, size_t length); bool operator==(const FontLanguage other) const { - return !isUnsupported() && isEqualScript(other) && mLanguage == other.mLanguage; + return !isUnsupported() && isEqualScript(other) && mLanguage == other.mLanguage && + mEmojiStyle == other.mEmojiStyle; } bool operator!=(const FontLanguage other) const { @@ -49,7 +60,7 @@ public: } bool isUnsupported() const { return mLanguage == 0ul; } - bool hasEmojiFlag() const { return mSubScriptBits & kEmojiFlag; } + EmojiStyle getEmojiStyle() const { return mEmojiStyle; } bool isEqualScript(const FontLanguage& other) const; @@ -64,7 +75,9 @@ public: // 0 = no match, 1 = script match, 2 = script and primary language match. int calcScoreFor(const FontLanguages& supported) const; - uint64_t getIdentifier() const { return (uint64_t)mScript << 32 | (uint64_t)mLanguage; } + uint64_t getIdentifier() const { + return (uint64_t)mScript << 32 | (uint64_t)mEmojiStyle << 24 | (uint64_t)mLanguage; + } private: friend class FontLanguages; // for FontLanguages constructor @@ -73,21 +86,22 @@ private: uint32_t mScript; // ISO 639-1 or ISO 639-2 compliant language code. - // The two or three letter language code is packed into 32 bit integer. + // The two or three letter language code is packed into 24 bit integer. // mLanguage = 0 means the FontLanguage is unsupported. uint32_t mLanguage; - // For faster comparing, use 8 bits for specific scripts. + // For faster comparing, use 7 bits for specific scripts. static const uint8_t kBopomofoFlag = 1u; - static const uint8_t kEmojiFlag = 1u << 1; - static const uint8_t kHanFlag = 1u << 2; - static const uint8_t kHangulFlag = 1u << 3; - static const uint8_t kHiraganaFlag = 1u << 4; - static const uint8_t kKatakanaFlag = 1u << 5; - static const uint8_t kSimplifiedChineseFlag = 1u << 6; - static const uint8_t kTraditionalChineseFlag = 1u << 7; + static const uint8_t kHanFlag = 1u << 1; + static const uint8_t kHangulFlag = 1u << 2; + static const uint8_t kHiraganaFlag = 1u << 3; + static const uint8_t kKatakanaFlag = 1u << 4; + static const uint8_t kSimplifiedChineseFlag = 1u << 5; + static const uint8_t kTraditionalChineseFlag = 1u << 6; uint8_t mSubScriptBits; + EmojiStyle mEmojiStyle; + static uint8_t scriptToSubScriptBits(uint32_t script); // Returns true if the provide subscript bits has the requested subscript bits. diff --git a/engine/src/flutter/tests/perftests/FontLanguage.cpp b/engine/src/flutter/tests/perftests/FontLanguage.cpp index 8f77e12b75..6c9c84de88 100644 --- a/engine/src/flutter/tests/perftests/FontLanguage.cpp +++ b/engine/src/flutter/tests/perftests/FontLanguage.cpp @@ -33,4 +33,11 @@ static void BM_FontLanguage_en_Latn_US(benchmark::State& state) { } BENCHMARK(BM_FontLanguage_en_Latn_US); +static void BM_FontLanguage_en_Latn_US_u_em_emoji(benchmark::State& state) { + while (state.KeepRunning()) { + FontLanguage language("en-Latn-US-u-em-emoji", 21); + } +} +BENCHMARK(BM_FontLanguage_en_Latn_US_u_em_emoji); + } // namespace minikin diff --git a/engine/src/flutter/tests/unittest/FontFamilyTest.cpp b/engine/src/flutter/tests/unittest/FontFamilyTest.cpp index 4aaa601a23..95151a2163 100644 --- a/engine/src/flutter/tests/unittest/FontFamilyTest.cpp +++ b/engine/src/flutter/tests/unittest/FontFamilyTest.cpp @@ -269,17 +269,130 @@ TEST_F(FontLanguagesTest, repeatedLanguageTests) { TEST_F(FontLanguagesTest, undEmojiTests) { FontLanguage emoji = createFontLanguage("und-Zsye"); - EXPECT_TRUE(emoji.hasEmojiFlag()); + EXPECT_EQ(FontLanguage::EMSTYLE_EMOJI, emoji.getEmojiStyle()); FontLanguage und = createFontLanguage("und"); - EXPECT_FALSE(und.hasEmojiFlag()); + EXPECT_EQ(FontLanguage::EMSTYLE_EMPTY, und.getEmojiStyle()); EXPECT_FALSE(emoji == und); FontLanguage undExample = createFontLanguage("und-example"); - EXPECT_FALSE(undExample.hasEmojiFlag()); + EXPECT_EQ(FontLanguage::EMSTYLE_EMPTY, undExample.getEmojiStyle()); EXPECT_FALSE(emoji == undExample); } +TEST_F(FontLanguagesTest, subtagEmojiTest) { + std::string subtagEmojiStrings[] = { + // Duplicate subtag case. + "und-Latn-u-em-emoji-u-em-text", + + // Strings that contain language. + "und-u-em-emoji", + "en-u-em-emoji", + + // Strings that contain the script. + "und-Jpan-u-em-emoji", + "en-Latn-u-em-emoji", + "und-Zsym-u-em-emoji", + "und-Zsye-u-em-emoji", + "en-Zsym-u-em-emoji", + "en-Zsye-u-em-emoji", + + // Strings that contain the county. + "und-US-u-em-emoji", + "en-US-u-em-emoji", + "und-Latn-US-u-em-emoji", + "en-Zsym-US-u-em-emoji", + "en-Zsye-US-u-em-emoji", + }; + + for (auto subtagEmojiString : subtagEmojiStrings) { + SCOPED_TRACE("Test for \"" + subtagEmojiString + "\""); + FontLanguage subtagEmoji = createFontLanguage(subtagEmojiString); + EXPECT_EQ(FontLanguage::EMSTYLE_EMOJI, subtagEmoji.getEmojiStyle()); + } +} + +TEST_F(FontLanguagesTest, subtagTextTest) { + std::string subtagTextStrings[] = { + // Duplicate subtag case. + "und-Latn-u-em-text-u-em-emoji", + + // Strings that contain language. + "und-u-em-text", + "en-u-em-text", + + // Strings that contain the script. + "und-Latn-u-em-text", + "en-Jpan-u-em-text", + "und-Zsym-u-em-text", + "und-Zsye-u-em-text", + "en-Zsym-u-em-text", + "en-Zsye-u-em-text", + + // Strings that contain the county. + "und-US-u-em-text", + "en-US-u-em-text", + "und-Latn-US-u-em-text", + "en-Zsym-US-u-em-text", + "en-Zsye-US-u-em-text", + }; + + for (auto subtagTextString : subtagTextStrings) { + SCOPED_TRACE("Test for \"" + subtagTextString + "\""); + FontLanguage subtagText = createFontLanguage(subtagTextString); + EXPECT_EQ(FontLanguage::EMSTYLE_TEXT, subtagText.getEmojiStyle()); + } +} + +// TODO: add more "und" language cases whose language and script are +// unexpectedly translated to en-Latn by ICU. +TEST_F(FontLanguagesTest, subtagDefaultTest) { + std::string subtagDefaultStrings[] = { + // Duplicate subtag case. + "en-Latn-u-em-default-u-em-emoji", + "en-Latn-u-em-default-u-em-text", + + // Strings that contain language. + "und-u-em-default", + "en-u-em-default", + + // Strings that contain the script. + "en-Latn-u-em-default", + "en-Zsym-u-em-default", + "en-Zsye-u-em-default", + + // Strings that contain the county. + "en-US-u-em-default", + "en-Latn-US-u-em-default", + "en-Zsym-US-u-em-default", + "en-Zsye-US-u-em-default", + }; + + for (auto subtagDefaultString : subtagDefaultStrings) { + SCOPED_TRACE("Test for \"" + subtagDefaultString + "\""); + FontLanguage subtagDefault = createFontLanguage(subtagDefaultString); + EXPECT_EQ(FontLanguage::EMSTYLE_DEFAULT, subtagDefault.getEmojiStyle()); + } +} + +TEST_F(FontLanguagesTest, subtagEmptyTest) { + std::string subtagEmptyStrings[] = { + "und", + "jp", + "en-US", + "en-Latn", + "en-Latn-US", + "en-Latn-US-u-em", + "en-Latn-US-u-em-defaultemoji", + }; + + for (auto subtagEmptyString : subtagEmptyStrings) { + SCOPED_TRACE("Test for \"" + subtagEmptyString + "\""); + FontLanguage subtagEmpty = createFontLanguage(subtagEmptyString); + EXPECT_EQ(FontLanguage::EMSTYLE_EMPTY, subtagEmpty.getEmojiStyle()); + } +} + TEST_F(FontLanguagesTest, registerLanguageListTest) { EXPECT_EQ(0UL, FontStyle::registerLanguageList("")); EXPECT_NE(0UL, FontStyle::registerLanguageList("en")); diff --git a/engine/src/flutter/tests/unittest/how_to_run.txt b/engine/src/flutter/tests/unittest/how_to_run.txt index c90c6640bd..4adfcb8d05 100644 --- a/engine/src/flutter/tests/unittest/how_to_run.txt +++ b/engine/src/flutter/tests/unittest/how_to_run.txt @@ -1,4 +1,4 @@ -mmm -j8 frameworks/minikin/tests/unittests && +mmm -j8 frameworks/minikin/tests/unittest && adb push $OUT/data/nativetest/minikin_tests/minikin_tests \ /data/nativetest/minikin_tests/minikin_tests && adb push frameworks/minikin/tests/data /data/nativetest/minikin_tests/ &&