• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
44 #ifdef USE_M133_SKIA
onCreateStyleSet(int index) const45 sk_sp<SkFontStyleSet> AssetFontManager::onCreateStyleSet(int index) const
46 #else
47 SkFontStyleSet* AssetFontManager::onCreateStyleSet(int index) const
48 #endif
49 {
50     SkASSERT(false);
51     return nullptr;
52 }
53 
54 #ifdef USE_M133_SKIA
onMatchFamily(const char name[]) const55 sk_sp<SkFontStyleSet> AssetFontManager::onMatchFamily(const char name[]) const
56 {
57     std::string familyName(name);
58     return sk_sp<SkFontStyleSet>(fontProvider_->MatchFamily(familyName));
59 }
60 #else
onMatchFamily(const char name[]) const61 SkFontStyleSet* AssetFontManager::onMatchFamily(const char name[]) const
62 {
63     std::string familyName(name);
64     return fontProvider_->MatchFamily(familyName);
65 }
66 #endif
67 
68 #ifdef USE_M133_SKIA
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const69 sk_sp<SkTypeface> AssetFontManager::onMatchFamilyStyle(const char familyName[], const SkFontStyle& style) const
70 {
71     sk_sp<SkFontStyleSet> fontStyleSet = sk_sp<SkFontStyleSet>(
72         fontProvider_->MatchFamily(std::string(familyName ? familyName : "")));
73     if (fontStyleSet == nullptr) {
74         return nullptr;
75     }
76     return fontStyleSet->matchStyle(style);
77 }
78 #else
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const79 SkTypeface* AssetFontManager::onMatchFamilyStyle(const char familyName[], const SkFontStyle& style) const
80 {
81     SkFontStyleSet* fontStyleSet = fontProvider_->MatchFamily(std::string(familyName ? familyName : ""));
82     if (fontStyleSet == nullptr)
83         return nullptr;
84     return fontStyleSet->matchStyle(style);
85 }
86 #endif
87 
88 #ifdef USE_M133_SKIA
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle &,const char * bcp47[],int bcp47Count,SkUnichar character) const89 sk_sp<SkTypeface> AssetFontManager::onMatchFamilyStyleCharacter(
90 #else
91 SkTypeface* AssetFontManager::onMatchFamilyStyleCharacter(
92 #endif
93     const char familyName[], const SkFontStyle&, const char* bcp47[], int bcp47Count, SkUnichar character) const
94 {
95     return nullptr;
96 }
97 
onMakeFromData(sk_sp<SkData>,int ttcIndex) const98 sk_sp<SkTypeface> AssetFontManager::onMakeFromData(sk_sp<SkData>, int ttcIndex) const
99 {
100     SkASSERT(false);
101     return nullptr;
102 }
103 
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int ttcIndex) const104 sk_sp<SkTypeface> AssetFontManager::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const
105 {
106     SkASSERT(false);
107     return nullptr;
108 }
109 
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &) const110 sk_sp<SkTypeface> AssetFontManager::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const
111 {
112     SkASSERT(false);
113     return nullptr;
114 }
115 
onMakeFromFile(const char path[],int ttcIndex) const116 sk_sp<SkTypeface> AssetFontManager::onMakeFromFile(const char path[], int ttcIndex) const
117 {
118     SkASSERT(false);
119     return nullptr;
120 }
121 
onLegacyMakeTypeface(const char familyName[],SkFontStyle) const122 sk_sp<SkTypeface> AssetFontManager::onLegacyMakeTypeface(const char familyName[], SkFontStyle) const
123 {
124     return nullptr;
125 }
126 
TestFontManager(std::unique_ptr<FontAssetProvider> fontProvider,std::vector<std::string> familyNames)127 TestFontManager::TestFontManager(
128     std::unique_ptr<FontAssetProvider> fontProvider, std::vector<std::string> familyNames)
129     : AssetFontManager(std::move(fontProvider)), testFontFamilyNames_(familyNames)
130 {}
131 
132 TestFontManager::~TestFontManager() = default;
133 
134 #ifdef USE_M133_SKIA
onMatchFamily(const char familyName[]) const135 sk_sp<SkFontStyleSet> TestFontManager::onMatchFamily(const char familyName[]) const
136 #else
137 SkFontStyleSet* TestFontManager::onMatchFamily(const char familyName[]) const
138 #endif
139 {
140     std::string requestedName(familyName);
141     std::string sanitizedName = testFontFamilyNames_[0];
142     for (const std::string& family : testFontFamilyNames_) {
143         if (requestedName == family) {
144             sanitizedName = family;
145         }
146     }
147     return AssetFontManager::onMatchFamily(sanitizedName.c_str());
148 }
149 
ParseInstallFontConfig(const std::string & configPath,std::vector<std::string> & fontPathVec)150 int DynamicFontManager::ParseInstallFontConfig(const std::string& configPath,
151     std::vector<std::string>& fontPathVec)
152 {
153     OHOS::Rosen::TextEngine::FontConfigJson fontConfigJson;
154     int ret = fontConfigJson.ParseInstallConfig(configPath.c_str(), fontPathVec);
155     if (ret != SUCCESSED) {
156         TEXT_LOGE_LIMIT3_MIN("Failed to parse json config file, ret = %d", ret);
157         return ERROR_PARSE_CONFIG_FAILED;
158     }
159     return SUCCESSED;
160 }
161 } // namespace txt
162