Merge "Add ApplyCustomFragmentPrefix" into 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
98   Vector<LineRun>& lines = textModel->mVisualModel->mLines;
99
100   // 4) Compare the results.
101
102   if( lines.Count() != data.numberOfLines )
103   {
104     std::cout << "  Different number of lines : " << lines.Count() << ", expected : " << data.numberOfLines << std::endl;
105     return false;
106   }
107
108   for( unsigned int index = 0u; index < data.numberOfLines; ++index )
109   {
110     const LineRun& line = *( lines.Begin() + index );
111     const LineRun& expectedLine = *( data.lines + index );
112
113     if( line.characterRun.characterIndex != expectedLine.characterRun.characterIndex )
114     {
115       std::cout << "  Different line info for line : " << index << std::endl;
116       Print( line );
117       std::cout << "  expected" << std::endl;
118       Print( expectedLine );
119       return false;
120     }
121     if( line.characterRun.numberOfCharacters != expectedLine.characterRun.numberOfCharacters )
122     {
123       std::cout << "  Different line info for line : " << index << std::endl;
124       Print( line );
125       std::cout << "  expected" << std::endl;
126       Print( expectedLine );
127       return false;
128     }
129   }
130
131   return true;
132 }
133
134 } // namespace
135
136
137 int UtcDaliTextHyphenWrapping(void)
138 {
139   ToolkitTestApplication application;
140   tet_infoline(" UtcDaliTextHyphenWrapping");
141
142   // Layout some lines of left to right text.
143
144   const std::string fontFamily( "TizenSans" );
145
146   // Set a known font description
147   FontDescriptionRun fontDescriptionRun1;
148   fontDescriptionRun1.characterRun.characterIndex = 0u;
149   fontDescriptionRun1.characterRun.numberOfCharacters = 13u;
150   fontDescriptionRun1.familyLength = fontFamily.size();
151   fontDescriptionRun1.familyName = new char[fontDescriptionRun1.familyLength];
152   memcpy( fontDescriptionRun1.familyName, fontFamily.c_str(), fontDescriptionRun1.familyLength );
153   fontDescriptionRun1.familyDefined = true;
154   fontDescriptionRun1.weightDefined = false;
155   fontDescriptionRun1.widthDefined = false;
156   fontDescriptionRun1.slantDefined = false;
157   fontDescriptionRun1.sizeDefined = false;
158
159   Vector<FontDescriptionRun> fontDescriptionRuns;
160   fontDescriptionRuns.PushBack( fontDescriptionRun1 );
161   Size textArea(65.0f, 200.f);
162
163   LineRun line1 =
164   {
165     { 0u, 5u },
166     { 0u, 5u },
167     0.f,
168     0.f,
169     0.f,
170     0.f,
171     0.f,
172     0.f,
173     false,
174     false
175   };
176   LineRun line2 =
177   {
178     { 5u, 8u },
179     { 5u, 8u },
180     0.f,
181     0.f,
182     0.f,
183     0.f,
184     0.f,
185     0.f,
186     false,
187     false
188   };
189
190   Vector<LineRun> lines;
191   lines.PushBack( line1 );
192   lines.PushBack( line2 );
193
194   LayoutTextData data =
195   {
196     "Hi Experiment",
197     textArea,
198     1u,
199     fontDescriptionRuns.Begin(),
200     2u,
201     lines.Begin(),
202     Layout::Engine::MULTI_LINE_BOX,
203     0u,
204     13u,
205     (Text::LineWrap::Mode)DevelText::LineWrap::HYPHENATION
206   };
207
208   if( !LayoutTextTest( data ) )
209   {
210     tet_result(TET_FAIL);
211   }
212
213   tet_result(TET_PASS);
214   END_TEST;
215 }
216
217 int UtcDaliTextMixedWrapping(void)
218 {
219   ToolkitTestApplication application;
220   tet_infoline(" UtcDaliTextMixedWrapping");
221
222   // Layout some lines of left to right text.
223
224   const std::string fontFamily( "DejaVuSans" );
225
226   // Set a known font description
227   FontDescriptionRun fontDescriptionRun1;
228   fontDescriptionRun1.characterRun.characterIndex = 0u;
229   fontDescriptionRun1.characterRun.numberOfCharacters = 13u;
230   fontDescriptionRun1.familyLength = fontFamily.size();
231   fontDescriptionRun1.familyName = new char[fontDescriptionRun1.familyLength];
232   memcpy( fontDescriptionRun1.familyName, fontFamily.c_str(), fontDescriptionRun1.familyLength );
233   fontDescriptionRun1.familyDefined = true;
234   fontDescriptionRun1.weightDefined = false;
235   fontDescriptionRun1.widthDefined = false;
236   fontDescriptionRun1.slantDefined = false;
237   fontDescriptionRun1.sizeDefined = false;
238
239   Vector<FontDescriptionRun> fontDescriptionRuns;
240   fontDescriptionRuns.PushBack( fontDescriptionRun1 );
241   Size textArea(72.0f, 200.f);
242
243   LineRun line1 =
244   {
245     { 0u, 3u },
246     { 0u, 3u },
247     0.f,
248     0.f,
249     0.f,
250     0.f,
251     0.f,
252     0.f,
253     false,
254     false
255   };
256   LineRun line2 =
257   {
258     { 3u, 6u },
259     { 3u, 6u },
260     0.f,
261     0.f,
262     0.f,
263     0.f,
264     0.f,
265     0.f,
266     false,
267     false
268   };
269   LineRun line3 =
270   {
271     { 9u, 4u },
272     { 9u, 4u },
273     0.f,
274     0.f,
275     0.f,
276     0.f,
277     0.f,
278     0.f,
279     false,
280     false
281   };
282
283   Vector<LineRun> lines;
284   lines.PushBack( line1 );
285   lines.PushBack( line2 );
286   lines.PushBack( line3 );
287
288   LayoutTextData data =
289   {
290     "Hi Experiment",
291     textArea,
292     1u,
293     fontDescriptionRuns.Begin(),
294     3u,
295     lines.Begin(),
296     Layout::Engine::MULTI_LINE_BOX,
297     0u,
298     13u,
299     (Text::LineWrap::Mode)DevelText::LineWrap::MIXED
300   };
301
302   if( !LayoutTextTest( data ) )
303   {
304     tet_result(TET_FAIL);
305   }
306
307   tet_result(TET_PASS);
308   END_TEST;
309 }