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