Enum properties added for the text effects style.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-effects-style.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 75fabd7..f65655b
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 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.
@@ -19,6 +19,7 @@
 #include <dali-toolkit/internal/text/text-effects-style.h>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/controls/text-controls/text-style-properties-devel.h>
 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
 #include <dali-toolkit/internal/text/property-string-parser.h>
 
@@ -35,6 +36,8 @@ namespace
 {
 const std::string COLOR_KEY( "color" );
 const std::string OFFSET_KEY( "offset" );
+const std::string BLUR_RADIUS_KEY( "blurRadius" );
+const std::string WIDTH_KEY( "width" );
 const std::string HEIGHT_KEY( "height" );
 const std::string ENABLE_KEY( "enable" );
 const std::string TRUE_TOKEN( "true" );
@@ -45,7 +48,9 @@ bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
                             bool& colorDefined,
                             Vector4& color,
                             bool& offsetDefined,
-                            Vector2& offset )
+                            Vector2& offset,
+                            bool& blurRadiusDefined,
+                            float& blurRadius )
 {
   const unsigned int numberOfItems = shadowPropertiesMap.Count();
 
@@ -54,23 +59,50 @@ bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
   {
     const KeyValuePair& valueGet = shadowPropertiesMap.GetKeyValue( index );
 
-    if( COLOR_KEY == valueGet.first.stringKey )
+    if( ( DevelText::Shadow::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
     {
       /// Color key.
       colorDefined = true;
 
-      const std::string colorStr = valueGet.second.Get<std::string>();
-
-      Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
+      if( valueGet.second.GetType() == Dali::Property::STRING )
+      {
+        const std::string colorStr = valueGet.second.Get<std::string>();
+        Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
+      }
+      else
+      {
+        color = valueGet.second.Get<Vector4>();
+      }
     }
-    else if( OFFSET_KEY == valueGet.first.stringKey )
+    else if( ( DevelText::Shadow::Property::OFFSET == valueGet.first.indexKey ) || ( OFFSET_KEY == valueGet.first.stringKey ) )
     {
       /// Offset key.
       offsetDefined = true;
 
-      const std::string offsetStr = valueGet.second.Get<std::string>();
+      if( valueGet.second.GetType() == Dali::Property::STRING )
+      {
+        const std::string offsetStr = valueGet.second.Get<std::string>();
+        StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
+      }
+      else
+      {
+        offset = valueGet.second.Get<Vector2>();
+      }
+    }
+    else if( ( DevelText::Shadow::Property::BLUR_RADIUS == valueGet.first.indexKey ) || ( BLUR_RADIUS_KEY == valueGet.first.stringKey ) )
+    {
+      /// Blur radius key.
+      blurRadiusDefined = true;
 
-      StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
+      if( valueGet.second.GetType() == Dali::Property::STRING )
+      {
+        const std::string blurRadiusStr = valueGet.second.Get<std::string>();
+        blurRadius = StringToFloat( blurRadiusStr.c_str() );
+      }
+      else
+      {
+        blurRadius = valueGet.second.Get<float>();
+      }
     }
   }
 
@@ -91,29 +123,106 @@ bool ParseUnderlineProperties( const Property::Map& underlinePropertiesMap,
   {
     const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
 
-    if( ENABLE_KEY == valueGet.first.stringKey )
+    if( ( DevelText::Underline::Property::ENABLE == valueGet.first.indexKey ) || ( ENABLE_KEY == valueGet.first.stringKey ) )
     {
       /// Enable key.
-      const std::string enableStr = valueGet.second.Get<std::string>();
-      enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
+      if( valueGet.second.GetType() == Dali::Property::STRING )
+      {
+        const std::string enableStr = valueGet.second.Get<std::string>();
+        enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
+      }
+      else
+      {
+        enabled = valueGet.second.Get<bool>();
+      }
     }
-    else if( COLOR_KEY == valueGet.first.stringKey )
+    else if( ( DevelText::Underline::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
     {
       /// Color key.
       colorDefined = true;
 
-      const std::string colorStr = valueGet.second.Get<std::string>();
-
-      Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
+      if( valueGet.second.GetType() == Dali::Property::STRING )
+      {
+        const std::string colorStr = valueGet.second.Get<std::string>();
+        Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
+      }
+      else
+      {
+        color = valueGet.second.Get<Vector4>();
+      }
     }
-    else if( HEIGHT_KEY == valueGet.first.stringKey )
+    else if( ( DevelText::Underline::Property::HEIGHT == valueGet.first.indexKey ) || ( HEIGHT_KEY == valueGet.first.stringKey ) )
     {
       /// Height key.
       heightDefined = true;
 
-      const std::string heightStr = valueGet.second.Get<std::string>();
+      if( valueGet.second.GetType() == Dali::Property::STRING )
+      {
+        const std::string heightStr = valueGet.second.Get<std::string>();
+        height = StringToFloat( heightStr.c_str() );
+      }
+      else
+      {
+        height = valueGet.second.Get<float>();
+      }
+    }
+  }
+
+  return 0u == numberOfItems;
+}
+
+bool ParseOutlineProperties( const Property::Map& underlinePropertiesMap,
+                               bool& colorDefined,
+                               Vector4& color,
+                               bool& widthDefined,
+                               unsigned int& width )
+{
+  const unsigned int numberOfItems = underlinePropertiesMap.Count();
+
+  // Parses and applies the style.
+  for( unsigned int index = 0u; index < numberOfItems; ++index )
+  {
+    const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
+
+    if( ( DevelText::Outline::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
+    {
+      /// Color key.
+      colorDefined = true;
+      color = valueGet.second.Get<Vector4>();
+    }
+    else if( ( DevelText::Outline::Property::WIDTH == valueGet.first.indexKey ) || ( WIDTH_KEY == valueGet.first.stringKey ) )
+    {
+      /// Width key.
+      widthDefined = true;
+      width = static_cast<unsigned int>( valueGet.second.Get<float>() );
+    }
+  }
+
+  return 0u == numberOfItems;
+}
 
-      height = StringToFloat( heightStr.c_str() );
+bool ParseBackgroundProperties( const Property::Map& backgroundProperties,
+                                bool& enabled,
+                                bool& colorDefined,
+                                Vector4& color )
+{
+  const unsigned int numberOfItems = backgroundProperties.Count();
+
+  // Parses and applies the style.
+  for( unsigned int index = 0u; index < numberOfItems; ++index )
+  {
+    const KeyValuePair& valueGet = backgroundProperties.GetKeyValue( index );
+
+    if( ( DevelText::Background::Property::ENABLE == valueGet.first.indexKey ) || ( ENABLE_KEY == valueGet.first.stringKey ) )
+    {
+      /// Enable key.
+      enabled = valueGet.second.Get<bool>();
+    }
+    else if( ( DevelText::Background::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
+    {
+      /// Color key.
+      colorDefined = true;
+      color = valueGet.second.Get<Vector4>();
     }
   }
 
@@ -157,7 +266,7 @@ bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& va
                                               heightDefined,
                                               height );
 
-            controller->UnderlineSetByString( !empty);
+            controller->UnderlineSetByString( !empty );
           }
         }
         else
@@ -290,6 +399,8 @@ bool SetShadowProperties( ControllerPtr controller, const Property::Value& value
         Vector4 color;
         bool offsetDefined = false;
         Vector2 offset;
+        bool blurRadiusDefined = false;
+        float blurRadius;
 
         bool empty = true;
 
@@ -305,7 +416,9 @@ bool SetShadowProperties( ControllerPtr controller, const Property::Value& value
                                           colorDefined,
                                           color,
                                           offsetDefined,
-                                          offset );
+                                          offset,
+                                          blurRadiusDefined,
+                                          blurRadius );
 
            controller->ShadowSetByString( !empty );
 
@@ -316,7 +429,9 @@ bool SetShadowProperties( ControllerPtr controller, const Property::Value& value
                                          colorDefined,
                                          color,
                                          offsetDefined,
-                                         offset );
+                                         offset,
+                                         blurRadiusDefined,
+                                         blurRadius );
 
           controller->ShadowSetByString( false );
         }
@@ -335,6 +450,12 @@ bool SetShadowProperties( ControllerPtr controller, const Property::Value& value
             controller->SetShadowOffset( offset );
             update = true;
           }
+
+          if( blurRadiusDefined && ( controller->GetShadowBlurRadius() != blurRadius ) )
+          {
+            controller->SetShadowBlurRadius( blurRadius );
+            update = true;
+          }
         }
         else
         {
@@ -369,6 +490,7 @@ void GetShadowProperties( ControllerPtr controller, Property::Value& value, Effe
       {
         const Vector4& color = controller->GetShadowColor();
         const Vector2& offset = controller->GetShadowOffset();
+        const float& blurRadius = controller->GetShadowBlurRadius();
 
         if ( controller->IsShadowSetByString() )
         {
@@ -380,7 +502,11 @@ void GetShadowProperties( ControllerPtr controller, Property::Value& value, Effe
 
           std::string offsetStr;
           Vector2ToString( offset, offsetStr );
-          shadowProperties += "\"offset\":\"" + offsetStr + "\"}";
+          shadowProperties += "\"offset\":\"" + offsetStr + "\",";
+
+          std::string blurRadiusStr;
+          FloatToString( blurRadius, blurRadiusStr );
+          shadowProperties += "\"blurRadius\":\"" + blurRadiusStr + "\"}";
 
           value = shadowProperties;
         }
@@ -388,13 +514,9 @@ void GetShadowProperties( ControllerPtr controller, Property::Value& value, Effe
         {
           Property::Map map;
 
-          std::string colorStr;
-          Vector4ToColorString( color, colorStr );
-          map.Insert( COLOR_KEY, colorStr );
-
-          std::string offsetStr;
-          Vector2ToString( offset, offsetStr );
-          map.Insert( OFFSET_KEY, offsetStr );
+          map.Insert( COLOR_KEY, color );
+          map.Insert( OFFSET_KEY, offset );
+          map.Insert( BLUR_RADIUS_KEY, blurRadius );
 
           value = map;
         }
@@ -463,29 +585,179 @@ bool SetOutlineProperties( ControllerPtr controller, const Property::Value& valu
 
   if( controller )
   {
-    const std::string properties = value.Get< std::string >();
-
     switch( type )
     {
       case EffectStyle::DEFAULT:
       {
-        // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
-        controller->SetDefaultOutlineProperties( properties );
+        const Property::Map& propertiesMap = value.Get<Property::Map>();
+
+        bool colorDefined = false;
+        Vector4 color;
+        bool widthDefined = false;
+        unsigned int width = 0u;
+
+        bool empty = true;
+
+        if ( propertiesMap.Empty() )
+        {
+           // Map empty so check if a string provided
+           // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
+           const std::string propertyString = value.Get<std::string>();
+
+           // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
+           controller->SetDefaultOutlineProperties( propertyString );
+
+           controller->OutlineSetByString( true );
+        }
+        else
+        {
+           empty = ParseOutlineProperties( propertiesMap,
+                                           colorDefined,
+                                           color,
+                                           widthDefined,
+                                           width );
+
+           controller->OutlineSetByString( false );
+        }
+
+        if( !empty )
+        {
+          // Sets the default outline values.
+          if( colorDefined && ( controller->GetOutlineColor() != color ) )
+          {
+            controller->SetOutlineColor( color );
+            update = true;
+          }
+
+          if( widthDefined && ( controller->GetOutlineWidth() != width ) )
+          {
+            controller->SetOutlineWidth( width );
+            update = true;
+          }
+        }
+        else
+        {
+          // Disable outline
+          if( 0u != controller->GetOutlineWidth() )
+          {
+            controller->SetOutlineWidth( 0u );
+            update = true;
+          }
+        }
+        break;
+      }
+      case EffectStyle::INPUT:
+      {
+        const std::string& outlineProperties = value.Get<std::string>();
+
+        controller->SetInputOutlineProperties( outlineProperties );
         break;
       }
+    } // switch
+  } // if( controller )
+
+  return update;
+}
+
+void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
+{
+  if( controller )
+  {
+    switch( type )
+    {
+      case EffectStyle::DEFAULT:
+      {
+        if ( controller->IsOutlineSetByString() )
+        {
+          value = controller->GetDefaultOutlineProperties();
+          break;
+        }
+        else
+        {
+          const Vector4& color = controller->GetOutlineColor();
+          const unsigned int width = controller->GetOutlineWidth();
+
+          Property::Map map;
+          map.Insert( COLOR_KEY, color );
+          map.Insert( WIDTH_KEY, static_cast<int>( width ) );
+
+          value = map;
+
+          break;
+        }
+      }
       case EffectStyle::INPUT:
       {
-        // Stores the input outline's properties string to be recovered by the GetOutlineProperties() function.
-        controller->SetInputOutlineProperties( properties );
+        value = controller->GetInputOutlineProperties();
         break;
       }
     }
   }
+}
+
+bool SetBackgroundProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
+{
+  bool update = false;
+
+  if( controller )
+  {
+    switch( type )
+    {
+      case EffectStyle::DEFAULT:
+      {
+        const Property::Map& propertiesMap = value.Get<Property::Map>();
+
+        bool enabled = false;
+        bool colorDefined = false;
+        Vector4 color;
+
+        bool empty = true;
+
+        if ( !propertiesMap.Empty() )
+        {
+           empty = ParseBackgroundProperties( propertiesMap,
+                                              enabled,
+                                              colorDefined,
+                                              color );
+        }
+
+        if( !empty )
+        {
+          if( enabled != controller->IsBackgroundEnabled() )
+          {
+            controller->SetBackgroundEnabled( enabled );
+            update = true;
+          }
+
+          if( colorDefined && ( controller->GetBackgroundColor() != color ) )
+          {
+            controller->SetBackgroundColor( color );
+            update = true;
+          }
+        }
+        else
+        {
+          // Disable background.
+          if( controller->IsBackgroundEnabled() )
+          {
+            controller->SetBackgroundEnabled( false );
+            update = true;
+          }
+        }
+        break;
+      }
+      case EffectStyle::INPUT:
+      {
+        // Text background is not supported while inputting yet
+        break;
+      }
+    } // switch
+  } // if( controller )
 
   return update;
 }
 
-void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
+void GetBackgroundProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
 {
   if( controller )
   {
@@ -493,18 +765,28 @@ void GetOutlineProperties( ControllerPtr controller, Property::Value& value, Eff
     {
       case EffectStyle::DEFAULT:
       {
-        value = controller->GetDefaultOutlineProperties();
+        const bool enabled = controller->IsBackgroundEnabled();
+        const Vector4& color = controller->GetBackgroundColor();
+
+        Property::Map map;
+        map.Insert( ENABLE_KEY, enabled );
+        map.Insert( COLOR_KEY, color );
+
+        value = map;
+
         break;
+
       }
       case EffectStyle::INPUT:
       {
-        value = controller->GetInputOutlineProperties();
+        // Text background is not supported while inputting yet
         break;
       }
     }
   }
 }
 
+
 } // namespace Text
 
 } // namespace Toolkit