summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeigo Nonaka <nona@google.com>2021-05-27 15:45:29 -0700
committerSeigo Nonaka <nona@google.com>2021-05-27 15:54:37 -0700
commit0a24da587de26c95a1a926a4b9d7a07dc93ee76a (patch)
tree7bf9c80beeb29005bd76da3426398d7459ec6b1c
parent2deb0c865fea3f7133b0403b9b33afb5261534c0 (diff)
downloadminikin-0a24da587de26c95a1a926a4b9d7a07dc93ee76a.tar.gz
Calculate set of available font
Bug: 188201287 Test: atest FontFamilyUpdateRequestTest Test: atest FontListParserTest Test: atest FontManagerTest Test: atest NativeSystemFontTest Test: atest PersistentSystemFontConfigTest Test: atest SystemFontsTest Test: atest SystemFontsUniqueNameTest Test: atest UpdatableFontDirTest Test: atest UpdatableSystemFontTest Test: minikin_tests Change-Id: Icfe9a5f9681e7fddc241a213ea63792465563073
-rw-r--r--include/minikin/SystemFonts.h15
-rw-r--r--libs/minikin/SystemFonts.cpp18
-rw-r--r--tests/unittest/SystemFontsTest.cpp36
3 files changed, 69 insertions, 0 deletions
diff --git a/include/minikin/SystemFonts.h b/include/minikin/SystemFonts.h
index 616997d..cf4ab75 100644
--- a/include/minikin/SystemFonts.h
+++ b/include/minikin/SystemFonts.h
@@ -55,6 +55,10 @@ public:
return getInstance().getFontMapInternal(func);
}
+ static void getFontSet(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) {
+ return getInstance().getFontSetInternal(func);
+ }
+
protected:
// Visible for testing purposes.
SystemFonts() {}
@@ -83,12 +87,23 @@ protected:
func(mCollections);
}
+ void getFontSetInternal(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) {
+ std::lock_guard<std::mutex> lock(mMutex);
+ if (!mFonts) {
+ buildFontSetLocked();
+ }
+ func(mFonts.value());
+ }
+
private:
static SystemFonts& getInstance();
+ void buildFontSetLocked() EXCLUSIVE_LOCKS_REQUIRED(mMutex);
+
std::map<std::string, std::shared_ptr<FontCollection>> mSystemFallbacks GUARDED_BY(mMutex);
std::shared_ptr<FontCollection> mDefaultFallback GUARDED_BY(mMutex);
std::vector<std::shared_ptr<FontCollection>> mCollections GUARDED_BY(mMutex);
+ std::optional<std::vector<std::shared_ptr<Font>>> mFonts GUARDED_BY(mMutex);
std::mutex mMutex;
};
diff --git a/libs/minikin/SystemFonts.cpp b/libs/minikin/SystemFonts.cpp
index b53c8de..9c8fa66 100644
--- a/libs/minikin/SystemFonts.cpp
+++ b/libs/minikin/SystemFonts.cpp
@@ -36,4 +36,22 @@ std::shared_ptr<FontCollection> SystemFonts::findFontCollectionInternal(
return mDefaultFallback;
}
+void SystemFonts::buildFontSetLocked() {
+ std::unordered_set<FontFamily*> uniqueFamilies;
+
+ for (const auto& collection : mCollections) {
+ for (const auto& family : collection->getFamilies()) {
+ uniqueFamilies.insert(family.get());
+ }
+ }
+
+ std::vector<std::shared_ptr<Font>> result;
+ for (const auto family : uniqueFamilies) {
+ for (size_t i = 0; i < family->getNumFonts(); ++i) {
+ result.push_back(family->getFontRef(i));
+ }
+ }
+ mFonts = std::move(result);
+}
+
} // namespace minikin
diff --git a/tests/unittest/SystemFontsTest.cpp b/tests/unittest/SystemFontsTest.cpp
index 511081a..f1b0109 100644
--- a/tests/unittest/SystemFontsTest.cpp
+++ b/tests/unittest/SystemFontsTest.cpp
@@ -21,6 +21,7 @@
#include "minikin/FontCollection.h"
#include "FontTestUtils.h"
+#include "PathUtils.h"
namespace minikin {
namespace {
@@ -34,6 +35,14 @@ public:
return findFontCollectionInternal(familyName);
}
+ void addFontMap(std::shared_ptr<FontCollection>&& collections) {
+ addFontMapInternal(std::move(collections));
+ }
+
+ void getFontSet(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) {
+ getFontSetInternal(func);
+ }
+
void registerFallback(const std::string& familyName,
const std::shared_ptr<FontCollection>& fc) {
registerFallbackInternal(familyName, fc);
@@ -78,5 +87,32 @@ TEST(SystemFontsTest, updateDefaultAndFallback) {
EXPECT_EQ(fc1, systemFonts.findFontCollection("sans"));
}
+TEST(SystemFontTest, getAvailableFont_dedupFonts) {
+ TestableSystemFonts systemFonts;
+ auto asciiFamily = buildFontFamily("Ascii.ttf");
+ auto boldFamily = buildFontFamily("Bold.ttf");
+ auto boldItalicFamily = buildFontFamily("BoldItalic.ttf");
+
+ auto fc1Families = std::vector<std::shared_ptr<FontFamily>>{asciiFamily, boldItalicFamily};
+ auto fc2Families = std::vector<std::shared_ptr<FontFamily>>{boldFamily, boldItalicFamily};
+ auto fc1 = std::make_shared<FontCollection>(std::move(fc1Families));
+ auto fc2 = std::make_shared<FontCollection>(std::move(fc2Families));
+
+ systemFonts.addFontMap(std::move(fc1));
+ systemFonts.addFontMap(std::move(fc2));
+
+ systemFonts.getFontSet([](const std::vector<std::shared_ptr<Font>>& fonts) {
+ EXPECT_EQ(3u, fonts.size()); // Ascii, Bold and BoldItalic
+ std::unordered_set<std::string> fontPaths;
+ for (const auto& font : fonts) {
+ fontPaths.insert(getBasename(font->typeface()->GetFontPath()));
+ }
+
+ EXPECT_TRUE(fontPaths.find("Ascii.ttf") != fontPaths.end());
+ EXPECT_TRUE(fontPaths.find("Bold.ttf") != fontPaths.end());
+ EXPECT_TRUE(fontPaths.find("BoldItalic.ttf") != fontPaths.end());
+ });
+}
+
} // namespace
} // namespace minikin