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