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