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 <string>
23 
24 #include "minikin/FontCollection.h"
25 #include "minikin/U16StringPiece.h"
26 
27 namespace minikin {
28 
29 // Provides a system font mapping.
30 class SystemFonts {
31 public:
findFontCollection(const std::string & familyName)32     static std::shared_ptr<FontCollection> findFontCollection(const std::string& familyName) {
33         return getInstance().findFontCollectionInternal(familyName);
34     }
35 
36     // Do not call this function outside Zygote process.
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 
42     // Do not call this function outside Zygote process.
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 protected:
48     // Visible for testing purposes.
SystemFonts()49     SystemFonts() {}
~SystemFonts()50     virtual ~SystemFonts() {}
51 
52     std::shared_ptr<FontCollection> findFontCollectionInternal(const std::string& familyName) const;
registerFallbackInternal(const std::string & familyName,const std::shared_ptr<FontCollection> & fc)53     void registerFallbackInternal(const std::string& familyName,
54                                   const std::shared_ptr<FontCollection>& fc) {
55         mSystemFallbacks.insert(std::make_pair(familyName, fc));
56     }
57 
registerDefaultInternal(const std::shared_ptr<FontCollection> & fc)58     void registerDefaultInternal(const std::shared_ptr<FontCollection>& fc) {
59         mDefaultFallback = fc;
60     }
61 
62 private:
63     static SystemFonts& getInstance();
64 
65     // There is no mutex guard here since registerFallback is designed to be
66     // called only in Zygote.
67     std::map<std::string, std::shared_ptr<FontCollection>> mSystemFallbacks;
68     std::shared_ptr<FontCollection> mDefaultFallback;
69 };
70 
71 }  // namespace minikin
72 
73 #endif  // MINIKIN_SYSTEM_FONTS_H
74