1 /*
2 * Copyright (c) 2021-2022 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/flutter_font_loader.h"
17
18 #include "flutter/lib/ui/text/font_collection.h"
19
20 #include "base/network/download_manager.h"
21 #include "core/components/font/flutter_font_collection.h"
22 #include "core/pipeline/base/flutter_render_context.h"
23
24 namespace OHOS::Ace {
25
FlutterFontLoader(const std::string & familyName,const std::string & familySrc)26 FlutterFontLoader::FlutterFontLoader(const std::string& familyName, const std::string& familySrc)
27 : FontLoader(familyName, familySrc) {}
28
AddFont(const RefPtr<PipelineBase> & context)29 void FlutterFontLoader::AddFont(const RefPtr<PipelineBase>& context)
30 {
31 if (familySrc_.empty()) {
32 return;
33 }
34
35 if (familySrc_.substr(0, 4) == FONT_SRC_NETWORK) {
36 // Get font from NetWork.
37 LoadFromNetwork(context);
38 } else {
39 // Get font from asset.
40 LoadFromAsset(context);
41 }
42 }
43
LoadFromNetwork(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineBase> & context)44 void FlutterFontLoader::LoadFromNetwork(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineBase>& context)
45 {
46 auto weakContext = AceType::WeakClaim(AceType::RawPtr(context));
47 context->GetTaskExecutor()->PostTask([weak = AceType::WeakClaim(this), weakContext] {
48 auto fontLoader = weak.Upgrade();
49 auto context = weakContext.Upgrade();
50 if (!fontLoader || !context) {
51 return;
52 }
53 std::vector<uint8_t> fontData;
54 if (!DownloadManager::GetInstance().Download(fontLoader->familySrc_, fontData) || fontData.empty()) {
55 return;
56 }
57 context->GetTaskExecutor()->PostTask([fontData, weak] {
58 auto fontLoader = weak.Upgrade();
59 if (!fontLoader) {
60 return;
61 }
62 // Load font.
63 FlutterFontCollection::GetInstance().LoadFontFromList(
64 fontData.data(), fontData.size(), fontLoader->familyName_);
65 fontLoader->isLoaded_ = true;
66
67 // When font is already loaded, notify all which used this font.
68 for (const auto& [node, callback] : fontLoader->callbacks_) {
69 if (callback) {
70 callback();
71 }
72 }
73 fontLoader->callbacks_.clear();
74 if (fontLoader->variationChanged_) {
75 fontLoader->variationChanged_();
76 }
77 }, TaskExecutor::TaskType::UI);
78 }, TaskExecutor::TaskType::BACKGROUND);
79 }
80
LoadFromAsset(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineBase> & context)81 void FlutterFontLoader::LoadFromAsset(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineBase>& context)
82 {
83 auto weakContext = AceType::WeakClaim(AceType::RawPtr(context));
84 context->GetTaskExecutor()->PostTask([weak = AceType::WeakClaim(this), weakContext] {
85 auto fontLoader = weak.Upgrade();
86 auto context = weakContext.Upgrade();
87 if (!fontLoader || !context) {
88 return;
89 }
90 auto assetManager = context->GetAssetManager();
91 if (!assetManager) {
92 LOGE("No asset manager!");
93 return;
94 }
95 std::string assetSrc(fontLoader->familySrc_);
96 if (assetSrc[0] == '/') {
97 assetSrc = assetSrc.substr(1); // get the asset src without '/'.
98 } else if (assetSrc[0] == '.' && assetSrc.size() > 2 && assetSrc[1] == '/') {
99 assetSrc = assetSrc.substr(2); // get the asset src without './'.
100 }
101 auto assetData = assetManager->GetAsset(assetSrc);
102 if (!assetData) {
103 LOGE("No asset data!");
104 return;
105 }
106
107 context->GetTaskExecutor()->PostTask([assetData, weak] {
108 auto fontLoader = weak.Upgrade();
109 if (!fontLoader) {
110 return;
111 }
112 // Load font.
113 FlutterFontCollection::GetInstance().LoadFontFromList(
114 assetData->GetData(), assetData->GetSize(), fontLoader->familyName_);
115 fontLoader->isLoaded_ = true;
116
117 for (const auto& [node, callback] : fontLoader->callbacks_) {
118 if (callback) {
119 callback();
120 }
121 }
122 fontLoader->callbacks_.clear();
123 if (fontLoader->variationChanged_) {
124 fontLoader->variationChanged_();
125 }
126 }, TaskExecutor::TaskType::UI);
127 }, TaskExecutor::TaskType::BACKGROUND);
128 }
129
130 } // namespace OHOS::Ace
131