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/ui_font.h"
17 #include "common/text.h"
18 #include "font/ui_font_cache.h"
19 #if ENABLE_VECTOR_FONT
20 #include "font/ui_font_vector.h"
21 #else
22 #include "font/ui_font_bitmap.h"
23 #endif
24 #include "graphic_config.h"
25 #if ENABLE_MULTI_FONT
26 #include "font/ui_multi_font_manager.h"
27 #endif
28
29 namespace OHOS {
30 bool UIFont::setFontAllocFlag_ = false;
UIFont()31 UIFont::UIFont() : instance_(nullptr), defaultInstance_(nullptr){};
32
~UIFont()33 UIFont::~UIFont(){};
34
GetInstance()35 UIFont* UIFont::GetInstance()
36 {
37 static UIFont instance;
38 #if ENABLE_VECTOR_FONT
39 if (instance.instance_ == nullptr) {
40 instance.defaultInstance_ = new UIFontVector();
41 instance.instance_ = instance.defaultInstance_;
42 setFontAllocFlag_ = true;
43 }
44 #else
45 if (instance.instance_ == nullptr) {
46 instance.defaultInstance_ = new UIFontBitmap();
47 instance.instance_ = instance.defaultInstance_;
48 setFontAllocFlag_ = true;
49 }
50 #endif
51 return &instance;
52 }
53
SetFont(BaseFont * font)54 void UIFont::SetFont(BaseFont* font)
55 {
56 if (font != nullptr) {
57 if (defaultInstance_ != nullptr && setFontAllocFlag_) {
58 delete defaultInstance_;
59 defaultInstance_ = nullptr;
60 setFontAllocFlag_ = false;
61 }
62 defaultInstance_ = font;
63 instance_ = font;
64 }
65 }
66
GetBitmap(uint32_t unicode,GlyphNode & glyphNode,uint8_t shapingFont)67 uint8_t* UIFont::GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint8_t shapingFont)
68 {
69 uint8_t* bitmap = nullptr;
70 uint8_t currentFontId = GetCurrentFontId();
71 #if ENABLE_MULTI_FONT
72 // shaping font is in search list, search shaping font first
73 if (shapingFont > 1) {
74 bitmap = instance_->GetBitmap(unicode, glyphNode, shapingFont);
75 SetCurrentFontId(currentFontId);
76 if (bitmap != nullptr) {
77 return bitmap;
78 }
79 }
80 #endif
81 bitmap = instance_->GetBitmap(unicode, glyphNode, currentFontId);
82 if (bitmap != nullptr) {
83 return bitmap;
84 }
85 #if ENABLE_MULTI_FONT
86 uint8_t* searchLists = nullptr;
87 int8_t listSize = UIMultiFontManager::GetInstance()->GetSearchFontList(currentFontId, &searchLists);
88 int8_t currentIndex = 0;
89 if ((searchLists == nullptr) || (listSize == 0)) {
90 return nullptr;
91 }
92 do {
93 SetCurrentFontId(searchLists[currentIndex], 0);
94 bitmap = instance_->GetBitmap(unicode, glyphNode, GetCurrentFontId());
95 if (bitmap != nullptr) {
96 SetCurrentFontId(currentFontId, 0);
97 return bitmap;
98 }
99 // switch to next search List
100 currentIndex++;
101 } while ((currentIndex < listSize) && (searchLists != nullptr));
102 SetCurrentFontId(currentFontId);
103 #endif
104 return nullptr;
105 }
106
GetWidth(uint32_t unicode,uint8_t shapingId)107 uint16_t UIFont::GetWidth(uint32_t unicode, uint8_t shapingId)
108 {
109 int16_t result;
110 uint8_t currentFontId = GetCurrentFontId();
111 #if ENABLE_MULTI_FONT
112 if (shapingId > 1) {
113 result = instance_->GetWidth(unicode, shapingId);
114 SetCurrentFontId(currentFontId);
115 if (result >= 0) {
116 return result;
117 }
118 }
119 #endif
120 result = instance_->GetWidth(unicode, currentFontId);
121 if (result >= 0) {
122 return result;
123 }
124
125 #if ENABLE_MULTI_FONT
126 uint8_t* searchLists = nullptr;
127 int8_t listSize = UIMultiFontManager::GetInstance()->GetSearchFontList(currentFontId, &searchLists);
128 if ((searchLists == nullptr) || (listSize == 0)) {
129 return 0;
130 }
131 int8_t currentIndex = 0;
132 do {
133 SetCurrentFontId(searchLists[currentIndex], 0);
134 result = instance_->GetWidth(unicode, GetCurrentFontId());
135 if (result >= 0) {
136 SetCurrentFontId(currentFontId, 0);
137 return result;
138 }
139 currentIndex++;
140 } while ((currentIndex < listSize) && (searchLists != nullptr));
141 SetCurrentFontId(currentFontId);
142 #endif
143 return 0;
144 }
145 } // namespace OHOS
146