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=faac5ef079a0b8bef3695d34255ef260df4a9b07;hp=66bb797058df0432dfce0a5177bfc6e82c6fe966;hb=71f0932efe2c993f7866949767019d170ced9938;hpb=4d305b893731daafafc293b92dc4fa25a896e929 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 66bb797..faac5ef --- 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) 2018 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 { @@ -102,7 +103,7 @@ void TypesetGlyph( GlyphData& data, if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) ) { // Do not write out of bounds. - break; + continue; } const int verticalOffset = yOffsetIndex * data.width; @@ -114,7 +115,7 @@ void TypesetGlyph( GlyphData& data, if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) ) { // Don't write out of bounds. - break; + continue; } uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() ); @@ -138,7 +139,7 @@ void TypesetGlyph( GlyphData& data, } // Update the alpha channel. - if( Typesetter::STYLE_MASK == style ) + 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; @@ -181,6 +182,9 @@ void TypesetGlyph( GlyphData& data, } else { + // Whether the given glyph is a color one. + const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format; + // Initial vertical offset. const int yOffset = data.verticalOffset + position->y; @@ -191,7 +195,7 @@ void TypesetGlyph( GlyphData& data, if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) ) { // Do not write out of bounds. - break; + continue; } const int verticalOffset = yOffsetIndex * data.width; @@ -203,27 +207,29 @@ void TypesetGlyph( GlyphData& data, if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) ) { // Don't write out of bounds. - break; + continue; } uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() ); - // Update the alpha channel. - const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index ); - - // Copy non-transparent pixels only - if ( alpha > 0u ) + if ( !isColorGlyph ) { - // Check alpha of overlapped pixels - uint8_t& currentAlpha = *( bitmapBuffer + verticalOffset + xOffsetIndex ); - uint8_t newAlpha = static_cast( color->a * static_cast( alpha ) ); -// printf("y: %d, x: %d: alpha: %u, currentAlpha: %u, newAlpha: %u, a: %u\n", yOffsetIndex, xOffsetIndex, alpha, currentAlpha, newAlpha, std::max( currentAlpha, newAlpha ) ); - - // 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 ); + // Update the alpha channel. + const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index ); + + // 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 ); + } } } } @@ -261,7 +267,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. @@ -271,28 +277,75 @@ PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bo // Retrieves the layout size. const Size& layoutSize = mModel->GetLayoutSize(); + const float outlineWidth = 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.0f : outlineWidth * 2.0f; + break; + } + } + // Set the offset for the vertical alignment. - int penY = 0u; + int penY = 0; 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, @@ -311,7 +364,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 ) { @@ -322,11 +375,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, 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 ) + { + // 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 @@ -334,7 +399,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, 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(); + + if ( blurRadius > Math::MACHINE_EPSILON_1 ) + { + shadowImageBuffer.ApplyGaussianBlur( blurRadius ); + } // Combine the two buffers imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight ); @@ -345,11 +418,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 @@ -358,7 +441,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(); @@ -379,6 +462,7 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth glyphData.width = bufferWidth; glyphData.height = bufferHeight; glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat ); + glyphData.horizontalOffset = 0; if ( Pixel::RGBA8888 == pixelFormat ) { @@ -401,18 +485,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 = 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; } } @@ -532,6 +636,10 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth { color = &( mModel->GetShadowColor() ); } + else if ( style == Typesetter::STYLE_OUTLINE ) + { + color = &( mModel->GetOutlineColor() ); + } else { color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u ); @@ -541,9 +649,20 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth 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, + glyphData.glyphBitmap, + outlineWidth ); + } + // Sets the glyph's bitmap into the bitmap of the whole text. if( NULL != glyphData.glyphBitmap.buffer ) @@ -566,7 +685,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; @@ -574,7 +693,7 @@ 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; @@ -596,6 +715,43 @@ Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth } } + // 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 + *( 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 ); + + *( bitmapBuffer + y * glyphData.width + x ) = backgroundPixel; + } + } + } + // Increases the vertical offset with the line's descender. glyphData.verticalOffset += static_cast( -line.descender ); }