1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINIKIN_SYSTEM_FONTS_H 18 #define MINIKIN_SYSTEM_FONTS_H 19 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <string> 25 26 #include "minikin/FontCollection.h" 27 #include "minikin/U16StringPiece.h" 28 29 namespace minikin { 30 31 // Provides a system font mapping. 32 class SystemFonts { 33 public: findFontCollection(const std::string & familyName)34 static std::shared_ptr<FontCollection> findFontCollection(const std::string& familyName) { 35 return getInstance().findFontCollectionInternal(familyName); 36 } 37 registerFallback(const std::string & familyName,const std::shared_ptr<FontCollection> & fc)38 static void registerFallback(const std::string& familyName, 39 const std::shared_ptr<FontCollection>& fc) { 40 return getInstance().registerFallbackInternal(familyName, fc); 41 } 42 registerDefault(const std::shared_ptr<FontCollection> & fc)43 static void registerDefault(const std::shared_ptr<FontCollection>& fc) { 44 return getInstance().registerDefaultInternal(fc); 45 } 46 47 using FontMapDeleter = std::function<void()>; 48 addFontMap(std::shared_ptr<FontCollection> && collections)49 static void addFontMap(std::shared_ptr<FontCollection>&& collections) { 50 return getInstance().addFontMapInternal(std::move(collections)); 51 } 52 53 // This obtains a mutex inside, so do not call this method inside callback. getFontSet(std::function<void (const std::vector<std::shared_ptr<Font>> &)> func)54 static void getFontSet(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) { 55 return getInstance().getFontSetInternal(func); 56 } 57 58 protected: 59 // Visible for testing purposes. SystemFonts()60 SystemFonts() {} ~SystemFonts()61 virtual ~SystemFonts() {} 62 63 std::shared_ptr<FontCollection> findFontCollectionInternal(const std::string& familyName); registerFallbackInternal(const std::string & familyName,const std::shared_ptr<FontCollection> & fc)64 void registerFallbackInternal(const std::string& familyName, 65 const std::shared_ptr<FontCollection>& fc) { 66 std::lock_guard<std::mutex> lock(mMutex); 67 mSystemFallbacks[familyName] = fc; 68 } 69 registerDefaultInternal(const std::shared_ptr<FontCollection> & fc)70 void registerDefaultInternal(const std::shared_ptr<FontCollection>& fc) { 71 std::lock_guard<std::mutex> lock(mMutex); 72 mDefaultFallback = fc; 73 } 74 addFontMapInternal(std::shared_ptr<FontCollection> && collections)75 void addFontMapInternal(std::shared_ptr<FontCollection>&& collections) { 76 std::lock_guard<std::mutex> lock(mMutex); 77 mCollections.emplace_back(std::move(collections)); 78 } 79 getFontSetInternal(std::function<void (const std::vector<std::shared_ptr<Font>> &)> func)80 void getFontSetInternal(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) { 81 std::lock_guard<std::mutex> lock(mMutex); 82 if (!mFonts) { 83 buildFontSetLocked(); 84 } 85 func(mFonts.value()); 86 } 87 88 private: 89 static SystemFonts& getInstance(); 90 91 void buildFontSetLocked() EXCLUSIVE_LOCKS_REQUIRED(mMutex); 92 93 std::map<std::string, std::shared_ptr<FontCollection>> mSystemFallbacks GUARDED_BY(mMutex); 94 std::shared_ptr<FontCollection> mDefaultFallback GUARDED_BY(mMutex); 95 std::vector<std::shared_ptr<FontCollection>> mCollections GUARDED_BY(mMutex); 96 std::optional<std::vector<std::shared_ptr<Font>>> mFonts GUARDED_BY(mMutex); 97 98 std::mutex mMutex; 99 }; 100 101 } // namespace minikin 102 103 #endif // MINIKIN_SYSTEM_FONTS_H 104