Refactor TextLabel to use text visual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.cpp
1 /*
2  * Copyright (c) 2017 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/visuals/text/text-visual.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/constraints.h>
23
24 // INTERNAL HEADER
25 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
26 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
27 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
28 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
29 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/text/text-font-style.h>
33 #include <dali-toolkit/internal/text/text-effects-style.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 namespace
45 {
46
47 // Property names - common properties defined in visual-string-constants.h/cpp
48 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
49 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
50 const char * const POINT_SIZE_PROPERTY( "pointSize" );
51 const char * const MULTI_LINE_PROPERTY( "multiLine" );
52 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
53 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
54 const char * const TEXT_COLOR_PROPERTY( "textColor" );
55 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
56 const char * const SHADOW_PROPERTY( "shadow" );
57 const char * const UNDERLINE_PROPERTY( "underline" );
58
59 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
60 {
61   { "BEGIN",  Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN  },
62   { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
63   { "END",    Toolkit::Text::Layout::HORIZONTAL_ALIGN_END    },
64 };
65 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
66
67 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
68 {
69   { "TOP",    Toolkit::Text::Layout::VERTICAL_ALIGN_TOP    },
70   { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
71   { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
72 };
73 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
74
75 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
76
77 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
78 {
79   const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
80                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE,
81                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
82
83   return std::string( name );
84 }
85
86 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
87 {
88   const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
89                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE,
90                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
91
92   return std::string( name );
93 }
94
95 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
96   attribute mediump vec2 aPosition;\n
97   uniform mediump mat4 uMvpMatrix;\n
98   uniform mediump vec3 uSize;\n
99   uniform mediump vec4 pixelArea;\n
100
101   uniform mediump mat4 uModelMatrix;\n
102   uniform mediump mat4 uViewMatrix;\n
103   uniform mediump mat4 uProjection;\n
104
105   varying mediump vec2 vTexCoord;\n
106
107   //Visual size and offset
108   uniform mediump vec2 offset;\n
109   uniform mediump vec2 size;\n
110   uniform mediump vec4 offsetSizeMode;\n
111   uniform mediump vec2 origin;\n
112   uniform mediump vec2 anchorPoint;\n
113
114   vec4 ComputeVertexPosition()\n
115   {\n
116     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
117     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
118     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
119   }\n
120
121   void main()\n
122   {\n
123     mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
124     mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
125     mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
126
127     vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
128     gl_Position = vertexPosition;\n
129   }\n
130 );
131
132 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
133   varying mediump vec2 vTexCoord;\n
134   uniform sampler2D sTexture;\n
135   uniform sampler2D sStyle;\n
136   uniform sampler2D sMask;\n
137   uniform lowp float uHasMultipleTextColors;\n
138   uniform lowp vec4 uTextColorAnimatable;\n
139   uniform mediump vec4 uAtlasRect;\n
140   uniform lowp vec4 uColor;\n
141   uniform lowp vec3 mixColor;\n
142   uniform lowp float opacity;\n
143   \n
144   void main()\n
145   {\n
146     mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
147     mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
148     mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
149     mediump vec4 maskTexture = texture2D( sMask, texCoord );\n
150
151     // Set the color of non-transparent pixel in text to what it is animated to.
152     // Markup text with multiple text colors are not animated (but can be supported later on if required).
153     // Emoji color are not animated.
154     mediump vec4 textColor = textTexture * textTexture.a;\n
155     mediump float vstep = step( 0.0001, textColor.a );\n
156     textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture.a * ( 1.0 - uHasMultipleTextColors ) );\n
157
158     // Draw the text as overlay above the style
159     gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
160   }\n
161 );
162
163 /**
164  * Return Property index for the given string key
165  * param[in] stringKey the string index key
166  * return the key as an index
167  */
168
169 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
170 {
171   Dali::Property::Index result = Property::INVALID_KEY;
172
173   if( stringKey == VISUAL_TYPE )
174   {
175     result = Toolkit::Visual::Property::TYPE;
176   }
177   else if( stringKey == TEXT_PROPERTY )
178   {
179     result = Toolkit::TextVisual::Property::TEXT;
180   }
181   else if( stringKey == FONT_FAMILY_PROPERTY )
182   {
183     result = Toolkit::TextVisual::Property::FONT_FAMILY;
184   }
185   else if( stringKey == FONT_STYLE_PROPERTY )
186   {
187     result = Toolkit::TextVisual::Property::FONT_STYLE;
188   }
189   else if( stringKey == POINT_SIZE_PROPERTY )
190   {
191     result = Toolkit::TextVisual::Property::POINT_SIZE;
192   }
193   else if( stringKey == MULTI_LINE_PROPERTY )
194   {
195     result = Toolkit::TextVisual::Property::MULTI_LINE;
196   }
197   else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
198   {
199     result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
200   }
201   else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
202   {
203     result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
204   }
205   else if( stringKey == TEXT_COLOR_PROPERTY )
206   {
207     result = Toolkit::TextVisual::Property::TEXT_COLOR;
208   }
209   else if( stringKey == ENABLE_MARKUP_PROPERTY )
210   {
211     result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
212   }
213   else if( stringKey == SHADOW_PROPERTY )
214   {
215     result = Toolkit::TextVisual::Property::SHADOW;
216   }
217   else if( stringKey == UNDERLINE_PROPERTY )
218   {
219     result = Toolkit::TextVisual::Property::UNDERLINE;
220   }
221
222   return result;
223 }
224
225 } // unnamed namespace
226
227 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
228 {
229   TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
230   TextVisualPtr->SetProperties( properties );
231   return TextVisualPtr;
232 }
233
234 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
235 {
236   Property::Map outMap;
237
238   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
239   {
240     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
241
242     Property::Index indexKey = keyValue.first.indexKey;
243
244     if ( keyValue.first.type == Property::Key::STRING )
245     {
246       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
247     }
248
249     outMap.Insert( indexKey, keyValue.second );
250   }
251
252   propertyMap = outMap;
253 }
254
255 float TextVisual::GetHeightForWidth( float width )
256 {
257   return mController->GetHeightForWidth( width );
258 }
259
260 void TextVisual::GetNaturalSize( Vector2& naturalSize )
261 {
262   naturalSize = mController->GetNaturalSize().GetVectorXY();
263 }
264
265 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
266 {
267   Property::Value value;
268
269   map.Clear();
270   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
271
272   std::string text;
273   mController->GetText( text );
274   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
275
276   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
277
278   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
279   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
280
281   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
282
283   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
284
285   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
286
287   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
288
289   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
290
291   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
292
293   GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
294   map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
295
296   GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
297   map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
298 }
299
300 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
301 {
302   map.Clear();
303   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
304   std::string text;
305   mController->GetText( text );
306   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
307 }
308
309
310 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
311 : Visual::Base( factoryCache ),
312   mController( Text::Controller::New() ),
313   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
314   mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
315   mRendererUpdateNeeded( false )
316 {
317 }
318
319 TextVisual::~TextVisual()
320 {
321 }
322
323 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
324 {
325   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
326   {
327     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
328
329     Property::Index indexKey = keyValue.first.indexKey;
330
331     if( keyValue.first.type == Property::Key::STRING )
332     {
333       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
334     }
335
336     DoSetProperty( indexKey, keyValue.second );
337   }
338
339   // Elide the text if it exceeds the boundaries.
340   mController->SetTextElideEnabled( true );
341
342   // Retrieve the layout engine to set the cursor's width.
343   Text::Layout::Engine& engine = mController->GetLayoutEngine();
344
345   // Sets 0 as cursor's width.
346   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
347 }
348
349 void TextVisual::DoSetOnStage( Actor& actor )
350 {
351   mControl = actor;
352
353   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
354
355   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::TEXT_SHADER );
356   if( ! shader )
357   {
358     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
359     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
360
361     mFactoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER, shader );
362   }
363
364   mImpl->mRenderer = Renderer::New( geometry, shader );
365   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
366
367   const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
368   Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
369
370   if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
371   {
372     // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
373     if( shaderTextColorIndex != Property::INVALID_INDEX )
374     {
375       Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
376       constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
377       constraint.Apply();
378     }
379   }
380
381   // Renderer needs textures and to be added to control
382   mRendererUpdateNeeded = true;
383
384   UpdateRenderer();
385 }
386
387 void TextVisual::DoSetOffStage( Actor& actor )
388 {
389   if( mImpl->mRenderer )
390   {
391     // Removes the renderer from the actor.
392     actor.RemoveRenderer( mImpl->mRenderer );
393
394     RemoveTextureSet();
395
396     // Resets the renderer.
397     mImpl->mRenderer.Reset();
398   }
399
400   // Resets the control handle.
401   mControl.Reset();
402 }
403
404 void TextVisual::OnSetTransform()
405 {
406   UpdateRenderer();
407 }
408
409 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
410 {
411   switch( index )
412   {
413     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
414     {
415       const bool enableMarkup = propertyValue.Get<bool>();
416       mController->SetMarkupProcessorEnabled( enableMarkup );
417       break;
418     }
419     case Toolkit::TextVisual::Property::TEXT:
420     {
421       mController->SetText( propertyValue.Get<std::string>() );
422       break;
423     }
424     case Toolkit::TextVisual::Property::FONT_FAMILY:
425     {
426       SetFontFamilyProperty( mController, propertyValue );
427       break;
428     }
429     case Toolkit::TextVisual::Property::FONT_STYLE:
430     {
431       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
432       break;
433     }
434     case Toolkit::TextVisual::Property::POINT_SIZE:
435     {
436       const float pointSize = propertyValue.Get<float>();
437       if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
438       {
439         mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
440       }
441       break;
442     }
443     case Toolkit::TextVisual::Property::MULTI_LINE:
444     {
445       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
446       break;
447     }
448     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
449     {
450       Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
451       if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
452                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE,
453                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
454                                                                                    alignment ) )
455       {
456         mController->SetHorizontalAlignment( alignment );
457       }
458       break;
459     }
460     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
461     {
462       Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
463       if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
464                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE,
465                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
466                                                                                  alignment ) )
467       {
468         mController->SetVerticalAlignment( alignment );
469       }
470       break;
471     }
472     case Toolkit::TextVisual::Property::TEXT_COLOR:
473     {
474       const Vector4& textColor = propertyValue.Get< Vector4 >();
475       if( mController->GetDefaultColor() != textColor )
476       {
477         mController->SetDefaultColor( textColor );
478       }
479       break;
480     }
481     case Toolkit::TextVisual::Property::SHADOW:
482     {
483       SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
484       break;
485     }
486     case Toolkit::TextVisual::Property::UNDERLINE:
487     {
488       SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
489       break;
490     }
491   }
492 }
493
494 void TextVisual::UpdateRenderer()
495 {
496   Actor control = mControl.GetHandle();
497   if( !control )
498   {
499     // Nothing to do.
500     return;
501   }
502
503   // Calculates the size to be used to relayout.
504   Vector2 relayoutSize;
505
506   const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
507   const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
508
509   // Round the size and offset to avoid pixel alignement issues.
510   relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
511   relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
512
513   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
514   {
515     // Removes the texture set.
516     RemoveTextureSet();
517
518     // Remove any renderer previously set.
519     if( mImpl->mRenderer )
520     {
521       control.RemoveRenderer( mImpl->mRenderer );
522     }
523
524     // Nothing else to do if the relayout size is zero.
525     ResourceReady();
526     return;
527   }
528
529   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
530
531   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
532    || mRendererUpdateNeeded )
533   {
534     mRendererUpdateNeeded = false;
535
536     // Removes the texture set.
537     RemoveTextureSet();
538
539     // Remove any renderer previously set.
540     if( mImpl->mRenderer )
541     {
542       control.RemoveRenderer( mImpl->mRenderer );
543     }
544
545     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
546         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
547     {
548       Vector4 atlasRect = FULL_TEXTURE_RECT;
549
550       // Create a texture for the text without any styles
551       PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES );
552
553       // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
554       // In that case, create a texture. TODO: should tile the text.
555
556       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
557                                       data.GetPixelFormat(),
558                                       data.GetWidth(),
559                                       data.GetHeight() );
560
561       texture.Upload( data );
562
563       TextureSet textureSet = TextureSet::New();
564       textureSet.SetTexture( 0u, texture );
565
566       // Create a texture for all the text styles (without the text itself)
567       PixelData styleData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_TEXT );
568
569       Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
570                                            styleData.GetPixelFormat(),
571                                            styleData.GetWidth(),
572                                            styleData.GetHeight() );
573
574       styleTexture.Upload( styleData );
575
576       textureSet.SetTexture( 1u, styleTexture );
577
578       // Create a texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
579       PixelData maskData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_MASK );
580
581       Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
582                                           styleData.GetPixelFormat(),
583                                           styleData.GetWidth(),
584                                           styleData.GetHeight() );
585
586       maskTexture.Upload( maskData );
587
588       textureSet.SetTexture( 2u, maskTexture );
589
590       // Filter mode needs to be set to nearest to avoid blurry text.
591       Sampler sampler = Sampler::New();
592       sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
593       textureSet.SetSampler( 0u, sampler );
594       textureSet.SetSampler( 1u, sampler );
595       textureSet.SetSampler( 2u, sampler );
596
597       mImpl->mRenderer.SetTextures( textureSet );
598
599       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
600
601       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
602
603       // Check whether it is a markup text with multiple text colors
604       const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
605       bool hasMultipleTextColors = ( NULL != colorsBuffer );
606       mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
607
608       //Register transform properties
609       mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
610
611       control.AddRenderer( mImpl->mRenderer );
612
613       // Text rendered and ready to display
614       ResourceReady();
615     }
616   }
617 }
618
619 void TextVisual::RemoveTextureSet()
620 {
621   if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
622   {
623     // Removes the text's image from the texture atlas.
624     Vector4 atlasRect;
625
626     const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
627     if( index != Property::INVALID_INDEX )
628     {
629       const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
630       atlasRectValue.Get( atlasRect );
631
632       const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
633       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
634     }
635   }
636 }
637
638 } // namespace Internal
639
640 } // namespace Toolkit
641
642 } // namespace Dali