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
This commit is contained in:
Seigo Nonaka
2015-09-24 18:41:41 +09:00
parent 89e185c3a8
commit ad9cb5909a
8 changed files with 177 additions and 2 deletions

View File

@@ -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;

View File

@@ -19,6 +19,7 @@
#include <vector>
#include <string>
#include <hb.h>
#include <utils/TypeHelpers.h>
@@ -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

View File

@@ -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

View File

@@ -20,6 +20,10 @@
#include <stdlib.h>
#include <stdint.h>
#include <hb.h>
#include <hb-ot.h>
#include "HbFaceCache.h"
#include "MinikinInternal.h"
#include <minikin/MinikinFont.h>
#include <minikin/AnalyzeStyle.h>
@@ -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

View File

@@ -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,

View File

@@ -40,6 +40,7 @@ LOCAL_STATIC_LIBRARIES += \
LOCAL_SRC_FILES += \
FontCollectionItemizeTest.cpp \
FontFamilyTest.cpp \
FontTestUtils.cpp \
MinikinFontForTest.cpp \
GraphemeBreakTests.cpp \

View File

@@ -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 <gtest/gtest.h>
#include <minikin/FontFamily.h>
#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<uint32_t>& 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<uint32_t>({kVS1, kVS17, kVS18, kVS19}));
const uint32_t kSupportedChar2 = 0x845B;
EXPECT_TRUE(family.getCoverage()->get(kSupportedChar2));
expectVSGlyphs(&family, kSupportedChar2, std::set<uint32_t>({kVS2, kVS18, kVS19, kVS20}));
const uint32_t kNoVsSupportedChar = 0x537F;
EXPECT_TRUE(family.getCoverage()->get(kNoVsSupportedChar));
expectVSGlyphs(&family, kNoVsSupportedChar, std::set<uint32_t>());
const uint32_t kVsOnlySupportedChar = 0x717D;
EXPECT_FALSE(family.getCoverage()->get(kVsOnlySupportedChar));
expectVSGlyphs(&family, kVsOnlySupportedChar, std::set<uint32_t>({kVS3, kVS19, kVS20}));
const uint32_t kNotSupportedChar = 0x845C;
EXPECT_FALSE(family.getCoverage()->get(kNotSupportedChar));
expectVSGlyphs(&family, kNotSupportedChar, std::set<uint32_t>());
}
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

View File

@@ -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