NPatch rendering optimization
[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 <limits>
22 #include <unistd.h>
23
24 #include <dali-toolkit-test-suite-utils.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <toolkit-text-utils.h>
27 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
28 #include <dali-toolkit/internal/text/rendering/view-model.h>
29 #include <dali-toolkit/internal/text/text-controller.h>
30 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
31 #include <dali/devel-api/text-abstraction/bitmap-font.h>
32 #include <dali-toolkit/devel-api/text/bitmap-font.h>
33
34 using namespace Dali;
35 using namespace Toolkit;
36 using namespace Text;
37
38 namespace
39 {
40 const std::string DEFAULT_FONT_DIR( "/resources/fonts" );
41 const PointSize26Dot6 EMOJI_FONT_SIZE = 3840u; // 60 * 64
42 } // namespace
43
44 int UtcDaliTextTypesetter(void)
45 {
46   tet_infoline(" UtcDaliTextTypesetter");
47   ToolkitTestApplication application;
48
49   // Creates a text controller.
50   ControllerPtr controller = Controller::New();
51
52   // Tests the rendering controller has been created.
53   TypesetterPtr typesetter = Typesetter::New( controller->GetTextModel() );
54   DALI_TEST_CHECK( typesetter );
55
56   tet_result(TET_PASS);
57   END_TEST;
58 }
59
60 int UtcDaliTextTypesetterGetViewModel(void)
61 {
62   tet_infoline(" UtcDaliTextTypesetter");
63   ToolkitTestApplication application;
64
65   // Creates a text controller.
66   ControllerPtr controller = Controller::New();
67
68   // Tests the rendering controller has been created.
69   TypesetterPtr typesetter = Typesetter::New( controller->GetTextModel() );
70   DALI_TEST_CHECK( typesetter );
71
72   // Tests the view model has been created.
73   ViewModel* model = typesetter->GetViewModel();
74   DALI_TEST_CHECK( NULL != model );
75
76   tet_result(TET_PASS);
77   END_TEST;
78 }
79
80 int UtcDaliTextRenderingControllerRender(void)
81 {
82   tet_infoline(" UtcDaliTextRenderingControllerRender");
83   ToolkitTestApplication application;
84
85   // Load some fonts.
86   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
87
88   char* pathNamePtr = get_current_dir_name();
89   const std::string pathName( pathNamePtr );
90   free( pathNamePtr );
91
92   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/BreezeColorEmoji.ttf", EMOJI_FONT_SIZE );
93   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf" );
94
95   // Creates a text controller.
96   ControllerPtr controller = Controller::New();
97
98   // Configures the text controller similarly to the text-label.
99   ConfigureTextLabel( controller );
100
101   // Sets the text.
102   controller->SetMarkupProcessorEnabled( true );
103   controller->SetText( "<font family='TizenSansRegular'>Hello world </font><font family='BreezeColorEmoji'>\xF0\x9F\x98\x81</font>" );
104
105   // Creates the text's model and relais-out the text.
106   const Size relayoutSize( 120.f, 60.f );
107   controller->Relayout( relayoutSize );
108
109   // Tests the rendering controller has been created.
110   TypesetterPtr renderingController = Typesetter::New( controller->GetTextModel() );
111   DALI_TEST_CHECK( renderingController );
112
113   // Renders the text and creates the final bitmap.
114   PixelData bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
115   DALI_TEST_CHECK( bitmap );
116
117   DALI_TEST_EQUALS( 120u, bitmap.GetWidth(), TEST_LOCATION );
118   DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
119   DALI_TEST_EQUALS( Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION );
120
121   // Changes vertical alignment.
122   controller->SetVerticalAlignment( Text::VerticalAlignment::CENTER );
123   controller->Relayout( relayoutSize );
124
125   // Renders the text and creates the final bitmap.
126   bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
127   DALI_TEST_CHECK( bitmap );
128
129   DALI_TEST_EQUALS( 120u, bitmap.GetWidth(), TEST_LOCATION );
130   DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
131   DALI_TEST_EQUALS( Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION );
132
133   controller->SetVerticalAlignment( Text::VerticalAlignment::BOTTOM );
134   controller->Relayout( relayoutSize );
135
136   // Renders the text and creates the final bitmap.
137   bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
138   DALI_TEST_CHECK( bitmap );
139
140   DALI_TEST_EQUALS( 120u, bitmap.GetWidth(), TEST_LOCATION );
141   DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
142   DALI_TEST_EQUALS( Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION );
143
144   tet_result(TET_PASS);
145   END_TEST;
146 }
147
148 int UtcDaliTextTypesetterVerticalLineAlignment(void)
149 {
150   tet_infoline(" UtcDaliTextTypesetter");
151   ToolkitTestApplication application;
152
153   // Creates a text controller.
154   ControllerPtr controller = Controller::New();
155
156   // Configures the text controller similarly to the text-label.
157   ConfigureTextLabel( controller );
158
159   // Sets the text.
160   controller->SetMarkupProcessorEnabled( true );
161   controller->SetText( "<font family='TizenSansRegular'>Hello world</font>" );
162
163   // Creates the text's model and relais-out the text.
164   const Size relayoutSize( 120.f, 60.f );
165   controller->Relayout( relayoutSize );
166
167   // Tests the rendering controller has been created.
168   TypesetterPtr renderingController = Typesetter::New( controller->GetTextModel() );
169   DALI_TEST_CHECK( renderingController );
170
171   {
172     controller->SetVerticalLineAlignment( Dali::Toolkit::DevelText::VerticalLineAlignment::TOP );
173     controller->Relayout( relayoutSize );
174
175     // Renders the text and creates the final bitmap.
176     auto bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
177     DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
178   }
179
180   {
181     controller->SetVerticalLineAlignment( Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE );
182     controller->Relayout( relayoutSize );
183
184     // Renders the text and creates the final bitmap.
185     auto bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
186     DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
187   }
188
189   {
190     controller->SetVerticalLineAlignment( Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM );
191     controller->Relayout( relayoutSize );
192
193     // Renders the text and creates the final bitmap.
194     auto bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
195     DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
196   }
197
198   tet_result(TET_PASS);
199   END_TEST;
200 }
201
202 int UtcDaliTextTypesetterBitmapFont(void)
203 {
204   tet_infoline("UtcDaliTextTypesetterBitmapFont ");
205   ToolkitTestApplication application;
206
207   DevelText::BitmapFontDescription fontDescription;
208   fontDescription.name = "Digits";
209   fontDescription.underlinePosition = 0.f;
210   fontDescription.underlineThickness = 0.f;
211   fontDescription.isColorFont = true;
212
213   fontDescription.glyphs.push_back( { TEST_RESOURCE_DIR "/fonts/bitmap/u0031.png", "0", 34.f, 0.f } );
214   fontDescription.glyphs.push_back( { TEST_RESOURCE_DIR "/fonts/bitmap/u0032.png", "1", 34.f, 0.f } );
215
216   TextAbstraction::BitmapFont bitmapFont;
217   DevelText::CreateBitmapFont( fontDescription, bitmapFont );
218
219   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
220   fontClient.GetFontId( bitmapFont );
221
222   // Creates a text controller.
223   ControllerPtr controller = Controller::New();
224
225   // Configures the text controller similarly to the text-label.
226   ConfigureTextLabel( controller );
227
228   // Sets the text.
229   controller->SetMarkupProcessorEnabled( true );
230   controller->SetText( "<font family='Digits'><color 'value'='red'>0</color></font>" );
231
232   // Creates the text's model and relais-out the text.
233   const Size relayoutSize( 31.f, 34.f );
234   controller->Relayout( relayoutSize );
235
236   // Tests the rendering controller has been created.
237   TypesetterPtr renderingController = Typesetter::New( controller->GetTextModel() );
238   DALI_TEST_CHECK( renderingController );
239
240   controller->Relayout( relayoutSize );
241
242   // Renders the text and creates the final bitmap.
243   auto bitmap = renderingController->Render( relayoutSize, Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT );
244
245   DALI_TEST_EQUALS( 31u, bitmap.GetWidth(), TEST_LOCATION );
246   DALI_TEST_EQUALS( 34u, bitmap.GetHeight(), TEST_LOCATION );
247
248   tet_result(TET_PASS);
249   END_TEST;
250 }