1 /*
2 * Copyright (c) 2020-2022 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 "graphic_config.h"
17 #include "ui_test_spannable_string.h"
18
19 #include "common/screen.h"
20 #if ENABLE_VECTOR_FONT
21 #else
22 #include "common/ui_text_language.h"
23 #endif
24 #include "components/ui_label.h"
25 #include "font/ui_font.h"
26 #if ENABLE_MULTI_FONT
27 #include "font/ui_multi_font_manager.h"
28 #endif
29
30 namespace OHOS {
31 namespace {
32 const int16_t GAP = 5;
33 const int16_t TITLE_HEIGHT = 20;
34 const uint16_t LABEL_WIDTH = 400;
35 const uint16_t LABEL_HEIGHT = 50;
36 const uint16_t FONT_SIZE = 30;
37 const uint16_t FONT_EMOJI_SIZE = 100;
38 const char *SOURCE_HAN_SANS_SC_REGULAR = "SourceHanSansSC-Regular.otf";
39 const char *ROBOTO_CONDENSED_REGULAR = "RobotoCondensed-Regular.ttf";
40 const char *DEFAULT_EMOJI_FILENAME = "NotoColorEmojiCompat.ttf";
41 const char *Amiri_Regular = "../../../../../../../third_party/harfbuzz/perf/fonts/Amiri-Regular.ttf";
42 const char *Roboto_Regular = "../../../../../../../third_party/harfbuzz/perf/fonts/Roboto-Regular.ttf";
43 const char *Source_HWSC = "../../../../../../../foundation/window/window_window_manager_lite/test/"
44 "SourceHanSansHWSC-VF.ttf";
45 const char *Source_SC = "../../../../../../../foundation/window/window_window_manager_lite/test/"
46 "SourceHanSansSC-VF.ttf";
47
48 // foundation\window\window_window_manager_lite\test\sample_ui.cpp
49
50 #if ENABLE_MULTI_FONT
51 const int16_t F_HWEMOJI_REGULAR_30_32 = 99;
52 #endif
53 } // namespace
54
SetUp()55 void UITestSpannableString::SetUp()
56 {
57 if (container_ == nullptr) {
58 container_ = new UIScrollView();
59 container_->SetThrowDrag(true);
60 container_->SetHorizontalScrollState(false);
61 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
62 positionX_ = 50; // 50: init position x
63 positionY_ = 5; // 5: init position y
64 }
65 }
66
TearDown()67 void UITestSpannableString::TearDown()
68 {
69 DeleteChildren(container_);
70 container_ = nullptr;
71 }
72
InnerTestTitle(const char * title)73 void UITestSpannableString::InnerTestTitle(const char *title)
74 {
75 UILabel *titleLabel = new UILabel();
76 titleLabel->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, Screen::GetInstance().GetWidth(),
77 TITLE_LABEL_DEFAULT_HEIGHT);
78 #if ENABLE_VECTOR_FONT
79 titleLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, DEFAULT_VECTOR_FONT_SIZE);
80 #else
81 titleLabel->SetFontId(F_SOURCEHANSANSSC_REGULAR_18_4);
82 #endif
83 titleLabel->SetText(title);
84 container_->Add(titleLabel);
85 positionY_ += TITLE_HEIGHT + GAP;
86 }
87
GetTestView()88 const UIView *UITestSpannableString::GetTestView()
89 {
90 UIKitSpannableStringTestNormal001();
91 UIKitSpannableStringTestTextStyle001();
92 UIKitSpannableStringTestFontSize001();
93 UIKitSpannableStringTestFontSize002();
94 UIKitSpannableStringTestFontId001();
95 UIKitSpannableStringTestFontId002();
96 return container_;
97 }
98
UIKitSpannableStringTestNormal001()99 void UITestSpannableString::UIKitSpannableStringTestNormal001()
100 {
101 if (container_ == nullptr) {
102 return;
103 }
104 InnerTestTitle(" Display singleline Chinese");
105 UILabel *label = new UILabel();
106 label->SetPosition(positionX_, positionY_);
107 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
108 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
109 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
110 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
111 #else
112 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
113 #endif
114 label->SetText(" 轻量图形子系统");
115 container_->Add(label);
116 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
117 }
118
UIKitSpannableStringTestTextStyle001()119 void UITestSpannableString::UIKitSpannableStringTestTextStyle001()
120 {
121 if (container_ == nullptr) {
122 return;
123 }
124 InnerTestTitle(" Display textStyle ");
125 UILabel *label = new UILabel();
126 label->SetPosition(positionX_, positionY_);
127 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
128 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
129 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
130 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
131 #else
132 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
133 #endif
134 label->SetText("正常粗体斜体粗斜体");
135
136 SpannableString *ss = new SpannableString();
137 TextStyle style = TextStyle::TEXT_STYLE_ITALIC;
138 ss->SetTextStyle(style, 4, 6); // 4:left edge; 6:right edge;
139 TextStyle style_bold = TextStyle::TEXT_STYLE_BOLD;
140 ss->SetTextStyle(style_bold, 2, 4); // 2:left edge; 4:right edge;
141 label->SetText(ss);
142 delete ss;
143 ss = nullptr;
144 container_->Add(label);
145 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
146 }
147
UIKitSpannableStringTestFontSize001()148 void UITestSpannableString::UIKitSpannableStringTestFontSize001()
149 {
150 if (container_ == nullptr) {
151 return;
152 }
153 InnerTestTitle(" Display SetFontSize, shall be same with SetAbsoluteSizeSpan");
154 UILabel *label = new UILabel();
155 label->SetPosition(positionX_, positionY_);
156 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
157 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
158 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
159 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
160 #else
161 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
162 #endif
163 label->SetText("轻量图形子系统");
164 SpannableString *ss = new SpannableString();
165 ss->SetFontSize(38, 1, 3); // 38:fontsize; 1:left edge; 3:right edge;
166 ss->SetFontSize(18, 2, 4); // 18:fontsize; 2:left edge; 4:right edge;
167 ss->SetFontSize(24, 3, 5); // 24:fontsize; 3:left edge; 5:right edge;
168 ss->SetFontSize(14, 10, 12); // 14:fontsize; 10:left edge; 12:right edge;
169 label->SetText(ss);
170 container_->Add(label);
171 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
172 delete ss;
173 ss = nullptr;
174 }
175
UIKitSpannableStringTestFontSize002()176 void UITestSpannableString::UIKitSpannableStringTestFontSize002()
177 {
178 if (container_ == nullptr) {
179 return;
180 }
181 InnerTestTitle(" Display SetAbsoluteSizeSpan, shall be same with SetFontSize");
182 UILabel *label = new UILabel();
183 label->SetPosition(positionX_, positionY_);
184 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
185 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
186 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
187 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
188 #else
189 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
190 #endif
191 label->SetText("轻量图形子系统");
192 label->SetAbsoluteSizeSpan(1, 2, 38); // 1 : start index; 2 : end index; 38 absolute font size
193 label->SetAbsoluteSizeSpan(2, 3, 18); // 2 : start index; 3 : end index; 18 absolute font size
194 label->SetAbsoluteSizeSpan(3, 4, 24); // 3 : start index; 4 : end index; 24 absolute font size
195 label->SetAbsoluteSizeSpan(10, 11, 14); // 10 : start index; 11 : end index; 14 font size
196
197 container_->Add(label);
198 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
199 }
200
UIKitSpannableStringTestFontId001()201 void UITestSpannableString::UIKitSpannableStringTestFontId001()
202 {
203 if (container_ == nullptr) {
204 return;
205 }
206 InnerTestTitle(" Display SetFontId, show two fontId can be found in different labels");
207 UILabel *label = new UILabel();
208 label->SetPosition(positionX_, positionY_);
209 label->Resize(LABEL_WIDTH * 2, LABEL_HEIGHT * 2); // 2 : double
210 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
211 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
212 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
213 #else
214 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
215 #endif
216 UIFont::GetInstance()->RegisterFontInfo(Amiri_Regular);
217 UILabel *label_2 = new UILabel();
218 label_2->SetPosition(positionX_, positionY_);
219 label_2->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2: double
220 label_2->SetFont(Amiri_Regular, FONT_SIZE);
221 label_2->SetText("IJK4 IJK8 IJK12 注意不同字体");
222 container_->Add(label_2);
223 positionY_ += LABEL_HEIGHT * 1 + GAP;
224
225 UIFont::GetInstance()->RegisterFontInfo(Roboto_Regular);
226 UILabel *label_3 = new UILabel();
227 label_3->SetPosition(positionX_, positionY_);
228 label_3->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2: double
229 label_3->SetFont(Roboto_Regular, FONT_SIZE);
230 label_3->SetText("IJK4 IJK8 IJK12 注意不同字体");
231 container_->Add(label_3);
232 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2: double
233 }
234
UIKitSpannableStringTestFontId002()235 void UITestSpannableString::UIKitSpannableStringTestFontId002()
236 {
237 if (container_ == nullptr) {
238 return;
239 }
240 InnerTestTitle(" Display SetFontId, two fontIds in one label");
241 UILabel *label = new UILabel();
242 label->SetPosition(positionX_, positionY_);
243 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
244 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
245 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
246 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
247 #else
248 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
249 #endif
250 label->SetText("字体一 字体二 注意不同字体");
251 SpannableString *ss = new SpannableString();
252 UIFont::GetInstance()->RegisterFontInfo(Source_HWSC);
253 UIFont::GetInstance()->RegisterFontInfo(Source_SC);
254 const uint16_t id_hwsc = UIFont::GetInstance()->GetFontId(Source_HWSC);
255 const uint16_t id_sc = UIFont::GetInstance()->GetFontId(Source_SC);
256 uint16_t findPath[] = {id_hwsc};
257 UIMultiFontManager::GetInstance()->SetSearchFontList(id_sc, findPath, sizeof(findPath));
258 ss->SetFontId(id_hwsc, 0, 4); // 0:left edge; 4: right edge;
259 ss->SetFontId(id_sc, 4, 8); // 4:left edge; 8: right edge;
260 label->SetText(ss);
261 container_->Add(label);
262 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2: double
263 delete ss;
264 ss = nullptr;
265 }
266 } // namespace OHOS
267