TextView - Right to Left implementation.
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-view / text-view-word-processor.cpp
index 6e3f8cc..800a96b 100644 (file)
@@ -39,33 +39,6 @@ namespace
 
 const std::string EMOJI_FONT_NAME( "SamsungEmoji" ); // Emoticons font family name.
 
-/**
- * Updates the word size and ascender.
- *
- * It's called after deleting some characters.
- *
- * @param[in] wordLayout The word layout info.
- */
-void UpdateLayoutInfo( WordLayoutInfo& wordLayout )
-{
-  // Initialize layout info for the whole word.
-  wordLayout.mSize = Size();
-  wordLayout.mAscender = 0.f;
-
-  // Traverse the character layout info to update the word layout.
-  for( CharacterLayoutInfoContainer::iterator layoutIt = wordLayout.mCharactersLayoutInfo.begin(), layoutEndIt = wordLayout.mCharactersLayoutInfo.end();
-       layoutIt != layoutEndIt;
-       ++layoutIt )
-  {
-    // Layout info for the current character.
-    CharacterLayoutInfo& layoutInfo( *layoutIt );
-
-    // Update layout info for the current word.
-    UpdateSize( wordLayout.mSize, layoutInfo.mSize );
-    wordLayout.mAscender = std::max( wordLayout.mAscender, layoutInfo.mAscender );
-  }
-}
-
 } // namespace
 
 /////////////////////
@@ -76,14 +49,20 @@ WordLayoutInfo::WordLayoutInfo()
 : mSize(),
   mAscender( 0.f ),
   mType( NoSeparator ),
+  mFirstCharacter( 0u ),
   mCharactersLayoutInfo()
 {
 }
 
+WordLayoutInfo::~WordLayoutInfo()
+{
+}
+
 WordLayoutInfo::WordLayoutInfo( const WordLayoutInfo& word )
 : mSize( word.mSize ),
   mAscender( word.mAscender ),
   mType( word.mType ),
+  mFirstCharacter( word.mFirstCharacter ),
   mCharactersLayoutInfo( word.mCharactersLayoutInfo )
 {
 }
@@ -93,142 +72,159 @@ WordLayoutInfo& WordLayoutInfo::operator=( const WordLayoutInfo& word )
   mSize = word.mSize;
   mAscender = word.mAscender;
   mType = word.mType;
+  mFirstCharacter = word.mFirstCharacter;
   mCharactersLayoutInfo = word.mCharactersLayoutInfo;
 
   return *this;
 }
 
-void CreateWordTextInfo( const MarkupProcessor::StyledTextArray& word,
-                         TextViewProcessor::WordLayoutInfo& wordLayoutInfo )
+void CreateWordTextInfo( const Text& paragraph,
+                         Vector<TextStyle*>& textStyles,
+                         WordLayoutInfo& wordLayoutInfo )
 {
   DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "-->TextViewProcessor::CreateWordTextInfo\n" );
   // Split in characters.
-  for( MarkupProcessor::StyledTextArray::const_iterator charIt = word.begin(), charEndIt = word.end(); charIt != charEndIt; ++charIt )
+  std::size_t characterIndex = wordLayoutInfo.mFirstCharacter;
+  for( CharacterLayoutInfoContainer::iterator it = wordLayoutInfo.mCharactersLayoutInfo.begin(),
+         endIt =  wordLayoutInfo.mCharactersLayoutInfo.end();
+       it != endIt;
+       ++it, ++characterIndex )
   {
-    const MarkupProcessor::StyledText& styledText( *charIt );
+    // Gets a reference of the character's layout info.
+    CharacterLayoutInfo& characterLayoutInfo( *it );
 
-    const std::size_t length = styledText.mText.GetLength();
+    // Gets the character and the style for that character from the paragraph.
+    Character character = paragraph[characterIndex];
+    TextStyle* textStyle = *( textStyles.Begin() + characterIndex );
 
-    // It could be a group of characters.
-    for( std::size_t index = 0; index < length; ++index )
-    {
-      MarkupProcessor::StyledText styledCharacter;
-      styledCharacter.mStyle = styledText.mStyle;
-      Character character = styledText.mText[index];
-      styledCharacter.mText.Append( character );
+    // Checks whether the character is an emoticon.
+    characterLayoutInfo.mIsColorGlyph = GlyphImage::IsColorGlyph( character );
+    DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "  Is color glyph: %s\n", ( characterLayoutInfo.mIsColorGlyph ? "True" : "False" ) );
 
-      // Create layout character info.
-      CharacterLayoutInfo characterLayoutInfo;
+    if( characterLayoutInfo.mIsColorGlyph )
+    {
+      // If the character is an emoticon a predefined font is set.
+      textStyle->SetFontName( EMOJI_FONT_NAME );
+    }
+    else
+    {
+      // Checks if the font family and the font style set in the text style supports the character.
+      // If not, it chooses the right font for the given character and style.
+      ChooseFontFamilyName( character, *textStyle );
+    }
 
-      characterLayoutInfo.mIsColorGlyph = GlyphImage::IsColorGlyph( character );
-      DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "  Is color glyph: %s\n", ( characterLayoutInfo.mIsColorGlyph ? "True" : "False" ) );
+    // Gets the metrics of the font.
+    const Font font = Font::New( FontParameters( textStyle->GetFontName(), textStyle->GetFontStyle(), textStyle->GetFontPointSize() ) );
+    const Font::Metrics metrics = font.GetMetrics( character );
+    const float ascender = font.GetAscender();
 
-      if( characterLayoutInfo.mIsColorGlyph )
-      {
-        styledCharacter.mStyle.SetFontName( EMOJI_FONT_NAME );
-      }
-      else
-      {
-        //Choose the right font for the given character and style.
-        ChooseFontFamilyName( styledCharacter );
-      }
+    // Fill Natural size info for current character.
 
-      const Font font = Font::New( FontParameters( styledCharacter.mStyle.GetFontName(), styledCharacter.mStyle.GetFontStyle(), styledCharacter.mStyle.GetFontPointSize() ) );
-      const Font::Metrics metrics = font.GetMetrics( character );
-      const float ascender = font.GetAscender();
+    // The font line's height is used as character's height.
+    characterLayoutInfo.mSize.height = font.GetLineHeight();
 
-      // Fill Natural size info for current character.
-      characterLayoutInfo.mHeight = font.GetLineHeight();
-      characterLayoutInfo.mAdvance = metrics.GetAdvance();
-      characterLayoutInfo.mBearing = metrics.GetBearing();
+    // The character's advance is used as charcter's width.
+    characterLayoutInfo.mSize.width = metrics.GetAdvance();
 
-      if( character.IsNewLine() && !characterLayoutInfo.mIsColorGlyph )
-      {
-        // A new line character doesn't have any width.
-        characterLayoutInfo.mSize.width = 0.f;
-      }
-      else
-      {
-        // Uses advance as width.
-        characterLayoutInfo.mSize.width = characterLayoutInfo.mAdvance;
-      }
-      characterLayoutInfo.mSize.height = characterLayoutInfo.mHeight;
-      characterLayoutInfo.mAscender = ascender;
+    // The ascender and bearing are used to position correctly glyphs of different font sizes.
+    characterLayoutInfo.mAscender = ascender;
+    characterLayoutInfo.mBearing = metrics.GetBearing();
 
-      if( styledCharacter.mStyle.IsUnderlineEnabled() )
-      {
-        characterLayoutInfo.mUnderlineThickness = font.GetUnderlineThickness(); // Both thickness and position includes the
-        characterLayoutInfo.mUnderlinePosition = font.GetUnderlinePosition();   // vertical pad adjust used in effects like glow or shadow.
-      }
+    if( character.IsNewLine() && !characterLayoutInfo.mIsColorGlyph )
+    {
+      // A new paragraph character '\n'  doesn't have any width.
+      characterLayoutInfo.mSize.width = 0.f;
+    }
 
-      // stores the styled text.
-      characterLayoutInfo.mStyledText.mText = styledCharacter.mText;
-      characterLayoutInfo.mStyledText.mStyle = styledCharacter.mStyle;
+    // Set's the underline thickness and position.
+    // Both thickness and position includes the vertical pad adjust used in effects like glow or shadow.
+    if( textStyle->IsUnderlineEnabled() )
+    {
+      characterLayoutInfo.mUnderlineThickness = font.GetUnderlineThickness();
+      characterLayoutInfo.mUnderlinePosition = font.GetUnderlinePosition();
+    }
 
-      // Add character layout info to the word layout info and update it.
-      wordLayoutInfo.mCharactersLayoutInfo.push_back( characterLayoutInfo );
-      UpdateSize( wordLayoutInfo.mSize, characterLayoutInfo.mSize );
-      wordLayoutInfo.mAscender = std::max( wordLayoutInfo.mAscender, characterLayoutInfo.mAscender );
-      wordLayoutInfo.mType = GetTextSeparatorType( character );
-    } // end of each character in the group of characters.
+    // Updates the word size and ascender.
+    UpdateSize( wordLayoutInfo.mSize, characterLayoutInfo.mSize );
+    wordLayoutInfo.mAscender = std::max( wordLayoutInfo.mAscender, characterLayoutInfo.mAscender );
   } // end of characters in the word.
   DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "<--TextViewProcessor::CreateWordTextInfo\n" );
 }
 
+void UpdateLayoutInfo( WordLayoutInfo& wordLayout )
+{
+  // Initialize layout info for the whole word.
+  wordLayout.mSize = Size::ZERO;
+  wordLayout.mAscender = 0.f;
+
+  // Traverse the character layout info to update the word layout.
+  for( CharacterLayoutInfoContainer::iterator layoutIt = wordLayout.mCharactersLayoutInfo.begin(), layoutEndIt = wordLayout.mCharactersLayoutInfo.end();
+       layoutIt != layoutEndIt;
+       ++layoutIt )
+  {
+    // Layout info for the current character.
+    CharacterLayoutInfo& layoutInfo( *layoutIt );
+
+    // Update layout info for the current word.
+    UpdateSize( wordLayout.mSize, layoutInfo.mSize );
+    wordLayout.mAscender = std::max( wordLayout.mAscender, layoutInfo.mAscender );
+  }
+}
+
 void RemoveCharactersFromWordInfo( TextView::RelayoutData& relayoutData,
                                    const std::size_t numberOfCharacters,
                                    bool& mergeWords,
-                                   bool& mergeLines,
-                                   TextViewProcessor::TextInfoIndices& textInfoIndicesBegin,
-                                   TextViewProcessor::TextInfoIndices& textInfoIndicesEnd,
-                                   TextViewProcessor::TextInfoIndices& textInfoMergeIndicesBegin,
-                                   TextViewProcessor::TextInfoIndices& textInfoMergeIndicesEnd,
-                                   TextViewProcessor::WordGroupLayoutInfo& groupLayout,
+                                   bool& mergeParagraphs,
+                                   TextInfoIndices& textInfoIndicesBegin,
+                                   TextInfoIndices& textInfoIndicesEnd,
+                                   TextInfoIndices& textInfoMergeIndicesBegin,
+                                   TextInfoIndices& textInfoMergeIndicesEnd,
+                                   ParagraphLayoutInfo& paragraphLayout,
                                    std::vector<TextActor>& removedTextActors )
 {
-  const TextViewProcessor::TextLayoutInfo& textLayoutInfo = relayoutData.mTextLayoutInfo;
+  const TextLayoutInfo& textLayoutInfo = relayoutData.mTextLayoutInfo;
 
   // Get the word.
-  WordLayoutInfo& wordLayout( *( groupLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex ) );
+  WordLayoutInfo& wordLayout( *( paragraphLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex ) );
 
-  if( TextViewProcessor::LineSeparator == wordLayout.mType )
+  if( ParagraphSeparator == wordLayout.mType )
   {
-    // If the word is a line separator and there is more lines, then current line and the line after need to be merged.
-    if( textInfoIndicesBegin.mLineIndex + 1 < textLayoutInfo.mLinesLayoutInfo.size() )
+    // If the word is a paragraph separator and there is more paragraphs, then current paragraph and the paragraph after need to be merged.
+    if( textInfoIndicesBegin.mParagraphIndex + 1u < textLayoutInfo.mParagraphsLayoutInfo.size() )
     {
-      // current line is not the last one.
+      // current paragraph is not the last one.
 
-      // Update indices to merge lines.
-      textInfoMergeIndicesBegin.mLineIndex = textInfoIndicesBegin.mLineIndex;
-      textInfoMergeIndicesEnd.mLineIndex = textInfoIndicesBegin.mLineIndex + 1;
+      // Update indices to merge paragraphs.
+      textInfoMergeIndicesBegin.mParagraphIndex = textInfoIndicesBegin.mParagraphIndex;
+      textInfoMergeIndicesEnd.mParagraphIndex = textInfoIndicesBegin.mParagraphIndex + 1u;
 
-      mergeLines = true;
+      mergeParagraphs = true;
 
-      ++textInfoIndicesBegin.mLineIndex; // increase both indices,
-      textInfoIndicesEnd.mLineIndex +=2; // will delete last line.
+      ++textInfoIndicesBegin.mParagraphIndex; // increase both indices,
+      textInfoIndicesEnd.mParagraphIndex += 2u; // will delete last paragraph.
     }
 
-    ++textInfoIndicesEnd.mWordIndex; //will delete the line separator;
+    ++textInfoIndicesEnd.mWordIndex; //will delete the paragraph separator;
   }
   else if( WordSeparator == wordLayout.mType )
   {
     // If the word is a word separator. Check if the word before and the word after can be merged.
 
-    if( ( 0 < textInfoIndicesBegin.mWordIndex ) && ( groupLayout.mWordsLayoutInfo.size() > textInfoIndicesBegin.mWordIndex + 1 ) )
+    if( ( 0u < textInfoIndicesBegin.mWordIndex ) && ( paragraphLayout.mWordsLayoutInfo.size() > textInfoIndicesBegin.mWordIndex + 1u ) )
     {
-      const WordLayoutInfo& wordLayoutBefore( *( groupLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex - 1 ) );
-      const WordLayoutInfo& wordLayoutAfter( *( groupLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex + 1 ) );
+      const WordLayoutInfo& wordLayoutBefore( *( paragraphLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex - 1u ) );
+      const WordLayoutInfo& wordLayoutAfter( *( paragraphLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex + 1u ) );
 
       if( ( NoSeparator == wordLayoutBefore.mType ) && ( NoSeparator == wordLayoutAfter.mType ) )
       {
-        // This word is a word separator (white space) and is not the first word of the group nor the last one.
+        // This word is a word separator (white space) and is not the first word of the paragraph nor the last one.
         mergeWords = true;
 
         // Set indices to merge the words.
-        textInfoMergeIndicesBegin.mWordIndex = textInfoIndicesBegin.mWordIndex - 1; // word before word separator.
-        textInfoMergeIndicesEnd.mWordIndex = textInfoIndicesBegin.mWordIndex + 1; // word after word separator.
+        textInfoMergeIndicesBegin.mWordIndex = textInfoIndicesBegin.mWordIndex - 1u; // word before word separator.
+        textInfoMergeIndicesEnd.mWordIndex = textInfoIndicesBegin.mWordIndex + 1u; // word after word separator.
 
-        textInfoIndicesEnd.mWordIndex += 2; // will delete the word separator and the merged word.
+        textInfoIndicesEnd.mWordIndex += 2u; // will delete the word separator and the merged word.
       }
       else
       {
@@ -264,7 +260,7 @@ void RemoveCharactersFromWord( const std::size_t position,
   // Removes a given number of characters from the given word starting from the 'position' index.
 
   // Early return.
-  if( 0 == numberOfCharacters )
+  if( 0u == numberOfCharacters )
   {
     // nothing to do if the number of characters is zero.
 
@@ -286,7 +282,7 @@ void SplitWord( const std::size_t position,
   // It moves characters from the first part of the word to the last one.
 
   // early returns
-  if( 0 == position )
+  if( 0u == position )
   {
     // the whole word goes to the last part of the word.
     lastWordLayoutInfo = firstWordLayoutInfo;
@@ -348,8 +344,8 @@ void MergeWord( WordLayoutInfo& firstWordLayoutInfo,
 
   if( ( NoSeparator != firstWordLayoutInfo.mType ) || ( NoSeparator != lastWordLayoutInfo.mType ) )
   {
-    // Do not merge white spaces or new line characters.
-    DALI_ASSERT_ALWAYS( !"TextViewProcessor::MergeWord(). ERROR: White spaces or new line characters can't be merged with other words." );
+    // Do not merge white spaces or new paragraph characters.
+    DALI_ASSERT_ALWAYS( !"TextViewProcessor::MergeWord(). ERROR: White spaces or new paragraph characters can't be merged with other words." );
   }
 
   // Merge layout info
@@ -380,7 +376,7 @@ CharacterLayoutInfo GetLastCharacterLayoutInfo( const WordLayoutInfo& wordLayout
 
   if( !wordLayoutInfo.mCharactersLayoutInfo.empty() )
   {
-    layoutInfo = *( wordLayoutInfo.mCharactersLayoutInfo.end() - 1 );
+    layoutInfo = *( wordLayoutInfo.mCharactersLayoutInfo.end() - 1u );
   }
 
   return layoutInfo;
@@ -406,9 +402,9 @@ void CollectTextActors( std::vector<TextActor>& textActors, const WordLayoutInfo
   }
 }
 
-void CollectTextActorsFromWords( std::vector<TextActor>& textActors, const WordGroupLayoutInfo& group, const std::size_t wordIndexBegin, const std::size_t wordIndexEnd )
+void CollectTextActorsFromWords( std::vector<TextActor>& textActors, const ParagraphLayoutInfo& paragraph, const std::size_t wordIndexBegin, const std::size_t wordIndexEnd )
 {
-  for( WordLayoutInfoContainer::const_iterator wordIt = group.mWordsLayoutInfo.begin() + wordIndexBegin, wordEndIt = group.mWordsLayoutInfo.begin() + wordIndexEnd;
+  for( WordLayoutInfoContainer::const_iterator wordIt = paragraph.mWordsLayoutInfo.begin() + wordIndexBegin, wordEndIt = paragraph.mWordsLayoutInfo.begin() + wordIndexEnd;
        wordIt != wordEndIt;
        ++wordIt )
   {