Add MIN_LINE_SIZE property
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / layouts / layout-engine.cpp
index d73ee6a..fb63d96 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
@@ -52,7 +52,8 @@ namespace
 const float MAX_FLOAT = std::numeric_limits<float>::max();
 const CharacterDirection LTR = false;
 const CharacterDirection RTL = !LTR;
-const float LINE_SPACING= 0.f;
+const float LINE_SPACING = 0.f;
+const float MIN_LINE_SIZE = 0.f;
 
 inline bool isEmptyLineAtLast( const Vector<LineRun>& lines, const Vector<LineRun>::Iterator& line )
 {
@@ -130,7 +131,8 @@ struct Engine::Impl
   Impl()
   : mLayout{ Layout::Engine::SINGLE_LINE_BOX },
     mCursorWidth{ 0.f },
-    mDefaultLineSpacing{ LINE_SPACING }
+    mDefaultLineSpacing{ LINE_SPACING },
+    mDefaultLineSize{ MIN_LINE_SIZE }
   {
   }
 
@@ -162,8 +164,12 @@ struct Engine::Impl
     // Sets the minimum descender.
     lineLayout.descender = std::min( lineLayout.descender, fontMetrics.descender );
 
-    // set the line spacing
-    lineLayout.lineSpacing = mDefaultLineSpacing;
+    // Sets the line size
+    lineLayout.lineSpacing = mDefaultLineSize - ( lineLayout.ascender + -lineLayout.descender );
+    lineLayout.lineSpacing = lineLayout.lineSpacing < 0.f ? 0.f : lineLayout.lineSpacing;
+
+    // Add the line spacing
+    lineLayout.lineSpacing += mDefaultLineSpacing;
   }
 
   /**
@@ -376,7 +382,7 @@ struct Engine::Impl
           }
           else
           {
-            lineLayout.length = length;
+            lineLayout.length = std::max( length, lineLayout.length );
           }
         }
       }
@@ -551,7 +557,7 @@ struct Engine::Impl
         tmpLineLayout.penX += tmpLineLayout.previousAdvance + tmpLineLayout.whiteSpaceLengthEndOfLine;
         tmpLineLayout.previousAdvance = ( glyphMetrics.advance + parameters.interGlyphExtraAdvance );
 
-        tmpLineLayout.length = tmpLineLayout.penX + glyphMetrics.xBearing + glyphMetrics.width;
+        tmpLineLayout.length = std::max( tmpLineLayout.length, tmpLineLayout.penX + glyphMetrics.xBearing + glyphMetrics.width );
 
         // Clear the white space length at the end of the line.
         tmpLineLayout.whiteSpaceLengthEndOfLine = 0.f;
@@ -721,7 +727,7 @@ struct Engine::Impl
         // Convert the character in the visual order into the glyph in the visual order.
         const GlyphIndex glyphIndex = *( charactersToGlyphsBuffer + characterVisualIndex ) + index;
 
-        DALI_ASSERT_DEBUG( 0u <= glyphIndex && glyphIndex < layoutParameters.textModel->mVisualModel->mGlyphs.Count() );
+        DALI_ASSERT_DEBUG( glyphIndex < layoutParameters.textModel->mVisualModel->mGlyphs.Count() );
 
         const GlyphInfo& glyph = *( glyphsBuffer + glyphIndex );
         Vector2& position = *( glyphPositionsBuffer + glyphIndex - layoutParameters.startGlyphIndex );
@@ -921,26 +927,9 @@ struct Engine::Impl
     lineRun.glyphRun.numberOfGlyphs = layout.numberOfGlyphs;
     lineRun.characterRun.characterIndex = layout.characterIndex;
     lineRun.characterRun.numberOfCharacters = layout.numberOfCharacters;
-    lineRun.lineSpacing = mDefaultLineSpacing;
+    lineRun.width = layout.length;
+    lineRun.extraLength = std::ceil( layout.whiteSpaceLengthEndOfLine );
 
-    if( isLastLine && !layoutParameters.isLastNewParagraph )
-    {
-      lineRun.width = layout.length;
-      if( LTR == layout.direction )
-      {
-        lineRun.width += layout.whiteSpaceLengthEndOfLine;
-        lineRun.extraLength = 0.f;
-      }
-      else
-      {
-        lineRun.extraLength = layout.whiteSpaceLengthEndOfLine;
-      }
-    }
-    else
-    {
-      lineRun.width = layout.length;
-      lineRun.extraLength = std::ceil( layout.whiteSpaceLengthEndOfLine );
-    }
 
     // Rounds upward to avoid a non integer size.
     lineRun.width = std::ceil( lineRun.width );
@@ -950,6 +939,12 @@ struct Engine::Impl
     lineRun.direction = layout.direction;
     lineRun.ellipsis = false;
 
+    lineRun.lineSpacing = mDefaultLineSize - ( lineRun.ascender + -lineRun.descender );
+    lineRun.lineSpacing = lineRun.lineSpacing < 0.f ? 0.f : lineRun.lineSpacing;
+
+    lineRun.lineSpacing += mDefaultLineSpacing;
+
+
     // Update the actual size.
     if( lineRun.width > layoutSize.width )
     {
@@ -1001,7 +996,11 @@ struct Engine::Impl
     lineRun.alignmentOffset = 0.f;
     lineRun.direction = LTR;
     lineRun.ellipsis = false;
-    lineRun.lineSpacing = mDefaultLineSpacing;
+
+    lineRun.lineSpacing = mDefaultLineSize - ( lineRun.ascender + -lineRun.descender );
+    lineRun.lineSpacing = lineRun.lineSpacing < 0.f ? 0.f : lineRun.lineSpacing;
+
+    lineRun.lineSpacing += mDefaultLineSpacing;
 
     layoutSize.height += ( lineRun.ascender + -lineRun.descender ) + lineRun.lineSpacing;
   }
@@ -1566,6 +1565,7 @@ struct Engine::Impl
   Type mLayout;
   float mCursorWidth;
   float mDefaultLineSpacing;
+  float mDefaultLineSize;
 
   IntrusivePtr<Metrics> mMetrics;
 };
@@ -1647,6 +1647,16 @@ float Engine::GetDefaultLineSpacing() const
   return mImpl->mDefaultLineSpacing;
 }
 
+void Engine::SetDefaultLineSize( float lineSize )
+{
+  mImpl->mDefaultLineSize = lineSize;
+}
+
+float Engine::GetDefaultLineSize() const
+{
+  return mImpl->mDefaultLineSize;
+}
+
 } // namespace Layout
 
 } // namespace Text