diff options
author | Seigo Nonaka <nona@google.com> | 2022-10-24 14:42:29 +0900 |
---|---|---|
committer | Seigo Nonaka <nona@google.com> | 2022-12-13 15:12:29 +0000 |
commit | 85797da4c8ad0279e8ab1473c893b8b2a936b898 (patch) | |
tree | a94adcedc9cb8ab66a71d49eacfe58e537057294 | |
parent | b77471dbb53a3d7838eb27063103cfffa3f8b403 (diff) | |
download | minikin-85797da4c8ad0279e8ab1473c893b8b2a936b898.tar.gz |
Add default family flag to the font family
The default family indicates that the default font to be
used for named font families. The named font family is used as a
highest priority family, like developer provided font family.
Before this change, it is determined if the family is located at
the top of the fallback list, it is treated as a default font.
Now, the named font family can be mulitple font families and they
are all needed to be treated as the highest priority font famlies.
To identify the families in the default family list.
Bug: 249787583
Test: atest TypefaceSystemFallbackTest FontListParserTest
Test: atest UpdatableFontDirTest UpdatableSystemFontTest
Test: atest GtsFontHostTestCases FontManagerTest
Test: minikin_tests
Change-Id: If01378628a0ed3f2e02bc0f346f2d05e76bd77bc
-rw-r--r-- | include/minikin/FontCollection.h | 2 | ||||
-rw-r--r-- | include/minikin/FontFamily.h | 7 | ||||
-rw-r--r-- | libs/minikin/FontCollection.cpp | 19 | ||||
-rw-r--r-- | libs/minikin/FontFamily.cpp | 19 | ||||
-rw-r--r-- | tests/unittest/FontCollectionItemizeTest.cpp | 9 | ||||
-rw-r--r-- | tests/util/FontTestUtils.cpp | 7 |
6 files changed, 45 insertions, 18 deletions
diff --git a/include/minikin/FontCollection.h b/include/minikin/FontCollection.h index b231224..1679abc 100644 --- a/include/minikin/FontCollection.h +++ b/include/minikin/FontCollection.h @@ -221,6 +221,8 @@ private: uint32_t calcCoverageScore(uint32_t ch, uint32_t vs, uint32_t localeListId, const std::shared_ptr<FontFamily>& fontFamily) const; + bool isPrimaryFamily(const std::shared_ptr<FontFamily>& fontFamily) const; + static uint32_t calcLocaleMatchingScore(uint32_t userLocaleListId, const FontFamily& fontFamily); diff --git a/include/minikin/FontFamily.h b/include/minikin/FontFamily.h index e2831e4..6161f5c 100644 --- a/include/minikin/FontFamily.h +++ b/include/minikin/FontFamily.h @@ -37,7 +37,7 @@ public: std::vector<std::shared_ptr<Font>>&& fonts); static std::shared_ptr<FontFamily> create(uint32_t localeListId, FamilyVariant variant, std::vector<std::shared_ptr<Font>>&& fonts, - bool isCustomFallback); + bool isCustomFallback, bool isDefaultFallback); FontFamily(FontFamily&&) = default; FontFamily& operator=(FontFamily&&) = default; @@ -60,6 +60,7 @@ public: size_t getSupportedAxesCount() const { return mSupportedAxesCount; } AxisTag getSupportedAxisAt(size_t index) const { return mSupportedAxes[index]; } bool isCustomFallback() const { return mIsCustomFallback; } + bool isDefaultFallback() const { return mIsDefaultFallback; } // Get Unicode coverage. const SparseBitSet& getCoverage() const { return mCoverage; } @@ -78,7 +79,8 @@ public: private: FontFamily(uint32_t localeListId, FamilyVariant variant, - std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback); + std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback, + bool isDefaultFallback); explicit FontFamily(BufferReader* reader, const std::shared_ptr<std::vector<Font>>& fonts); void writeTo(BufferWriter* writer, uint32_t* fontIndex) const; @@ -100,6 +102,7 @@ private: FamilyVariant mVariant; // 1 byte bool mIsColorEmoji; // 1 byte bool mIsCustomFallback; // 1 byte + bool mIsDefaultFallback; // 1 byte MINIKIN_PREVENT_COPY_AND_ASSIGN(FontFamily); }; diff --git a/libs/minikin/FontCollection.cpp b/libs/minikin/FontCollection.cpp index 6b0a762..0c1a173 100644 --- a/libs/minikin/FontCollection.cpp +++ b/libs/minikin/FontCollection.cpp @@ -335,6 +335,22 @@ uint32_t FontCollection::calcFamilyScore(uint32_t ch, uint32_t vs, FamilyVariant return coverageScore << 29 | localeScore << 1 | variantScore; } +// Returns true if +// - the fontFamily is a developer specified custom fallback. +// - no custom fallback is provided and the fontFamily is a default fallback. +bool FontCollection::isPrimaryFamily(const std::shared_ptr<FontFamily>& fontFamily) const { + // If the font family is provided by developers, it is primary. + if (fontFamily->isCustomFallback()) { + return true; + } + + if (getFamilyAt(0)->isCustomFallback()) { + return false; + } else { + return fontFamily->isDefaultFallback(); + } +} + // Calculates a font score based on variation sequence coverage. // - Returns kUnsupportedFontScore if the font doesn't support the variation sequence or its base // character. @@ -353,8 +369,7 @@ uint32_t FontCollection::calcCoverageScore(uint32_t ch, uint32_t vs, uint32_t lo return kUnsupportedFontScore; } - if ((vs == 0 || hasVSGlyph) && - (getFamilyAt(0) == fontFamily || fontFamily->isCustomFallback())) { + if ((vs == 0 || hasVSGlyph) && isPrimaryFamily(fontFamily)) { // If the first font family supports the given character or variation sequence, always use // it. return kFirstFontScore; diff --git a/libs/minikin/FontFamily.cpp b/libs/minikin/FontFamily.cpp index 66e0d51..836674c 100644 --- a/libs/minikin/FontFamily.cpp +++ b/libs/minikin/FontFamily.cpp @@ -43,20 +43,22 @@ std::shared_ptr<FontFamily> FontFamily::create(std::vector<std::shared_ptr<Font> // static std::shared_ptr<FontFamily> FontFamily::create(FamilyVariant variant, std::vector<std::shared_ptr<Font>>&& fonts) { - return create(kEmptyLocaleListId, variant, std::move(fonts), false /* isCustomFallback */); + return create(kEmptyLocaleListId, variant, std::move(fonts), false /* isCustomFallback */, + false /* isDefaultFallback */); } // static std::shared_ptr<FontFamily> FontFamily::create(uint32_t localeListId, FamilyVariant variant, std::vector<std::shared_ptr<Font>>&& fonts, - bool isCustomFallback) { + bool isCustomFallback, bool isDefaultFallback) { // TODO(b/174672300): Revert back to make_shared. - return std::shared_ptr<FontFamily>( - new FontFamily(localeListId, variant, std::move(fonts), isCustomFallback)); + return std::shared_ptr<FontFamily>(new FontFamily(localeListId, variant, std::move(fonts), + isCustomFallback, isDefaultFallback)); } FontFamily::FontFamily(uint32_t localeListId, FamilyVariant variant, - std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback) + std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback, + bool isDefaultFallback) : mFonts(std::make_unique<std::shared_ptr<Font>[]>(fonts.size())), // computeCoverage may update supported axes and coverages later. mSupportedAxes(nullptr), @@ -69,7 +71,8 @@ FontFamily::FontFamily(uint32_t localeListId, FamilyVariant variant, mVariant(variant), mIsColorEmoji(LocaleListCache::getById(localeListId).getEmojiStyle() == EmojiStyle::EMOJI), - mIsCustomFallback(isCustomFallback) { + mIsCustomFallback(isCustomFallback), + mIsDefaultFallback(isDefaultFallback) { MINIKIN_ASSERT(!fonts.empty(), "FontFamily must contain at least one font."); MINIKIN_ASSERT(fonts.size() <= std::numeric_limits<uint32_t>::max(), "Number of fonts must be less than 2^32."); @@ -103,6 +106,7 @@ FontFamily::FontFamily(BufferReader* reader, const std::shared_ptr<std::vector<F } mIsColorEmoji = static_cast<bool>(reader->read<uint8_t>()); mIsCustomFallback = static_cast<bool>(reader->read<uint8_t>()); + mIsDefaultFallback = static_cast<bool>(reader->read<uint8_t>()); mCoverage = SparseBitSet(reader); // Read mCmapFmt14Coverage. As it can have null entries, it is stored in the buffer as a sparse // array (size, non-null entry count, array of (index, entry)). @@ -128,6 +132,7 @@ void FontFamily::writeTo(BufferWriter* writer, uint32_t* fontIndex) const { writer->writeArray<AxisTag>(mSupportedAxes.get(), mSupportedAxesCount); writer->write<uint8_t>(mIsColorEmoji); writer->write<uint8_t>(mIsCustomFallback); + writer->write<uint8_t>(mIsDefaultFallback); mCoverage.writeTo(writer); // Write mCmapFmt14Coverage as a sparse array (size, non-null entry count, // array of (index, entry)) @@ -337,7 +342,7 @@ std::shared_ptr<FontFamily> FontFamily::createFamilyWithVariation( } } - return create(mLocaleListId, mVariant, std::move(fonts), mIsCustomFallback); + return create(mLocaleListId, mVariant, std::move(fonts), mIsCustomFallback, mIsDefaultFallback); } } // namespace minikin diff --git a/tests/unittest/FontCollectionItemizeTest.cpp b/tests/unittest/FontCollectionItemizeTest.cpp index 9cb0c2a..1723519 100644 --- a/tests/unittest/FontCollectionItemizeTest.cpp +++ b/tests/unittest/FontCollectionItemizeTest.cpp @@ -938,7 +938,8 @@ TEST(FontCollectionItemizeTest, itemize_LocaleScore) { std::vector<std::shared_ptr<Font>> fonts; fonts.push_back(Font::Builder(firstFamilyMinikinFont).build()); auto firstFamily = FontFamily::create(registerLocaleList("und"), FamilyVariant::DEFAULT, - std::move(fonts), false /* isCustomFallback */); + std::move(fonts), false /* isCustomFallback */, + false /* isDefaultFallback */); families.push_back(firstFamily); // Prepare font families @@ -951,9 +952,9 @@ TEST(FontCollectionItemizeTest, itemize_LocaleScore) { std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath(kJAFont)); std::vector<std::shared_ptr<Font>> fonts; fonts.push_back(Font::Builder(minikinFont).build()); - auto family = FontFamily::create(registerLocaleList(testCase.fontLocales[i]), - FamilyVariant::DEFAULT, std::move(fonts), - false /* isCustomFallback */); + auto family = FontFamily::create( + registerLocaleList(testCase.fontLocales[i]), FamilyVariant::DEFAULT, + std::move(fonts), false /* isCustomFallback */, false /* isDefaultFallback */); families.push_back(family); fontLocaleIdxMap.insert(std::make_pair(minikinFont.get(), i)); } diff --git a/tests/util/FontTestUtils.cpp b/tests/util/FontTestUtils.cpp index 1f539c3..a1025fe 100644 --- a/tests/util/FontTestUtils.cpp +++ b/tests/util/FontTestUtils.cpp @@ -109,8 +109,9 @@ std::vector<std::shared_ptr<FontFamily>> getFontFamilies(const std::string& font family = FontFamily::create(variant, std::move(fonts)); } else { uint32_t langId = registerLocaleList(std::string((const char*)lang, xmlStrlen(lang))); - family = FontFamily::create(langId, variant, std::move(fonts), - false /* isCustomFallback */); + family = + FontFamily::create(langId, variant, std::move(fonts), + false /* isCustomFallback */, false /* isdefaultFallback */); } families.push_back(family); } @@ -135,7 +136,7 @@ std::shared_ptr<FontFamily> buildFontFamily(const std::string& filePath, const s std::vector<std::shared_ptr<Font>> fonts; fonts.push_back(Font::Builder(font).build()); return FontFamily::create(LocaleListCache::getId(lang), FamilyVariant::DEFAULT, - std::move(fonts), isCustomFallback); + std::move(fonts), isCustomFallback, false /* isDefaultFallback */); } } // namespace minikin |