From f20a2ef2715ef026763e7b639788ccdad4ed5dc4 Mon Sep 17 00:00:00 2001 From: Yirui Huang Date: Thu, 15 Sep 2016 18:04:34 +0900 Subject: [PATCH] Change language score calculation Change language score calculation in the calculation of the font family. Instead of language and script matching, a match in subtag is added. In addition, a match in subtag has a higher priority than a match in script. The score levels are divided into 5 score levels and the limit of the number of font languages is changed to 12 from 17. Multiple languages selection rule could to be added in the future. Bug: 31608997 Test: Done by unittests. Change-Id: I1e7177095f604fd1794bc99ca36c705dcb4c56e7 --- .../flutter/libs/minikin/FontCollection.cpp | 20 ++++++----- .../src/flutter/libs/minikin/FontLanguage.cpp | 35 +++++++++++++------ .../src/flutter/libs/minikin/FontLanguage.h | 4 +-- .../unittest/FontCollectionItemizeTest.cpp | 14 ++++++++ 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/engine/src/flutter/libs/minikin/FontCollection.cpp b/engine/src/flutter/libs/minikin/FontCollection.cpp index 5bc3634e82..291833c553 100644 --- a/engine/src/flutter/libs/minikin/FontCollection.cpp +++ b/engine/src/flutter/libs/minikin/FontCollection.cpp @@ -234,22 +234,24 @@ uint32_t FontCollection::calcCoverageScore(uint32_t ch, uint32_t vs, FontFamily* return 1; } -// Calculates font scores based on the script matching and primary langauge matching. +// Calculate font scores based on the script matching, subtag matching and primary langauge matching. // -// If the font's script doesn't support the requested script, the font gets a score of 0. If the -// font's script supports the requested script and the font has the same primary language as the -// requested one, the font gets a score of 2. If the font's script supports the requested script -// but the primary language is different from the requested one, the font gets a score of 1. +// 1. If only the font's language matches or there is no matches between requested font and +// supported font, then the font obtains a score of 0. +// 2. Without a match in language, considering subtag may change font's EmojiStyle over script, +// a match in subtag gets a score of 2 and a match in scripts gains a score of 1. +// 3. Regarding to two elements matchings, language-and-subtag matching has a score of 4, while +// language-and-script obtains a socre of 3 with the same reason above. // // If two languages in the requested list have the same language score, the font matching with // higher priority language gets a higher score. For example, in the case the user requested // language list is "ja-Jpan,en-Latn". The score of for the font of "ja-Jpan" gets a higher score // than the font of "en-Latn". // -// To achieve the above two conditions, the language score is determined as follows: -// LanguageScore = s(0) * 3^(m - 1) + s(1) * 3^(m - 2) + ... + s(m - 2) * 3 + s(m - 1) +// To achieve score calculation with priorities, the language score is determined as follows: +// LanguageScore = s(0) * 5^(m - 1) + s(1) * 5^(m - 2) + ... + s(m - 2) * 5 + s(m - 1) // Here, m is the maximum number of languages to be compared, and s(i) is the i-th language's -// matching score. The possible values of s(i) are 0, 1 and 2. +// matching score. The possible values of s(i) are 0, 1, 2, 3 and 4. uint32_t FontCollection::calcLanguageMatchingScore( uint32_t userLangListId, const FontFamily& fontFamily) { const FontLanguages& langList = FontLanguageListCache::getById(userLangListId); @@ -258,7 +260,7 @@ uint32_t FontCollection::calcLanguageMatchingScore( const size_t maxCompareNum = std::min(langList.size(), FONT_LANGUAGES_LIMIT); uint32_t score = 0; for (size_t i = 0; i < maxCompareNum; ++i) { - score = score * 3u + langList[i].calcScoreFor(fontLanguages); + score = score * 5u + langList[i].calcScoreFor(fontLanguages); } return score; } diff --git a/engine/src/flutter/libs/minikin/FontLanguage.cpp b/engine/src/flutter/libs/minikin/FontLanguage.cpp index fca4cdbfcd..c6ddda5ae9 100644 --- a/engine/src/flutter/libs/minikin/FontLanguage.cpp +++ b/engine/src/flutter/libs/minikin/FontLanguage.cpp @@ -176,28 +176,41 @@ bool FontLanguage::supportsHbScript(hb_script_t script) const { } int FontLanguage::calcScoreFor(const FontLanguages& supported) const { - int score = 0; + bool languageScriptMatch = false; + bool subtagMatch = false; + bool scriptMatch = false; + for (size_t i = 0; i < supported.size(); ++i) { + if (mEmojiStyle != EMSTYLE_EMPTY && + mEmojiStyle == supported[i].mEmojiStyle) { + subtagMatch = true; + if (mLanguage == supported[i].mLanguage) { + return 4; + } + } if (isEqualScript(supported[i]) || supportsScript(supported[i].mSubScriptBits, mSubScriptBits)) { + scriptMatch = true; if (mLanguage == supported[i].mLanguage) { - return 2; - } else { - score = 1; + languageScriptMatch = true; } } } - if (score == 1) { - return score; - } - if (supportsScript(supported.getUnionOfSubScriptBits(), mSubScriptBits)) { - // Gives score of 2 only if the language matches all of the font languages except for the - // exact match case handled above. - return (mLanguage == supported[0].mLanguage && supported.isAllTheSameLanguage()) ? 2 : 1; + scriptMatch = true; + if (mLanguage == supported[0].mLanguage && supported.isAllTheSameLanguage()) { + return 3; + } } + if (languageScriptMatch) { + return 3; + } else if (subtagMatch) { + return 2; + } else if (scriptMatch) { + return 1; + } return 0; } diff --git a/engine/src/flutter/libs/minikin/FontLanguage.h b/engine/src/flutter/libs/minikin/FontLanguage.h index 25a5cc3dfa..1bebdfeeb9 100644 --- a/engine/src/flutter/libs/minikin/FontLanguage.h +++ b/engine/src/flutter/libs/minikin/FontLanguage.h @@ -24,9 +24,9 @@ namespace minikin { -// Due to the limits in font fallback score calculation, we can't use anything more than 17 +// Due to the limits in font fallback score calculation, we can't use anything more than 12 // languages. -const size_t FONT_LANGUAGES_LIMIT = 17; +const size_t FONT_LANGUAGES_LIMIT = 12; class FontLanguages; // FontLanguage is a compact representation of a BCP 47 language tag. It diff --git a/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp b/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp index 02bc03bbe6..52786d0c7e 100644 --- a/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp +++ b/engine/src/flutter/tests/unittest/FontCollectionItemizeTest.cpp @@ -780,6 +780,20 @@ TEST_F(FontCollectionItemizeTest, itemize_LanguageScore) { // Language match with unified subscript bits. { "zh-Hanb", { "zh-Hant", "zh-Bopo", "ja-Hant,ja-Bopo", "zh-Hant,zh-Bopo"}, 3 }, { "zh-Hanb", { "zh-Hant", "zh-Bopo", "ja-Hant,zh-Bopo", "zh-Hant,zh-Bopo"}, 3 }, + + // Two elements subtag matching: language and subtag or language or script. + { "ja-Kana-u-em-emoji", { "zh-Hant", "ja-Kana"}, 1 }, + { "ja-Kana-u-em-emoji", { "zh-Hant", "ja-Kana", "ja-Zsye"}, 2 }, + { "ja-Zsym-u-em-emoji", { "ja-Kana", "ja-Zsym", "ja-Zsye"}, 2 }, + + // One element subtag matching: subtag only or script only. + { "en-Latn-u-em-emoji", { "ja-Latn", "ja-Zsye"}, 1 }, + { "en-Zsym-u-em-emoji", { "ja-Zsym", "ja-Zsye"}, 1 }, + { "en-Zsye-u-em-text", { "ja-Zsym", "ja-Zsye"}, 0 }, + + // Multiple languages list with subtags. + { "en-Latn,ja-Jpan-u-em-text", { "en-Latn", "en-Zsye", "en-Zsym"}, 0 }, + { "en-Latn,en-Zsye,ja-Jpan-u-em-text", { "zh", "en-Zsye", "en-Zsym"}, 1 }, }; for (auto testCase : testCases) {