2c8121b5d79ebc8f83017befb05926ad67bb8cda
[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/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/devel-api/text/text-enumerations-devel.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 } // unnamed namespace
343
344 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
345 {
346   TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
347   TextVisualPtr->SetProperties( properties );
348   return TextVisualPtr;
349 }
350
351 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
352 {
353   Property::Map outMap;
354
355   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
356   {
357     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
358
359     Property::Index indexKey = keyValue.first.indexKey;
360
361     if ( keyValue.first.type == Property::Key::STRING )
362     {
363       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
364     }
365
366     outMap.Insert( indexKey, keyValue.second );
367   }
368
369   propertyMap = outMap;
370 }
371
372 float TextVisual::GetHeightForWidth( float width )
373 {
374   return mController->GetHeightForWidth( width );
375 }
376
377 void TextVisual::GetNaturalSize( Vector2& naturalSize )
378 {
379   naturalSize = mController->GetNaturalSize().GetVectorXY();
380 }
381
382 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
383 {
384   Property::Value value;
385
386   map.Clear();
387   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
388
389   std::string text;
390   mController->GetText( text );
391   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
392
393   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
394
395   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
396   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
397
398   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
399
400   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
401
402   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
403
404   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
405
406   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
407
408   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
409
410   GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
411   map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
412
413   GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
414   map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
415
416   GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
417   map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
418 }
419
420 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
421 {
422   map.Clear();
423   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
424   std::string text;
425   mController->GetText( text );
426   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
427 }
428
429
430 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
431 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
432   mController( Text::Controller::New() ),
433   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
434   mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
435   mRendererUpdateNeeded( false )
436 {
437 }
438
439 TextVisual::~TextVisual()
440 {
441 }
442
443 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
444 {
445   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
446   {
447     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
448
449     Property::Index indexKey = keyValue.first.indexKey;
450
451     if( keyValue.first.type == Property::Key::STRING )
452     {
453       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
454     }
455
456     DoSetProperty( indexKey, keyValue.second );
457   }
458
459   // Elide the text if it exceeds the boundaries.
460   mController->SetTextElideEnabled( true );
461
462   // Retrieve the layout engine to set the cursor's width.
463   Text::Layout::Engine& engine = mController->GetLayoutEngine();
464
465   // Sets 0 as cursor's width.
466   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
467 }
468
469 void TextVisual::DoSetOnStage( Actor& actor )
470 {
471   mControl = actor;
472
473   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
474   Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
475
476   mImpl->mRenderer = Renderer::New( geometry, shader );
477   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
478
479   // Enable the pre-multiplied alpha to improve the text quality
480   EnablePreMultipliedAlpha(true);
481
482   const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
483   Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
484
485   if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
486   {
487     // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
488     if( shaderTextColorIndex != Property::INVALID_INDEX )
489     {
490       Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
491       constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
492       constraint.Apply();
493     }
494   }
495
496   // Renderer needs textures and to be added to control
497   mRendererUpdateNeeded = true;
498
499   UpdateRenderer();
500 }
501
502 void TextVisual::DoSetOffStage( Actor& actor )
503 {
504   if( mImpl->mRenderer )
505   {
506     // Removes the renderer from the actor.
507     actor.RemoveRenderer( mImpl->mRenderer );
508
509     RemoveTextureSet();
510
511     // Resets the renderer.
512     mImpl->mRenderer.Reset();
513   }
514
515   // Resets the control handle.
516   mControl.Reset();
517 }
518
519 void TextVisual::OnSetTransform()
520 {
521   UpdateRenderer();
522 }
523
524 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
525 {
526   switch( index )
527   {
528     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
529     {
530       const bool enableMarkup = propertyValue.Get<bool>();
531       mController->SetMarkupProcessorEnabled( enableMarkup );
532       break;
533     }
534     case Toolkit::TextVisual::Property::TEXT:
535     {
536       mController->SetText( propertyValue.Get<std::string>() );
537       break;
538     }
539     case Toolkit::TextVisual::Property::FONT_FAMILY:
540     {
541       SetFontFamilyProperty( mController, propertyValue );
542       break;
543     }
544     case Toolkit::TextVisual::Property::FONT_STYLE:
545     {
546       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
547       break;
548     }
549     case Toolkit::TextVisual::Property::POINT_SIZE:
550     {
551       const float pointSize = propertyValue.Get<float>();
552       if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
553       {
554         mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
555       }
556       break;
557     }
558     case Toolkit::TextVisual::Property::MULTI_LINE:
559     {
560       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
561       break;
562     }
563     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
564     {
565       if( mController )
566       {
567         Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
568         if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
569         {
570           mController->SetHorizontalAlignment( alignment );
571         }
572       }
573       break;
574     }
575     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
576     {
577       if( mController )
578       {
579         Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
580         if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
581         {
582           mController->SetVerticalAlignment( alignment );
583         }
584       }
585       break;
586     }
587     case Toolkit::TextVisual::Property::TEXT_COLOR:
588     {
589       const Vector4& textColor = propertyValue.Get< Vector4 >();
590       if( mController->GetDefaultColor() != textColor )
591       {
592         mController->SetDefaultColor( textColor );
593       }
594       break;
595     }
596     case Toolkit::TextVisual::Property::SHADOW:
597     {
598       SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
599       break;
600     }
601     case Toolkit::TextVisual::Property::UNDERLINE:
602     {
603       SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
604       break;
605     }
606     case Toolkit::DevelTextVisual::Property::OUTLINE:
607     {
608       SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
609       break;
610     }
611   }
612 }
613
614 void TextVisual::UpdateRenderer()
615 {
616   Actor control = mControl.GetHandle();
617   if( !control )
618   {
619     // Nothing to do.
620     return;
621   }
622
623   // Calculates the size to be used to relayout.
624   Vector2 relayoutSize;
625
626   const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
627   const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
628
629   // Round the size and offset to avoid pixel alignement issues.
630   relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
631   relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
632
633   std::string text;
634   mController->GetText( text );
635
636   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
637   {
638     // Removes the texture set.
639     RemoveTextureSet();
640
641     // Remove any renderer previously set.
642     if( mImpl->mRenderer )
643     {
644       control.RemoveRenderer( mImpl->mRenderer );
645     }
646
647     // Nothing else to do if the relayout size is zero.
648     ResourceReady( Toolkit::Visual::ResourceStatus::READY );
649     return;
650   }
651
652   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
653
654   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
655    || mRendererUpdateNeeded )
656   {
657     mRendererUpdateNeeded = false;
658
659     // Removes the texture set.
660     RemoveTextureSet();
661
662     // Remove any renderer previously set.
663     if( mImpl->mRenderer )
664     {
665       control.RemoveRenderer( mImpl->mRenderer );
666     }
667
668     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
669         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
670     {
671       // Check whether it is a markup text with multiple text colors
672       const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
673       bool hasMultipleTextColors = ( NULL != colorsBuffer );
674
675       // Check whether the text contains any emoji
676       bool containsEmoji = false;
677
678       Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
679       const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
680       for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
681       {
682         const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
683         if( TextAbstraction::EMOJI == scriptRun.script )
684         {
685           containsEmoji = true;
686           break;
687         }
688       }
689
690       // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
691
692       bool shadowEnabled = false;
693       const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
694       if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
695       {
696         shadowEnabled = true;
697       }
698
699       const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
700       const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
701
702       const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
703
704       TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsEmoji, styleEnabled );
705       mImpl->mRenderer.SetTextures( textureSet );
706
707       Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
708       mImpl->mRenderer.SetShader(shader);
709
710       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
711
712       Vector4 atlasRect = FULL_TEXTURE_RECT;
713       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
714       mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
715
716       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
717
718       //Register transform properties
719       mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
720
721       control.AddRenderer( mImpl->mRenderer );
722
723       // Text rendered and ready to display
724       ResourceReady( Toolkit::Visual::ResourceStatus::READY );
725     }
726   }
727 }
728
729 void TextVisual::RemoveTextureSet()
730 {
731   if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
732   {
733     // Removes the text's image from the texture atlas.
734     Vector4 atlasRect;
735
736     const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
737     if( index != Property::INVALID_INDEX )
738     {
739       const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
740       atlasRectValue.Get( atlasRect );
741
742       const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
743       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
744     }
745   }
746 }
747
748 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
749 {
750   // Filter mode needs to be set to linear to produce better quality while scaling.
751   Sampler sampler = Sampler::New();
752   sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
753
754   TextureSet textureSet = TextureSet::New();
755
756   // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
757   Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
758
759   // Check the text direction
760   Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
761
762   // Create a texture for the text without any styles
763   PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
764
765   // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
766   // In that case, create a texture. TODO: should tile the text.
767
768   Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
769                                   data.GetPixelFormat(),
770                                   data.GetWidth(),
771                                   data.GetHeight() );
772
773   texture.Upload( data );
774
775   textureSet.SetTexture( 0u, texture );
776   textureSet.SetSampler( 0u, sampler );
777
778   if ( styleEnabled )
779   {
780     // Create RGBA texture for all the text styles (without the text itself)
781     PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
782
783     Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
784                                          styleData.GetPixelFormat(),
785                                          styleData.GetWidth(),
786                                          styleData.GetHeight() );
787
788     styleTexture.Upload( styleData );
789
790     textureSet.SetTexture( 1u, styleTexture );
791     textureSet.SetSampler( 1u, sampler );
792   }
793
794   if ( containsEmoji && !hasMultipleTextColors )
795   {
796     // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
797     PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
798
799     Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
800                                         maskData.GetPixelFormat(),
801                                         maskData.GetWidth(),
802                                         maskData.GetHeight() );
803
804     maskTexture.Upload( maskData );
805
806     if ( !styleEnabled )
807     {
808       textureSet.SetTexture( 1u, maskTexture );
809       textureSet.SetSampler( 1u, sampler );
810     }
811     else
812     {
813       textureSet.SetTexture( 2u, maskTexture );
814       textureSet.SetSampler( 2u, sampler );
815     }
816   }
817
818   return textureSet;
819 }
820
821 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
822 {
823   Shader shader;
824
825   if( hasMultipleTextColors && !styleEnabled )
826   {
827     // We don't animate text color if the text contains multiple colors
828     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
829     if( !shader )
830     {
831       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
832       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
833       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
834     }
835   }
836   else if( hasMultipleTextColors && styleEnabled )
837   {
838     // We don't animate text color if the text contains multiple colors
839     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
840     if( !shader )
841     {
842       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
843       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
844       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
845     }
846   }
847   else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
848   {
849     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
850     if( !shader )
851     {
852       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
853       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
854       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
855     }
856   }
857   else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
858   {
859     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
860     if( !shader )
861     {
862       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
863       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
864       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
865     }
866   }
867   else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
868   {
869     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
870     if( !shader )
871     {
872       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
873       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
874       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
875     }
876   }
877   else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
878   {
879     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
880     if( !shader )
881     {
882       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
883       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
884       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
885     }
886   }
887
888   return shader;
889 }
890
891 } // namespace Internal
892
893 } // namespace Toolkit
894
895 } // namespace Dali