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_ASSET_FONT_MANAGER_H
17 #define ROSEN_MODULES_SPTEXT_ASSET_FONT_MANAGER_H
18 
19 #include <memory>
20 #include <utility>
21 
22 #include "utils.h"
23 #include "include/core/SkFontMgr.h"
24 #include "include/core/SkStream.h"
25 #include "txt/font_asset_provider.h"
26 #include "txt/typeface_font_asset_provider.h"
27 
28 namespace txt {
29 class AssetFontManager : public SkFontMgr {
30 public:
31     explicit AssetFontManager(std::unique_ptr<FontAssetProvider> fontProvider);
32 
33     ~AssetFontManager() override;
34 
35 protected:
36     SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
37 
38     std::unique_ptr<FontAssetProvider> fontProvider_;
39 
40 private:
41     int onCountFamilies() const override;
42 
43     void onGetFamilyName(int index, SkString* familyName) const override;
44 
45     SkFontStyleSet* onCreateStyleSet(int index) const override;
46 
47     SkTypeface* onMatchFamilyStyle(const char familyName[], const SkFontStyle&) const override;
48 
49     SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, const char* bcp47[],
50         int bcp47Count, SkUnichar character) const override;
51 
52     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override;
53 
54     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
55 
56     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override;
57 
58     sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
59 
60     sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const override;
61 
62     DISALLOW_COPY_AND_ASSIGN(AssetFontManager);
63 };
64 
65 class DynamicFontManager : public AssetFontManager {
66 public:
DynamicFontManager()67     DynamicFontManager() : AssetFontManager(
68         std::make_unique<TypefaceFontAssetProvider>()) {}
69 
font_provider()70     TypefaceFontAssetProvider& font_provider() const
71     {
72         return static_cast<TypefaceFontAssetProvider&>(*fontProvider_);
73     }
74     int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec);
75 };
76 
77 class TestFontManager : public AssetFontManager {
78 public:
79     TestFontManager(std::unique_ptr<FontAssetProvider> fontProvider, std::vector<std::string> familyNames);
80 
81     ~TestFontManager() override;
82 
83 private:
84     std::vector<std::string> testFontFamilyNames_;
85 
86     SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
87 
88     DISALLOW_COPY_AND_ASSIGN(TestFontManager);
89 };
90 } // namespace txt
91 
92 #endif // ROSEN_MODULES_SPTEXT_ASSET_FONT_MANAGER_H
93