diff --git a/engine/src/flutter/libs/minikin/FontLanguage.cpp b/engine/src/flutter/libs/minikin/FontLanguage.cpp index c6ddda5ae9..0897c06ea5 100644 --- a/engine/src/flutter/libs/minikin/FontLanguage.cpp +++ b/engine/src/flutter/libs/minikin/FontLanguage.cpp @@ -41,60 +41,169 @@ static bool isEmojiSubtag(const char* buf, size_t bufLen, const char* subtag, si buf[subtagLen] == '-' || buf[subtagLen] == '_'); } +// Pack the three letter code into 15 bits and stored to 16 bit integer. The highest bit is 0. +// For the region code, the letters must be all digits in three letter case, so the number of +// possible values are 10. For the language code, the letters must be all small alphabets, so the +// number of possible values are 26. Thus, 5 bits are sufficient for each case and we can pack the +// three letter language code or region code to 15 bits. +// +// In case of two letter code, use fullbit(0x1f) for the first letter instead. +static uint16_t packLanguageOrRegion(const char* c, size_t length, uint8_t twoLetterBase, + uint8_t threeLetterBase) { + if (length == 2) { + return 0x7c00u | // 0x1fu << 10 + (uint16_t)(c[0] - twoLetterBase) << 5 | + (uint16_t)(c[1] - twoLetterBase); + } else { + return ((uint16_t)(c[0] - threeLetterBase) << 10) | + (uint16_t)(c[1] - threeLetterBase) << 5 | + (uint16_t)(c[2] - threeLetterBase); + } +} + +static size_t unpackLanguageOrRegion(uint16_t in, char* out, uint8_t twoLetterBase, + uint8_t threeLetterBase) { + uint8_t first = (in >> 10) & 0x1f; + uint8_t second = (in >> 5) & 0x1f; + uint8_t third = in & 0x1f; + + if (first == 0x1f) { + out[0] = second + twoLetterBase; + out[1] = third + twoLetterBase; + return 2; + } else { + out[0] = first + threeLetterBase; + out[1] = second + threeLetterBase; + out[2] = third + threeLetterBase; + return 3; + } +} + +// Find the next '-' or '_' index from startOffset position. If not found, returns bufferLength. +static size_t nextDelimiterIndex(const char* buffer, size_t bufferLength, size_t startOffset) { + for (size_t i = startOffset; i < bufferLength; ++i) { + if (buffer[i] == '-' || buffer[i] == '_') { + return i; + } + } + return bufferLength; +} + +static inline bool isLowercase(char c) { + return 'a' <= c && c <= 'z'; +} + +static inline bool isUppercase(char c) { + return 'A' <= c && c <= 'Z'; +} + +static inline bool isDigit(char c) { + return '0' <= c && c <= '9'; +} + +// Returns true if the buffer is valid for language code. +static inline bool isValidLanguageCode(const char* buffer, size_t length) { + if (length != 2 && length != 3) return false; + if (!isLowercase(buffer[0])) return false; + if (!isLowercase(buffer[1])) return false; + if (length == 3 && !isLowercase(buffer[2])) return false; + return true; +} + +// Returns true if buffer is valid for script code. The length of buffer must be 4. +static inline bool isValidScriptCode(const char* buffer) { + return isUppercase(buffer[0]) && isLowercase(buffer[1]) && isLowercase(buffer[2]) && + isLowercase(buffer[3]); +} + +// Returns true if the buffer is valid for region code. +static inline bool isValidRegionCode(const char* buffer, size_t length) { + return (length == 2 && isUppercase(buffer[0]) && isUppercase(buffer[1])) || + (length == 3 && isDigit(buffer[0]) && isDigit(buffer[1]) && isDigit(buffer[2])); +} + // Parse BCP 47 language identifier into internal structure FontLanguage::FontLanguage(const char* buf, size_t length) : FontLanguage() { - size_t i; - for (i = 0; i < length; i++) { - char c = buf[i]; - if (c == '-' || c == '_') break; - } - if (i == 2 || i == 3) { // only accept two or three letter language code. - mLanguage = buf[0] | (buf[1] << 8) | ((i == 3) ? (buf[2] << 16) : 0); + size_t firstDelimiterPos = nextDelimiterIndex(buf, length, 0); + if (isValidLanguageCode(buf, firstDelimiterPos)) { + mLanguage = packLanguageOrRegion(buf, firstDelimiterPos, 'a', 'a'); } else { // We don't understand anything other than two-letter or three-letter // language codes, so we skip parsing the rest of the string. - mLanguage = 0ul; return; } - size_t next; - for (i++; i < length; i = next + 1) { - for (next = i; next < length; next++) { - char c = buf[next]; - if (c == '-' || c == '_') break; - } - if (next - i == 4 && 'A' <= buf[i] && buf[i] <= 'Z') { - mScript = SCRIPT_TAG(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]); - } + if (firstDelimiterPos == length) { + mHbLanguage = hb_language_from_string(getString().c_str(), -1); + return; // Language code only. } - 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; + size_t nextComponentStartPos = firstDelimiterPos + 1; + size_t nextDelimiterPos = nextDelimiterIndex(buf, length, nextComponentStartPos); + size_t componentLength = nextDelimiterPos - nextComponentStartPos; + + if (componentLength == 4) { + // Possibly script code. + const char* p = buf + nextComponentStartPos; + if (isValidScriptCode(p)) { + mScript = SCRIPT_TAG(p[0], p[1], p[2], p[3]); + mSubScriptBits = scriptToSubScriptBits(mScript); + } + + if (nextDelimiterPos == length) { + mHbLanguage = hb_language_from_string(getString().c_str(), -1); + mEmojiStyle = resolveEmojiStyle(buf, length, mScript); + return; // No region code. + } + + nextComponentStartPos = nextDelimiterPos + 1; + nextDelimiterPos = nextDelimiterIndex(buf, length, nextComponentStartPos); + componentLength = nextDelimiterPos - nextComponentStartPos; } + + if (componentLength == 2 || componentLength == 3) { + // Possibly region code. + const char* p = buf + nextComponentStartPos; + if (isValidRegionCode(p, componentLength)) { + mRegion = packLanguageOrRegion(p, componentLength, 'A', '0'); + } + } + + mHbLanguage = hb_language_from_string(getString().c_str(), -1); + mEmojiStyle = resolveEmojiStyle(buf, length, mScript); +} + +// static +FontLanguage::EmojiStyle FontLanguage::resolveEmojiStyle(const char* buf, size_t length, + uint32_t script) { + // First, lookup emoji subtag. // 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; + if (length >= kMinSubtagLength) { + static const char kPrefix[] = "-u-em-"; + const char *pos = std::search(buf, buf + length, kPrefix, kPrefix + strlen(kPrefix)); + if (pos != buf + length) { // found + pos += strlen(kPrefix); + const size_t remainingLength = length - (pos - buf); + if (isEmojiSubtag(pos, remainingLength, "emoji", 5)){ + return EMSTYLE_EMOJI; + } else if (isEmojiSubtag(pos, remainingLength, "text", 4)){ + return EMSTYLE_TEXT; + } else if (isEmojiSubtag(pos, remainingLength, "default", 7)){ + return EMSTYLE_DEFAULT; + } + } } - 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; + // If no emoji subtag was provided, resolve the emoji style from script code. + if (script == SCRIPT_TAG('Z', 's', 'y', 'e')) { + return EMSTYLE_EMOJI; + } else if (script == SCRIPT_TAG('Z', 's', 'y', 'm')) { + return EMSTYLE_TEXT; } + + return EMSTYLE_EMPTY; } //static @@ -140,21 +249,21 @@ uint8_t FontLanguage::scriptToSubScriptBits(uint32_t script) { } std::string FontLanguage::getString() const { - if (mLanguage == 0ul) { + if (isUnsupported()) { return "und"; } char buf[16]; - size_t i = 0; - buf[i++] = mLanguage & 0xFF ; - buf[i++] = (mLanguage >> 8) & 0xFF; - char third_letter = (mLanguage >> 16) & 0xFF; - if (third_letter != 0) buf[i++] = third_letter; + size_t i = unpackLanguageOrRegion(mLanguage, buf, 'a', 'a'); if (mScript != 0) { - buf[i++] = '-'; - buf[i++] = (mScript >> 24) & 0xFFu; - buf[i++] = (mScript >> 16) & 0xFFu; - buf[i++] = (mScript >> 8) & 0xFFu; - buf[i++] = mScript & 0xFFu; + buf[i++] = '-'; + buf[i++] = (mScript >> 24) & 0xFFu; + buf[i++] = (mScript >> 16) & 0xFFu; + buf[i++] = (mScript >> 8) & 0xFFu; + buf[i++] = mScript & 0xFFu; + } + if (mRegion != INVALID_CODE) { + buf[i++] = '-'; + i += unpackLanguageOrRegion(mRegion, buf + i, 'A', '0'); } return std::string(buf, i); } diff --git a/engine/src/flutter/libs/minikin/FontLanguage.h b/engine/src/flutter/libs/minikin/FontLanguage.h index 1bebdfeeb9..6a50b1d4cc 100644 --- a/engine/src/flutter/libs/minikin/FontLanguage.h +++ b/engine/src/flutter/libs/minikin/FontLanguage.h @@ -27,6 +27,10 @@ namespace minikin { // Due to the limits in font fallback score calculation, we can't use anything more than 12 // languages. const size_t FONT_LANGUAGES_LIMIT = 12; + +// The language or region code is encoded to 15 bits. +const uint16_t INVALID_CODE = 0x7fff; + class FontLanguages; // FontLanguage is a compact representation of a BCP 47 language tag. It @@ -43,7 +47,9 @@ public: // Default constructor creates the unsupported language. FontLanguage() : mScript(0ul), - mLanguage(0ul), + mLanguage(INVALID_CODE), + mRegion(INVALID_CODE), + mHbLanguage(HB_LANGUAGE_INVALID), mSubScriptBits(0ul), mEmojiStyle(EMSTYLE_EMPTY) {} @@ -52,15 +58,17 @@ public: bool operator==(const FontLanguage other) const { return !isUnsupported() && isEqualScript(other) && mLanguage == other.mLanguage && - mEmojiStyle == other.mEmojiStyle; + mRegion == other.mRegion && mEmojiStyle == other.mEmojiStyle; } bool operator!=(const FontLanguage other) const { return !(*this == other); } - bool isUnsupported() const { return mLanguage == 0ul; } + bool isUnsupported() const { return mLanguage == INVALID_CODE; } EmojiStyle getEmojiStyle() const { return mEmojiStyle; } + hb_language_t getHbLanguage() const { return mHbLanguage; } + bool isEqualScript(const FontLanguage& other) const; @@ -76,7 +84,8 @@ public: int calcScoreFor(const FontLanguages& supported) const; uint64_t getIdentifier() const { - return (uint64_t)mScript << 32 | (uint64_t)mEmojiStyle << 24 | (uint64_t)mLanguage; + return ((uint64_t)mLanguage << 49) | ((uint64_t)mScript << 17) | ((uint64_t)mRegion << 2) | + mEmojiStyle; } private: @@ -86,9 +95,16 @@ private: uint32_t mScript; // ISO 639-1 or ISO 639-2 compliant language code. - // The two or three letter language code is packed into 24 bit integer. + // The two- or three-letter language code is packed into a 15 bit integer. // mLanguage = 0 means the FontLanguage is unsupported. - uint32_t mLanguage; + uint16_t mLanguage; + + // ISO 3166-1 or UN M.49 compliant region code. The two-letter or three-digit region code is + // packed into a 15 bit integer. + uint16_t mRegion; + + // The language to be passed HarfBuzz shaper. + hb_language_t mHbLanguage; // For faster comparing, use 7 bits for specific scripts. static const uint8_t kBopomofoFlag = 1u; @@ -104,6 +120,8 @@ private: static uint8_t scriptToSubScriptBits(uint32_t script); + static EmojiStyle resolveEmojiStyle(const char* buf, size_t length, uint32_t script); + // Returns true if the provide subscript bits has the requested subscript bits. // Note that this function returns false if the requested subscript bits are empty. static bool supportsScript(uint8_t providedBits, uint8_t requestedBits); diff --git a/engine/src/flutter/libs/minikin/Layout.cpp b/engine/src/flutter/libs/minikin/Layout.cpp index 98e9fdb07b..117f326236 100644 --- a/engine/src/flutter/libs/minikin/Layout.cpp +++ b/engine/src/flutter/libs/minikin/Layout.cpp @@ -811,8 +811,7 @@ void Layout::doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t break; } } - hb_buffer_set_language(buffer, - hb_language_from_string(hbLanguage->getString().c_str(), -1)); + hb_buffer_set_language(buffer, hbLanguage->getHbLanguage()); } hb_buffer_add_utf16(buffer, buf, bufSize, srunstart + start, srunend - srunstart); if (ctx->paint.hyphenEdit.hasHyphen() && srunend > srunstart) { diff --git a/engine/src/flutter/tests/unittest/FontFamilyTest.cpp b/engine/src/flutter/tests/unittest/FontFamilyTest.cpp index 1655760bcf..fb917500f0 100644 --- a/engine/src/flutter/tests/unittest/FontFamilyTest.cpp +++ b/engine/src/flutter/tests/unittest/FontFamilyTest.cpp @@ -41,6 +41,10 @@ static FontLanguage createFontLanguage(const std::string& input) { return FontLanguageListCache::getById(langId)[0]; } +static FontLanguage createFontLanguageWithoutICUSanitization(const std::string& input) { + return FontLanguage(input.c_str(), input.size()); +} + TEST_F(FontLanguageTest, basicTests) { FontLanguage defaultLang; FontLanguage emptyLang("", 0); @@ -74,19 +78,25 @@ TEST_F(FontLanguageTest, basicTests) { } TEST_F(FontLanguageTest, getStringTest) { - EXPECT_EQ("en-Latn", createFontLanguage("en").getString()); - EXPECT_EQ("en-Latn", createFontLanguage("en-Latn").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("en").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("en-Latn").getString()); // Capitalized language code or lowercased script should be normalized. - EXPECT_EQ("en-Latn", createFontLanguage("EN-LATN").getString()); - EXPECT_EQ("en-Latn", createFontLanguage("EN-latn").getString()); - EXPECT_EQ("en-Latn", createFontLanguage("en-latn").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("EN-LATN").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("EN-latn").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("en-latn").getString()); // Invalid script should be kept. - EXPECT_EQ("en-Xyzt", createFontLanguage("en-xyzt").getString()); + EXPECT_EQ("en-Xyzt-US", createFontLanguage("en-xyzt").getString()); - EXPECT_EQ("en-Latn", createFontLanguage("en-Latn-US").getString()); - EXPECT_EQ("ja-Jpan", createFontLanguage("ja").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("en-Latn-US").getString()); + EXPECT_EQ("ja-Jpan-JP", createFontLanguage("ja").getString()); + EXPECT_EQ("zh-Hant-TW", createFontLanguage("zh-TW").getString()); + EXPECT_EQ("zh-Hant-HK", createFontLanguage("zh-HK").getString()); + EXPECT_EQ("zh-Hant-MO", createFontLanguage("zh-MO").getString()); + EXPECT_EQ("zh-Hans-CN", createFontLanguage("zh").getString()); + EXPECT_EQ("zh-Hans-CN", createFontLanguage("zh-CN").getString()); + EXPECT_EQ("zh-Hans-SG", createFontLanguage("zh-SG").getString()); EXPECT_EQ("und", createFontLanguage("und").getString()); EXPECT_EQ("und", createFontLanguage("UND").getString()); EXPECT_EQ("und", createFontLanguage("Und").getString()); @@ -94,10 +104,44 @@ TEST_F(FontLanguageTest, getStringTest) { EXPECT_EQ("und-Zsye", createFontLanguage("Und-ZSYE").getString()); EXPECT_EQ("und-Zsye", createFontLanguage("Und-zsye").getString()); - EXPECT_EQ("de-Latn", createFontLanguage("de-1901").getString()); + EXPECT_EQ("de-Latn-DE", createFontLanguage("de-1901").getString()); + + EXPECT_EQ("es-Latn-419", createFontLanguage("es-Latn-419").getString()); + + // Emoji subtag is dropped from getString(). + EXPECT_EQ("es-Latn-419", createFontLanguage("es-419-u-em-emoji").getString()); + EXPECT_EQ("es-Latn-419", createFontLanguage("es-Latn-419-u-em-emoji").getString()); // This is not a necessary desired behavior, just known behavior. - EXPECT_EQ("en-Latn", createFontLanguage("und-Abcdefgh").getString()); + EXPECT_EQ("en-Latn-US", createFontLanguage("und-Abcdefgh").getString()); +} + +TEST_F(FontLanguageTest, testReconstruction) { + EXPECT_EQ("en", createFontLanguageWithoutICUSanitization("en").getString()); + EXPECT_EQ("fil", createFontLanguageWithoutICUSanitization("fil").getString()); + EXPECT_EQ("und", createFontLanguageWithoutICUSanitization("und").getString()); + + EXPECT_EQ("en-Latn", createFontLanguageWithoutICUSanitization("en-Latn").getString()); + EXPECT_EQ("fil-Taga", createFontLanguageWithoutICUSanitization("fil-Taga").getString()); + EXPECT_EQ("und-Zsye", createFontLanguageWithoutICUSanitization("und-Zsye").getString()); + + EXPECT_EQ("en-US", createFontLanguageWithoutICUSanitization("en-US").getString()); + EXPECT_EQ("fil-PH", createFontLanguageWithoutICUSanitization("fil-PH").getString()); + EXPECT_EQ("es-419", createFontLanguageWithoutICUSanitization("es-419").getString()); + + EXPECT_EQ("en-Latn-US", createFontLanguageWithoutICUSanitization("en-Latn-US").getString()); + EXPECT_EQ("fil-Taga-PH", createFontLanguageWithoutICUSanitization("fil-Taga-PH").getString()); + EXPECT_EQ("es-Latn-419", createFontLanguageWithoutICUSanitization("es-Latn-419").getString()); + + // Possible minimum/maximum values. + EXPECT_EQ("aa", createFontLanguageWithoutICUSanitization("aa").getString()); + EXPECT_EQ("zz", createFontLanguageWithoutICUSanitization("zz").getString()); + EXPECT_EQ("aa-Aaaa", createFontLanguageWithoutICUSanitization("aa-Aaaa").getString()); + EXPECT_EQ("zz-Zzzz", createFontLanguageWithoutICUSanitization("zz-Zzzz").getString()); + EXPECT_EQ("aaa-Aaaa-AA", createFontLanguageWithoutICUSanitization("aaa-Aaaa-AA").getString()); + EXPECT_EQ("zzz-Zzzz-ZZ", createFontLanguageWithoutICUSanitization("zzz-Zzzz-ZZ").getString()); + EXPECT_EQ("aaa-Aaaa-000", createFontLanguageWithoutICUSanitization("aaa-Aaaa-000").getString()); + EXPECT_EQ("zzz-Zzzz-999", createFontLanguageWithoutICUSanitization("zzz-Zzzz-999").getString()); } TEST_F(FontLanguageTest, ScriptEqualTest) { @@ -247,6 +291,7 @@ TEST_F(FontLanguagesTest, unsupportedLanguageTests) { TEST_F(FontLanguagesTest, repeatedLanguageTests) { FontLanguage english = createFontLanguage("en"); FontLanguage french = createFontLanguage("fr"); + FontLanguage canadianFrench = createFontLanguage("fr-CA"); FontLanguage englishInLatn = createFontLanguage("en-Latn"); ASSERT_TRUE(english == englishInLatn); @@ -254,11 +299,16 @@ TEST_F(FontLanguagesTest, repeatedLanguageTests) { EXPECT_EQ(1u, langs.size()); EXPECT_EQ(english, langs[0]); - // Country codes are ignored. - const FontLanguages& fr = createFontLanguages("fr,fr-CA,fr-FR"); + const FontLanguages& fr = createFontLanguages("fr,fr-FR,fr-Latn-FR"); EXPECT_EQ(1u, fr.size()); EXPECT_EQ(french, fr[0]); + // ICU appends FR to fr. The third language is dropped which is same as the first language. + const FontLanguages& fr2 = createFontLanguages("fr,fr-CA,fr-FR"); + EXPECT_EQ(2u, fr2.size()); + EXPECT_EQ(french, fr2[0]); + EXPECT_EQ(canadianFrench, fr2[1]); + // The order should be kept. const FontLanguages& langs2 = createFontLanguages("en,fr,en-Latn"); EXPECT_EQ(2u, langs2.size()); @@ -266,6 +316,17 @@ TEST_F(FontLanguagesTest, repeatedLanguageTests) { EXPECT_EQ(french, langs2[1]); } +TEST_F(FontLanguagesTest, identifierTest) { + EXPECT_EQ(createFontLanguage("en-Latn-US"), createFontLanguage("en-Latn-US")); + EXPECT_EQ(createFontLanguage("zh-Hans-CN"), createFontLanguage("zh-Hans-CN")); + EXPECT_EQ(createFontLanguage("en-Zsye-US"), createFontLanguage("en-Zsye-US")); + + EXPECT_NE(createFontLanguage("en-Latn-US"), createFontLanguage("en-Latn-GB")); + EXPECT_NE(createFontLanguage("en-Latn-US"), createFontLanguage("en-Zsye-US")); + EXPECT_NE(createFontLanguage("es-Latn-US"), createFontLanguage("en-Latn-US")); + EXPECT_NE(createFontLanguage("zh-Hant-HK"), createFontLanguage("zh-Hant-TW")); +} + TEST_F(FontLanguagesTest, undEmojiTests) { FontLanguage emoji = createFontLanguage("und-Zsye"); EXPECT_EQ(FontLanguage::EMSTYLE_EMOJI, emoji.getEmojiStyle()); @@ -299,9 +360,11 @@ TEST_F(FontLanguagesTest, subtagEmojiTest) { // Strings that contain the county. "und-US-u-em-emoji", "en-US-u-em-emoji", + "es-419-u-em-emoji", "und-Latn-US-u-em-emoji", "en-Zsym-US-u-em-emoji", "en-Zsye-US-u-em-emoji", + "es-Zsye-419-u-em-emoji", }; for (auto subtagEmojiString : subtagEmojiStrings) { @@ -331,9 +394,11 @@ TEST_F(FontLanguagesTest, subtagTextTest) { // Strings that contain the county. "und-US-u-em-text", "en-US-u-em-text", + "es-419-u-em-text", "und-Latn-US-u-em-text", "en-Zsym-US-u-em-text", "en-Zsye-US-u-em-text", + "es-Zsye-419-u-em-text", }; for (auto subtagTextString : subtagTextStrings) { @@ -363,8 +428,10 @@ TEST_F(FontLanguagesTest, subtagDefaultTest) { // Strings that contain the county. "en-US-u-em-default", "en-Latn-US-u-em-default", + "es-Latn-419-u-em-default", "en-Zsym-US-u-em-default", "en-Zsye-US-u-em-default", + "es-Zsye-419-u-em-default", }; for (auto subtagDefaultString : subtagDefaultStrings) {