Fix for text renderer. Set the right size in order to cull the actor correctly.
[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& textSize( view.GetActualSize() );
180     const Vector2 halfTextSize( textSize * 0.5f );
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     const Vector4* const colorsBuffer = colors.Begin();
216
217     for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
218     {
219       const GlyphInfo& glyph = *( glyphsBuffer + i );
220
221       const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( i, underlineRuns );
222       thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
223
224       // No operation for white space
225       if( glyph.width && glyph.height )
226       {
227         // Are we still using the same fontId as previous
228         if( underlineGlyph && ( glyph.fontId != lastUnderlinedFontId ) )
229         {
230           // We need to fetch fresh font underline metrics
231           FontMetrics fontMetrics;
232           mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
233           currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
234           const float descender = ceil( fabsf( fontMetrics.descender ) );
235
236           if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
237           {
238             currentUnderlineThickness = fontMetrics.underlineThickness;
239
240             // Ensure underline will be at least a pixel high
241             if ( currentUnderlineThickness < ONE )
242             {
243               currentUnderlineThickness = ONE;
244             }
245             else
246             {
247               currentUnderlineThickness = ceil( currentUnderlineThickness );
248             }
249           }
250
251           // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
252           if( currentUnderlinePosition > descender )
253           {
254             currentUnderlinePosition = descender;
255           }
256
257           if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
258           {
259             // Move offset down by one ( EFL behavior )
260             currentUnderlinePosition = ONE;
261           }
262
263           lastUnderlinedFontId = glyph.fontId;
264         } // underline
265
266         if( !mGlyphManager.IsCached( glyph.fontId, glyph.index, slot ) )
267         {
268           // Select correct size for new atlas if needed....?
269           if( lastFontId != glyph.fontId )
270           {
271             uint32_t index = 0u;
272             for( std::vector<MaxBlockSize>::const_iterator it = mBlockSizes.begin(),
273                    endIt = mBlockSizes.end();
274                  it != endIt;
275                  ++it, ++index )
276             {
277               const MaxBlockSize& blockSize = *it;
278               if( blockSize.mFontId == glyph.fontId )
279               {
280                 currentBlockSize = index;
281                 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
282                                                DEFAULT_ATLAS_HEIGHT,
283                                                blockSize.mNeededBlockWidth,
284                                                blockSize.mNeededBlockHeight );
285               }
286             }
287           }
288
289           // Create a new image for the glyph
290           BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
291           if( bitmap )
292           {
293             MaxBlockSize& blockSize = mBlockSizes[currentBlockSize];
294
295             // Ensure that the next image will fit into the current block size
296             bool setSize = false;
297             if( bitmap.GetWidth() > blockSize.mNeededBlockWidth )
298             {
299               setSize = true;
300               blockSize.mNeededBlockWidth = bitmap.GetWidth();
301             }
302             if( bitmap.GetHeight() > blockSize.mNeededBlockHeight )
303             {
304               setSize = true;
305               blockSize.mNeededBlockHeight = bitmap.GetHeight();
306             }
307
308             if( setSize )
309             {
310               mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
311                                              DEFAULT_ATLAS_HEIGHT,
312                                              blockSize.mNeededBlockWidth,
313                                              blockSize.mNeededBlockHeight );
314             }
315
316             // Locate a new slot for our glyph
317             mGlyphManager.Add( glyph, bitmap, slot );
318           }
319         }
320         else
321         {
322           // We have 2+ copies of the same glyph
323           mGlyphManager.AdjustReferenceCount( glyph.fontId, glyph.index, 1/*increment*/ );
324         }
325
326         // Move the origin (0,0) of the mesh to the center of the actor
327         const Vector2 position = *( positionsBuffer + i ) - halfTextSize;
328
329         // Generate mesh data for this quad, plugging in our supplied position
330         AtlasManager::Mesh2D newMesh;
331         mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
332         textCacheEntry.mFontId = glyph.fontId;
333         textCacheEntry.mImageId = slot.mImageId;
334         textCacheEntry.mIndex = glyph.index;
335         newTextCache.PushBack( textCacheEntry );
336
337         AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
338
339         // Adjust the vertices if the fixed-size font should be down-scaled
340         if( glyph.scaleFactor > 0 )
341         {
342           for( unsigned int index = 0u, size = newMesh.mVertices.Count();
343                index < size;
344                ++index )
345           {
346             AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
347
348             // Set the position of the vertex.
349             vertex.mPosition.x = position.x + ( ( vertex.mPosition.x - position.x ) * glyph.scaleFactor );
350             vertex.mPosition.y = position.y + ( ( vertex.mPosition.y - position.y ) * glyph.scaleFactor );
351           }
352         }
353
354         // Get the color of the character.
355         const Vector4& color = *( colorsBuffer + i );
356
357         for( unsigned int index = 0u, size = newMesh.mVertices.Count();
358              index < size;
359              ++index )
360         {
361           AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
362
363           // Set the color of the vertex.
364           vertex.mColor = color;
365         }
366
367         // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
368         StitchTextMesh( meshContainer,
369                         newMesh,
370                         extents,
371                         position.y + glyph.yBearing,
372                         underlineGlyph,
373                         currentUnderlinePosition,
374                         currentUnderlineThickness,
375                         slot );
376         lastFontId = glyph.fontId;
377       }
378     } // glyphs
379
380     // Now remove references for the old text
381     RemoveText();
382     mTextCache.Swap( newTextCache );
383
384     if( thereAreUnderlinedGlyphs )
385     {
386       // Check to see if any of the text needs an underline
387       GenerateUnderlines( meshContainer, extents, underlineColor );
388     }
389
390     // For each MeshData object, create a mesh actor and add to the renderable actor
391     if( !meshContainer.empty() )
392     {
393       for( std::vector< MeshRecord >::iterator it = meshContainer.begin(),
394               endIt = meshContainer.end();
395             it != endIt; ++it )
396       {
397         MeshRecord& meshRecord = *it;
398
399         Actor actor = CreateMeshActor( meshRecord, textSize );
400
401         // Create an effect if necessary
402         if( style == STYLE_DROP_SHADOW )
403         {
404           // Change the color of the vertices.
405           for( Vector<AtlasManager::Vertex2D>::Iterator vIt =  meshRecord.mMesh.mVertices.Begin(),
406                  vEndIt = meshRecord.mMesh.mVertices.End();
407                vIt != vEndIt;
408                ++vIt )
409           {
410             AtlasManager::Vertex2D& vertex = *vIt;
411
412             vertex.mColor = shadowColor;
413           }
414
415           // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
416           Actor containerActor = Actor::New();
417           containerActor.SetParentOrigin( ParentOrigin::CENTER );
418           containerActor.SetSize( actorSize );
419
420           Actor shadowActor = CreateMeshActor( meshRecord, textSize );
421 #if defined(DEBUG_ENABLED)
422           shadowActor.SetName( "Text Shadow renderable actor" );
423 #endif
424           // Offset shadow in x and y
425           shadowActor.RegisterProperty("uOffset", shadowOffset );
426           if( actor.GetRendererCount() )
427           {
428             Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) );
429             int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
430             renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 );
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 );
483     PropertyBuffer quadIndices = PropertyBuffer::New( mQuadIndexFormat );
484     quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ), meshRecord.mMesh.mVertices.Size() );
485     quadIndices.SetData(  const_cast< unsigned int* >(           &meshRecord.mMesh.mIndices[ 0 ] ),  meshRecord.mMesh.mIndices.Size() );
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.SetProperty( Dali::Renderer::Property::BLENDING_MODE, BlendingMode::ON );
494     renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth );
495     Actor actor = Actor::New();
496 #if defined(DEBUG_ENABLED)
497     actor.SetName( "Text renderable actor" );
498 #endif
499     actor.AddRenderer( renderer );
500
501     // Keep all of the origins aligned
502     actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
503     actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
504
505     actor.SetSize( actorSize );
506     actor.RegisterProperty("uOffset", Vector2::ZERO );
507     return actor;
508   }
509
510   void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
511                        AtlasManager::Mesh2D& newMesh,
512                        Vector< Extent >& extents,
513                        float baseLine,
514                        bool underlineGlyph,
515                        float underlinePosition,
516                        float underlineThickness,
517                        AtlasManager::AtlasSlot& slot )
518   {
519     if ( slot.mImageId )
520     {
521       float left = newMesh.mVertices[ 0 ].mPosition.x;
522       float right = newMesh.mVertices[ 1 ].mPosition.x;
523
524       // Check to see if there's a mesh data object that references the same atlas ?
525       uint32_t index = 0;
526       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(),
527               mEndIt = meshContainer.end();
528             mIt != mEndIt;
529             ++mIt, ++index )
530       {
531         if( slot.mAtlasId == mIt->mAtlasId )
532         {
533           // Append the mesh to the existing mesh and adjust any extents
534           Toolkit::Internal::AtlasMeshFactory::AppendMesh( mIt->mMesh, newMesh );
535
536           if( underlineGlyph )
537           {
538             AdjustExtents( extents,
539                            meshContainer,
540                            index,
541                            left,
542                            right,
543                            baseLine,
544                            underlinePosition,
545                            underlineThickness );
546           }
547
548           return;
549         }
550       }
551
552       // No mesh data object currently exists that references this atlas, so create a new one
553       MeshRecord meshRecord;
554       meshRecord.mAtlasId = slot.mAtlasId;
555       meshRecord.mMesh = newMesh;
556       meshContainer.push_back( meshRecord );
557
558       if( underlineGlyph )
559       {
560         // Adjust extents for this new meshrecord
561         AdjustExtents( extents,
562                        meshContainer,
563                        meshContainer.size() - 1u,
564                        left,
565                        right,
566                        baseLine,
567                        underlinePosition,
568                        underlineThickness );
569       }
570     }
571   }
572
573   void AdjustExtents( Vector< Extent >& extents,
574                       std::vector< MeshRecord>& meshRecords,
575                       uint32_t index,
576                       float left,
577                       float right,
578                       float baseLine,
579                       float underlinePosition,
580                       float underlineThickness )
581   {
582     bool foundExtent = false;
583     for ( Vector< Extent >::Iterator eIt = extents.Begin(),
584             eEndIt = extents.End();
585           eIt != eEndIt;
586           ++eIt )
587     {
588       if ( Equals( baseLine, eIt->mBaseLine ) )
589       {
590         foundExtent = true;
591         if ( left < eIt->mLeft )
592         {
593           eIt->mLeft = left;
594         }
595         if ( right > eIt->mRight  )
596         {
597           eIt->mRight = right;
598         }
599
600         if ( underlinePosition > eIt->mUnderlinePosition )
601         {
602           eIt->mUnderlinePosition = underlinePosition;
603         }
604         if ( underlineThickness > eIt->mUnderlineThickness )
605         {
606           eIt->mUnderlineThickness = underlineThickness;
607         }
608       }
609     }
610     if ( !foundExtent )
611     {
612       Extent extent;
613       extent.mLeft = left;
614       extent.mRight = right;
615       extent.mBaseLine = baseLine;
616       extent.mUnderlinePosition = underlinePosition;
617       extent.mUnderlineThickness = underlineThickness;
618       extent.mMeshRecordIndex = index;
619       extents.PushBack( extent );
620     }
621   }
622
623   void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
624   {
625     for( Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
626            glyphEndIt = glyphs.End();
627          glyphIt != glyphEndIt;
628          ++glyphIt )
629     {
630       const FontId fontId = (*glyphIt).fontId;
631       bool foundFont = false;
632
633       for( std::vector< MaxBlockSize >::const_iterator blockIt = mBlockSizes.begin(),
634              blockEndIt = mBlockSizes.end();
635            blockIt != blockEndIt;
636            ++blockIt )
637       {
638         if( (*blockIt).mFontId == fontId )
639         {
640           foundFont = true;
641           break;
642         }
643       }
644
645       if ( !foundFont )
646       {
647         FontMetrics fontMetrics;
648         mFontClient.GetFontMetrics( fontId, fontMetrics );
649
650         MaxBlockSize maxBlockSize;
651         maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
652         maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
653         maxBlockSize.mFontId = fontId;
654
655         mBlockSizes.push_back( maxBlockSize );
656       }
657     }
658   }
659
660   void GenerateUnderlines( std::vector< MeshRecord >& meshRecords,
661                            Vector< Extent >& extents,
662                            const Vector4& underlineColor )
663   {
664     AtlasManager::Mesh2D newMesh;
665     unsigned short faceIndex = 0;
666     for ( Vector< Extent >::ConstIterator eIt = extents.Begin(),
667             eEndIt = extents.End();
668           eIt != eEndIt;
669           ++eIt )
670     {
671       AtlasManager::Vertex2D vert;
672       uint32_t index = eIt->mMeshRecordIndex;
673       Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
674
675       // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
676       float u = HALF / uv.x;
677       float v = HALF / uv.y;
678       float thickness = eIt->mUnderlineThickness;
679       float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
680       float tlx = eIt->mLeft;
681       float brx = eIt->mRight;
682
683       vert.mPosition.x = tlx;
684       vert.mPosition.y = baseLine;
685       vert.mTexCoords.x = ZERO;
686       vert.mTexCoords.y = ZERO;
687       newMesh.mVertices.PushBack( vert );
688
689       vert.mPosition.x = brx;
690       vert.mPosition.y = baseLine;
691       vert.mTexCoords.x = u;
692       newMesh.mVertices.PushBack( vert );
693
694       vert.mPosition.x = tlx;
695       vert.mPosition.y = baseLine + thickness;
696       vert.mTexCoords.x = ZERO;
697       vert.mTexCoords.y = v;
698       newMesh.mVertices.PushBack( vert );
699
700       vert.mPosition.x = brx;
701       vert.mPosition.y = baseLine + thickness;
702       vert.mTexCoords.x = u;
703       newMesh.mVertices.PushBack( vert );
704
705       // Six indices in counter clockwise winding
706       newMesh.mIndices.PushBack( faceIndex + 1u );
707       newMesh.mIndices.PushBack( faceIndex );
708       newMesh.mIndices.PushBack( faceIndex + 2u );
709       newMesh.mIndices.PushBack( faceIndex + 2u );
710       newMesh.mIndices.PushBack( faceIndex + 3u );
711       newMesh.mIndices.PushBack( faceIndex + 1u );
712       faceIndex += 4;
713
714       vert.mColor = underlineColor;
715
716       Toolkit::Internal::AtlasMeshFactory::AppendMesh( meshRecords[ index ].mMesh, newMesh );
717     }
718   }
719
720   Actor mActor;                                       ///< The actor parent which renders the text
721   AtlasGlyphManager mGlyphManager;                    ///< Glyph Manager to handle upload and caching
722   TextAbstraction::FontClient mFontClient;            ///> The font client used to supply glyph information
723   std::vector< MaxBlockSize > mBlockSizes;            ///> Maximum size needed to contain a glyph in a block within a new atlas
724   Vector< TextCacheEntry > mTextCache;                ///> Caches data from previous render
725   Property::Map mQuadVertexFormat;                    ///> Describes the vertex format for text
726   Property::Map mQuadIndexFormat;                     ///> Describes the index format for text
727   int mDepth;                                         ///> DepthIndex passed by control when connect to stage
728 };
729
730 Text::RendererPtr AtlasRenderer::New()
731 {
732   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
733
734   return Text::RendererPtr( new AtlasRenderer() );
735 }
736
737 Actor AtlasRenderer::Render( Text::ViewInterface& view, int depth )
738 {
739   UnparentAndReset( mImpl->mActor );
740
741   Length numberOfGlyphs = view.GetNumberOfGlyphs();
742
743   if( numberOfGlyphs > 0u )
744   {
745     Vector<GlyphInfo> glyphs;
746     glyphs.Resize( numberOfGlyphs );
747
748     Vector<Vector2> positions;
749     positions.Resize( numberOfGlyphs );
750
751     Vector<Vector4> colors;
752     colors.Resize( numberOfGlyphs, view.GetTextColor() );
753
754     numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
755                                      positions.Begin(),
756                                      colors.Begin(),
757                                      0u,
758                                      numberOfGlyphs );
759
760     glyphs.Resize( numberOfGlyphs );
761     positions.Resize( numberOfGlyphs );
762     colors.Resize( numberOfGlyphs );
763
764     mImpl->AddGlyphs( view,
765                       positions,
766                       glyphs,
767                       colors,
768                       depth );
769
770     /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
771     /* 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. */
772     if ( !mImpl->mActor )
773     {
774       mImpl->mActor = Actor::New();
775     }
776   }
777
778   return mImpl->mActor;
779 }
780
781 AtlasRenderer::AtlasRenderer()
782 {
783   mImpl = new Impl();
784
785 }
786
787 AtlasRenderer::~AtlasRenderer()
788 {
789   mImpl->RemoveText();
790   delete mImpl;
791 }