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 #include "txt/asset_font_manager.h"
17 
18 #include <memory>
19 
20 #include "font_config.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypeface.h"
23 #include "utils/text_log.h"
24 
25 namespace txt {
AssetFontManager(std::unique_ptr<FontAssetProvider> fontProvider)26 AssetFontManager::AssetFontManager(std::unique_ptr<FontAssetProvider> fontProvider)
27     : fontProvider_(std::move(fontProvider))
28 {
29     SkASSERT(fontProvider_ != nullptr);
30 }
31 
32 AssetFontManager::~AssetFontManager() = default;
33 
onCountFamilies() const34 int AssetFontManager::onCountFamilies() const
35 {
36     return fontProvider_->GetFamilyCount();
37 }
38 
onGetFamilyName(int index,SkString * familyName) const39 void AssetFontManager::onGetFamilyName(int index, SkString* familyName) const
40 {
41     familyName->set(fontProvider_->GetFamilyName(index).c_str());
42 }
43 
onCreateStyleSet(int index) const44 SkFontStyleSet* AssetFontManager::onCreateStyleSet(int index) const
45 {
46     SkASSERT(false);
47     return nullptr;
48 }
49 
onMatchFamily(const char name[]) const50 SkFontStyleSet* AssetFontManager::onMatchFamily(const char name[]) const
51 {
52     std::string familyName(name);
53     return fontProvider_->MatchFamily(familyName);
54 }
55 
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const56 SkTypeface* AssetFontManager::onMatchFamilyStyle(const char familyName[], const SkFontStyle& style) const
57 {
58     SkFontStyleSet* fontStyleSet = fontProvider_->MatchFamily(std::string(familyName ? familyName : ""));
59     if (fontStyleSet == nullptr)
60         return nullptr;
61     return fontStyleSet->matchStyle(style);
62 }
63 
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle &,const char * bcp47[],int bcp47Count,SkUnichar character) const64 SkTypeface* AssetFontManager::onMatchFamilyStyleCharacter(
65     const char familyName[], const SkFontStyle&, const char* bcp47[], int bcp47Count, SkUnichar character) const
66 {
67     return nullptr;
68 }
69 
onMakeFromData(sk_sp<SkData>,int ttcIndex) const70 sk_sp<SkTypeface> AssetFontManager::onMakeFromData(sk_sp<SkData>, int ttcIndex) const
71 {
72     SkASSERT(false);
73     return nullptr;
74 }
75 
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int ttcIndex) const76 sk_sp<SkTypeface> AssetFontManager::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const
77 {
78     SkASSERT(false);
79     return nullptr;
80 }
81 
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &) const82 sk_sp<SkTypeface> AssetFontManager::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const
83 {
84     SkASSERT(false);
85     return nullptr;
86 }
87 
onMakeFromFile(const char path[],int ttcIndex) const88 sk_sp<SkTypeface> AssetFontManager::onMakeFromFile(const char path[], int ttcIndex) const
89 {
90     SkASSERT(false);
91     return nullptr;
92 }
93 
onLegacyMakeTypeface(const char familyName[],SkFontStyle) const94 sk_sp<SkTypeface> AssetFontManager::onLegacyMakeTypeface(const char familyName[], SkFontStyle) const
95 {
96     return nullptr;
97 }
98 
TestFontManager(std::unique_ptr<FontAssetProvider> fontProvider,std::vector<std::string> familyNames)99 TestFontManager::TestFontManager(
100     std::unique_ptr<FontAssetProvider> fontProvider, std::vector<std::string> familyNames)
101     : AssetFontManager(std::move(fontProvider)), testFontFamilyNames_(familyNames)
102 {}
103 
104 TestFontManager::~TestFontManager() = default;
105 
onMatchFamily(const char familyName[]) const106 SkFontStyleSet* TestFontManager::onMatchFamily(const char familyName[]) const
107 {
108     std::string requestedName(familyName);
109     std::string sanitizedName = testFontFamilyNames_[0];
110     for (const std::string& family : testFontFamilyNames_) {
111         if (requestedName == family) {
112             sanitizedName = family;
113         }
114     }
115     return AssetFontManager::onMatchFamily(sanitizedName.c_str());
116 }
117 
ParseInstallFontConfig(const std::string & configPath,std::vector<std::string> & fontPathVec)118 int DynamicFontManager::ParseInstallFontConfig(const std::string& configPath,
119     std::vector<std::string>& fontPathVec)
120 {
121     OHOS::Rosen::TextEngine::FontConfigJson fontConfigJson;
122     int ret = fontConfigJson.ParseInstallConfig(configPath.c_str(), fontPathVec);
123     if (ret != SUCCESSED) {
124         TEXT_LOGE_LIMIT3_MIN("Failed to parse json config file, ret = %d", ret);
125         return ERROR_PARSE_CONFIG_FAILED;
126     }
127     return SUCCESSED;
128 }
129 } // namespace txt
130