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