Merge "Text cleaning." into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / dali-toolkit-test-utils / toolkit-text-utils.cpp
1 /*
2  * Copyright (c) 2018 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 // FILE HEADER
19 #include "toolkit-text-utils.h"
20
21 // EXTERNAL INCLUDES
22 #include <limits>
23 #include <dali/devel-api/text-abstraction/font-client.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/bidirectional-support.h>
27 #include <dali-toolkit/internal/text/character-set-conversion.h>
28 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
29 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
30 #include <dali-toolkit/internal/text/multi-language-support.h>
31 #include <dali-toolkit/internal/text/segmentation.h>
32 #include <dali-toolkit/internal/text/shaper.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
34 #include <dali-toolkit/internal/text/markup-processor.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Text
43 {
44
45 /**
46  * @brief Frees previously allocated bidirectional resources.
47  *
48  * @param[in] bidirectionalLineInfo Bidirectional info per line.
49  * @param[in] index Index to the first line with bidirectional info to be freed.
50  */
51 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
52                                          uint32_t index )
53 {
54   // Free the allocated memory used to store the conversion table in the bidirectional line info run.
55   for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
56          endIt = bidirectionalLineInfo.End();
57        it != endIt;
58        ++it )
59   {
60     BidirectionalLineInfoRun& bidiLineInfo = *it;
61
62     free( bidiLineInfo.visualToLogicalMap );
63   }
64 }
65
66 /**
67  * @brief Clear all the model data except for LogicalModel::mText.
68  *
69  * @param[in] characterIndex Clear data starting from the index.
70  */
71 void ClearModelData( CharacterIndex characterIndex,
72                      LogicalModelPtr logicalModel,
73                      VisualModelPtr visualModel )
74 {
75   // n.b. This does not Clear the mText from mLogicalModel
76
77   // Frees previously allocated resources.
78   FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
79
80   logicalModel->mScriptRuns.Clear();
81   logicalModel->mFontRuns.Clear();
82   logicalModel->mBidirectionalParagraphInfo.Clear();
83   logicalModel->mCharacterDirections.Clear();
84   logicalModel->mBidirectionalLineInfo.Clear();
85   visualModel->mGlyphs.Clear();
86   visualModel->mGlyphsToCharacters.Clear();
87   visualModel->mCharactersToGlyph.Clear();
88   visualModel->mCharactersPerGlyph.Clear();
89   visualModel->mGlyphsPerCharacter.Clear();
90   visualModel->mGlyphPositions.Clear();
91   visualModel->mLines.Clear();
92
93   visualModel->ClearCaches();
94 }
95
96 void CreateTextModel( const std::string& text,
97                       const Size& textArea,
98                       const Vector<FontDescriptionRun>& fontDescriptions,
99                       const LayoutOptions& options,
100                       Size& layoutSize,
101                       ModelPtr& textModel,
102                       MetricsPtr& metrics,
103                       bool markupProcessorEnabled )
104 {
105   textModel = Model::New(); ///< Pointer to the text's model.
106   LogicalModelPtr logicalModel = textModel->mLogicalModel;
107   VisualModelPtr visualModel = textModel->mVisualModel;
108
109   MarkupProcessData markupProcessData( logicalModel->mColorRuns,
110                                        logicalModel->mFontDescriptionRuns,
111                                        logicalModel->mEmbeddedItems );
112
113   Length textSize = 0u;
114   const uint8_t* utf8 = NULL;
115   if( markupProcessorEnabled )
116   {
117     ProcessMarkupString( text, markupProcessData );
118     textSize = markupProcessData.markupProcessedText.size();
119
120     // This is a bit horrible but std::string returns a (signed) char*
121     utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
122   }
123   else
124   {
125     textSize = text.size();
126
127     // This is a bit horrible but std::string returns a (signed) char*
128     utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
129   }
130
131   // 1) Convert to utf32
132   Vector<Character>& utf32Characters = logicalModel->mText;
133   utf32Characters.Resize( textSize );
134
135   // Transform a text array encoded in utf8 into an array encoded in utf32.
136   // It returns the actual number of characters.
137   Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
138   utf32Characters.Resize( characterCount );
139
140   // 2) Set the break and paragraph info.
141   Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
142   lineBreakInfo.Resize( characterCount );
143
144   SetLineBreakInfo( utf32Characters,
145                     0u,
146                     characterCount,
147                     lineBreakInfo );
148
149   if( 0u == characterCount )
150   {
151     // Nothing else to do if the number of characters is zero.
152     return;
153   }
154
155   // 3) Set the script info.
156   MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
157
158   Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
159   multilanguageSupport.SetScripts( utf32Characters,
160                                    0u,
161                                    characterCount,
162                                    scripts );
163
164   // 4) Set the font info
165   Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
166   fontDescriptionRuns = fontDescriptions;
167   Vector<FontRun>& validFonts = logicalModel->mFontRuns;
168
169   // The default font description.
170   TextAbstraction::FontDescription fontDescription;
171
172   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
173   fontClient.SetDpi( 96u, 96u );
174
175   // Validates the fonts. If there is a character with no assigned font it sets a default one.
176   // After this call, fonts are validated.
177   multilanguageSupport.ValidateFonts( utf32Characters,
178                                       scripts,
179                                       fontDescriptionRuns,
180                                       fontDescription,
181                                       TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
182                                       0u,
183                                       characterCount,
184                                       validFonts );
185
186   // 5) Set the bidirectional info per paragraph.
187   Vector<Character> mirroredUtf32Characters;
188   bool textMirrored = false;
189
190   // Reserve some space for the vector of paragraph's bidirectional info.
191   Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
192
193   // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
194   SetBidirectionalInfo( utf32Characters,
195                         scripts,
196                         lineBreakInfo,
197                         0u,
198                         characterCount,
199                         bidirectionalInfo );
200
201   // Create the paragraph info.
202   logicalModel->CreateParagraphInfo( 0u,
203                                      characterCount );
204
205   // 6) Set character directions.
206   Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
207   if( 0u != bidirectionalInfo.Count() )
208   {
209     // Only set the character directions if there is right to left characters.
210     GetCharactersDirection( bidirectionalInfo,
211                             characterCount,
212                             0u,
213                             characterCount,
214                             characterDirections );
215
216
217     // This paragraph has right to left text. Some characters may need to be mirrored.
218     textMirrored = GetMirroredText( utf32Characters,
219                                     characterDirections,
220                                     bidirectionalInfo,
221                                     0u,
222                                     characterCount,
223                                     mirroredUtf32Characters );
224   }
225   else
226   {
227     // There is no right to left characters. Clear the directions vector.
228     characterDirections.Clear();
229   }
230
231   // 7) Shape the text.
232
233   Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
234   Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
235   Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
236   Vector<GlyphIndex> newParagraphGlyphs;
237
238   const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
239
240   ShapeText( textToShape,
241              lineBreakInfo,
242              scripts,
243              validFonts,
244              0u,
245              0u,
246              characterCount,
247              glyphs,
248              glyphsToCharactersMap,
249              charactersPerGlyph,
250              newParagraphGlyphs );
251
252   // Create the 'number of glyphs' per character and the glyph to character conversion tables.
253   visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
254   visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
255
256   const Length numberOfGlyphs = glyphs.Count();
257
258   // 8) Get the glyph metrics
259   metrics = Metrics::New( fontClient );
260
261   GlyphInfo* glyphsBuffer = glyphs.Begin();
262   metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
263
264   // Update the width and advance of all new paragraph characters.
265   for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
266          endIt = newParagraphGlyphs.End();
267        it != endIt;
268        ++it )
269   {
270     const GlyphIndex index = *it;
271     GlyphInfo& glyph = *( glyphsBuffer + index );
272
273     glyph.xBearing = 0.f;
274     glyph.width = 0.f;
275     glyph.advance = 0.f;
276   }
277
278   // 9) Layout the text
279   Layout::Engine layoutEngine;
280   layoutEngine.SetMetrics( metrics );
281   layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
282
283   // Set the layout parameters.
284   textModel->mHorizontalAlignment = Text::HorizontalAlignment::BEGIN;
285   textModel->mLineWrapMode = LineWrap::WORD;
286   textModel->mIgnoreSpacesAfterText = true;
287   textModel->mMatchSystemLanguageDirection = false;
288   Layout::Parameters layoutParameters( textArea,
289                                        textModel );
290
291   Vector<LineRun>& lines = visualModel->mLines;
292
293   Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
294   glyphPositions.Resize( numberOfGlyphs );
295
296   layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( characterCount - 1u ) ) );
297
298   // The initial glyph and the number of glyphs to layout.
299   layoutParameters.startGlyphIndex = 0u;
300   layoutParameters.numberOfGlyphs = numberOfGlyphs;
301   layoutParameters.startLineIndex = 0u;
302   layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
303
304   bool isAutoScroll = false;
305   layoutEngine.LayoutText( layoutParameters,
306                            glyphPositions,
307                            lines,
308                            layoutSize,
309                            false,
310                            isAutoScroll );
311
312   // 10) Reorder the lines
313   if( 0u != bidirectionalInfo.Count() )
314   {
315     Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
316
317     // Get the lines
318     const Length numberOfLines = lines.Count();
319
320     // Reorder the lines.
321     bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
322     ReorderLines( bidirectionalInfo,
323                   0u,
324                   characterCount,
325                   lines,
326                   bidirectionalLineInfo );
327
328     // Set the bidirectional info per line into the layout parameters.
329     layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
330     layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
331
332     if( options.reorder )
333     {
334       // Re-layout the text. Reorder those lines with right to left characters.
335       layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
336                                              0u,
337                                              characterCount,
338                                              glyphPositions );
339     }
340   }
341
342   if( options.align )
343   {
344     float alignmentOffset = 0.f;
345     layoutEngine.Align( textArea,
346                         0u,
347                         characterCount,
348                         Text::HorizontalAlignment::BEGIN,
349                         lines,
350                         alignmentOffset,
351                         Dali::LayoutDirection::LEFT_TO_RIGHT,
352                         false );
353   }
354 }
355
356 void ConfigureTextLabel( ControllerPtr controller )
357 {
358   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
359   fontClient.SetDpi( 93u, 93u );
360
361   // Set the text layout as multi-line.
362   controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
363
364   // Set cursor's width to zero.
365   controller->GetLayoutEngine().SetCursorWidth( 0 );
366
367   InputMethodContext inputMethodContext = InputMethodContext::New();
368   // Disables the text input.
369   controller->EnableTextInput( NULL, inputMethodContext );
370
371   // Disables the vertical scrolling.
372   controller->SetVerticalScrollEnabled( false );
373
374   // Disables the horizontal scrolling.
375   controller->SetHorizontalScrollEnabled( false );
376
377   // Enable the text elide.
378   controller->SetTextElideEnabled( true );
379 }
380
381 void ConfigureTextField( ControllerPtr controller )
382 {
383   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
384   fontClient.SetDpi( 93u, 93u );
385
386   // Creates a decorator.
387   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
388                                                        *controller );
389
390   // Set the text layout as multi-line.
391   controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
392
393   InputMethodContext inputMethodContext = InputMethodContext::New();
394   // Enables the text input.
395   controller->EnableTextInput( decorator, inputMethodContext );
396
397   // Enables the vertical scrolling after the text input has been enabled.
398   controller->SetVerticalScrollEnabled( false );
399
400   // Disables the horizontal scrolling.
401   controller->SetHorizontalScrollEnabled( true );
402
403   // No maximum number of characters.
404   controller->SetMaximumNumberOfCharacters( 50u );
405
406   // Disable the text elide.
407   controller->SetTextElideEnabled( false );
408 }
409
410 void ConfigureTextEditor( ControllerPtr controller )
411 {
412   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
413   fontClient.SetDpi( 93u, 93u );
414
415   // Creates a decorator.
416   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
417                                                        *controller );
418
419   // Set the text layout as multi-line.
420   controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
421
422   InputMethodContext inputMethodContext = InputMethodContext::New();
423   // Enables the text input.
424   controller->EnableTextInput( decorator, inputMethodContext );
425
426   // Enables the vertical scrolling after the text input has been enabled.
427   controller->SetVerticalScrollEnabled( true );
428
429   // Disables the horizontal scrolling.
430   controller->SetHorizontalScrollEnabled( false );
431
432   // No maximum number of characters.
433   controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
434
435   // Disable the text elide.
436   controller->SetTextElideEnabled( false );
437 }
438
439 } // namespace Text
440
441 } // namespace Toolkit
442
443 } // namespace Dali