Merge "DALi Version 2.0.34" 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) 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                                        logicalModel->mBackgroundColorRuns);
117
118   Length textSize = 0u;
119   const uint8_t* utf8 = NULL;
120   if( markupProcessorEnabled )
121   {
122     ProcessMarkupString( text, markupProcessData );
123     textSize = markupProcessData.markupProcessedText.size();
124
125     // This is a bit horrible but std::string returns a (signed) char*
126     utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
127   }
128   else
129   {
130     textSize = text.size();
131
132     // This is a bit horrible but std::string returns a (signed) char*
133     utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
134   }
135
136   // 1) Convert to utf32
137   Vector<Character>& utf32Characters = logicalModel->mText;
138   utf32Characters.Resize( textSize );
139
140   // Transform a text array encoded in utf8 into an array encoded in utf32.
141   // It returns the actual number of characters.
142   Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
143   utf32Characters.Resize( characterCount );
144
145   // 2) Set the break and paragraph info.
146   Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
147   lineBreakInfo.Resize( characterCount );
148
149   SetLineBreakInfo( utf32Characters,
150                     0u,
151                     characterCount,
152                     lineBreakInfo );
153
154   if( 0u == characterCount )
155   {
156     // Nothing else to do if the number of characters is zero.
157     return;
158   }
159
160   textModel->mLineWrapMode = wrapMode;
161
162   if(textModel->mLineWrapMode == ((Text::LineWrap::Mode)DevelText::LineWrap::HYPHENATION) ||
163        textModel->mLineWrapMode == ((Text::LineWrap::Mode)DevelText::LineWrap::MIXED))
164   {
165     CharacterIndex end                 = characterCount;
166     LineBreakInfo* lineBreakInfoBuffer = lineBreakInfo.Begin();
167
168     for(CharacterIndex index = 0; index < end; index++)
169     {
170       CharacterIndex wordEnd = index;
171       while((*(lineBreakInfoBuffer + wordEnd) != TextAbstraction::LINE_ALLOW_BREAK) && (*(lineBreakInfoBuffer + wordEnd) != TextAbstraction::LINE_MUST_BREAK))
172       {
173         wordEnd++;
174       }
175
176       if((wordEnd + 1) == end) // add last char
177       {
178         wordEnd++;
179       }
180
181       Vector<bool> hyphens = GetWordHyphens(utf32Characters.Begin() + index, wordEnd - index, nullptr);
182
183       for(CharacterIndex i = 0; i < (wordEnd - index); i++)
184       {
185         if(hyphens[i])
186         {
187           *(lineBreakInfoBuffer + index + i) = TextAbstraction::LINE_HYPHENATION_BREAK;
188         }
189       }
190
191       index = wordEnd;
192     }
193   }
194
195   // 3) Set the script info.
196   MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
197
198   Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
199   multilanguageSupport.SetScripts( utf32Characters,
200                                    0u,
201                                    characterCount,
202                                    scripts );
203
204   // 4) Set the font info
205   Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
206   fontDescriptionRuns = fontDescriptions;
207   Vector<FontRun>& validFonts = logicalModel->mFontRuns;
208
209   // The default font description.
210   TextAbstraction::FontDescription fontDescription;
211
212   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
213   fontClient.SetDpi( 96u, 96u );
214
215   // Validates the fonts. If there is a character with no assigned font it sets a default one.
216   // After this call, fonts are validated.
217   multilanguageSupport.ValidateFonts( utf32Characters,
218                                       scripts,
219                                       fontDescriptionRuns,
220                                       fontDescription,
221                                       TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
222                                       0u,
223                                       characterCount,
224                                       validFonts );
225
226   // 5) Set the bidirectional info per paragraph.
227   Vector<Character> mirroredUtf32Characters;
228   bool textMirrored = false;
229
230   // Reserve some space for the vector of paragraph's bidirectional info.
231   Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
232
233   // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
234   SetBidirectionalInfo( utf32Characters,
235                         scripts,
236                         lineBreakInfo,
237                         0u,
238                         characterCount,
239                         bidirectionalInfo );
240
241   // Create the paragraph info.
242   logicalModel->CreateParagraphInfo( 0u,
243                                      characterCount );
244
245   // 6) Set character directions.
246   Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
247   if( 0u != bidirectionalInfo.Count() )
248   {
249     // Only set the character directions if there is right to left characters.
250     GetCharactersDirection( bidirectionalInfo,
251                             characterCount,
252                             0u,
253                             characterCount,
254                             characterDirections );
255
256
257     // This paragraph has right to left text. Some characters may need to be mirrored.
258     textMirrored = GetMirroredText( utf32Characters,
259                                     characterDirections,
260                                     bidirectionalInfo,
261                                     0u,
262                                     characterCount,
263                                     mirroredUtf32Characters );
264   }
265   else
266   {
267     // There is no right to left characters. Clear the directions vector.
268     characterDirections.Clear();
269   }
270
271   // 7) Shape the text.
272
273   Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
274   Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
275   Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
276   Vector<GlyphIndex> newParagraphGlyphs;
277
278   const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
279
280   ShapeText( textToShape,
281              lineBreakInfo,
282              scripts,
283              validFonts,
284              0u,
285              0u,
286              characterCount,
287              glyphs,
288              glyphsToCharactersMap,
289              charactersPerGlyph,
290              newParagraphGlyphs );
291
292   // Create the 'number of glyphs' per character and the glyph to character conversion tables.
293   visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
294   visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
295
296   const Length numberOfGlyphs = glyphs.Count();
297
298   // 8) Get the glyph metrics
299   metrics = Metrics::New( fontClient );
300
301   GlyphInfo* glyphsBuffer = glyphs.Begin();
302   metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
303
304   // Update the width and advance of all new paragraph characters.
305   for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
306          endIt = newParagraphGlyphs.End();
307        it != endIt;
308        ++it )
309   {
310     const GlyphIndex index = *it;
311     GlyphInfo& glyph = *( glyphsBuffer + index );
312
313     glyph.xBearing = 0.f;
314     glyph.width = 0.f;
315     glyph.advance = 0.f;
316   }
317
318   // 9) Layout the text
319   Layout::Engine layoutEngine;
320   layoutEngine.SetMetrics( metrics );
321   layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
322
323   // Set the layout parameters.
324   textModel->mHorizontalAlignment = Text::HorizontalAlignment::BEGIN;
325   textModel->mIgnoreSpacesAfterText = true;
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->SetMatchLayoutDirection(DevelText::MatchLayoutDirection::CONTENTS);
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->SetMatchLayoutDirection(DevelText::MatchLayoutDirection::CONTENTS);
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->SetMatchLayoutDirection(DevelText::MatchLayoutDirection::CONTENTS);
452 }
453
454 } // namespace Text
455
456 } // namespace Toolkit
457
458 } // namespace Dali