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