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         GRAPHIC_LOGE("GlyphsManager::SetFile() instance->SetFile() failed");
53         return INVALID_RET_VALUE;
54     }
55 
56     glyphsFiles_.PushBack(instance);
57     fileType_ = fileType;
58     return RET_VALUE_OK;
59 }
60 
GetFontVersion(const char * fontName,char * version,uint8_t len)61 int8_t GlyphsManager::GetFontVersion(const char* fontName, char* version, uint8_t len)
62 {
63     for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
64         if (glyphsFiles_[i]->GetFontVersion(fontName, version, len) == RET_VALUE_OK) {
65             return RET_VALUE_OK;
66         }
67     }
68     return INVALID_RET_VALUE;
69 }
70 
GetFontHeader(uint16_t fontId)71 const FontHeader* GlyphsManager::GetFontHeader(uint16_t fontId)
72 {
73     for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
74         const FontHeader* tmp = glyphsFiles_[i]->GetFontHeader(fontId);
75         if (tmp != nullptr) {
76             return tmp;
77         }
78     }
79 
80     return nullptr;
81 }
82 
GetGlyphNodeFromFiles(uint32_t unicode,uint16_t fontId)83 const GlyphNode* GlyphsManager::GetGlyphNodeFromFiles(uint32_t unicode, uint16_t fontId)
84 {
85     int8_t ret = INVALID_RET_VALUE;
86     GlyphNode nodeInfo;
87     for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
88         ret = glyphsFiles_[i]->GetNodeFromFile(unicode, fontId, nodeInfo);
89         if (ret == RET_VALUE_OK) {
90             nodeInfo.fontId = fontId;
91             break;
92         }
93     }
94 
95     if (ret != RET_VALUE_OK) {
96         return nullptr;
97     }
98 
99     GlyphCacheNode* cacheNode = UIFontCacheManager::GetInstance()->GetNodeCacheSpace(unicode, fontId);
100     if (cacheNode == nullptr) {
101         return nullptr;
102     }
103     cacheNode->node = nodeInfo;
104     cacheNode->cacheType = fileType_;
105 
106     return &(cacheNode->node);
107 }
108 
GetGlyphNode(uint32_t unicode,uint16_t fontId)109 const GlyphNode* GlyphsManager::GetGlyphNode(uint32_t unicode, uint16_t fontId)
110 {
111     GlyphCacheNode* cacheNode =
112         UIFontCacheManager::GetInstance()->GetNodeFromCache(unicode, fontId, GlyphCacheType::CACHE_TYPE_NONE);
113     if (cacheNode != nullptr) {
114         return &(cacheNode->node);
115     }
116 
117     const GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNodeFromFiles(unicode, fontId));
118     if (node != nullptr) {
119         return node;
120     }
121     return nullptr;
122 }
123 
GetFontHeight(uint16_t fontId)124 int16_t GlyphsManager::GetFontHeight(uint16_t fontId)
125 {
126     for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
127         int16_t height = glyphsFiles_[i]->GetFontHeight(fontId);
128         if (height != INVALID_RET_VALUE) {
129             return height;
130         }
131     }
132 
133     return INVALID_RET_VALUE;
134 }
135 
GetFontWidth(uint32_t unicode,uint16_t fontId)136 int16_t GlyphsManager::GetFontWidth(uint32_t unicode, uint16_t fontId)
137 {
138     const GlyphNode* node = GetGlyphNode(unicode, fontId);
139     if (node == nullptr) {
140         return INVALID_RET_VALUE;
141     }
142     return node->advance;
143 }
144 
GetBitmap(uint32_t unicode,BufferInfo & bufInfo,uint16_t fontId)145 int8_t GlyphsManager::GetBitmap(uint32_t unicode, BufferInfo& bufInfo, uint16_t fontId)
146 {
147     GlyphCacheNode* cacheNode = UIFontCacheManager::GetInstance()->GetNodeFromCache(unicode, fontId, fileType_);
148     if (cacheNode != nullptr && cacheNode->cacheType == fileType_) {
149         for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
150             int8_t ret = glyphsFiles_[i]->GetBitmap(cacheNode->node, bufInfo);
151             if (ret == RET_VALUE_OK) {
152                 return RET_VALUE_OK;
153             }
154         }
155     }
156 
157     GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNodeFromFiles(unicode, fontId));
158     if (node == nullptr) {
159         GRAPHIC_LOGE("GlyphsManager::GetBitmap node not found");
160         return INVALID_RET_VALUE;
161     }
162 
163     for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
164         int8_t ret = glyphsFiles_[i]->GetBitmap(*node, bufInfo);
165         if (ret == RET_VALUE_OK) {
166             return RET_VALUE_OK;
167         }
168     }
169     return INVALID_RET_VALUE;
170 }
171 } // namespace OHOS
172