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