fix crash in FontCollection::init() when FontFamily is empty (flutter/engine#23019)

This commit is contained in:
liguisheng
2020-12-17 07:29:02 +08:00
committed by GitHub
parent 21a7f7065e
commit 1387021a0d
4 changed files with 39 additions and 24 deletions

View File

@@ -51,21 +51,20 @@ std::string GetFontLocale(uint32_t langListId) {
return langs.size() ? langs[0].getString() : "";
}
FontCollection::FontCollection(std::shared_ptr<FontFamily>&& typeface)
: mMaxChar(0) {
std::vector<std::shared_ptr<FontFamily>> typefaces;
typefaces.push_back(typeface);
init(typefaces);
std::shared_ptr<minikin::FontCollection> FontCollection::Create(
const std::vector<std::shared_ptr<FontFamily>>& typefaces) {
std::shared_ptr<minikin::FontCollection> font_collection(
new minikin::FontCollection());
if (!font_collection || !font_collection->init(typefaces)) {
return nullptr;
}
return font_collection;
}
FontCollection::FontCollection(
const vector<std::shared_ptr<FontFamily>>& typefaces)
: mMaxChar(0) {
init(typefaces);
}
FontCollection::FontCollection() : mMaxChar(0) {}
void FontCollection::init(
const vector<std::shared_ptr<FontFamily>>& typefaces) {
bool FontCollection::init(
const std::vector<std::shared_ptr<FontFamily>>& typefaces) {
std::scoped_lock _l(gMinikinLock);
mId = sNextId++;
vector<uint32_t> lastChar;
@@ -91,10 +90,14 @@ void FontCollection::init(
mSupportedAxes.insert(supportedAxes.begin(), supportedAxes.end());
}
nTypefaces = mFamilies.size();
LOG_ALWAYS_FATAL_IF(nTypefaces == 0,
"Font collection must have at least one valid typeface");
LOG_ALWAYS_FATAL_IF(nTypefaces > 254,
"Font collection may only have up to 254 font families.");
if (nTypefaces == 0) {
ALOGE("Font collection must have at least one valid typeface.");
return false;
}
if (nTypefaces > 254) {
ALOGE("Font collection may only have up to 254 font families.");
return false;
}
size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
// TODO: Use variation selector map for mRanges construction.
// A font can have a glyph for a base code point and variation selector pair
@@ -122,9 +125,12 @@ void FontCollection::init(
}
range->end = mFamilyVec.size();
}
// See the comment in Range for more details.
LOG_ALWAYS_FATAL_IF(mFamilyVec.size() >= 0xFFFF,
"Exceeded the maximum indexable cmap coverage.");
if (mFamilyVec.size() >= 0xFFFF) {
ALOGE("Exceeded the maximum indexable cmap coverage.");
return false;
}
return true;
}
// Special scores for the font fallback.
@@ -566,7 +572,7 @@ std::shared_ptr<FontCollection> FontCollection::createCollectionWithVariation(
}
}
return std::shared_ptr<FontCollection>(new FontCollection(families));
return FontCollection::Create(std::move(families));
}
uint32_t FontCollection::getId() const {

View File

@@ -28,10 +28,12 @@
namespace minikin {
class FontCollection {
private:
explicit FontCollection();
public:
explicit FontCollection(
static std::shared_ptr<minikin::FontCollection> Create(
const std::vector<std::shared_ptr<FontFamily>>& typefaces);
explicit FontCollection(std::shared_ptr<FontFamily>&& typeface);
// libtxt extension: an interface for looking up fallback fonts for characters
// that do not match this collection's font families.
@@ -94,7 +96,7 @@ class FontCollection {
};
// Initialize the FontCollection.
void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
bool init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
const std::shared_ptr<FontFamily>& getFamilyForChar(uint32_t ch,
uint32_t vs,

View File

@@ -207,7 +207,11 @@ FontCollection::GetMinikinFontCollectionForFamilies(
}
// Create the minikin font collection.
auto font_collection =
std::make_shared<minikin::FontCollection>(std::move(minikin_families));
minikin::FontCollection::Create(std::move(minikin_families));
if (!font_collection) {
font_collections_cache_[family_key] = nullptr;
return nullptr;
}
if (enable_font_fallback_) {
font_collection->set_fallback_font_provider(
std::make_unique<TxtFallbackFontProvider>(shared_from_this()));

View File

@@ -807,6 +807,9 @@ void ParagraphTxt::Layout(double width) {
std::shared_ptr<minikin::FontCollection> minikin_font_collection =
GetMinikinFontCollectionForStyle(run.style());
if (!minikin_font_collection) {
return;
}
// Lay out this run.
uint16_t* text_ptr = text_.data();