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