Merge "Fixed an incorrect ellipsis" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / text-atlas-renderer.cpp
1 /*
2  * Copyright (c) 2019 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/public-api/rendering/geometry.h>
23 #include <dali/public-api/rendering/renderer.h>
24 #include <dali/devel-api/text-abstraction/font-client.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/animation/constraints.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/internal/text/glyph-run.h>
31 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
32 #include <dali-toolkit/internal/text/rendering/atlas/atlas-mesh-factory.h>
33 #include <dali-toolkit/internal/text/text-view.h>
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37 using namespace Dali::Toolkit::Text;
38
39 namespace
40 {
41 #if defined(DEBUG_ENABLED)
42   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_RENDERING");
43 #endif
44
45 #define MAKE_SHADER(A)#A
46
47 const char* VERTEX_SHADER = MAKE_SHADER(
48 attribute mediump vec2    aPosition;
49 attribute mediump vec2    aTexCoord;
50 attribute mediump vec4    aColor;
51 uniform   mediump vec2    uOffset;
52 uniform     highp mat4    uMvpMatrix;
53 varying   mediump vec2    vTexCoord;
54 varying   mediump vec4    vColor;
55
56 void main()
57 {
58   mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 );
59   gl_Position = uMvpMatrix * position;
60   vTexCoord = aTexCoord;
61   vColor = aColor;
62 }
63 );
64
65 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
66 uniform lowp    vec4      uColor;
67 uniform lowp    vec4      textColorAnimatable;
68 uniform         sampler2D sTexture;
69 varying mediump vec2      vTexCoord;
70 varying mediump vec4      vColor;
71
72 void main()
73 {
74   mediump vec4 color = texture2D( sTexture, vTexCoord );
75   gl_FragColor = vec4( vColor.rgb * uColor.rgb * textColorAnimatable.rgb, uColor.a * vColor.a * textColorAnimatable.a * color.r );
76 }
77 );
78
79 const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER(
80 uniform lowp    vec4      uColor;
81 uniform lowp    vec4      textColorAnimatable;
82 uniform         sampler2D sTexture;
83 varying mediump vec2      vTexCoord;
84
85 void main()
86 {
87   gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * textColorAnimatable;
88 }
89 );
90
91 const float ZERO( 0.0f );
92 const float HALF( 0.5f );
93 const float ONE( 1.0f );
94 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
95 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
96 const uint16_t NO_OUTLINE = 0u;
97 }
98
99 struct AtlasRenderer::Impl
100 {
101   enum Style
102   {
103     STYLE_NORMAL,
104     STYLE_DROP_SHADOW
105   };
106
107   struct MeshRecord
108   {
109     MeshRecord()
110     : mAtlasId( 0u )
111     {
112     }
113
114     uint32_t mAtlasId;
115     AtlasManager::Mesh2D mMesh;
116   };
117
118   /**
119    * brief Struct used to generate the underline mesh.
120    * There is one Extent per line of text.
121    */
122   struct Extent
123   {
124     Extent()
125     : mBaseLine( 0.0f ),
126       mLeft( 0.0f ),
127       mRight( 0.0f ),
128       mUnderlinePosition( 0.0f ),
129       mUnderlineThickness( 0.0f ),
130       mMeshRecordIndex( 0u )
131     {
132     }
133
134     float mBaseLine;
135     float mLeft;
136     float mRight;
137     float mUnderlinePosition;
138     float mUnderlineThickness;
139     uint32_t mMeshRecordIndex;
140   };
141
142   struct MaxBlockSize
143   {
144     MaxBlockSize()
145     : mFontId( 0 ),
146       mNeededBlockWidth( 0 ),
147       mNeededBlockHeight( 0 )
148     {
149     }
150
151     FontId mFontId;
152     uint32_t mNeededBlockWidth;
153     uint32_t mNeededBlockHeight;
154   };
155
156   struct CheckEntry
157   {
158     CheckEntry()
159     : mFontId( 0 ),
160       mIndex( 0 )
161     {
162     }
163
164     FontId mFontId;
165     Text::GlyphIndex mIndex;
166   };
167
168   struct TextCacheEntry
169   {
170     TextCacheEntry()
171     : mFontId{ 0u },
172       mIndex{ 0u },
173       mImageId{ 0u },
174       mOutlineWidth{ 0u },
175       isItalic{ false },
176       isBold{ false }
177     {
178     }
179
180     FontId mFontId;
181     Text::GlyphIndex mIndex;
182     uint32_t mImageId;
183     uint16_t mOutlineWidth;
184     bool isItalic:1;
185     bool isBold:1;
186   };
187
188   Impl()
189   : mDepth( 0 )
190   {
191     mGlyphManager = AtlasGlyphManager::Get();
192     mFontClient = TextAbstraction::FontClient::Get();
193
194     mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
195     mQuadVertexFormat[ "aTexCoord" ] = Property::VECTOR2;
196     mQuadVertexFormat[ "aColor" ] = Property::VECTOR4;
197   }
198
199   bool IsGlyphUnderlined( GlyphIndex index,
200                           const Vector<GlyphRun>& underlineRuns )
201   {
202     for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
203            endIt = underlineRuns.End();
204            it != endIt;
205          ++it )
206     {
207       const GlyphRun& run = *it;
208
209       if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
210       {
211         return true;
212       }
213     }
214
215     return false;
216   }
217
218   void CacheGlyph( const GlyphInfo& glyph, FontId lastFontId, const AtlasGlyphManager::GlyphStyle& style, AtlasManager::AtlasSlot& slot )
219   {
220     const bool glyphNotCached = !mGlyphManager.IsCached( glyph.fontId, glyph.index, style, slot );  // Check FontGlyphRecord vector for entry with glyph index and fontId
221
222     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "AddGlyphs fontID[%u] glyphIndex[%u] [%s]\n", glyph.fontId, glyph.index, (glyphNotCached)?"not cached":"cached" );
223
224     if( glyphNotCached )
225     {
226       MaxBlockSize& blockSize = mBlockSizes[0u];
227
228       if ( lastFontId != glyph.fontId )
229       {
230         uint32_t index = 0u;
231         // Looks through all stored block sizes until finds the one which mataches required glyph font it.  Ensures new atlas block size will match existing for same font id.
232         // CalculateBlocksSize() above ensures a block size entry exists.
233         for( std::vector<MaxBlockSize>::const_iterator it = mBlockSizes.begin(),
234                endIt = mBlockSizes.end();
235              it != endIt;
236              ++it, ++index )
237         {
238           const MaxBlockSize& blockSizeEntry = *it;
239           if( blockSizeEntry.mFontId == glyph.fontId )
240           {
241             blockSize = mBlockSizes[index];
242           }
243         }
244       }
245
246       // Create a new image for the glyph
247       PixelData bitmap;
248
249       // Whether the glyph is an outline.
250       const bool isOutline = 0u != style.outline;
251
252       // Whether the current glyph is a color one.
253       const bool isColorGlyph = mFontClient.IsColorGlyph( glyph.fontId, glyph.index );
254
255       if( !isOutline || ( isOutline && !isColorGlyph) )
256       {
257         // Retrieve the emoji's bitmap.
258         TextAbstraction::FontClient::GlyphBufferData glyphBufferData;
259         glyphBufferData.width = isColorGlyph ? glyph.width : 0;   // Desired width and height.
260         glyphBufferData.height = isColorGlyph ? glyph.height : 0;
261
262         mFontClient.CreateBitmap( glyph.fontId,
263                                   glyph.index,
264                                   glyph.isItalicRequired,
265                                   glyph.isBoldRequired,
266                                   glyphBufferData,
267                                   style.outline );
268
269         // Create the pixel data.
270         bitmap = PixelData::New( glyphBufferData.buffer,
271                                  glyphBufferData.width * glyphBufferData.height * GetBytesPerPixel( glyphBufferData.format ),
272                                  glyphBufferData.width,
273                                  glyphBufferData.height,
274                                  glyphBufferData.format,
275                                  PixelData::DELETE_ARRAY );
276
277         if( bitmap )
278         {
279           // Ensure that the next image will fit into the current block size
280           if( bitmap.GetWidth() > blockSize.mNeededBlockWidth )
281           {
282             blockSize.mNeededBlockWidth = bitmap.GetWidth();
283           }
284
285           if( bitmap.GetHeight() > blockSize.mNeededBlockHeight )
286           {
287             blockSize.mNeededBlockHeight = bitmap.GetHeight();
288           }
289
290           // If CheckAtlas in AtlasManager::Add can't fit the bitmap in the current atlas it will create a new atlas
291
292           // Setting the block size and size of new atlas does not mean a new one will be created. An existing atlas may still surffice.
293           mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
294                                          DEFAULT_ATLAS_HEIGHT,
295                                          blockSize.mNeededBlockWidth,
296                                          blockSize.mNeededBlockHeight );
297
298           // Locate a new slot for our glyph
299           mGlyphManager.Add( glyph, style, bitmap, slot ); // slot will be 0 is glyph not added
300         }
301       }
302     }
303     else
304     {
305       // We have 2+ copies of the same glyph
306       mGlyphManager.AdjustReferenceCount( glyph.fontId, glyph.index, style, 1 ); //increment
307     }
308   }
309
310   void GenerateMesh( const GlyphInfo& glyph,
311                      const Vector2& position,
312                      const Vector4& color,
313                      uint16_t outline,
314                      AtlasManager::AtlasSlot& slot,
315                      bool underlineGlyph,
316                      float currentUnderlinePosition,
317                      float currentUnderlineThickness,
318                      std::vector<MeshRecord>& meshContainer,
319                      Vector<TextCacheEntry>& newTextCache,
320                      Vector<Extent>& extents )
321   {
322     // Generate mesh data for this quad, plugging in our supplied position
323     AtlasManager::Mesh2D newMesh;
324     mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
325
326     TextCacheEntry textCacheEntry;
327     textCacheEntry.mFontId = glyph.fontId;
328     textCacheEntry.mImageId = slot.mImageId;
329     textCacheEntry.mIndex = glyph.index;
330     textCacheEntry.mOutlineWidth = outline;
331     textCacheEntry.isItalic = glyph.isItalicRequired;
332     textCacheEntry.isBold = glyph.isBoldRequired;
333
334     newTextCache.PushBack( textCacheEntry );
335
336     AtlasManager::Vertex2D* verticesBuffer = newMesh.mVertices.Begin();
337
338     for( unsigned int index = 0u, size = newMesh.mVertices.Count();
339          index < size;
340          ++index )
341     {
342       AtlasManager::Vertex2D& vertex = *( verticesBuffer + index );
343
344       // Set the color of the vertex.
345       vertex.mColor = color;
346     }
347
348     // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
349     StitchTextMesh( meshContainer,
350                     newMesh,
351                     extents,
352                     position.y + glyph.yBearing,
353                     underlineGlyph,
354                     currentUnderlinePosition,
355                     currentUnderlineThickness,
356                     slot );
357   }
358
359   void CreateActors( const std::vector<MeshRecord>& meshContainer,
360                      const Size& textSize,
361                      const Vector4& color,
362                      const Vector4& shadowColor,
363                      const Vector2& shadowOffset,
364                      Actor textControl,
365                      Property::Index animatablePropertyIndex,
366                      bool drawShadow )
367   {
368     if( !mActor )
369     {
370       // Create a container actor to act as a common parent for text and shadow, to avoid color inheritence issues.
371       mActor = Actor::New();
372       mActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
373       mActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
374       mActor.SetSize( textSize );
375       mActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
376     }
377
378     for( std::vector< MeshRecord >::const_iterator it = meshContainer.begin(),
379            endIt = meshContainer.end();
380          it != endIt; ++it )
381     {
382       const MeshRecord& meshRecord = *it;
383
384       Actor actor = CreateMeshActor( textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_NORMAL );
385
386       // Whether the actor has renderers.
387       const bool hasRenderer = actor.GetRendererCount() > 0u;
388
389       // Create an effect if necessary
390       if( hasRenderer &&
391           drawShadow )
392       {
393         // Change the color of the vertices.
394         for( Vector<AtlasManager::Vertex2D>::Iterator vIt =  meshRecord.mMesh.mVertices.Begin(),
395                vEndIt = meshRecord.mMesh.mVertices.End();
396              vIt != vEndIt;
397              ++vIt )
398         {
399           AtlasManager::Vertex2D& vertex = *vIt;
400
401           vertex.mColor = shadowColor;
402         }
403
404         Actor shadowActor = CreateMeshActor(textControl, animatablePropertyIndex, color, meshRecord, textSize, STYLE_DROP_SHADOW );
405 #if defined(DEBUG_ENABLED)
406         shadowActor.SetName( "Text Shadow renderable actor" );
407 #endif
408         // Offset shadow in x and y
409         shadowActor.RegisterProperty("uOffset", shadowOffset );
410         Dali::Renderer renderer( shadowActor.GetRendererAt( 0 ) );
411         int depthIndex = renderer.GetProperty<int>(Dali::Renderer::Property::DEPTH_INDEX);
412         renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, depthIndex - 1 );
413         mActor.Add( shadowActor );
414       }
415
416       if( hasRenderer )
417       {
418         mActor.Add( actor );
419       }
420     }
421   }
422
423   void AddGlyphs( Text::ViewInterface& view,
424                   Actor textControl,
425                   Property::Index animatablePropertyIndex,
426                   const Vector<Vector2>& positions,
427                   const Vector<GlyphInfo>& glyphs,
428                   const Vector4& defaultColor,
429                   const Vector4* const colorsBuffer,
430                   const ColorIndex* const colorIndicesBuffer,
431                   int depth,
432                   float minLineOffset )
433   {
434     AtlasManager::AtlasSlot slot;
435     slot.mImageId = 0u;
436     slot.mAtlasId = 0u;
437
438     AtlasManager::AtlasSlot slotOutline;
439     slotOutline.mImageId = 0u;
440     slotOutline.mAtlasId = 0u;
441
442     std::vector< MeshRecord > meshContainer;
443     std::vector< MeshRecord > meshContainerOutline;
444     Vector< Extent > extents;
445     mDepth = depth;
446
447     const Vector2& textSize( view.GetLayoutSize() );
448     const Vector2 halfTextSize( textSize * 0.5f );
449     const Vector2& shadowOffset( view.GetShadowOffset() );
450     const Vector4& shadowColor( view.GetShadowColor() );
451     const bool underlineEnabled = view.IsUnderlineEnabled();
452     const Vector4& underlineColor( view.GetUnderlineColor() );
453     const float underlineHeight = view.GetUnderlineHeight();
454     const uint16_t outlineWidth = view.GetOutlineWidth();
455     const Vector4& outlineColor( view.GetOutlineColor() );
456     const bool isOutline = 0u != outlineWidth;
457
458     const bool useDefaultColor = ( NULL == colorsBuffer );
459
460     // Get the underline runs.
461     const Length numberOfUnderlineRuns = view.GetNumberOfUnderlineRuns();
462     Vector<GlyphRun> underlineRuns;
463     underlineRuns.Resize( numberOfUnderlineRuns );
464     view.GetUnderlineRuns( underlineRuns.Begin(),
465                            0u,
466                            numberOfUnderlineRuns );
467
468     bool thereAreUnderlinedGlyphs = false;
469
470     float currentUnderlinePosition = ZERO;
471     float currentUnderlineThickness = underlineHeight;
472     FontId lastFontId = 0;
473     FontId lastUnderlinedFontId = 0;
474     Style style = STYLE_NORMAL;
475
476     if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
477     {
478       style = STYLE_DROP_SHADOW;
479     }
480
481     CalculateBlocksSize( glyphs );
482
483     // Avoid emptying mTextCache (& removing references) until after incremented references for the new text
484     Vector< TextCacheEntry > newTextCache;
485     const GlyphInfo* const glyphsBuffer = glyphs.Begin();
486     const Vector2* const positionsBuffer = positions.Begin();
487     const Vector2 lineOffsetPosition( minLineOffset, 0.f );
488
489     for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
490     {
491       const GlyphInfo& glyph = *( glyphsBuffer + i );
492       const bool isGlyphUnderlined = underlineEnabled || IsGlyphUnderlined( i, underlineRuns );
493       thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || isGlyphUnderlined;
494
495       // No operation for white space
496       if( glyph.width && glyph.height )
497       {
498         // Are we still using the same fontId as previous
499         if( isGlyphUnderlined && ( glyph.fontId != lastUnderlinedFontId ) )
500         {
501           // We need to fetch fresh font underline metrics
502           FontMetrics fontMetrics;
503           mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
504           currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
505           const float descender = ceil( fabsf( fontMetrics.descender ) );
506
507           if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
508           {
509             currentUnderlineThickness = fontMetrics.underlineThickness;
510
511             // Ensure underline will be at least a pixel high
512             if ( currentUnderlineThickness < ONE )
513             {
514               currentUnderlineThickness = ONE;
515             }
516             else
517             {
518               currentUnderlineThickness = ceil( currentUnderlineThickness );
519             }
520           }
521
522           // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
523           if( currentUnderlinePosition > descender )
524           {
525             currentUnderlinePosition = descender;
526           }
527
528           if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
529           {
530             // Move offset down by one ( EFL behavior )
531             currentUnderlinePosition = ONE;
532           }
533
534           lastUnderlinedFontId = glyph.fontId;
535         } // underline
536
537         AtlasGlyphManager::GlyphStyle style;
538         style.isItalic = glyph.isItalicRequired;
539         style.isBold = glyph.isBoldRequired;
540
541         // Retrieves and caches the glyph's bitmap.
542         CacheGlyph( glyph, lastFontId, style, slot );
543
544         // Retrieves and caches the outline glyph's bitmap.
545         if( isOutline )
546         {
547           style.outline = outlineWidth;
548           CacheGlyph( glyph, lastFontId, style, slotOutline );
549         }
550
551         // Move the origin (0,0) of the mesh to the center of the actor
552         const Vector2 position = *( positionsBuffer + i ) - halfTextSize - lineOffsetPosition;
553
554         if ( 0u != slot.mImageId ) // invalid slot id, glyph has failed to be added to atlas
555         {
556           Vector2 positionPlusOutlineOffset = position;
557           if( isOutline )
558           {
559             // Add an offset to the text.
560             const float outlineWidthOffset = static_cast<float>( outlineWidth );
561             positionPlusOutlineOffset += Vector2( outlineWidthOffset, outlineWidthOffset );
562           }
563
564           // Get the color of the character.
565           const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndicesBuffer + i );
566           const Vector4& color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + colorIndex - 1u );
567
568           GenerateMesh( glyph,
569                         positionPlusOutlineOffset,
570                         color,
571                         NO_OUTLINE,
572                         slot,
573                         isGlyphUnderlined,
574                         currentUnderlinePosition,
575                         currentUnderlineThickness,
576                         meshContainer,
577                         newTextCache,
578                         extents);
579
580           lastFontId = glyph.fontId; // Prevents searching for existing blocksizes when string of the same fontId.
581         }
582
583         if( isOutline && ( 0u != slotOutline.mImageId ) ) // invalid slot id, glyph has failed to be added to atlas
584         {
585           GenerateMesh( glyph,
586                         position,
587                         outlineColor,
588                         outlineWidth,
589                         slotOutline,
590                         false,
591                         currentUnderlinePosition,
592                         currentUnderlineThickness,
593                         meshContainerOutline,
594                         newTextCache,
595                         extents);
596         }
597       }
598     } // glyphs
599
600     // Now remove references for the old text
601     RemoveText();
602     mTextCache.Swap( newTextCache );
603
604     if( thereAreUnderlinedGlyphs )
605     {
606       // Check to see if any of the text needs an underline
607       GenerateUnderlines( meshContainer, extents, underlineColor );
608     }
609
610     // For each MeshData object, create a mesh actor and add to the renderable actor
611     bool isShadowDrawn = false;
612     if( !meshContainerOutline.empty() )
613     {
614       const bool drawShadow = STYLE_DROP_SHADOW == style;
615       CreateActors( meshContainerOutline,
616                     textSize,
617                     outlineColor,
618                     shadowColor,
619                     shadowOffset,
620                     textControl,
621                     animatablePropertyIndex,
622                     drawShadow );
623
624       isShadowDrawn = drawShadow;
625     }
626
627     // For each MeshData object, create a mesh actor and add to the renderable actor
628     if( !meshContainer.empty() )
629     {
630       const bool drawShadow = !isShadowDrawn && ( STYLE_DROP_SHADOW == style );
631       CreateActors( meshContainer,
632                     textSize,
633                     defaultColor,
634                     shadowColor,
635                     shadowOffset,
636                     textControl,
637                     animatablePropertyIndex,
638                     drawShadow );
639     }
640
641 #if defined(DEBUG_ENABLED)
642     Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
643     DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
644                                                 metrics.mGlyphCount,
645                                                 metrics.mAtlasMetrics.mAtlasCount,
646                                                 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
647
648     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "%s\n", metrics.mVerboseGlyphCounts.c_str() );
649
650     for( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
651     {
652       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "   Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
653                                                  i + 1, i > 8 ? "" : " ",
654                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8  " : "BGRA",
655                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
656                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
657                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
658                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
659                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
660                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
661     }
662 #endif
663   }
664
665   void RemoveText()
666   {
667     for( Vector< TextCacheEntry >::Iterator oldTextIter = mTextCache.Begin(); oldTextIter != mTextCache.End(); ++oldTextIter )
668     {
669       AtlasGlyphManager::GlyphStyle style;
670       style.outline = oldTextIter->mOutlineWidth;
671       style.isItalic = oldTextIter->isItalic;
672       style.isBold = oldTextIter->isBold;
673       mGlyphManager.AdjustReferenceCount( oldTextIter->mFontId, oldTextIter->mIndex, style, -1/*decrement*/ );
674     }
675     mTextCache.Resize( 0 );
676   }
677
678   Actor CreateMeshActor( Actor textControl, Property::Index animatablePropertyIndex, const Vector4& defaultColor, const MeshRecord& meshRecord,
679                          const Vector2& actorSize, Style style )
680   {
681     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat );
682     quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ), meshRecord.mMesh.mVertices.Size() );
683
684     Geometry quadGeometry = Geometry::New();
685     quadGeometry.AddVertexBuffer( quadVertices );
686     quadGeometry.SetIndexBuffer( &meshRecord.mMesh.mIndices[0],  meshRecord.mMesh.mIndices.Size() );
687
688     TextureSet textureSet( mGlyphManager.GetTextures( meshRecord.mAtlasId ) );
689
690     // Choose the shader to use.
691     const bool isColorShader = ( STYLE_DROP_SHADOW != style ) && ( Pixel::BGRA8888 == mGlyphManager.GetPixelFormat( meshRecord.mAtlasId ) );
692     Shader shader;
693     if( isColorShader )
694     {
695       // The glyph is an emoji and is not a shadow.
696       if( !mShaderRgba )
697       {
698         mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA );
699       }
700       shader = mShaderRgba;
701     }
702     else
703     {
704       // The glyph is text or a shadow.
705       if( !mShaderL8 )
706       {
707         mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
708       }
709       shader = mShaderL8;
710     }
711
712     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "defaultColor[%f, %f, %f, %f ]\n", defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a  );
713
714     Dali::Property::Index shaderTextColorIndex = shader.RegisterProperty( "textColorAnimatable", defaultColor );
715
716     if ( animatablePropertyIndex != Property::INVALID_INDEX )
717     {
718       // create constraint for the animatable text's color Property with textColorAnimatable in the shader.
719       if( shaderTextColorIndex  )
720       {
721         Constraint constraint = Constraint::New<Vector4>( shader, shaderTextColorIndex, EqualToConstraint() );
722         constraint.AddSource( Source( textControl, animatablePropertyIndex ) );
723         constraint.Apply();
724       }
725     }
726     else
727     {
728       // If not animating the text colour then set to 1's so shader uses the current vertex color
729       shader.RegisterProperty( "textColorAnimatable", Vector4(1.0, 1.0, 1.0, 1.0 ) );
730     }
731
732     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, shader );
733     renderer.SetTextures( textureSet );
734     renderer.SetProperty( Dali::Renderer::Property::BLEND_MODE, BlendMode::ON );
735     renderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT + mDepth );
736
737     Actor actor = Actor::New();
738 #if defined(DEBUG_ENABLED)
739     actor.SetName( "Text renderable actor" );
740 #endif
741     actor.AddRenderer( renderer );
742     // Keep all of the origins aligned
743     actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
744     actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
745     actor.SetSize( actorSize );
746     actor.RegisterProperty("uOffset", Vector2::ZERO );
747     actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
748
749     return actor;
750   }
751
752   void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
753                        AtlasManager::Mesh2D& newMesh,
754                        Vector< Extent >& extents,
755                        float baseLine,
756                        bool underlineGlyph,
757                        float underlinePosition,
758                        float underlineThickness,
759                        AtlasManager::AtlasSlot& slot )
760   {
761     if ( slot.mImageId )
762     {
763       float left = newMesh.mVertices[ 0 ].mPosition.x;
764       float right = newMesh.mVertices[ 1 ].mPosition.x;
765
766       // Check to see if there's a mesh data object that references the same atlas ?
767       uint32_t index = 0;
768       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(),
769               mEndIt = meshContainer.end();
770             mIt != mEndIt;
771             ++mIt, ++index )
772       {
773         if( slot.mAtlasId == mIt->mAtlasId )
774         {
775           // Append the mesh to the existing mesh and adjust any extents
776           Toolkit::Internal::AtlasMeshFactory::AppendMesh( mIt->mMesh, newMesh );
777
778           if( underlineGlyph )
779           {
780             AdjustExtents( extents,
781                            meshContainer,
782                            index,
783                            left,
784                            right,
785                            baseLine,
786                            underlinePosition,
787                            underlineThickness );
788           }
789
790           return;
791         }
792       }
793
794       // No mesh data object currently exists that references this atlas, so create a new one
795       MeshRecord meshRecord;
796       meshRecord.mAtlasId = slot.mAtlasId;
797       meshRecord.mMesh = newMesh;
798       meshContainer.push_back( meshRecord );
799
800       if( underlineGlyph )
801       {
802         // Adjust extents for this new meshrecord
803         AdjustExtents( extents,
804                        meshContainer,
805                        meshContainer.size() - 1u,
806                        left,
807                        right,
808                        baseLine,
809                        underlinePosition,
810                        underlineThickness );
811       }
812     }
813   }
814
815   void AdjustExtents( Vector< Extent >& extents,
816                       std::vector< MeshRecord>& meshRecords,
817                       uint32_t index,
818                       float left,
819                       float right,
820                       float baseLine,
821                       float underlinePosition,
822                       float underlineThickness )
823   {
824     bool foundExtent = false;
825     for ( Vector< Extent >::Iterator eIt = extents.Begin(),
826             eEndIt = extents.End();
827           eIt != eEndIt;
828           ++eIt )
829     {
830       if ( Equals( baseLine, eIt->mBaseLine ) )
831       {
832         foundExtent = true;
833         if ( left < eIt->mLeft )
834         {
835           eIt->mLeft = left;
836         }
837         if ( right > eIt->mRight  )
838         {
839           eIt->mRight = right;
840         }
841
842         if ( underlinePosition > eIt->mUnderlinePosition )
843         {
844           eIt->mUnderlinePosition = underlinePosition;
845         }
846         if ( underlineThickness > eIt->mUnderlineThickness )
847         {
848           eIt->mUnderlineThickness = underlineThickness;
849         }
850       }
851     }
852     if ( !foundExtent )
853     {
854       Extent extent;
855       extent.mLeft = left;
856       extent.mRight = right;
857       extent.mBaseLine = baseLine;
858       extent.mUnderlinePosition = underlinePosition;
859       extent.mUnderlineThickness = underlineThickness;
860       extent.mMeshRecordIndex = index;
861       extents.PushBack( extent );
862     }
863   }
864
865   void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
866   {
867     for( Vector<GlyphInfo>::ConstIterator glyphIt = glyphs.Begin(),
868            glyphEndIt = glyphs.End();
869          glyphIt != glyphEndIt;
870          ++glyphIt )
871     {
872       const FontId fontId = (*glyphIt).fontId;
873       bool foundFont = false;
874
875       for( std::vector< MaxBlockSize >::const_iterator blockIt = mBlockSizes.begin(),
876              blockEndIt = mBlockSizes.end();
877            blockIt != blockEndIt;
878            ++blockIt )
879       {
880         if( (*blockIt).mFontId == fontId )  // Different size fonts will have a different fontId
881         {
882           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize match found fontID(%u) glyphIndex(%u)\n", fontId, (*glyphIt).index );
883           foundFont = true;
884           break;
885         }
886       }
887
888       if ( !foundFont )
889       {
890         FontMetrics fontMetrics;
891         mFontClient.GetFontMetrics( fontId, fontMetrics );
892
893         MaxBlockSize maxBlockSize;
894         maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
895         maxBlockSize.mNeededBlockHeight = maxBlockSize.mNeededBlockWidth;
896         maxBlockSize.mFontId = fontId;
897         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::CalculateBlocksSize New font with no matched blocksize, setting blocksize[%u]\n", maxBlockSize.mNeededBlockWidth );
898         mBlockSizes.push_back( maxBlockSize );
899       }
900     }
901   }
902
903   void GenerateUnderlines( std::vector< MeshRecord >& meshRecords,
904                            Vector< Extent >& extents,
905                            const Vector4& underlineColor )
906   {
907     AtlasManager::Mesh2D newMesh;
908     unsigned short faceIndex = 0;
909     for ( Vector< Extent >::ConstIterator eIt = extents.Begin(),
910             eEndIt = extents.End();
911           eIt != eEndIt;
912           ++eIt )
913     {
914       AtlasManager::Vertex2D vert;
915       uint32_t index = eIt->mMeshRecordIndex;
916       Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
917
918       // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
919       float u = HALF / uv.x;
920       float v = HALF / uv.y;
921       float thickness = eIt->mUnderlineThickness;
922       float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
923       float tlx = eIt->mLeft;
924       float brx = eIt->mRight;
925
926       vert.mPosition.x = tlx;
927       vert.mPosition.y = baseLine;
928       vert.mTexCoords.x = ZERO;
929       vert.mTexCoords.y = ZERO;
930       vert.mColor = underlineColor;
931       newMesh.mVertices.PushBack( vert );
932
933       vert.mPosition.x = brx;
934       vert.mPosition.y = baseLine;
935       vert.mTexCoords.x = u;
936       vert.mColor = underlineColor;
937       newMesh.mVertices.PushBack( vert );
938
939       vert.mPosition.x = tlx;
940       vert.mPosition.y = baseLine + thickness;
941       vert.mTexCoords.x = ZERO;
942       vert.mTexCoords.y = v;
943       vert.mColor = underlineColor;
944       newMesh.mVertices.PushBack( vert );
945
946       vert.mPosition.x = brx;
947       vert.mPosition.y = baseLine + thickness;
948       vert.mTexCoords.x = u;
949       vert.mColor = underlineColor;
950       newMesh.mVertices.PushBack( vert );
951
952       // Six indices in counter clockwise winding
953       newMesh.mIndices.PushBack( faceIndex + 1u );
954       newMesh.mIndices.PushBack( faceIndex );
955       newMesh.mIndices.PushBack( faceIndex + 2u );
956       newMesh.mIndices.PushBack( faceIndex + 2u );
957       newMesh.mIndices.PushBack( faceIndex + 3u );
958       newMesh.mIndices.PushBack( faceIndex + 1u );
959       faceIndex += 4;
960
961       Toolkit::Internal::AtlasMeshFactory::AppendMesh( meshRecords[ index ].mMesh, newMesh );
962     }
963   }
964
965   Actor mActor;                                       ///< The actor parent which renders the text
966   AtlasGlyphManager mGlyphManager;                    ///< Glyph Manager to handle upload and caching
967   TextAbstraction::FontClient mFontClient;            ///< The font client used to supply glyph information
968   Shader mShaderL8;                                   ///< The shader for glyphs and emoji's shadows.
969   Shader mShaderRgba;                                 ///< The shader for emojis.
970   std::vector< MaxBlockSize > mBlockSizes;            ///< Maximum size needed to contain a glyph in a block within a new atlas
971   Vector< TextCacheEntry > mTextCache;                ///< Caches data from previous render
972   Property::Map mQuadVertexFormat;                    ///< Describes the vertex format for text
973   int mDepth;                                         ///< DepthIndex passed by control when connect to stage
974 };
975
976 Text::RendererPtr AtlasRenderer::New()
977 {
978   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
979
980   return Text::RendererPtr( new AtlasRenderer() );
981 }
982
983 Actor AtlasRenderer::Render( Text::ViewInterface& view,
984                              Actor textControl,
985                              Property::Index animatablePropertyIndex,
986                              float& alignmentOffset,
987                              int depth )
988 {
989   DALI_LOG_INFO( gLogFilter, Debug::General, "Text::AtlasRenderer::Render()\n" );
990
991   UnparentAndReset( mImpl->mActor );
992
993   Length numberOfGlyphs = view.GetNumberOfGlyphs();
994
995   if( numberOfGlyphs > 0u )
996   {
997     Vector<GlyphInfo> glyphs;
998     glyphs.Resize( numberOfGlyphs );
999
1000     Vector<Vector2> positions;
1001     positions.Resize( numberOfGlyphs );
1002
1003     numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
1004                                      positions.Begin(),
1005                                      alignmentOffset,
1006                                      0u,
1007                                      numberOfGlyphs );
1008
1009     glyphs.Resize( numberOfGlyphs );
1010     positions.Resize( numberOfGlyphs );
1011
1012     const Vector4* const colorsBuffer = view.GetColors();
1013     const ColorIndex* const colorIndicesBuffer = view.GetColorIndices();
1014     const Vector4& defaultColor = view.GetTextColor();
1015
1016     mImpl->AddGlyphs( view,
1017                       textControl,
1018                       animatablePropertyIndex,
1019                       positions,
1020                       glyphs,
1021                       defaultColor,
1022                       colorsBuffer,
1023                       colorIndicesBuffer,
1024                       depth,
1025                       alignmentOffset );
1026
1027     /* In the case where AddGlyphs does not create a renderable Actor for example when glyphs are all whitespace create a new Actor. */
1028     /* 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. */
1029     if ( !mImpl->mActor )
1030     {
1031       mImpl->mActor = Actor::New();
1032     }
1033   }
1034
1035   return mImpl->mActor;
1036 }
1037
1038 AtlasRenderer::AtlasRenderer()
1039 {
1040   mImpl = new Impl();
1041
1042 }
1043
1044 AtlasRenderer::~AtlasRenderer()
1045 {
1046   mImpl->RemoveText();
1047   delete mImpl;
1048 }