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/dynamic_file_font_provider.h"
17
18 #include "mock.h"
19 #include "texgine/utils/exlog.h"
20 #include "typeface.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace TextEngine {
25 #define SUCCESSED 0
26 #define FAILED 1
Create()27 std::shared_ptr<DynamicFileFontProvider> DynamicFileFontProvider::Create() noexcept(true)
28 {
29 return std::make_shared<DynamicFileFontProvider>();
30 }
31
LoadFont(const std::string & familyName,const std::string & path)32 int DynamicFileFontProvider::LoadFont(const std::string &familyName, const std::string &path) noexcept(true)
33 {
34 LOGEX_FUNC_LINE(DEBUG) << "loading font: '" << path << "'";
35 std::error_code ec;
36 auto ret = StdFilesystemExists(path, ec);
37 if (ec) {
38 LOGEX_FUNC_LINE(ERROR) << "open file failed: " << ec.message();
39 return FAILED;
40 }
41
42 if (!ret) {
43 LOGEX_FUNC_LINE(ERROR) << "file is not exists";
44 return FAILED;
45 }
46
47 MockIFStream ifs(path);
48 if (!ifs.StdFilesystemIsOpen()) {
49 LOGEX_FUNC_LINE(ERROR) << "file open failed";
50 return FAILED;
51 }
52
53 ifs.StdFilesystemSeekg(0, ifs.end);
54 if (!ifs.good()) {
55 LOGEX_FUNC_LINE(ERROR) << "seekg(0, ifs.end) failed!";
56 return FAILED;
57 }
58
59 auto size = ifs.StdFilesystemTellg();
60 if (ifs.fail()) {
61 LOGEX_FUNC_LINE(ERROR) << "tellg failed!";
62 return FAILED;
63 }
64
65 ifs.StdFilesystemSeekg(ifs.beg);
66 if (!ifs.good()) {
67 LOGEX_FUNC_LINE(ERROR) << "seekg(ifs.beg) failed!";
68 return FAILED;
69 }
70
71 std::unique_ptr<char[]> buf = std::make_unique<char[]>(size);
72 ifs.StdFilesystemRead(buf.get(), size);
73 if (!ifs.good()) {
74 LOGEX_FUNC_LINE(ERROR) << "read failed!";
75 return FAILED;
76 }
77
78 ifs.StdFilestystemClose();
79 return DynamicFontProvider::LoadFont(familyName, buf.get(), size);
80 }
81 } // namespace TextEngine
82 } // namespace Rosen
83 } // namespace OHOS
84