[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Typesetter.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <limits>
23
24 #include <dali-toolkit-test-suite-utils.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/devel-api/text/bitmap-font.h>
27 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
28 #include <dali-toolkit/internal/text/controller/text-controller.h>
29 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
30 #include <dali-toolkit/internal/text/rendering/view-model.h>
31 #include <dali/devel-api/text-abstraction/bitmap-font.h>
32 #include <toolkit-environment-variable.h>
33 #include <toolkit-text-utils.h>
34
35 using namespace Dali;
36 using namespace Toolkit;
37 using namespace Text;
38
39 namespace
40 {
41 const std::string     DEFAULT_FONT_DIR("/resources/fonts");
42 const PointSize26Dot6 EMOJI_FONT_SIZE = 3840u; // 60 * 64
43
44 constexpr auto DALI_RENDERED_GLYPH_COMPRESS_POLICY = "DALI_RENDERED_GLYPH_COMPRESS_POLICY";
45 } // namespace
46
47 int UtcDaliTextTypesetter(void)
48 {
49   tet_infoline(" UtcDaliTextTypesetter");
50   ToolkitTestApplication application;
51
52   // Creates a text controller.
53   ControllerPtr controller = Controller::New();
54
55   // Tests the rendering controller has been created.
56   TypesetterPtr typesetter = Typesetter::New(controller->GetTextModel());
57   DALI_TEST_CHECK(typesetter);
58
59   tet_result(TET_PASS);
60   END_TEST;
61 }
62
63 int UtcDaliTextTypesetterGetViewModel(void)
64 {
65   tet_infoline(" UtcDaliTextTypesetter");
66   ToolkitTestApplication application;
67
68   // Creates a text controller.
69   ControllerPtr controller = Controller::New();
70
71   // Tests the rendering controller has been created.
72   TypesetterPtr typesetter = Typesetter::New(controller->GetTextModel());
73   DALI_TEST_CHECK(typesetter);
74
75   // Tests the view model has been created.
76   ViewModel* model = typesetter->GetViewModel();
77   DALI_TEST_CHECK(NULL != model);
78
79   tet_result(TET_PASS);
80   END_TEST;
81 }
82
83 int UtcDaliTextRenderingControllerRenderRGBA(void)
84 {
85   tet_infoline(" UtcDaliTextRenderingControllerRenderRGBA");
86   ToolkitTestApplication application;
87
88   // Load some fonts.
89   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
90
91   char*             pathNamePtr = get_current_dir_name();
92   const std::string pathName(pathNamePtr);
93   free(pathNamePtr);
94
95   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/BreezeColorEmoji.ttf", EMOJI_FONT_SIZE);
96   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
97
98   // Creates a text controller.
99   ControllerPtr controller = Controller::New();
100
101   // Configures the text controller similarly to the text-label.
102   ConfigureTextLabel(controller);
103
104   // Sets the text.
105   controller->SetMarkupProcessorEnabled(true);
106   controller->SetText("<font family='TizenSansRegular'>Hello world </font><font family='BreezeColorEmoji'>\xF0\x9F\x98\x81</font>");
107
108   // Creates the text's model and relais-out the text.
109   const Size relayoutSize(120.f, 60.f);
110   controller->Relayout(relayoutSize);
111
112   // Tests the rendering controller has been created.
113   TypesetterPtr renderingController = Typesetter::New(controller->GetTextModel());
114   DALI_TEST_CHECK(renderingController);
115
116   // Renders the text and creates the final bitmap.
117   PixelData bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
118   DALI_TEST_CHECK(bitmap);
119
120   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
121   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
122   DALI_TEST_EQUALS(Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION);
123
124   // Changes vertical alignment.
125   controller->SetVerticalAlignment(Text::VerticalAlignment::CENTER);
126   controller->Relayout(relayoutSize);
127
128   // Renders the text and creates the final bitmap.
129   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
130   DALI_TEST_CHECK(bitmap);
131
132   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
133   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
134   DALI_TEST_EQUALS(Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION);
135
136   controller->SetVerticalAlignment(Text::VerticalAlignment::BOTTOM);
137   controller->Relayout(relayoutSize);
138
139   // Renders the text and creates the final bitmap.
140   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
141   DALI_TEST_CHECK(bitmap);
142
143   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
144   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
145   DALI_TEST_EQUALS(Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION);
146
147   tet_result(TET_PASS);
148   END_TEST;
149 }
150
151 int UtcDaliTextRenderingControllerRenderLuminance(void)
152 {
153   tet_infoline(" UtcDaliTextRenderingControllerRenderLuminance");
154   ToolkitTestApplication application;
155
156   // Load some fonts.
157   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
158
159   char*             pathNamePtr = get_current_dir_name();
160   const std::string pathName(pathNamePtr);
161   free(pathNamePtr);
162
163   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/BreezeColorEmoji.ttf", EMOJI_FONT_SIZE);
164   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
165
166   // Creates a text controller.
167   ControllerPtr controller = Controller::New();
168
169   // Configures the text controller similarly to the text-label.
170   ConfigureTextLabel(controller);
171
172   // Sets the text.
173   controller->SetMarkupProcessorEnabled(true);
174   controller->SetText("<font family='TizenSansRegular'>Hello world </font><font family='BreezeColorEmoji'>\xF0\x9F\x98\x81</font>");
175
176   // Creates the text's model and relais-out the text.
177   const Size relayoutSize(120.f, 60.f);
178   controller->Relayout(relayoutSize);
179
180   // Tests the rendering controller has been created.
181   TypesetterPtr renderingController = Typesetter::New(controller->GetTextModel());
182   DALI_TEST_CHECK(renderingController);
183
184   // Renders the text and creates the final bitmap.
185   PixelData bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT, Typesetter::RENDER_TEXT_AND_STYLES, false, Pixel::L8);
186   DALI_TEST_CHECK(bitmap);
187
188   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
189   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
190   DALI_TEST_EQUALS(Pixel::L8, bitmap.GetPixelFormat(), TEST_LOCATION);
191
192   // Changes vertical alignment.
193   controller->SetVerticalAlignment(Text::VerticalAlignment::CENTER);
194   controller->Relayout(relayoutSize);
195
196   // Renders the text and creates the final bitmap.
197   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT, Typesetter::RENDER_TEXT_AND_STYLES, false, Pixel::L8);
198   DALI_TEST_CHECK(bitmap);
199
200   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
201   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
202   DALI_TEST_EQUALS(Pixel::L8, bitmap.GetPixelFormat(), TEST_LOCATION);
203
204   controller->SetVerticalAlignment(Text::VerticalAlignment::BOTTOM);
205   controller->Relayout(relayoutSize);
206
207   // Renders the text and creates the final bitmap.
208   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT, Typesetter::RENDER_TEXT_AND_STYLES, false, Pixel::L8);
209   DALI_TEST_CHECK(bitmap);
210
211   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
212   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
213   DALI_TEST_EQUALS(Pixel::L8, bitmap.GetPixelFormat(), TEST_LOCATION);
214
215   tet_result(TET_PASS);
216
217   END_TEST;
218 }
219
220 int UtcDaliTextRenderingControllerRenderWithCompressRGBA(void)
221 {
222   EnvironmentVariable::SetTestEnvironmentVariable(DALI_RENDERED_GLYPH_COMPRESS_POLICY, "m");
223
224   tet_infoline(" UtcDaliTextRenderingControllerRenderWithCompressRGBA");
225   ToolkitTestApplication application;
226
227   // Load some fonts.
228   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
229
230   char*             pathNamePtr = get_current_dir_name();
231   const std::string pathName(pathNamePtr);
232   free(pathNamePtr);
233
234   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/BreezeColorEmoji.ttf", EMOJI_FONT_SIZE);
235   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
236
237   // Creates a text controller.
238   ControllerPtr controller = Controller::New();
239
240   // Configures the text controller similarly to the text-label.
241   ConfigureTextLabel(controller);
242
243   // Sets the text.
244   controller->SetMarkupProcessorEnabled(true);
245   controller->SetText("<font family='TizenSansRegular'>Hello world </font><font family='BreezeColorEmoji'>\xF0\x9F\x98\x81</font>");
246
247   // Creates the text's model and relais-out the text.
248   const Size relayoutSize(120.f, 60.f);
249   controller->Relayout(relayoutSize);
250
251   // Tests the rendering controller has been created.
252   TypesetterPtr renderingController = Typesetter::New(controller->GetTextModel());
253   DALI_TEST_CHECK(renderingController);
254
255   // Renders the text and creates the final bitmap.
256   PixelData bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
257   DALI_TEST_CHECK(bitmap);
258
259   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
260   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
261   DALI_TEST_EQUALS(Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION);
262
263   // Changes vertical alignment.
264   controller->SetVerticalAlignment(Text::VerticalAlignment::CENTER);
265   controller->Relayout(relayoutSize);
266
267   // Renders the text and creates the final bitmap.
268   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
269   DALI_TEST_CHECK(bitmap);
270
271   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
272   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
273   DALI_TEST_EQUALS(Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION);
274
275   controller->SetVerticalAlignment(Text::VerticalAlignment::BOTTOM);
276   controller->Relayout(relayoutSize);
277
278   // Renders the text and creates the final bitmap.
279   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
280   DALI_TEST_CHECK(bitmap);
281
282   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
283   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
284   DALI_TEST_EQUALS(Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION);
285
286   tet_result(TET_PASS);
287
288   END_TEST;
289 }
290
291 int UtcDaliTextRenderingControllerRenderWithCompressLuminance(void)
292 {
293   EnvironmentVariable::SetTestEnvironmentVariable(DALI_RENDERED_GLYPH_COMPRESS_POLICY, "m");
294
295   tet_infoline(" UtcDaliTextRenderingControllerRenderWithCompressLuminance");
296   ToolkitTestApplication application;
297
298   // Load some fonts.
299   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
300
301   char*             pathNamePtr = get_current_dir_name();
302   const std::string pathName(pathNamePtr);
303   free(pathNamePtr);
304
305   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/BreezeColorEmoji.ttf", EMOJI_FONT_SIZE);
306   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
307
308   // Creates a text controller.
309   ControllerPtr controller = Controller::New();
310
311   // Configures the text controller similarly to the text-label.
312   ConfigureTextLabel(controller);
313
314   // Sets the text.
315   controller->SetMarkupProcessorEnabled(true);
316   controller->SetText("<font family='TizenSansRegular'>Hello world </font><font family='BreezeColorEmoji'>\xF0\x9F\x98\x81</font>");
317
318   // Creates the text's model and relais-out the text.
319   const Size relayoutSize(120.f, 60.f);
320   controller->Relayout(relayoutSize);
321
322   // Tests the rendering controller has been created.
323   TypesetterPtr renderingController = Typesetter::New(controller->GetTextModel());
324   DALI_TEST_CHECK(renderingController);
325
326   // Renders the text and creates the final bitmap.
327   PixelData bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT, Typesetter::RENDER_TEXT_AND_STYLES, false, Pixel::L8);
328   DALI_TEST_CHECK(bitmap);
329
330   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
331   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
332   DALI_TEST_EQUALS(Pixel::L8, bitmap.GetPixelFormat(), TEST_LOCATION);
333
334   // Changes vertical alignment.
335   controller->SetVerticalAlignment(Text::VerticalAlignment::CENTER);
336   controller->Relayout(relayoutSize);
337
338   // Renders the text and creates the final bitmap.
339   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT, Typesetter::RENDER_TEXT_AND_STYLES, false, Pixel::L8);
340   DALI_TEST_CHECK(bitmap);
341
342   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
343   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
344   DALI_TEST_EQUALS(Pixel::L8, bitmap.GetPixelFormat(), TEST_LOCATION);
345
346   controller->SetVerticalAlignment(Text::VerticalAlignment::BOTTOM);
347   controller->Relayout(relayoutSize);
348
349   // Renders the text and creates the final bitmap.
350   bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT, Typesetter::RENDER_TEXT_AND_STYLES, false, Pixel::L8);
351   DALI_TEST_CHECK(bitmap);
352
353   DALI_TEST_EQUALS(120u, bitmap.GetWidth(), TEST_LOCATION);
354   DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
355   DALI_TEST_EQUALS(Pixel::L8, bitmap.GetPixelFormat(), TEST_LOCATION);
356
357   tet_result(TET_PASS);
358
359   END_TEST;
360 }
361
362 int UtcDaliTextTypesetterVerticalLineAlignment(void)
363 {
364   tet_infoline(" UtcDaliTextTypesetter");
365   ToolkitTestApplication application;
366
367   // Creates a text controller.
368   ControllerPtr controller = Controller::New();
369
370   // Configures the text controller similarly to the text-label.
371   ConfigureTextLabel(controller);
372
373   // Sets the text.
374   controller->SetMarkupProcessorEnabled(true);
375   controller->SetText("<font family='TizenSansRegular'>Hello world</font>");
376
377   // Creates the text's model and relais-out the text.
378   const Size relayoutSize(120.f, 60.f);
379   controller->Relayout(relayoutSize);
380
381   // Tests the rendering controller has been created.
382   TypesetterPtr renderingController = Typesetter::New(controller->GetTextModel());
383   DALI_TEST_CHECK(renderingController);
384
385   {
386     controller->SetVerticalLineAlignment(Dali::Toolkit::DevelText::VerticalLineAlignment::TOP);
387     controller->Relayout(relayoutSize);
388
389     // Renders the text and creates the final bitmap.
390     auto bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
391     DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
392   }
393
394   {
395     controller->SetVerticalLineAlignment(Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE);
396     controller->Relayout(relayoutSize);
397
398     // Renders the text and creates the final bitmap.
399     auto bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
400     DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
401   }
402
403   {
404     controller->SetVerticalLineAlignment(Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM);
405     controller->Relayout(relayoutSize);
406
407     // Renders the text and creates the final bitmap.
408     auto bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
409     DALI_TEST_EQUALS(60u, bitmap.GetHeight(), TEST_LOCATION);
410   }
411
412   tet_result(TET_PASS);
413   END_TEST;
414 }
415
416 int UtcDaliTextTypesetterBitmapFont(void)
417 {
418   tet_infoline("UtcDaliTextTypesetterBitmapFont ");
419   ToolkitTestApplication application;
420
421   DevelText::BitmapFontDescription fontDescription;
422   fontDescription.name               = "Digits";
423   fontDescription.underlinePosition  = 0.f;
424   fontDescription.underlineThickness = 0.f;
425   fontDescription.isColorFont        = true;
426
427   fontDescription.glyphs.push_back({TEST_RESOURCE_DIR "/fonts/bitmap/u0031.png", "0", 34.f, 0.f});
428   fontDescription.glyphs.push_back({TEST_RESOURCE_DIR "/fonts/bitmap/u0032.png", "1", 34.f, 0.f});
429
430   TextAbstraction::BitmapFont bitmapFont;
431   DevelText::CreateBitmapFont(fontDescription, bitmapFont);
432
433   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
434   fontClient.GetFontId(bitmapFont);
435
436   // Creates a text controller.
437   ControllerPtr controller = Controller::New();
438
439   // Configures the text controller similarly to the text-label.
440   ConfigureTextLabel(controller);
441
442   // Sets the text.
443   controller->SetMarkupProcessorEnabled(true);
444   controller->SetText("<font family='Digits'><color 'value'='red'>0</color></font>");
445
446   // Creates the text's model and relais-out the text.
447   const Size relayoutSize(31.f, 34.f);
448   controller->Relayout(relayoutSize);
449
450   // Tests the rendering controller has been created.
451   TypesetterPtr renderingController = Typesetter::New(controller->GetTextModel());
452   DALI_TEST_CHECK(renderingController);
453
454   controller->Relayout(relayoutSize);
455
456   // Renders the text and creates the final bitmap.
457   auto bitmap = renderingController->Render(relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT);
458
459   DALI_TEST_EQUALS(31u, bitmap.GetWidth(), TEST_LOCATION);
460   DALI_TEST_EQUALS(34u, bitmap.GetHeight(), TEST_LOCATION);
461
462   tet_result(TET_PASS);
463   END_TEST;
464 }