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