diff --git a/engine/src/flutter/include/minikin/FontCollection.h b/engine/src/flutter/include/minikin/FontCollection.h index 2364538799..d5fc5a11bd 100644 --- a/engine/src/flutter/include/minikin/FontCollection.h +++ b/engine/src/flutter/include/minikin/FontCollection.h @@ -59,9 +59,14 @@ private: static const int kLogCharsPerPage = 8; static const int kPageMask = (1 << kLogCharsPerPage) - 1; + // mFamilyVec holds the indices of the mFamilies and mRanges holds the range of indices of + // mFamilyVec. The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of + // the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01 + // (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports + // only limited range of code points. struct Range { - size_t start; - size_t end; + uint16_t start; + uint16_t end; }; // Initialize the FontCollection. @@ -96,10 +101,12 @@ private: // Following two vectors are pre-calculated tables for resolving coverage faster. // For example, to iterate over all fonts which support Unicode code point U+XXYYZZ, - // iterate font families from mFamilyVec[mRanges[0xXXYY].start] to + // iterate font families index from mFamilyVec[mRanges[0xXXYY].start] to // mFamilyVec[mRange[0xXXYY].end] instead of whole mFamilies. + // This vector contains indices into mFamilies. + // This vector can't be empty. std::vector mRanges; - std::vector> mFamilyVec; + std::vector mFamilyVec; // This vector has pointers to the font family instances which have cmap 14 subtables. std::vector> mVSFamilyVec; diff --git a/engine/src/flutter/libs/minikin/FontCollection.cpp b/engine/src/flutter/libs/minikin/FontCollection.cpp index 8ec302b5ea..a3d2d974e0 100644 --- a/engine/src/flutter/libs/minikin/FontCollection.cpp +++ b/engine/src/flutter/libs/minikin/FontCollection.cpp @@ -118,8 +118,9 @@ void FontCollection::init(const vector>& typefaces) 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."); size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage; - size_t offset = 0; // TODO: Use variation selector map for mRanges construction. // A font can have a glyph for a base code point and variation selector pair but no glyph for // the base code point without variation selector. The family won't be listed in the range in @@ -131,12 +132,11 @@ void FontCollection::init(const vector>& typefaces) #ifdef VERBOSE_DEBUG ALOGD("i=%zd: range start = %zd\n", i, offset); #endif - range->start = offset; + range->start = mFamilyVec.size(); for (size_t j = 0; j < nTypefaces; j++) { if (lastChar[j] < (i + 1) << kLogCharsPerPage) { const std::shared_ptr& family = mFamilies[j]; - mFamilyVec.push_back(family); - offset++; + mFamilyVec.push_back(static_cast(j)); uint32_t nextChar = family->getCoverage().nextSetBit((i + 1) << kLogCharsPerPage); #ifdef VERBOSE_DEBUG ALOGD("nextChar = %d (j = %zd)\n", nextChar, j); @@ -144,8 +144,11 @@ void FontCollection::init(const vector>& typefaces) lastChar[j] = nextChar; } } - range->end = offset; + 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."); } // Special scores for the font fallback. @@ -286,11 +289,10 @@ const std::shared_ptr& FontCollection::getFamilyForChar(uint32_t ch, return mFamilies[0]; } - const std::vector>& familyVec = (vs == 0) ? mFamilyVec : mFamilies; Range range = mRanges[ch >> kLogCharsPerPage]; if (vs != 0) { - range = { 0, mFamilies.size() }; + range = { 0, static_cast(mFamilies.size()) }; } #ifdef VERBOSE_DEBUG @@ -299,7 +301,8 @@ const std::shared_ptr& FontCollection::getFamilyForChar(uint32_t ch, int bestFamilyIndex = -1; uint32_t bestScore = kUnsupportedFontScore; for (size_t i = range.start; i < range.end; i++) { - const std::shared_ptr& family = familyVec[i]; + const std::shared_ptr& family = + vs == 0 ? mFamilies[mFamilyVec[i]] : mFamilies[i]; const uint32_t score = calcFamilyScore(ch, vs, variant, langListId, family); if (score == kFirstFontScore) { // If the first font family supports the given character or variation sequence, always @@ -325,7 +328,7 @@ const std::shared_ptr& FontCollection::getFamilyForChar(uint32_t ch, } return mFamilies[0]; } - return familyVec[bestFamilyIndex]; + return vs == 0 ? mFamilies[mFamilyVec[bestFamilyIndex]] : mFamilies[bestFamilyIndex]; } const uint32_t NBSP = 0x00A0;