From ad9cb5909a84f8b8c298d8e0d78e7394fe4803d6 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Thu, 24 Sep 2015 18:41:41 +0900 Subject: [PATCH] Introduce FontFamily::hasVariationSelector This CL introduces new method hasVariationSelector into FontFamily but it is not used in production code. So no behavior changes are expected. This CL contains the following changes: - Introduce hasVariationSelector which returns true if the corresponding font has a glyph for a code point and variation selector pair. - Introduce purgeHbFontCache since hb_face_t won't be released by keeping hb_font_t. - Introduce unit tests with self-built font. Change-Id: I659a6d03d9ec446b409e1fba2758452abb9f44fa --- .../flutter/include/minikin/FontCollection.h | 4 + .../src/flutter/include/minikin/FontFamily.h | 17 ++- .../flutter/libs/minikin/FontCollection.cpp | 7 + .../src/flutter/libs/minikin/FontFamily.cpp | 25 ++++ engine/src/flutter/libs/minikin/Layout.cpp | 2 + engine/src/flutter/tests/Android.mk | 1 + engine/src/flutter/tests/FontFamilyTest.cpp | 122 ++++++++++++++++++ engine/src/flutter/tests/how_to_run.txt | 1 + 8 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 engine/src/flutter/tests/FontFamilyTest.cpp diff --git a/engine/src/flutter/include/minikin/FontCollection.h b/engine/src/flutter/include/minikin/FontCollection.h index ffdb4d1819..c4daf98fa6 100644 --- a/engine/src/flutter/include/minikin/FontCollection.h +++ b/engine/src/flutter/include/minikin/FontCollection.h @@ -47,6 +47,10 @@ public: FakedFont baseFontFaked(FontStyle style); uint32_t getId() const; + + // Calls each managed font family's FontFamily::purgeHbFontCache method. + // Caller should acquire a lock before calling the method. + void purgeFontFamilyHbFontCache() const; private: static const int kLogCharsPerPage = 8; static const int kPageMask = (1 << kLogCharsPerPage) - 1; diff --git a/engine/src/flutter/include/minikin/FontFamily.h b/engine/src/flutter/include/minikin/FontFamily.h index d21a20af71..b1994042e3 100644 --- a/engine/src/flutter/include/minikin/FontFamily.h +++ b/engine/src/flutter/include/minikin/FontFamily.h @@ -19,6 +19,7 @@ #include #include +#include #include @@ -123,9 +124,9 @@ struct FakedFont { class FontFamily : public MinikinRefCounted { public: - FontFamily() { } + FontFamily() : mHbFont(nullptr) { } - FontFamily(FontLanguage lang, int variant) : mLang(lang), mVariant(variant) { + FontFamily(FontLanguage lang, int variant) : mLang(lang), mVariant(variant), mHbFont(nullptr) { } ~FontFamily(); @@ -147,6 +148,16 @@ public: // Get Unicode coverage. Lifetime of returned bitset is same as receiver. May return nullptr on // error. const SparseBitSet* getCoverage(); + + // Returns true if the font has a glyph for the code point and variation selector pair. + // Caller should acquire a lock before calling the method. + bool hasVariationSelector(uint32_t codepoint, uint32_t variationSelector); + + // Purges cached mHbFont. + // hb_font_t keeps a reference to hb_face_t which is managed by HbFaceCache. Thus, + // it is good to purge hb_font_t once it is no longer necessary. + // Caller should acquire a lock before calling the method. + void purgeHbFontCache(); private: void addFontLocked(MinikinFont* typeface, FontStyle style); @@ -163,6 +174,8 @@ private: SparseBitSet mCoverage; bool mCoverageValid; + + hb_font_t* mHbFont; }; } // namespace android diff --git a/engine/src/flutter/libs/minikin/FontCollection.cpp b/engine/src/flutter/libs/minikin/FontCollection.cpp index b4bfe313ba..36d47ded9d 100644 --- a/engine/src/flutter/libs/minikin/FontCollection.cpp +++ b/engine/src/flutter/libs/minikin/FontCollection.cpp @@ -233,4 +233,11 @@ uint32_t FontCollection::getId() const { return mId; } +void FontCollection::purgeFontFamilyHbFontCache() const { + assertMinikinLocked(); + for (size_t i = 0; i < mFamilies.size(); ++i) { + mFamilies[i]->purgeHbFontCache(); + } +} + } // namespace android diff --git a/engine/src/flutter/libs/minikin/FontFamily.cpp b/engine/src/flutter/libs/minikin/FontFamily.cpp index c70cf88023..a6632e0339 100644 --- a/engine/src/flutter/libs/minikin/FontFamily.cpp +++ b/engine/src/flutter/libs/minikin/FontFamily.cpp @@ -20,6 +20,10 @@ #include #include +#include +#include + +#include "HbFaceCache.h" #include "MinikinInternal.h" #include #include @@ -229,4 +233,25 @@ const SparseBitSet* FontFamily::getCoverage() { return &mCoverage; } +bool FontFamily::hasVariationSelector(uint32_t codepoint, uint32_t variationSelector) { + assertMinikinLocked(); + if (!mHbFont) { + const FontStyle defaultStyle; + MinikinFont* minikinFont = getClosestMatch(defaultStyle).font; + hb_face_t* face = getHbFaceLocked(minikinFont); + mHbFont = hb_font_create(face); + hb_ot_font_set_funcs(mHbFont); + } + uint32_t unusedGlyph; + return hb_font_get_glyph(mHbFont, codepoint, variationSelector, &unusedGlyph); +} + +void FontFamily::purgeHbFontCache() { + assertMinikinLocked(); + if (mHbFont) { + hb_font_destroy(mHbFont); + mHbFont = nullptr; + } +} + } // namespace android diff --git a/engine/src/flutter/libs/minikin/Layout.cpp b/engine/src/flutter/libs/minikin/Layout.cpp index 63841515bf..6d62d5d7d8 100644 --- a/engine/src/flutter/libs/minikin/Layout.cpp +++ b/engine/src/flutter/libs/minikin/Layout.cpp @@ -131,6 +131,7 @@ public: layout->setFontCollection(collection); layout->mAdvances.resize(mCount, 0); ctx->clearHbFonts(); + collection->purgeFontFamilyHbFontCache(); layout->doLayoutRun(mChars, mStart, mCount, mNchars, mIsRtl, ctx); } @@ -595,6 +596,7 @@ void Layout::doLayout(const uint16_t* buf, size_t start, size_t count, size_t bu start); } ctx.clearHbFonts(); + mCollection->purgeFontFamilyHbFontCache(); } void Layout::doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize, diff --git a/engine/src/flutter/tests/Android.mk b/engine/src/flutter/tests/Android.mk index b4d5e91bc3..b69b8f243f 100644 --- a/engine/src/flutter/tests/Android.mk +++ b/engine/src/flutter/tests/Android.mk @@ -40,6 +40,7 @@ LOCAL_STATIC_LIBRARIES += \ LOCAL_SRC_FILES += \ FontCollectionItemizeTest.cpp \ + FontFamilyTest.cpp \ FontTestUtils.cpp \ MinikinFontForTest.cpp \ GraphemeBreakTests.cpp \ diff --git a/engine/src/flutter/tests/FontFamilyTest.cpp b/engine/src/flutter/tests/FontFamilyTest.cpp new file mode 100644 index 0000000000..a6813f013b --- /dev/null +++ b/engine/src/flutter/tests/FontFamilyTest.cpp @@ -0,0 +1,122 @@ +/* + * 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 "MinikinFontForTest.h" +#include "MinikinInternal.h" + +namespace android { + +// The test font has following glyphs. +// U+82A6 +// U+82A6 U+FE00 (VS1) +// U+82A6 U+E0100 (VS17) +// U+82A6 U+E0101 (VS18) +// U+82A6 U+E0102 (VS19) +// U+845B +// U+845B U+FE00 (VS2) +// U+845B U+E0101 (VS18) +// U+845B U+E0102 (VS19) +// U+845B U+E0103 (VS20) +// U+537F +// U+717D U+FE02 (VS3) +// U+717D U+E0102 (VS19) +// U+717D U+E0103 (VS20) +const char kVsTestFont[] = "/data/minikin/test/data/VarioationSelectorTest-Regular.ttf"; + +class FontFamilyTest : public testing::Test { +public: + virtual void SetUp() override { + if (access(kVsTestFont, R_OK) != 0) { + FAIL() << "Unable to read " << kVsTestFont << ". " + << "Please prepare the test data directory. " + << "For more details, please see how_to_run.txt."; + } + } +}; + +// Asserts that the font family has glyphs for and only for specified codepoint +// and variationSelector pairs. +void expectVSGlyphs(FontFamily* family, uint32_t codepoint, const std::set& vs) { + for (uint32_t i = 0xFE00; i <= 0xE01EF; ++i) { + // Move to variation selectors supplements after variation selectors. + if (i == 0xFF00) { + i = 0xE0100; + } + if (vs.find(i) == vs.end()) { + EXPECT_FALSE(family->hasVariationSelector(codepoint, i)) + << "Glyph for U+" << std::hex << codepoint << " U+" << i; + } else { + EXPECT_TRUE(family->hasVariationSelector(codepoint, i)) + << "Glyph for U+" << std::hex << codepoint << " U+" << i; + } + + } +} + +TEST_F(FontFamilyTest, hasVariationSelectorTest) { + MinikinFontForTest minikinFont(kVsTestFont); + FontFamily family; + family.addFont(&minikinFont); + + AutoMutex _l(gMinikinLock); + + const uint32_t kVS1 = 0xFE00; + const uint32_t kVS2 = 0xFE01; + const uint32_t kVS3 = 0xFE02; + const uint32_t kVS17 = 0xE0100; + const uint32_t kVS18 = 0xE0101; + const uint32_t kVS19 = 0xE0102; + const uint32_t kVS20 = 0xE0103; + + const uint32_t kSupportedChar1 = 0x82A6; + EXPECT_TRUE(family.getCoverage()->get(kSupportedChar1)); + expectVSGlyphs(&family, kSupportedChar1, std::set({kVS1, kVS17, kVS18, kVS19})); + + const uint32_t kSupportedChar2 = 0x845B; + EXPECT_TRUE(family.getCoverage()->get(kSupportedChar2)); + expectVSGlyphs(&family, kSupportedChar2, std::set({kVS2, kVS18, kVS19, kVS20})); + + const uint32_t kNoVsSupportedChar = 0x537F; + EXPECT_TRUE(family.getCoverage()->get(kNoVsSupportedChar)); + expectVSGlyphs(&family, kNoVsSupportedChar, std::set()); + + const uint32_t kVsOnlySupportedChar = 0x717D; + EXPECT_FALSE(family.getCoverage()->get(kVsOnlySupportedChar)); + expectVSGlyphs(&family, kVsOnlySupportedChar, std::set({kVS3, kVS19, kVS20})); + + const uint32_t kNotSupportedChar = 0x845C; + EXPECT_FALSE(family.getCoverage()->get(kNotSupportedChar)); + expectVSGlyphs(&family, kNotSupportedChar, std::set()); +} + +TEST_F(FontFamilyTest, hasVariationSelectorWorksAfterpurgeHbFontCache) { + MinikinFontForTest minikinFont(kVsTestFont); + FontFamily family; + family.addFont(&minikinFont); + + const uint32_t kVS1 = 0xFE00; + const uint32_t kSupportedChar1 = 0x82A6; + + AutoMutex _l(gMinikinLock); + EXPECT_TRUE(family.hasVariationSelector(kSupportedChar1, kVS1)); + + family.purgeHbFontCache(); + EXPECT_TRUE(family.hasVariationSelector(kSupportedChar1, kVS1)); +} +} // namespace android diff --git a/engine/src/flutter/tests/how_to_run.txt b/engine/src/flutter/tests/how_to_run.txt index a6b7952855..09c3b0614e 100644 --- a/engine/src/flutter/tests/how_to_run.txt +++ b/engine/src/flutter/tests/how_to_run.txt @@ -1,4 +1,5 @@ mmm -j8 frameworks/minikin/tests && adb push $OUT/data/nativetest/minikin_tests/minikin_tests \ /data/nativetest/minikin_tests/minikin_tests && +adb push -p frameworks/minikin/tests/data /data/minikin/test/data && adb shell /data/nativetest/minikin_tests/minikin_tests