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