Merge "Improve the underline markup" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / markup-processor.cpp
index b656aa4..197dff1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
 #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/markup-processor-paragraph.h>
 #include <dali-toolkit/internal/text/markup-processor-span.h>
+#include <dali-toolkit/internal/text/markup-processor-strikethrough.h>
+#include <dali-toolkit/internal/text/markup-processor-underline.h>
 #include <dali-toolkit/internal/text/xhtml-entities.h>
 
 namespace Dali
@@ -57,6 +60,8 @@ const std::string XHTML_ITEM_TAG("item");
 const std::string XHTML_ANCHOR_TAG("a");
 const std::string XHTML_BACKGROUND_TAG("background");
 const std::string XHTML_SPAN_TAG("span");
+const std::string XHTML_STRIKETHROUGH_TAG("s");
+const std::string XHTML_PARAGRAPH_TAG("p");
 
 const char LESS_THAN      = '<';
 const char GREATER_THAN   = '>';
@@ -71,13 +76,14 @@ const char CHAR_ARRAY_END = '\0';
 const char HEX_CODE       = 'x';
 
 const char WHITE_SPACE = 0x20; // ASCII value of the white space.
+const char NEW_LINE    = 0x0A; // ASCII value of the newline.
 
 // Range 1 0x0u < XHTML_DECIMAL_ENTITY_RANGE <= 0xD7FFu
 // Range 2 0xE000u < XHTML_DECIMAL_ENTITY_RANGE <= 0xFFFDu
 // Range 3 0x10000u < XHTML_DECIMAL_ENTITY_RANGE <= 0x10FFFFu
 const unsigned long XHTML_DECIMAL_ENTITY_RANGE[] = {0x0u, 0xD7FFu, 0xE000u, 0xFFFDu, 0x10000u, 0x10FFFFu};
 
-const unsigned int MAX_NUM_OF_ATTRIBUTES = 5u;  ///< The font tag has the 'family', 'size' 'weight', 'width' and 'slant' attrubutes.
+const unsigned int MAX_NUM_OF_ATTRIBUTES = 11u; ///< The span tag has the 'font-family', 'font-size' 'font-weight', 'font-width', 'font-slant','text-color', 'u-color', 'u-height','u-type','u-dash-gap'and 'u-dash-width' attrubutes.
 const unsigned int DEFAULT_VECTOR_SIZE   = 16u; ///< Default size of run vectors.
 
 #if defined(DEBUG_ENABLED)
@@ -134,8 +140,10 @@ struct Span
 {
   RunIndex colorRunIndex;
   RunIndex fontRunIndex;
+  RunIndex underlinedCharacterRunIndex;
   bool     isColorDefined;
   bool     isFontDefined;
+  bool     isUnderlinedCharacterDefined;
 };
 
 /**
@@ -189,10 +197,35 @@ void Initialize(UnderlinedCharacterRun& underlinedCharacterRun)
  */
 void Initialize(Span& span)
 {
-  span.colorRunIndex  = 0u;
-  span.isColorDefined = false;
-  span.fontRunIndex   = 0u;
-  span.isFontDefined  = false;
+  span.colorRunIndex                = 0u;
+  span.isColorDefined               = false;
+  span.fontRunIndex                 = 0u;
+  span.isFontDefined                = false;
+  span.underlinedCharacterRunIndex  = 0u;
+  span.isUnderlinedCharacterDefined = false;
+}
+
+/**
+ * @brief Initializes a strikethrough character run to its defaults.
+ *
+ * @param[in,out] strikethroughCharacterRun The strikethrough character run to initialize.
+ */
+void Initialize(StrikethroughCharacterRun& strikethroughCharacterRun)
+{
+  strikethroughCharacterRun.characterRun.characterIndex     = 0u;
+  strikethroughCharacterRun.characterRun.numberOfCharacters = 0u;
+  strikethroughCharacterRun.isColorSet                      = false;
+}
+
+/**
+ * @brief Initializes a  bounded-paragraph character run to its defaults.
+ *
+ * @param[in,out] boundedParagraphRun The bounded paragraphRun run to initialize.
+ */
+void Initialize(BoundedParagraphRun& boundedParagraphRun)
+{
+  boundedParagraphRun.characterRun.characterIndex     = 0u;
+  boundedParagraphRun.characterRun.numberOfCharacters = 0u;
 }
 
 /**
@@ -622,6 +655,30 @@ void ProcessItemTag(
 }
 
 /**
+ * @brief Processes the paragraph-tag
+ *
+ * @param[in/out] markupProcessData The markup process data
+ * @param[in] tag The current tag
+ * @param[in] isEndBuffer Whether the end of buffer
+ * @param[in/out] characterIndex The current character index
+ */
+void ProcessParagraphTag(
+  MarkupProcessData& markupProcessData,
+  const Tag          tag,
+  bool               isEndBuffer,
+  CharacterIndex&    characterIndex)
+{
+  if((characterIndex > 0 &&
+      markupProcessData.markupProcessedText[characterIndex - 1u] != NEW_LINE) &&
+     (!(tag.isEndTag && isEndBuffer)))
+  {
+    // Insert new-line character at the start and end of paragraph.
+    markupProcessData.markupProcessedText.append(1u, NEW_LINE);
+    ++characterIndex;
+  }
+}
+
+/**
  * @brief Processes the anchor tag
  *
  * @param[in/out] markupProcessData The markup process data
@@ -666,14 +723,16 @@ void ProcessAnchorTag(
  * @param[in] tagReference The tagReference we should increment/decrement
  */
 void ProcessSpanForRun(
-  const Tag&                  spanTag,
-  StyleStack<Span>&           spanStack,
-  Vector<ColorRun>&           colorRuns,
-  Vector<FontDescriptionRun>& fontRuns,
-  RunIndex&                   colorRunIndex,
-  RunIndex&                   fontRunIndex,
-  const CharacterIndex        characterIndex,
-  int&                        tagReference)
+  const Tag&                      spanTag,
+  StyleStack<Span>&               spanStack,
+  Vector<ColorRun>&               colorRuns,
+  Vector<FontDescriptionRun>&     fontRuns,
+  Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
+  RunIndex&                       colorRunIndex,
+  RunIndex&                       fontRunIndex,
+  RunIndex&                       underlinedCharacterRunIndex,
+  const CharacterIndex            characterIndex,
+  int&                            tagReference)
 {
   if(!spanTag.isEndTag)
   {
@@ -684,17 +743,22 @@ void ProcessSpanForRun(
     FontDescriptionRun fontRun;
     Initialize(fontRun);
 
+    UnderlinedCharacterRun underlinedCharacterRun;
+    Initialize(underlinedCharacterRun);
+
     Span span;
     Initialize(span);
 
     // Fill the run with the parameters.
-    colorRun.characterRun.characterIndex = characterIndex;
-    fontRun.characterRun.characterIndex  = characterIndex;
+    colorRun.characterRun.characterIndex               = characterIndex;
+    fontRun.characterRun.characterIndex                = characterIndex;
+    underlinedCharacterRun.characterRun.characterIndex = characterIndex;
 
-    span.colorRunIndex = colorRunIndex;
-    span.fontRunIndex  = fontRunIndex;
+    span.colorRunIndex               = colorRunIndex;
+    span.fontRunIndex                = fontRunIndex;
+    span.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
 
-    ProcessSpanTag(spanTag, colorRun, fontRun, span.isColorDefined, span.isFontDefined);
+    ProcessSpanTag(spanTag, colorRun, fontRun, underlinedCharacterRun, span.isColorDefined, span.isFontDefined, span.isUnderlinedCharacterDefined);
 
     // Push the span into the stack.
     spanStack.Push(span);
@@ -714,6 +778,13 @@ void ProcessSpanForRun(
       ++fontRunIndex;
     }
 
+    if(span.isUnderlinedCharacterDefined)
+    {
+      // Push the run in the logical model.
+      underlinedCharacterRuns.PushBack(underlinedCharacterRun);
+      ++underlinedCharacterRunIndex;
+    }
+
     // Increase reference
     ++tagReference;
   }
@@ -736,6 +807,12 @@ void ProcessSpanForRun(
         fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
       }
 
+      if(span.isUnderlinedCharacterDefined)
+      {
+        UnderlinedCharacterRun& underlinedCharacterRun         = *(underlinedCharacterRuns.Begin() + span.underlinedCharacterRunIndex);
+        underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
+      }
+
       --tagReference;
     }
   }
@@ -749,13 +826,21 @@ void ProcessSpanForRun(
  * @param[in] colorRunIndex The color run index
  * @param[in] underlinedCharacterRunIndex The underlined character run index
  * @param[in] backgroundRunIndex The background run index
+ * @param[in] boundedParagraphRunIndex The bounded paragraph run index
+ *
  */
-void ResizeModelVectors(MarkupProcessData& markupProcessData, const RunIndex fontRunIndex, const RunIndex colorRunIndex, const RunIndex underlinedCharacterRunIndex, const RunIndex backgroundRunIndex)
+void ResizeModelVectors(MarkupProcessData& markupProcessData,
+                        const RunIndex     fontRunIndex,
+                        const RunIndex     colorRunIndex,
+                        const RunIndex     underlinedCharacterRunIndex,
+                        const RunIndex     backgroundRunIndex,
+                        const RunIndex     boundedParagraphRunIndex)
 {
   markupProcessData.fontRuns.Resize(fontRunIndex);
   markupProcessData.colorRuns.Resize(colorRunIndex);
   markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex);
   markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex);
+  markupProcessData.boundedParagraphRuns.Resize(boundedParagraphRunIndex);
 
 #ifdef DEBUG_ENABLED
   for(unsigned int i = 0; i < colorRunIndex; ++i)
@@ -875,10 +960,12 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar
   StyleStack<Span> spanStack;
 
   // Points the next free position in the vector of runs.
-  RunIndex colorRunIndex               = 0u;
-  RunIndex fontRunIndex                = 0u;
-  RunIndex underlinedCharacterRunIndex = 0u;
-  RunIndex backgroundRunIndex          = 0u;
+  RunIndex colorRunIndex                  = 0u;
+  RunIndex fontRunIndex                   = 0u;
+  RunIndex underlinedCharacterRunIndex    = 0u;
+  RunIndex backgroundRunIndex             = 0u;
+  RunIndex strikethroughCharacterRunIndex = 0u;
+  RunIndex boundedParagraphRunIndex       = 0u;
 
   // check tag reference
   int colorTagReference      = 0u;
@@ -888,6 +975,8 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar
   int uTagReference          = 0u;
   int backgroundTagReference = 0u;
   int spanTagReference       = 0u;
+  int sTagReference          = 0u;
+  int pTagReference          = 0u;
 
   // Give an initial default value to the model's vectors.
   markupProcessData.colorRuns.Reserve(DEFAULT_VECTOR_SIZE);
@@ -924,7 +1013,7 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar
       else if(TokenComparison(XHTML_U_TAG, tag.buffer, tag.length))
       {
         ProcessTagForRun<UnderlinedCharacterRun>(
-          markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) {});
+          markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) { ProcessUnderlineTag(tag, run); });
       } // <u></u>
       else if(TokenComparison(XHTML_B_TAG, tag.buffer, tag.length))
       {
@@ -949,7 +1038,13 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar
             run.color = Color::BLUE;
             ProcessColorTag(tag, run);
           });
-        /* TODO - underline */
+        /* Underline */
+        ProcessTagForRun<UnderlinedCharacterRun>(
+          markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) {
+            run.properties.color        = Color::BLUE;
+            run.properties.colorDefined = true;
+            ProcessUnderlineTag(tag, run);
+          });
       } // <a href=https://www.tizen.org>tizen</a>
       else if(TokenComparison(XHTML_SHADOW_TAG, tag.buffer, tag.length))
       {
@@ -977,9 +1072,20 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar
       }
       else if(TokenComparison(XHTML_SPAN_TAG, tag.buffer, tag.length))
       {
-        ProcessSpanForRun(tag, spanStack, markupProcessData.colorRuns, markupProcessData.fontRuns, colorRunIndex, fontRunIndex, characterIndex, spanTagReference);
+        ProcessSpanForRun(tag, spanStack, markupProcessData.colorRuns, markupProcessData.fontRuns, markupProcessData.underlinedCharacterRuns, colorRunIndex, fontRunIndex, underlinedCharacterRunIndex, characterIndex, spanTagReference);
       }
-    } // end if( IsTag() )
+      else if(TokenComparison(XHTML_STRIKETHROUGH_TAG, tag.buffer, tag.length))
+      {
+        ProcessTagForRun<StrikethroughCharacterRun>(
+          markupProcessData.strikethroughCharacterRuns, styleStack, tag, characterIndex, strikethroughCharacterRunIndex, sTagReference, [](const Tag& tag, StrikethroughCharacterRun& run) { ProcessStrikethroughTag(tag, run); });
+      } // <s></s>
+      else if(TokenComparison(XHTML_PARAGRAPH_TAG, tag.buffer, tag.length))
+      {
+        ProcessParagraphTag(markupProcessData, tag, (markupStringBuffer == markupStringEndBuffer), characterIndex);
+        ProcessTagForRun<BoundedParagraphRun>(
+          markupProcessData.boundedParagraphRuns, styleStack, tag, characterIndex, boundedParagraphRunIndex, pTagReference, [](const Tag& tag, BoundedParagraphRun& run) { ProcessAttributesOfParagraphTag(tag, run); });
+      } // <p></p>
+    }   // end if( IsTag() )
     else if(markupStringBuffer < markupStringEndBuffer)
     {
       ProcessMarkupStringBuffer(markupProcessData, markupStringBuffer, markupStringEndBuffer, characterIndex);
@@ -987,7 +1093,10 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar
   }
 
   // Resize the model's vectors.
-  ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, backgroundRunIndex);
+  ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, backgroundRunIndex, boundedParagraphRunIndex);
+
+  // Handle the nested tags
+  OverrideNestedUnderlinedCharacterRuns(markupProcessData.underlinedCharacterRuns);
 }
 
 } // namespace Text