Text Shadow Implementation via copied renderer.
[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/public-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     : mColor( Color::BLACK ),
63       mAtlasId( 0 )
64     {
65     }
66
67     Vector4 mColor;
68     uint32_t mAtlasId;
69     AtlasManager::Mesh2D mMesh;
70     FrameBufferImage mBuffer;
71   };
72
73   /**
74    * brief Struct used to generate the underline mesh.
75    * There is one Extent per line of text.
76    */
77   struct Extent
78   {
79     Extent()
80     : mBaseLine( 0.0f ),
81       mLeft( 0.0f ),
82       mRight( 0.0f ),
83       mUnderlinePosition( 0.0f ),
84       mUnderlineThickness( 0.0f ),
85       mMeshRecordIndex( 0 )
86     {
87     }
88
89     float mBaseLine;
90     float mLeft;
91     float mRight;
92     float mUnderlinePosition;
93     float mUnderlineThickness;
94     uint32_t mMeshRecordIndex;
95   };
96
97   struct MaxBlockSize
98   {
99     MaxBlockSize()
100     : mFontId( 0 ),
101       mNeededBlockWidth( 0 ),
102       mNeededBlockHeight( 0 )
103     {
104     }
105
106     FontId mFontId;
107     uint32_t mNeededBlockWidth;
108     uint32_t mNeededBlockHeight;
109   };
110
111   struct CheckEntry
112   {
113     CheckEntry()
114     : mFontId( 0 ),
115       mIndex( 0 )
116     {
117     }
118
119     FontId mFontId;
120     Text::GlyphIndex mIndex;
121   };
122
123   struct TextCacheEntry
124   {
125     TextCacheEntry()
126     : mFontId( 0 ),
127       mIndex( 0 ),
128       mImageId( 0 )
129     {
130     }
131
132     FontId mFontId;
133     Text::GlyphIndex mIndex;
134     uint32_t mImageId;
135   };
136
137   Impl()
138   : mDepth( 0 )
139   {
140     mGlyphManager = AtlasGlyphManager::Get();
141     mFontClient = TextAbstraction::FontClient::Get();
142
143     mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
144     mQuadVertexFormat[ "aTexCoord" ] = Property::VECTOR2;
145     mQuadIndexFormat[ "indices" ] = Property::INTEGER;
146   }
147
148   bool IsGlyphUnderlined( GlyphIndex index,
149                           const Vector<GlyphRun>& underlineRuns )
150   {
151     for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
152            endIt = underlineRuns.End();
153            it != endIt;
154          ++it )
155     {
156       const GlyphRun& run = *it;
157
158       if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
159       {
160         return true;
161       }
162     }
163
164     return false;
165   }
166
167   void AddGlyphs( Text::ViewInterface& view,
168                   const Vector<Vector2>& positions,
169                   const Vector<GlyphInfo>& glyphs,
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 Vector4& textColor( view.GetTextColor() );
181     const Vector2& shadowOffset( view.GetShadowOffset() );
182     const Vector4& shadowColor( view.GetShadowColor() );
183     const bool underlineEnabled( view.IsUnderlineEnabled() );
184     const Vector4& underlineColor( view.GetUnderlineColor() );
185     const float underlineHeight( view.GetUnderlineHeight() );
186
187     // Get the underline runs.
188     const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
189     Vector<GlyphRun> underlineRuns;
190     underlineRuns.Resize( numberOfUnderlineRuns );
191     view.GetUnderlineRuns( underlineRuns.Begin(),
192                            0u,
193                            numberOfUnderlineRuns );
194
195     bool thereAreUnderlinedGlyphs = false;
196
197     float currentUnderlinePosition = ZERO;
198     float currentUnderlineThickness = underlineHeight;
199     uint32_t currentBlockSize = 0;
200     FontId lastFontId = 0;
201     FontId lastUnderlinedFontId = 0;
202     Style style = STYLE_NORMAL;
203
204     if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
205     {
206       style = STYLE_DROP_SHADOW;
207     }
208
209     CalculateBlocksSize( glyphs );
210
211     // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
212     Vector< TextCacheEntry > newTextCache;
213     const GlyphInfo* const glyphsBuffer = glyphs.Begin();
214     const Vector2* const positionsBuffer = positions.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 ( underlineHeight == ZERO )
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           if ( ZERO == currentUnderlinePosition )
256           {
257             // Move offset down by one ( EFL behavior )
258             currentUnderlinePosition = ONE;
259           }
260
261           lastUnderlinedFontId = glyph.fontId;
262         } // underline
263
264         if ( !mGlyphManager.IsCached( glyph.fontId, glyph.index, slot ) )
265         {
266           // Select correct size for new atlas if needed....?
267           if ( lastFontId != glyph.fontId )
268           {
269             for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
270             {
271               if ( mBlockSizes[ j ].mFontId == glyph.fontId )
272               {
273                 currentBlockSize = j;
274                 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
275                                                DEFAULT_ATLAS_HEIGHT,
276                                                mBlockSizes[ j ].mNeededBlockWidth,
277                                                mBlockSizes[ j ].mNeededBlockHeight );
278               }
279             }
280           }
281
282           // Create a new image for the glyph
283           BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
284           if ( bitmap )
285           {
286             // Ensure that the next image will fit into the current block size
287             bool setSize = false;
288             if ( bitmap.GetWidth() > mBlockSizes[ currentBlockSize ].mNeededBlockWidth )
289             {
290               setSize = true;
291               mBlockSizes[ currentBlockSize ].mNeededBlockWidth = bitmap.GetWidth();
292             }
293             if ( bitmap.GetHeight() > mBlockSizes[ currentBlockSize ].mNeededBlockHeight )
294             {
295               setSize = true;
296               mBlockSizes[ currentBlockSize ].mNeededBlockHeight = bitmap.GetHeight();
297             }
298
299             if ( setSize )
300             {
301               mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
302                                              DEFAULT_ATLAS_HEIGHT,
303                                              mBlockSizes[ currentBlockSize ].mNeededBlockWidth,
304                                              mBlockSizes[ currentBlockSize ].mNeededBlockHeight );
305             }
306
307             // Locate a new slot for our glyph
308             mGlyphManager.Add( glyph, bitmap, slot );
309           }
310         }
311         else
312         {
313           // We have 2+ copies of the same glyph
314           mGlyphManager.AdjustReferenceCount( glyph.fontId, glyph.index, 1/*increment*/ );
315         }
316
317         // Move the origin (0,0) of the mesh to the center of the actor
318         Vector2 position = *( positionsBuffer + i ) - halfActorSize;
319
320         // Generate mesh data for this quad, plugging in our supplied position
321         AtlasManager::Mesh2D newMesh;
322         mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
323         textCacheEntry.mFontId = glyph.fontId;
324         textCacheEntry.mImageId = slot.mImageId;
325         textCacheEntry.mIndex = glyph.index;
326         newTextCache.PushBack( textCacheEntry );
327
328         // Adjust the vertices if the fixed-size font should be down-scaled
329         if( glyph.scaleFactor > 0 )
330         {
331           for( unsigned int i=0; i<newMesh.mVertices.Count(); ++i )
332           {
333             newMesh.mVertices[i].mPosition.x = position.x + ( ( newMesh.mVertices[i].mPosition.x - position.x ) * glyph.scaleFactor );
334             newMesh.mVertices[i].mPosition.y = position.y + ( ( newMesh.mVertices[i].mPosition.y - position.y ) * glyph.scaleFactor );
335           }
336         }
337
338         // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
339         StitchTextMesh( meshContainer,
340                         newMesh,
341                         extents,
342                         textColor,
343                         position.y + glyph.yBearing,
344                         underlineGlyph,
345                         currentUnderlinePosition,
346                         currentUnderlineThickness,
347                         slot );
348         lastFontId = glyph.fontId;
349       }
350     } // glyphs
351
352     // Now remove references for the old text
353     RemoveText();
354     mTextCache.Swap( newTextCache );
355
356     if( thereAreUnderlinedGlyphs )
357     {
358       // Check to see if any of the text needs an underline
359       GenerateUnderlines( meshContainer, extents, underlineColor, textColor );
360     }
361
362     // For each MeshData object, create a mesh actor and add to the renderable actor
363     if ( meshContainer.size() )
364     {
365       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt )
366       {
367         Actor actor = CreateMeshActor( *mIt, actorSize );
368
369         // Create an effect if necessary
370         if ( style == STYLE_DROP_SHADOW )
371         {
372           // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
373           Actor containerActor = Actor::New();
374           containerActor.SetParentOrigin( ParentOrigin::CENTER );
375           containerActor.SetSize( actorSize );
376
377           Actor shadowActor = Actor::New();
378 #if defined(DEBUG_ENABLED)
379           shadowActor.SetName( "Text Shadow renderable actor" );
380 #endif
381           // Offset shadow in x and y
382           shadowActor.RegisterProperty("uOffset", shadowOffset );
383           if ( actor.GetRendererCount() )
384           {
385             Dali::Renderer renderer( actor.GetRendererAt( 0 ) );
386             Geometry geometry = renderer.GetGeometry();
387             Material material = renderer.GetMaterial();
388
389             Dali::Renderer shadowRenderer = Dali::Renderer::New( geometry, material );
390             shadowRenderer.SetDepthIndex( renderer.GetDepthIndex() - 1 );
391             shadowActor.AddRenderer( shadowRenderer );
392             shadowActor.SetParentOrigin( ParentOrigin::CENTER );
393             shadowActor.SetSize( actorSize );
394             shadowActor.SetColor( shadowColor );
395             containerActor.Add( shadowActor );
396             containerActor.Add( actor );
397             actor = containerActor;
398           }
399         }
400
401         if( mActor )
402         {
403           mActor.Add( actor );
404         }
405         else
406         {
407           mActor = actor;
408         }
409       }
410     }
411 #if defined(DEBUG_ENABLED)
412     Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
413     DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
414                                                 metrics.mGlyphCount,
415                                                 metrics.mAtlasMetrics.mAtlasCount,
416                                                 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
417
418     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str() );
419
420     for ( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
421     {
422       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "   Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
423                                                  i + 1, i > 8 ? "" : " ",
424                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8  " : "BGRA",
425                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
426                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
427                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
428                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
429                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
430                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
431     }
432 #endif
433   }
434
435   void RemoveText()
436   {
437     for ( Vector< TextCacheEntry >::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter )
438     {
439       mGlyphManager.AdjustReferenceCount( oldTextIter->mFontId, oldTextIter->mIndex, -1/*decrement*/ );
440     }
441     mTextCache.Resize( 0 );
442   }
443
444   Actor CreateMeshActor( const MeshRecord& meshRecord, const Vector2& actorSize )
445   {
446     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat, meshRecord.mMesh.mVertices.Size() );
447     PropertyBuffer quadIndices = PropertyBuffer::New( mQuadIndexFormat, meshRecord.mMesh.mIndices.Size() );
448     quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ) );
449     quadIndices.SetData( const_cast< unsigned int* >( &meshRecord.mMesh.mIndices[ 0 ] ) );
450
451     Geometry quadGeometry = Geometry::New();
452     quadGeometry.AddVertexBuffer( quadVertices );
453     quadGeometry.SetIndexBuffer( quadIndices );
454
455     Material material = mGlyphManager.GetMaterial( meshRecord.mAtlasId );
456     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, material );
457     renderer.SetDepthIndex( CONTENT_DEPTH_INDEX + mDepth );
458     Actor actor = Actor::New();
459 #if defined(DEBUG_ENABLED)
460     actor.SetName( "Text renderable actor" );
461 #endif
462     actor.AddRenderer( renderer );
463     actor.SetParentOrigin( ParentOrigin::CENTER ); // Keep all of the origins aligned
464     actor.SetSize( actorSize );
465     actor.SetColor( meshRecord.mColor );
466     actor.RegisterProperty("uOffset", Vector2::ZERO );
467     return actor;
468   }
469
470   void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
471                        AtlasManager::Mesh2D& newMesh,
472                        Vector< Extent >& extents,
473                        const Vector4& color,
474                        float baseLine,
475                        bool underlineGlyph,
476                        float underlinePosition,
477                        float underlineThickness,
478                        AtlasManager::AtlasSlot& slot )
479   {
480     if ( slot.mImageId )
481     {
482       float left = newMesh.mVertices[ 0 ].mPosition.x;
483       float right = newMesh.mVertices[ 1 ].mPosition.x;
484
485       // Check to see if there's a mesh data object that references the same atlas ?
486       uint32_t index = 0;
487       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(),
488               mEndIt = meshContainer.end();
489             mIt != mEndIt;
490             ++mIt, ++index )
491       {
492         if ( slot.mAtlasId == mIt->mAtlasId && color == mIt->mColor )
493         {
494           // Append the mesh to the existing mesh and adjust any extents
495           Toolkit::Internal::AtlasMeshFactory::AppendMesh( mIt->mMesh, newMesh );
496
497           if( underlineGlyph )
498           {
499             AdjustExtents( extents,
500                            meshContainer,
501                            index,
502                            left,
503                            right,
504                            baseLine,
505                            underlinePosition,
506                            underlineThickness );
507           }
508
509           return;
510         }
511       }
512
513       // No mesh data object currently exists that references this atlas, so create a new one
514       MeshRecord meshRecord;
515       meshRecord.mAtlasId = slot.mAtlasId;
516       meshRecord.mMesh = newMesh;
517       meshRecord.mColor = color;
518       meshContainer.push_back( meshRecord );
519
520       if( underlineGlyph )
521       {
522         // Adjust extents for this new meshrecord
523         AdjustExtents( extents,
524                        meshContainer,
525                        meshContainer.size() - 1u,
526                        left,
527                        right,
528                        baseLine,
529                        underlinePosition,
530                        underlineThickness );
531       }
532     }
533   }
534
535   void AdjustExtents( Vector< Extent >& extents,
536                       std::vector< MeshRecord>& meshRecords,
537                       uint32_t index,
538                       float left,
539                       float right,
540                       float baseLine,
541                       float underlinePosition,
542                       float underlineThickness )
543   {
544     bool foundExtent = false;
545     for ( Vector< Extent >::Iterator eIt = extents.Begin(),
546             eEndIt = extents.End();
547           eIt != eEndIt;
548           ++eIt )
549     {
550       if ( Equals( baseLine, eIt->mBaseLine ) )
551       {
552         foundExtent = true;
553         if ( left < eIt->mLeft )
554         {
555           eIt->mLeft = left;
556         }
557         if ( right > eIt->mRight  )
558         {
559           eIt->mRight = right;
560         }
561
562         if ( underlinePosition > eIt->mUnderlinePosition )
563         {
564           eIt->mUnderlinePosition = underlinePosition;
565         }
566         if ( underlineThickness > eIt->mUnderlineThickness )
567         {
568           eIt->mUnderlineThickness = underlineThickness;
569         }
570       }
571     }
572     if ( !foundExtent )
573     {
574       Extent extent;
575       extent.mLeft = left;
576       extent.mRight = right;
577       extent.mBaseLine = baseLine;
578       extent.mUnderlinePosition = underlinePosition;
579       extent.mUnderlineThickness = underlineThickness;
580       extent.mMeshRecordIndex = index;
581       extents.PushBack( extent );
582     }
583   }
584
585   void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
586   {
587     MaxBlockSize maxBlockSize;
588     for ( uint32_t i = 0; i < glyphs.Size(); ++i )
589     {
590       FontId fontId = glyphs[ i ].fontId;
591       bool foundFont = false;
592       for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
593       {
594         if ( mBlockSizes[ j ].mFontId == fontId )
595         {
596           foundFont = true;
597         }
598       }
599       if ( !foundFont )
600       {
601         FontMetrics fontMetrics;
602         mFontClient.GetFontMetrics( fontId, fontMetrics );
603         maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
604         maxBlockSize.mNeededBlockHeight = static_cast< uint32_t >( fontMetrics.height );
605         maxBlockSize.mFontId = fontId;
606         mBlockSizes.push_back( maxBlockSize );
607       }
608     }
609   }
610
611   void GenerateUnderlines( std::vector< MeshRecord >& meshRecords,
612                            Vector< Extent >& extents,
613                            const Vector4& underlineColor,
614                            const Vector4& textColor )
615   {
616     AtlasManager::Mesh2D newMesh;
617     unsigned short faceIndex = 0;
618     for ( Vector< Extent >::ConstIterator eIt = extents.Begin(),
619             eEndIt = extents.End();
620           eIt != eEndIt;
621           ++eIt )
622     {
623       AtlasManager::Vertex2D vert;
624       uint32_t index = eIt->mMeshRecordIndex;
625       Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
626
627       // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
628       float u = HALF / uv.x;
629       float v = HALF / uv.y;
630       float thickness = eIt->mUnderlineThickness;
631       float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
632       float tlx = eIt->mLeft;
633       float brx = eIt->mRight;
634
635       vert.mPosition.x = tlx;
636       vert.mPosition.y = baseLine;
637       vert.mTexCoords.x = ZERO;
638       vert.mTexCoords.y = ZERO;
639       newMesh.mVertices.PushBack( vert );
640
641       vert.mPosition.x = brx;
642       vert.mPosition.y = baseLine;
643       vert.mTexCoords.x = u;
644       newMesh.mVertices.PushBack( vert );
645
646       vert.mPosition.x = tlx;
647       vert.mPosition.y = baseLine + thickness;
648       vert.mTexCoords.x = ZERO;
649       vert.mTexCoords.y = v;
650       newMesh.mVertices.PushBack( vert );
651
652       vert.mPosition.x = brx;
653       vert.mPosition.y = baseLine + thickness;
654       vert.mTexCoords.x = u;
655       newMesh.mVertices.PushBack( vert );
656
657       // Six indices in counter clockwise winding
658       newMesh.mIndices.PushBack( faceIndex + 1u );
659       newMesh.mIndices.PushBack( faceIndex );
660       newMesh.mIndices.PushBack( faceIndex + 2u );
661       newMesh.mIndices.PushBack( faceIndex + 2u );
662       newMesh.mIndices.PushBack( faceIndex + 3u );
663       newMesh.mIndices.PushBack( faceIndex + 1u );
664       faceIndex += 4;
665
666       if ( underlineColor == textColor )
667       {
668         Toolkit::Internal::AtlasMeshFactory::AppendMesh( meshRecords[ index ].mMesh, newMesh );
669       }
670       else
671       {
672         MeshRecord record;
673         record.mMesh = newMesh;
674         record.mAtlasId = meshRecords[ index ].mAtlasId;
675         record.mColor = underlineColor;
676         meshRecords.push_back( record );
677       }
678     }
679   }
680
681   Actor mActor;                                       ///< The actor parent which renders the text
682   AtlasGlyphManager mGlyphManager;                    ///< Glyph Manager to handle upload and caching
683   TextAbstraction::FontClient mFontClient;            ///> The font client used to supply glyph information
684   std::vector< MaxBlockSize > mBlockSizes;            ///> Maximum size needed to contain a glyph in a block within a new atlas
685   Vector< TextCacheEntry > mTextCache;                ///> Caches data from previous render
686   Property::Map mQuadVertexFormat;                    ///> Describes the vertex format for text
687   Property::Map mQuadIndexFormat;                     ///> Describes the index format for text
688   int mDepth;                                         ///> DepthIndex passed by control when connect to stage
689 };
690
691 Text::RendererPtr AtlasRenderer::New()
692 {
693   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
694
695   return Text::RendererPtr( new AtlasRenderer() );
696 }
697
698 Actor AtlasRenderer::Render( Text::ViewInterface& view, int depth )
699 {
700   UnparentAndReset( mImpl->mActor );
701
702   Length numberOfGlyphs = view.GetNumberOfGlyphs();
703
704   if( numberOfGlyphs > 0u )
705   {
706     Vector<GlyphInfo> glyphs;
707     glyphs.Resize( numberOfGlyphs );
708
709     Vector<Vector2> positions;
710     positions.Resize( numberOfGlyphs );
711
712     numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
713                                      positions.Begin(),
714                                      0u,
715                                      numberOfGlyphs );
716     glyphs.Resize( numberOfGlyphs );
717     positions.Resize( numberOfGlyphs );
718
719     mImpl->AddGlyphs( view,
720                       positions,
721                       glyphs,
722                       depth );
723   }
724
725   return mImpl->mActor;
726 }
727
728 AtlasRenderer::AtlasRenderer()
729 {
730   mImpl = new Impl();
731
732 }
733
734 AtlasRenderer::~AtlasRenderer()
735 {
736   mImpl->RemoveText();
737   delete mImpl;
738 }