2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.h>
22 #include <dali/public-api/rendering/geometry.h>
23 #include <dali/public-api/rendering/renderer.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/animation/constraints.h>
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/internal/text/glyph-run.h>
31 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
32 #include <dali-toolkit/internal/text/rendering/atlas/atlas-mesh-factory.h>
33 #include <dali-toolkit/internal/text/text-view.h>
36 using namespace Dali::Toolkit;
37 using namespace Dali::Toolkit::Text;
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_RENDERING");
45 #define MAKE_SHADER(A)#A
47 const char* VERTEX_SHADER = MAKE_SHADER(
48 attribute mediump vec2 aPosition;
49 attribute mediump vec2 aTexCoord;
50 attribute mediump vec4 aColor;
51 uniform mediump vec2 uOffset;
52 uniform mediump mat4 uMvpMatrix;
53 varying mediump vec2 vTexCoord;
54 varying mediump vec4 vColor;
58 mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 );
59 gl_Position = uMvpMatrix * position;
60 vTexCoord = aTexCoord;
65 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
66 uniform lowp vec4 uColor;
67 uniform lowp vec4 textColorAnimatable;
68 uniform sampler2D sTexture;
69 varying mediump vec2 vTexCoord;
70 varying mediump vec4 vColor;
74 mediump vec4 color = texture2D( sTexture, vTexCoord );
75 gl_FragColor = vec4( vColor.rgb * uColor.rgb * textColorAnimatable.rgb, uColor.a * vColor.a * textColorAnimatable.a * color.r );
79 const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER(
80 uniform lowp vec4 uColor;
81 uniform lowp vec4 textColorAnimatable;
82 uniform sampler2D sTexture;
83 varying mediump vec2 vTexCoord;
87 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * textColorAnimatable;
91 const float ZERO( 0.0f );
92 const float HALF( 0.5f );
93 const float ONE( 1.0f );
94 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
95 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
96 const uint32_t NO_OUTLINE = 0;
99 struct AtlasRenderer::Impl
115 AtlasManager::Mesh2D mMesh;
119 * brief Struct used to generate the underline mesh.
120 * There is one Extent per line of text.
128 mUnderlinePosition( 0.0f ),
129 mUnderlineThickness( 0.0f ),
130 mMeshRecordIndex( 0u )
137 float mUnderlinePosition;
138 float mUnderlineThickness;
139 uint32_t mMeshRecordIndex;
146 mNeededBlockWidth( 0 ),
147 mNeededBlockHeight( 0 )
152 uint32_t mNeededBlockWidth;
153 uint32_t mNeededBlockHeight;
165 Text::GlyphIndex mIndex;
168 struct TextCacheEntry
179 Text::GlyphIndex mIndex;
180 uint32_t mOutlineWidth;
187 mGlyphManager = AtlasGlyphManager::Get();
188 mFontClient = TextAbstraction::FontClient::Get();
190 mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
191 mQuadVertexFormat[ "aTexCoord" ] = Property::VECTOR2;
192 mQuadVertexFormat[ "aColor" ] = Property::VECTOR4;
195 bool IsGlyphUnderlined( GlyphIndex index,
196 const Vector<GlyphRun>& underlineRuns )
198 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
199 endIt = underlineRuns.End();
203 const GlyphRun& run = *it;
205 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
214 void CacheGlyph( const GlyphInfo& glyph, FontId lastFontId, uint32_t outline, AtlasManager::AtlasSlot& slot )
216 const bool glyphNotCached = !mGlyphManager.IsCached( glyph.fontId, glyph.index, outline, slot ); // Check FontGlyphRecord vector for entry with glyph index and fontId
218 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "AddGlyphs fontID[%u] glyphIndex[%u] [%s]\n", glyph.fontId, glyph.index, (glyphNotCached)?"not cached":"cached" );
222 MaxBlockSize& blockSize = mBlockSizes[0u];
224 if ( lastFontId != glyph.fontId )
227 // Looks through all stored block sizes until finds the one which mataches required glyph font it. Ensures new atlas block size will match existing for same font id.
228 // CalculateBlocksSize() above ensures a block size entry exists.
229 for( std::vector<MaxBlockSize>::const_iterator it = mBlockSizes.begin(),
230 endIt = mBlockSizes.end();
234 const MaxBlockSize& blockSizeEntry = *it;
235 if( blockSizeEntry.mFontId == glyph.fontId )
237 blockSize = mBlockSizes[index];
242 // Create a new image for the glyph
245 // Whether the glyph is an outline.
246 const bool isOutline = 0u != outline;
248 // Whether the current glyph is a color one.
249 const bool isColorGlyph = mFontClient.IsColorGlyph( glyph.fontId, glyph.index );
251 if( !isOutline || ( isOutline && !isColorGlyph) )
253 // Retrieve the emoji's bitmap.
254 TextAbstraction::FontClient::GlyphBufferData glyphBufferData;
255 glyphBufferData.width = isColorGlyph ? glyph.width : 0; // Desired width and height.
256 glyphBufferData.height = isColorGlyph ? glyph.height : 0;
258 mFontClient.CreateBitmap( glyph.fontId,
263 // Create the pixel data.
264 bitmap = PixelData::New( glyphBufferData.buffer,
265 glyphBufferData.width * glyphBufferData.height * GetBytesPerPixel( glyphBufferData.format ),
266 glyphBufferData.width,
267 glyphBufferData.height,
268 glyphBufferData.format,
269 PixelData::DELETE_ARRAY );
273 // Ensure that the next image will fit into the current block size
274 if( bitmap.GetWidth() > blockSize.mNeededBlockWidth )
276 blockSize.mNeededBlockWidth = bitmap.GetWidth();
279 if( bitmap.GetHeight() > blockSize.mNeededBlockHeight )
281 blockSize.mNeededBlockHeight = bitmap.GetHeight();
284 // If CheckAtlas in AtlasManager::Add can't fit the bitmap in the current atlas it will create a new atlas
286 // Setting the block size and size of new atlas does not mean a new one will be created. An existing atlas may still surffice.
287 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
288 DEFAULT_ATLAS_HEIGHT,
289 blockSize.mNeededBlockWidth,
290 blockSize.mNeededBlockHeight );
292 // Locate a new slot for our glyph
293 mGlyphManager.Add( glyph, outline, bitmap, slot ); // slot will be 0 is glyph not added
299 // We have 2+ copies of the same glyph
300 mGlyphManager.AdjustReferenceCount( glyph.fontId, glyph.index, outline, 1 ); //increment
304 void GenerateMesh( const GlyphInfo& glyph,
305 const Vector2& position,
306 const Vector4& color,
308 AtlasManager::AtlasSlot& slot,
310 float currentUnderlinePosition,
311 float currentUnderlineThickness,
312 std::vector<MeshRecord>& meshContainer,
313 Vector<TextCacheEntry>& newTextCache,
314 Vector<Extent>& extents )
316 // Generate mesh data for this quad, plugging in our supplied position
317 AtlasManager::Mesh2D newMesh;
318 mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
320 TextCacheEntry textCacheEntry;
321 textCacheEntry.mFontId = glyph.fontId;
322 textCacheEntry.mImageId = slot.mImageId;
323 textCacheEntry.mIndex = glyph.index;
324 textCacheEntry.mOutlineWidth = outline;
326 newTextCache.PushBack( textCacheEntry );
328 AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
330 for( unsigned int index = 0u, size = newMesh.mVertices.Count();
334 AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
336 // Set the color of the vertex.
337 vertex.mColor = color;
340 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
341 StitchTextMesh( meshContainer,
344 position.y + glyph.yBearing,
346 currentUnderlinePosition,
347 currentUnderlineThickness,
351 void CreateActors( const std::vector<MeshRecord>& meshContainer,
352 const Size& textSize,
353 const Vector4& color,
354 const Vector4& shadowColor,
355 const Vector2& shadowOffset,
357 Property::Index animatablePropertyIndex,
362 // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
363 mActor = Actor::New();
364 mActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
365 mActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
366 mActor.SetSize( textSize );
367 mActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
370 for( std::vector< MeshRecord >::const_iterator it = meshContainer.begin(),
371 endIt = meshContainer.end();
374 const MeshRecord& meshRecord = *it;
376 Actor actor = CreateMeshActor( textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_NORMAL );
378 // Whether the actor has renderers.
379 const bool hasRenderer = actor.GetRendererCount() > 0u;
381 // Create an effect if necessary
385 // Change the color of the vertices.
386 for( Vector<AtlasManager::Vertex2D>::Iterator vIt = meshRecord.mMesh.mVertices.Begin(),
387 vEndIt = meshRecord.mMesh.mVertices.End();
391 AtlasManager::Vertex2D& vertex = *vIt;
393 vertex.mColor = shadowColor;
396 Actor shadowActor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_DROP_SHADOW );
397 #if defined(DEBUG_ENABLED)
398 shadowActor.SetName( "Text Shadow renderable actor" );
400 // Offset shadow in x and y
401 shadowActor.RegisterProperty("uOffset", shadowOffset );
402 Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) );
403 int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
404 renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 );
405 mActor.Add( shadowActor );
415 void AddGlyphs( Text::ViewInterface& view,
417 Property::Index animatablePropertyIndex,
418 const Vector<Vector2>& positions,
419 const Vector<GlyphInfo>& glyphs,
420 const Vector4& defaultColor,
421 const Vector4* const colorsBuffer,
422 const ColorIndex* const colorIndicesBuffer,
424 float minLineOffset )
426 AtlasManager::AtlasSlot slot;
430 AtlasManager::AtlasSlot slotOutline;
431 slotOutline.mImageId = 0u;
432 slotOutline.mAtlasId = 0u;
434 std::vector< MeshRecord > meshContainer;
435 std::vector< MeshRecord > meshContainerOutline;
436 Vector< Extent > extents;
439 const Vector2& textSize( view.GetLayoutSize() );
440 const Vector2 halfTextSize( textSize * 0.5f );
441 const Vector2& shadowOffset( view.GetShadowOffset() );
442 const Vector4& shadowColor( view.GetShadowColor() );
443 const bool underlineEnabled = view.IsUnderlineEnabled();
444 const Vector4& underlineColor( view.GetUnderlineColor() );
445 const float underlineHeight = view.GetUnderlineHeight();
446 const unsigned int outlineWidth = view.GetOutlineWidth();
447 const Vector4& outlineColor( view.GetOutlineColor() );
448 const bool isOutline = 0u != outlineWidth;
450 const bool useDefaultColor = ( NULL == colorsBuffer );
452 // Get the underline runs.
453 const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
454 Vector<GlyphRun> underlineRuns;
455 underlineRuns.Resize( numberOfUnderlineRuns );
456 view.GetUnderlineRuns( underlineRuns.Begin(),
458 numberOfUnderlineRuns );
460 bool thereAreUnderlinedGlyphs = false;
462 float currentUnderlinePosition = ZERO;
463 float currentUnderlineThickness = underlineHeight;
464 FontId lastFontId = 0;
465 FontId lastUnderlinedFontId = 0;
466 Style style = STYLE_NORMAL;
468 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
470 style = STYLE_DROP_SHADOW;
473 CalculateBlocksSize( glyphs );
475 // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
476 Vector< TextCacheEntry > newTextCache;
477 const GlyphInfo* const glyphsBuffer = glyphs.Begin();
478 const Vector2* const positionsBuffer = positions.Begin();
479 const Vector2 lineOffsetPosition( minLineOffset, 0.f );
481 for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
483 const GlyphInfo& glyph = *( glyphsBuffer + i );
484 const bool isGlyphUnderlined = underlineEnabled || IsGlyphUnderlined( i, underlineRuns );
485 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || isGlyphUnderlined;
487 // No operation for white space
488 if( glyph.width && glyph.height )
490 // Are we still using the same fontId as previous
491 if( isGlyphUnderlined && ( glyph.fontId != lastUnderlinedFontId ) )
493 // We need to fetch fresh font underline metrics
494 FontMetrics fontMetrics;
495 mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
496 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
497 const float descender = ceil( fabsf( fontMetrics.descender ) );
499 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
501 currentUnderlineThickness = fontMetrics.underlineThickness;
503 // Ensure underline will be at least a pixel high
504 if ( currentUnderlineThickness < ONE )
506 currentUnderlineThickness = ONE;
510 currentUnderlineThickness = ceil( currentUnderlineThickness );
514 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
515 if( currentUnderlinePosition > descender )
517 currentUnderlinePosition = descender;
520 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
522 // Move offset down by one ( EFL behavior )
523 currentUnderlinePosition = ONE;
526 lastUnderlinedFontId = glyph.fontId;
529 // Retrieves and caches the glyph's bitmap.
530 CacheGlyph( glyph, lastFontId, NO_OUTLINE, slot );
532 // Retrieves and caches the outline glyph's bitmap.
535 CacheGlyph( glyph, lastFontId, outlineWidth, slotOutline );
538 // Move the origin (0,0) of the mesh to the center of the actor
539 const Vector2 position = *( positionsBuffer + i ) - halfTextSize - lineOffsetPosition;
541 if ( 0u != slot.mImageId ) // invalid slot id, glyph has failed to be added to atlas
543 Vector2 positionPlusOutlineOffset = position;
546 // Add an offset to the text.
547 const float outlineWidthOffset = static_cast<float>( outlineWidth );
548 positionPlusOutlineOffset += Vector2( outlineWidthOffset, outlineWidthOffset );
551 // Get the color of the character.
552 const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndicesBuffer + i );
553 const Vector4& color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + colorIndex - 1u );
556 positionPlusOutlineOffset,
561 currentUnderlinePosition,
562 currentUnderlineThickness,
567 lastFontId = glyph.fontId; // Prevents searching for existing blocksizes when string of the same fontId.
570 if( isOutline && ( 0u != slotOutline.mImageId ) ) // invalid slot id, glyph has failed to be added to atlas
578 currentUnderlinePosition,
579 currentUnderlineThickness,
580 meshContainerOutline,
587 // Now remove references for the old text
589 mTextCache.Swap( newTextCache );
591 if( thereAreUnderlinedGlyphs )
593 // Check to see if any of the text needs an underline
594 GenerateUnderlines( meshContainer, extents, underlineColor );
597 // For each MeshData object, create a mesh actor and add to the renderable actor
598 bool isShadowDrawn = false;
599 if( !meshContainerOutline.empty() )
601 const bool drawShadow = STYLE_DROP_SHADOW == style;
602 CreateActors( meshContainerOutline,
608 animatablePropertyIndex,
611 isShadowDrawn = drawShadow;
614 // For each MeshData object, create a mesh actor and add to the renderable actor
615 if( !meshContainer.empty() )
617 const bool drawShadow = !isShadowDrawn && ( STYLE_DROP_SHADOW == style );
618 CreateActors( meshContainer,
624 animatablePropertyIndex,
628 #if defined(DEBUG_ENABLED)
629 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
630 DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
632 metrics.mAtlasMetrics.mAtlasCount,
633 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
635 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str() );
637 for( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
639 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
640 i + 1, i > 8 ? "" : " ",
641 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8 " : "BGRA",
642 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
643 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
644 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
645 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
646 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
647 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
654 for( Vector< TextCacheEntry >::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter )
656 mGlyphManager.AdjustReferenceCount( oldTextIter->mFontId, oldTextIter->mIndex, oldTextIter->mOutlineWidth, -1/*decrement*/ );
658 mTextCache.Resize( 0 );
661 Actor CreateMeshActor( Actor textControl, Property::Index animatablePropertyIndex, const Vector4& defaultColor, const MeshRecord& meshRecord,
662 const Vector2& actorSize, Style style )
664 PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat );
665 quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ), meshRecord.mMesh.mVertices.Size() );
667 Geometry quadGeometry = Geometry::New();
668 quadGeometry.AddVertexBuffer( quadVertices );
669 quadGeometry.SetIndexBuffer( &meshRecord.mMesh.mIndices[0], meshRecord.mMesh.mIndices.Size() );
671 TextureSet textureSet( mGlyphManager.GetTextures( meshRecord.mAtlasId ) );
673 // Choose the shader to use.
674 const bool isColorShader = ( STYLE_DROP_SHADOW != style ) && ( Pixel::BGRA8888 == mGlyphManager.GetPixelFormat( meshRecord.mAtlasId ) );
678 // The glyph is an emoji and is not a shadow.
681 mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA );
683 shader = mShaderRgba;
687 // The glyph is text or a shadow.
690 mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
695 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "defaultColor[%f, %f, %f, %f ]\n", defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a );
697 Dali::Property::Index shaderTextColorIndex = shader.RegisterProperty( "textColorAnimatable", defaultColor );
699 if ( animatablePropertyIndex != Property::INVALID_INDEX )
701 // create constraint for the animatable text's color Property with textColorAnimatable in the shader.
702 if( shaderTextColorIndex )
704 Constraint constraint = Constraint::New<Vector4>( shader, shaderTextColorIndex, EqualToConstraint() );
705 constraint.AddSource( Source( textControl, animatablePropertyIndex ) );
711 // If not animating the text colour then set to 1's so shader uses the current vertex color
712 shader.RegisterProperty( "textColorAnimatable", Vector4(1.0, 1.0, 1.0, 1.0 ) );
715 Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, shader );
716 renderer.SetTextures( textureSet );
717 renderer.SetProperty( Dali::Renderer::Property::BLEND_MODE, BlendMode::ON );
718 renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth );
720 Actor actor = Actor::New();
721 #if defined(DEBUG_ENABLED)
722 actor.SetName( "Text renderable actor" );
724 actor.AddRenderer( renderer );
725 // Keep all of the origins aligned
726 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
727 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
728 actor.SetSize( actorSize );
729 actor.RegisterProperty("uOffset", Vector2::ZERO );
730 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
735 void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
736 AtlasManager::Mesh2D& newMesh,
737 Vector< Extent >& extents,
740 float underlinePosition,
741 float underlineThickness,
742 AtlasManager::AtlasSlot& slot )
746 float left = newMesh.mVertices[ 0 ].mPosition.x;
747 float right = newMesh.mVertices[ 1 ].mPosition.x;
749 // Check to see if there's a mesh data object that references the same atlas ?
751 for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(),
752 mEndIt = meshContainer.end();
756 if( slot.mAtlasId == mIt->mAtlasId )
758 // Append the mesh to the existing mesh and adjust any extents
759 Toolkit::Internal::AtlasMeshFactory::AppendMesh( mIt->mMesh, newMesh );
763 AdjustExtents( extents,
770 underlineThickness );
777 // No mesh data object currently exists that references this atlas, so create a new one
778 MeshRecord meshRecord;
779 meshRecord.mAtlasId = slot.mAtlasId;
780 meshRecord.mMesh = newMesh;
781 meshContainer.push_back( meshRecord );
785 // Adjust extents for this new meshrecord
786 AdjustExtents( extents,
788 meshContainer.size() - 1u,
793 underlineThickness );
798 void AdjustExtents( Vector< Extent >& extents,
799 std::vector< MeshRecord>& meshRecords,
804 float underlinePosition,
805 float underlineThickness )
807 bool foundExtent = false;
808 for ( Vector< Extent >::Iterator eIt = extents.Begin(),
809 eEndIt = extents.End();
813 if ( Equals( baseLine, eIt->mBaseLine ) )
816 if ( left < eIt->mLeft )
820 if ( right > eIt->mRight )
825 if ( underlinePosition > eIt->mUnderlinePosition )
827 eIt->mUnderlinePosition = underlinePosition;
829 if ( underlineThickness > eIt->mUnderlineThickness )
831 eIt->mUnderlineThickness = underlineThickness;
839 extent.mRight = right;
840 extent.mBaseLine = baseLine;
841 extent.mUnderlinePosition = underlinePosition;
842 extent.mUnderlineThickness = underlineThickness;
843 extent.mMeshRecordIndex = index;
844 extents.PushBack( extent );
848 void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
850 for( Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
851 glyphEndIt = glyphs.End();
852 glyphIt != glyphEndIt;
855 const FontId fontId = (*glyphIt).fontId;
856 bool foundFont = false;
858 for( std::vector< MaxBlockSize >::const_iterator blockIt = mBlockSizes.begin(),
859 blockEndIt = mBlockSizes.end();
860 blockIt != blockEndIt;
863 if( (*blockIt).mFontId == fontId ) // Different size fonts will have a different fontId
865 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize match found fontID(%u) glyphIndex(%u)\n", fontId, (*glyphIt).index );
873 FontMetrics fontMetrics;
874 mFontClient.GetFontMetrics( fontId, fontMetrics );
876 MaxBlockSize maxBlockSize;
877 maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
878 maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
879 maxBlockSize.mFontId = fontId;
880 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize New font with no matched blocksize, setting blocksize[%u]\n", maxBlockSize.mNeededBlockWidth );
881 mBlockSizes.push_back( maxBlockSize );
886 void GenerateUnderlines( std::vector< MeshRecord >& meshRecords,
887 Vector< Extent >& extents,
888 const Vector4& underlineColor )
890 AtlasManager::Mesh2D newMesh;
891 unsigned short faceIndex = 0;
892 for ( Vector< Extent >::ConstIterator eIt = extents.Begin(),
893 eEndIt = extents.End();
897 AtlasManager::Vertex2D vert;
898 uint32_t index = eIt->mMeshRecordIndex;
899 Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
901 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
902 float u = HALF / uv.x;
903 float v = HALF / uv.y;
904 float thickness = eIt->mUnderlineThickness;
905 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
906 float tlx = eIt->mLeft;
907 float brx = eIt->mRight;
909 vert.mPosition.x = tlx;
910 vert.mPosition.y = baseLine;
911 vert.mTexCoords.x = ZERO;
912 vert.mTexCoords.y = ZERO;
913 vert.mColor = underlineColor;
914 newMesh.mVertices.PushBack( vert );
916 vert.mPosition.x = brx;
917 vert.mPosition.y = baseLine;
918 vert.mTexCoords.x = u;
919 vert.mColor = underlineColor;
920 newMesh.mVertices.PushBack( vert );
922 vert.mPosition.x = tlx;
923 vert.mPosition.y = baseLine + thickness;
924 vert.mTexCoords.x = ZERO;
925 vert.mTexCoords.y = v;
926 vert.mColor = underlineColor;
927 newMesh.mVertices.PushBack( vert );
929 vert.mPosition.x = brx;
930 vert.mPosition.y = baseLine + thickness;
931 vert.mTexCoords.x = u;
932 vert.mColor = underlineColor;
933 newMesh.mVertices.PushBack( vert );
935 // Six indices in counter clockwise winding
936 newMesh.mIndices.PushBack( faceIndex + 1u );
937 newMesh.mIndices.PushBack( faceIndex );
938 newMesh.mIndices.PushBack( faceIndex + 2u );
939 newMesh.mIndices.PushBack( faceIndex + 2u );
940 newMesh.mIndices.PushBack( faceIndex + 3u );
941 newMesh.mIndices.PushBack( faceIndex + 1u );
944 Toolkit::Internal::AtlasMeshFactory::AppendMesh( meshRecords[ index ].mMesh, newMesh );
948 Actor mActor; ///< The actor parent which renders the text
949 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
950 TextAbstraction::FontClient mFontClient; ///< The font client used to supply glyph information
951 Shader mShaderL8; ///< The shader for glyphs and emoji's shadows.
952 Shader mShaderRgba; ///< The shader for emojis.
953 std::vector< MaxBlockSize > mBlockSizes; ///< Maximum size needed to contain a glyph in a block within a new atlas
954 Vector< TextCacheEntry > mTextCache; ///< Caches data from previous render
955 Property::Map mQuadVertexFormat; ///< Describes the vertex format for text
956 int mDepth; ///< DepthIndex passed by control when connect to stage
959 Text::RendererPtr AtlasRenderer::New()
961 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
963 return Text::RendererPtr( new AtlasRenderer() );
966 Actor AtlasRenderer::Render( Text::ViewInterface& view,
968 Property::Index animatablePropertyIndex,
969 float& alignmentOffset,
972 DALI_LOG_INFO( gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n" );
974 UnparentAndReset( mImpl->mActor );
976 Length numberOfGlyphs = view.GetNumberOfGlyphs();
978 if( numberOfGlyphs > 0u )
980 Vector<GlyphInfo> glyphs;
981 glyphs.Resize( numberOfGlyphs );
983 Vector<Vector2> positions;
984 positions.Resize( numberOfGlyphs );
986 numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
992 glyphs.Resize( numberOfGlyphs );
993 positions.Resize( numberOfGlyphs );
995 const Vector4* const colorsBuffer = view.GetColors();
996 const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
997 const Vector4& defaultColor = view.GetTextColor();
999 mImpl->AddGlyphs( view,
1001 animatablePropertyIndex,
1010 /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
1011 /* 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. */
1012 if ( !mImpl->mActor )
1014 mImpl->mActor = Actor::New();
1018 return mImpl->mActor;
1021 AtlasRenderer::AtlasRenderer()
1027 AtlasRenderer::~AtlasRenderer()
1029 mImpl->RemoveText();