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