Reduce Cyclomatic Complexity of some methods in text-typesetter, transtion-data ...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / text-typesetter.cpp
index c6e0638..05b4a67 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.
 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
 
 // EXTERNAL INCLUDES
-#include <dali/devel-api/text-abstraction/font-client.h>
 #include <memory.h>
+#include <dali/devel-api/text-abstraction/font-client.h>
 #include <dali/public-api/common/constants.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/rendering/view-model.h>
+#include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
 
 namespace Dali
 {
@@ -79,22 +80,19 @@ void TypesetGlyph( GlyphData& data,
   if ( Pixel::RGBA8888 == pixelFormat )
   {
     // Whether the given glyph is a color one.
-    const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
+    const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap;
+    const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel( data.glyphBitmap.format );
+    const uint32_t alphaIndex = glyphPixelSize - 1u;
+    const bool swapChannelsBR = Pixel::BGRA8888 == data.glyphBitmap.format;
 
     // Pointer to the color glyph if there is one.
     const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>( data.glyphBitmap.buffer ) : NULL;
 
-    // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
-    // The format is RGBA8888.
-    uint32_t packedColor = 0u;
-    uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
-    *( packedColorBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
-    *( packedColorBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
-      *packedColorBuffer       = static_cast<uint8_t>( color->r * 255.f );
-
     // Initial vertical offset.
     const int yOffset = data.verticalOffset + position->y;
 
+    uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
+
     // Traverse the pixels of the glyph line per line.
     for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
     {
@@ -117,35 +115,51 @@ void TypesetGlyph( GlyphData& data,
           continue;
         }
 
-        uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
-
         if( isColorGlyph )
         {
-          // Retrieves the color from the color glyph. The format is BGRA8888.
+          // Retrieves the color from the color glyph.
           uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
           uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
 
-          if( Typesetter::STYLE_SHADOW == style )
-          {
-            // The shadow of color glyph needs to have the shadow color.
-            *( packedColorGlyphBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
-            *( packedColorGlyphBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
-              *packedColorGlyphBuffer       = static_cast<uint8_t>( color->r * 255.f );
-          }
-          else
-          {
-            std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
-          }
-
           // Update the alpha channel.
           if( Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style ) // Outline not shown for color glyph
           {
             // Create an alpha mask for color glyph.
             *( packedColorGlyphBuffer + 3u ) = 0u;
+            *( packedColorGlyphBuffer + 2u ) = 0u;
+            *( packedColorGlyphBuffer + 1u ) = 0u;
+              *packedColorGlyphBuffer        = 0u;
           }
           else
           {
-            *( packedColorGlyphBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
+            const uint8_t colorAlpha = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
+            *( packedColorGlyphBuffer + 3u ) = colorAlpha;
+
+            if( Typesetter::STYLE_SHADOW == style )
+            {
+              // The shadow of color glyph needs to have the shadow color.
+              *( packedColorGlyphBuffer + 2u ) = static_cast<uint8_t>( color->b * colorAlpha );
+              *( packedColorGlyphBuffer + 1u ) = static_cast<uint8_t>( color->g * colorAlpha );
+                *packedColorGlyphBuffer        = static_cast<uint8_t>( color->r * colorAlpha );
+            }
+            else
+            {
+              if( swapChannelsBR )
+              {
+                std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
+              }
+
+              *( packedColorGlyphBuffer + 2u ) = ( *( packedColorGlyphBuffer + 2u ) * colorAlpha / 255 );
+              *( packedColorGlyphBuffer + 1u ) = ( *( packedColorGlyphBuffer + 1u ) * colorAlpha / 255 );
+                *packedColorGlyphBuffer        = ( *( packedColorGlyphBuffer      ) * colorAlpha / 255 );
+
+              if( data.glyphBitmap.isColorBitmap )
+              {
+                *( packedColorGlyphBuffer + 2u ) = static_cast<uint8_t>( *( packedColorGlyphBuffer + 2u ) * color->b );
+                *( packedColorGlyphBuffer + 1u ) = static_cast<uint8_t>( *( packedColorGlyphBuffer + 1u ) * color->g );
+                  *packedColorGlyphBuffer        = static_cast<uint8_t>(   *packedColorGlyphBuffer * color->r );
+              }
+            }
           }
 
           // Set the color into the final pixel buffer.
@@ -153,8 +167,13 @@ void TypesetGlyph( GlyphData& data,
         }
         else
         {
+          // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
+          // The format is RGBA8888.
+          uint32_t packedColor = 0u;
+          uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
+
           // Update the alpha channel.
-          const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
+          const uint8_t alpha = *( data.glyphBitmap.buffer + glyphPixelSize * ( glyphBufferOffset + index ) + alphaIndex );
 
           // Copy non-transparent pixels only
           if ( alpha > 0u )
@@ -163,14 +182,18 @@ void TypesetGlyph( GlyphData& data,
             uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
             uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( &currentColor );
 
-            uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
-            uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
-
             // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
             // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
             // semi-transparent gaps between joint glyphs with overlapped pixels, which could
             // happen, for example, in the RTL text when we copy glyphs from right to left).
-            *( packedColorBuffer + 3u ) = std::max( currentAlpha, newAlpha );
+            uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
+            currentAlpha = std::max( currentAlpha, alpha );
+
+            // Color is pre-muliplied with its alpha.
+            *( packedColorBuffer + 3u ) = static_cast<uint8_t>( color->a * currentAlpha );
+            *( packedColorBuffer + 2u ) = static_cast<uint8_t>( color->b * currentAlpha );
+            *( packedColorBuffer + 1u ) = static_cast<uint8_t>( color->g * currentAlpha );
+            *( packedColorBuffer      ) = static_cast<uint8_t>( color->r * currentAlpha );
 
             // Set the color into the final pixel buffer.
             currentColor = packedColor;
@@ -182,11 +205,15 @@ void TypesetGlyph( GlyphData& data,
   else
   {
     // Whether the given glyph is a color one.
-    const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
+    const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap;
+    const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel( data.glyphBitmap.format );
+    const uint32_t alphaIndex = glyphPixelSize - 1u;
 
     // Initial vertical offset.
     const int yOffset = data.verticalOffset + position->y;
 
+    uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
+
     // Traverse the pixels of the glyph line per line.
     for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
     {
@@ -209,25 +236,22 @@ void TypesetGlyph( GlyphData& data,
           continue;
         }
 
-        uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
-
         if ( !isColorGlyph )
         {
           // Update the alpha channel.
-          const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
+          const uint8_t alpha = *( data.glyphBitmap.buffer + glyphPixelSize * ( glyphBufferOffset + index ) + alphaIndex );
 
           // Copy non-transparent pixels only
           if ( alpha > 0u )
           {
             // Check alpha of overlapped pixels
             uint8_t& currentAlpha = *( bitmapBuffer + verticalOffset + xOffsetIndex );
-            uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
 
             // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
             // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
             // semi-transparent gaps between joint glyphs with overlapped pixels, which could
             // happen, for example, in the RTL text when we copy glyphs from right to left).
-            *( bitmapBuffer + verticalOffset + xOffsetIndex ) = std::max( currentAlpha, newAlpha );
+            currentAlpha = std::max( currentAlpha, alpha );
           }
         }
       }
@@ -254,6 +278,149 @@ bool IsGlyphUnderlined( GlyphIndex index,
   return false;
 }
 
+/// Helper method to fetch the underline metrics for the specified font glyph
+void FetchFontUnderlineMetrics(
+    TextAbstraction::FontClient& fontClient,
+    const GlyphInfo* const glyphInfo,
+    float& currentUnderlinePosition,
+    const float underlineHeight,
+    float& currentUnderlineThickness,
+    float& maxUnderlineThickness,
+    FontId& lastUnderlinedFontId)
+{
+  FontMetrics fontMetrics;
+  fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
+  currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
+  const float descender = ceil( fabsf( fontMetrics.descender ) );
+
+  if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
+  {
+    currentUnderlineThickness = fontMetrics.underlineThickness;
+
+    // Ensure underline will be at least a pixel high
+    if ( currentUnderlineThickness < 1.0f )
+    {
+      currentUnderlineThickness = 1.0f;
+    }
+    else
+    {
+      currentUnderlineThickness = ceil( currentUnderlineThickness );
+    }
+  }
+
+  // The underline thickness should be the max underline thickness of all glyphs of the line.
+  if ( currentUnderlineThickness > maxUnderlineThickness )
+  {
+    maxUnderlineThickness = currentUnderlineThickness;
+  }
+
+  // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
+  if( currentUnderlinePosition > descender )
+  {
+    currentUnderlinePosition = descender;
+  }
+
+  if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
+  {
+    // Move offset down by one ( EFL behavior )
+    currentUnderlinePosition = 1.0f;
+  }
+
+  lastUnderlinedFontId = glyphInfo->fontId;
+}
+
+/// Draws the specified color to the pixel buffer
+void WriteColorToPixelBuffer(
+    GlyphData& glyphData,
+    uint32_t* bitmapBuffer,
+    const Vector4& color,
+    const unsigned int x,
+    const unsigned int y)
+{
+  // Always RGBA image for text with styles
+  uint32_t pixel = *( bitmapBuffer + y * glyphData.width + x );
+  uint8_t* pixelBuffer = reinterpret_cast<uint8_t*>( &pixel );
+
+  // Write the color to the pixel buffer
+  uint8_t colorAlpha = static_cast< uint8_t >( color.a * 255.f );
+  *( pixelBuffer + 3u ) = colorAlpha;
+  *( pixelBuffer + 2u ) = static_cast< uint8_t >( color.b * colorAlpha );
+  *( pixelBuffer + 1u ) = static_cast< uint8_t >( color.g * colorAlpha );
+  *( pixelBuffer      ) = static_cast< uint8_t >( color.r * colorAlpha );
+
+  *( bitmapBuffer + y * glyphData.width + x ) = pixel;
+}
+
+/// Draws the specified underline color to the buffer
+void DrawUnderline(
+    const Vector4& underlineColor,
+    const unsigned int bufferWidth,
+    const unsigned int bufferHeight,
+    GlyphData& glyphData,
+    const float baseline,
+    const float currentUnderlinePosition,
+    const float maxUnderlineThickness,
+    const float lineExtentLeft,
+    const float lineExtentRight)
+{
+  int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
+  uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
+
+  for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
+  {
+    if( y > bufferHeight - 1 )
+    {
+      // Do not write out of bounds.
+      break;
+    }
+
+    for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
+    {
+      if( x > bufferWidth - 1 )
+      {
+        // Do not write out of bounds.
+        break;
+      }
+
+      WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
+    }
+  }
+}
+
+/// Draws the background color to the buffer
+void DrawBackgroundColor(
+    Vector4 backgroundColor,
+    const unsigned int bufferWidth,
+    const unsigned int bufferHeight,
+    GlyphData& glyphData,
+    const float baseline,
+    const LineRun& line,
+    const float lineExtentLeft,
+    const float lineExtentRight)
+{
+  uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
+
+  for( int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++ )
+  {
+    if( ( y < 0 ) || ( y > static_cast<int>(bufferHeight - 1) ) )
+    {
+      // Do not write out of bounds.
+      continue;
+    }
+
+    for( int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
+    {
+      if( ( x < 0 ) || ( x > static_cast<int>(bufferWidth - 1) ) )
+      {
+        // Do not write out of bounds.
+        continue;
+      }
+
+      WriteColorToPixelBuffer(glyphData, bitmapBuffer, backgroundColor, x, y);
+    }
+  }
+}
+
 } // namespace
 
 TypesetterPtr Typesetter::New( const ModelInterface* const model )
@@ -266,7 +433,7 @@ ViewModel* Typesetter::GetViewModel()
   return mModel;
 }
 
-PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
+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.
 
@@ -276,6 +443,30 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
   // Retrieves the layout size.
   const Size& layoutSize = mModel->GetLayoutSize();
 
+  const int outlineWidth = static_cast<int>( mModel->GetOutlineWidth() );
+
+  // Set the offset for the horizontal alignment according to the text direction and outline width.
+  int penX = 0;
+
+  switch( mModel->GetHorizontalAlignment() )
+  {
+    case HorizontalAlignment::BEGIN:
+    {
+      // No offset to add.
+      break;
+    }
+    case HorizontalAlignment::CENTER:
+    {
+      penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth : outlineWidth;
+      break;
+    }
+    case HorizontalAlignment::END:
+    {
+      penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2 : outlineWidth * 2;
+      break;
+    }
+  }
+
   // Set the offset for the vertical alignment.
   int penY = 0u;
 
@@ -289,6 +480,7 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
     case VerticalAlignment::CENTER:
     {
       penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
+      penY = penY < 0.f ? 0.f : penY;
       break;
     }
     case VerticalAlignment::BOTTOM:
@@ -298,6 +490,29 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
     }
   }
 
+  // Calculate vertical line alignment
+  switch( mModel->GetVerticalLineAlignment() )
+  {
+    case DevelText::VerticalLineAlignment::TOP:
+    {
+      break;
+    }
+    case DevelText::VerticalLineAlignment::MIDDLE:
+    {
+      const auto& line = *mModel->GetLines();
+      penY -= line.descender;
+      penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
+      break;
+    }
+    case DevelText::VerticalLineAlignment::BOTTOM:
+    {
+      const auto& line = *mModel->GetLines();
+      const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
+      penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
+      break;
+    }
+  }
+
   // Generate the image buffers of the text for each different style first,
   // then combine all of them together as one final image buffer. We try to
   // do all of these in CPU only, so that once the final texture is generated,
@@ -316,7 +531,7 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
   if( RENDER_MASK == behaviour )
   {
     // Generate the image buffer as an alpha mask for color glyphs.
-    imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
+    imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
   }
   else if( RENDER_NO_TEXT == behaviour )
   {
@@ -327,18 +542,18 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
   else
   {
     // Generate the image buffer for the text with no style.
-    imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
+    imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
   }
 
   if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
   {
 
     // Generate the outline if enabled
-    const float outlineWidth = mModel->GetOutlineWidth();
-    if ( outlineWidth > Math::MACHINE_EPSILON_1 )
+    const uint16_t outlineWidth = mModel->GetOutlineWidth();
+    if ( outlineWidth != 0u )
     {
       // Create the image buffer for outline
-      Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
+      Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
 
       // Combine the two buffers
       imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
@@ -351,7 +566,7 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
     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, penY, 0u, numberOfGlyphs - 1 );
+      Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
 
       // Check whether it will be a soft shadow
       const float& blurRadius = mModel->GetShadowBlurRadius();
@@ -370,11 +585,21 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
     if ( underlineEnabled )
     {
       // Create the image buffer for underline
-      Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
+      Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
 
       // Combine the two buffers
       imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
     }
+
+    // Generate the background if enabled
+    const bool backgroundEnabled = mModel->IsBackgroundEnabled();
+    if ( backgroundEnabled )
+    {
+      Devel::PixelBuffer backgroundImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
+
+      // Combine the two buffers
+      imageBuffer = CombineImageBuffer( imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight );
+    }
   }
 
   // Create the final PixelData for the combined image buffer
@@ -383,7 +608,7 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo
   return pixelData;
 }
 
-Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex )
+Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex )
 {
   // Retrieve lines, glyphs, positions and colors from the view model.
   const Length modelNumberOfLines = mModel->GetNumberOfLines();
@@ -427,12 +652,19 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth
 
     // 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 );
+    }
+
     // Retrieves the glyph's outline width
-    float outlineWidth = mModel->GetOutlineWidth();
+    float outlineWidth = static_cast<float>( mModel->GetOutlineWidth() );
 
     if( style == Typesetter::STYLE_OUTLINE )
     {
@@ -504,45 +736,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth
       if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
       {
         // We need to fetch fresh font underline metrics
-        FontMetrics fontMetrics;
-        fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
-        currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
-        const float descender = ceil( fabsf( fontMetrics.descender ) );
-
-        if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
-        {
-          currentUnderlineThickness = fontMetrics.underlineThickness;
-
-          // Ensure underline will be at least a pixel high
-          if ( currentUnderlineThickness < 1.0f )
-          {
-            currentUnderlineThickness = 1.0f;
-          }
-          else
-          {
-            currentUnderlineThickness = ceil( currentUnderlineThickness );
-          }
-        }
-
-        // The underline thickness should be the max underline thickness of all glyphs of the line.
-        if ( currentUnderlineThickness > maxUnderlineThickness )
-        {
-          maxUnderlineThickness = currentUnderlineThickness;
-        }
-
-        // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
-        if( currentUnderlinePosition > descender )
-        {
-          currentUnderlinePosition = descender;
-        }
-
-        if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
-        {
-          // Move offset down by one ( EFL behavior )
-          currentUnderlinePosition = 1.0f;
-        }
-
-        lastUnderlinedFontId = glyphInfo->fontId;
+        FetchFontUnderlineMetrics(fontClient, glyphInfo, currentUnderlinePosition, underlineHeight, currentUnderlineThickness, maxUnderlineThickness, lastUnderlinedFontId);
       } // underline
 
       // Retrieves the glyph's position.
@@ -564,22 +758,27 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth
       }
 
       // Retrieves the glyph's color.
-      const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
+      const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndexBuffer + glyphIndex );
 
-      const Vector4* color;
+      Vector4 color;
       if ( style == Typesetter::STYLE_SHADOW )
       {
-        color = &( mModel->GetShadowColor() );
+        color = mModel->GetShadowColor();
       }
       else if ( style == Typesetter::STYLE_OUTLINE )
       {
-        color = &( mModel->GetOutlineColor() );
+        color = mModel->GetOutlineColor();
       }
       else
       {
-        color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
+        color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + ( colorIndex - 1u ) );
       }
 
+      // Premultiply alpha
+      color.r *= color.a;
+      color.g *= color.a;
+      color.b *= color.a;
+
       // Retrieves the glyph's bitmap.
       glyphData.glyphBitmap.buffer = NULL;
       glyphData.glyphBitmap.width = glyphInfo->width;   // Desired width and height.
@@ -591,19 +790,40 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth
         outlineWidth = 0.0f;
       }
 
-      fontClient.CreateBitmap( glyphInfo->fontId,
-                               glyphInfo->index,
-                               glyphData.glyphBitmap,
-                               outlineWidth );
+      if( style != Typesetter::STYLE_UNDERLINE )
+      {
+        fontClient.CreateBitmap( glyphInfo->fontId,
+                                 glyphInfo->index,
+                                 glyphInfo->isItalicRequired,
+                                 glyphInfo->isBoldRequired,
+                                 glyphData.glyphBitmap,
+                                 static_cast<int>( outlineWidth ) );
+      }
 
       // Sets the glyph's bitmap into the bitmap of the whole text.
       if( NULL != glyphData.glyphBitmap.buffer )
       {
+        if ( style == Typesetter::STYLE_OUTLINE )
+        {
+          // Set the position offset for the current glyph
+          glyphData.horizontalOffset -= glyphData.glyphBitmap.outlineOffsetX;
+          glyphData.verticalOffset -= glyphData.glyphBitmap.outlineOffsetY;
+        }
+
+        // Set the buffer of the glyph's bitmap into the final bitmap's buffer
         TypesetGlyph( glyphData,
                       position,
-                      color,
+                      &color,
                       style,
                       pixelFormat);
+
+        if ( style == Typesetter::STYLE_OUTLINE )
+        {
+          // Reset the position offset for the next glyph
+          glyphData.horizontalOffset += glyphData.glyphBitmap.outlineOffsetX;
+          glyphData.verticalOffset += glyphData.glyphBitmap.outlineOffsetY;
+        }
+
         // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
         delete []glyphData.glyphBitmap.buffer;
         glyphData.glyphBitmap.buffer = NULL;
@@ -613,38 +833,13 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth
     // Draw the underline from the leftmost glyph to the rightmost glyph
     if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
     {
-      int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
-
-      for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
-      {
-        if( y > bufferHeight - 1 )
-        {
-          // Do not write out of bounds.
-          break;
-        }
-
-        for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
-        {
-          if( x > bufferWidth - 1 )
-          {
-            // Do not write out of bounds.
-            break;
-          }
-
-          // Always RGBA image for text with styles
-          uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
-          uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
-          uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
-
-          // Write the underline color to the pixel buffer
-          *( underlinePixelBuffer ) = static_cast<uint8_t>( underlineColor.r * 255.f );
-          *( underlinePixelBuffer + 1u ) = static_cast<uint8_t>( underlineColor.g * 255.f );
-          *( underlinePixelBuffer + 2u ) = static_cast<uint8_t>( underlineColor.b * 255.f );
-          *( underlinePixelBuffer + 3u ) = static_cast<uint8_t>( underlineColor.a * 255.f );
+      DrawUnderline(underlineColor, bufferWidth, bufferHeight, glyphData, baseline, currentUnderlinePosition, maxUnderlineThickness, lineExtentLeft, lineExtentRight);
+    }
 
-          *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
-        }
-      }
+    // Draw the background color from the leftmost glyph to the rightmost glyph
+    if ( style == Typesetter::STYLE_BACKGROUND )
+    {
+      DrawBackgroundColor(mModel->GetBackgroundColor(), bufferWidth, bufferHeight, glyphData, baseline, line, lineExtentLeft, lineExtentRight);
     }
 
     // Increases the vertical offset with the line's descender.
@@ -693,15 +888,14 @@ Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBu
     // Otherwise, copy pixel from topBuffer to combinedBuffer.
 
     unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
-    unsigned int alphaBuffer2 = bottomBuffer[pixelIndex*4+3];
 
-    if ( alphaBuffer1 != 255 || alphaBuffer2 != 255 )
+    if ( alphaBuffer1 != 255 )
     {
       // At least one pixel is not fully opaque
       // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
-      combinedBuffer[pixelIndex*4] = ( topBuffer[pixelIndex*4] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
-      combinedBuffer[pixelIndex*4+1] = ( topBuffer[pixelIndex*4+1] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4+1] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
-      combinedBuffer[pixelIndex*4+2] = ( topBuffer[pixelIndex*4+2] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4+2] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
+      combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4] + ( bottomBuffer[pixelIndex*4] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
+      combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1] + ( bottomBuffer[pixelIndex*4+1] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
+      combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2] + ( bottomBuffer[pixelIndex*4+2] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
       combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
     }
     else