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