Text Fit
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 7c6ef25..0259391
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 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.
@@ -20,6 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <dali/public-api/object/type-registry-helper.h>
+#include <dali/public-api/common/stage.h>
 #include <dali/devel-api/object/property-helper-devel.h>
 #include <dali/devel-api/adaptor-framework/image-loading.h>
 #include <dali/integration-api/debug.h>
@@ -73,6 +74,12 @@ namespace
     0.5f,  // VerticalAlignment::CENTER
     1.0f   // VerticalAlignment::BOTTOM
   };
+
+  const std::string TEXT_FIT_ENABLE_KEY( "enable" );
+  const std::string TEXT_FIT_MIN_SIZE_KEY( "minSize" );
+  const std::string TEXT_FIT_MAX_SIZE_KEY( "maxSize" );
+  const std::string TEXT_FIT_STEP_SIZE_KEY( "stepSize" );
+  const std::string TEXT_FIT_FONT_SIZE_TYPE_KEY( "fontSizeType" );
 }
 
 namespace
@@ -107,11 +114,11 @@ DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "multiLine",
 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "horizontalAlignment",       STRING,  HORIZONTAL_ALIGNMENT       )
 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "verticalAlignment",         STRING,  VERTICAL_ALIGNMENT         )
 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "unusedPropertyTextColor",   VECTOR4, UNUSED_PROPERTY_TEXT_COLOR )
-DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "shadowOffset",              VECTOR2, SHADOW_OFFSET              )
-DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "shadowColor",               VECTOR4, SHADOW_COLOR               )
-DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "underlineEnabled",          BOOLEAN, UNDERLINE_ENABLED          )
-DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "underlineColor",            VECTOR4, UNDERLINE_COLOR            )
-DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "underlineHeight",           FLOAT,   UNDERLINE_HEIGHT           )
+DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "reservedProperty01",        STRING,  RESERVED_PROPERTY_01       )
+DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "reservedProperty02",        STRING,  RESERVED_PROPERTY_02       )
+DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "reservedProperty03",        STRING,  RESERVED_PROPERTY_03       )
+DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "reservedProperty04",        STRING,  RESERVED_PROPERTY_04       )
+DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "reservedProperty05",        STRING,  RESERVED_PROPERTY_05       )
 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "enableMarkup",              BOOLEAN, ENABLE_MARKUP              )
 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "enableAutoScroll",          BOOLEAN, ENABLE_AUTO_SCROLL         )
 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "autoScrollSpeed",           INTEGER, AUTO_SCROLL_SPEED          )
@@ -131,6 +138,9 @@ DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "lineWrapMode",
 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY( Toolkit, TextLabel, "textDirection",       INTEGER, TEXT_DIRECTION             )
 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "verticalLineAlignment",     INTEGER, VERTICAL_LINE_ALIGNMENT    )
 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "textBackground",            MAP,     BACKGROUND                 )
+DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "ignoreSpacesAfterText",     BOOLEAN, IGNORE_SPACES_AFTER_TEXT   )
+DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "matchSystemLanguageDirection", BOOLEAN, MATCH_SYSTEM_LANGUAGE_DIRECTION )
+DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "textFit",                   MAP,     TEXT_FIT                 )
 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, TextLabel, "textColor",      Color::BLACK,     TEXT_COLOR     )
 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit,    TextLabel, "textColorRed",   TEXT_COLOR_RED,   TEXT_COLOR, 0  )
 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit,    TextLabel, "textColorGreen", TEXT_COLOR_GREEN, TEXT_COLOR, 1  )
@@ -266,73 +276,6 @@ void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Pr
         impl.mTextUpdateNeeded = true;
         break;
       }
-
-      case Toolkit::TextLabel::Property::SHADOW_OFFSET:
-      {
-        if( impl.mController )
-        {
-          const Vector2& shadowOffset = value.Get< Vector2 >();
-          if ( impl.mController->GetShadowOffset() != shadowOffset )
-          {
-            impl.mController->SetShadowOffset( shadowOffset );
-            impl.mTextUpdateNeeded = true;
-          }
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::SHADOW_COLOR:
-      {
-        if( impl.mController )
-        {
-          const Vector4& shadowColor = value.Get< Vector4 >();
-          if ( impl.mController->GetShadowColor() != shadowColor )
-          {
-            impl.mController->SetShadowColor( shadowColor );
-            impl.mTextUpdateNeeded = true;
-          }
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
-      {
-        if( impl.mController )
-        {
-          const Vector4& color = value.Get< Vector4 >();
-          if ( impl.mController->GetUnderlineColor() != color )
-          {
-            impl.mController->SetUnderlineColor( color );
-            impl.mTextUpdateNeeded = true;
-          }
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
-      {
-        if( impl.mController )
-        {
-          const bool enabled = value.Get< bool >();
-          if ( impl.mController->IsUnderlineEnabled() != enabled )
-          {
-            impl.mController->SetUnderlineEnabled( enabled );
-            impl.mTextUpdateNeeded = true;
-          }
-        }
-        break;
-      }
-
-      case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
-      {
-        if( impl.mController )
-        {
-          float height = value.Get< float >();
-          if( fabsf( impl.mController->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 )
-          {
-            impl.mController->SetUnderlineHeight( height );
-            impl.mTextUpdateNeeded = true;
-          }
-        }
-        break;
-      }
       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
       {
         if( impl.mController )
@@ -361,7 +304,6 @@ void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Pr
              // If request is enable (true) then start autoscroll as not already running
              else
              {
-               impl.mController->SetTextElideEnabled( false );
                impl.mController->SetAutoScrollEnabled( enableAutoScroll );
              }
           }
@@ -533,6 +475,84 @@ void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
+      case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
+      {
+        impl.mController->SetIgnoreSpacesAfterText(value.Get< bool >());
+        break;
+      }
+      case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
+      {
+        impl.mController->SetMatchSystemLanguageDirection(value.Get< bool >());
+        break;
+      }
+      case Toolkit::DevelTextLabel::Property::TEXT_FIT:
+      {
+        const Property::Map& propertiesMap = value.Get<Property::Map>();
+
+        bool enabled = false;
+        float minSize = 0.f;
+        float maxSize = 0.f;
+        float stepSize = 0.f;
+        bool isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
+        Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
+
+        if ( !propertiesMap.Empty() )
+        {
+          const unsigned int numberOfItems = propertiesMap.Count();
+
+          // Parses and applies
+          for( unsigned int index = 0u; index < numberOfItems; ++index )
+          {
+            const KeyValuePair& valueGet = propertiesMap.GetKeyValue( index );
+
+            if( ( Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey ) || ( TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey ) )
+            {
+              /// Enable key.
+              enabled = valueGet.second.Get< bool >();
+            }
+            else if( ( Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey ) )
+            {
+              /// min size.
+              minSize = valueGet.second.Get< float >();
+              isMinSizeSet = true;
+            }
+            else if( ( Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey ) )
+            {
+              /// max size.
+              maxSize = valueGet.second.Get< float >();
+              isMaxSizeSet = true;
+            }
+            else if( ( Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey ) )
+            {
+              /// step size.
+              stepSize = valueGet.second.Get< float >();
+              isStepSizeSet = true;
+            }
+            else if( ( Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey ) || ( TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey ) )
+            {
+              if( "pixelSize" == valueGet.second.Get< std::string >() )
+              {
+                type = Controller::FontSizeType::PIXEL_SIZE;
+              }
+            }
+          }
+
+          impl.mController->SetTextFitEnabled( enabled );
+          if( isMinSizeSet )
+          {
+            impl.mController->SetTextFitMinSize( minSize, type );
+          }
+          if( isMaxSizeSet )
+          {
+            impl.mController->SetTextFitMaxSize( maxSize, type );
+          }
+          if( isStepSizeSet )
+          {
+            impl.mController->SetTextFitStepSize( stepSize, type );
+          }
+        }
+        break;
+      }
     }
 
     // Request relayout when text update is needed. It's necessary to call it
@@ -544,10 +564,6 @@ void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Pr
       impl.RequestTextRelayout();
     }
   }
-
-
-
-
 }
 
 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
@@ -637,46 +653,6 @@ Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index inde
         value = label.GetProperty( Toolkit::TextLabel::Property::TEXT_COLOR );
         break;
       }
-      case Toolkit::TextLabel::Property::SHADOW_OFFSET:
-      {
-        if ( impl.mController )
-        {
-          value = impl.mController->GetShadowOffset();
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::SHADOW_COLOR:
-      {
-        if ( impl.mController )
-        {
-          value = impl.mController->GetShadowColor();
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
-      {
-        if ( impl.mController )
-        {
-          value = impl.mController->GetUnderlineColor();
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
-      {
-        if ( impl.mController )
-        {
-          value = impl.mController->IsUnderlineEnabled();
-        }
-        break;
-      }
-      case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
-      {
-        if ( impl.mController )
-        {
-          value = impl.mController->GetUnderlineHeight();
-        }
-        break;
-      }
       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
       {
         if( impl.mController )
@@ -831,6 +807,33 @@ Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index inde
         GetBackgroundProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
         break;
       }
+      case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
+      {
+        value = impl.mController->IsIgnoreSpacesAfterText();
+        break;
+      }
+      case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
+      {
+        value = impl.mController->IsMatchSystemLanguageDirection();
+        break;
+      }
+      case Toolkit::DevelTextLabel::Property::TEXT_FIT:
+      {
+        const bool enabled = impl.mController->IsTextFitEnabled();
+        const float minSize = impl.mController->GetTextFitMinSize();
+        const float maxSize = impl.mController->GetTextFitMaxSize();
+        const float stepSize = impl.mController->GetTextFitStepSize();
+
+        Property::Map map;
+        map.Insert( TEXT_FIT_ENABLE_KEY, enabled );
+        map.Insert( TEXT_FIT_MIN_SIZE_KEY, minSize );
+        map.Insert( TEXT_FIT_MAX_SIZE_KEY, maxSize );
+        map.Insert( TEXT_FIT_STEP_SIZE_KEY, stepSize );
+        map.Insert( TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize" );
+
+        value = map;
+        break;
+      }
     }
   }
 
@@ -862,6 +865,11 @@ void TextLabel::OnInitialize()
   // Enable the text ellipsis.
   mController->SetTextElideEnabled( true );   // If false then text larger than control will overflow
 
+  // Sets layoutDirection value
+  Dali::Stage stage = Dali::Stage::GetCurrent();
+  Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( stage.GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
+  mController->SetLayoutDirection( layoutDirection );
+
   Layout::Engine& engine = mController->GetLayoutEngine();
   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
 }
@@ -951,7 +959,18 @@ void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
 
   Vector2 contentSize( size.x - ( padding.start + padding.end ), size.y - ( padding.top + padding.bottom ) );
 
-  const Text::Controller::UpdateTextType updateTextType = mController->Relayout( contentSize );
+  // If the same text comes for relayouting for same layout size we don't need to calculate point size again.
+  // Like for color change.
+  if (mController->IsTextFitEnabled() && (mController->GetTextFitContentSize() != contentSize ) )
+  {
+    mController->FitPointSizeforLayout(contentSize);
+    mController->SetTextFitContentSize(contentSize);
+  }
+
+  // Support Right-To-Left
+  Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
+
+  const Text::Controller::UpdateTextType updateTextType = mController->Relayout( contentSize, layoutDirection );
 
   if( ( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
      || mTextUpdateNeeded )
@@ -962,7 +981,6 @@ void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
     TextVisual::EnableRendererUpdate( mVisual );
 
     // Support Right-To-Left of padding
-    Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
     if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
     {
       std::swap( padding.start, padding.end );
@@ -1008,7 +1026,7 @@ void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
 void TextLabel::RequestTextRelayout()
 {
   RelayoutRequest();
-  Toolkit::DevelControl::RequestLayout( *this );
+  // Signal that a Relayout may be needed
 }
 
 void TextLabel::SetUpAutoScrolling()