1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ROSEN_MODULES_SPTEXT_TYPEFACE_FONT_ASSET_PROVIDER_H
17 #define ROSEN_MODULES_SPTEXT_TYPEFACE_FONT_ASSET_PROVIDER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 #include <mutex>
23 
24 #include "include/core/SkFontMgr.h"
25 #include "txt/font_asset_provider.h"
26 #include "utils.h"
27 
28 namespace txt {
29 class TypefaceFontStyleSet : public SkFontStyleSet {
30 public:
31     TypefaceFontStyleSet();
32 
33     ~TypefaceFontStyleSet() override;
34 
35     void registerTypeface(sk_sp<SkTypeface> typeface);
36 
37     void unregisterTypefaces();
38 
39     int count() override;
40 
41     void getStyle(int index, SkFontStyle* style, SkString* name) override;
42 
43     SkTypeface* createTypeface(int index) override;
44 
45     SkTypeface* matchStyle(const SkFontStyle& pattern) override;
46 
47 private:
48     std::vector<sk_sp<SkTypeface>> typefaces_;
49 
50     DISALLOW_COPY_AND_ASSIGN(TypefaceFontStyleSet);
51 };
52 
53 class TypefaceFontAssetProvider : public FontAssetProvider {
54 public:
55     TypefaceFontAssetProvider();
56     ~TypefaceFontAssetProvider() override;
57 
58     void RegisterTypeface(sk_sp<SkTypeface> typeface);
59 
60     void RegisterTypeface(sk_sp<SkTypeface> typeface, std::string familyNameAlias);
61 
62     size_t GetFamilyCount() const override;
63 
64     std::string GetFamilyName(int index) const override;
65 
66     SkFontStyleSet* MatchFamily(const std::string& familyName) override;
67 
68 private:
69     std::unordered_map<std::string, sk_sp<TypefaceFontStyleSet>> registeredFamilies_;
70     std::vector<std::string> familyNames_;
71     mutable std::mutex assetMutex_;
72 
73     DISALLOW_COPY_AND_ASSIGN(TypefaceFontAssetProvider);
74 };
75 } // namespace txt
76 
77 #endif // ROSEN_MODULES_SPTEXT_TYPEFACE_FONT_ASSET_PROVIDER_H
78