1 /*
2 * Copyright (c) 2021-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 "core/components/font/rosen_font_collection.h"
17
18 #include "txt/src/minikin/FontFamily.h"
19 #include "txt/src/minikin/FontLanguageListCache.h"
20 #include "include/core/SkTypeface.h"
21 #include "base/i18n/localization.h"
22 #include "base/log/ace_trace.h"
23 #include "base/log/log.h"
24 #include "base/utils/system_properties.h"
25 #include "base/utils/utils.h"
26 #include "core/common/ace_engine.h"
27 #include "core/components_ng/render/drawing.h"
28 #include "rosen_text/properties/font_collection_txt.h"
29
30 namespace OHOS::Ace {
31
32 RosenFontCollection RosenFontCollection::instance;
33
GetFontCollection()34 std::shared_ptr<txt::FontCollection> RosenFontCollection::GetFontCollection()
35 {
36 std::call_once(fontFlag_, [this]() {
37 auto rosenCollection = RSFontCollection::GetInstance(false);
38 auto collectionTxtBase = rosenCollection->GetFontCollection();
39 auto collectionTxt = std::static_pointer_cast<rosen::FontCollectionTxt>(collectionTxtBase);
40 if (collectionTxt) {
41 fontCollection_ = collectionTxt->GetFontCollection();
42 dynamicFontManager_ = collectionTxt->GetDynamicFontManager();
43 } else {
44 LOGE("Fail to get FontFollectionTxt!");
45 }
46 });
47 return fontCollection_;
48 }
49
GetDynamicFontManager()50 sk_sp<txt::DynamicFontManager> RosenFontCollection::GetDynamicFontManager()
51 {
52 return dynamicFontManager_;
53 }
54
LoadFontFromList(const uint8_t * fontData,size_t length,std::string familyName)55 void RosenFontCollection::LoadFontFromList(const uint8_t* fontData, size_t length, std::string familyName)
56 {
57 std::call_once(fontFlag_, [this]() {
58 auto rosenCollection = RSFontCollection::GetInstance(false);
59 auto collectionTxtBase = rosenCollection->GetFontCollection();
60 auto collectionTxt = std::static_pointer_cast<rosen::FontCollectionTxt>(collectionTxtBase);
61 if (collectionTxt) {
62 fontCollection_ = collectionTxt->GetFontCollection();
63 dynamicFontManager_ = collectionTxt->GetDynamicFontManager();
64 }
65 });
66
67 auto it = std::find(families_.begin(), families_.end(), familyName);
68 if (it != families_.end()) {
69 return;
70 }
71
72 families_.emplace_back(familyName);
73
74 if (fontCollection_) {
75 std::unique_ptr<SkStreamAsset> font_stream = std::make_unique<SkMemoryStream>(fontData, length, true);
76 sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(std::move(font_stream));
77 txt::TypefaceFontAssetProvider& font_provider = dynamicFontManager_->font_provider();
78 if (familyName.empty()) {
79 font_provider.RegisterTypeface(typeface);
80 } else {
81 font_provider.RegisterTypeface(typeface, familyName);
82 }
83 fontCollection_->ClearFontFamilyCache();
84 }
85 }
86
GetInstance()87 RosenFontCollection& RosenFontCollection::GetInstance()
88 {
89 return instance;
90 }
91
VaryFontCollectionWithFontWeightScale(float fontWeightScale)92 void RosenFontCollection::VaryFontCollectionWithFontWeightScale(float fontWeightScale)
93 {
94 if (LessOrEqual(fontWeightScale, 0.0)) {
95 return;
96 }
97
98 if (fontCollection_) {
99 fontCollection_->VaryFontCollectionWithFontWeightScale(fontWeightScale);
100 }
101 }
102
LoadSystemFont()103 void RosenFontCollection::LoadSystemFont()
104 {
105 ACE_FUNCTION_TRACE();
106
107 if (fontCollection_) {
108 fontCollection_->LoadSystemFont();
109 }
110 }
111
SetIsZawgyiMyanmar(bool isZawgyiMyanmar)112 void RosenFontCollection::SetIsZawgyiMyanmar(bool isZawgyiMyanmar)
113 {
114 ACE_FUNCTION_TRACE();
115
116 if (isZawgyiMyanmar_ == isZawgyiMyanmar) {
117 return;
118 }
119 isZawgyiMyanmar_ = isZawgyiMyanmar;
120
121 if (fontCollection_) {
122 fontCollection_->SetIsZawgyiMyanmar(isZawgyiMyanmar);
123 }
124
125 AceEngine::Get().NotifyContainers([](const RefPtr<Container>& container) {
126 if (container) {
127 container->NotifyFontNodes();
128 }
129 });
130 }
131
132 } // namespace OHOS::Ace
133