Merge "Disable pixel alignment while scrolling the text" into 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 <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
31 using namespace Dali;
32 using namespace Toolkit;
33 using namespace Text;
34
35 namespace
36 {
37 const std::string DEFAULT_FONT_DIR( "/resources/fonts" );
38 const PointSize26Dot6 EMOJI_FONT_SIZE = 3840u; // 60 * 64
39 } // namespace
40
41 int UtcDaliTextTypesetter(void)
42 {
43   tet_infoline(" UtcDaliTextTypesetter");
44   ToolkitTestApplication application;
45
46   // Creates a text controller.
47   ControllerPtr controller = Controller::New();
48
49   // Tests the rendering controller has been created.
50   TypesetterPtr typesetter = Typesetter::New( controller->GetTextModel() );
51   DALI_TEST_CHECK( typesetter );
52
53   tet_result(TET_PASS);
54   END_TEST;
55 }
56
57 int UtcDaliTextTypesetterGetViewModel(void)
58 {
59   tet_infoline(" UtcDaliTextTypesetter");
60   ToolkitTestApplication application;
61
62   // Creates a text controller.
63   ControllerPtr controller = Controller::New();
64
65   // Tests the rendering controller has been created.
66   TypesetterPtr typesetter = Typesetter::New( controller->GetTextModel() );
67   DALI_TEST_CHECK( typesetter );
68
69   // Tests the view model has been created.
70   ViewModel* model = typesetter->GetViewModel();
71   DALI_TEST_CHECK( NULL != model );
72
73   tet_result(TET_PASS);
74   END_TEST;
75 }
76
77 int UtcDaliTextRenderingControllerRender(void)
78 {
79   tet_infoline(" UtcDaliTextRenderingControllerRender");
80   ToolkitTestApplication application;
81
82   // Load some fonts.
83   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
84
85   char* pathNamePtr = get_current_dir_name();
86   const std::string pathName( pathNamePtr );
87   free( pathNamePtr );
88
89   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/BreezeColorEmoji.ttf", EMOJI_FONT_SIZE );
90   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf" );
91
92   // Creates a text controller.
93   ControllerPtr controller = Controller::New();
94
95   // Configures the text controller similarly to the text-label.
96   ConfigureTextLabel( controller );
97
98   // Sets the text.
99   controller->SetMarkupProcessorEnabled( true );
100   controller->SetText( "<font family='TizenSansRegular'>Hello world </font><font family='BreezeColorEmoji'>\xF0\x9F\x98\x81</font>" );
101
102   // Creates the text's model and relais-out the text.
103   const Size relayoutSize( 120.f, 60.f );
104   controller->Relayout( relayoutSize );
105
106   // Tests the rendering controller has been created.
107   TypesetterPtr renderingController = Typesetter::New( controller->GetTextModel() );
108   DALI_TEST_CHECK( renderingController );
109
110   // Renders the text and creates the final bitmap.
111   PixelData bitmap = renderingController->Render( relayoutSize );
112   DALI_TEST_CHECK( bitmap );
113
114   DALI_TEST_EQUALS( 120u, bitmap.GetWidth(), TEST_LOCATION );
115   DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
116   DALI_TEST_EQUALS( Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION );
117
118   // Changes vertical alignment.
119   controller->SetVerticalAlignment( Text::VerticalAlignment::CENTER );
120   controller->Relayout( relayoutSize );
121
122   // Renders the text and creates the final bitmap.
123   bitmap = renderingController->Render( relayoutSize );
124   DALI_TEST_CHECK( bitmap );
125
126   DALI_TEST_EQUALS( 120u, bitmap.GetWidth(), TEST_LOCATION );
127   DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
128   DALI_TEST_EQUALS( Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION );
129
130   controller->SetVerticalAlignment( Text::VerticalAlignment::BOTTOM );
131   controller->Relayout( relayoutSize );
132
133   // Renders the text and creates the final bitmap.
134   bitmap = renderingController->Render( relayoutSize );
135   DALI_TEST_CHECK( bitmap );
136
137   DALI_TEST_EQUALS( 120u, bitmap.GetWidth(), TEST_LOCATION );
138   DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
139   DALI_TEST_EQUALS( Pixel::RGBA8888, bitmap.GetPixelFormat(), TEST_LOCATION );
140
141   tet_result(TET_PASS);
142   END_TEST;
143 }
144
145 int UtcDaliTextTypesetterVerticalLineAlignment(void)
146 {
147   tet_infoline(" UtcDaliTextTypesetter");
148   ToolkitTestApplication application;
149
150   // Creates a text controller.
151   ControllerPtr controller = Controller::New();
152
153   // Configures the text controller similarly to the text-label.
154   ConfigureTextLabel( controller );
155
156   // Sets the text.
157   controller->SetMarkupProcessorEnabled( true );
158   controller->SetText( "<font family='TizenSansRegular'>Hello world</font>" );
159
160   // Creates the text's model and relais-out the text.
161   const Size relayoutSize( 120.f, 60.f );
162   controller->Relayout( relayoutSize );
163
164   // Tests the rendering controller has been created.
165   TypesetterPtr renderingController = Typesetter::New( controller->GetTextModel() );
166   DALI_TEST_CHECK( renderingController );
167
168   {
169     controller->SetVerticalLineAlignment(Dali::Toolkit::DevelText::VerticalLineAlignment::TOP);
170     controller->Relayout(relayoutSize);
171
172     // Renders the text and creates the final bitmap.
173     auto bitmap = renderingController->Render(relayoutSize);
174     DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
175   }
176
177   {
178     controller->SetVerticalLineAlignment(Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE);
179     controller->Relayout(relayoutSize);
180
181     // Renders the text and creates the final bitmap.
182     auto bitmap = renderingController->Render(relayoutSize);
183     DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
184   }
185
186   {
187     controller->SetVerticalLineAlignment(Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM);
188     controller->Relayout(relayoutSize);
189
190     // Renders the text and creates the final bitmap.
191     auto bitmap = renderingController->Render(relayoutSize);
192     DALI_TEST_EQUALS( 60u, bitmap.GetHeight(), TEST_LOCATION );
193   }
194
195   tet_result(TET_PASS);
196   END_TEST;
197 }