Merge "Improve the underline markup" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / layouts / layout-engine.cpp
index b27f634..53030e2 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.
@@ -28,6 +28,7 @@
 #include <dali-toolkit/internal/text/bidirectional-support.h>
 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
+#include <dali-toolkit/internal/text/layouts/layout-engine-helper-functions.h>
 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
 
 namespace Dali
@@ -36,6 +37,20 @@ namespace Toolkit
 {
 namespace Text
 {
+float GetLineHeight(const LineRun lineRun, bool isLastLine)
+{
+  // The line height is the addition of the line ascender, the line descender and the line spacing.
+  // However, the line descender has a negative value, hence the subtraction.
+  // In case this is the only/last line then line spacing should be ignored.
+  float lineHeight = lineRun.ascender - lineRun.descender;
+
+  if(!isLastLine || lineRun.lineSpacing > 0)
+  {
+    lineHeight += lineRun.lineSpacing;
+  }
+  return lineHeight;
+}
+
 namespace Layout
 {
 namespace
@@ -44,12 +59,13 @@ namespace
 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_LAYOUT");
 #endif
 
-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              MIN_LINE_SIZE  = 0.f;
-const Character          HYPHEN_UNICODE = 0x002D;
+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              MIN_LINE_SIZE      = 0.f;
+const Character          HYPHEN_UNICODE     = 0x002D;
+const float              RELATIVE_LINE_SIZE = 1.f;
 
 inline bool isEmptyLineAtLast(const Vector<LineRun>& lines, const Vector<LineRun>::Iterator& line)
 {
@@ -147,11 +163,55 @@ struct Engine::Impl
   : mLayout{Layout::Engine::SINGLE_LINE_BOX},
     mCursorWidth{0.f},
     mDefaultLineSpacing{LINE_SPACING},
-    mDefaultLineSize{MIN_LINE_SIZE}
+    mDefaultLineSize{MIN_LINE_SIZE},
+    mRelativeLineSize{RELATIVE_LINE_SIZE}
   {
   }
 
   /**
+   * @brief get the line spacing.
+   *
+   * @param[in] textSize The text size.
+   * @return the line spacing value.
+   */
+  float GetLineSpacing(float textSize)
+  {
+    float lineSpacing;
+    float relTextSize;
+
+    // Sets the line size
+    lineSpacing = mDefaultLineSize - textSize;
+    lineSpacing = lineSpacing < 0.f ? 0.f : lineSpacing;
+
+    // Add the line spacing
+    lineSpacing += mDefaultLineSpacing;
+
+    //subtract line spcaing if relativeLineSize < 1 & larger than min height
+    relTextSize = textSize * mRelativeLineSize;
+    if(relTextSize > mDefaultLineSize)
+    {
+      if(mRelativeLineSize < 1)
+      {
+        //subtract the difference (always will be positive)
+        lineSpacing -= (textSize - relTextSize);
+      }
+      else
+      {
+        //reverse the addition in the top.
+        if(mDefaultLineSize > textSize)
+        {
+          lineSpacing -= mDefaultLineSize - textSize;
+        }
+
+        //add difference instead
+        lineSpacing += relTextSize - textSize;
+      }
+    }
+
+    return lineSpacing;
+  }
+
+  /**
    * @brief Updates the line ascender and descender with the metrics of a new font.
    *
    * @param[in] glyphMetrics The metrics of the new font.
@@ -179,12 +239,7 @@ struct Engine::Impl
     // Sets the minimum descender.
     lineLayout.descender = std::min(lineLayout.descender, fontMetrics.descender);
 
-    // 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;
+    lineLayout.lineSpacing = GetLineSpacing(lineLayout.ascender + -lineLayout.descender);
   }
 
   /**
@@ -244,10 +299,13 @@ struct Engine::Impl
 
     const float      outlineWidth                = static_cast<float>(parameters.textModel->GetOutlineWidth());
     const GlyphIndex lastGlyphOfParagraphPlusOne = parameters.startGlyphIndex + parameters.numberOfGlyphs;
+    const float      characterSpacing            = parameters.textModel->mVisualModel->GetCharacterSpacing();
 
     CharacterIndex characterLogicalIndex = 0u;
     CharacterIndex characterVisualIndex  = 0u;
 
+    float calculatedAdvance = 0.f;
+
     // If there are characters in the second half of Line then the first visual index mapped from visualToLogicalMapSecondHalf
     // Otherwise maps the first visual index from visualToLogicalMap.
     // This is to initialize the first visual index.
@@ -272,7 +330,8 @@ struct Engine::Impl
         {
           const GlyphInfo& glyphInfo = *(glyphsBuffer + *(charactersToGlyphsBuffer + characterVisualIndex));
 
-          whiteSpaceLengthEndOfLine += glyphInfo.advance;
+          calculatedAdvance = GetCalculatedAdvance(*(textBuffer + characterVisualIndex), characterSpacing, glyphInfo.advance);
+          whiteSpaceLengthEndOfLine += calculatedAdvance;
 
           ++characterLogicalIndex;
           characterVisualIndex = bidirectionalLineInfo.characterRunForSecondHalfLine.characterIndex + *(bidirectionalLineInfo.visualToLogicalMapSecondHalf + characterLogicalIndex);
@@ -293,7 +352,8 @@ struct Engine::Impl
         {
           const GlyphInfo& glyphInfo = *(glyphsBuffer + *(charactersToGlyphsBuffer + characterVisualIndex));
 
-          whiteSpaceLengthEndOfLine += glyphInfo.advance;
+          calculatedAdvance = GetCalculatedAdvance(*(textBuffer + characterVisualIndex), characterSpacing, glyphInfo.advance);
+          whiteSpaceLengthEndOfLine += calculatedAdvance;
 
           ++characterLogicalIndex;
           characterVisualIndex = bidirectionalLineInfo.characterRun.characterIndex + *(bidirectionalLineInfo.visualToLogicalMap + characterLogicalIndex);
@@ -310,11 +370,13 @@ struct Engine::Impl
                                                                   charactersPerGlyphBuffer);
 
     GlyphMetrics glyphMetrics;
+    calculatedAdvance = GetCalculatedAdvance(*(textBuffer + characterVisualIndex), characterSpacing, (*(glyphsBuffer + glyphIndex)).advance);
     GetGlyphsMetrics(glyphIndex,
                      numberOfGLyphsInGroup,
                      glyphMetrics,
                      glyphsBuffer,
-                     mMetrics);
+                     mMetrics,
+                     calculatedAdvance);
 
     float penX = -glyphMetrics.xBearing + mCursorWidth + outlineWidth;
 
@@ -339,11 +401,13 @@ struct Engine::Impl
         characterLogicalIndex += *(charactersPerGlyphBuffer + glyphIndex + numberOfGLyphsInGroup - 1u);
 
         GlyphMetrics glyphMetrics;
+        calculatedAdvance = GetCalculatedAdvance(*(textBuffer + characterVisualIndex), characterSpacing, (*(glyphsBuffer + glyphIndex)).advance);
         GetGlyphsMetrics(glyphIndex,
                          numberOfGLyphsInGroup,
                          glyphMetrics,
                          glyphsBuffer,
-                         mMetrics);
+                         mMetrics,
+                         calculatedAdvance);
 
         if(isWhiteSpace)
         {
@@ -399,11 +463,13 @@ struct Engine::Impl
       characterLogicalIndex += *(charactersPerGlyphBuffer + glyphIndex + numberOfGLyphsInGroup - 1u);
 
       GlyphMetrics glyphMetrics;
+      calculatedAdvance = GetCalculatedAdvance(*(textBuffer + characterVisualIndex), characterSpacing, (*(glyphsBuffer + glyphIndex)).advance);
       GetGlyphsMetrics(glyphIndex,
                        numberOfGLyphsInGroup,
                        glyphMetrics,
                        glyphsBuffer,
-                       mMetrics);
+                       mMetrics,
+                       calculatedAdvance);
 
       if(isWhiteSpace)
       {
@@ -633,13 +699,20 @@ struct Engine::Impl
     float widthFirstHalf = ((ellipsisPosition != DevelText::EllipsisPosition::MIDDLE) ? targetWidth : targetWidth - std::floor(targetWidth / 2));
 
     bool isSecondHalf = false;
+    // Character Spacing
+    const float             characterSpacing          = parameters.textModel->mVisualModel->GetCharacterSpacing();
+    float                   calculatedAdvance         = 0.f;
+    Vector<CharacterIndex>& glyphToCharacterMap       = parameters.textModel->mVisualModel->mGlyphsToCharacters;
+    const CharacterIndex*   glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
 
     GlyphMetrics glyphMetrics;
+    calculatedAdvance = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + lineLayout.glyphIndex))), characterSpacing, (*(glyphsBuffer + lineLayout.glyphIndex)).advance);
     GetGlyphsMetrics(lineLayout.glyphIndex,
                      numberOfGLyphsInGroup,
                      glyphMetrics,
                      glyphsBuffer,
-                     mMetrics);
+                     mMetrics,
+                     calculatedAdvance);
 
     // Set the direction of the first character of the line.
     lineLayout.characterIndex = *(glyphsToCharactersBuffer + lineLayout.glyphIndex);
@@ -674,11 +747,13 @@ struct Engine::Impl
                                                                     charactersPerGlyphBuffer);
 
       GlyphMetrics glyphMetrics;
+      calculatedAdvance = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + glyphIndex))), characterSpacing, (*(glyphsBuffer + glyphIndex)).advance);
       GetGlyphsMetrics(glyphIndex,
                        numberOfGLyphsInGroup,
                        glyphMetrics,
                        glyphsBuffer,
-                       mMetrics);
+                       mMetrics,
+                       calculatedAdvance);
 
       const bool isLastGlyph = glyphIndex + numberOfGLyphsInGroup == totalNumberOfGlyphs;
 
@@ -733,7 +808,8 @@ struct Engine::Impl
       if(isWhiteSpace)
       {
         // Add the length to the length of white spaces at the end of the line.
-        tmpLineLayout.whiteSpaceLengthEndOfLine += glyphMetrics.advance; // The advance is used as the width is always zero for the white spaces.
+        tmpLineLayout.whiteSpaceLengthEndOfLine += glyphMetrics.advance;
+        // The advance is used as the width is always zero for the white spaces.
       }
       else
       {
@@ -771,11 +847,13 @@ struct Engine::Impl
         while(tmpLineLayout.length + tmpLineLayout.whiteSpaceLengthEndOfLine > targetWidth && glyphIndexToRemove < glyphIndex)
         {
           GlyphMetrics glyphMetrics;
+          calculatedAdvance = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + glyphIndexToRemove))), characterSpacing, (*(glyphsBuffer + glyphIndexToRemove)).advance);
           GetGlyphsMetrics(glyphIndexToRemove,
                            numberOfGLyphsInGroup,
                            glyphMetrics,
                            glyphsBuffer,
-                           mMetrics);
+                           mMetrics,
+                           calculatedAdvance);
 
           const Length numberOfGLyphsInGroup = GetNumberOfGlyphsOfGroup(glyphIndexToRemove,
                                                                         lastGlyphOfParagraphPlusOne,
@@ -1002,16 +1080,14 @@ struct Engine::Impl
     const GlyphInfo& glyph = *(glyphsBuffer + startIndexForGlyph);
     float            penX  = -glyph.xBearing + mCursorWidth + outlineWidth; //
 
-    for(GlyphIndex i = 0u; i < numberOfGlyphs; ++i)
-    {
-      const GlyphInfo& glyph    = *(glyphsBuffer + startIndexForGlyph + i);
-      Vector2&         position = *(glyphPositionsBuffer + startIndexForGlyphPositions + i);
-
-      position.x = penX + glyph.xBearing;
-      position.y = -glyph.yBearing;
-
-      penX += (glyph.advance + interGlyphExtraAdvance);
-    }
+    CalculateGlyphPositionsLTR(layoutParameters.textModel->mVisualModel,
+                               layoutParameters.textModel->mLogicalModel,
+                               interGlyphExtraAdvance,
+                               numberOfGlyphs,
+                               startIndexForGlyph, // startIndexForGlyph is the index of the first glyph in the line
+                               startIndexForGlyphPositions,
+                               glyphPositionsBuffer,
+                               penX);
 
     if(layout.isSplitToTwoHalves)
     {
@@ -1019,16 +1095,14 @@ struct Engine::Impl
       const Length     numberOfGlyphsInSecondHalfLine         = layout.numberOfGlyphsInSecondHalfLine;
       const GlyphIndex startIndexForGlyphPositionsnSecondHalf = layout.glyphIndexInSecondHalfLine - layoutParameters.startGlyphIndex;
 
-      for(GlyphIndex i = 0u; i < numberOfGlyphsInSecondHalfLine; ++i)
-      {
-        const GlyphInfo& glyph    = *(glyphsBuffer + startIndexForGlyphInSecondHalf + i);
-        Vector2&         position = *(glyphPositionsBuffer + startIndexForGlyphPositionsnSecondHalf + i);
-
-        position.x = penX + glyph.xBearing;
-        position.y = -glyph.yBearing;
-
-        penX += (glyph.advance + interGlyphExtraAdvance);
-      }
+      CalculateGlyphPositionsLTR(layoutParameters.textModel->mVisualModel,
+                                 layoutParameters.textModel->mLogicalModel,
+                                 interGlyphExtraAdvance,
+                                 numberOfGlyphsInSecondHalfLine,
+                                 startIndexForGlyphInSecondHalf, // startIndexForGlyph is the index of the first glyph in the line
+                                 startIndexForGlyphPositionsnSecondHalf,
+                                 glyphPositionsBuffer,
+                                 penX);
     }
   }
 
@@ -1037,11 +1111,9 @@ struct Engine::Impl
                          LayoutBidiParameters& layoutBidiParameters,
                          const LineLayout&     layout)
   {
-    const Character* const          textBuffer               = layoutParameters.textModel->mLogicalModel->mText.Begin();
     const BidirectionalLineInfoRun& bidiLine                 = layoutParameters.textModel->mLogicalModel->mBidirectionalLineInfo[layoutBidiParameters.bidiLineIndex];
     const GlyphInfo* const          glyphsBuffer             = layoutParameters.textModel->mVisualModel->mGlyphs.Begin();
     const GlyphIndex* const         charactersToGlyphsBuffer = layoutParameters.textModel->mVisualModel->mCharactersToGlyph.Begin();
-    const Length* const             glyphsPerCharacterBuffer = layoutParameters.textModel->mVisualModel->mGlyphsPerCharacter.Begin();
 
     CharacterIndex characterLogicalIndex = 0u;
     CharacterIndex characterVisualIndex  = bidiLine.characterRunForSecondHalfLine.characterIndex + *(bidiLine.visualToLogicalMapSecondHalf + characterLogicalIndex);
@@ -1051,20 +1123,14 @@ struct Engine::Impl
 
     if(layout.isSplitToTwoHalves)
     {
-      while(TextAbstraction::IsWhiteSpace(*(textBuffer + characterVisualIndex)))
-      {
-        const GlyphIndex glyphIndex = *(charactersToGlyphsBuffer + characterVisualIndex);
-        const GlyphInfo& glyph      = *(glyphsBuffer + glyphIndex);
-
-        Vector2& position = *(glyphPositionsBuffer + glyphIndex - layoutParameters.startGlyphIndex);
-        position.x        = penX;
-        position.y        = -glyph.yBearing;
-
-        penX += glyph.advance;
-
-        ++characterLogicalIndex;
-        characterVisualIndex = bidiLine.characterRun.characterIndex + *(bidiLine.visualToLogicalMap + characterLogicalIndex);
-      }
+      CalculateGlyphPositionsRTL(layoutParameters.textModel->mVisualModel,
+                                 layoutParameters.textModel->mLogicalModel,
+                                 layoutBidiParameters.bidiLineIndex,
+                                 layoutParameters.startGlyphIndex,
+                                 glyphPositionsBuffer,
+                                 characterVisualIndex,
+                                 characterLogicalIndex,
+                                 penX);
     }
 
     if(characterLogicalIndex == bidiLine.characterRunForSecondHalfLine.numberOfCharacters)
@@ -1073,20 +1139,14 @@ struct Engine::Impl
       characterLogicalIndex = 0u;
       characterVisualIndex  = bidiLine.characterRun.characterIndex + *(bidiLine.visualToLogicalMap + characterLogicalIndex);
 
-      while(TextAbstraction::IsWhiteSpace(*(textBuffer + characterVisualIndex)))
-      {
-        const GlyphIndex glyphIndex = *(charactersToGlyphsBuffer + characterVisualIndex);
-        const GlyphInfo& glyph      = *(glyphsBuffer + glyphIndex);
-
-        Vector2& position = *(glyphPositionsBuffer + glyphIndex - layoutParameters.startGlyphIndex);
-        position.x        = penX;
-        position.y        = -glyph.yBearing;
-
-        penX += glyph.advance;
-
-        ++characterLogicalIndex;
-        characterVisualIndex = bidiLine.characterRun.characterIndex + *(bidiLine.visualToLogicalMap + characterLogicalIndex);
-      }
+      CalculateGlyphPositionsRTL(layoutParameters.textModel->mVisualModel,
+                                 layoutParameters.textModel->mLogicalModel,
+                                 layoutBidiParameters.bidiLineIndex,
+                                 layoutParameters.startGlyphIndex,
+                                 glyphPositionsBuffer,
+                                 characterVisualIndex,
+                                 characterLogicalIndex,
+                                 penX);
     }
 
     const GlyphIndex glyphIndex = *(charactersToGlyphsBuffer + characterVisualIndex);
@@ -1097,59 +1157,28 @@ struct Engine::Impl
     // Traverses the characters of the right to left paragraph.
     if(layout.isSplitToTwoHalves && !extendedToSecondHalf)
     {
-      for(; characterLogicalIndex < bidiLine.characterRunForSecondHalfLine.numberOfCharacters;
-          ++characterLogicalIndex)
-      {
-        // Convert the character in the logical order into the character in the visual order.
-        const CharacterIndex characterVisualIndex = bidiLine.characterRunForSecondHalfLine.characterIndex + *(bidiLine.visualToLogicalMapSecondHalf + characterLogicalIndex);
-
-        // Get the number of glyphs of the character.
-        const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + characterVisualIndex);
-
-        for(GlyphIndex index = 0u; index < numberOfGlyphs; ++index)
-        {
-          // Convert the character in the visual order into the glyph in the visual order.
-          const GlyphIndex glyphIndex = *(charactersToGlyphsBuffer + characterVisualIndex) + index;
-
-          DALI_ASSERT_DEBUG(glyphIndex < layoutParameters.textModel->mVisualModel->mGlyphs.Count());
-
-          const GlyphInfo& glyph    = *(glyphsBuffer + glyphIndex);
-          Vector2&         position = *(glyphPositionsBuffer + glyphIndex - layoutParameters.startGlyphIndex);
-
-          position.x = penX + glyph.xBearing;
-          position.y = -glyph.yBearing;
-
-          penX += (glyph.advance + layoutParameters.interGlyphExtraAdvance);
-        }
-      }
+      TraversesCharactersForGlyphPositionsRTL(layoutParameters.textModel->mVisualModel,
+                                              layoutParameters.textModel->mLogicalModel->mText.Begin(),
+                                              layoutParameters.startGlyphIndex,
+                                              layoutParameters.interGlyphExtraAdvance,
+                                              bidiLine.characterRunForSecondHalfLine,
+                                              bidiLine.visualToLogicalMapSecondHalf,
+                                              glyphPositionsBuffer,
+                                              characterLogicalIndex,
+                                              penX);
     }
 
     characterLogicalIndex = extendedToSecondHalf ? characterLogicalIndex : 0u;
-    for(; characterLogicalIndex < bidiLine.characterRun.numberOfCharacters;
-        ++characterLogicalIndex)
-    {
-      // Convert the character in the logical order into the character in the visual order.
-      const CharacterIndex characterVisualIndex = bidiLine.characterRun.characterIndex + *(bidiLine.visualToLogicalMap + characterLogicalIndex);
-
-      // Get the number of glyphs of the character.
-      const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + characterVisualIndex);
-
-      for(GlyphIndex index = 0u; index < numberOfGlyphs; ++index)
-      {
-        // Convert the character in the visual order into the glyph in the visual order.
-        const GlyphIndex glyphIndex = *(charactersToGlyphsBuffer + characterVisualIndex) + index;
 
-        DALI_ASSERT_DEBUG(glyphIndex < layoutParameters.textModel->mVisualModel->mGlyphs.Count());
-
-        const GlyphInfo& glyph    = *(glyphsBuffer + glyphIndex);
-        Vector2&         position = *(glyphPositionsBuffer + glyphIndex - layoutParameters.startGlyphIndex);
-
-        position.x = penX + glyph.xBearing;
-        position.y = -glyph.yBearing;
-
-        penX += (glyph.advance + layoutParameters.interGlyphExtraAdvance);
-      }
-    }
+    TraversesCharactersForGlyphPositionsRTL(layoutParameters.textModel->mVisualModel,
+                                            layoutParameters.textModel->mLogicalModel->mText.Begin(),
+                                            layoutParameters.startGlyphIndex,
+                                            layoutParameters.interGlyphExtraAdvance,
+                                            bidiLine.characterRun,
+                                            bidiLine.visualToLogicalMap,
+                                            glyphPositionsBuffer,
+                                            characterLogicalIndex,
+                                            penX);
   }
 
   /**
@@ -1275,7 +1304,7 @@ struct Engine::Impl
       layoutSize.width = layoutParameters.boundingBox.width;
       if(layoutSize.height < Math::MACHINE_EPSILON_1000)
       {
-        layoutSize.height += (lineRun->ascender + -lineRun->descender) + lineRun->lineSpacing;
+        layoutSize.height += GetLineHeight(*lineRun, true);
       }
 
       const Vector<BidirectionalLineInfoRun>& bidirectionalLinesInfo = layoutParameters.textModel->mLogicalModel->mBidirectionalLineInfo;
@@ -1371,10 +1400,7 @@ 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;
+    lineRun.lineSpacing = GetLineSpacing(lineRun.ascender + -lineRun.descender);
 
     // Update the actual size.
     if(lineRun.width > layoutSize.width)
@@ -1382,7 +1408,7 @@ struct Engine::Impl
       layoutSize.width = lineRun.width;
     }
 
-    layoutSize.height += (lineRun.ascender + -lineRun.descender) + lineRun.lineSpacing;
+    layoutSize.height += GetLineHeight(lineRun, isLastLine);
   }
 
   /**
@@ -1428,12 +1454,9 @@ struct Engine::Impl
     lineRun.direction                       = LTR;
     lineRun.ellipsis                        = false;
 
-    lineRun.lineSpacing = mDefaultLineSize - (lineRun.ascender + -lineRun.descender);
-    lineRun.lineSpacing = lineRun.lineSpacing < 0.f ? 0.f : lineRun.lineSpacing;
-
-    lineRun.lineSpacing += mDefaultLineSpacing;
+    lineRun.lineSpacing = GetLineSpacing(lineRun.ascender + -lineRun.descender);
 
-    layoutSize.height += (lineRun.ascender + -lineRun.descender) + lineRun.lineSpacing;
+    layoutSize.height += GetLineHeight(lineRun, true);
   }
 
   /**
@@ -1450,14 +1473,15 @@ struct Engine::Impl
         it != endIt;
         ++it)
     {
-      const LineRun& line = *it;
+      const LineRun& line       = *it;
+      bool           isLastLine = (it + 1 == endIt);
 
       if(line.width > layoutSize.width)
       {
         layoutSize.width = line.width;
       }
 
-      layoutSize.height += (line.ascender + -line.descender) + line.lineSpacing;
+      layoutSize.height += GetLineHeight(line, isLastLine);
     }
   }
 
@@ -1829,14 +1853,7 @@ struct Engine::Impl
         }
 
         // Updates the vertical pen's position.
-        penY += -layout.descender + layout.lineSpacing + mDefaultLineSpacing;
-        // If there is a defaultLineSize, updates the pen's position.
-        if(mDefaultLineSize > 0.f)
-        {
-          float lineSpacing = mDefaultLineSize - (layout.ascender + -layout.descender);
-          lineSpacing       = lineSpacing < 0.f ? 0.f : lineSpacing;
-          penY += lineSpacing;
-        }
+        penY += -layout.descender + layout.lineSpacing + GetLineSpacing(layout.ascender + -layout.descender);
 
         // Increase the glyph index.
         index = nextIndex;
@@ -1861,7 +1878,6 @@ struct Engine::Impl
             linesBuffer[lineIndex].ellipsis = false;
           }
           numberOfLines--;
-          lineIndex++;
         }
         linesBuffer[0u].ellipsis = true;
       }
@@ -2096,6 +2112,7 @@ struct Engine::Impl
   float mDefaultLineSize;
 
   IntrusivePtr<Metrics> mMetrics;
+  float                 mRelativeLineSize;
 };
 
 Engine::Engine()
@@ -2187,6 +2204,16 @@ float Engine::GetDefaultLineSize() const
   return mImpl->mDefaultLineSize;
 }
 
+void Engine::SetRelativeLineSize(float relativeLineSize)
+{
+  mImpl->mRelativeLineSize = relativeLineSize;
+}
+
+float Engine::GetRelativeLineSize() const
+{
+  return mImpl->mRelativeLineSize;
+}
+
 } // namespace Layout
 
 } // namespace Text