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/public-api/images/frame-buffer-image.h>
25 #include <dali/devel-api/text-abstraction/font-client.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/animation/constraints.h>
30 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
31 #include <dali-toolkit/internal/text/glyph-run.h>
32 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
33 #include <dali-toolkit/internal/text/rendering/atlas/atlas-mesh-factory.h>
34 #include <dali-toolkit/internal/text/text-view.h>
37 using namespace Dali::Toolkit;
38 using namespace Dali::Toolkit::Text;
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_RENDERING");
46 #define MAKE_SHADER(A)#A
48 const char* VERTEX_SHADER = MAKE_SHADER(
49 attribute mediump vec2 aPosition;
50 attribute mediump vec2 aTexCoord;
51 attribute mediump vec4 aColor;
52 uniform mediump vec2 uOffset;
53 uniform mediump mat4 uMvpMatrix;
54 varying mediump vec2 vTexCoord;
55 varying mediump vec4 vColor;
59 mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 );
60 gl_Position = uMvpMatrix * position;
61 vTexCoord = aTexCoord;
66 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
67 uniform lowp vec4 uColor;
68 uniform lowp vec4 textColorAnimatable;
69 uniform sampler2D sTexture;
70 varying mediump vec2 vTexCoord;
71 varying mediump vec4 vColor;
75 mediump vec4 color = texture2D( sTexture, vTexCoord );
76 gl_FragColor = vec4( vColor.rgb * uColor.rgb * textColorAnimatable.rgb, uColor.a * vColor.a * textColorAnimatable.a * color.r );
80 const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER(
81 uniform lowp vec4 uColor;
82 uniform lowp vec4 textColorAnimatable;
83 uniform sampler2D sTexture;
84 varying mediump vec2 vTexCoord;
88 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * textColorAnimatable;
92 const float ZERO( 0.0f );
93 const float HALF( 0.5f );
94 const float ONE( 1.0f );
95 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
96 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
97 const int NO_OUTLINE( 0 );
100 struct AtlasRenderer::Impl
116 AtlasManager::Mesh2D mMesh;
117 FrameBufferImage mBuffer;
121 * brief Struct used to generate the underline mesh.
122 * There is one Extent per line of text.
130 mUnderlinePosition( 0.0f ),
131 mUnderlineThickness( 0.0f ),
132 mMeshRecordIndex( 0u )
139 float mUnderlinePosition;
140 float mUnderlineThickness;
141 uint32_t mMeshRecordIndex;
148 mNeededBlockWidth( 0 ),
149 mNeededBlockHeight( 0 )
154 uint32_t mNeededBlockWidth;
155 uint32_t mNeededBlockHeight;
167 Text::GlyphIndex mIndex;
170 struct TextCacheEntry
180 Text::GlyphIndex mIndex;
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, AtlasManager::AtlasSlot& slot )
216 bool glyphNotCached = !mGlyphManager.IsCached( glyph.fontId, glyph.index, 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 current glyph is a color one.
246 const bool isColorGlyph = mFontClient.IsColorGlyph( glyph.fontId, glyph.index );
248 // Retrieve the emoji's bitmap.
249 TextAbstraction::FontClient::GlyphBufferData glyphBufferData;
250 glyphBufferData.width = isColorGlyph ? glyph.width : 0; // Desired width and height.
251 glyphBufferData.height = isColorGlyph ? glyph.height : 0;
253 mFontClient.CreateBitmap( glyph.fontId,
258 // Create the pixel data.
259 bitmap = PixelData::New( glyphBufferData.buffer,
260 glyph.width * glyph.height * GetBytesPerPixel( glyphBufferData.format ),
263 glyphBufferData.format,
264 PixelData::DELETE_ARRAY );
268 // Ensure that the next image will fit into the current block size
269 if( bitmap.GetWidth() > blockSize.mNeededBlockWidth )
271 blockSize.mNeededBlockWidth = bitmap.GetWidth();
274 if( bitmap.GetHeight() > blockSize.mNeededBlockHeight )
276 blockSize.mNeededBlockHeight = bitmap.GetHeight();
279 // If CheckAtlas in AtlasManager::Add can't fit the bitmap in the current atlas it will create a new atlas
281 // Setting the block size and size of new atlas does not mean a new one will be created. An existing atlas may still surffice.
282 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
283 DEFAULT_ATLAS_HEIGHT,
284 blockSize.mNeededBlockWidth,
285 blockSize.mNeededBlockHeight );
287 // Locate a new slot for our glyph
288 mGlyphManager.Add( glyph, bitmap, slot ); // slot will be 0 is glyph not added
293 // We have 2+ copies of the same glyph
294 mGlyphManager.AdjustReferenceCount( glyph.fontId, glyph.index, 1/*increment*/ );
298 void GenerateMesh( const GlyphInfo& glyph,
299 const Vector2& position,
300 const Vector4& color,
301 AtlasManager::AtlasSlot& slot,
303 float currentUnderlinePosition,
304 float currentUnderlineThickness,
305 std::vector<MeshRecord>& meshContainer,
306 Vector<TextCacheEntry>& newTextCache,
307 Vector<Extent>& extents )
309 // Generate mesh data for this quad, plugging in our supplied position
310 AtlasManager::Mesh2D newMesh;
311 mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
313 TextCacheEntry textCacheEntry;
314 textCacheEntry.mFontId = glyph.fontId;
315 textCacheEntry.mImageId = slot.mImageId;
316 textCacheEntry.mIndex = glyph.index;
318 newTextCache.PushBack( textCacheEntry );
320 AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
322 for( unsigned int index = 0u, size = newMesh.mVertices.Count();
326 AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
328 // Set the color of the vertex.
329 vertex.mColor = color;
332 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
333 StitchTextMesh( meshContainer,
336 position.y + glyph.yBearing,
338 currentUnderlinePosition,
339 currentUnderlineThickness,
343 void CreateActors( const std::vector<MeshRecord>& meshContainer,
344 const Size& textSize,
345 const Vector4& defaultColor,
346 const Vector4& shadowColor,
347 const Vector2& shadowOffset,
350 Property::Index animatablePropertyIndex )
354 // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
355 mActor = Actor::New();
356 mActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
357 mActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
358 mActor.SetSize( textSize );
359 mActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
362 for( std::vector< MeshRecord >::const_iterator it = meshContainer.begin(),
363 endIt = meshContainer.end();
366 const MeshRecord& meshRecord = *it;
368 Actor actor = CreateMeshActor( textControl, animatablePropertyIndex, defaultColor, meshRecord, textSize, STYLE_NORMAL );
370 // Whether the actor has renderers.
371 const bool hasRenderer = actor.GetRendererCount() > 0u;
373 // Create an effect if necessary
375 ( style == STYLE_DROP_SHADOW ) )
377 // Change the color of the vertices.
378 for( Vector<AtlasManager::Vertex2D>::Iterator vIt = meshRecord.mMesh.mVertices.Begin(),
379 vEndIt = meshRecord.mMesh.mVertices.End();
383 AtlasManager::Vertex2D& vertex = *vIt;
385 vertex.mColor = shadowColor;
388 Actor shadowActor = CreateMeshActor(textControl, animatablePropertyIndex, defaultColor, meshRecord, textSize, STYLE_DROP_SHADOW );
389 #if defined(DEBUG_ENABLED)
390 shadowActor.SetName( "Text Shadow renderable actor" );
392 // Offset shadow in x and y
393 shadowActor.RegisterProperty("uOffset", shadowOffset );
394 Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) );
395 int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
396 renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 );
397 mActor.Add( shadowActor );
407 void AddGlyphs( Text::ViewInterface& view,
409 Property::Index animatablePropertyIndex,
410 const Vector<Vector2>& positions,
411 const Vector<GlyphInfo>& glyphs,
412 const Vector4& defaultColor,
413 const Vector4* const colorsBuffer,
414 const ColorIndex* const colorIndicesBuffer,
416 float minLineOffset )
418 AtlasManager::AtlasSlot slot;
419 std::vector< MeshRecord > meshContainer;
420 Vector< Extent > extents;
423 const Vector2& textSize( view.GetLayoutSize() );
424 const Vector2 halfTextSize( textSize * 0.5f );
425 const Vector2& shadowOffset( view.GetShadowOffset() );
426 const Vector4& shadowColor( view.GetShadowColor() );
427 const bool underlineEnabled( view.IsUnderlineEnabled() );
428 const Vector4& underlineColor( view.GetUnderlineColor() );
429 const float underlineHeight( view.GetUnderlineHeight() );
431 const bool useDefaultColor = ( NULL == colorsBuffer );
433 // Get the underline runs.
434 const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
435 Vector<GlyphRun> underlineRuns;
436 underlineRuns.Resize( numberOfUnderlineRuns );
437 view.GetUnderlineRuns( underlineRuns.Begin(),
439 numberOfUnderlineRuns );
441 bool thereAreUnderlinedGlyphs = false;
443 float currentUnderlinePosition = ZERO;
444 float currentUnderlineThickness = underlineHeight;
445 FontId lastFontId = 0;
446 FontId lastUnderlinedFontId = 0;
447 Style style = STYLE_NORMAL;
449 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
451 style = STYLE_DROP_SHADOW;
454 CalculateBlocksSize( glyphs );
456 // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
457 Vector< TextCacheEntry > newTextCache;
458 const GlyphInfo* const glyphsBuffer = glyphs.Begin();
459 const Vector2* const positionsBuffer = positions.Begin();
460 const Vector2 lineOffsetPosition( minLineOffset, 0.f );
462 for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
464 const GlyphInfo& glyph = *( glyphsBuffer + i );
465 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( i, underlineRuns );
466 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
468 // No operation for white space
469 if( glyph.width && glyph.height )
471 // Are we still using the same fontId as previous
472 if( underlineGlyph && ( glyph.fontId != lastUnderlinedFontId ) )
474 // We need to fetch fresh font underline metrics
475 FontMetrics fontMetrics;
476 mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
477 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
478 const float descender = ceil( fabsf( fontMetrics.descender ) );
480 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
482 currentUnderlineThickness = fontMetrics.underlineThickness;
484 // Ensure underline will be at least a pixel high
485 if ( currentUnderlineThickness < ONE )
487 currentUnderlineThickness = ONE;
491 currentUnderlineThickness = ceil( currentUnderlineThickness );
495 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
496 if( currentUnderlinePosition > descender )
498 currentUnderlinePosition = descender;
501 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
503 // Move offset down by one ( EFL behavior )
504 currentUnderlinePosition = ONE;
507 lastUnderlinedFontId = glyph.fontId;
510 // Retrieves and caches the glyph's bitmap.
511 CacheGlyph( glyph, lastFontId, slot );
513 // Move the origin (0,0) of the mesh to the center of the actor
514 const Vector2 position = *( positionsBuffer + i ) - halfTextSize - lineOffsetPosition;
516 if ( 0u != slot.mImageId ) // invalid slot id, glyph has failed to be added to atlas
518 // Get the color of the character.
519 const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndicesBuffer + i );
520 const Vector4& color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + colorIndex - 1u );
527 currentUnderlinePosition,
528 currentUnderlineThickness,
533 lastFontId = glyph.fontId; // Prevents searching for existing blocksizes when string of the same fontId.
538 // Now remove references for the old text
540 mTextCache.Swap( newTextCache );
542 if( thereAreUnderlinedGlyphs )
544 // Check to see if any of the text needs an underline
545 GenerateUnderlines( meshContainer, extents, underlineColor );
548 // For each MeshData object, create a mesh actor and add to the renderable actor
549 if( !meshContainer.empty() )
551 CreateActors( meshContainer,
558 animatablePropertyIndex );
561 #if defined(DEBUG_ENABLED)
562 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
563 DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
565 metrics.mAtlasMetrics.mAtlasCount,
566 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
568 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str() );
570 for( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
572 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
573 i + 1, i > 8 ? "" : " ",
574 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8 " : "BGRA",
575 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
576 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
577 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
578 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
579 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
580 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
587 for( Vector< TextCacheEntry >::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter )
589 mGlyphManager.AdjustReferenceCount( oldTextIter->mFontId, oldTextIter->mIndex, -1/*decrement*/ );
591 mTextCache.Resize( 0 );
594 Actor CreateMeshActor( Actor textControl, Property::Index animatablePropertyIndex, const Vector4& defaultColor, const MeshRecord& meshRecord,
595 const Vector2& actorSize, Style style )
597 PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat );
598 quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ), meshRecord.mMesh.mVertices.Size() );
600 Geometry quadGeometry = Geometry::New();
601 quadGeometry.AddVertexBuffer( quadVertices );
602 quadGeometry.SetIndexBuffer( &meshRecord.mMesh.mIndices[0], meshRecord.mMesh.mIndices.Size() );
604 TextureSet textureSet( mGlyphManager.GetTextures( meshRecord.mAtlasId ) );
606 // Choose the shader to use.
607 const bool isColorShader = ( STYLE_DROP_SHADOW != style ) && ( Pixel::BGRA8888 == mGlyphManager.GetPixelFormat( meshRecord.mAtlasId ) );
611 // The glyph is an emoji and is not a shadow.
614 mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA );
616 shader = mShaderRgba;
620 // The glyph is text or a shadow.
623 mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
628 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "defaultColor[%f, %f, %f, %f ]\n", defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a );
630 Dali::Property::Index shaderTextColorIndex = shader.RegisterProperty( "textColorAnimatable", defaultColor );
632 if ( animatablePropertyIndex != Property::INVALID_INDEX )
634 // create constraint for the animatable text's color Property with textColorAnimatable in the shader.
635 if( shaderTextColorIndex )
637 Constraint constraint = Constraint::New<Vector4>( shader, shaderTextColorIndex, EqualToConstraint() );
638 constraint.AddSource( Source( textControl, animatablePropertyIndex ) );
644 // If not animating the text colour then set to 1's so shader uses the current vertex color
645 shader.RegisterProperty( "textColorAnimatable", Vector4(1.0, 1.0, 1.0, 1.0 ) );
648 Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, shader );
649 renderer.SetTextures( textureSet );
650 renderer.SetProperty( Dali::Renderer::Property::BLEND_MODE, BlendMode::ON );
651 renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth );
653 Actor actor = Actor::New();
654 #if defined(DEBUG_ENABLED)
655 actor.SetName( "Text renderable actor" );
657 actor.AddRenderer( renderer );
658 // Keep all of the origins aligned
659 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
660 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
661 actor.SetSize( actorSize );
662 actor.RegisterProperty("uOffset", Vector2::ZERO );
663 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
667 void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
668 AtlasManager::Mesh2D& newMesh,
669 Vector< Extent >& extents,
672 float underlinePosition,
673 float underlineThickness,
674 AtlasManager::AtlasSlot& slot )
678 float left = newMesh.mVertices[ 0 ].mPosition.x;
679 float right = newMesh.mVertices[ 1 ].mPosition.x;
681 // Check to see if there's a mesh data object that references the same atlas ?
683 for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(),
684 mEndIt = meshContainer.end();
688 if( slot.mAtlasId == mIt->mAtlasId )
690 // Append the mesh to the existing mesh and adjust any extents
691 Toolkit::Internal::AtlasMeshFactory::AppendMesh( mIt->mMesh, newMesh );
695 AdjustExtents( extents,
702 underlineThickness );
709 // No mesh data object currently exists that references this atlas, so create a new one
710 MeshRecord meshRecord;
711 meshRecord.mAtlasId = slot.mAtlasId;
712 meshRecord.mMesh = newMesh;
713 meshContainer.push_back( meshRecord );
717 // Adjust extents for this new meshrecord
718 AdjustExtents( extents,
720 meshContainer.size() - 1u,
725 underlineThickness );
730 void AdjustExtents( Vector< Extent >& extents,
731 std::vector< MeshRecord>& meshRecords,
736 float underlinePosition,
737 float underlineThickness )
739 bool foundExtent = false;
740 for ( Vector< Extent >::Iterator eIt = extents.Begin(),
741 eEndIt = extents.End();
745 if ( Equals( baseLine, eIt->mBaseLine ) )
748 if ( left < eIt->mLeft )
752 if ( right > eIt->mRight )
757 if ( underlinePosition > eIt->mUnderlinePosition )
759 eIt->mUnderlinePosition = underlinePosition;
761 if ( underlineThickness > eIt->mUnderlineThickness )
763 eIt->mUnderlineThickness = underlineThickness;
771 extent.mRight = right;
772 extent.mBaseLine = baseLine;
773 extent.mUnderlinePosition = underlinePosition;
774 extent.mUnderlineThickness = underlineThickness;
775 extent.mMeshRecordIndex = index;
776 extents.PushBack( extent );
780 void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
782 for( Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
783 glyphEndIt = glyphs.End();
784 glyphIt != glyphEndIt;
787 const FontId fontId = (*glyphIt).fontId;
788 bool foundFont = false;
790 for( std::vector< MaxBlockSize >::const_iterator blockIt = mBlockSizes.begin(),
791 blockEndIt = mBlockSizes.end();
792 blockIt != blockEndIt;
795 if( (*blockIt).mFontId == fontId ) // Different size fonts will have a different fontId
797 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize match found fontID(%u) glyphIndex(%u)\n", fontId, (*glyphIt).index );
805 FontMetrics fontMetrics;
806 mFontClient.GetFontMetrics( fontId, fontMetrics );
808 MaxBlockSize maxBlockSize;
809 maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
810 maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
811 maxBlockSize.mFontId = fontId;
812 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize New font with no matched blocksize, setting blocksize[%u]\n", maxBlockSize.mNeededBlockWidth );
813 mBlockSizes.push_back( maxBlockSize );
818 void GenerateUnderlines( std::vector< MeshRecord >& meshRecords,
819 Vector< Extent >& extents,
820 const Vector4& underlineColor )
822 AtlasManager::Mesh2D newMesh;
823 unsigned short faceIndex = 0;
824 for ( Vector< Extent >::ConstIterator eIt = extents.Begin(),
825 eEndIt = extents.End();
829 AtlasManager::Vertex2D vert;
830 uint32_t index = eIt->mMeshRecordIndex;
831 Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
833 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
834 float u = HALF / uv.x;
835 float v = HALF / uv.y;
836 float thickness = eIt->mUnderlineThickness;
837 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
838 float tlx = eIt->mLeft;
839 float brx = eIt->mRight;
841 vert.mPosition.x = tlx;
842 vert.mPosition.y = baseLine;
843 vert.mTexCoords.x = ZERO;
844 vert.mTexCoords.y = ZERO;
845 vert.mColor = underlineColor;
846 newMesh.mVertices.PushBack( vert );
848 vert.mPosition.x = brx;
849 vert.mPosition.y = baseLine;
850 vert.mTexCoords.x = u;
851 vert.mColor = underlineColor;
852 newMesh.mVertices.PushBack( vert );
854 vert.mPosition.x = tlx;
855 vert.mPosition.y = baseLine + thickness;
856 vert.mTexCoords.x = ZERO;
857 vert.mTexCoords.y = v;
858 vert.mColor = underlineColor;
859 newMesh.mVertices.PushBack( vert );
861 vert.mPosition.x = brx;
862 vert.mPosition.y = baseLine + thickness;
863 vert.mTexCoords.x = u;
864 vert.mColor = underlineColor;
865 newMesh.mVertices.PushBack( vert );
867 // Six indices in counter clockwise winding
868 newMesh.mIndices.PushBack( faceIndex + 1u );
869 newMesh.mIndices.PushBack( faceIndex );
870 newMesh.mIndices.PushBack( faceIndex + 2u );
871 newMesh.mIndices.PushBack( faceIndex + 2u );
872 newMesh.mIndices.PushBack( faceIndex + 3u );
873 newMesh.mIndices.PushBack( faceIndex + 1u );
876 Toolkit::Internal::AtlasMeshFactory::AppendMesh( meshRecords[ index ].mMesh, newMesh );
880 Actor mActor; ///< The actor parent which renders the text
881 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
882 TextAbstraction::FontClient mFontClient; ///< The font client used to supply glyph information
883 Shader mShaderL8; ///< The shader for glyphs and emoji's shadows.
884 Shader mShaderRgba; ///< The shader for emojis.
885 std::vector< MaxBlockSize > mBlockSizes; ///< Maximum size needed to contain a glyph in a block within a new atlas
886 Vector< TextCacheEntry > mTextCache; ///< Caches data from previous render
887 Property::Map mQuadVertexFormat; ///< Describes the vertex format for text
888 int mDepth; ///< DepthIndex passed by control when connect to stage
891 Text::RendererPtr AtlasRenderer::New()
893 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
895 return Text::RendererPtr( new AtlasRenderer() );
898 Actor AtlasRenderer::Render( Text::ViewInterface& view,
900 Property::Index animatablePropertyIndex,
901 float& alignmentOffset,
904 DALI_LOG_INFO( gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n" );
906 UnparentAndReset( mImpl->mActor );
908 Length numberOfGlyphs = view.GetNumberOfGlyphs();
910 if( numberOfGlyphs > 0u )
912 Vector<GlyphInfo> glyphs;
913 glyphs.Resize( numberOfGlyphs );
915 Vector<Vector2> positions;
916 positions.Resize( numberOfGlyphs );
918 numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
924 glyphs.Resize( numberOfGlyphs );
925 positions.Resize( numberOfGlyphs );
927 const Vector4* const colorsBuffer = view.GetColors();
928 const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
929 const Vector4& defaultColor = view.GetTextColor();
931 mImpl->AddGlyphs( view,
933 animatablePropertyIndex,
942 /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
943 /* 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. */
944 if ( !mImpl->mActor )
946 mImpl->mActor = Actor::New();
950 return mImpl->mActor;
953 AtlasRenderer::AtlasRenderer()
959 AtlasRenderer::~AtlasRenderer()