summaryrefslogtreecommitdiff
path: root/include/minikin/SystemFonts.h
blob: 4ef27057cac0b444faca5d0916f8f9bfbc45dfba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * 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 <map>
#include <memory>
#include <mutex>
#include <string>

#include "minikin/FontCollection.h"
#include "minikin/U16StringPiece.h"

namespace minikin {

// Provides a system font mapping.
class SystemFonts {
public:
    static std::shared_ptr<FontCollection> findFontCollection(const std::string& familyName) {
        return getInstance().findFontCollectionInternal(familyName);
    }

    static void registerFallback(const std::string& familyName,
                                 const std::shared_ptr<FontCollection>& fc) {
        return getInstance().registerFallbackInternal(familyName, fc);
    }

    static void registerDefault(const std::shared_ptr<FontCollection>& fc) {
        return getInstance().registerDefaultInternal(fc);
    }

    using FontMapDeleter = std::function<void()>;

    static void addFontMap(std::shared_ptr<FontCollection>&& 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<void(const std::vector<std::shared_ptr<Font>>&)> func) {
        return getInstance().getFontSetInternal(func);
    }

protected:
    // Visible for testing purposes.
    SystemFonts() {}
    virtual ~SystemFonts() {}

    std::shared_ptr<FontCollection> findFontCollectionInternal(const std::string& familyName);
    void registerFallbackInternal(const std::string& familyName,
                                  const std::shared_ptr<FontCollection>& fc) {
        std::lock_guard<std::mutex> lock(mMutex);
        mSystemFallbacks[familyName] = fc;
    }

    void registerDefaultInternal(const std::shared_ptr<FontCollection>& fc) {
        std::lock_guard<std::mutex> lock(mMutex);
        mDefaultFallback = fc;
    }

    void addFontMapInternal(std::shared_ptr<FontCollection>&& collections) {
        std::lock_guard<std::mutex> lock(mMutex);
        mCollections.emplace_back(std::move(collections));
    }

    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;
};

}  // namespace minikin

#endif  // MINIKIN_SYSTEM_FONTS_H