Makes the LTR/RTL alignment of text follow the system language by default.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / dali-toolkit-test-utils / toolkit-text-utils.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 43fc5ac..b2a6f5a
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@
 #include <dali-toolkit/internal/text/segmentation.h>
 #include <dali-toolkit/internal/text/shaper.h>
 #include <dali-toolkit/internal/text/text-controller-impl.h>
+#include <dali-toolkit/internal/text/markup-processor.h>
+#include <dali-toolkit/internal/text/hyphenator.h>
 
 namespace Dali
 {
@@ -78,7 +80,6 @@ void ClearModelData( CharacterIndex characterIndex,
 
   logicalModel->mScriptRuns.Clear();
   logicalModel->mFontRuns.Clear();
-  logicalModel->mWordBreakInfo.Clear();
   logicalModel->mBidirectionalParagraphInfo.Clear();
   logicalModel->mCharacterDirections.Clear();
   logicalModel->mBidirectionalLineInfo.Clear();
@@ -98,45 +99,98 @@ void CreateTextModel( const std::string& text,
                       const Vector<FontDescriptionRun>& fontDescriptions,
                       const LayoutOptions& options,
                       Size& layoutSize,
-                      LogicalModelPtr& logicalModel,
-                      VisualModelPtr& visualModel,
-                      MetricsPtr& metrics )
+                      ModelPtr& textModel,
+                      MetricsPtr& metrics,
+                      bool markupProcessorEnabled,
+                      LineWrap::Mode wrapMode )
 {
-  logicalModel = LogicalModel::New();
-  visualModel = VisualModel::New();
+  textModel = Model::New(); ///< Pointer to the text's model.
+  LogicalModelPtr logicalModel = textModel->mLogicalModel;
+  VisualModelPtr visualModel = textModel->mVisualModel;
+
+  MarkupProcessData markupProcessData( logicalModel->mColorRuns,
+                                       logicalModel->mFontDescriptionRuns,
+                                       logicalModel->mEmbeddedItems,
+                                       logicalModel->mAnchors,
+                                       logicalModel->mUnderlinedCharacterRuns,
+                                       logicalModel->mBackgroundColorRuns);
+
+  Length textSize = 0u;
+  const uint8_t* utf8 = NULL;
+  if( markupProcessorEnabled )
+  {
+    ProcessMarkupString( text, markupProcessData );
+    textSize = markupProcessData.markupProcessedText.size();
+
+    // This is a bit horrible but std::string returns a (signed) char*
+    utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
+  }
+  else
+  {
+    textSize = text.size();
+
+    // This is a bit horrible but std::string returns a (signed) char*
+    utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
+  }
 
   // 1) Convert to utf32
   Vector<Character>& utf32Characters = logicalModel->mText;
-  utf32Characters.Resize( text.size() );
+  utf32Characters.Resize( textSize );
 
-  const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
-                                                   text.size(),
-                                                   &utf32Characters[0u] );
-  utf32Characters.Resize( numberOfCharacters );
+  // Transform a text array encoded in utf8 into an array encoded in utf32.
+  // It returns the actual number of characters.
+  Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
+  utf32Characters.Resize( characterCount );
 
   // 2) Set the break and paragraph info.
   Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
-  lineBreakInfo.Resize( numberOfCharacters );
+  lineBreakInfo.Resize( characterCount );
 
   SetLineBreakInfo( utf32Characters,
                     0u,
-                    numberOfCharacters,
+                    characterCount,
                     lineBreakInfo );
 
-  if( 0u == numberOfCharacters )
+  if( 0u == characterCount )
   {
     // Nothing else to do if the number of characters is zero.
     return;
   }
 
-  // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
-  Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
-  wordBreakInfo.Resize( numberOfCharacters );
+  textModel->mLineWrapMode = wrapMode;
 
-  SetWordBreakInfo( utf32Characters,
-                    0u,
-                    numberOfCharacters,
-                    wordBreakInfo );
+  if(textModel->mLineWrapMode == ((Text::LineWrap::Mode)DevelText::LineWrap::HYPHENATION) ||
+       textModel->mLineWrapMode == ((Text::LineWrap::Mode)DevelText::LineWrap::MIXED))
+  {
+    CharacterIndex end                 = characterCount;
+    LineBreakInfo* lineBreakInfoBuffer = lineBreakInfo.Begin();
+
+    for(CharacterIndex index = 0; index < end; index++)
+    {
+      CharacterIndex wordEnd = index;
+      while((*(lineBreakInfoBuffer + wordEnd) != TextAbstraction::LINE_ALLOW_BREAK) && (*(lineBreakInfoBuffer + wordEnd) != TextAbstraction::LINE_MUST_BREAK))
+      {
+        wordEnd++;
+      }
+
+      if((wordEnd + 1) == end) // add last char
+      {
+        wordEnd++;
+      }
+
+      Vector<bool> hyphens = GetWordHyphens(utf32Characters.Begin() + index, wordEnd - index, nullptr);
+
+      for(CharacterIndex i = 0; i < (wordEnd - index); i++)
+      {
+        if(hyphens[i])
+        {
+          *(lineBreakInfoBuffer + index + i) = TextAbstraction::LINE_HYPHENATION_BREAK;
+        }
+      }
+
+      index = wordEnd;
+    }
+  }
 
   // 3) Set the script info.
   MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
@@ -144,7 +198,7 @@ void CreateTextModel( const std::string& text,
   Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
   multilanguageSupport.SetScripts( utf32Characters,
                                    0u,
-                                   numberOfCharacters,
+                                   characterCount,
                                    scripts );
 
   // 4) Set the font info
@@ -166,7 +220,7 @@ void CreateTextModel( const std::string& text,
                                       fontDescription,
                                       TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
                                       0u,
-                                      numberOfCharacters,
+                                      characterCount,
                                       validFonts );
 
   // 5) Set the bidirectional info per paragraph.
@@ -181,12 +235,12 @@ void CreateTextModel( const std::string& text,
                         scripts,
                         lineBreakInfo,
                         0u,
-                        numberOfCharacters,
+                        characterCount,
                         bidirectionalInfo );
 
   // Create the paragraph info.
   logicalModel->CreateParagraphInfo( 0u,
-                                     numberOfCharacters );
+                                     characterCount );
 
   // 6) Set character directions.
   Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
@@ -194,9 +248,9 @@ void CreateTextModel( const std::string& text,
   {
     // Only set the character directions if there is right to left characters.
     GetCharactersDirection( bidirectionalInfo,
-                            numberOfCharacters,
+                            characterCount,
                             0u,
-                            numberOfCharacters,
+                            characterCount,
                             characterDirections );
 
 
@@ -205,7 +259,7 @@ void CreateTextModel( const std::string& text,
                                     characterDirections,
                                     bidirectionalInfo,
                                     0u,
-                                    numberOfCharacters,
+                                    characterCount,
                                     mirroredUtf32Characters );
   }
   else
@@ -229,15 +283,15 @@ void CreateTextModel( const std::string& text,
              validFonts,
              0u,
              0u,
-             numberOfCharacters,
+             characterCount,
              glyphs,
              glyphsToCharactersMap,
              charactersPerGlyph,
              newParagraphGlyphs );
 
   // Create the 'number of glyphs' per character and the glyph to character conversion tables.
-  visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
-  visualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
+  visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
+  visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
 
   const Length numberOfGlyphs = glyphs.Count();
 
@@ -267,29 +321,17 @@ void CreateTextModel( const std::string& text,
   layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
 
   // Set the layout parameters.
-  const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
-  const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
-
+  textModel->mHorizontalAlignment = Text::HorizontalAlignment::BEGIN;
+  textModel->mIgnoreSpacesAfterText = true;
   Layout::Parameters layoutParameters( textArea,
-                                       utf32Characters.Begin(),
-                                       lineBreakInfo.Begin(),
-                                       wordBreakInfo.Begin(),
-                                       ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
-                                       glyphs.Begin(),
-                                       glyphsToCharactersMap.Begin(),
-                                       charactersPerGlyph.Begin(),
-                                       charactersToGlyph.Begin(),
-                                       glyphsPerCharacter.Begin(),
-                                       numberOfGlyphs,
-                                       Layout::HORIZONTAL_ALIGN_BEGIN,
-                                       Layout::LineWrap::WORD );
+                                       textModel );
 
   Vector<LineRun>& lines = visualModel->mLines;
 
   Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
   glyphPositions.Resize( numberOfGlyphs );
 
-  layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
+  layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( characterCount - 1u ) ) );
 
   // The initial glyph and the number of glyphs to layout.
   layoutParameters.startGlyphIndex = 0u;
@@ -297,51 +339,23 @@ void CreateTextModel( const std::string& text,
   layoutParameters.startLineIndex = 0u;
   layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
 
+  bool isAutoScroll = false;
   layoutEngine.LayoutText( layoutParameters,
-                           glyphPositions,
-                           lines,
                            layoutSize,
-                           false );
-
-  // 10) Reorder the lines
-  if( 0u != bidirectionalInfo.Count() )
-  {
-    Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
-
-    // Get the lines
-    const Length numberOfLines = lines.Count();
-
-    // Reorder the lines.
-    bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
-    ReorderLines( bidirectionalInfo,
-                  0u,
-                  numberOfCharacters,
-                  lines,
-                  bidirectionalLineInfo );
-
-    // Set the bidirectional info per line into the layout parameters.
-    layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
-    layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
-
-    if( options.reorder )
-    {
-      // Re-layout the text. Reorder those lines with right to left characters.
-      layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
-                                             0u,
-                                             numberOfCharacters,
-                                             glyphPositions );
-    }
-  }
+                           false,
+                           isAutoScroll );
 
   if( options.align )
   {
     float alignmentOffset = 0.f;
     layoutEngine.Align( textArea,
                         0u,
-                        numberOfCharacters,
-                        Layout::HORIZONTAL_ALIGN_BEGIN,
+                        characterCount,
+                        Text::HorizontalAlignment::BEGIN,
                         lines,
-                        alignmentOffset );
+                        alignmentOffset,
+                        Dali::LayoutDirection::LEFT_TO_RIGHT,
+                        false );
   }
 }
 
@@ -356,8 +370,9 @@ void ConfigureTextLabel( ControllerPtr controller )
   // Set cursor's width to zero.
   controller->GetLayoutEngine().SetCursorWidth( 0 );
 
+  InputMethodContext inputMethodContext = InputMethodContext::New();
   // Disables the text input.
-  controller->EnableTextInput( NULL );
+  controller->EnableTextInput( NULL, inputMethodContext );
 
   // Disables the vertical scrolling.
   controller->SetVerticalScrollEnabled( false );
@@ -367,6 +382,9 @@ void ConfigureTextLabel( ControllerPtr controller )
 
   // Enable the text elide.
   controller->SetTextElideEnabled( true );
+
+  // Disable match system language direction
+  controller->SetMatchLayoutDirection(DevelText::MatchLayoutDirection::CONTENTS);
 }
 
 void ConfigureTextField( ControllerPtr controller )
@@ -381,8 +399,9 @@ void ConfigureTextField( ControllerPtr controller )
   // Set the text layout as multi-line.
   controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
 
+  InputMethodContext inputMethodContext = InputMethodContext::New();
   // Enables the text input.
-  controller->EnableTextInput( decorator );
+  controller->EnableTextInput( decorator, inputMethodContext );
 
   // Enables the vertical scrolling after the text input has been enabled.
   controller->SetVerticalScrollEnabled( false );
@@ -395,6 +414,9 @@ void ConfigureTextField( ControllerPtr controller )
 
   // Disable the text elide.
   controller->SetTextElideEnabled( false );
+
+  // Disable match system language direction
+  controller->SetMatchLayoutDirection(DevelText::MatchLayoutDirection::CONTENTS);
 }
 
 void ConfigureTextEditor( ControllerPtr controller )
@@ -409,8 +431,9 @@ void ConfigureTextEditor( ControllerPtr controller )
   // Set the text layout as multi-line.
   controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
 
+  InputMethodContext inputMethodContext = InputMethodContext::New();
   // Enables the text input.
-  controller->EnableTextInput( decorator );
+  controller->EnableTextInput( decorator, inputMethodContext );
 
   // Enables the vertical scrolling after the text input has been enabled.
   controller->SetVerticalScrollEnabled( true );
@@ -423,6 +446,9 @@ void ConfigureTextEditor( ControllerPtr controller )
 
   // Disable the text elide.
   controller->SetTextElideEnabled( false );
+
+  // Disable match system language direction
+  controller->SetMatchLayoutDirection(DevelText::MatchLayoutDirection::CONTENTS);
 }
 
 } // namespace Text