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_typeface.h"
17 
18 #include "src/ports/SkFontMgr_custom.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace TextEngine {
TexgineTypeface()23 TexgineTypeface::TexgineTypeface(): typeface_(RSTypeface::MakeDefault()) {}
24 
TexgineTypeface(std::shared_ptr<RSTypeface> typeface)25 TexgineTypeface::TexgineTypeface(std::shared_ptr<RSTypeface> typeface): typeface_(typeface) {}
26 
TexgineTypeface(void * context)27 TexgineTypeface::TexgineTypeface(void *context) : typeface_(
28     std::shared_ptr<RSTypeface>{reinterpret_cast<RSTypeface *>(context), [](auto p) {}}) {}
29 
GetTypeface() const30 std::shared_ptr<RSTypeface> TexgineTypeface::GetTypeface() const
31 {
32     return typeface_;
33 }
34 
GetTableSize(uint32_t tag) const35 size_t TexgineTypeface::GetTableSize(uint32_t tag) const
36 {
37     if (typeface_ == nullptr) {
38         return 0;
39     }
40     return typeface_->GetTableSize(tag);
41 }
42 
GetTableData(uint32_t tag,size_t offset,size_t length,void * data) const43 size_t TexgineTypeface::GetTableData(uint32_t tag, size_t offset, size_t length, void *data) const
44 {
45     if (typeface_ == nullptr) {
46         return 0;
47     }
48     return typeface_->GetTableData(tag, offset, length, data);
49 }
50 
GetUnitsPerEm() const51 int TexgineTypeface::GetUnitsPerEm() const
52 {
53     if (typeface_ == nullptr) {
54         return 0;
55     }
56     return typeface_->GetUnitsPerEm();
57 }
58 
MakeFromStream(std::unique_ptr<TexgineMemoryStream> stream,int index)59 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromStream(
60     std::unique_ptr<TexgineMemoryStream> stream, int index)
61 {
62     if (stream == nullptr) {
63         return nullptr;
64     }
65     auto skTypeface = RSTypeface::MakeFromStream(stream->GetStream());
66     return std::make_shared<TexgineTypeface>(skTypeface);
67 }
68 
MakeFromFile(const std::string & path,int index)69 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromFile(const std::string &path, int index)
70 {
71     auto st = RSTypeface::MakeFromFile(path.c_str(), index);
72     return std::make_shared<TexgineTypeface>(st);
73 }
74 
GetFamilyName(TexgineString * name) const75 void TexgineTypeface::GetFamilyName(TexgineString *name) const
76 {
77     if (typeface_ == nullptr || name == nullptr) {
78         return;
79     }
80     *(name->GetString()) = typeface_->GetFamilyName();
81 }
82 
GetFontStyle() const83 std::shared_ptr<TexgineFontStyle> TexgineTypeface::GetFontStyle() const
84 {
85     if (typeface_ == nullptr) {
86         return nullptr;
87     }
88     auto style = typeface_->GetFontStyle();
89     auto texgineFontStyle = std::make_shared<TexgineFontStyle>();
90     texgineFontStyle->SetStyle(style);
91     return texgineFontStyle;
92 }
93 
FontStyleDetection()94 size_t TexgineTypeface::FontStyleDetection()
95 {
96     return static_cast<size_t>(typeface_->GetFontStyle().GetSlant());
97 }
98 
InputOriginalStyle(bool primitivism)99 void TexgineTypeface::InputOriginalStyle(bool primitivism)
100 {
101     rawInformation_ = primitivism;
102 }
103 
DetectRawInformation() const104 bool TexgineTypeface::DetectRawInformation() const
105 {
106     return rawInformation_;
107 }
108 } // namespace TextEngine
109 } // namespace Rosen
110 } // namespace OHOS
111