1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 "texgine_font_manager.h"
17 
18 #include <mutex>
19 
20 #include "texgine_font_style_set.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace TextEngine {
TexgineFontManager()25 TexgineFontManager::TexgineFontManager() : fontMgr_(RSFontMgr::CreateDefaultFontMgr()) {}
26 
RefDefault()27 std::shared_ptr<TexgineFontManager> TexgineFontManager::RefDefault()
28 {
29     auto manager = std::make_shared<TexgineFontManager>();
30     manager->SetFontMgr(RSFontMgr::CreateDefaultFontMgr());
31     return manager;
32 }
33 
GetFontMgr() const34 std::shared_ptr<RSFontMgr> TexgineFontManager::GetFontMgr() const
35 {
36     return fontMgr_;
37 }
38 
SetFontMgr(const std::shared_ptr<RSFontMgr> mgr)39 void TexgineFontManager::SetFontMgr(const std::shared_ptr<RSFontMgr> mgr)
40 {
41     fontMgr_ = mgr;
42 }
43 
MatchFamilyStyleCharacter(const std::string & familyName,const TexgineFontStyle & style,const char * bcp47[],int bcp47Count,int32_t character)44 std::shared_ptr<TexgineTypeface> TexgineFontManager::MatchFamilyStyleCharacter(const std::string &familyName,
45     const TexgineFontStyle &style, const char* bcp47[], int bcp47Count, int32_t character)
46 {
47     if (fontMgr_ == nullptr || style.GetFontStyle() == nullptr) {
48         return nullptr;
49     }
50 
51     RSTypeface* tf = fontMgr_->MatchFamilyStyleCharacter(familyName.c_str(),
52         *style.GetFontStyle(), bcp47, bcp47Count, character);
53     return std::make_shared<TexgineTypeface>(tf);
54 }
55 
MatchFamily(const std::string & familyName)56 std::shared_ptr<TexgineFontStyleSet> TexgineFontManager::MatchFamily(const std::string &familyName)
57 {
58     if (fontMgr_ == nullptr) {
59         return nullptr;
60     }
61 
62     RSFontStyleSet* set = fontMgr_->MatchFamily(familyName.c_str());
63     return std::make_shared<TexgineFontStyleSet>(set);
64 }
65 
MatchFamilyStyle(const std::string & familyName,const TexgineFontStyle & style)66 std::shared_ptr<TexgineTypeface> TexgineFontManager::MatchFamilyStyle(const std::string &familyName,
67     const TexgineFontStyle &style)
68 {
69     if (fontMgr_ == nullptr) {
70         return nullptr;
71     }
72     RSTypeface* rsTypeface = nullptr;
73     if (familyName != "") {
74         rsTypeface = fontMgr_->MatchFamilyStyle(familyName.c_str(), *style.GetFontStyle());
75     } else {
76         rsTypeface = fontMgr_->MatchFamilyStyle(nullptr, *style.GetFontStyle());
77     }
78 
79     if (!rsTypeface) {
80         return nullptr;
81     }
82     std::shared_ptr<RSTypeface> typeface(rsTypeface);
83     return std::make_shared<TexgineTypeface>(typeface);
84 }
85 } // namespace TextEngine
86 } // namespace Rosen
87 } // namespace OHOS
88