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