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 "ui_test_font.h"
17 #include "common/screen.h"
18 #if ENABLE_VECTOR_FONT
19 #else
20 #include "common/ui_text_language.h"
21 #endif
22 #include "components/ui_label.h"
23 #include "font/ui_font.h"
24 #if ENABLE_MULTI_FONT
25 #include "font/ui_multi_font_manager.h"
26 #endif
27
28 namespace OHOS {
29 namespace {
30 const int16_t GAP = 5;
31 const int16_t TITLE_HEIGHT = 20;
32 const uint16_t LABEL_WIDTH = 400;
33 const uint16_t LABEL_HEIGHT = 50;
34 const uint16_t FONT_SIZE = 30;
35 const char* SOURCE_HAN_SANS_SC_REGULAR = "SourceHanSansSC-Regular.otf";
36 const char* ROBOTO_CONDENSED_REGULAR = "RobotoCondensed-Regular.ttf";
37 } // namespace
38
SetUp()39 void UITestFont::SetUp()
40 {
41 if (container_ == nullptr) {
42 container_ = new UIScrollView();
43 container_->SetThrowDrag(true);
44 container_->SetHorizontalScrollState(false);
45 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - LABEL_HEIGHT);
46 positionX_ = 50; // 50: init position x
47 positionY_ = 5; // 5: init position y
48 }
49 }
50
TearDown()51 void UITestFont::TearDown()
52 {
53 DeleteChildren(container_);
54 container_ = nullptr;
55 }
56
InnerTestTitle(const char * title)57 void UITestFont::InnerTestTitle(const char* title)
58 {
59 UILabel* titleLabel = new UILabel();
60 titleLabel->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, Screen::GetInstance().GetWidth(),
61 TITLE_LABEL_DEFAULT_HEIGHT);
62 #if ENABLE_VECTOR_FONT
63 titleLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, DEFAULT_VECTOR_FONT_SIZE);
64 #else
65 titleLabel->SetFontId(F_SOURCEHANSANSSC_REGULAR_18_4);
66 #endif
67 titleLabel->SetText(title);
68 container_->Add(titleLabel);
69 positionY_ += TITLE_HEIGHT + GAP;
70 }
71
GetTestView()72 const UIView* UITestFont::GetTestView()
73 {
74 UIKitFontTestDispaly001();
75 UIKitFontTestDispaly002();
76 UIKitFontTestDispaly003();
77 UIKitFontTestDispaly004();
78 UIKitFontTestDispaly005();
79 UIKitFontTestDispaly006();
80 UIKitFontTestDispaly007();
81 UIKitFontTestDispaly008();
82 UIKitFontTestDispaly009();
83 UIKitFontTestDispaly010();
84 UIKitFontTestBaseline001();
85 UIKitFontTestBaseline002();
86 UIKitFontTestLineHeight001();
87 UIKitFontTestLineHeight002();
88 #if ENABLE_MULTI_FONT
89 UIKitFontMultiLanguage001();
90 UIKitFontMultiLanguage002();
91 #endif
92 return container_;
93 }
UIKitFontTestDispaly001()94 void UITestFont::UIKitFontTestDispaly001()
95 {
96 if (container_ == nullptr) {
97 return;
98 }
99 InnerTestTitle(" Display single line Chinese");
100 UILabel* label = new UILabel();
101 label->SetPosition(positionX_, positionY_);
102 label->Resize(LABEL_WIDTH, LABEL_HEIGHT);
103 #if ENABLE_VECTOR_FONT
104 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
105 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
106 #else
107 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
108 #endif
109 label->SetText("轻量图形子系统");
110 container_->Add(label);
111 positionY_ += LABEL_HEIGHT + GAP;
112 }
113
UIKitFontTestDispaly002()114 void UITestFont::UIKitFontTestDispaly002()
115 {
116 if (container_ == nullptr) {
117 return;
118 }
119 InnerTestTitle(" Display multiline Chinese");
120 UILabel* label = new UILabel();
121 label->SetPosition(positionX_, positionY_);
122 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
123 #if ENABLE_VECTOR_FONT
124 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
125 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
126 #else
127 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
128 #endif
129 label->SetText(" 你好\n轻量图形子系统");
130 container_->Add(label);
131 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
132 }
133
134
UIKitFontTestDispaly003()135 void UITestFont::UIKitFontTestDispaly003()
136 {
137 if (container_ == nullptr) {
138 return;
139 }
140 InnerTestTitle(" Display single line English");
141 UILabel* label = new UILabel();
142 label->SetPosition(positionX_, positionY_);
143 label->Resize(LABEL_WIDTH, LABEL_HEIGHT);
144 #if ENABLE_VECTOR_FONT
145 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
146 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
147 #else
148 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
149 #endif
150 label->SetText("uikit test");
151 container_->Add(label);
152 positionY_ += LABEL_HEIGHT + GAP;
153 }
154
155
UIKitFontTestDispaly004()156 void UITestFont::UIKitFontTestDispaly004()
157 {
158 if (container_ == nullptr) {
159 return;
160 }
161 InnerTestTitle(" Display multiline English");
162 UILabel* label = new UILabel();
163 label->SetPosition(positionX_, positionY_);
164 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
165 #if ENABLE_VECTOR_FONT
166 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
167 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
168 #else
169 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
170 #endif
171 label->SetText(" Hello\n uikit");
172 container_->Add(label);
173 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
174 }
175
176
UIKitFontTestDispaly005()177 void UITestFont::UIKitFontTestDispaly005()
178 {
179 if (container_ == nullptr) {
180 return;
181 }
182 InnerTestTitle(" Display single line Chinese and English");
183 UILabel* label = new UILabel();
184 label->SetPosition(positionX_, positionY_);
185 label->Resize(LABEL_WIDTH, LABEL_HEIGHT);
186 #if ENABLE_VECTOR_FONT
187 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
188 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
189 #else
190 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
191 #endif
192 label->SetText("你好,轻量级图形 uikit");
193 container_->Add(label);
194 positionY_ += LABEL_HEIGHT + GAP;
195 }
196
UIKitFontTestDispaly006()197 void UITestFont::UIKitFontTestDispaly006()
198 {
199 if (container_ == nullptr) {
200 return;
201 }
202 InnerTestTitle(" Display multiline Chinese and English");
203 UILabel* label = new UILabel();
204 label->SetPosition(positionX_, positionY_);
205 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
206 #if ENABLE_VECTOR_FONT
207 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
208 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
209 #else
210 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
211 #endif
212 label->SetText("你好\n轻量级图形 uikit");
213 container_->Add(label);
214 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
215 }
216
217
UIKitFontTestDispaly007()218 void UITestFont::UIKitFontTestDispaly007()
219 {
220 if (container_ == nullptr) {
221 return;
222 }
223 InnerTestTitle(" Display single line text color");
224 UILabel* label = new UILabel();
225 label->SetPosition(positionX_, positionY_);
226 label->Resize(LABEL_WIDTH, LABEL_HEIGHT);
227 #if ENABLE_VECTOR_FONT
228 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
229 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
230 #else
231 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
232 #endif
233 label->SetStyle(STYLE_TEXT_COLOR, Color::Red().full);
234 label->SetText("你好,轻量级图形");
235 container_->Add(label);
236 positionY_ += LABEL_HEIGHT + GAP;
237 }
238
UIKitFontTestDispaly008()239 void UITestFont::UIKitFontTestDispaly008()
240 {
241 if (container_ == nullptr) {
242 return;
243 }
244 InnerTestTitle(" Display multiline text color");
245 UILabel* label = new UILabel();
246 label->SetPosition(positionX_, positionY_);
247 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
248 #if ENABLE_VECTOR_FONT
249 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
250 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
251 #else
252 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
253 #endif
254 label->SetStyle(STYLE_TEXT_COLOR, Color::Yellow().full);
255 label->SetText("你好\n 轻量级图形 uikit");
256 container_->Add(label);
257 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
258 }
259
260
UIKitFontTestDispaly009()261 void UITestFont::UIKitFontTestDispaly009()
262 {
263 if (container_ == nullptr) {
264 return;
265 }
266 InnerTestTitle(" Display multiline text color");
267 UILabel* label = new UILabel();
268 label->SetPosition(positionX_ + 100, positionY_); // 100 : offset x
269 label->Resize(LABEL_WIDTH, LABEL_HEIGHT);
270 label->SetLineBreakMode(UILabel::LineBreakMode::LINE_BREAK_ADAPT);
271 #if ENABLE_VECTOR_FONT
272 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
273 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
274 #else
275 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
276 #endif
277 label->Rotate(90, Vector2<float>{0, 0}); // 90 : angle
278 label->SetText("轻量级图形");
279 container_->Add(label);
280 positionY_ += LABEL_HEIGHT * 4 + GAP; // 4 : coefficient
281 }
282
283
UIKitFontTestDispaly010()284 void UITestFont::UIKitFontTestDispaly010()
285 {
286 if (container_ == nullptr) {
287 return;
288 }
289 InnerTestTitle(" Display multiline text color");
290 UILabel* label = new UILabel();
291 label->SetPosition(positionX_ + 100, positionY_); // 100 : offset x
292 label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT); // 2 : half
293 label->SetLineBreakMode(UILabel::LineBreakMode::LINE_BREAK_MARQUEE);
294 #if ENABLE_VECTOR_FONT
295 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
296 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
297 #else
298 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
299 #endif
300 label->Rotate(90, Vector2<float>{0, 0}); // 90 : angle
301 label->SetText("轻量级图形,轻量级图形,轻量级图形");
302 container_->Add(label);
303 positionY_ += LABEL_HEIGHT * 4 + GAP; // 4 : coefficient
304 }
305
306
UIKitFontTestBaseline001()307 void UITestFont::UIKitFontTestBaseline001()
308 {
309 if (container_ == nullptr) {
310 return;
311 }
312 InnerTestTitle("Font baseline alignment");
313 UILabel* label = new UILabel();
314 label->SetPosition(positionX_, positionY_);
315 label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT); // 2 : half
316 #if ENABLE_VECTOR_FONT
317 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
318 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
319 #else
320 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
321 #endif
322 label->SetText("hello, uikit");
323
324 UILabel* label2 = new UILabel();
325 label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half
326 label2->Resize(LABEL_WIDTH, LABEL_HEIGHT);
327 #if ENABLE_VECTOR_FONT
328 UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR);
329 label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE);
330 #else
331 label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4);
332 #endif
333 label2->SetText("hello, uikit");
334
335 container_->Add(label);
336 container_->Add(label2);
337 positionY_ += LABEL_HEIGHT + GAP;
338 }
339
UIKitFontTestBaseline002()340 void UITestFont::UIKitFontTestBaseline002()
341 {
342 if (container_ == nullptr) {
343 return;
344 }
345 InnerTestTitle(" Font baseline alignment");
346 UILabel* label = new UILabel();
347 label->SetPosition(positionX_, positionY_);
348 label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT); // 2 : half
349 #if ENABLE_VECTOR_FONT
350 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
351 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
352 #else
353 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
354 #endif
355 label->SetText("hello, uikit");
356 label->SetStyle(STYLE_LINE_HEIGHT, 30); // 30 : line height
357
358 UILabel* label2 = new UILabel();
359 label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half
360 label2->Resize(LABEL_WIDTH, LABEL_HEIGHT);
361 #if ENABLE_VECTOR_FONT
362 UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR);
363 label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE);
364 #else
365 label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4);
366 #endif
367 label2->SetText("hello, uikit");
368 label2->SetStyle(STYLE_LINE_HEIGHT, 30); // 30 : line height
369
370 container_->Add(label);
371 container_->Add(label2);
372 positionY_ += LABEL_HEIGHT + GAP;
373 }
374
UIKitFontTestLineHeight001()375 void UITestFont::UIKitFontTestLineHeight001()
376 {
377 if (container_ == nullptr) {
378 return;
379 }
380 InnerTestTitle(" Font lineheight alignment");
381 UILabel* label = new UILabel();
382 label->SetPosition(positionX_, positionY_);
383 label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half
384 #if ENABLE_VECTOR_FONT
385 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
386 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
387 #else
388 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
389 #endif
390 label->SetText("hello,\n uikit");
391
392 UILabel* label2 = new UILabel();
393 label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half
394 label2->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half
395 #if ENABLE_VECTOR_FONT
396 UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR);
397 label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE);
398 #else
399 label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4);
400 #endif
401 label2->SetText("hello,\n uikit");
402
403 container_->Add(label);
404 container_->Add(label2);
405 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
406 }
407
UIKitFontTestLineHeight002()408 void UITestFont::UIKitFontTestLineHeight002()
409 {
410 if (container_ == nullptr) {
411 return;
412 }
413 InnerTestTitle(" Font lineheight alignment");
414 UILabel* label = new UILabel();
415 label->SetPosition(positionX_, positionY_);
416 label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half
417 #if ENABLE_VECTOR_FONT
418 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
419 label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE);
420 #else
421 label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4);
422 #endif
423 label->SetText("hello,\n uikit");
424 label->SetStyle(STYLE_LINE_HEIGHT, 40); // 40 : line height
425
426 UILabel* label2 = new UILabel();
427 label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half
428 label2->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half
429 #if ENABLE_VECTOR_FONT
430 UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR);
431 label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE);
432 #else
433 label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4);
434 #endif
435 label2->SetText("hello,\n uikit");
436 label2->SetStyle(STYLE_LINE_HEIGHT, 40); // 40 : line height
437
438 container_->Add(label);
439 container_->Add(label2);
440 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
441 }
442
443 #if ENABLE_MULTI_FONT
UIKitFontMultiLanguage001()444 void UITestFont::UIKitFontMultiLanguage001()
445 {
446 if (container_ == nullptr) {
447 return;
448 }
449 #if ENABLE_VECTOR_FONT
450 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
451 UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR);
452 uint8_t findPath[] = {UIFont::GetInstance()->GetFontId(SOURCE_HAN_SANS_SC_REGULAR)};
453 UIMultiFontManager::GetInstance()->SetSearchFontList(UIFont::GetInstance()->GetFontId(ROBOTO_CONDENSED_REGULAR),
454 findPath, sizeof(findPath));
455 #else
456 uint8_t findPath[] = {F_SOURCEHANSANSSC_REGULAR_30_4};
457 UIMultiFontManager::GetInstance()->SetSearchFontList(F_ROBOTOCONDENSED_REGULAR_30_4, findPath, sizeof(findPath));
458 #endif
459 InnerTestTitle(" Display multilingual display");
460 UILabel* label = new UILabel();
461 label->SetPosition(positionX_, positionY_);
462 label->Resize(LABEL_WIDTH, LABEL_HEIGHT);
463 #if ENABLE_VECTOR_FONT
464 label->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE);
465 #else
466 label->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4);
467 #endif
468 label->SetText("Hello, HarmonyOS Lite GUI");
469 container_->Add(label);
470 positionY_ += LABEL_HEIGHT + GAP;
471 }
472
UIKitFontMultiLanguage002()473 void UITestFont::UIKitFontMultiLanguage002()
474 {
475 if (container_ == nullptr) {
476 return;
477 }
478 #if ENABLE_VECTOR_FONT
479 UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR);
480 UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR);
481 uint8_t findPath[] = {UIFont::GetInstance()->GetFontId(SOURCE_HAN_SANS_SC_REGULAR)};
482 UIMultiFontManager::GetInstance()->SetSearchFontList(UIFont::GetInstance()->GetFontId(ROBOTO_CONDENSED_REGULAR),
483 findPath, sizeof(findPath));
484 #else
485 uint8_t findPath[] = {F_SOURCEHANSANSSC_REGULAR_30_4};
486 UIMultiFontManager::GetInstance()->SetSearchFontList(F_ROBOTOCONDENSED_REGULAR_30_4, findPath, sizeof(findPath));
487 #endif
488 InnerTestTitle(" Display multilingual display");
489 UILabel* label = new UILabel();
490 label->SetPosition(positionX_, positionY_);
491 label->Resize(LABEL_WIDTH, LABEL_HEIGHT * 2); // 2 : double
492 #if ENABLE_VECTOR_FONT
493 label->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE);
494 #else
495 label->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4);
496 #endif
497 label->SetText("Hello\n HarmonyOS Lite GUI");
498 container_->Add(label);
499 positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double
500 }
501 #endif // ENABLE_MULTI_FONT
502 } // namespace OHOS
503