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_(SkTypeface::MakeDefault())
24 {
25 }
26
TexgineTypeface(SkTypeface * tf)27 TexgineTypeface::TexgineTypeface(SkTypeface *tf) : TexgineTypeface(sk_sp<SkTypeface>(tf))
28 {
29 if (tf) {
30 tf->ref();
31 }
32 }
33
TexgineTypeface(sk_sp<SkTypeface> typeface)34 TexgineTypeface::TexgineTypeface(sk_sp<SkTypeface> typeface): typeface_(typeface)
35 {
36 }
37
TexgineTypeface(void * context)38 TexgineTypeface::TexgineTypeface(void *context)
39 {
40 auto stf = reinterpret_cast<SkTypeface *>(context);
41 typeface_ = sk_sp<SkTypeface>(stf);
42 if (stf) {
43 stf->ref();
44 }
45 }
46
GetTypeface() const47 sk_sp<SkTypeface> TexgineTypeface::GetTypeface() const
48 {
49 return typeface_;
50 }
51
GetTableSize(uint32_t tag) const52 size_t TexgineTypeface::GetTableSize(uint32_t tag) const
53 {
54 if (typeface_ == nullptr) {
55 return 0;
56 }
57 return typeface_->getTableSize(tag);
58 }
59
GetTableData(uint32_t tag,size_t offset,size_t length,void * data) const60 size_t TexgineTypeface::GetTableData(uint32_t tag, size_t offset, size_t length, void *data) const
61 {
62 if (typeface_ == nullptr) {
63 return 0;
64 }
65 return typeface_->getTableData(tag, offset, length, data);
66 }
67
GetUnitsPerEm() const68 int TexgineTypeface::GetUnitsPerEm() const
69 {
70 if (typeface_ == nullptr) {
71 return 0;
72 }
73 return typeface_->getUnitsPerEm();
74 }
75
MakeFromStream(std::unique_ptr<TexgineMemoryStream> stream,int index)76 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromStream(
77 std::unique_ptr<TexgineMemoryStream> stream, int index)
78 {
79 if (stream == nullptr) {
80 return nullptr;
81 }
82 auto skTypeface = SkTypeface::MakeFromStream(stream->GetStream());
83 return std::make_shared<TexgineTypeface>(skTypeface);
84 }
85
MakeFromFile(const std::string & path,int index)86 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromFile(const std::string &path, int index)
87 {
88 auto st = SkTypeface::MakeFromFile(path.c_str(), index);
89 return std::make_shared<TexgineTypeface>(st);
90 }
91
GetFamilyName(TexgineString * name) const92 void TexgineTypeface::GetFamilyName(TexgineString *name) const
93 {
94 if (typeface_ == nullptr || name == nullptr) {
95 return;
96 }
97 typeface_->getFamilyName(name->GetString());
98 }
99
GetFontStyle() const100 std::shared_ptr<TexgineFontStyle> TexgineTypeface::GetFontStyle() const
101 {
102 if (typeface_ == nullptr) {
103 return nullptr;
104 }
105
106 auto style = typeface_->fontStyle();
107 auto texgineFontStyle = std::make_shared<TexgineFontStyle>();
108 texgineFontStyle->SetStyle(style);
109 return texgineFontStyle;
110 }
111 } // namespace TextEngine
112 } // namespace Rosen
113 } // namespace OHOS
114