[dali_2.1.6] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Hyphen-Wrapping.cpp
1 /*
2  * Copyright (c) 2021 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 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
23 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
24 #include <dali-toolkit/internal/text/text-run-container.h>
25 #include <dali-toolkit-test-suite-utils.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <toolkit-text-utils.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31 using namespace Text;
32
33
34 namespace
35 {
36
37 const std::string DEFAULT_FONT_DIR( "/resources/fonts" );
38
39 struct LayoutTextData
40 {
41   std::string          text;
42   Size                 textArea;
43   unsigned int         numberOfFonts;
44   FontDescriptionRun  *fontDescriptions;
45   unsigned int         numberOfLines;
46   LineRun*             lines;
47   Layout::Engine::Type layout;
48   unsigned int         startIndex;
49   unsigned int         numberOfGlyphs;
50   Text::LineWrap::Mode wrapMode;
51 };
52
53 void Print( const LineRun& line )
54 {
55   std::cout << "        glyph run, index : " << line.glyphRun.glyphIndex << ", num glyphs : " << line.glyphRun.numberOfGlyphs << std::endl;
56   std::cout << "    character run, index : " << line.characterRun.characterIndex << ", num chars : " << line.characterRun.numberOfCharacters << std::endl;
57 }
58
59 bool LayoutTextTest( const LayoutTextData& data )
60 {
61   // Load some fonts.
62   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
63   fontClient.SetDpi( 96u, 96u );
64
65   char* pathNamePtr = get_current_dir_name();
66   const std::string pathName( pathNamePtr );
67   free( pathNamePtr );
68
69   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf" );
70
71   // 1) Create the model.
72   ModelPtr textModel;
73   MetricsPtr metrics;
74   Size layoutSize;
75
76   Vector<FontDescriptionRun> fontDescriptionRuns;
77   if( 0u != data.numberOfFonts )
78   {
79     fontDescriptionRuns.Insert( fontDescriptionRuns.End(),
80                                 data.fontDescriptions,
81                                 data.fontDescriptions + data.numberOfFonts );
82   }
83
84   LayoutOptions options;
85   options.align = false;
86   CreateTextModel( data.text,
87                    data.textArea,
88                    fontDescriptionRuns,
89                    options,
90                    layoutSize,
91                    textModel,
92                    metrics,
93                    false,
94                    data.wrapMode,
95                    false,
96                    Toolkit::DevelText::EllipsisPosition::END,
97                    0.f );
98
99   Vector<LineRun>& lines = textModel->mVisualModel->mLines;
100
101   // 4) Compare the results.
102
103   if( lines.Count() != data.numberOfLines )
104   {
105     std::cout << "  Different number of lines : " << lines.Count() << ", expected : " << data.numberOfLines << std::endl;
106     return false;
107   }
108
109   for( unsigned int index = 0u; index < data.numberOfLines; ++index )
110   {
111     const LineRun& line = *( lines.Begin() + index );
112     const LineRun& expectedLine = *( data.lines + index );
113
114     if( line.characterRun.characterIndex != expectedLine.characterRun.characterIndex )
115     {
116       std::cout << "  Different line info for line : " << index << std::endl;
117       Print( line );
118       std::cout << "  expected" << std::endl;
119       Print( expectedLine );
120       return false;
121     }
122     if( line.characterRun.numberOfCharacters != expectedLine.characterRun.numberOfCharacters )
123     {
124       std::cout << "  Different line info for line : " << index << std::endl;
125       Print( line );
126       std::cout << "  expected" << std::endl;
127       Print( expectedLine );
128       return false;
129     }
130   }
131
132   return true;
133 }
134
135 } // namespace
136
137
138 int UtcDaliTextHyphenWrapping(void)
139 {
140   ToolkitTestApplication application;
141   tet_infoline(" UtcDaliTextHyphenWrapping");
142
143   // Layout some lines of left to right text.
144
145   const std::string fontFamily( "TizenSans" );
146
147   // Set a known font description
148   FontDescriptionRun fontDescriptionRun1;
149   fontDescriptionRun1.characterRun.characterIndex = 0u;
150   fontDescriptionRun1.characterRun.numberOfCharacters = 13u;
151   fontDescriptionRun1.familyLength = fontFamily.size();
152   fontDescriptionRun1.familyName = new char[fontDescriptionRun1.familyLength];
153   memcpy( fontDescriptionRun1.familyName, fontFamily.c_str(), fontDescriptionRun1.familyLength );
154   fontDescriptionRun1.familyDefined = true;
155   fontDescriptionRun1.weightDefined = false;
156   fontDescriptionRun1.widthDefined = false;
157   fontDescriptionRun1.slantDefined = false;
158   fontDescriptionRun1.sizeDefined = false;
159
160   Vector<FontDescriptionRun> fontDescriptionRuns;
161   fontDescriptionRuns.PushBack( fontDescriptionRun1 );
162   Size textArea(65.0f, 200.f);
163
164   LineRun line1 =
165   {
166     { 0u, 5u },
167     { 0u, 5u },
168     0.f,
169     0.f,
170     0.f,
171     0.f,
172     0.f,
173     0.f,
174     false,
175     false
176   };
177   LineRun line2 =
178   {
179     { 5u, 8u },
180     { 5u, 8u },
181     0.f,
182     0.f,
183     0.f,
184     0.f,
185     0.f,
186     0.f,
187     false,
188     false
189   };
190
191   Vector<LineRun> lines;
192   lines.PushBack( line1 );
193   lines.PushBack( line2 );
194
195   LayoutTextData data =
196   {
197     "Hi Experiment",
198     textArea,
199     1u,
200     fontDescriptionRuns.Begin(),
201     2u,
202     lines.Begin(),
203     Layout::Engine::MULTI_LINE_BOX,
204     0u,
205     13u,
206     (Text::LineWrap::Mode)DevelText::LineWrap::HYPHENATION
207   };
208
209   if( !LayoutTextTest( data ) )
210   {
211     tet_result(TET_FAIL);
212   }
213
214   tet_result(TET_PASS);
215   END_TEST;
216 }
217
218 int UtcDaliTextMixedWrapping(void)
219 {
220   ToolkitTestApplication application;
221   tet_infoline(" UtcDaliTextMixedWrapping");
222
223   // Layout some lines of left to right text.
224
225   const std::string fontFamily( "DejaVuSans" );
226
227   // Set a known font description
228   FontDescriptionRun fontDescriptionRun1;
229   fontDescriptionRun1.characterRun.characterIndex = 0u;
230   fontDescriptionRun1.characterRun.numberOfCharacters = 13u;
231   fontDescriptionRun1.familyLength = fontFamily.size();
232   fontDescriptionRun1.familyName = new char[fontDescriptionRun1.familyLength];
233   memcpy( fontDescriptionRun1.familyName, fontFamily.c_str(), fontDescriptionRun1.familyLength );
234   fontDescriptionRun1.familyDefined = true;
235   fontDescriptionRun1.weightDefined = false;
236   fontDescriptionRun1.widthDefined = false;
237   fontDescriptionRun1.slantDefined = false;
238   fontDescriptionRun1.sizeDefined = false;
239
240   Vector<FontDescriptionRun> fontDescriptionRuns;
241   fontDescriptionRuns.PushBack( fontDescriptionRun1 );
242   Size textArea(72.0f, 200.f);
243
244   LineRun line1 =
245   {
246     { 0u, 3u },
247     { 0u, 3u },
248     0.f,
249     0.f,
250     0.f,
251     0.f,
252     0.f,
253     0.f,
254     false,
255     false
256   };
257   LineRun line2 =
258   {
259     { 3u, 6u },
260     { 3u, 6u },
261     0.f,
262     0.f,
263     0.f,
264     0.f,
265     0.f,
266     0.f,
267     false,
268     false
269   };
270   LineRun line3 =
271   {
272     { 9u, 4u },
273     { 9u, 4u },
274     0.f,
275     0.f,
276     0.f,
277     0.f,
278     0.f,
279     0.f,
280     false,
281     false
282   };
283
284   Vector<LineRun> lines;
285   lines.PushBack( line1 );
286   lines.PushBack( line2 );
287   lines.PushBack( line3 );
288
289   LayoutTextData data =
290   {
291     "Hi Experiment",
292     textArea,
293     1u,
294     fontDescriptionRuns.Begin(),
295     3u,
296     lines.Begin(),
297     Layout::Engine::MULTI_LINE_BOX,
298     0u,
299     13u,
300     (Text::LineWrap::Mode)DevelText::LineWrap::MIXED
301   };
302
303   if( !LayoutTextTest( data ) )
304   {
305     tet_result(TET_FAIL);
306   }
307
308   tet_result(TET_PASS);
309   END_TEST;
310 }