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=20ea5f72f0b8b51a61773f4c8739868d3f9d5940;hp=faac5ef079a0b8bef3695d34255ef260df4a9b07;hb=8bd6da23805f5ee7c029a0e00d4c9bef93e539c0;hpb=d8a9f274a51e12a7d5690f691234e50fc06aa1fe diff --git a/dali-toolkit/internal/text/rendering/text-typesetter.cpp b/dali-toolkit/internal/text/rendering/text-typesetter.cpp index faac5ef..20ea5f7 100755 --- a/dali-toolkit/internal/text/rendering/text-typesetter.cpp +++ b/dali-toolkit/internal/text/rendering/text-typesetter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 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. @@ -80,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( 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; + 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 ) { @@ -118,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( &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 - { - 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( color->a * static_cast( *( packedColorGlyphBuffer + 3u ) ) ); + 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. @@ -154,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( &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 ) @@ -164,14 +182,18 @@ void TypesetGlyph( GlyphData& data, uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex ); uint8_t* packedCurrentColorBuffer = reinterpret_cast( ¤tColor ); - uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u ); - uint8_t newAlpha = static_cast( color->a * static_cast( 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( 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; @@ -183,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( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex ) { @@ -210,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( color->a * static_cast( 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 ); } } } @@ -277,7 +300,7 @@ PixelData Typesetter::Render( const Vector2& size, Toolkit::DevelText::TextDirec // Retrieves the layout size. const Size& layoutSize = mModel->GetLayoutSize(); - const float outlineWidth = mModel->GetOutlineWidth(); + 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; @@ -296,13 +319,13 @@ PixelData Typesetter::Render( const Vector2& size, Toolkit::DevelText::TextDirec } case HorizontalAlignment::END: { - penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2.0f : outlineWidth * 2.0f; + penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2 : outlineWidth * 2; break; } } // Set the offset for the vertical alignment. - int penY = 0; + int penY = 0u; switch( mModel->GetVerticalAlignment() ) { @@ -382,8 +405,8 @@ PixelData Typesetter::Render( const Vector2& size, Toolkit::DevelText::TextDirec { // 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, penX, penY, 0u, numberOfGlyphs -1 ); @@ -497,7 +520,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth } // Retrieves the glyph's outline width - float outlineWidth = mModel->GetOutlineWidth(); + float outlineWidth = static_cast( mModel->GetOutlineWidth() ); if( style == Typesetter::STYLE_OUTLINE ) { @@ -629,22 +652,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. @@ -659,8 +687,10 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth { fontClient.CreateBitmap( glyphInfo->fontId, glyphInfo->index, + glyphInfo->isItalicRequired, + glyphInfo->isBoldRequired, glyphData.glyphBitmap, - outlineWidth ); + static_cast( outlineWidth ) ); } @@ -669,7 +699,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth { TypesetGlyph( glyphData, position, - color, + &color, style, pixelFormat); // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer @@ -705,10 +735,11 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth 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; } @@ -742,10 +773,11 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth uint8_t* backgroundPixelBuffer = reinterpret_cast( &backgroundPixel ); // Write the background color to the pixel buffer - *( backgroundPixelBuffer ) = static_cast( backgroundColor.r * 255.f ); - *( backgroundPixelBuffer + 1u ) = static_cast( backgroundColor.g * 255.f ); - *( backgroundPixelBuffer + 2u ) = static_cast( backgroundColor.b * 255.f ); - *( backgroundPixelBuffer + 3u ) = static_cast( backgroundColor.a * 255.f ); + 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; } @@ -798,15 +830,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