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