X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Frendering%2Ftext-typesetter.cpp;h=5b424be6a4a8892a78c1b253a54f3fac006f3cdf;hp=11cde00a2b5affcc38556341e318f55881c2933f;hb=efcb5c8e24a267e30a8faf9af88177fd75ea37cf;hpb=d9e73542cfca2591f40ca9ffeefa1e20e0c2bbd0 diff --git a/dali-toolkit/internal/text/rendering/text-typesetter.cpp b/dali-toolkit/internal/text/rendering/text-typesetter.cpp old mode 100644 new mode 100755 index 11cde00..5b424be --- a/dali-toolkit/internal/text/rendering/text-typesetter.cpp +++ b/dali-toolkit/internal/text/rendering/text-typesetter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 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. @@ -19,12 +19,13 @@ #include // EXTERNAL INCLUDES -#include #include +#include #include // INTERNAL INCLUDES #include +#include namespace Dali { @@ -59,11 +60,13 @@ struct GlyphData * @param[in] position The position of the glyph. * @param[in] color The color of the glyph. * @param[in] style The style of the text. + * @param[in] pixelFormat The format of the pixel in the image that the text is rendered as (i.e. either Pixel::BGRA8888 or Pixel::L8). */ void TypesetGlyph( GlyphData& data, const Vector2* const position, const Vector4* const color, - Typesetter::Style style) + Typesetter::Style style, + Pixel::Format pixelFormat ) { if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) ) { @@ -74,87 +77,183 @@ void TypesetGlyph( GlyphData& data, const int widthMinusOne = static_cast( data.width - 1u ); const int heightMinusOne = static_cast( data.height - 1u ); - // Whether the given glyph is a color one. - const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format; + if ( Pixel::RGBA8888 == pixelFormat ) + { + // Whether the given glyph is a color one. + 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( data.glyphBitmap.buffer ) : NULL; + // Pointer to the color glyph if there is one. + const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast( 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( &packedColor ); - *( packedColorBuffer + 2 ) = static_cast( color->b * 255.f ); - *( packedColorBuffer + 1 ) = static_cast( color->g * 255.f ); - *packedColorBuffer = static_cast( color->r * 255.f ); + // Initial vertical offset. + const int yOffset = data.verticalOffset + position->y; - // 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( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex ) - { - const int yOffsetIndex = yOffset + lineIndex; - if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) ) + // Traverse the pixels of the glyph line per line. + for( int lineIndex = 0, glyphHeight = static_cast( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex ) { - // Do not write out of bounds. - break; - } - - const int verticalOffset = yOffsetIndex * data.width; - const int xOffset = data.horizontalOffset + position->x; - const int glyphBufferOffset = lineIndex * static_cast( data.glyphBitmap.width ); - for( int index = 0, glyphWidth = static_cast( data.glyphBitmap.width ); index < glyphWidth; ++index ) - { - const int xOffsetIndex = xOffset + index; - if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) ) + const int yOffsetIndex = yOffset + lineIndex; + if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) ) { - // Don't write out of bounds. - break; + // Do not write out of bounds. + continue; } - uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() ); - - if( isColorGlyph ) + const int verticalOffset = yOffsetIndex * data.width; + const int xOffset = data.horizontalOffset + position->x; + const int glyphBufferOffset = lineIndex * static_cast( data.glyphBitmap.width ); + for( int index = 0, glyphWidth = static_cast( data.glyphBitmap.width ); index < glyphWidth; ++index ) { - // Retrieves the color from the color glyph. The format is BGRA8888. - uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index ); - uint8_t* packedColorGlyphBuffer = reinterpret_cast( &packedColorGlyph ); - - if( Typesetter::STYLE_SHADOW == style ) - { - // The shadow of color glyph needs to have the shadow color. - *( packedColorGlyphBuffer + 2 ) = static_cast( color->b * 255.f ); - *( packedColorGlyphBuffer + 1 ) = static_cast( color->g * 255.f ); - *packedColorGlyphBuffer = static_cast( color->r * 255.f ); - } - else + const int xOffsetIndex = xOffset + index; + if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) ) { - std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R. + // Don't write out of bounds. + continue; } - // Update the alpha channel. - if( Typesetter::STYLE_MASK == style ) + if( isColorGlyph ) { - // Create an alpha mask for color glyph. - *( packedColorGlyphBuffer + 3u ) = 0u; + // Retrieves the color from the color glyph. + uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index ); + uint8_t* packedColorGlyphBuffer = reinterpret_cast( &packedColorGlyph ); + + // 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 + { + const uint8_t colorAlpha = static_cast( color->a * static_cast( *( 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( color->b * colorAlpha ); + *( packedColorGlyphBuffer + 1u ) = static_cast( color->g * colorAlpha ); + *packedColorGlyphBuffer = static_cast( 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( *( packedColorGlyphBuffer + 2u ) * color->b ); + *( packedColorGlyphBuffer + 1u ) = static_cast( *( packedColorGlyphBuffer + 1u ) * color->g ); + *packedColorGlyphBuffer = static_cast( *packedColorGlyphBuffer * color->r ); + } + } + } + + // Set the color into the final pixel buffer. + *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph; } else { - *( packedColorGlyphBuffer + 3u ) = static_cast( color->a * static_cast( *( packedColorGlyphBuffer + 3u ) ) ); + // 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( &packedColor ); + + // Update the alpha channel. + const uint8_t alpha = *( data.glyphBitmap.buffer + glyphPixelSize * ( glyphBufferOffset + index ) + alphaIndex ); + + // Copy non-transparent pixels only + if ( alpha > 0u ) + { + // Check alpha of overlapped pixels + uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex ); + uint8_t* packedCurrentColorBuffer = reinterpret_cast( ¤tColor ); + + // 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). + uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u ); + currentAlpha = std::max( currentAlpha, alpha ); + + // Color is pre-muliplied with its alpha. + *( packedColorBuffer + 3u ) = static_cast( color->a * currentAlpha ); + *( packedColorBuffer + 2u ) = static_cast( color->b * currentAlpha ); + *( packedColorBuffer + 1u ) = static_cast( color->g * currentAlpha ); + *( packedColorBuffer ) = static_cast( color->r * currentAlpha ); + + // Set the color into the final pixel buffer. + currentColor = packedColor; + } } + } + } + } + else + { + // Whether the given glyph is a color one. + 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; - // Set the color into the final pixel buffer. - *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph; + 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( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex ) + { + const int yOffsetIndex = yOffset + lineIndex; + if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) ) + { + // Do not write out of bounds. + continue; } - else + + const int verticalOffset = yOffsetIndex * data.width; + const int xOffset = data.horizontalOffset + position->x; + const int glyphBufferOffset = lineIndex * static_cast( data.glyphBitmap.width ); + for( int index = 0, glyphWidth = static_cast( data.glyphBitmap.width ); index < glyphWidth; ++index ) { - // Update the alpha channel. - const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index ); - *( packedColorBuffer + 3u ) = static_cast( color->a * static_cast( alpha ) ); + const int xOffsetIndex = xOffset + index; + if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) ) + { + // Don't write out of bounds. + continue; + } - // Set the color into the final pixel buffer. - *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColor; + if ( !isColorGlyph ) + { + // Update the alpha channel. + 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 ); + + // 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). + currentAlpha = std::max( currentAlpha, alpha ); + } + } } } } @@ -191,7 +290,7 @@ ViewModel* Typesetter::GetViewModel() return mModel; } -PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment ) +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. @@ -201,28 +300,75 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo // Retrieves the layout size. const Size& layoutSize = mModel->GetLayoutSize(); + const int outlineWidth = static_cast( 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; switch( mModel->GetVerticalAlignment() ) { - case Layout::VERTICAL_ALIGN_TOP: + case VerticalAlignment::TOP: { // No offset to add. break; } - case Layout::VERTICAL_ALIGN_CENTER: + case VerticalAlignment::CENTER: { penY = static_cast( 0.5f * ( size.height - layoutSize.height ) ); break; } - case Layout::VERTICAL_ALIGN_BOTTOM: + case VerticalAlignment::BOTTOM: { penY = static_cast( size.height - layoutSize.height ); break; } } + // 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(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(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, @@ -241,7 +387,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, penY, 0u, numberOfGlyphs - 1 ); + imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 ); } else if( RENDER_NO_TEXT == behaviour ) { @@ -252,11 +398,23 @@ 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, 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 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, penX, penY, 0u, numberOfGlyphs -1 ); + + // Combine the two buffers + imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight ); + } + // @todo. Support shadow and underline for partial text later on. // Generate the shadow if enabled @@ -264,7 +422,15 @@ 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, 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(); + + if ( blurRadius > Math::MACHINE_EPSILON_1 ) + { + shadowImageBuffer.ApplyGaussianBlur( blurRadius ); + } // Combine the two buffers imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight ); @@ -275,11 +441,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, 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 @@ -288,7 +464,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, 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(); @@ -306,13 +482,21 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth // Create and initialize the pixel buffer. GlyphData glyphData; glyphData.verticalOffset = verticalOffset; - glyphData.width = bufferWidth; glyphData.height = bufferHeight; - const unsigned int bufferSizeInt = bufferWidth * bufferHeight; - const unsigned int bufferSizeChar = 4u * bufferSizeInt; - glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 ); - memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar ); + glyphData.bitmapBuffer = Devel::PixelBuffer::New( 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(); @@ -324,18 +508,38 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth // Sets the horizontal offset of the line. glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast( line.alignmentOffset ); + glyphData.horizontalOffset += horizontalOffset; // Increases the vertical offset with the line's ascender. glyphData.verticalOffset += static_cast( line.ascender ); - if ( style == Typesetter::STYLE_SHADOW ) + // Include line spacing after first line + if( lineIndex > 0u ) + { + glyphData.verticalOffset += static_cast( line.lineSpacing ); + } + + // Retrieves the glyph's outline width + float outlineWidth = static_cast( mModel->GetOutlineWidth() ); + + if( style == Typesetter::STYLE_OUTLINE ) + { + glyphData.horizontalOffset -= outlineWidth; + if( lineIndex == 0u ) + { + // Only need to add the vertical outline offset for the first line + glyphData.verticalOffset -= outlineWidth; + } + } + else if ( style == Typesetter::STYLE_SHADOW ) { const Vector2& shadowOffset = mModel->GetShadowOffset(); - glyphData.horizontalOffset += shadowOffset.x; + glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline + if ( lineIndex == 0u ) { - // Only need to add the vertical shadow offset for once - glyphData.verticalOffset += shadowOffset.y; + // Only need to add the vertical shadow offset for first line + glyphData.verticalOffset += shadowOffset.y - outlineWidth; } } @@ -448,33 +652,72 @@ 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(); } 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. glyphData.glyphBitmap.height = glyphInfo->height; - fontClient.CreateBitmap( glyphInfo->fontId, - glyphInfo->index, - glyphData.glyphBitmap ); + + if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW ) + { + // Don't render outline for other styles + outlineWidth = 0.0f; + } + + if( style != Typesetter::STYLE_UNDERLINE ) + { + fontClient.CreateBitmap( glyphInfo->fontId, + glyphInfo->index, + glyphInfo->isItalicRequired, + glyphInfo->isBoldRequired, + glyphData.glyphBitmap, + static_cast( 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, - style ); + &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; @@ -488,7 +731,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ ) { - if( ( y < 0 ) || ( y > bufferHeight - 1 ) ) + if( y > bufferHeight - 1 ) { // Do not write out of bounds. break; @@ -496,27 +739,67 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ ) { - if( ( x < 0 ) || ( x > bufferWidth - 1 ) ) + 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( &underlinePixel ); // Write the underline color to the pixel buffer - *( underlinePixelBuffer ) = static_cast( underlineColor.r * 255.f ); - *( underlinePixelBuffer + 1u ) = static_cast( underlineColor.g * 255.f ); - *( underlinePixelBuffer + 2u ) = static_cast( underlineColor.b * 255.f ); - *( underlinePixelBuffer + 3u ) = static_cast( underlineColor.a * 255.f ); + uint8_t colorAlpha = static_cast< uint8_t >( underlineColor.a * 255.f ); + *( underlinePixelBuffer + 3u ) = colorAlpha; + *( underlinePixelBuffer + 2u ) = static_cast< uint8_t >( underlineColor.b * colorAlpha ); + *( underlinePixelBuffer + 1u ) = static_cast< uint8_t >( underlineColor.g * colorAlpha ); + *( underlinePixelBuffer ) = static_cast< uint8_t >( underlineColor.r * colorAlpha ); *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel; } } } + // Draw the background color from the leftmost glyph to the rightmost glyph + if ( style == Typesetter::STYLE_BACKGROUND ) + { + Vector4 backgroundColor = mModel->GetBackgroundColor(); + + for( int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++ ) + { + if( ( y < 0 ) || ( y > static_cast(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(bufferWidth - 1) ) ) + { + // Do not write out of bounds. + continue; + } + + // Always RGBA image for text with styles + uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() ); + uint32_t backgroundPixel = *( bitmapBuffer + y * glyphData.width + x ); + uint8_t* backgroundPixelBuffer = reinterpret_cast( &backgroundPixel ); + + // Write the background color to the pixel buffer + uint8_t colorAlpha = static_cast< uint8_t >( backgroundColor.a * 255.f ); + *( backgroundPixelBuffer + 3u ) = colorAlpha; + *( backgroundPixelBuffer + 2u ) = static_cast< uint8_t >( backgroundColor.b * colorAlpha ); + *( backgroundPixelBuffer + 1u ) = static_cast< uint8_t >( backgroundColor.g * colorAlpha ); + *( backgroundPixelBuffer ) = static_cast< uint8_t >( backgroundColor.r * colorAlpha ); + + *( bitmapBuffer + y * glyphData.width + x ) = backgroundPixel; + } + } + } + // Increases the vertical offset with the line's descender. glyphData.verticalOffset += static_cast( -line.descender ); } @@ -549,6 +832,7 @@ Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBu return topPixelBuffer; } + // Always combine two RGBA images const unsigned int bufferSizeInt = bufferWidth * bufferHeight; const unsigned int bufferSizeChar = 4u * bufferSizeInt; @@ -562,15 +846,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