• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_font_provider.h"
17 
18 #include <iostream>
19 #include <mutex>
20 
21 #include "dynamic_font_style_set.h"
22 #include "texgine_exception.h"
23 #include "texgine_stream.h"
24 #include "texgine/utils/exlog.h"
25 #include "typeface.h"
26 #include "variant_font_style_set.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace TextEngine {
31 #define PARAMETERERROR 1
32 #define APIERROR 2
33 
Create()34 std::shared_ptr<DynamicFontProvider> DynamicFontProvider::Create() noexcept(true)
35 {
36     return std::make_shared<DynamicFontProvider>();
37 }
38 
LoadFont(const std::string & familyName,const void * data,size_t datalen)39 int DynamicFontProvider::LoadFont(const std::string &familyName, const void *data, size_t datalen) noexcept(true)
40 {
41     if (data == nullptr) {
42         LOGEX_FUNC_LINE(ERROR) << "data is nullptr!";
43         return PARAMETERERROR;
44     }
45 
46     if (datalen == 0) {
47         LOGEX_FUNC_LINE(ERROR) << "datalen is 0!";
48         return PARAMETERERROR;
49     }
50 
51     auto stream = TexgineMemoryStream::MakeCopy(data, datalen);
52     if (stream == nullptr) {
53         LOGEX_FUNC_LINE(ERROR) << "stream is nullptr!";
54         return APIERROR;
55     }
56 
57     auto texgineTypeface = TexgineTypeface::MakeFromStream(std::move(stream));
58     if (texgineTypeface == nullptr) {
59         LOGEX_FUNC_LINE(ERROR) << "texgineTypeface is nullptr!";
60         return APIERROR;
61     }
62 
63     auto typeface = std::make_unique<Typeface>(texgineTypeface);
64     LOGEX_FUNC_LINE_DEBUG() << "load font name:" << familyName;
65     auto dfss = std::make_shared<DynamicFontStyleSet>(std::move(typeface));
66     fontStyleSetMap_[familyName] = std::make_shared<VariantFontStyleSet>(dfss);
67     return 0;
68 }
69 
MatchFamily(const std::string & familyName)70 std::shared_ptr<VariantFontStyleSet> DynamicFontProvider::MatchFamily(const std::string &familyName) noexcept(true)
71 {
72     for (auto &[name, fss] : fontStyleSetMap_) {
73         if (familyName == name) {
74             return fss;
75         }
76     }
77     return nullptr;
78 }
79 } // namespace TextEngine
80 } // namespace Rosen
81 } // namespace OHOS
82