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