diff --git a/third_party/txt/src/minikin/FontCollection.cpp b/third_party/txt/src/minikin/FontCollection.cpp index f89cde86e3c04..91d1ac295cdc1 100644 --- a/third_party/txt/src/minikin/FontCollection.cpp +++ b/third_party/txt/src/minikin/FontCollection.cpp @@ -51,21 +51,20 @@ std::string GetFontLocale(uint32_t langListId) { return langs.size() ? langs[0].getString() : ""; } -FontCollection::FontCollection(std::shared_ptr&& typeface) - : mMaxChar(0) { - std::vector> typefaces; - typefaces.push_back(typeface); - init(typefaces); +std::shared_ptr FontCollection::Create( + const std::vector>& typefaces) { + std::shared_ptr font_collection( + new minikin::FontCollection()); + if (!font_collection || !font_collection->init(typefaces)) { + return nullptr; + } + return font_collection; } -FontCollection::FontCollection( - const vector>& typefaces) - : mMaxChar(0) { - init(typefaces); -} +FontCollection::FontCollection() : mMaxChar(0) {} -void FontCollection::init( - const vector>& typefaces) { +bool FontCollection::init( + const std::vector>& typefaces) { std::scoped_lock _l(gMinikinLock); mId = sNextId++; vector 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::createCollectionWithVariation( } } - return std::shared_ptr(new FontCollection(families)); + return FontCollection::Create(std::move(families)); } uint32_t FontCollection::getId() const { diff --git a/third_party/txt/src/minikin/FontCollection.h b/third_party/txt/src/minikin/FontCollection.h index f79c24ba6fa06..85ca1f4b86619 100644 --- a/third_party/txt/src/minikin/FontCollection.h +++ b/third_party/txt/src/minikin/FontCollection.h @@ -28,10 +28,12 @@ namespace minikin { class FontCollection { + private: + explicit FontCollection(); + public: - explicit FontCollection( + static std::shared_ptr Create( const std::vector>& typefaces); - explicit FontCollection(std::shared_ptr&& 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>& typefaces); + bool init(const std::vector>& typefaces); const std::shared_ptr& getFamilyForChar(uint32_t ch, uint32_t vs, diff --git a/third_party/txt/src/txt/font_collection.cc b/third_party/txt/src/txt/font_collection.cc index 01edddb440167..353a10e51c08e 100644 --- a/third_party/txt/src/txt/font_collection.cc +++ b/third_party/txt/src/txt/font_collection.cc @@ -207,7 +207,11 @@ FontCollection::GetMinikinFontCollectionForFamilies( } // Create the minikin font collection. auto font_collection = - std::make_shared(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(shared_from_this())); diff --git a/third_party/txt/src/txt/paragraph_txt.cc b/third_party/txt/src/txt/paragraph_txt.cc index f395ba8160cc1..42943d22b8e16 100644 --- a/third_party/txt/src/txt/paragraph_txt.cc +++ b/third_party/txt/src/txt/paragraph_txt.cc @@ -807,6 +807,9 @@ void ParagraphTxt::Layout(double width) { std::shared_ptr minikin_font_collection = GetMinikinFontCollectionForStyle(run.style()); + if (!minikin_font_collection) { + return; + } // Lay out this run. uint16_t* text_ptr = text_.data();