Reduce text visual memory consumption for text with no styles and emojis
[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/devel-api/visuals/text-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.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
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45
46 namespace
47 {
48
49 // Property names - common properties defined in visual-string-constants.h/cpp
50 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
51 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
52 const char * const POINT_SIZE_PROPERTY( "pointSize" );
53 const char * const MULTI_LINE_PROPERTY( "multiLine" );
54 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
55 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
56 const char * const TEXT_COLOR_PROPERTY( "textColor" );
57 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
58 const char * const SHADOW_PROPERTY( "shadow" );
59 const char * const UNDERLINE_PROPERTY( "underline" );
60
61 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
62 {
63   { "BEGIN",  Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN  },
64   { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
65   { "END",    Toolkit::Text::Layout::HORIZONTAL_ALIGN_END    },
66 };
67 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
68
69 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
70 {
71   { "TOP",    Toolkit::Text::Layout::VERTICAL_ALIGN_TOP    },
72   { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
73   { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
74 };
75 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
76
77 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
78
79 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
80 {
81   const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
82                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE,
83                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
84
85   return std::string( name );
86 }
87
88 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
89 {
90   const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
91                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE,
92                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
93
94   return std::string( name );
95 }
96
97 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
98   attribute mediump vec2 aPosition;\n
99   uniform mediump mat4 uMvpMatrix;\n
100   uniform mediump vec3 uSize;\n
101   uniform mediump vec4 pixelArea;\n
102
103   uniform mediump mat4 uModelMatrix;\n
104   uniform mediump mat4 uViewMatrix;\n
105   uniform mediump mat4 uProjection;\n
106
107   varying mediump vec2 vTexCoord;\n
108
109   //Visual size and offset
110   uniform mediump vec2 offset;\n
111   uniform mediump vec2 size;\n
112   uniform mediump vec4 offsetSizeMode;\n
113   uniform mediump vec2 origin;\n
114   uniform mediump vec2 anchorPoint;\n
115
116   vec4 ComputeVertexPosition()\n
117   {\n
118     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
119     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
120     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
121   }\n
122
123   void main()\n
124   {\n
125     mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
126     mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
127     mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
128
129     vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
130     gl_Position = vertexPosition;\n
131   }\n
132 );
133
134 const char* FRAGMENT_SHADER_ATLAS_CLAMP_RGBA = DALI_COMPOSE_SHADER(
135   varying mediump vec2 vTexCoord;\n
136   uniform sampler2D sTexture;\n
137   uniform sampler2D sStyle;\n
138   uniform sampler2D sMask;\n
139   uniform lowp float uHasMultipleTextColors;\n
140   uniform lowp vec4 uTextColorAnimatable;\n
141   uniform mediump vec4 uAtlasRect;\n
142   uniform lowp vec4 uColor;\n
143   uniform lowp vec3 mixColor;\n
144   uniform lowp float opacity;\n
145   \n
146   void main()\n
147   {\n
148     mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
149     mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
150     mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
151     mediump vec4 maskTexture = texture2D( sMask, texCoord );\n
152
153     // Set the color of non-transparent pixel in text to what it is animated to.
154     // Markup text with multiple text colors are not animated (but can be supported later on if required).
155     // Emoji color are not animated.
156     mediump vec4 textColor = textTexture * textTexture.a;\n
157     mediump float vstep = step( 0.0001, textColor.a );\n
158     textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture.a * ( 1.0 - uHasMultipleTextColors ) );\n
159
160     // Draw the text as overlay above the style
161     gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
162   }\n
163 );
164
165 const char* FRAGMENT_SHADER_ATLAS_CLAMP_L8 = DALI_COMPOSE_SHADER(
166   varying mediump vec2 vTexCoord;\n
167   uniform sampler2D sTexture;\n
168   uniform lowp vec4 uTextColorAnimatable;\n
169   uniform mediump vec4 uAtlasRect;\n
170   uniform lowp vec4 uColor;\n
171   uniform lowp vec3 mixColor;\n
172   uniform lowp float opacity;\n
173   \n
174   void main()\n
175   {\n
176     mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
177     mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
178
179     // Set the color of the text to what it is animated to.
180     gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, opacity );\n
181   }\n
182 );
183
184 /**
185  * Return Property index for the given string key
186  * param[in] stringKey the string index key
187  * return the key as an index
188  */
189
190 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
191 {
192   Dali::Property::Index result = Property::INVALID_KEY;
193
194   if( stringKey == VISUAL_TYPE )
195   {
196     result = Toolkit::Visual::Property::TYPE;
197   }
198   else if( stringKey == TEXT_PROPERTY )
199   {
200     result = Toolkit::TextVisual::Property::TEXT;
201   }
202   else if( stringKey == FONT_FAMILY_PROPERTY )
203   {
204     result = Toolkit::TextVisual::Property::FONT_FAMILY;
205   }
206   else if( stringKey == FONT_STYLE_PROPERTY )
207   {
208     result = Toolkit::TextVisual::Property::FONT_STYLE;
209   }
210   else if( stringKey == POINT_SIZE_PROPERTY )
211   {
212     result = Toolkit::TextVisual::Property::POINT_SIZE;
213   }
214   else if( stringKey == MULTI_LINE_PROPERTY )
215   {
216     result = Toolkit::TextVisual::Property::MULTI_LINE;
217   }
218   else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
219   {
220     result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
221   }
222   else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
223   {
224     result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
225   }
226   else if( stringKey == TEXT_COLOR_PROPERTY )
227   {
228     result = Toolkit::TextVisual::Property::TEXT_COLOR;
229   }
230   else if( stringKey == ENABLE_MARKUP_PROPERTY )
231   {
232     result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
233   }
234   else if( stringKey == SHADOW_PROPERTY )
235   {
236     result = Toolkit::TextVisual::Property::SHADOW;
237   }
238   else if( stringKey == UNDERLINE_PROPERTY )
239   {
240     result = Toolkit::TextVisual::Property::UNDERLINE;
241   }
242
243   return result;
244 }
245
246 } // unnamed namespace
247
248 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
249 {
250   TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
251   TextVisualPtr->SetProperties( properties );
252   return TextVisualPtr;
253 }
254
255 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
256 {
257   Property::Map outMap;
258
259   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
260   {
261     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
262
263     Property::Index indexKey = keyValue.first.indexKey;
264
265     if ( keyValue.first.type == Property::Key::STRING )
266     {
267       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
268     }
269
270     outMap.Insert( indexKey, keyValue.second );
271   }
272
273   propertyMap = outMap;
274 }
275
276 float TextVisual::GetHeightForWidth( float width )
277 {
278   return mController->GetHeightForWidth( width );
279 }
280
281 void TextVisual::GetNaturalSize( Vector2& naturalSize )
282 {
283   naturalSize = mController->GetNaturalSize().GetVectorXY();
284 }
285
286 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
287 {
288   Property::Value value;
289
290   map.Clear();
291   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
292
293   std::string text;
294   mController->GetText( text );
295   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
296
297   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
298
299   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
300   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
301
302   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
303
304   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
305
306   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
307
308   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
309
310   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
311
312   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
313
314   GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
315   map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
316
317   GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
318   map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
319 }
320
321 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
322 {
323   map.Clear();
324   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
325   std::string text;
326   mController->GetText( text );
327   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
328 }
329
330
331 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
332 : Visual::Base( factoryCache ),
333   mController( Text::Controller::New() ),
334   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
335   mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
336   mRendererUpdateNeeded( false )
337 {
338 }
339
340 TextVisual::~TextVisual()
341 {
342 }
343
344 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
345 {
346   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
347   {
348     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
349
350     Property::Index indexKey = keyValue.first.indexKey;
351
352     if( keyValue.first.type == Property::Key::STRING )
353     {
354       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
355     }
356
357     DoSetProperty( indexKey, keyValue.second );
358   }
359
360   // Elide the text if it exceeds the boundaries.
361   mController->SetTextElideEnabled( true );
362
363   // Retrieve the layout engine to set the cursor's width.
364   Text::Layout::Engine& engine = mController->GetLayoutEngine();
365
366   // Sets 0 as cursor's width.
367   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
368 }
369
370 void TextVisual::DoSetOnStage( Actor& actor )
371 {
372   mControl = actor;
373
374   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
375   Shader shader = GetTextShader(mFactoryCache, true);
376
377   mImpl->mRenderer = Renderer::New( geometry, shader );
378   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
379
380   const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
381   Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
382
383   if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
384   {
385     // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
386     if( shaderTextColorIndex != Property::INVALID_INDEX )
387     {
388       Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
389       constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
390       constraint.Apply();
391     }
392   }
393
394   // Renderer needs textures and to be added to control
395   mRendererUpdateNeeded = true;
396
397   UpdateRenderer();
398 }
399
400 void TextVisual::DoSetOffStage( Actor& actor )
401 {
402   if( mImpl->mRenderer )
403   {
404     // Removes the renderer from the actor.
405     actor.RemoveRenderer( mImpl->mRenderer );
406
407     RemoveTextureSet();
408
409     // Resets the renderer.
410     mImpl->mRenderer.Reset();
411   }
412
413   // Resets the control handle.
414   mControl.Reset();
415 }
416
417 void TextVisual::OnSetTransform()
418 {
419   UpdateRenderer();
420 }
421
422 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
423 {
424   switch( index )
425   {
426     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
427     {
428       const bool enableMarkup = propertyValue.Get<bool>();
429       mController->SetMarkupProcessorEnabled( enableMarkup );
430       break;
431     }
432     case Toolkit::TextVisual::Property::TEXT:
433     {
434       mController->SetText( propertyValue.Get<std::string>() );
435       break;
436     }
437     case Toolkit::TextVisual::Property::FONT_FAMILY:
438     {
439       SetFontFamilyProperty( mController, propertyValue );
440       break;
441     }
442     case Toolkit::TextVisual::Property::FONT_STYLE:
443     {
444       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
445       break;
446     }
447     case Toolkit::TextVisual::Property::POINT_SIZE:
448     {
449       const float pointSize = propertyValue.Get<float>();
450       if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
451       {
452         mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
453       }
454       break;
455     }
456     case Toolkit::TextVisual::Property::MULTI_LINE:
457     {
458       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
459       break;
460     }
461     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
462     {
463       Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
464       if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
465                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE,
466                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
467                                                                                    alignment ) )
468       {
469         mController->SetHorizontalAlignment( alignment );
470       }
471       break;
472     }
473     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
474     {
475       Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
476       if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
477                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE,
478                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
479                                                                                  alignment ) )
480       {
481         mController->SetVerticalAlignment( alignment );
482       }
483       break;
484     }
485     case Toolkit::TextVisual::Property::TEXT_COLOR:
486     {
487       const Vector4& textColor = propertyValue.Get< Vector4 >();
488       if( mController->GetDefaultColor() != textColor )
489       {
490         mController->SetDefaultColor( textColor );
491       }
492       break;
493     }
494     case Toolkit::TextVisual::Property::SHADOW:
495     {
496       SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
497       break;
498     }
499     case Toolkit::TextVisual::Property::UNDERLINE:
500     {
501       SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
502       break;
503     }
504   }
505 }
506
507 void TextVisual::UpdateRenderer()
508 {
509   Actor control = mControl.GetHandle();
510   if( !control )
511   {
512     // Nothing to do.
513     return;
514   }
515
516   // Calculates the size to be used to relayout.
517   Vector2 relayoutSize;
518
519   const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
520   const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
521
522   // Round the size and offset to avoid pixel alignement issues.
523   relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
524   relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
525
526   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
527   {
528     // Removes the texture set.
529     RemoveTextureSet();
530
531     // Remove any renderer previously set.
532     if( mImpl->mRenderer )
533     {
534       control.RemoveRenderer( mImpl->mRenderer );
535     }
536
537     // Nothing else to do if the relayout size is zero.
538     ResourceReady();
539     return;
540   }
541
542   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
543
544   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
545    || mRendererUpdateNeeded )
546   {
547     mRendererUpdateNeeded = false;
548
549     // Removes the texture set.
550     RemoveTextureSet();
551
552     // Remove any renderer previously set.
553     if( mImpl->mRenderer )
554     {
555       control.RemoveRenderer( mImpl->mRenderer );
556     }
557
558     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
559         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
560     {
561       // Check whether it is a markup text with multiple text colors
562       const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
563       bool hasMultipleTextColors = ( NULL != colorsBuffer );
564
565       // Check whether the text contains any emoji
566       bool containsEmoji = false;
567
568       Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
569       const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
570       for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
571       {
572         const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
573         if( TextAbstraction::EMOJI == scriptRun.script )
574         {
575           containsEmoji = true;
576           break;
577         }
578       }
579
580       // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
581       bool shadowEnabled = false;
582       const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
583       if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
584       {
585         shadowEnabled = true;
586       }
587
588       const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
589
590       if ( hasMultipleTextColors || containsEmoji || shadowEnabled || underlineEnabled )
591       {
592         // Create RGBA textures if the text contains emojis or styles or multiple text colors
593
594         // Create a texture for the text without any styles
595         PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES );
596
597         // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
598         // In that case, create a texture. TODO: should tile the text.
599
600         Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
601                                         data.GetPixelFormat(),
602                                         data.GetWidth(),
603                                         data.GetHeight() );
604
605         texture.Upload( data );
606
607         TextureSet textureSet = TextureSet::New();
608         textureSet.SetTexture( 0u, texture );
609
610         // Create a texture for all the text styles (without the text itself)
611         PixelData styleData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_TEXT );
612
613         Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
614                                              styleData.GetPixelFormat(),
615                                              styleData.GetWidth(),
616                                              styleData.GetHeight() );
617
618         styleTexture.Upload( styleData );
619
620         textureSet.SetTexture( 1u, styleTexture );
621
622         // Create a texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
623         PixelData maskData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_MASK );
624
625         Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
626                                             styleData.GetPixelFormat(),
627                                             styleData.GetWidth(),
628                                             styleData.GetHeight() );
629
630         maskTexture.Upload( maskData );
631
632         textureSet.SetTexture( 2u, maskTexture );
633
634         // Filter mode needs to be set to nearest to produce better quality while static.
635         Sampler sampler = Sampler::New();
636         sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
637         textureSet.SetSampler( 0u, sampler );
638         textureSet.SetSampler( 1u, sampler );
639         textureSet.SetSampler( 2u, sampler );
640
641         mImpl->mRenderer.SetTextures( textureSet );
642
643         Shader shader = GetTextShader(mFactoryCache, true); // RGBA shader
644         mImpl->mRenderer.SetShader(shader);
645       }
646       else
647       {
648         // Create L8 texture if the text contains only single text color with no emoji and no style
649
650         // Create a texture for the text without any styles
651         PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES, false, Pixel::L8 );
652
653         // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
654         // In that case, create a texture. TODO: should tile the text.
655
656         Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
657                                         data.GetPixelFormat(),
658                                         data.GetWidth(),
659                                         data.GetHeight() );
660
661         texture.Upload( data );
662
663         TextureSet textureSet = TextureSet::New();
664         textureSet.SetTexture( 0u, texture );
665
666         // Filter mode needs to be set to nearest to produce better quality while static.
667         Sampler sampler = Sampler::New();
668         sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
669         textureSet.SetSampler( 0u, sampler );
670
671         mImpl->mRenderer.SetTextures( textureSet );
672
673         Shader shader = GetTextShader(mFactoryCache, false); // L8 shader
674         mImpl->mRenderer.SetShader(shader);
675       }
676
677       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
678
679       Vector4 atlasRect = FULL_TEXTURE_RECT;
680       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
681       mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
682
683       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
684
685       //Register transform properties
686       mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
687
688       control.AddRenderer( mImpl->mRenderer );
689
690       // Text rendered and ready to display
691       ResourceReady();
692     }
693   }
694 }
695
696 void TextVisual::RemoveTextureSet()
697 {
698   if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
699   {
700     // Removes the text's image from the texture atlas.
701     Vector4 atlasRect;
702
703     const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
704     if( index != Property::INVALID_INDEX )
705     {
706       const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
707       atlasRectValue.Get( atlasRect );
708
709       const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
710       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
711     }
712   }
713 }
714
715 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool isRgbaTexture )
716 {
717   Shader shader;
718   if( isRgbaTexture )
719   {
720     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_RGBA );
721     if( !shader )
722     {
723       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP_RGBA );
724       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
725       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_RGBA, shader );
726     }
727   }
728   else
729   {
730     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_L8 );
731     if( !shader )
732     {
733       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP_L8 );
734       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
735       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_L8, shader );
736     }
737   }
738
739   return shader;
740 }
741
742 } // namespace Internal
743
744 } // namespace Toolkit
745
746 } // namespace Dali