Merge "Add ApplyCustomFragmentPrefix" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / text-typesetter.cpp
index 83d67d5..cea2a19 100644 (file)
@@ -417,6 +417,116 @@ void DrawBackgroundColor(
   }
 }
 
+Devel::PixelBuffer DrawGlyphsBackground(const ViewModel* model, Devel::PixelBuffer& buffer, const unsigned int bufferWidth, const unsigned int bufferHeight, bool ignoreHorizontalAlignment, int horizontalOffset, int verticalOffset)
+{
+  // Retrieve lines, glyphs, positions and colors from the view model.
+  const Length            modelNumberOfLines           = model->GetNumberOfLines();
+  const LineRun* const    modelLinesBuffer             = model->GetLines();
+  const Length            numberOfGlyphs               = model->GetNumberOfGlyphs();
+  const GlyphInfo* const  glyphsBuffer                 = model->GetGlyphs();
+  const Vector2* const    positionBuffer               = model->GetLayout();
+  const Vector4* const    backgroundColorsBuffer       = model->GetBackgroundColors();
+  const ColorIndex* const backgroundColorIndicesBuffer = model->GetBackgroundColorIndices();
+
+  // Create and initialize the pixel buffer.
+  GlyphData glyphData;
+  glyphData.verticalOffset   = verticalOffset;
+  glyphData.width            = bufferWidth;
+  glyphData.height           = bufferHeight;
+  glyphData.bitmapBuffer     = buffer;
+  glyphData.horizontalOffset = 0;
+
+  ColorIndex prevBackgroundColorIndex = 0;
+  ColorIndex backgroundColorIndex     = 0;
+
+  // Traverses the lines of the text.
+  for(LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex)
+  {
+    const LineRun& line = *(modelLinesBuffer + lineIndex);
+
+    // Sets the horizontal offset of the line.
+    glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>(line.alignmentOffset);
+    glyphData.horizontalOffset += horizontalOffset;
+
+    // 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);
+    }
+
+    float left     = bufferWidth;
+    float right    = 0.0f;
+    float baseline = 0.0f;
+
+    // Traverses the glyphs of the line.
+    const GlyphIndex endGlyphIndex = std::min(numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs);
+    for(GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex)
+    {
+      // Retrieve the glyph's info.
+      const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
+
+      if((glyphInfo->width < Math::MACHINE_EPSILON_1000) ||
+         (glyphInfo->height < Math::MACHINE_EPSILON_1000))
+      {
+        // Nothing to do if default background color, the glyph's width or height is zero.
+        continue;
+      }
+
+      backgroundColorIndex = (nullptr == backgroundColorsBuffer) ? 0u : *(backgroundColorIndicesBuffer + glyphIndex);
+
+      if((backgroundColorIndex != prevBackgroundColorIndex) &&
+         (prevBackgroundColorIndex != 0u))
+      {
+        const Vector4& backgroundColor = *(backgroundColorsBuffer + prevBackgroundColorIndex - 1u);
+        DrawBackgroundColor(backgroundColor, bufferWidth, bufferHeight, glyphData, baseline, line, left, right);
+      }
+
+      if(backgroundColorIndex == 0u)
+      {
+        prevBackgroundColorIndex = backgroundColorIndex;
+        //if background color is the default do nothing
+        continue;
+      }
+
+      // Retrieves the glyph's position.
+      const Vector2* const position = positionBuffer + glyphIndex;
+
+      if(baseline < position->y + glyphInfo->yBearing)
+      {
+        baseline = position->y + glyphInfo->yBearing;
+      }
+
+      // Calculate the positions of leftmost and rightmost glyphs in the current line
+      if((position->x < left) || (backgroundColorIndex != prevBackgroundColorIndex))
+      {
+        left = position->x - glyphInfo->xBearing;
+      }
+
+      if(position->x + glyphInfo->width > right)
+      {
+        right = position->x - glyphInfo->xBearing + glyphInfo->advance;
+      }
+
+      prevBackgroundColorIndex = backgroundColorIndex;
+    }
+
+    //draw last background at line end if not default
+    if(backgroundColorIndex != 0u)
+    {
+      const Vector4& backgroundColor = *(backgroundColorsBuffer + backgroundColorIndex - 1u);
+      DrawBackgroundColor(backgroundColor, bufferWidth, bufferHeight, glyphData, baseline, line, left, right);
+    }
+
+    // Increases the vertical offset with the line's descender.
+    glyphData.verticalOffset += static_cast<int>(-line.descender);
+  }
+
+  return glyphData.bitmapBuffer;
+}
+
 } // namespace
 
 TypesetterPtr Typesetter::New(const ModelInterface* const model)
@@ -429,6 +539,24 @@ ViewModel* Typesetter::GetViewModel()
   return mModel;
 }
 
+Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth, const unsigned int bufferHeight, Pixel::Format pixelFormat)
+{
+  Devel::PixelBuffer imageBuffer = Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat);
+
+  if(Pixel::RGBA8888 == pixelFormat)
+  {
+    const unsigned int bufferSizeInt  = bufferWidth * bufferHeight;
+    const unsigned int bufferSizeChar = 4u * bufferSizeInt;
+    memset(imageBuffer.GetBuffer(), 0u, bufferSizeChar);
+  }
+  else
+  {
+    memset(imageBuffer.GetBuffer(), 0, bufferWidth * bufferHeight);
+  }
+
+  return imageBuffer;
+}
+
 PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat)
 {
   // @todo. This initial implementation for a TextLabel has only one visible page.
@@ -520,14 +648,16 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect
   const unsigned int bufferSizeInt  = bufferWidth * bufferHeight;
   const unsigned int bufferSizeChar = 4u * bufferSizeInt;
 
-  Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
+  //Elided text in ellipsis at START could start on index greater than 0
+  auto startIndexOfGlyphs = mModel->GetStartIndexOfElidedGlyphs();
+  auto endIndexOfGlyphs   = mModel->GetEndIndexOfElidedGlyphs();
 
   Devel::PixelBuffer imageBuffer;
 
   if(RENDER_MASK == behaviour)
   {
     // Generate the image buffer as an alpha mask for color glyphs.
-    imageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1);
+    imageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
   }
   else if(RENDER_NO_TEXT == behaviour)
   {
@@ -538,7 +668,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect
   else
   {
     // Generate the image buffer for the text with no style.
-    imageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1);
+    imageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
   }
 
   if((RENDER_NO_STYLES != behaviour) && (RENDER_MASK != behaviour))
@@ -548,7 +678,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect
     if(outlineWidth != 0u)
     {
       // Create the image buffer for outline
-      Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1);
+      Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
 
       // Combine the two buffers
       imageBuffer = CombineImageBuffer(imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight);
@@ -561,7 +691,7 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect
     if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
     {
       // Create the image buffer for shadow
-      Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1);
+      Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
 
       // Check whether it will be a soft shadow
       const float& blurRadius = mModel->GetShadowBlurRadius();
@@ -580,17 +710,32 @@ PixelData Typesetter::Render(const Vector2& size, Toolkit::DevelText::TextDirect
     if(underlineEnabled)
     {
       // Create the image buffer for underline
-      Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1);
+      Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
 
       // Combine the two buffers
       imageBuffer = CombineImageBuffer(imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight);
     }
 
     // Generate the background if enabled
-    const bool backgroundEnabled = mModel->IsBackgroundEnabled();
-    if(backgroundEnabled)
+    const bool backgroundEnabled   = mModel->IsBackgroundEnabled();
+    const bool backgroundMarkupSet = mModel->IsMarkupBackgroundColorSet();
+    if(backgroundEnabled || backgroundMarkupSet)
     {
-      Devel::PixelBuffer backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1);
+      Devel::PixelBuffer backgroundImageBuffer;
+
+      if(backgroundEnabled)
+      {
+        backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, startIndexOfGlyphs, endIndexOfGlyphs);
+      }
+      else
+      {
+        backgroundImageBuffer = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat);
+      }
+
+      if(backgroundMarkupSet)
+      {
+        DrawGlyphsBackground(mModel, backgroundImageBuffer, bufferWidth, bufferHeight, ignoreHorizontalAlignment, penX, penY);
+      }
 
       // Combine the two buffers
       imageBuffer = CombineImageBuffer(imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight);
@@ -612,7 +757,6 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
   // Retrieve lines, glyphs, positions and colors from the view model.
   const Length            modelNumberOfLines = mModel->GetNumberOfLines();
   const LineRun* const    modelLinesBuffer   = mModel->GetLines();
-  const Length            numberOfGlyphs     = mModel->GetNumberOfGlyphs();
   const GlyphInfo* const  glyphsBuffer       = mModel->GetGlyphs();
   const Vector2* const    positionBuffer     = mModel->GetLayout();
   const Vector4* const    colorsBuffer       = mModel->GetColors();
@@ -621,6 +765,13 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
   const Length*           hyphenIndices      = mModel->GetHyphenIndices();
   const Length            hyphensCount       = mModel->GetHyphensCount();
 
+  // Elided text info. Indices according to elided text and Ellipsis position.
+  const auto startIndexOfGlyphs              = mModel->GetStartIndexOfElidedGlyphs();
+  const auto endIndexOfGlyphs                = mModel->GetEndIndexOfElidedGlyphs();
+  const auto firstMiddleIndexOfElidedGlyphs  = mModel->GetFirstMiddleIndexOfElidedGlyphs();
+  const auto secondMiddleIndexOfElidedGlyphs = mModel->GetSecondMiddleIndexOfElidedGlyphs();
+  const auto ellipsisPosition                = mModel->GetEllipsisPosition();
+
   // Whether to use the default color.
   const bool     useDefaultColor = (NULL == colorsBuffer);
   const Vector4& defaultColor    = mModel->GetDefaultColor();
@@ -630,20 +781,9 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
   glyphData.verticalOffset   = verticalOffset;
   glyphData.width            = bufferWidth;
   glyphData.height           = bufferHeight;
-  glyphData.bitmapBuffer     = Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat);
+  glyphData.bitmapBuffer     = CreateImageBuffer(bufferWidth, bufferHeight, pixelFormat);
   glyphData.horizontalOffset = 0;
 
-  if(Pixel::RGBA8888 == pixelFormat)
-  {
-    const unsigned int bufferSizeInt  = bufferWidth * bufferHeight;
-    const unsigned int bufferSizeChar = 4u * bufferSizeInt;
-    memset(glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar);
-  }
-  else
-  {
-    memset(glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight);
-  }
-
   // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
   TextAbstraction::FontClient fontClient  = TextAbstraction::FontClient::Get();
   Length                      hyphenIndex = 0;
@@ -714,8 +854,11 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
     bool  addHyphen       = false;
 
     // Traverses the glyphs of the line.
-    const GlyphIndex endGlyphIndex = std::min(numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs);
-    for(GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex)
+    const GlyphIndex startGlyphIndex = std::max(line.glyphRun.glyphIndex, startIndexOfGlyphs);
+    GlyphIndex       endGlyphIndex   = (line.isSplitToTwoHalves ? line.glyphRunSecondHalf.glyphIndex + line.glyphRunSecondHalf.numberOfGlyphs : line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs) - 1u;
+    endGlyphIndex                    = std::min(endGlyphIndex, endIndexOfGlyphs);
+
+    for(GlyphIndex glyphIndex = startGlyphIndex; glyphIndex <= endGlyphIndex; ++glyphIndex)
     {
       if(glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex)
       {
@@ -723,6 +866,25 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
         continue;
       }
 
+      //To handle START case of ellipsis, the first glyph has been shifted
+      //glyphIndex represent indices in whole glyphs but elidedGlyphIndex represents indices in elided Glyphs
+      GlyphIndex elidedGlyphIndex = glyphIndex - startIndexOfGlyphs;
+
+      //To handle MIDDLE case of ellipsis, the first glyph in the second half of line has been shifted and skip the removed glyph from middle.
+      if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
+      {
+        if(glyphIndex > firstMiddleIndexOfElidedGlyphs &&
+           glyphIndex < secondMiddleIndexOfElidedGlyphs)
+        {
+          // Ignore any glyph that removed for MIDDLE ellipsis
+          continue;
+        }
+        if(glyphIndex >= secondMiddleIndexOfElidedGlyphs)
+        {
+          elidedGlyphIndex -= (secondMiddleIndexOfElidedGlyphs - firstMiddleIndexOfElidedGlyphs - 1u);
+        }
+      }
+
       // Retrieve the glyph's info.
       const GlyphInfo* glyphInfo;
 
@@ -733,7 +895,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
       }
       else
       {
-        glyphInfo = glyphsBuffer + glyphIndex;
+        glyphInfo = glyphsBuffer + elidedGlyphIndex;
       }
 
       if((glyphInfo->width < Math::MACHINE_EPSILON_1000) ||
@@ -754,11 +916,11 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer(const unsigned int bufferWidth,
       } // underline
 
       // Retrieves the glyph's position.
-      Vector2 position = *(positionBuffer + glyphIndex);
+      Vector2 position = *(positionBuffer + elidedGlyphIndex);
 
       if(addHyphen)
       {
-        GlyphInfo tempInfo = *(glyphsBuffer + glyphIndex);
+        GlyphInfo tempInfo = *(glyphsBuffer + elidedGlyphIndex);
         position.x         = position.x + tempInfo.advance - tempInfo.xBearing + glyphInfo->xBearing;
         position.y         = -glyphInfo->yBearing;
       }