Using delete[] for pointer that created with new[]
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / logical-model-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 8af3e91..66c6d89
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 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.
@@ -38,12 +38,26 @@ void FreeFontFamilyNames( Vector<FontDescriptionRun>& fontDescriptionRuns )
        it != endIt;
        ++it )
   {
-    delete (*it).familyName;
+    delete[] (*it).familyName;
   }
 
   fontDescriptionRuns.Clear();
 }
 
+void FreeEmbeddedItems( Vector<EmbeddedItem>& embeddedItem )
+{
+  for( Vector<EmbeddedItem>::Iterator it = embeddedItem.Begin(),
+         endIt = embeddedItem.End();
+       it != endIt;
+       ++it )
+  {
+    EmbeddedItem& item = *it;
+    delete[] item.url;
+  }
+
+  embeddedItem.Clear();
+}
+
 LogicalModelPtr LogicalModel::New()
 {
   return LogicalModelPtr( new LogicalModel() );
@@ -53,9 +67,10 @@ Script LogicalModel::GetScript( CharacterIndex characterIndex ) const
 {
   // If this operation is too slow, consider a binary search.
 
+  const ScriptRun* const scriptRunBuffer = mScriptRuns.Begin();
   for( Length index = 0u, length = mScriptRuns.Count(); index < length; ++index )
   {
-    const ScriptRun* const scriptRun = mScriptRuns.Begin() + index;
+    const ScriptRun* const scriptRun = scriptRunBuffer + index;
 
     if( ( scriptRun->characterRun.characterIndex <= characterIndex ) &&
         ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) )
@@ -78,182 +93,188 @@ CharacterDirection LogicalModel::GetCharacterDirection( CharacterIndex character
   return *( mCharacterDirections.Begin() + characterIndex );
 }
 
-void LogicalModel::SetVisualToLogicalMap( const BidirectionalLineInfoRun* const bidirectionalInfo,
-                                          Length numberOfRuns,
-                                          CharacterIndex startIndex,
-                                          Length numberOfCharacters )
+CharacterIndex LogicalModel::GetLogicalCursorIndex( CharacterIndex visualCursorIndex )
 {
-  mVisualToLogicalMap.Resize( numberOfCharacters );
-  mLogicalToVisualMap.Resize( numberOfCharacters );
-
-  const Length numberOfCharactersPlus = numberOfCharacters + 1u;
-  mVisualToLogicalCursorMap.Resize( numberOfCharactersPlus );
+  // The character's directions buffer.
+  const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
 
-  CharacterIndex* modelVisualToLogicalMapBuffer = mVisualToLogicalMap.Begin();
-  CharacterIndex* modelLogicalToVisualMapBuffer = mLogicalToVisualMap.Begin();
+  // The bidirectional line info.
+  const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
 
-  CharacterIndex* modelVisualToLogicalCursorMap = mVisualToLogicalCursorMap.Begin();
+  // Whether the paragraph starts with a right to left character.
+  const bool isRightToLeftParagraph = bidirectionalLineInfo->direction;
 
-  CharacterIndex lastIndex = startIndex;
-  for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
-  {
-    const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
+  // The total number of characters of the line.
+  const Length lastCharacterIndex = bidirectionalLineInfo->characterRun.characterIndex + bidirectionalLineInfo->characterRun.numberOfCharacters;
 
-    if( bidiLineInfo.characterRun.characterIndex + bidiLineInfo.characterRun.numberOfCharacters <= startIndex )
-    {
-      // Skip this paragraph. It has been already processed.
-      continue;
-    }
+  CharacterIndex logicalCursorIndex = 0u;
 
-    if( lastIndex < bidiLineInfo.characterRun.characterIndex )
+  if( bidirectionalLineInfo->characterRun.characterIndex == visualCursorIndex )
+  {
+    if( isRightToLeftParagraph )
     {
-      // Fill with the identity.
-      for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
-      {
-        *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
-      }
+      logicalCursorIndex = lastCharacterIndex;
     }
-
-    // Fill the conversion table of the run.
-    for( CharacterIndex index = 0u;
-         index < bidiLineInfo.characterRun.numberOfCharacters;
-         ++index, ++lastIndex )
+    else // else logical position is the first of the line.
     {
-      *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
+      logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
     }
   }
-
-  // Complete with the identity if there are some left to right characters after the last right to left.
-  for( ; lastIndex < numberOfCharacters; ++lastIndex )
+  else if( lastCharacterIndex == visualCursorIndex )
   {
-    *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
+    if( isRightToLeftParagraph )
+    {
+      logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
+    }
+    else // else logical position is the number of characters.
+    {
+      logicalCursorIndex = lastCharacterIndex;
+    }
   }
-
-  // Sets the logical to visual conversion map.
-  for( CharacterIndex index = startIndex; index < numberOfCharacters; ++index )
+  else
   {
-    *( modelLogicalToVisualMapBuffer + *( modelVisualToLogicalMapBuffer + index ) ) = index;
-  }
+    // Get the character indexed by  index - 1 and index
+    // and calculate the logical position according the directions of
+    // both characters and the direction of the paragraph.
 
-  // Sets the visual to logical conversion map for cursor positions.
+    const CharacterIndex previousVisualCursorIndex = visualCursorIndex - 1u;
+    const CharacterIndex previousLogicalCursorIndex = *( bidirectionalLineInfo->visualToLogicalMap + previousVisualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex ) + bidirectionalLineInfo->characterRun.characterIndex;
+    const CharacterIndex currentLogicalCursorIndex = *( bidirectionalLineInfo->visualToLogicalMap + visualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex ) + bidirectionalLineInfo->characterRun.characterIndex;
 
-  const Length numberOfBidirectionalParagraphs = mBidirectionalParagraphInfo.Count();
-  BidirectionalParagraphInfoRun* bidirectionalParagraphInfoBuffer = mBidirectionalParagraphInfo.Begin();
-  BidirectionalParagraphInfoRun* bidirectionalParagraph = bidirectionalParagraphInfoBuffer;
-
-  const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
+    const CharacterDirection previousCharacterDirection = *( modelCharacterDirections + previousLogicalCursorIndex );
+    const CharacterDirection currentCharacterDirection = *( modelCharacterDirections + currentLogicalCursorIndex );
 
-  Length bidirectionalParagraphIndex = 0u;
-  bool isRightToLeftParagraph = false;
-  for( CharacterIndex index = startIndex; index < numberOfCharactersPlus; ++index )
-  {
-    if( bidirectionalParagraph &&
-        ( bidirectionalParagraph->characterRun.characterIndex == index ) )
-    {
-      isRightToLeftParagraph = *( modelCharacterDirections + index );
-    }
-
-    if( 0u == index )
+    if( previousCharacterDirection == currentCharacterDirection )
     {
-      if( isRightToLeftParagraph )
-      {
-        *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
-      }
-      else // else logical position is zero.
+      // Both glyphs have the same direction.
+      if( previousCharacterDirection )
       {
-        *( modelVisualToLogicalCursorMap + index ) = 0u;
+        logicalCursorIndex = previousLogicalCursorIndex;
       }
-    }
-    else if( numberOfCharacters == index )
-    {
-      if( isRightToLeftParagraph )
-      {
-        *( modelVisualToLogicalCursorMap + index ) = 0u;
-      }
-      else // else logical position is the number of characters.
+      else
       {
-        *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
+        logicalCursorIndex = currentLogicalCursorIndex;
       }
     }
     else
     {
-      // Get the character indexed by  index - 1 and index
-      // and calculate the logical position according the directions of
-      // both characters and the direction of the paragraph.
-
-      const CharacterIndex previousIndex = index - 1u;
-      const CharacterIndex logicalPosition0 = *( modelVisualToLogicalMapBuffer + previousIndex );
-      const CharacterIndex logicalPosition1 = *( modelVisualToLogicalMapBuffer + index );
-
-      const CharacterDirection direction0 = *( modelCharacterDirections + logicalPosition0 );
-      const CharacterDirection direction1 = *( modelCharacterDirections + logicalPosition1 );
-
-      if( direction0 == direction1 )
+      if( isRightToLeftParagraph )
       {
-        // Both glyphs have the same direction.
-        if( direction0 )
+        if( currentCharacterDirection )
         {
-          *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
+          logicalCursorIndex = currentLogicalCursorIndex + 1u;
         }
         else
         {
-          *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
+          logicalCursorIndex = previousLogicalCursorIndex;
         }
       }
       else
       {
-        if( isRightToLeftParagraph )
+        if( previousCharacterDirection )
         {
-          if( direction1 )
-          {
-            *( modelVisualToLogicalCursorMap + index ) = logicalPosition1 + 1u;
-          }
-          else
-          {
-            *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
-          }
+          logicalCursorIndex = currentLogicalCursorIndex;
         }
         else
         {
-          if( direction0 )
-          {
-            *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
-          }
-          else
-          {
-            *( modelVisualToLogicalCursorMap + index ) = logicalPosition0 + 1u;
-          }
+          logicalCursorIndex = previousLogicalCursorIndex + 1u;
         }
       }
     }
+  }
+
+  return logicalCursorIndex;
+}
+
+CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex )
+{
+  // The bidirectional line info.
+  const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
+
+  return *( bidirectionalLineInfo->visualToLogicalMap + visualCharacterIndex - bidirectionalLineInfo->characterRun.characterIndex ) + bidirectionalLineInfo->characterRun.characterIndex;
+}
 
-    if( bidirectionalParagraph &&
-        ( bidirectionalParagraph->characterRun.characterIndex + bidirectionalParagraph->characterRun.numberOfCharacters == index ) )
+bool LogicalModel::FetchBidirectionalLineInfo( CharacterIndex characterIndex )
+{
+  // The number of bidirectional lines.
+  const Length numberOfBidirectionalLines = mBidirectionalLineInfo.Count();
+
+  if( 0u == numberOfBidirectionalLines )
+  {
+    // If there is no bidirectional info.
+    return false;
+  }
+
+  // Find the bidi line where the character is laid-out.
+
+  const BidirectionalLineInfoRun* const bidirectionalLineInfoBuffer = mBidirectionalLineInfo.Begin();
+
+  // Check first if the character is in the previously fetched line.
+
+  BidirectionalLineRunIndex bidiLineIndex = 0u;
+  CharacterIndex lastCharacterOfRightToLeftRun = 0u;
+  if( mBidirectionalLineIndex < numberOfBidirectionalLines )
+  {
+    const BidirectionalLineInfoRun& bidiLineRun = *( bidirectionalLineInfoBuffer + mBidirectionalLineIndex );
+
+    const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
+    if( ( bidiLineRun.characterRun.characterIndex <= characterIndex ) &&
+        ( characterIndex < lastCharacterOfRunPlusOne ) )
+    {
+      // The character is in the previously fetched bidi line.
+      return true;
+    }
+    else
     {
-      isRightToLeftParagraph = false;
-      ++bidirectionalParagraphIndex;
-      if( bidirectionalParagraphIndex < numberOfBidirectionalParagraphs )
+      // The character is not in the previously fetched line.
+      // Set the bidi line index from where to start the fetch.
+
+      if( characterIndex < bidiLineRun.characterRun.characterIndex )
       {
-        bidirectionalParagraph = bidirectionalParagraphInfoBuffer + bidirectionalParagraphIndex;
+        // Start the fetch from the beginning.
+        bidiLineIndex = 0u;
       }
       else
       {
-        bidirectionalParagraph = NULL;
+        // Start the fetch from the next line.
+        bidiLineIndex = mBidirectionalLineIndex + 1u;
+        lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
       }
     }
   }
-}
 
-CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
-{
-  if( 0u == mVisualToLogicalMap.Count() )
+  // The character has not been found in the previously fetched bidi line.
+  for( Vector<BidirectionalLineInfoRun>::ConstIterator it = bidirectionalLineInfoBuffer + bidiLineIndex,
+         endIt = mBidirectionalLineInfo.End();
+       it != endIt;
+       ++it, ++bidiLineIndex )
   {
-    // If there is no visual to logical info is because the whole text is left to right.
-    // Return the identity.
-    return visualCharacterIndex;
+    const BidirectionalLineInfoRun& bidiLineRun = *it;
+
+    if( ( lastCharacterOfRightToLeftRun < characterIndex ) &&
+        ( characterIndex < bidiLineRun.characterRun.characterIndex ) )
+    {
+      // The character is not inside a bidi line.
+      return false;
+    }
+
+    const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
+    lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
+    if( ( bidiLineRun.characterRun.characterIndex <= characterIndex ) &&
+        ( characterIndex < lastCharacterOfRunPlusOne ) )
+    {
+      // Bidi line found. Fetch the line.
+      mBidirectionalLineIndex = bidiLineIndex;
+      return true;
+    }
   }
 
-  return *( mVisualToLogicalMap.Begin() + visualCharacterIndex );
+  return false;
+}
+
+BidirectionalLineRunIndex LogicalModel::GetBidirectionalLineInfo() const
+{
+  return mBidirectionalLineIndex;
 }
 
 void LogicalModel::UpdateTextStyleRuns( CharacterIndex index, int numberOfCharacters )
@@ -307,6 +328,7 @@ void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
   if( colorOverriden )
   {
     style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
+    style.isDefaultColor = false;
   }
 
   // Reset the run index.
@@ -372,7 +394,7 @@ void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
     const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
 
     style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
-    style.familyDefined = true;
+    style.isFamilyDefined = true;
   }
 
   // Set the font's weight if it's overriden.
@@ -381,7 +403,7 @@ void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
     const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
 
     style.weight = fontDescriptionRun.weight;
-    style.weightDefined = true;
+    style.isWeightDefined = true;
   }
 
   // Set the font's width if it's overriden.
@@ -390,7 +412,7 @@ void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
     const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
 
     style.width = fontDescriptionRun.width;
-    style.widthDefined = true;
+    style.isWidthDefined = true;
   }
 
   // Set the font's slant if it's overriden.
@@ -399,7 +421,7 @@ void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
     const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
 
     style.slant = fontDescriptionRun.slant;
-    style.slantDefined = true;
+    style.isSlantDefined = true;
   }
 
   // Set the font's size if it's overriden.
@@ -408,11 +430,8 @@ void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
     const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
 
     style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
-    style.sizeDefined = true;
+    style.isSizeDefined = true;
   }
-
-  // Reset the run index.
-  runIndex = 0u;
 }
 
 void LogicalModel::ClearFontDescriptionRuns()
@@ -420,12 +439,144 @@ void LogicalModel::ClearFontDescriptionRuns()
   FreeFontFamilyNames( mFontDescriptionRuns );
 }
 
+void LogicalModel::CreateParagraphInfo( CharacterIndex startIndex,
+                                        Length numberOfCharacters )
+{
+  const Length totalNumberOfCharacters = mLineBreakInfo.Count();
+
+  // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
+  Vector<CharacterIndex> paragraphs;
+  paragraphs.Reserve( numberOfCharacters );
+  const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
+  const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
+  for( Length index = startIndex; index < lastCharacterIndexPlusOne; ++index )
+  {
+    if( TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index ) )
+    {
+      paragraphs.PushBack( index );
+    }
+  }
+
+  // Whether the current paragraphs are updated or set from scratch.
+  const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
+
+  // Reserve space for current paragraphs plus new ones.
+  const Length numberOfNewParagraphs = paragraphs.Count();
+  const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
+  mParagraphInfo.Resize( totalNumberOfParagraphs );
+
+  ParagraphRun* paragraphInfoBuffer = NULL;
+  Vector<ParagraphRun> newParagraphs;
+
+  if( updateCurrentParagraphs )
+  {
+    newParagraphs.Resize( numberOfNewParagraphs );
+    paragraphInfoBuffer = newParagraphs.Begin();
+  }
+  else
+  {
+    paragraphInfoBuffer = mParagraphInfo.Begin();
+  }
+
+  // Find where to insert the new paragraphs.
+  ParagraphRunIndex paragraphIndex = 0u;
+  CharacterIndex firstIndex = startIndex;
+
+  if( updateCurrentParagraphs )
+  {
+    for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
+           endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
+         it != endIt;
+         ++it )
+    {
+      const ParagraphRun& paragraph( *it );
+
+      if( startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters )
+      {
+        firstIndex = paragraph.characterRun.characterIndex;
+        break;
+      }
+
+      ++paragraphIndex;
+    }
+  }
+
+  // Create the paragraph info.
+  ParagraphRunIndex newParagraphIndex = 0u;
+  for( Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
+         endIt = paragraphs.End();
+       it != endIt;
+       ++it, ++newParagraphIndex )
+  {
+    const CharacterIndex index = *it;
+
+    ParagraphRun& paragraph = *( paragraphInfoBuffer + newParagraphIndex );
+    paragraph.characterRun.characterIndex = firstIndex;
+    paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
+
+    firstIndex += paragraph.characterRun.numberOfCharacters;
+  }
+
+
+  // Insert the new paragraphs.
+  if( updateCurrentParagraphs )
+  {
+    mParagraphInfo.Insert( mParagraphInfo.Begin() + paragraphIndex,
+                           newParagraphs.Begin(),
+                           newParagraphs.End() );
+
+    mParagraphInfo.Resize( totalNumberOfParagraphs );
+
+    // Update the next paragraph indices.
+    for( Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
+           endIt = mParagraphInfo.End();
+         it != endIt;
+         ++it )
+    {
+      ParagraphRun& paragraph( *it );
+
+      paragraph.characterRun.characterIndex += numberOfCharacters;
+    }
+  }
+}
+
+void LogicalModel::FindParagraphs( CharacterIndex index,
+                                   Length numberOfCharacters,
+                                   Vector<ParagraphRunIndex>& paragraphs )
+{
+  // Reserve som space for the paragraph indices.
+  paragraphs.Reserve( mParagraphInfo.Count() );
+
+  // Traverse the paragraphs to find which ones contain the given characters.
+  ParagraphRunIndex paragraphIndex = 0u;
+  for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
+         endIt = mParagraphInfo.End();
+       it != endIt;
+       ++it, ++paragraphIndex )
+  {
+    const ParagraphRun& paragraph( *it );
+
+    if( ( paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index ) &&
+        ( paragraph.characterRun.characterIndex < index + numberOfCharacters ) )
+    {
+      paragraphs.PushBack( paragraphIndex );
+    }
+  }
+}
+
+void LogicalModel::ClearEmbeddedImages()
+{
+  FreeEmbeddedItems( mEmbeddedItems );
+}
+
 LogicalModel::~LogicalModel()
 {
   ClearFontDescriptionRuns();
+  ClearEmbeddedImages();
 }
 
 LogicalModel::LogicalModel()
+: mBidirectionalLineIndex( 0u )
 {
 }