diff options
author | Seigo Nonaka <nona@google.com> | 2021-02-03 21:17:43 -0800 |
---|---|---|
committer | Seigo Nonaka <nona@google.com> | 2021-02-03 21:38:34 -0800 |
commit | 4fa056355fbeb1135e593985110f29c1726b76ba (patch) | |
tree | 946ca70dd4d44648033dcf86f145244e96510732 | |
parent | cdde2687a7d5f5a21653c4478b994bd3df619879 (diff) | |
download | minikin-4fa056355fbeb1135e593985110f29c1726b76ba.tar.gz |
Save LocaleList ID to Font instance
Unfortunately, Java Font API has getLocaleList API which gives
locale list if it is in the system font family.
Bug: 179113771
Test: minikin_tests
Test: atest CtsTextTestCases CtsGraphicsTestCases
Change-Id: I81c7f7f97ba90e41212456c92e422eb98e9f6c03
-rw-r--r-- | include/minikin/Font.h | 30 | ||||
-rw-r--r-- | include/minikin/FontFamily.h | 11 | ||||
-rw-r--r-- | include/minikin/LocaleList.h | 4 | ||||
-rw-r--r-- | libs/minikin/Font.cpp | 8 | ||||
-rw-r--r-- | libs/minikin/FontFamily.cpp | 20 | ||||
-rw-r--r-- | libs/minikin/LocaleListCache.cpp | 11 | ||||
-rw-r--r-- | libs/minikin/LocaleListCache.h | 5 | ||||
-rw-r--r-- | tests/unittest/FontTest.cpp | 3 |
8 files changed, 60 insertions, 32 deletions
diff --git a/include/minikin/Font.h b/include/minikin/Font.h index bd31429..bcd630e 100644 --- a/include/minikin/Font.h +++ b/include/minikin/Font.h @@ -26,6 +26,7 @@ #include "minikin/FontStyle.h" #include "minikin/FontVariation.h" #include "minikin/HbUtils.h" +#include "minikin/LocaleList.h" #include "minikin/Macros.h" #include "minikin/MinikinFont.h" @@ -91,12 +92,18 @@ public: return *this; } + Builder& setLocaleListId(uint32_t id) { + mLocaleListId = id; + return *this; + } + std::shared_ptr<Font> build(); private: std::shared_ptr<MinikinFont> mTypeface; uint16_t mWeight = static_cast<uint16_t>(FontStyle::Weight::NORMAL); FontStyle::Slant mSlant = FontStyle::Slant::UPRIGHT; + uint32_t mLocaleListId = kEmptyLocaleListId; bool mIsWeightSet = false; bool mIsSlantSet = false; }; @@ -110,10 +117,10 @@ public: using TypefaceWriter = void(BufferWriter* writer, const MinikinFont* typeface); template <TypefaceReader typefaceReader> - static std::shared_ptr<Font> readFrom(BufferReader* reader) { + static std::shared_ptr<Font> readFrom(BufferReader* reader, uint32_t localeListId) { FontStyle style = FontStyle(reader); TypefaceLoader typefaceLoader = typefaceReader(reader); - return std::shared_ptr<Font>(new Font(style, std::move(typefaceLoader))); + return std::shared_ptr<Font>(new Font(style, std::move(typefaceLoader), localeListId)); } template <TypefaceWriter typefaceWriter> @@ -122,6 +129,9 @@ public: typefaceWriter(writer, typeface().get()); } + // This locale list is just for API compatibility. This is not used in font selection or family + // fallback. + uint32_t getLocaleListId() const { return mLocaleListId; } const std::shared_ptr<MinikinFont>& typeface() const; inline FontStyle style() const { return mStyle; } const HbFontUniquePtr& baseFont() const; @@ -130,10 +140,16 @@ public: private: // Use Builder instead. - Font(std::shared_ptr<MinikinFont>&& typeface, FontStyle style, HbFontUniquePtr&& baseFont) - : mTypeface(std::move(typeface)), mStyle(style), mBaseFont(std::move(baseFont)) {} - Font(FontStyle style, TypefaceLoader&& typefaceLoader) - : mStyle(style), mTypefaceLoader(std::move(typefaceLoader)) {} + Font(std::shared_ptr<MinikinFont>&& typeface, FontStyle style, HbFontUniquePtr&& baseFont, + uint32_t localeListId) + : mTypeface(std::move(typeface)), + mStyle(style), + mBaseFont(std::move(baseFont)), + mLocaleListId(localeListId) {} + Font(FontStyle style, TypefaceLoader&& typefaceLoader, uint32_t localeListId) + : mStyle(style), + mTypefaceLoader(std::move(typefaceLoader)), + mLocaleListId(localeListId) {} void initTypefaceLocked() const EXCLUSIVE_LOCKS_REQUIRED(mTypefaceMutex); @@ -150,6 +166,8 @@ private: // Non-empty if created by readFrom(). TypefaceLoader mTypefaceLoader; + uint32_t mLocaleListId; + // Stop copying and moving Font(Font&& o) = delete; Font& operator=(Font&& o) = delete; diff --git a/include/minikin/FontFamily.h b/include/minikin/FontFamily.h index a23190b..c5557a5 100644 --- a/include/minikin/FontFamily.h +++ b/include/minikin/FontFamily.h @@ -40,17 +40,19 @@ public: template <Font::TypefaceReader typefaceReader> static std::shared_ptr<FontFamily> readFrom(BufferReader* reader) { + uint32_t localeListId = readLocaleListInternal(reader); uint32_t fontsCount = reader->read<uint32_t>(); std::vector<std::shared_ptr<Font>> fonts; fonts.reserve(fontsCount); for (uint32_t i = 0; i < fontsCount; i++) { - fonts.emplace_back(Font::readFrom<typefaceReader>(reader)); + fonts.emplace_back(Font::readFrom<typefaceReader>(reader, localeListId)); } - return readFromInternal(reader, std::move(fonts)); + return readFromInternal(reader, std::move(fonts), localeListId); } template <Font::TypefaceWriter typefaceWriter> void writeTo(BufferWriter* writer) const { + writeLocaleListInternal(writer); writer->write<uint32_t>(mFonts.size()); for (const std::shared_ptr<Font>& font : mFonts) { font->writeTo<typefaceWriter>(writer); @@ -93,8 +95,11 @@ private: bool isCustomFallback, SparseBitSet&& coverage, std::vector<std::unique_ptr<SparseBitSet>>&& cmapFmt14Coverage); + static uint32_t readLocaleListInternal(BufferReader* reader); static std::shared_ptr<FontFamily> readFromInternal(BufferReader* reader, - std::vector<std::shared_ptr<Font>>&& fonts); + std::vector<std::shared_ptr<Font>>&& fonts, + uint32_t localeListId); + void writeLocaleListInternal(BufferWriter* writer) const; void writeToInternal(BufferWriter* writer) const; void computeCoverage(); diff --git a/include/minikin/LocaleList.h b/include/minikin/LocaleList.h index 9c14b63..d173a46 100644 --- a/include/minikin/LocaleList.h +++ b/include/minikin/LocaleList.h @@ -21,6 +21,10 @@ namespace minikin { +// A special ID for the empty locale list. +// This value must be 0 since the empty locale list is inserted into mLocaleLists by default. +const static uint32_t kEmptyLocaleListId = 0; + // Looks up a locale list from an internal cache and returns its ID. // If the passed locale list is not in the cache, registers it and returns newly assigned ID. // TODO: Introduce LocaleId type. diff --git a/libs/minikin/Font.cpp b/libs/minikin/Font.cpp index 30a2734..ba42b2c 100644 --- a/libs/minikin/Font.cpp +++ b/libs/minikin/Font.cpp @@ -35,8 +35,8 @@ namespace minikin { std::shared_ptr<Font> Font::Builder::build() { if (mIsWeightSet && mIsSlantSet) { // No need to read OS/2 header of the font file. - return std::shared_ptr<Font>( - new Font(std::move(mTypeface), FontStyle(mWeight, mSlant), prepareFont(mTypeface))); + return std::shared_ptr<Font>(new Font(std::move(mTypeface), FontStyle(mWeight, mSlant), + prepareFont(mTypeface), mLocaleListId)); } HbFontUniquePtr font = prepareFont(mTypeface); @@ -47,8 +47,8 @@ std::shared_ptr<Font> Font::Builder::build() { if (!mIsSlantSet) { mSlant = styleFromFont.slant(); } - return std::shared_ptr<Font>( - new Font(std::move(mTypeface), FontStyle(mWeight, mSlant), std::move(font))); + return std::shared_ptr<Font>(new Font(std::move(mTypeface), FontStyle(mWeight, mSlant), + std::move(font), mLocaleListId)); } const std::shared_ptr<MinikinFont>& Font::typeface() const { diff --git a/libs/minikin/FontFamily.cpp b/libs/minikin/FontFamily.cpp index ef262e9..d23e5b9 100644 --- a/libs/minikin/FontFamily.cpp +++ b/libs/minikin/FontFamily.cpp @@ -39,8 +39,7 @@ FontFamily::FontFamily(std::vector<std::shared_ptr<Font>>&& fonts) : FontFamily(FamilyVariant::DEFAULT, std::move(fonts)) {} FontFamily::FontFamily(FamilyVariant variant, std::vector<std::shared_ptr<Font>>&& fonts) - : FontFamily(LocaleListCache::kEmptyListId, variant, std::move(fonts), - false /* isCustomFallback */) {} + : FontFamily(kEmptyLocaleListId, variant, std::move(fonts), false /* isCustomFallback */) {} FontFamily::FontFamily(uint32_t localeListId, FamilyVariant variant, std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback) @@ -68,11 +67,11 @@ FontFamily::FontFamily(uint32_t localeListId, FamilyVariant variant, mCoverage(std::move(coverage)), mCmapFmt14Coverage(std::move(cmapFmt14Coverage)) {} -// Read fields other than mFonts. +// Read fields other than mFonts, mLocaleList. // static -std::shared_ptr<FontFamily> FontFamily::readFromInternal( - BufferReader* reader, std::vector<std::shared_ptr<Font>>&& fonts) { - uint32_t localeListId = LocaleListCache::readFrom(reader); +std::shared_ptr<FontFamily> FontFamily::readFromInternal(BufferReader* reader, + std::vector<std::shared_ptr<Font>>&& fonts, + uint32_t localeListId) { // FamilyVariant is uint8_t static_assert(sizeof(FamilyVariant) == 1); FamilyVariant variant = reader->read<FamilyVariant>(); @@ -97,9 +96,13 @@ std::shared_ptr<FontFamily> FontFamily::readFromInternal( isCustomFallback, std::move(coverage), std::move(cmapFmt14Coverage))); } +// static +uint32_t FontFamily::readLocaleListInternal(BufferReader* reader) { + return LocaleListCache::readFrom(reader); +} + // Write fields other than mFonts. void FontFamily::writeToInternal(BufferWriter* writer) const { - LocaleListCache::writeTo(writer, mLocaleListId); writer->write<FamilyVariant>(mVariant); std::vector<AxisTag> axes(mSupportedAxes.begin(), mSupportedAxes.end()); // Sort axes to be deterministic. @@ -124,6 +127,9 @@ void FontFamily::writeToInternal(BufferWriter* writer) const { } } +void FontFamily::writeLocaleListInternal(BufferWriter* writer) const { + LocaleListCache::writeTo(writer, mLocaleListId); +} // Compute a matching metric between two styles - 0 is an exact match static int computeMatch(FontStyle style1, FontStyle style2) { if (style1 == style2) return 0; diff --git a/libs/minikin/LocaleListCache.cpp b/libs/minikin/LocaleListCache.cpp index 7065272..38800f7 100644 --- a/libs/minikin/LocaleListCache.cpp +++ b/libs/minikin/LocaleListCache.cpp @@ -22,6 +22,7 @@ #include <log/log.h> #include <minikin/Hasher.h> +#include <minikin/LocaleList.h> #include <unicode/uloc.h> #include <unicode/umachine.h> @@ -30,8 +31,6 @@ namespace minikin { -const uint32_t LocaleListCache::kEmptyListId; - // Returns the text length of output. static size_t toLanguageTag(char* output, size_t outSize, const StringPiece& locale) { output[0] = '\0'; @@ -119,11 +118,11 @@ size_t LocaleListCache::LocaleVectorHash::operator()(const std::vector<Locale>& } LocaleListCache::LocaleListCache() { - // Insert an empty locale list for mapping default locale list to kEmptyListId. + // Insert an empty locale list for mapping default locale list to kEmptyLocaleListId. // The default locale list has only one Locale and it is the unsupported locale. mLocaleLists.emplace_back(); - mLocaleListLookupTable.emplace(std::vector<Locale>(), kEmptyListId); - mLocaleListStringCache.emplace("", kEmptyListId); + mLocaleListLookupTable.emplace(std::vector<Locale>(), kEmptyLocaleListId); + mLocaleListStringCache.emplace("", kEmptyLocaleListId); } uint32_t LocaleListCache::getIdInternal(const std::string& locales) { @@ -139,7 +138,7 @@ uint32_t LocaleListCache::getIdInternal(const std::string& locales) { uint32_t LocaleListCache::getIdInternal(std::vector<Locale>&& locales) { if (locales.empty()) { - return kEmptyListId; + return kEmptyLocaleListId; } const auto& it = mLocaleListLookupTable.find(locales); if (it != mLocaleListLookupTable.end()) { diff --git a/libs/minikin/LocaleListCache.h b/libs/minikin/LocaleListCache.h index 4e3c806..a83b1c8 100644 --- a/libs/minikin/LocaleListCache.h +++ b/libs/minikin/LocaleListCache.h @@ -29,11 +29,6 @@ namespace minikin { class LocaleListCache { public: - // A special ID for the empty locale list. - // This value must be 0 since the empty locale list is inserted into mLocaleLists by - // default. - const static uint32_t kEmptyListId = 0; - // A special ID for the invalid locale list. const static uint32_t kInvalidListId = (uint32_t)(-1); diff --git a/tests/unittest/FontTest.cpp b/tests/unittest/FontTest.cpp index dc26ab2..68f5b51 100644 --- a/tests/unittest/FontTest.cpp +++ b/tests/unittest/FontTest.cpp @@ -30,7 +30,8 @@ TEST(FontTest, BufferTest) { std::vector<uint8_t> buffer = writeToBuffer<Font, writeFreeTypeMinikinFontForTest>(*original); BufferReader reader(buffer.data()); - std::shared_ptr<Font> font = Font::readFrom<readFreeTypeMinikinFontForTest>(&reader); + std::shared_ptr<Font> font = + Font::readFrom<readFreeTypeMinikinFontForTest>(&reader, kEmptyLocaleListId); EXPECT_EQ(minikinFont->GetFontPath(), font->typeface()->GetFontPath()); EXPECT_EQ(original->style(), font->style()); EXPECT_NE(nullptr, font->baseFont()); |