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