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