• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 
25 #include "minikin/FontCollection.h"
26 #include "minikin/U16StringPiece.h"
27 
28 namespace minikin {
29 
30 // Provides a system font mapping.
31 class SystemFonts {
32 public:
findFontCollection(const std::string & familyName)33     static std::shared_ptr<FontCollection> findFontCollection(const std::string& familyName) {
34         return getInstance().findFontCollectionInternal(familyName);
35     }
36 
registerFallback(const std::string & familyName,const std::shared_ptr<FontCollection> & fc)37     static void registerFallback(const std::string& familyName,
38                                  const std::shared_ptr<FontCollection>& fc) {
39         return getInstance().registerFallbackInternal(familyName, fc);
40     }
41 
registerDefault(const std::shared_ptr<FontCollection> & fc)42     static void registerDefault(const std::shared_ptr<FontCollection>& fc) {
43         return getInstance().registerDefaultInternal(fc);
44     }
45 
46     using FontMapDeleter = std::function<void()>;
47 
addFontMap(std::shared_ptr<FontCollection> && collections)48     static void addFontMap(std::shared_ptr<FontCollection>&& collections) {
49         return getInstance().addFontMapInternal(std::move(collections));
50     }
51 
52     // 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)53     static void getFontSet(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) {
54         return getInstance().getFontSetInternal(func);
55     }
56 
57 protected:
58     // Visible for testing purposes.
SystemFonts()59     SystemFonts() {}
~SystemFonts()60     virtual ~SystemFonts() {}
61 
62     std::shared_ptr<FontCollection> findFontCollectionInternal(const std::string& familyName);
registerFallbackInternal(const std::string & familyName,const std::shared_ptr<FontCollection> & fc)63     void registerFallbackInternal(const std::string& familyName,
64                                   const std::shared_ptr<FontCollection>& fc) {
65         std::lock_guard<std::mutex> lock(mMutex);
66         mSystemFallbacks[familyName] = fc;
67     }
68 
registerDefaultInternal(const std::shared_ptr<FontCollection> & fc)69     void registerDefaultInternal(const std::shared_ptr<FontCollection>& fc) {
70         std::lock_guard<std::mutex> lock(mMutex);
71         mDefaultFallback = fc;
72     }
73 
addFontMapInternal(std::shared_ptr<FontCollection> && collections)74     void addFontMapInternal(std::shared_ptr<FontCollection>&& collections) {
75         std::lock_guard<std::mutex> lock(mMutex);
76         mCollections.emplace_back(std::move(collections));
77     }
78 
getFontSetInternal(std::function<void (const std::vector<std::shared_ptr<Font>> &)> func)79     void getFontSetInternal(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) {
80         std::lock_guard<std::mutex> lock(mMutex);
81         if (!mFonts) {
82             buildFontSetLocked();
83         }
84         func(mFonts.value());
85     }
86 
87 private:
88     static SystemFonts& getInstance();
89 
90     void buildFontSetLocked() EXCLUSIVE_LOCKS_REQUIRED(mMutex);
91 
92     std::map<std::string, std::shared_ptr<FontCollection>> mSystemFallbacks GUARDED_BY(mMutex);
93     std::shared_ptr<FontCollection> mDefaultFallback GUARDED_BY(mMutex);
94     std::vector<std::shared_ptr<FontCollection>> mCollections GUARDED_BY(mMutex);
95     std::optional<std::vector<std::shared_ptr<Font>>> mFonts GUARDED_BY(mMutex);
96 
97     std::mutex mMutex;
98 };
99 
100 }  // namespace minikin
101 
102 #endif  // MINIKIN_SYSTEM_FONTS_H
103