/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MINIKIN_SYSTEM_FONTS_H #define MINIKIN_SYSTEM_FONTS_H #include #include #include #include #include #include "minikin/FontCollection.h" #include "minikin/U16StringPiece.h" namespace minikin { // Provides a system font mapping. class SystemFonts { public: static std::shared_ptr findFontCollection(const std::string& familyName) { return getInstance().findFontCollectionInternal(familyName); } static void registerFallback(const std::string& familyName, const std::shared_ptr& fc) { return getInstance().registerFallbackInternal(familyName, fc); } static void registerDefault(const std::shared_ptr& fc) { return getInstance().registerDefaultInternal(fc); } using FontMapDeleter = std::function; static void addFontMap(std::shared_ptr&& collections) { return getInstance().addFontMapInternal(std::move(collections)); } // This obtains a mutex inside, so do not call this method inside callback. static void getFontSet(std::function>&)> func) { return getInstance().getFontSetInternal(func); } protected: // Visible for testing purposes. SystemFonts() {} virtual ~SystemFonts() {} std::shared_ptr findFontCollectionInternal(const std::string& familyName); void registerFallbackInternal(const std::string& familyName, const std::shared_ptr& fc) { std::lock_guard lock(mMutex); mSystemFallbacks[familyName] = fc; } void registerDefaultInternal(const std::shared_ptr& fc) { std::lock_guard lock(mMutex); mDefaultFallback = fc; } void addFontMapInternal(std::shared_ptr&& collections) { std::lock_guard lock(mMutex); mCollections.emplace_back(std::move(collections)); } void getFontSetInternal(std::function>&)> func) { std::lock_guard lock(mMutex); if (!mFonts) { buildFontSetLocked(); } func(mFonts.value()); } private: static SystemFonts& getInstance(); void buildFontSetLocked() EXCLUSIVE_LOCKS_REQUIRED(mMutex); std::map> mSystemFallbacks GUARDED_BY(mMutex); std::shared_ptr mDefaultFallback GUARDED_BY(mMutex); std::vector> mCollections GUARDED_BY(mMutex); std::optional>> mFonts GUARDED_BY(mMutex); std::mutex mMutex; }; } // namespace minikin #endif // MINIKIN_SYSTEM_FONTS_H