1 /*
2 * Copyright (c) 2020-2021 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 "font/glyphs_manager.h"
17 #include "font/ui_font_builder.h"
18 #include "font/ui_font_cache_manager.h"
19 #include "gfx_utils/file.h"
20 #include "gfx_utils/graphic_log.h"
21 #include "securec.h"
22
23 namespace OHOS {
GlyphsManager()24 GlyphsManager::GlyphsManager() : fileType_(0) {}
25
~GlyphsManager()26 GlyphsManager::~GlyphsManager()
27 {
28 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
29 delete glyphsFiles_[i];
30 glyphsFiles_[i] = nullptr;
31 }
32 }
33
SetFile(const char * fontName,int32_t fp,uint32_t start,uint16_t fileType)34 int8_t GlyphsManager::SetFile(const char* fontName, int32_t fp, uint32_t start, uint16_t fileType)
35 {
36 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
37 if (glyphsFiles_[i]->IsSameFile(fontName)) {
38 GRAPHIC_LOGE("GlyphsManager::SetFile font name repeat");
39 return INVALID_RET_VALUE;
40 }
41 }
42
43 GlyphsFile* instance = new GlyphsFile();
44 if (instance == nullptr) {
45 GRAPHIC_LOGE("GlyphsManager::SetFile new failed");
46 return INVALID_RET_VALUE;
47 }
48
49 if (instance->SetFile(fontName, fp, start) != RET_VALUE_OK) {
50 delete instance;
51 instance = nullptr;
52 return INVALID_RET_VALUE;
53 }
54
55 glyphsFiles_.PushBack(instance);
56 fileType_ = fileType;
57 return RET_VALUE_OK;
58 }
59
GetFontVersion(const char * fontName,char * version,uint8_t len)60 int8_t GlyphsManager::GetFontVersion(const char* fontName, char* version, uint8_t len)
61 {
62 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
63 if (glyphsFiles_[i]->GetFontVersion(fontName, version, len) == RET_VALUE_OK) {
64 return RET_VALUE_OK;
65 }
66 }
67 return INVALID_RET_VALUE;
68 }
69
GetFontHeader(uint16_t fontId)70 const FontHeader* GlyphsManager::GetFontHeader(uint16_t fontId)
71 {
72 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
73 const FontHeader* tmp = glyphsFiles_[i]->GetFontHeader(fontId);
74 if (tmp != nullptr) {
75 return tmp;
76 }
77 }
78
79 return nullptr;
80 }
81
GetGlyphNodeFromFiles(uint32_t unicode,uint16_t fontId)82 const GlyphNode* GlyphsManager::GetGlyphNodeFromFiles(uint32_t unicode, uint16_t fontId)
83 {
84 int8_t ret = INVALID_RET_VALUE;
85 GlyphNode nodeInfo;
86 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
87 ret = glyphsFiles_[i]->GetNodeFromFile(unicode, fontId, nodeInfo);
88 if (ret == RET_VALUE_OK) {
89 nodeInfo.fontId = fontId;
90 break;
91 }
92 }
93
94 if (ret != RET_VALUE_OK) {
95 return nullptr;
96 }
97
98 GlyphCacheNode* cacheNode = UIFontCacheManager::GetInstance()->GetNodeCacheSpace(unicode, fontId);
99 if (cacheNode == nullptr) {
100 return nullptr;
101 }
102 cacheNode->node = nodeInfo;
103 cacheNode->cacheType = fileType_;
104
105 return &(cacheNode->node);
106 }
107
GetGlyphNode(uint32_t unicode,uint16_t fontId)108 const GlyphNode* GlyphsManager::GetGlyphNode(uint32_t unicode, uint16_t fontId)
109 {
110 GlyphCacheNode* cacheNode =
111 UIFontCacheManager::GetInstance()->GetNodeFromCache(unicode, fontId, GlyphCacheType::CACHE_TYPE_NONE);
112 if (cacheNode != nullptr) {
113 return &(cacheNode->node);
114 }
115
116 const GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNodeFromFiles(unicode, fontId));
117 if (node != nullptr) {
118 return node;
119 }
120 return nullptr;
121 }
122
GetFontHeight(uint16_t fontId)123 int16_t GlyphsManager::GetFontHeight(uint16_t fontId)
124 {
125 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
126 int16_t height = glyphsFiles_[i]->GetFontHeight(fontId);
127 if (height != INVALID_RET_VALUE) {
128 return height;
129 }
130 }
131
132 return INVALID_RET_VALUE;
133 }
134
GetFontWidth(uint32_t unicode,uint16_t fontId)135 int16_t GlyphsManager::GetFontWidth(uint32_t unicode, uint16_t fontId)
136 {
137 const GlyphNode* node = GetGlyphNode(unicode, fontId);
138 if (node == nullptr) {
139 return INVALID_RET_VALUE;
140 }
141 return node->advance;
142 }
143
GetBitmap(uint32_t unicode,BufferInfo & bufInfo,uint16_t fontId)144 int8_t GlyphsManager::GetBitmap(uint32_t unicode, BufferInfo& bufInfo, uint16_t fontId)
145 {
146 GlyphCacheNode* cacheNode = UIFontCacheManager::GetInstance()->GetNodeFromCache(unicode, fontId, fileType_);
147 if (cacheNode != nullptr && cacheNode->cacheType == fileType_) {
148 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
149 int8_t ret = glyphsFiles_[i]->GetBitmap(cacheNode->node, bufInfo);
150 if (ret == RET_VALUE_OK) {
151 return RET_VALUE_OK;
152 }
153 }
154 }
155
156 GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNodeFromFiles(unicode, fontId));
157 if (node == nullptr) {
158 GRAPHIC_LOGE("GlyphsManager::GetBitmap node not found");
159 return INVALID_RET_VALUE;
160 }
161
162 for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
163 int8_t ret = glyphsFiles_[i]->GetBitmap(*node, bufInfo);
164 if (ret == RET_VALUE_OK) {
165 return RET_VALUE_OK;
166 }
167 }
168 return INVALID_RET_VALUE;
169 }
170 } // namespace OHOS
171