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