diff --git a/engine/src/flutter/include/minikin/FontCollection.h b/engine/src/flutter/include/minikin/FontCollection.h index 09c9356e09..cd14261640 100644 --- a/engine/src/flutter/include/minikin/FontCollection.h +++ b/engine/src/flutter/include/minikin/FontCollection.h @@ -65,7 +65,7 @@ private: size_t end; }; - FontFamily* getFamilyForChar(uint32_t ch, uint32_t vs, FontLanguage lang, int variant) const; + FontFamily* getFamilyForChar(uint32_t ch, uint32_t vs, uint32_t langListId, int variant) const; // static for allocating unique id's static uint32_t sNextId; diff --git a/engine/src/flutter/include/minikin/FontFamily.h b/engine/src/flutter/include/minikin/FontFamily.h index 776bcc41be..539a06cef0 100644 --- a/engine/src/flutter/include/minikin/FontFamily.h +++ b/engine/src/flutter/include/minikin/FontFamily.h @@ -87,35 +87,42 @@ private: }; // FontStyle represents all style information needed to select an actual font -// from a collection. The implementation is packed into a single 32-bit word +// from a collection. The implementation is packed into two 32-bit words // so it can be efficiently copied, embedded in other objects, etc. class FontStyle { public: - FontStyle(int weight = 4, bool italic = false) { - bits = (weight & kWeightMask) | (italic ? kItalicMask : 0); - } - FontStyle(FontLanguage lang, int variant = 0, int weight = 4, bool italic = false) { - bits = (weight & kWeightMask) | (italic ? kItalicMask : 0) - | (variant << kVariantShift) | (lang.bits() << kLangShift); - } - FontStyle(FontLanguages langs, int variant = 0, int weight = 4, bool italic = false) : - // TODO: Use all the languages in langs - FontStyle(langs[0], variant, weight, italic) { } + FontStyle() : FontStyle(0 /* variant */, 4 /* weight */, false /* italic */) {} + FontStyle(int weight, bool italic) : FontStyle(0 /* variant */, weight, italic) {} + FontStyle(uint32_t langListId) + : FontStyle(langListId, 0 /* variant */, 4 /* weight */, false /* italic */) {} + + FontStyle(int variant, int weight, bool italic); + FontStyle(uint32_t langListId, int variant, int weight, bool italic); + int getWeight() const { return bits & kWeightMask; } bool getItalic() const { return (bits & kItalicMask) != 0; } int getVariant() const { return (bits >> kVariantShift) & kVariantMask; } - FontLanguage getLanguage() const { return FontLanguage(bits >> kLangShift); } + uint32_t getLanguageListId() const { return mLanguageListId; } - bool operator==(const FontStyle other) const { return bits == other.bits; } + bool operator==(const FontStyle other) const { + return bits == other.bits && mLanguageListId == other.mLanguageListId; + } - hash_t hash() const { return bits; } + hash_t hash() const; + + // Looks up a language list from an internal cache and returns its ID. + // If the passed language list is not in the cache, registers it and returns newly assigned ID. + static uint32_t registerLanguageList(const std::string& languages); private: static const uint32_t kWeightMask = (1 << 4) - 1; static const uint32_t kItalicMask = 1 << 4; static const int kVariantShift = 5; static const uint32_t kVariantMask = (1 << 2) - 1; - static const int kLangShift = 7; + + static uint32_t pack(int variant, int weight, bool italic); + uint32_t bits; + uint32_t mLanguageListId; }; enum FontVariant { diff --git a/engine/src/flutter/libs/minikin/Android.mk b/engine/src/flutter/libs/minikin/Android.mk index c728b09b84..4c945a60c9 100644 --- a/engine/src/flutter/libs/minikin/Android.mk +++ b/engine/src/flutter/libs/minikin/Android.mk @@ -21,6 +21,7 @@ minikin_src_files := \ CmapCoverage.cpp \ FontCollection.cpp \ FontFamily.cpp \ + FontLanguageListCache.cpp \ GraphemeBreak.cpp \ HbFaceCache.cpp \ Hyphenator.cpp \ diff --git a/engine/src/flutter/libs/minikin/FontCollection.cpp b/engine/src/flutter/libs/minikin/FontCollection.cpp index d089e8b0d9..6201f7492f 100644 --- a/engine/src/flutter/libs/minikin/FontCollection.cpp +++ b/engine/src/flutter/libs/minikin/FontCollection.cpp @@ -22,6 +22,7 @@ #include "unicode/unistr.h" #include "unicode/unorm2.h" +#include "FontLanguageListCache.h" #include "MinikinInternal.h" #include @@ -116,11 +117,15 @@ FontCollection::~FontCollection() { // font, but not the whole variation sequence. // 8. Highest score wins, with ties resolved to the first font. FontFamily* FontCollection::getFamilyForChar(uint32_t ch, uint32_t vs, - FontLanguage lang, int variant) const { + uint32_t langListId, int variant) const { if (ch >= mMaxChar) { return NULL; } + const FontLanguages& langList = FontLanguageListCache::getById(langListId); + // TODO: use all languages in langList. + const FontLanguage lang = (langList.size() == 0) ? FontLanguage() : langList[0]; + // Even if the font supports variation sequence, mRanges isn't aware of the base character of // the sequence. Search all FontFamilies if variation sequence is specified. // TODO: Always use mRanges for font search. @@ -162,6 +167,12 @@ FontFamily* FontCollection::getFamilyForChar(uint32_t ch, uint32_t vs, } } } + if (bestFamily == nullptr && vs != 0) { + // If no fonts support the codepoint and variation selector pair, + // fallback to select a font family that supports just the base + // character, ignoring the variation selector. + return getFamilyForChar(ch, 0, langListId, variant); + } if (bestFamily == nullptr && !mFamilyVec.empty()) { UErrorCode errorCode = U_ZERO_ERROR; const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode); @@ -171,7 +182,7 @@ FontFamily* FontCollection::getFamilyForChar(uint32_t ch, uint32_t vs, if (U_SUCCESS(errorCode) && len > 0) { int off = 0; U16_NEXT_UNSAFE(decomposed, off, ch); - return getFamilyForChar(ch, vs, lang, variant); + return getFamilyForChar(ch, vs, langListId, variant); } } bestFamily = mFamilies[0]; @@ -222,7 +233,7 @@ bool FontCollection::hasVariationSelector(uint32_t baseCodepoint, void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style, vector* result) const { - FontLanguage lang = style.getLanguage(); + const uint32_t langListId = style.getLanguageListId(); int variant = style.getVariant(); FontFamily* lastFamily = NULL; Run* run = NULL; @@ -261,8 +272,8 @@ void FontCollection::itemize(const uint16_t *string, size_t string_size, FontSty } if (!shouldContinueRun) { - FontFamily* family = - getFamilyForChar(ch, isVariationSelector(nextCh) ? nextCh : 0, lang, variant); + FontFamily* family = getFamilyForChar(ch, isVariationSelector(nextCh) ? nextCh : 0, + langListId, variant); if (utf16Pos == 0 || family != lastFamily) { size_t start = utf16Pos; // Workaround for Emoji keycap until we implement per-cluster font diff --git a/engine/src/flutter/libs/minikin/FontFamily.cpp b/engine/src/flutter/libs/minikin/FontFamily.cpp index 5adff57b6e..5a3952437a 100644 --- a/engine/src/flutter/libs/minikin/FontFamily.cpp +++ b/engine/src/flutter/libs/minikin/FontFamily.cpp @@ -26,6 +26,9 @@ #include #include +#include + +#include "FontLanguageListCache.h" #include "HbFaceCache.h" #include "MinikinInternal.h" #include @@ -159,6 +162,31 @@ FontLanguages::FontLanguages(const char* buf, size_t size) { } } +FontStyle::FontStyle(int variant, int weight, bool italic) + : FontStyle(FontLanguageListCache::kEmptyListId, variant, weight, italic) { +} + +FontStyle::FontStyle(uint32_t languageListId, int variant, int weight, bool italic) + : bits(pack(variant, weight, italic)), mLanguageListId(languageListId) { +} + +hash_t FontStyle::hash() const { + uint32_t hash = JenkinsHashMix(0, bits); + hash = JenkinsHashMix(hash, mLanguageListId); + return JenkinsHashWhiten(hash); +} + +// static +uint32_t FontStyle::registerLanguageList(const std::string& languages) { + AutoMutex _l(gMinikinLock); + return FontLanguageListCache::getId(languages); +} + +// static +uint32_t FontStyle::pack(int variant, int weight, bool italic) { + return (weight & kWeightMask) | (italic ? kItalicMask : 0) | (variant << kVariantShift); +} + FontFamily::~FontFamily() { for (size_t i = 0; i < mFonts.size(); i++) { mFonts[i].typeface->UnrefLocked(); diff --git a/engine/src/flutter/libs/minikin/FontLanguageListCache.cpp b/engine/src/flutter/libs/minikin/FontLanguageListCache.cpp new file mode 100644 index 0000000000..e1c2343dda --- /dev/null +++ b/engine/src/flutter/libs/minikin/FontLanguageListCache.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "Minikin" + +#include "FontLanguageListCache.h" + +#include + +#include "MinikinInternal.h" + +namespace android { + +const uint32_t FontLanguageListCache::kEmptyListId; + +// static +uint32_t FontLanguageListCache::getId(const std::string& languages) { + FontLanguageListCache* inst = FontLanguageListCache::getInstance(); + std::unordered_map::const_iterator it = + inst->mLanguageListLookupTable.find(languages); + if (it != inst->mLanguageListLookupTable.end()) { + return it->second; + } + + // Given language list is not in cache. Insert it and return newly assigned ID. + const uint32_t nextId = inst->mLanguageLists.size(); + inst->mLanguageLists.push_back(FontLanguages(languages.c_str(), languages.size())); + inst->mLanguageListLookupTable.insert(std::make_pair(languages, nextId)); + return nextId; +} + +// static +const FontLanguages& FontLanguageListCache::getById(uint32_t id) { + FontLanguageListCache* inst = FontLanguageListCache::getInstance(); + LOG_ALWAYS_FATAL_IF(id >= inst->mLanguageLists.size(), "Lookup by unknown language list ID."); + return inst->mLanguageLists[id]; +} + +// static +FontLanguageListCache* FontLanguageListCache::getInstance() { + assertMinikinLocked(); + static FontLanguageListCache* instance = nullptr; + if (instance == nullptr) { + instance = new FontLanguageListCache(); + + // Insert an empty language list for mapping empty language list to kEmptyListId. + instance->mLanguageLists.push_back(FontLanguages()); + instance->mLanguageListLookupTable.insert(std::make_pair("", kEmptyListId)); + } + return instance; +} + +} // namespace android diff --git a/engine/src/flutter/libs/minikin/FontLanguageListCache.h b/engine/src/flutter/libs/minikin/FontLanguageListCache.h new file mode 100644 index 0000000000..7d627b562e --- /dev/null +++ b/engine/src/flutter/libs/minikin/FontLanguageListCache.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINIKIN_FONT_LANGUAGE_LIST_CACHE_H +#define MINIKIN_FONT_LANGUAGE_LIST_CACHE_H + +#include + +#include + +namespace android { + +class FontLanguageListCache { +public: + // A special ID for the empty language list. + // This value must be 0 since the empty language list is inserted into mLanguageLists by + // default. + const static uint32_t kEmptyListId = 0; + + // Returns language list ID for the given string representation of FontLanguages. + // Caller should acquire a lock before calling the method. + static uint32_t getId(const std::string& languages); + + // Caller should acquire a lock before calling the method. + static const FontLanguages& getById(uint32_t id); + +private: + FontLanguageListCache() {} // Singleton + ~FontLanguageListCache() {} + + // Caller should acquire a lock before calling the method. + static FontLanguageListCache* getInstance(); + + std::vector mLanguageLists; + + // A map from string representation of the font language list to the ID. + std::unordered_map mLanguageListLookupTable; +}; + +} // namespace android + +#endif // MINIKIN_FONT_LANGUAGE_LIST_CACHE_H diff --git a/engine/src/flutter/libs/minikin/Layout.cpp b/engine/src/flutter/libs/minikin/Layout.cpp index 3b140faa2b..af5e6fe75a 100644 --- a/engine/src/flutter/libs/minikin/Layout.cpp +++ b/engine/src/flutter/libs/minikin/Layout.cpp @@ -34,6 +34,7 @@ #include #include +#include "FontLanguageListCache.h" #include "LayoutUtils.h" #include "HbFaceCache.h" #include "MinikinInternal.h" @@ -742,9 +743,11 @@ void Layout::doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t hb_buffer_clear_contents(buffer); hb_buffer_set_script(buffer, script); hb_buffer_set_direction(buffer, isRtl? HB_DIRECTION_RTL : HB_DIRECTION_LTR); - FontLanguage language = ctx->style.getLanguage(); - if (language) { - string lang = language.getString(); + const FontLanguages& langList = + FontLanguageListCache::getById(ctx->style.getLanguageListId()); + if (langList.size() != 0) { + // TODO: use all languages in langList. + string lang = langList[0].getString(); hb_buffer_set_language(buffer, hb_language_from_string(lang.c_str(), -1)); } hb_buffer_add_utf16(buffer, buf, bufSize, srunstart + start, srunend - srunstart); diff --git a/engine/src/flutter/tests/Android.mk b/engine/src/flutter/tests/Android.mk index d12dca72e3..2eb2abe2a2 100644 --- a/engine/src/flutter/tests/Android.mk +++ b/engine/src/flutter/tests/Android.mk @@ -42,6 +42,7 @@ LOCAL_SRC_FILES += \ FontCollectionTest.cpp \ FontCollectionItemizeTest.cpp \ FontFamilyTest.cpp \ + FontLanguageListCacheTest.cpp \ FontTestUtils.cpp \ MinikinFontForTest.cpp \ GraphemeBreakTests.cpp \ diff --git a/engine/src/flutter/tests/FontCollectionItemizeTest.cpp b/engine/src/flutter/tests/FontCollectionItemizeTest.cpp index a7da4269a6..d0dc39c974 100644 --- a/engine/src/flutter/tests/FontCollectionItemizeTest.cpp +++ b/engine/src/flutter/tests/FontCollectionItemizeTest.cpp @@ -197,9 +197,9 @@ TEST(FontCollectionItemizeTest, itemize_non_latin) { std::unique_ptr collection = getFontCollection(kTestFontDir, kItemizeFontXml); std::vector runs; - FontStyle kJAStyle = FontStyle(FontLanguage("ja_JP", 5)); - FontStyle kUSStyle = FontStyle(FontLanguage("en_US", 5)); - FontStyle kZH_HansStyle = FontStyle(FontLanguage("zh_Hans", 7)); + FontStyle kJAStyle = FontStyle(FontStyle::registerLanguageList("ja_JP")); + FontStyle kUSStyle = FontStyle(FontStyle::registerLanguageList("en_US")); + FontStyle kZH_HansStyle = FontStyle(FontStyle::registerLanguageList("zh_Hans")); // All Japanese Hiragana characters. itemize(collection.get(), "U+3042 U+3044 U+3046 U+3048 U+304A", kUSStyle, &runs); @@ -275,7 +275,7 @@ TEST(FontCollectionItemizeTest, itemize_mixed) { std::unique_ptr collection = getFontCollection(kTestFontDir, kItemizeFontXml); std::vector runs; - FontStyle kUSStyle = FontStyle(FontLanguage("en_US", 5)); + FontStyle kUSStyle = FontStyle(FontStyle::registerLanguageList("en_US")); itemize(collection.get(), "'a' U+4F60 'b' U+4F60 'c'", kUSStyle, &runs); ASSERT_EQ(5U, runs.size()); @@ -318,8 +318,8 @@ TEST(FontCollectionItemizeTest, itemize_variationSelector) { // Chinese font. Also a glyph for U+242EE is provided by both Japanese and // Traditional Chinese font. To avoid effects of device default locale, // explicitly specify the locale. - FontStyle kZH_HansStyle = FontStyle(FontLanguage("zh_Hans", 7)); - FontStyle kZH_HantStyle = FontStyle(FontLanguage("zh_Hant", 7)); + FontStyle kZH_HansStyle = FontStyle(FontStyle::registerLanguageList("zh_Hans")); + FontStyle kZH_HantStyle = FontStyle(FontStyle::registerLanguageList("zh_Hant")); // U+4FAE is available in both zh_Hans and ja font, but U+4FAE,U+FE00 is // only available in ja font. @@ -457,8 +457,8 @@ TEST(FontCollectionItemizeTest, itemize_variationSelectorSupplement) { // Chinese font. Also a glyph for U+242EE is provided by both Japanese and // Traditional Chinese font. To avoid effects of device default locale, // explicitly specify the locale. - FontStyle kZH_HansStyle = FontStyle(FontLanguage("zh_Hans", 7)); - FontStyle kZH_HantStyle = FontStyle(FontLanguage("zh_Hant", 7)); + FontStyle kZH_HansStyle = FontStyle(FontStyle::registerLanguageList("zh_Hans")); + FontStyle kZH_HantStyle = FontStyle(FontStyle::registerLanguageList("zh_Hant")); // U+845B is available in both zh_Hans and ja font, but U+845B,U+E0100 is // only available in ja font. @@ -602,9 +602,10 @@ TEST(FontCollectionItemizeTest, itemize_fakery) { std::unique_ptr collection = getFontCollection(kTestFontDir, kItemizeFontXml); std::vector runs; - FontStyle kJABoldStyle = FontStyle(FontLanguage("ja_JP", 5), 0, 7, false); - FontStyle kJAItalicStyle = FontStyle(FontLanguage("ja_JP", 5), 0, 5, true); - FontStyle kJABoldItalicStyle = FontStyle(FontLanguage("ja_JP", 5), 0, 7, true); + FontStyle kJABoldStyle = FontStyle(FontStyle::registerLanguageList("ja_JP"), 0, 7, false); + FontStyle kJAItalicStyle = FontStyle(FontStyle::registerLanguageList("ja_JP"), 0, 5, true); + FontStyle kJABoldItalicStyle = + FontStyle(FontStyle::registerLanguageList("ja_JP"), 0, 7, true); // Currently there is no italic or bold font for Japanese. FontFakery has // the differences between desired and actual font style. diff --git a/engine/src/flutter/tests/FontFamilyTest.cpp b/engine/src/flutter/tests/FontFamilyTest.cpp index d46245cd1f..fa8302c60c 100644 --- a/engine/src/flutter/tests/FontFamilyTest.cpp +++ b/engine/src/flutter/tests/FontFamilyTest.cpp @@ -17,6 +17,7 @@ #include #include +#include "FontLanguageListCache.h" #include "MinikinFontForTest.h" #include "MinikinInternal.h" @@ -83,6 +84,27 @@ TEST(FontLanguagesTest, undEmojiTests) { EXPECT_FALSE(emoji == undExample); } +TEST(FontLanguagesTest, registerLanguageListTest) { + EXPECT_EQ(0UL, FontStyle::registerLanguageList("")); + EXPECT_NE(0UL, FontStyle::registerLanguageList("en")); + EXPECT_NE(0UL, FontStyle::registerLanguageList("jp")); + EXPECT_NE(0UL, FontStyle::registerLanguageList("en,zh-Hans")); + + EXPECT_EQ(FontStyle::registerLanguageList("en"), FontStyle::registerLanguageList("en")); + EXPECT_NE(FontStyle::registerLanguageList("en"), FontStyle::registerLanguageList("jp")); + + EXPECT_EQ(FontStyle::registerLanguageList("en,zh-Hans"), + FontStyle::registerLanguageList("en,zh-Hans")); + EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"), + FontStyle::registerLanguageList("zh-Hans,en")); + EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"), + FontStyle::registerLanguageList("jp")); + EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"), + FontStyle::registerLanguageList("en")); + EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"), + FontStyle::registerLanguageList("en,zh-Hant")); +} + // The test font has following glyphs. // U+82A6 // U+82A6 U+FE00 (VS1) diff --git a/engine/src/flutter/tests/FontLanguageListCacheTest.cpp b/engine/src/flutter/tests/FontLanguageListCacheTest.cpp new file mode 100644 index 0000000000..29757fedce --- /dev/null +++ b/engine/src/flutter/tests/FontLanguageListCacheTest.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include "FontLanguageListCache.h" + +namespace android { + +TEST(FontLanguageListCacheTest, getId) { + EXPECT_EQ(0UL, FontLanguageListCache::getId("")); + EXPECT_NE(0UL, FontStyle::registerLanguageList("en")); + EXPECT_NE(0UL, FontStyle::registerLanguageList("jp")); + EXPECT_NE(0UL, FontStyle::registerLanguageList("en,zh-Hans")); + + EXPECT_EQ(FontLanguageListCache::getId("en"), FontLanguageListCache::getId("en")); + EXPECT_NE(FontLanguageListCache::getId("en"), FontLanguageListCache::getId("jp")); + + EXPECT_EQ(FontLanguageListCache::getId("en,zh-Hans"), + FontLanguageListCache::getId("en,zh-Hans")); + EXPECT_NE(FontLanguageListCache::getId("en,zh-Hans"), + FontLanguageListCache::getId("zh-Hans,en")); + EXPECT_NE(FontLanguageListCache::getId("en,zh-Hans"), + FontLanguageListCache::getId("jp")); + EXPECT_NE(FontLanguageListCache::getId("en,zh-Hans"), + FontLanguageListCache::getId("en")); + EXPECT_NE(FontLanguageListCache::getId("en,zh-Hans"), + FontLanguageListCache::getId("en,zh-Hant")); +} + +TEST(FontLanguageListCacheTest, getById) { + FontLanguage english("en", 2); + FontLanguage japanese("jp", 2); + + EXPECT_EQ(0UL, FontLanguageListCache::getById(0).size()); + + FontLanguages langs = FontLanguageListCache::getById(FontLanguageListCache::getId("en")); + ASSERT_EQ(1UL, langs.size()); + EXPECT_EQ(english, langs[0]); + + langs = FontLanguageListCache::getById(FontLanguageListCache::getId("en,jp")); + ASSERT_EQ(2UL, langs.size()); + EXPECT_EQ(english, langs[0]); + EXPECT_EQ(japanese, langs[1]); +} + +} // android