X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Frendering%2Fatlas%2Ftext-atlas-renderer.cpp;h=6fe86a453e1b3ceeb702b1e8a527e8011f0f5920;hp=f2e5596bb537315ecda4b99cdf386fe2514f8c7d;hb=f483e6aecc52fc644e0be9b41874b1b8daf493ba;hpb=479693b4fd773eb8888ae401d02b49188293e231 diff --git a/dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.cpp b/dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.cpp index f2e5596..6fe86a4 100644 --- a/dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.cpp +++ b/dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2016 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,10 +19,11 @@ #include // EXTERNAL INCLUDES -#include -#include -#include +#include +#include +#include #include +#include // INTERNAL INCLUDES #include @@ -41,6 +42,50 @@ namespace Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_RENDERING"); #endif +#define MAKE_SHADER(A)#A + +const char* VERTEX_SHADER = MAKE_SHADER( +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +attribute mediump vec4 aColor; +uniform mediump vec2 uOffset; +uniform mediump mat4 uMvpMatrix; +varying mediump vec2 vTexCoord; +varying mediump vec4 vColor; + +void main() +{ + mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 ); + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; + vColor = aColor; +} +); + +const char* FRAGMENT_SHADER_L8 = MAKE_SHADER( +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; +varying mediump vec4 vColor; + +void main() +{ + mediump vec4 color = texture2D( sTexture, vTexCoord ); + gl_FragColor = vec4( vColor.rgb * uColor.rgb, vColor.a * uColor.a * color.r ); +} +); + +const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER( +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; +} +); + const float ZERO( 0.0f ); const float HALF( 0.5f ); const float ONE( 1.0f ); @@ -168,7 +213,8 @@ struct AtlasRenderer::Impl const Vector4& defaultColor, const Vector4* const colorsBuffer, const ColorIndex* const colorIndicesBuffer, - int depth ) + int depth, + float minLineOffset ) { AtlasManager::AtlasSlot slot; std::vector< MeshRecord > meshContainer; @@ -176,7 +222,6 @@ struct AtlasRenderer::Impl TextCacheEntry textCacheEntry; mDepth = depth; - const Vector2& actorSize( view.GetControlSize() ); const Vector2& textSize( view.GetLayoutSize() ); const Vector2 halfTextSize( textSize * 0.5f ); const Vector2& shadowOffset( view.GetShadowOffset() ); @@ -215,11 +260,11 @@ struct AtlasRenderer::Impl Vector< TextCacheEntry > newTextCache; const GlyphInfo* const glyphsBuffer = glyphs.Begin(); const Vector2* const positionsBuffer = positions.Begin(); + const Vector2 lineOffsetPosition( minLineOffset, 0.f ); for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i ) { const GlyphInfo& glyph = *( glyphsBuffer + i ); - const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( i, underlineRuns ); thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph; @@ -289,7 +334,28 @@ struct AtlasRenderer::Impl } // Create a new image for the glyph - BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index ); + PixelData bitmap; + + // Whether the current glyph is a color one. + const bool isColorGlyph = mFontClient.IsColorGlyph( glyph.fontId, glyph.index ); + + // Retrieve the emoji's bitmap. + TextAbstraction::FontClient::GlyphBufferData glyphBufferData; + glyphBufferData.width = isColorGlyph ? glyph.width : 0; // Desired width and height. + glyphBufferData.height = isColorGlyph ? glyph.height : 0; + + mFontClient.CreateBitmap( glyph.fontId, + glyph.index, + glyphBufferData ); + + // Create the pixel data. + bitmap = PixelData::New( glyphBufferData.buffer, + glyph.width * glyph.height * GetBytesPerPixel( glyphBufferData.format ), + glyph.width, + glyph.height, + glyphBufferData.format, + PixelData::DELETE_ARRAY ); + if( bitmap ) { MaxBlockSize& blockSize = mBlockSizes[currentBlockSize]; @@ -326,7 +392,7 @@ struct AtlasRenderer::Impl } // Move the origin (0,0) of the mesh to the center of the actor - const Vector2 position = *( positionsBuffer + i ) - halfTextSize; + const Vector2 position = *( positionsBuffer + i ) - halfTextSize - lineOffsetPosition; // Generate mesh data for this quad, plugging in our supplied position AtlasManager::Mesh2D newMesh; @@ -338,21 +404,6 @@ struct AtlasRenderer::Impl AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin(); - // Adjust the vertices if the fixed-size font should be down-scaled - if( glyph.scaleFactor > 0 ) - { - for( unsigned int index = 0u, size = newMesh.mVertices.Count(); - index < size; - ++index ) - { - AtlasManager::Vertex2D& vertex = *( verticesBuffer + index ); - - // Set the position of the vertex. - vertex.mPosition.x = position.x + ( ( vertex.mPosition.x - position.x ) * glyph.scaleFactor ); - vertex.mPosition.y = position.y + ( ( vertex.mPosition.y - position.y ) * glyph.scaleFactor ); - } - } - // Get the color of the character. const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndicesBuffer + i ); const Vector4& color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + colorIndex - 1u ); @@ -393,16 +444,30 @@ struct AtlasRenderer::Impl // For each MeshData object, create a mesh actor and add to the renderable actor if( !meshContainer.empty() ) { + if( !mActor ) + { + // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues. + mActor = Actor::New(); + mActor.SetParentOrigin( ParentOrigin::TOP_LEFT ); + mActor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); + mActor.SetSize( textSize ); + mActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); + } + for( std::vector< MeshRecord >::iterator it = meshContainer.begin(), endIt = meshContainer.end(); it != endIt; ++it ) { MeshRecord& meshRecord = *it; - Actor actor = CreateMeshActor( meshRecord, textSize ); + Actor actor = CreateMeshActor( meshRecord, textSize, STYLE_NORMAL ); + + // Whether the actor has renderers. + const bool hasRenderer = actor.GetRendererCount() > 0u; // Create an effect if necessary - if( style == STYLE_DROP_SHADOW ) + if( hasRenderer && + ( style == STYLE_DROP_SHADOW ) ) { // Change the color of the vertices. for( Vector::Iterator vIt = meshRecord.mMesh.mVertices.Begin(), @@ -415,39 +480,22 @@ struct AtlasRenderer::Impl vertex.mColor = shadowColor; } - // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues. - Actor containerActor = Actor::New(); - containerActor.SetParentOrigin( ParentOrigin::CENTER ); - containerActor.SetSize( actorSize ); - - Actor shadowActor = CreateMeshActor( meshRecord, textSize ); + Actor shadowActor = CreateMeshActor( meshRecord, textSize, STYLE_DROP_SHADOW ); #if defined(DEBUG_ENABLED) shadowActor.SetName( "Text Shadow renderable actor" ); #endif // Offset shadow in x and y shadowActor.RegisterProperty("uOffset", shadowOffset ); - if( actor.GetRendererCount() ) - { - Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) ); - int depthIndex = renderer.GetProperty(Dali::Renderer::Property::DEPTH_INDEX); - renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 ); - containerActor.Add( shadowActor ); - containerActor.Add( actor ); -#if defined(DEBUG_ENABLED) - containerActor.SetName("TextContainer"); -#endif - actor = containerActor; - } + Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) ); + int depthIndex = renderer.GetProperty(Dali::Renderer::Property::DEPTH_INDEX); + renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 ); + mActor.Add( shadowActor ); } - if( mActor ) + if( hasRenderer ) { mActor.Add( actor ); } - else - { - mActor = actor; - } } } #if defined(DEBUG_ENABLED) @@ -483,7 +531,7 @@ struct AtlasRenderer::Impl mTextCache.Resize( 0 ); } - Actor CreateMeshActor( const MeshRecord& meshRecord, const Vector2& actorSize ) + Actor CreateMeshActor( const MeshRecord& meshRecord, const Vector2& actorSize, Style style ) { PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat ); quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ), meshRecord.mMesh.mVertices.Size() ); @@ -493,22 +541,45 @@ struct AtlasRenderer::Impl quadGeometry.SetIndexBuffer( &meshRecord.mMesh.mIndices[0], meshRecord.mMesh.mIndices.Size() ); TextureSet textureSet( mGlyphManager.GetTextures( meshRecord.mAtlasId ) ); - Shader shader( mGlyphManager.GetShader( meshRecord.mAtlasId ) ); + + // Choose the shader to use. + const bool isColorShader = ( STYLE_DROP_SHADOW != style ) && ( Pixel::BGRA8888 == mGlyphManager.GetPixelFormat( meshRecord.mAtlasId ) ); + Shader shader; + if( isColorShader ) + { + // The glyph is an emoji and is not a shadow. + if( !mShaderRgba ) + { + mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA ); + } + shader = mShaderRgba; + } + else + { + // The glyph is text or a shadow. + if( !mShaderL8 ) + { + mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 ); + } + shader = mShaderL8; + } + Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, shader ); renderer.SetTextures( textureSet ); renderer.SetProperty( Dali::Renderer::Property::BLEND_MODE, BlendMode::ON ); renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth ); + Actor actor = Actor::New(); #if defined(DEBUG_ENABLED) actor.SetName( "Text renderable actor" ); #endif actor.AddRenderer( renderer ); - // Keep all of the origins aligned actor.SetParentOrigin( ParentOrigin::TOP_LEFT ); actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); actor.SetSize( actorSize ); actor.RegisterProperty("uOffset", Vector2::ZERO ); + actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); return actor; } @@ -726,11 +797,13 @@ struct AtlasRenderer::Impl Actor mActor; ///< The actor parent which renders the text AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching - TextAbstraction::FontClient mFontClient; ///> The font client used to supply glyph information - std::vector< MaxBlockSize > mBlockSizes; ///> Maximum size needed to contain a glyph in a block within a new atlas - Vector< TextCacheEntry > mTextCache; ///> Caches data from previous render - Property::Map mQuadVertexFormat; ///> Describes the vertex format for text - int mDepth; ///> DepthIndex passed by control when connect to stage + TextAbstraction::FontClient mFontClient; ///< The font client used to supply glyph information + Shader mShaderL8; ///< The shader for glyphs and emoji's shadows. + Shader mShaderRgba; ///< The shader for emojis. + std::vector< MaxBlockSize > mBlockSizes; ///< Maximum size needed to contain a glyph in a block within a new atlas + Vector< TextCacheEntry > mTextCache; ///< Caches data from previous render + Property::Map mQuadVertexFormat; ///< Describes the vertex format for text + int mDepth; ///< DepthIndex passed by control when connect to stage }; Text::RendererPtr AtlasRenderer::New() @@ -740,7 +813,9 @@ Text::RendererPtr AtlasRenderer::New() return Text::RendererPtr( new AtlasRenderer() ); } -Actor AtlasRenderer::Render( Text::ViewInterface& view, int depth ) +Actor AtlasRenderer::Render( Text::ViewInterface& view, + float& alignmentOffset, + int depth ) { DALI_LOG_INFO( gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n" ); @@ -758,6 +833,7 @@ Actor AtlasRenderer::Render( Text::ViewInterface& view, int depth ) numberOfGlyphs = view.GetGlyphs( glyphs.Begin(), positions.Begin(), + alignmentOffset, 0u, numberOfGlyphs ); @@ -774,7 +850,8 @@ Actor AtlasRenderer::Render( Text::ViewInterface& view, int depth ) defaultColor, colorsBuffer, colorIndicesBuffer, - depth ); + depth, + alignmentOffset ); /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */ /* This renderable actor is used to position the text, other "decorations" can rely on there always being an Actor regardless of it is whitespace or regular text. */