Merge "fix linespacing calculation in TextLabel" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / text-typesetter.cpp
index 790e247..500a081 100644 (file)
@@ -26,6 +26,7 @@
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
+#include <dali-toolkit/internal/text/rendering/styles/character-spacing-helper-functions.h>
 #include <dali-toolkit/internal/text/rendering/styles/strikethrough-helper-functions.h>
 #include <dali-toolkit/internal/text/rendering/styles/underline-helper-functions.h>
 #include <dali-toolkit/internal/text/rendering/view-model.h>
@@ -260,31 +261,6 @@ void TypesetGlyph(GlyphData&           data,
   }
 }
 
-bool doGlyphHaveStrikethrough(GlyphIndex                           index,
-                              const Vector<StrikethroughGlyphRun>& strikethroughRuns,
-                              Vector4&                             strikethroughColor)
-{
-  for(Vector<StrikethroughGlyphRun>::ConstIterator it    = strikethroughRuns.Begin(),
-                                                   endIt = strikethroughRuns.End();
-      it != endIt;
-      ++it)
-  {
-    const StrikethroughGlyphRun& run = *it;
-
-    if((run.glyphRun.glyphIndex <= index) && (index < run.glyphRun.glyphIndex + run.glyphRun.numberOfGlyphs))
-    {
-      if(run.isColorSet)
-      {
-        strikethroughColor = run.color;
-      }
-
-      return true;
-    }
-  }
-
-  return false;
-}
-
 /// Draws the specified color to the pixel buffer
 void WriteColorToPixelBuffer(
   GlyphData&         glyphData,
@@ -546,18 +522,20 @@ Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuff
 }
 
 /// Draws the specified strikethrough color to the buffer
-void DrawStrikethrough(
-  const Vector4&     strikethroughColor,
-  const unsigned int bufferWidth,
-  const unsigned int bufferHeight,
-  GlyphData&         glyphData,
-  const float        baseline,
-  const LineRun&     line,
-  const float        maxStrikethroughHeight,
-  const float        lineExtentLeft,
-  const float        lineExtentRight,
-  float              strikethroughStartingYPosition)
+void DrawStrikethrough(const unsigned int                  bufferWidth,
+                       const unsigned int                  bufferHeight,
+                       GlyphData&                          glyphData,
+                       const float                         baseline,
+                       const float                         strikethroughStartingYPosition,
+                       const float                         maxStrikethroughHeight,
+                       const float                         lineExtentLeft,
+                       const float                         lineExtentRight,
+                       const StrikethroughStyleProperties& commonStrikethroughProperties,
+                       const StrikethroughStyleProperties& currentStrikethroughProperties,
+                       const LineRun&                      line)
 {
+  const Vector4& strikethroughColor = currentStrikethroughProperties.colorDefined ? currentStrikethroughProperties.color : commonStrikethroughProperties.color;
+
   uint32_t* bitmapBuffer = reinterpret_cast<uint32_t*>(glyphData.bitmapBuffer.GetBuffer());
 
   for(unsigned int y = strikethroughStartingYPosition; y < strikethroughStartingYPosition + maxStrikethroughHeight; y++)
@@ -840,7 +818,6 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
   // Whether to use the default color.
   const bool     useDefaultColor = (NULL == colorsBuffer);
   const Vector4& defaultColor    = mModel->GetDefaultColor();
-  Vector4        currentStrikethroughColor;
 
   // Create and initialize the pixel buffer.
   GlyphData glyphData;
@@ -871,12 +848,6 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
     // Increases the vertical offset with the line's ascender.
     glyphData.verticalOffset += static_cast<int>(line.ascender);
 
-    // Include line spacing after first line
-    if(lineIndex > 0u)
-    {
-      glyphData.verticalOffset += static_cast<int>(line.lineSpacing);
-    }
-
     // Retrieves the glyph's outline width
     float outlineWidth = static_cast<float>(mModel->GetOutlineWidth());
 
@@ -901,11 +872,12 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
       }
     }
 
-    const bool     underlineEnabled     = mModel->IsUnderlineEnabled();
-    const bool     strikethroughEnabled = mModel->IsStrikethroughEnabled();
-    const Vector4& strikethroughColor   = mModel->GetStrikethroughColor();
-    const float    strikethroughHeight  = mModel->GetStrikethroughHeight();
-    const float    characterSpacing     = mModel->GetCharacterSpacing();
+    const bool  underlineEnabled      = mModel->IsUnderlineEnabled();
+    const bool  strikethroughEnabled  = mModel->IsStrikethroughEnabled();
+    const float modelCharacterSpacing = mModel->GetCharacterSpacing();
+
+    // Get the character-spacing runs.
+    const Vector<CharacterSpacingGlyphRun>& characterSpacingGlyphRuns = mModel->GetCharacterSpacingGlyphRuns();
 
     // Aggregate underline-style-properties from mModel
     const UnderlineStyleProperties modelUnderlineProperties{mModel->GetUnderlineType(),
@@ -919,6 +891,12 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
                                                             true,
                                                             true};
 
+    // Aggregate strikethrough-style-properties from mModel
+    const StrikethroughStyleProperties modelStrikethroughProperties{mModel->GetStrikethroughColor(),
+                                                                    mModel->GetStrikethroughHeight(),
+                                                                    true,
+                                                                    true};
+
     // Get the underline runs.
     const Length               numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
     Vector<UnderlinedGlyphRun> underlineRuns;
@@ -931,16 +909,17 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
     strikethroughRuns.Resize(numberOfStrikethroughRuns);
     mModel->GetStrikethroughRuns(strikethroughRuns.Begin(), 0u, numberOfStrikethroughRuns);
 
-    bool thereAreUnderlinedGlyphs = false;
-    bool strikethroughGlyphsExist = false;
+    bool thereAreUnderlinedGlyphs    = false;
+    bool thereAreStrikethroughGlyphs = false;
 
     float currentUnderlinePosition   = 0.0f;
     float currentUnderlineHeight     = modelUnderlineProperties.height;
     float maxUnderlineHeight         = currentUnderlineHeight;
     auto  currentUnderlineProperties = modelUnderlineProperties;
 
-    float currentStrikethroughHeight     = strikethroughHeight;
+    float currentStrikethroughHeight     = modelStrikethroughProperties.height;
     float maxStrikethroughHeight         = currentStrikethroughHeight;
+    auto  currentStrikethroughProperties = modelStrikethroughProperties;
     float strikethroughStartingYPosition = 0.0f;
 
     FontId lastFontId = 0;
@@ -1004,13 +983,15 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
 
       Vector<UnderlinedGlyphRun>::ConstIterator currentUnderlinedGlyphRunIt = underlineRuns.End();
       const bool                                underlineGlyph              = underlineEnabled || IsGlyphUnderlined(glyphIndex, underlineRuns, currentUnderlinedGlyphRunIt);
-      currentUnderlineProperties                                            = GetCurrentUnderlineProperties(underlineGlyph, underlineRuns, currentUnderlinedGlyphRunIt, modelUnderlineProperties);
-      currentUnderlineHeight                                                = GetCurrentUnderlineHeight(underlineRuns, currentUnderlinedGlyphRunIt, modelUnderlineProperties.height);
+      currentUnderlineProperties                                            = GetCurrentUnderlineProperties(glyphIndex, underlineGlyph, underlineRuns, currentUnderlinedGlyphRunIt, modelUnderlineProperties);
+      currentUnderlineHeight                                                = currentUnderlineProperties.height;
       thereAreUnderlinedGlyphs                                              = thereAreUnderlinedGlyphs || underlineGlyph;
 
-      currentStrikethroughColor     = strikethroughColor;
-      const bool strikethroughGlyph = strikethroughEnabled || doGlyphHaveStrikethrough(glyphIndex, strikethroughRuns, currentStrikethroughColor);
-      strikethroughGlyphsExist      = strikethroughGlyphsExist || strikethroughGlyph;
+      Vector<StrikethroughGlyphRun>::ConstIterator currentStrikethroughGlyphRunIt = strikethroughRuns.End();
+      const bool                                   strikethroughGlyph             = strikethroughEnabled || IsGlyphStrikethrough(glyphIndex, strikethroughRuns, currentStrikethroughGlyphRunIt);
+      currentStrikethroughProperties                                              = GetCurrentStrikethroughProperties(glyphIndex, strikethroughGlyph, strikethroughRuns, currentStrikethroughGlyphRunIt, modelStrikethroughProperties);
+      currentStrikethroughHeight                                                  = currentStrikethroughProperties.height;
+      thereAreStrikethroughGlyphs                                                 = thereAreStrikethroughGlyphs || strikethroughGlyph;
 
       // Are we still using the same fontId as previous
       if((glyphInfo->fontId != lastFontId) && (strikethroughGlyph || underlineGlyph))
@@ -1041,10 +1022,11 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
 
       if(addHyphen)
       {
-        GlyphInfo tempInfo = *(glyphsBuffer + elidedGlyphIndex);
-        calculatedAdvance  = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + elidedGlyphIndex))), characterSpacing, tempInfo.advance);
-        position.x         = position.x + calculatedAdvance - tempInfo.xBearing + glyphInfo->xBearing;
-        position.y         = -glyphInfo->yBearing;
+        GlyphInfo   tempInfo         = *(glyphsBuffer + elidedGlyphIndex);
+        const float characterSpacing = GetGlyphCharacterSpacing(glyphIndex, characterSpacingGlyphRuns, modelCharacterSpacing);
+        calculatedAdvance            = GetCalculatedAdvance(*(textBuffer + (*(glyphToCharacterMapBuffer + elidedGlyphIndex))), characterSpacing, tempInfo.advance);
+        position.x                   = position.x + calculatedAdvance - tempInfo.xBearing + glyphInfo->xBearing;
+        position.y                   = -glyphInfo->yBearing;
       }
 
       if(baseline < position.y + glyphInfo->yBearing)
@@ -1163,15 +1145,15 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
     }
 
     // Draw the strikethrough from the leftmost glyph to the rightmost glyph
-    if(strikethroughGlyphsExist && style == Typesetter::STYLE_STRIKETHROUGH)
+    if(thereAreStrikethroughGlyphs && style == Typesetter::STYLE_STRIKETHROUGH)
     {
       //TODO : The currently implemented strikethrough creates a strikethrough on the line level. We need to create different strikethroughs the case of glyphs with different sizes.
       strikethroughStartingYPosition = (glyphData.verticalOffset + baseline + currentUnderlinePosition) - ((line.ascender) * HALF); // Since Free Type font doesn't contain the strikethrough-position property, strikethrough position will be calculated by moving the underline position upwards by half the value of the line height.
-      DrawStrikethrough(currentStrikethroughColor, bufferWidth, bufferHeight, glyphData, baseline, line, maxStrikethroughHeight, lineExtentLeft, lineExtentRight, strikethroughStartingYPosition);
+      DrawStrikethrough(bufferWidth, bufferHeight, glyphData, baseline, strikethroughStartingYPosition, maxStrikethroughHeight, lineExtentLeft, lineExtentRight, modelStrikethroughProperties, currentStrikethroughProperties, line);
     }
 
-    // Increases the vertical offset with the line's descender.
-    glyphData.verticalOffset += static_cast<int>(-line.descender);
+    // Increases the vertical offset with the line's descender & line spacing.
+    glyphData.verticalOffset += static_cast<int>(-line.descender+line.lineSpacing);
   }
 
   return glyphData.bitmapBuffer;
@@ -1256,24 +1238,15 @@ Devel::PixelBuffer Typesetter::ApplyUnderlineMarkupImageBuffer(Devel::PixelBuffe
   //The outer loop to iterate on the separated chunks of underlined glyph runs
   while(itGlyphRun != endItGlyphRun)
   {
-    const UnderlineStyleProperties& firstUnderlineStyleProperties = itGlyphRun->properties;
-
     startGlyphIndex = itGlyphRun->glyphRun.glyphIndex;
-    endGlyphIndex   = startGlyphIndex;
-    //The inner loop to make a connected underline for the consecutive characters
-    do
-    {
-      endGlyphIndex += itGlyphRun->glyphRun.numberOfGlyphs;
-      itGlyphRun++;
-    } while(itGlyphRun != endItGlyphRun && itGlyphRun->glyphRun.glyphIndex == endGlyphIndex &&
-            (firstUnderlineStyleProperties == itGlyphRun->properties));
-
-    endGlyphIndex--;
+    endGlyphIndex   = startGlyphIndex + itGlyphRun->glyphRun.numberOfGlyphs - 1;
 
     // Create the image buffer for underline
     Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, horizontalOffset, verticalOffset, startGlyphIndex, endGlyphIndex);
     // Combine the two buffers
-    topPixelBuffer = CombineImageBuffer(topPixelBuffer, underlineImageBuffer, bufferWidth, bufferHeight);
+    topPixelBuffer = CombineImageBuffer(underlineImageBuffer, topPixelBuffer, bufferWidth, bufferHeight);
+
+    itGlyphRun++;
   }
 
   return topPixelBuffer;