Dali-Text: Keyboard Shortcuts
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / markup-processor.cpp
index 5bbe5ad..dac4f92 100755 (executable)
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/character-set-conversion.h>
 #include <dali-toolkit/internal/text/markup-processor-color.h>
+#include <dali-toolkit/internal/text/markup-processor-embedded-item.h>
 #include <dali-toolkit/internal/text/markup-processor-font.h>
 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
 #include <dali-toolkit/internal/text/xhtml-entities.h>
 
-
-
 namespace Dali
 {
 
@@ -53,6 +52,7 @@ const std::string XHTML_U_TAG("u");
 const std::string XHTML_SHADOW_TAG("shadow");
 const std::string XHTML_GLOW_TAG("glow");
 const std::string XHTML_OUTLINE_TAG("outline");
+const std::string XHTML_ITEM_TAG("item");
 
 const char LESS_THAN      = '<';
 const char GREATER_THAN   = '>';
@@ -66,7 +66,7 @@ const char SEMI_COLON     = ';';
 const char CHAR_ARRAY_END = '\0';
 const char HEX_CODE       = 'x';
 
-const char WHITE_SPACE    = 0x20; // ASCII value of the white space.
+const char WHITE_SPACE    = 0x20;        // ASCII value of the white space.
 
 // Range 1 0x0u < XHTML_DECIMAL_ENTITY_RANGE <= 0xD7FFu
 // Range 2 0xE000u < XHTML_DECIMAL_ENTITY_RANGE <= 0xFFFDu
@@ -153,6 +153,11 @@ void Initialize( FontDescriptionRun& fontRun )
  */
 void ParseAttributes( Tag& tag )
 {
+  if( tag.buffer == NULL )
+  {
+    return;
+  }
+
   tag.attributes.Resize( MAX_NUM_OF_ATTRIBUTES );
 
   // Find first the tag name.
@@ -305,12 +310,15 @@ bool IsTag( const char*& markupStringBuffer,
   bool isQuotationOpen = false;
   bool attributesFound = false;
   tag.isEndTag = false;
+  bool isPreviousLessThan = false;
+  bool isPreviousSlash = false;
 
   const char character = *markupStringBuffer;
   if( LESS_THAN == character ) // '<'
   {
     tag.buffer = NULL;
     tag.length = 0u;
+    isPreviousLessThan = true;
 
     // if the iterator is pointing to a '<' character, then check if it's a mark-up tag is needed.
     ++markupStringBuffer;
@@ -322,12 +330,20 @@ bool IsTag( const char*& markupStringBuffer,
       {
         const char character = *markupStringBuffer;
 
-        if( SLASH == character ) // '/'
+        if( !isQuotationOpen && ( SLASH == character ) ) // '/'
         {
-          // if the tag has a '/' then it's an end or empty tag.
-          tag.isEndTag = true;
+          if (isPreviousLessThan)
+          {
+            tag.isEndTag = true;
+          }
+          else
+          {
+            // if the tag has a '/' it may be an end tag.
+            isPreviousSlash = true;
+          }
 
-          if( ( markupStringBuffer + 1u < markupStringEndBuffer ) && ( WHITE_SPACE >= *( markupStringBuffer + 1u ) ) && ( !isQuotationOpen ) )
+          isPreviousLessThan = false;
+          if( ( markupStringBuffer + 1u < markupStringEndBuffer ) && ( WHITE_SPACE >= *( markupStringBuffer + 1u ) ) )
           {
             ++markupStringBuffer;
             SkipWhiteSpace( markupStringBuffer, markupStringEndBuffer );
@@ -337,11 +353,21 @@ bool IsTag( const char*& markupStringBuffer,
         else if( GREATER_THAN == character ) // '>'
         {
           isTag = true;
+          if (isPreviousSlash)
+          {
+            tag.isEndTag = true;
+          }
+
+          isPreviousSlash = false;
+          isPreviousLessThan = false;
         }
         else if( QUOTATION_MARK == character )
         {
           isQuotationOpen = !isQuotationOpen;
           ++tag.length;
+
+          isPreviousSlash = false;
+          isPreviousLessThan = false;
         }
         else if( WHITE_SPACE >= character ) // ' '
         {
@@ -361,6 +387,9 @@ bool IsTag( const char*& markupStringBuffer,
 
           // If it's not any of the 'special' characters then just add it to the tag string.
           ++tag.length;
+
+          isPreviousSlash = false;
+          isPreviousLessThan = false;
         }
       }
     }
@@ -461,6 +490,8 @@ bool XHTMLNumericEntityToUtf8 ( const char* markupText, char* utf8 )
 
 void ProcessMarkupString( const std::string& markupString, MarkupProcessData& markupProcessData )
 {
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "markupString: %s\n", markupString.c_str() );
+
   // Reserve space for the plain text.
   const Length markupStringSize = markupString.size();
   markupProcessData.markupProcessedText.reserve( markupStringSize );
@@ -472,6 +503,12 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
   StyleStack::RunIndex colorRunIndex = 0u;
   StyleStack::RunIndex fontRunIndex = 0u;
 
+  // check tag reference
+  int colorTagReference = 0u;
+  int fontTagReference = 0u;
+  int iTagReference = 0u;
+  int bTagReference = 0u;
+
   // Give an initial default value to the model's vectors.
   markupProcessData.colorRuns.Reserve( DEFAULT_VECTOR_SIZE );
   markupProcessData.fontRuns.Reserve( DEFAULT_VECTOR_SIZE );
@@ -484,6 +521,7 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
   CharacterIndex characterIndex = 0u;
   for( ; markupStringBuffer < markupStringEndBuffer; )
   {
+    tag.attributes.Clear();
     if( IsTag( markupStringBuffer,
                markupStringEndBuffer,
                tag ) )
@@ -510,12 +548,19 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
 
           // Point the next color run.
           ++colorRunIndex;
+
+          // Increase reference
+          ++colorTagReference;
         }
         else
         {
-          // Pop the top of the stack and set the number of characters of the run.
-          ColorRun& colorRun = *( markupProcessData.colorRuns.Begin() + styleStack.Pop() );
-          colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
+          if( colorTagReference > 0 )
+          {
+            // Pop the top of the stack and set the number of characters of the run.
+            ColorRun& colorRun = *( markupProcessData.colorRuns.Begin() + styleStack.Pop() );
+            colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
+            --colorTagReference;
+          }
         }
       } // <color></color>
       else if( TokenComparison( XHTML_I_TAG, tag.buffer, tag.length ) )
@@ -539,12 +584,19 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
 
           // Point the next free font run.
           ++fontRunIndex;
+
+          // Increase reference
+          ++iTagReference;
         }
         else
         {
-          // Pop the top of the stack and set the number of characters of the run.
-          FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
-          fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+          if( iTagReference > 0 )
+          {
+            // Pop the top of the stack and set the number of characters of the run.
+            FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
+            fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+            --iTagReference;
+          }
         }
       } // <i></i>
       else if( TokenComparison( XHTML_U_TAG, tag.buffer, tag.length ) )
@@ -579,12 +631,19 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
 
           // Point the next free font run.
           ++fontRunIndex;
+
+          // Increase reference
+          ++bTagReference;
         }
         else
         {
-          // Pop the top of the stack and set the number of characters of the run.
-          FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
-          fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+          if( bTagReference > 0 )
+          {
+            // Pop the top of the stack and set the number of characters of the run.
+            FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
+            fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+            --bTagReference;
+          }
         }
       } // <b></b>
       else if( TokenComparison( XHTML_FONT_TAG, tag.buffer, tag.length ) )
@@ -608,12 +667,19 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
 
           // Point the next free font run.
           ++fontRunIndex;
+
+          // Increase reference
+          ++fontTagReference;
         }
         else
         {
-          // Pop the top of the stack and set the number of characters of the run.
-          FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
-          fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+          if( fontTagReference > 0 )
+          {
+            // Pop the top of the stack and set the number of characters of the run.
+            FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
+            fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+            --fontTagReference;
+          }
         }
       } // <font></font>
       else if( TokenComparison( XHTML_SHADOW_TAG, tag.buffer, tag.length ) )
@@ -649,6 +715,22 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
           // Pop the top of the stack and set the number of characters of the run.
         }
       } // <outline></outline>
+      else if (TokenComparison(XHTML_ITEM_TAG, tag.buffer, tag.length))
+      {
+        if (tag.isEndTag)
+        {
+          // Create an embedded item instance.
+          EmbeddedItem item;
+          item.characterIndex = characterIndex;
+          ProcessEmbeddedItem(tag, item);
+
+          markupProcessData.items.PushBack(item);
+
+          // Insert white space character that will be replaced by the item.
+          markupProcessData.markupProcessedText.append( 1u, WHITE_SPACE );
+          ++characterIndex;
+        }
+      }
     }  // end if( IsTag() )
     else if( markupStringBuffer < markupStringEndBuffer )
     {
@@ -748,6 +830,14 @@ void ProcessMarkupString( const std::string& markupString, MarkupProcessData& ma
   else
   {
     markupProcessData.colorRuns.Resize( colorRunIndex );
+
+#ifdef DEBUG_ENABLED
+    for( unsigned int i=0; i<colorRunIndex; ++i )
+    {
+      ColorRun& run = markupProcessData.colorRuns[i];
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "run[%d] index: %d, length: %d, color %f,%f,%f,%f\n", i, run.characterRun.characterIndex, run.characterRun.numberOfCharacters, run.color.r, run.color.g, run.color.b, run.color.a );
+    }
+#endif
   }
 }