Text - Fix for placement actor.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / text-atlas-renderer.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.h>
20
21 // EXTERNAL INCLUDES
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
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
29 #include <dali-toolkit/internal/text/glyph-run.h>
30 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
31 #include <dali-toolkit/internal/text/rendering/atlas/atlas-mesh-factory.h>
32 #include <dali-toolkit/internal/text/text-view.h>
33
34 using namespace Dali;
35 using namespace Dali::Toolkit;
36 using namespace Dali::Toolkit::Text;
37
38 namespace
39 {
40 #if defined(DEBUG_ENABLED)
41   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_RENDERING");
42 #endif
43
44 const float ZERO( 0.0f );
45 const float HALF( 0.5f );
46 const float ONE( 1.0f );
47 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
48 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
49 }
50
51 struct AtlasRenderer::Impl
52 {
53   enum Style
54   {
55     STYLE_NORMAL,
56     STYLE_DROP_SHADOW
57   };
58
59   struct MeshRecord
60   {
61     MeshRecord()
62     : mAtlasId( 0u )
63     {
64     }
65
66     uint32_t mAtlasId;
67     AtlasManager::Mesh2D mMesh;
68     FrameBufferImage mBuffer;
69   };
70
71   /**
72    * brief Struct used to generate the underline mesh.
73    * There is one Extent per line of text.
74    */
75   struct Extent
76   {
77     Extent()
78     : mBaseLine( 0.0f ),
79       mLeft( 0.0f ),
80       mRight( 0.0f ),
81       mUnderlinePosition( 0.0f ),
82       mUnderlineThickness( 0.0f ),
83       mMeshRecordIndex( 0u )
84     {
85     }
86
87     float mBaseLine;
88     float mLeft;
89     float mRight;
90     float mUnderlinePosition;
91     float mUnderlineThickness;
92     uint32_t mMeshRecordIndex;
93   };
94
95   struct MaxBlockSize
96   {
97     MaxBlockSize()
98     : mFontId( 0 ),
99       mNeededBlockWidth( 0 ),
100       mNeededBlockHeight( 0 )
101     {
102     }
103
104     FontId mFontId;
105     uint32_t mNeededBlockWidth;
106     uint32_t mNeededBlockHeight;
107   };
108
109   struct CheckEntry
110   {
111     CheckEntry()
112     : mFontId( 0 ),
113       mIndex( 0 )
114     {
115     }
116
117     FontId mFontId;
118     Text::GlyphIndex mIndex;
119   };
120
121   struct TextCacheEntry
122   {
123     TextCacheEntry()
124     : mFontId( 0 ),
125       mIndex( 0 ),
126       mImageId( 0 )
127     {
128     }
129
130     FontId mFontId;
131     Text::GlyphIndex mIndex;
132     uint32_t mImageId;
133   };
134
135   Impl()
136   : mDepth( 0 )
137   {
138     mGlyphManager = AtlasGlyphManager::Get();
139     mFontClient = TextAbstraction::FontClient::Get();
140
141     mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
142     mQuadVertexFormat[ "aTexCoord" ] = Property::VECTOR2;
143     mQuadVertexFormat[ "aColor" ] = Property::VECTOR4;
144   }
145
146   bool IsGlyphUnderlined( GlyphIndex index,
147                           const Vector<GlyphRun>& underlineRuns )
148   {
149     for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
150            endIt = underlineRuns.End();
151            it != endIt;
152          ++it )
153     {
154       const GlyphRun& run = *it;
155
156       if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
157       {
158         return true;
159       }
160     }
161
162     return false;
163   }
164
165   void AddGlyphs( Text::ViewInterface& view,
166                   const Vector<Vector2>& positions,
167                   const Vector<GlyphInfo>& glyphs,
168                   const Vector4& defaultColor,
169                   const Vector4* const colorsBuffer,
170                   const ColorIndex* const colorIndicesBuffer,
171                   int depth,
172                   float minLineOffset )
173   {
174     AtlasManager::AtlasSlot slot;
175     std::vector< MeshRecord > meshContainer;
176     Vector< Extent > extents;
177     TextCacheEntry textCacheEntry;
178     mDepth = depth;
179
180     const Vector2& textSize( view.GetLayoutSize() );
181     const Vector2 halfTextSize( textSize * 0.5f );
182     const Vector2& shadowOffset( view.GetShadowOffset() );
183     const Vector4& shadowColor( view.GetShadowColor() );
184     const bool underlineEnabled( view.IsUnderlineEnabled() );
185     const Vector4& underlineColor( view.GetUnderlineColor() );
186     const float underlineHeight( view.GetUnderlineHeight() );
187
188     const bool useDefaultColor = ( NULL == colorsBuffer );
189
190     // Get the underline runs.
191     const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
192     Vector<GlyphRun> underlineRuns;
193     underlineRuns.Resize( numberOfUnderlineRuns );
194     view.GetUnderlineRuns( underlineRuns.Begin(),
195                            0u,
196                            numberOfUnderlineRuns );
197
198     bool thereAreUnderlinedGlyphs = false;
199
200     float currentUnderlinePosition = ZERO;
201     float currentUnderlineThickness = underlineHeight;
202     uint32_t currentBlockSize = 0;
203     FontId lastFontId = 0;
204     FontId lastUnderlinedFontId = 0;
205     Style style = STYLE_NORMAL;
206
207     if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
208     {
209       style = STYLE_DROP_SHADOW;
210     }
211
212     CalculateBlocksSize( glyphs );
213
214     // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
215     Vector< TextCacheEntry > newTextCache;
216     const GlyphInfo* const glyphsBuffer = glyphs.Begin();
217     const Vector2* const positionsBuffer = positions.Begin();
218     const Vector2 lineOffsetPosition( minLineOffset, 0.f );
219
220     for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
221     {
222       const GlyphInfo& glyph = *( glyphsBuffer + i );
223       const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( i, underlineRuns );
224       thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
225
226       // No operation for white space
227       if( glyph.width && glyph.height )
228       {
229         // Are we still using the same fontId as previous
230         if( underlineGlyph && ( glyph.fontId != lastUnderlinedFontId ) )
231         {
232           // We need to fetch fresh font underline metrics
233           FontMetrics fontMetrics;
234           mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
235           currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
236           const float descender = ceil( fabsf( fontMetrics.descender ) );
237
238           if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
239           {
240             currentUnderlineThickness = fontMetrics.underlineThickness;
241
242             // Ensure underline will be at least a pixel high
243             if ( currentUnderlineThickness < ONE )
244             {
245               currentUnderlineThickness = ONE;
246             }
247             else
248             {
249               currentUnderlineThickness = ceil( currentUnderlineThickness );
250             }
251           }
252
253           // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
254           if( currentUnderlinePosition > descender )
255           {
256             currentUnderlinePosition = descender;
257           }
258
259           if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
260           {
261             // Move offset down by one ( EFL behavior )
262             currentUnderlinePosition = ONE;
263           }
264
265           lastUnderlinedFontId = glyph.fontId;
266         } // underline
267
268         if( !mGlyphManager.IsCached( glyph.fontId, glyph.index, slot ) )
269         {
270           // Select correct size for new atlas if needed....?
271           if( lastFontId != glyph.fontId )
272           {
273             uint32_t index = 0u;
274             for( std::vector<MaxBlockSize>::const_iterator it = mBlockSizes.begin(),
275                    endIt = mBlockSizes.end();
276                  it != endIt;
277                  ++it, ++index )
278             {
279               const MaxBlockSize& blockSize = *it;
280               if( blockSize.mFontId == glyph.fontId )
281               {
282                 currentBlockSize = index;
283                 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
284                                                DEFAULT_ATLAS_HEIGHT,
285                                                blockSize.mNeededBlockWidth,
286                                                blockSize.mNeededBlockHeight );
287               }
288             }
289           }
290
291           // Create a new image for the glyph
292           PixelData bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
293           if( bitmap )
294           {
295             MaxBlockSize& blockSize = mBlockSizes[currentBlockSize];
296
297             // Ensure that the next image will fit into the current block size
298             bool setSize = false;
299             if( bitmap.GetWidth() > blockSize.mNeededBlockWidth )
300             {
301               setSize = true;
302               blockSize.mNeededBlockWidth = bitmap.GetWidth();
303             }
304             if( bitmap.GetHeight() > blockSize.mNeededBlockHeight )
305             {
306               setSize = true;
307               blockSize.mNeededBlockHeight = bitmap.GetHeight();
308             }
309
310             if( setSize )
311             {
312               mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
313                                              DEFAULT_ATLAS_HEIGHT,
314                                              blockSize.mNeededBlockWidth,
315                                              blockSize.mNeededBlockHeight );
316             }
317
318             // Locate a new slot for our glyph
319             mGlyphManager.Add( glyph, bitmap, slot );
320           }
321         }
322         else
323         {
324           // We have 2+ copies of the same glyph
325           mGlyphManager.AdjustReferenceCount( glyph.fontId, glyph.index, 1/*increment*/ );
326         }
327
328         // Move the origin (0,0) of the mesh to the center of the actor
329         const Vector2 position = *( positionsBuffer + i ) - halfTextSize - lineOffsetPosition;
330
331         // Generate mesh data for this quad, plugging in our supplied position
332         AtlasManager::Mesh2D newMesh;
333         mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
334         textCacheEntry.mFontId = glyph.fontId;
335         textCacheEntry.mImageId = slot.mImageId;
336         textCacheEntry.mIndex = glyph.index;
337         newTextCache.PushBack( textCacheEntry );
338
339         AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
340
341         // Adjust the vertices if the fixed-size font should be down-scaled
342         if( glyph.scaleFactor > 0 )
343         {
344           for( unsigned int index = 0u, size = newMesh.mVertices.Count();
345                index < size;
346                ++index )
347           {
348             AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
349
350             // Set the position of the vertex.
351             vertex.mPosition.x = position.x + ( ( vertex.mPosition.x - position.x ) * glyph.scaleFactor );
352             vertex.mPosition.y = position.y + ( ( vertex.mPosition.y - position.y ) * glyph.scaleFactor );
353           }
354         }
355
356         // Get the color of the character.
357         const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndicesBuffer + i );
358         const Vector4& color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + colorIndex - 1u );
359
360         for( unsigned int index = 0u, size = newMesh.mVertices.Count();
361              index < size;
362              ++index )
363         {
364           AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
365
366           // Set the color of the vertex.
367           vertex.mColor = color;
368         }
369
370         // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
371         StitchTextMesh( meshContainer,
372                         newMesh,
373                         extents,
374                         position.y + glyph.yBearing,
375                         underlineGlyph,
376                         currentUnderlinePosition,
377                         currentUnderlineThickness,
378                         slot );
379         lastFontId = glyph.fontId;
380       }
381     } // glyphs
382
383     // Now remove references for the old text
384     RemoveText();
385     mTextCache.Swap( newTextCache );
386
387     if( thereAreUnderlinedGlyphs )
388     {
389       // Check to see if any of the text needs an underline
390       GenerateUnderlines( meshContainer, extents, underlineColor );
391     }
392
393     // For each MeshData object, create a mesh actor and add to the renderable actor
394     if( !meshContainer.empty() )
395     {
396       if( !mActor )
397       {
398         // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
399         mActor = Actor::New();
400         mActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
401         mActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
402         mActor.SetSize( textSize );
403       }
404
405       for( std::vector< MeshRecord >::iterator it = meshContainer.begin(),
406               endIt = meshContainer.end();
407             it != endIt; ++it )
408       {
409         MeshRecord& meshRecord = *it;
410
411         Actor actor = CreateMeshActor( meshRecord, textSize );
412
413         // Whether the actor has renderers.
414         const bool hasRenderer = actor.GetRendererCount() > 0u;
415
416         // Create an effect if necessary
417         if( hasRenderer &&
418             ( style == STYLE_DROP_SHADOW ) )
419         {
420           // Change the color of the vertices.
421           for( Vector<AtlasManager::Vertex2D>::Iterator vIt =  meshRecord.mMesh.mVertices.Begin(),
422                  vEndIt = meshRecord.mMesh.mVertices.End();
423                vIt != vEndIt;
424                ++vIt )
425           {
426             AtlasManager::Vertex2D& vertex = *vIt;
427
428             vertex.mColor = shadowColor;
429           }
430
431           Actor shadowActor = CreateMeshActor( meshRecord, textSize );
432 #if defined(DEBUG_ENABLED)
433           shadowActor.SetName( "Text Shadow renderable actor" );
434 #endif
435           // Offset shadow in x and y
436           shadowActor.RegisterProperty("uOffset", shadowOffset );
437           Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) );
438           int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
439           renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 );
440           mActor.Add( shadowActor );
441         }
442
443         if( hasRenderer )
444         {
445           mActor.Add( actor );
446         }
447       }
448     }
449 #if defined(DEBUG_ENABLED)
450     Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
451     DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
452                                                 metrics.mGlyphCount,
453                                                 metrics.mAtlasMetrics.mAtlasCount,
454                                                 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
455
456     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str() );
457
458     for( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
459     {
460       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "   Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
461                                                  i + 1, i > 8 ? "" : " ",
462                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8  " : "BGRA",
463                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
464                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
465                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
466                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
467                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
468                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
469     }
470 #endif
471   }
472
473   void RemoveText()
474   {
475     for( Vector< TextCacheEntry >::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter )
476     {
477       mGlyphManager.AdjustReferenceCount( oldTextIter->mFontId, oldTextIter->mIndex, -1/*decrement*/ );
478     }
479     mTextCache.Resize( 0 );
480   }
481
482   Actor CreateMeshActor( const MeshRecord& meshRecord, const Vector2& actorSize )
483   {
484     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat );
485     quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ), meshRecord.mMesh.mVertices.Size() );
486
487     Geometry quadGeometry = Geometry::New();
488     quadGeometry.AddVertexBuffer( quadVertices );
489     quadGeometry.SetIndexBuffer( &meshRecord.mMesh.mIndices[0],  meshRecord.mMesh.mIndices.Size() );
490
491     TextureSet textureSet( mGlyphManager.GetTextures( meshRecord.mAtlasId ) );
492     Shader shader( mGlyphManager.GetShader( meshRecord.mAtlasId ) );
493     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, shader );
494     renderer.SetTextures( textureSet );
495     renderer.SetProperty( Dali::Renderer::Property::BLEND_MODE, BlendMode::ON );
496     renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth );
497     Actor actor = Actor::New();
498 #if defined(DEBUG_ENABLED)
499     actor.SetName( "Text renderable actor" );
500 #endif
501     actor.AddRenderer( renderer );
502
503     // Keep all of the origins aligned
504     actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
505     actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
506     actor.SetSize( actorSize );
507     actor.RegisterProperty("uOffset", Vector2::ZERO );
508     return actor;
509   }
510
511   void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
512                        AtlasManager::Mesh2D& newMesh,
513                        Vector< Extent >& extents,
514                        float baseLine,
515                        bool underlineGlyph,
516                        float underlinePosition,
517                        float underlineThickness,
518                        AtlasManager::AtlasSlot& slot )
519   {
520     if ( slot.mImageId )
521     {
522       float left = newMesh.mVertices[ 0 ].mPosition.x;
523       float right = newMesh.mVertices[ 1 ].mPosition.x;
524
525       // Check to see if there's a mesh data object that references the same atlas ?
526       uint32_t index = 0;
527       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(),
528               mEndIt = meshContainer.end();
529             mIt != mEndIt;
530             ++mIt, ++index )
531       {
532         if( slot.mAtlasId == mIt->mAtlasId )
533         {
534           // Append the mesh to the existing mesh and adjust any extents
535           Toolkit::Internal::AtlasMeshFactory::AppendMesh( mIt->mMesh, newMesh );
536
537           if( underlineGlyph )
538           {
539             AdjustExtents( extents,
540                            meshContainer,
541                            index,
542                            left,
543                            right,
544                            baseLine,
545                            underlinePosition,
546                            underlineThickness );
547           }
548
549           return;
550         }
551       }
552
553       // No mesh data object currently exists that references this atlas, so create a new one
554       MeshRecord meshRecord;
555       meshRecord.mAtlasId = slot.mAtlasId;
556       meshRecord.mMesh = newMesh;
557       meshContainer.push_back( meshRecord );
558
559       if( underlineGlyph )
560       {
561         // Adjust extents for this new meshrecord
562         AdjustExtents( extents,
563                        meshContainer,
564                        meshContainer.size() - 1u,
565                        left,
566                        right,
567                        baseLine,
568                        underlinePosition,
569                        underlineThickness );
570       }
571     }
572   }
573
574   void AdjustExtents( Vector< Extent >& extents,
575                       std::vector< MeshRecord>& meshRecords,
576                       uint32_t index,
577                       float left,
578                       float right,
579                       float baseLine,
580                       float underlinePosition,
581                       float underlineThickness )
582   {
583     bool foundExtent = false;
584     for ( Vector< Extent >::Iterator eIt = extents.Begin(),
585             eEndIt = extents.End();
586           eIt != eEndIt;
587           ++eIt )
588     {
589       if ( Equals( baseLine, eIt->mBaseLine ) )
590       {
591         foundExtent = true;
592         if ( left < eIt->mLeft )
593         {
594           eIt->mLeft = left;
595         }
596         if ( right > eIt->mRight  )
597         {
598           eIt->mRight = right;
599         }
600
601         if ( underlinePosition > eIt->mUnderlinePosition )
602         {
603           eIt->mUnderlinePosition = underlinePosition;
604         }
605         if ( underlineThickness > eIt->mUnderlineThickness )
606         {
607           eIt->mUnderlineThickness = underlineThickness;
608         }
609       }
610     }
611     if ( !foundExtent )
612     {
613       Extent extent;
614       extent.mLeft = left;
615       extent.mRight = right;
616       extent.mBaseLine = baseLine;
617       extent.mUnderlinePosition = underlinePosition;
618       extent.mUnderlineThickness = underlineThickness;
619       extent.mMeshRecordIndex = index;
620       extents.PushBack( extent );
621     }
622   }
623
624   void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
625   {
626     for( Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
627            glyphEndIt = glyphs.End();
628          glyphIt != glyphEndIt;
629          ++glyphIt )
630     {
631       const FontId fontId = (*glyphIt).fontId;
632       bool foundFont = false;
633
634       for( std::vector< MaxBlockSize >::const_iterator blockIt = mBlockSizes.begin(),
635              blockEndIt = mBlockSizes.end();
636            blockIt != blockEndIt;
637            ++blockIt )
638       {
639         if( (*blockIt).mFontId == fontId )
640         {
641           foundFont = true;
642           break;
643         }
644       }
645
646       if ( !foundFont )
647       {
648         FontMetrics fontMetrics;
649         mFontClient.GetFontMetrics( fontId, fontMetrics );
650
651         MaxBlockSize maxBlockSize;
652         maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
653         maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
654         maxBlockSize.mFontId = fontId;
655
656         mBlockSizes.push_back( maxBlockSize );
657       }
658     }
659   }
660
661   void GenerateUnderlines( std::vector< MeshRecord >& meshRecords,
662                            Vector< Extent >& extents,
663                            const Vector4& underlineColor )
664   {
665     AtlasManager::Mesh2D newMesh;
666     unsigned short faceIndex = 0;
667     for ( Vector< Extent >::ConstIterator eIt = extents.Begin(),
668             eEndIt = extents.End();
669           eIt != eEndIt;
670           ++eIt )
671     {
672       AtlasManager::Vertex2D vert;
673       uint32_t index = eIt->mMeshRecordIndex;
674       Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
675
676       // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
677       float u = HALF / uv.x;
678       float v = HALF / uv.y;
679       float thickness = eIt->mUnderlineThickness;
680       float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
681       float tlx = eIt->mLeft;
682       float brx = eIt->mRight;
683
684       vert.mPosition.x = tlx;
685       vert.mPosition.y = baseLine;
686       vert.mTexCoords.x = ZERO;
687       vert.mTexCoords.y = ZERO;
688       vert.mColor = underlineColor;
689       newMesh.mVertices.PushBack( vert );
690
691       vert.mPosition.x = brx;
692       vert.mPosition.y = baseLine;
693       vert.mTexCoords.x = u;
694       vert.mColor = underlineColor;
695       newMesh.mVertices.PushBack( vert );
696
697       vert.mPosition.x = tlx;
698       vert.mPosition.y = baseLine + thickness;
699       vert.mTexCoords.x = ZERO;
700       vert.mTexCoords.y = v;
701       vert.mColor = underlineColor;
702       newMesh.mVertices.PushBack( vert );
703
704       vert.mPosition.x = brx;
705       vert.mPosition.y = baseLine + thickness;
706       vert.mTexCoords.x = u;
707       vert.mColor = underlineColor;
708       newMesh.mVertices.PushBack( vert );
709
710       // Six indices in counter clockwise winding
711       newMesh.mIndices.PushBack( faceIndex + 1u );
712       newMesh.mIndices.PushBack( faceIndex );
713       newMesh.mIndices.PushBack( faceIndex + 2u );
714       newMesh.mIndices.PushBack( faceIndex + 2u );
715       newMesh.mIndices.PushBack( faceIndex + 3u );
716       newMesh.mIndices.PushBack( faceIndex + 1u );
717       faceIndex += 4;
718
719       Toolkit::Internal::AtlasMeshFactory::AppendMesh( meshRecords[ index ].mMesh, newMesh );
720     }
721   }
722
723   Actor mActor;                                       ///< The actor parent which renders the text
724   AtlasGlyphManager mGlyphManager;                    ///< Glyph Manager to handle upload and caching
725   TextAbstraction::FontClient mFontClient;            ///> The font client used to supply glyph information
726   std::vector< MaxBlockSize > mBlockSizes;            ///> Maximum size needed to contain a glyph in a block within a new atlas
727   Vector< TextCacheEntry > mTextCache;                ///> Caches data from previous render
728   Property::Map mQuadVertexFormat;                    ///> Describes the vertex format for text
729   int mDepth;                                         ///> DepthIndex passed by control when connect to stage
730 };
731
732 Text::RendererPtr AtlasRenderer::New()
733 {
734   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
735
736   return Text::RendererPtr( new AtlasRenderer() );
737 }
738
739 Actor AtlasRenderer::Render( Text::ViewInterface& view,
740                              float& alignmentOffset,
741                              int depth )
742 {
743   DALI_LOG_INFO( gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n" );
744
745   UnparentAndReset( mImpl->mActor );
746
747   Length numberOfGlyphs = view.GetNumberOfGlyphs();
748
749   if( numberOfGlyphs > 0u )
750   {
751     Vector<GlyphInfo> glyphs;
752     glyphs.Resize( numberOfGlyphs );
753
754     Vector<Vector2> positions;
755     positions.Resize( numberOfGlyphs );
756
757     numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
758                                      positions.Begin(),
759                                      alignmentOffset,
760                                      0u,
761                                      numberOfGlyphs );
762
763     glyphs.Resize( numberOfGlyphs );
764     positions.Resize( numberOfGlyphs );
765
766     const Vector4* const colorsBuffer = view.GetColors();
767     const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
768     const Vector4& defaultColor = view.GetTextColor();
769
770     mImpl->AddGlyphs( view,
771                       positions,
772                       glyphs,
773                       defaultColor,
774                       colorsBuffer,
775                       colorIndicesBuffer,
776                       depth,
777                       alignmentOffset );
778
779     /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
780     /* 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. */
781     if ( !mImpl->mActor )
782     {
783       mImpl->mActor = Actor::New();
784     }
785   }
786
787   return mImpl->mActor;
788 }
789
790 AtlasRenderer::AtlasRenderer()
791 {
792   mImpl = new Impl();
793
794 }
795
796 AtlasRenderer::~AtlasRenderer()
797 {
798   mImpl->RemoveText();
799   delete mImpl;
800 }