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