Uses depth within tree to set the depth index of text controls and their renderers.
[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/dali.h>
23 #include <dali/devel-api/object/property-buffer.h>
24 #include <dali/devel-api/rendering/geometry.h>
25 #include <dali/devel-api/rendering/renderer.h>
26 #include <dali/devel-api/rendering/sampler.h>
27 #include <dali/devel-api/rendering/shader.h>
28 #include <dali/devel-api/text-abstraction/font-client.h>
29 #include <dali/integration-api/debug.h>
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.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 float TWO( 2.0f );
48 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
49 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
50 }
51 struct AtlasRenderer::Impl : public ConnectionTracker
52 {
53
54   enum Style
55   {
56     STYLE_NORMAL,
57     STYLE_DROP_SHADOW
58   };
59
60   struct MeshRecord
61   {
62     Vector4 mColor;
63     uint32_t mAtlasId;
64     AtlasManager::Mesh2D mMesh;
65     FrameBufferImage mBuffer;
66     bool mIsUnderline;
67   };
68
69   struct Extent
70   {
71     float mBaseLine;
72     float mLeft;
73     float mRight;
74     float mUnderlinePosition;
75     float mUnderlineThickness;
76     uint32_t mMeshRecordIndex;
77   };
78
79   struct AtlasRecord
80   {
81     uint32_t mImageId;
82     Text::GlyphIndex mIndex;
83   };
84
85   struct MaxBlockSize
86   {
87     FontId mFontId;
88     uint32_t mNeededBlockWidth;
89     uint32_t mNeededBlockHeight;
90   };
91
92   Impl()
93   {
94     mGlyphManager = AtlasGlyphManager::Get();
95     mFontClient = TextAbstraction::FontClient::Get();
96
97     mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
98     mQuadVertexFormat[ "aTexCoord" ] = Property::VECTOR2;
99     mQuadIndexFormat[ "indices" ] = Property::UNSIGNED_INTEGER;
100   }
101
102   void AddGlyphs( const std::vector<Vector2>& positions,
103                   const Vector<GlyphInfo>& glyphs,
104                   const Vector4& textColor,
105                   const Vector2& shadowOffset,
106                   const Vector4& shadowColor,
107                   bool underlineEnabled,
108                   const Vector4& underlineColor,
109                   float underlineHeight,
110                   unsigned int depth )
111   {
112     AtlasManager::AtlasSlot slot;
113     std::vector< MeshRecord > meshContainer;
114     Vector< Extent > extents;
115     mDepth = static_cast< int >( depth );
116
117     float currentUnderlinePosition = ZERO;
118     float currentUnderlineThickness = underlineHeight;
119     uint32_t currentBlockSize = 0;
120     FontId lastFontId = 0;
121     Style style = STYLE_NORMAL;
122
123     if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
124     {
125       style = STYLE_DROP_SHADOW;
126     }
127
128     if ( mImageIds.Size() )
129     {
130       // Unreference any currently used glyphs
131       RemoveText();
132     }
133
134     CalculateBlocksSize( glyphs );
135
136     for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
137     {
138       const GlyphInfo& glyph = glyphs[ i ];
139
140       // No operation for white space
141       if ( glyph.width && glyph.height )
142       {
143         // Are we still using the same fontId as previous
144         if ( glyph.fontId != lastFontId )
145         {
146           // We need to fetch fresh font underline metrics
147           FontMetrics fontMetrics;
148           mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
149           currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
150           float descender = ceil( fabsf( fontMetrics.descender ) );
151
152           if ( underlineHeight == ZERO )
153           {
154             currentUnderlineThickness = fontMetrics.underlineThickness;
155
156             // Ensure underline will be at least a pixel high
157             if ( currentUnderlineThickness < ONE )
158             {
159               currentUnderlineThickness = ONE;
160             }
161             else
162             {
163               currentUnderlineThickness = ceil( currentUnderlineThickness );
164             }
165           }
166
167           // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
168           if ( currentUnderlinePosition > descender )
169           {
170             currentUnderlinePosition = descender;
171           }
172           if ( ZERO == currentUnderlinePosition )
173           {
174             // Move offset down by one ( EFL behavior )
175             currentUnderlinePosition = ONE;
176           }
177         }
178
179         const Vector2& position = positions[ i ];
180         AtlasManager::Mesh2D newMesh;
181         mGlyphManager.Cached( glyph.fontId, glyph.index, slot );
182
183         if ( slot.mImageId )
184         {
185           // This glyph already exists so generate mesh data plugging in our supplied position
186           mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
187           mImageIds.PushBack( slot.mImageId );
188         }
189         else
190         {
191
192           // Select correct size for new atlas if needed....?
193           if ( lastFontId != glyph.fontId )
194           {
195             for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
196             {
197               if ( mBlockSizes[ j ].mFontId == glyph.fontId )
198               {
199                 currentBlockSize = j;
200                 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
201                                                DEFAULT_ATLAS_HEIGHT,
202                                                mBlockSizes[ j ].mNeededBlockWidth,
203                                                mBlockSizes[ j ].mNeededBlockHeight );
204               }
205             }
206           }
207
208           // Create a new image for the glyph
209           BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
210           if ( bitmap )
211           {
212             // Ensure that the next image will fit into the current block size
213             bool setSize = false;
214             if ( bitmap.GetWidth() > mBlockSizes[ currentBlockSize ].mNeededBlockWidth )
215             {
216               setSize = true;
217               mBlockSizes[ currentBlockSize ].mNeededBlockWidth = bitmap.GetWidth();
218             }
219             if ( bitmap.GetHeight() > mBlockSizes[ currentBlockSize ].mNeededBlockHeight )
220             {
221               setSize = true;
222               mBlockSizes[ currentBlockSize ].mNeededBlockHeight = bitmap.GetHeight();
223             }
224
225             if ( setSize )
226             {
227               mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
228                                              DEFAULT_ATLAS_HEIGHT,
229                                              mBlockSizes[ currentBlockSize ].mNeededBlockWidth,
230                                              mBlockSizes[ currentBlockSize ].mNeededBlockHeight );
231             }
232
233             // Locate a new slot for our glyph
234             mGlyphManager.Add( glyph, bitmap, slot );
235
236             // Generate mesh data for this quad, plugging in our supplied position
237             if ( slot.mImageId )
238             {
239               mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
240               mImageIds.PushBack( slot.mImageId );
241             }
242           }
243         }
244         // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
245         StitchTextMesh( meshContainer,
246                         newMesh,
247                         extents,
248                         textColor,
249                         position.y + glyph.yBearing,
250                         currentUnderlinePosition,
251                         currentUnderlineThickness,
252                         slot );
253        lastFontId = glyph.fontId;
254       }
255     }
256
257     if ( underlineEnabled )
258     {
259       // Check to see if any of the text needs an underline
260       GenerateUnderlines( meshContainer, extents, underlineColor, textColor );
261     }
262
263     // For each MeshData object, create a mesh actor and add to the renderable actor
264     if ( meshContainer.size() )
265     {
266       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt )
267       {
268         Actor actor = CreateMeshActor( *mIt );
269
270         // Create an effect if necessary
271         if ( style == STYLE_DROP_SHADOW )
272         {
273           actor.Add( GenerateShadow( *mIt, shadowOffset, shadowColor ) );
274         }
275
276         if ( mActor )
277         {
278           mActor.Add( actor );
279         }
280         else
281         {
282           mActor = actor;
283         }
284       }
285       mActor.OffStageSignal().Connect( this, &AtlasRenderer::Impl::OffStageDisconnect );
286     }
287 #if defined(DEBUG_ENABLED)
288     Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
289     DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
290                                                 metrics.mGlyphCount,
291                                                 metrics.mAtlasMetrics.mAtlasCount,
292                                                 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
293     for ( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
294     {
295       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
296                                                  i + 1, i > 8 ? "" : " ",
297                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8  " : "BGRA",
298                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
299                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
300                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
301                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
302                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
303                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
304     }
305 #endif
306   }
307
308   Actor CreateMeshActor( const MeshRecord& meshRecord )
309   {
310     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat, meshRecord.mMesh.mVertices.Size() );
311     PropertyBuffer quadIndices = PropertyBuffer::New( mQuadIndexFormat, meshRecord.mMesh.mIndices.Size() );
312     quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ) );
313     quadIndices.SetData( const_cast< unsigned int* >( &meshRecord.mMesh.mIndices[ 0 ] ) );
314
315     Geometry quadGeometry = Geometry::New();
316     quadGeometry.AddVertexBuffer( quadVertices );
317     quadGeometry.SetIndexBuffer( quadIndices );
318
319     Material material = mGlyphManager.GetMaterial( meshRecord.mAtlasId );
320     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, material );
321     renderer.SetDepthIndex( mDepth );
322     Actor actor = Actor::New();
323     actor.AddRenderer( renderer );
324     actor.SetSize( 1.0f, 1.0f );
325     actor.SetColor( meshRecord.mColor );
326
327     if ( meshRecord.mIsUnderline )
328     {
329       actor.SetColorMode( USE_OWN_COLOR );
330     }
331     else
332     {
333       actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
334     }
335     return actor;
336   }
337
338   void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
339                        AtlasManager::Mesh2D& newMesh,
340                        Vector< Extent >& extents,
341                        const Vector4& color,
342                        float baseLine,
343                        float underlinePosition,
344                        float underlineThickness,
345                        AtlasManager::AtlasSlot& slot )
346   {
347     if ( slot.mImageId )
348     {
349       float left = newMesh.mVertices[ 0 ].mPosition.x;
350       float right = newMesh.mVertices[ 1 ].mPosition.x;
351
352       // Check to see if there's a mesh data object that references the same atlas ?
353       uint32_t index = 0;
354       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt, ++index )
355       {
356         if ( slot.mAtlasId == mIt->mAtlasId && color == mIt->mColor )
357         {
358           // Stitch the mesh to the existing mesh and adjust any extents
359           mGlyphManager.StitchMesh( mIt->mMesh, newMesh );
360           AdjustExtents( extents,
361                          meshContainer,
362                          index,
363                          left,
364                          right,
365                          baseLine,
366                          underlinePosition,
367                          underlineThickness );
368           return;
369         }
370       }
371
372       // No mesh data object currently exists that references this atlas, so create a new one
373       MeshRecord meshRecord;
374       meshRecord.mAtlasId = slot.mAtlasId;
375       meshRecord.mMesh = newMesh;
376       meshRecord.mColor = color;
377       meshRecord.mIsUnderline = false;
378       meshContainer.push_back( meshRecord );
379
380       // Adjust extents for this new meshrecord
381       AdjustExtents( extents,
382                      meshContainer,
383                      meshContainer.size() - 1u,
384                      left,
385                      right,
386                      baseLine,
387                      underlinePosition,
388                      underlineThickness );
389
390     }
391   }
392
393   void AdjustExtents( Vector< Extent >& extents,
394                       std::vector< MeshRecord>& meshRecords,
395                       uint32_t index,
396                       float left,
397                       float right,
398                       float baseLine,
399                       float underlinePosition,
400                       float underlineThickness )
401   {
402     bool foundExtent = false;
403     for ( Vector< Extent >::Iterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
404     {
405       if ( Equals( baseLine, eIt->mBaseLine ) )
406       {
407         foundExtent = true;
408         if ( left < eIt->mLeft )
409         {
410           eIt->mLeft = left;
411         }
412         if ( right > eIt->mRight  )
413         {
414           eIt->mRight = right;
415         }
416
417         if ( underlinePosition > eIt->mUnderlinePosition )
418         {
419           eIt->mUnderlinePosition = underlinePosition;
420         }
421         if ( underlineThickness > eIt->mUnderlineThickness )
422         {
423           eIt->mUnderlineThickness = underlineThickness;
424         }
425       }
426     }
427     if ( !foundExtent )
428     {
429       Extent extent;
430       extent.mLeft = left;
431       extent.mRight = right;
432       extent.mBaseLine = baseLine;
433       extent.mUnderlinePosition = underlinePosition;
434       extent.mUnderlineThickness = underlineThickness;
435       extent.mMeshRecordIndex = index;
436       extents.PushBack( extent );
437     }
438   }
439
440   // Unreference any glyphs that were used with this actor
441   void OffStageDisconnect( Dali::Actor actor )
442   {
443     RemoveText();
444   }
445
446   void RemoveText()
447   {
448     for ( uint32_t i = 0; i < mImageIds.Size(); ++i )
449     {
450       mGlyphManager.Remove( mImageIds[ i ] );
451     }
452     mImageIds.Resize( 0 );
453   }
454
455   void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
456   {
457     MaxBlockSize maxBlockSize;
458     for ( uint32_t i = 0; i < glyphs.Size(); ++i )
459     {
460       FontId fontId = glyphs[ i ].fontId;
461       bool foundFont = false;
462       for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
463       {
464         if ( mBlockSizes[ j ].mFontId == fontId )
465         {
466           foundFont = true;
467         }
468       }
469       if ( !foundFont )
470       {
471         FontMetrics fontMetrics;
472         mFontClient.GetFontMetrics( fontId, fontMetrics );
473         maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
474         maxBlockSize.mNeededBlockHeight = static_cast< uint32_t >( fontMetrics.height );
475         maxBlockSize.mFontId = fontId;
476         mBlockSizes.push_back( maxBlockSize );
477       }
478     }
479   }
480
481   void GenerateUnderlines( std::vector< MeshRecord>& meshRecords,
482                            Vector< Extent >& extents,
483                            const Vector4& underlineColor,
484                            const Vector4& textColor )
485   {
486     AtlasManager::Mesh2D newMesh;
487     unsigned short faceIndex = 0;
488     for ( Vector< Extent >::ConstIterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
489     {
490       AtlasManager::Vertex2D vert;
491       uint32_t index = eIt->mMeshRecordIndex;
492       Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
493
494       // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
495       float u = HALF / uv.x;
496       float v = HALF / uv.y;
497       float thickness = eIt->mUnderlineThickness;
498       float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
499       float tlx = eIt->mLeft;
500       float brx = eIt->mRight;
501
502       vert.mPosition.x = tlx;
503       vert.mPosition.y = baseLine;
504       vert.mTexCoords.x = ZERO;
505       vert.mTexCoords.y = ZERO;
506       newMesh.mVertices.PushBack( vert );
507
508       vert.mPosition.x = brx;
509       vert.mPosition.y = baseLine;
510       vert.mTexCoords.x = u;
511       newMesh.mVertices.PushBack( vert );
512
513       vert.mPosition.x = tlx;
514       vert.mPosition.y = baseLine + thickness;
515       vert.mTexCoords.x = ZERO;
516       vert.mTexCoords.y = v;
517       newMesh.mVertices.PushBack( vert );
518
519       vert.mPosition.x = brx;
520       vert.mPosition.y = baseLine + thickness;
521       vert.mTexCoords.x = u;
522       newMesh.mVertices.PushBack( vert );
523
524       // Six indices in counter clockwise winding
525       newMesh.mIndices.PushBack( faceIndex + 1u );
526       newMesh.mIndices.PushBack( faceIndex );
527       newMesh.mIndices.PushBack( faceIndex + 2u );
528       newMesh.mIndices.PushBack( faceIndex + 2u );
529       newMesh.mIndices.PushBack( faceIndex + 3u );
530       newMesh.mIndices.PushBack( faceIndex + 1u );
531       faceIndex += 4;
532
533       if ( underlineColor == textColor )
534       {
535         mGlyphManager.StitchMesh( meshRecords[ index ].mMesh, newMesh );
536       }
537       else
538       {
539         MeshRecord record;
540         record.mMesh = newMesh;
541         record.mAtlasId = meshRecords[ index ].mAtlasId;
542         record.mColor = underlineColor;
543         record.mIsUnderline = true;
544         meshRecords.push_back( record );
545       }
546     }
547   }
548
549   Actor GenerateShadow( MeshRecord& meshRecord,
550                         const Vector2& shadowOffset,
551                         const Vector4& shadowColor )
552   {
553     // Scan vertex buffer to determine width and height of effect buffer needed
554     const Vector< AtlasManager::Vertex2D >& verts = meshRecord.mMesh.mVertices;
555     float tlx = verts[ 0 ].mPosition.x;
556     float tly = verts[ 0 ].mPosition.y;
557     float brx = ZERO;
558     float bry = ZERO;
559
560     for ( uint32_t i = 0; i < verts.Size(); ++i )
561     {
562       if ( verts[ i ].mPosition.x < tlx )
563       {
564         tlx = verts[ i ].mPosition.x;
565       }
566       if ( verts[ i ].mPosition.y < tly )
567       {
568         tly = verts[ i ].mPosition.y;
569       }
570       if ( verts[ i ].mPosition.x > brx )
571       {
572         brx = verts[ i ].mPosition.x;
573       }
574       if ( verts[ i ].mPosition.y > bry )
575       {
576         bry = verts[ i ].mPosition.y;
577       }
578     }
579
580     float width = brx - tlx;
581     float height = bry - tly;
582     float divWidth = TWO / width;
583     float divHeight = TWO / height;
584
585     // Create a buffer to render to
586     meshRecord.mBuffer = FrameBufferImage::New( width, height );
587
588     // We will render a quad into this buffer
589     unsigned int indices[ 6 ] = { 1, 0, 2, 2, 3, 1 };
590     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat, 4u );
591     PropertyBuffer quadIndices = PropertyBuffer::New( mQuadIndexFormat, sizeof(indices)/sizeof(indices[0]) );
592
593     AtlasManager::Vertex2D vertices[ 4 ] = {
594     { Vector2( tlx + shadowOffset.x, tly + shadowOffset.y ), Vector2( ZERO, ZERO ) },
595     { Vector2( brx + shadowOffset.x, tly + shadowOffset.y ), Vector2( ONE, ZERO ) },
596     { Vector2( tlx + shadowOffset.x, bry + shadowOffset.y ), Vector2( ZERO, ONE ) },
597     { Vector2( brx + shadowOffset.x, bry + shadowOffset.y ), Vector2( ONE, ONE ) } };
598
599     quadVertices.SetData( vertices );
600     quadIndices.SetData( indices );
601
602     Geometry quadGeometry = Geometry::New();
603     quadGeometry.AddVertexBuffer( quadVertices );
604     quadGeometry.SetIndexBuffer( quadIndices );
605
606     Sampler sampler = Sampler::New( meshRecord.mBuffer, "sTexture" );
607     Material material = Material::New( mGlyphManager.GetEffectBufferShader() );
608     material.AddSampler( sampler );
609
610     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, material );
611
612     // Ensure shadow is behind the text...
613     renderer.SetDepthIndex( mDepth + CONTENT_DEPTH_INDEX - 1 );
614     Actor actor = Actor::New();
615     actor.AddRenderer( renderer );
616     actor.SetSize( 1.0f, 1.0f );
617
618     // Create a sub actor to render the source with normalized vertex positions
619     Vector< AtlasManager::Vertex2D > normVertexList;
620     for ( uint32_t i = 0; i < verts.Size(); ++i )
621     {
622       AtlasManager::Vertex2D vertex = verts[ i ];
623       vertex.mPosition.x = ( ( vertex.mPosition.x - tlx ) * divWidth ) - ONE;
624       vertex.mPosition.y = ( ( vertex.mPosition.y - tly ) * divHeight ) - ONE;
625       normVertexList.PushBack( vertex );
626     }
627
628     PropertyBuffer normVertices = PropertyBuffer::New( mQuadVertexFormat, normVertexList.Size() );
629     PropertyBuffer normIndices = PropertyBuffer::New( mQuadIndexFormat, meshRecord.mMesh.mIndices.Size() );
630     normVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &normVertexList[ 0 ] ) );
631     normIndices.SetData( const_cast< unsigned int* >( &meshRecord.mMesh.mIndices[ 0 ] ) );
632
633     Geometry normGeometry = Geometry::New();
634     normGeometry.AddVertexBuffer( normVertices );
635     normGeometry.SetIndexBuffer( normIndices );
636
637     Material normMaterial = Material::New( mGlyphManager.GetGlyphShadowShader() );
638     Sampler normSampler =  mGlyphManager.GetSampler( meshRecord.mAtlasId );
639     normMaterial.AddSampler( normSampler );
640     Dali::Renderer normRenderer = Dali::Renderer::New( normGeometry, normMaterial );
641     Actor subActor = Actor::New();
642     subActor.AddRenderer( normRenderer );
643     subActor.SetSize( 1.0f, 1.0f );
644     subActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
645     subActor.SetColor( shadowColor );
646
647     // Create a render task to render the effect
648     RenderTask task = Stage::GetCurrent().GetRenderTaskList().CreateTask();
649     task.SetTargetFrameBuffer( meshRecord.mBuffer );
650     task.SetSourceActor( subActor );
651     task.SetClearEnabled( true );
652     task.SetClearColor( Vector4::ZERO );
653     task.SetExclusive( true );
654     task.SetRefreshRate( RenderTask::REFRESH_ONCE );
655     task.FinishedSignal().Connect( this, &AtlasRenderer::Impl::RenderComplete );
656     actor.Add( subActor );
657
658     return actor;
659   }
660
661   void RenderComplete( RenderTask& renderTask )
662   {
663     // Disconnect and remove this single shot render task
664     renderTask.FinishedSignal().Disconnect( this, &AtlasRenderer::Impl::RenderComplete );
665     Stage::GetCurrent().GetRenderTaskList().RemoveTask( renderTask );
666
667     // Get the actor used for render to buffer and remove it from the parent
668     Actor renderActor = renderTask.GetSourceActor();
669     if ( renderActor )
670     {
671       Actor parent = renderActor.GetParent();
672       if ( parent )
673       {
674         parent.Remove( renderActor );
675       }
676     }
677   }
678
679   Actor mActor;                                       ///< The actor parent which renders the text
680   AtlasGlyphManager mGlyphManager;                    ///< Glyph Manager to handle upload and caching
681   Vector< uint32_t > mImageIds;                       ///< A list of imageIDs used by the renderer
682   TextAbstraction::FontClient mFontClient;            ///> The font client used to supply glyph information
683   std::vector< MaxBlockSize > mBlockSizes;            ///> Maximum size needed to contain a glyph in a block within a new atlas
684   std::vector< uint32_t > mFace;                      ///> Face indices for a quad
685   Property::Map mQuadVertexFormat;
686   Property::Map mQuadIndexFormat;
687   int mDepth;
688 };
689
690 Text::RendererPtr AtlasRenderer::New()
691 {
692   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
693
694   return Text::RendererPtr( new AtlasRenderer() );
695 }
696
697 Actor AtlasRenderer::Render( Text::ViewInterface& view, unsigned int depth )
698 {
699   UnparentAndReset( mImpl->mActor );
700
701   Length numberOfGlyphs = view.GetNumberOfGlyphs();
702
703   if( numberOfGlyphs > 0u )
704   {
705     Vector<GlyphInfo> glyphs;
706     glyphs.Resize( numberOfGlyphs );
707
708     std::vector<Vector2> positions;
709     positions.resize( numberOfGlyphs );
710
711     numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
712                                      &positions[0],
713                                      0u,
714                                      numberOfGlyphs );
715     glyphs.Resize( numberOfGlyphs );
716     positions.resize( numberOfGlyphs );
717
718     mImpl->AddGlyphs( positions,
719                       glyphs,
720                       view.GetTextColor(),
721                       view.GetShadowOffset(),
722                       view.GetShadowColor(),
723                       view.IsUnderlineEnabled(),
724                       view.GetUnderlineColor(),
725                       view.GetUnderlineHeight(),
726                       depth );
727   }
728
729   return mImpl->mActor;
730 }
731
732 AtlasRenderer::AtlasRenderer()
733 {
734   mImpl = new Impl();
735
736 }
737
738 AtlasRenderer::~AtlasRenderer()
739 {
740   delete mImpl;
741 }