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