Create Renderer when the Visual is created
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.cpp
1 /*
2  * Copyright (c) 2021 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/rendering/renderer-devel.h>
24 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
25 #include <dali/devel-api/adaptor-framework/image-loading.h>
26 #include <dali/devel-api/adaptor-framework/window-devel.h>
27 #include <dali/devel-api/images/pixel-data-devel.h>
28 #include <string.h>
29
30 // INTERNAL HEADER
31 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
32 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
33 #include <dali-toolkit/public-api/visuals/visual-properties.h>
34 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
38 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
39 #include <dali-toolkit/internal/text/text-font-style.h>
40 #include <dali-toolkit/internal/text/text-effects-style.h>
41 #include <dali-toolkit/internal/text/script-run.h>
42 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
43 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
44 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
45
46 namespace Dali
47 {
48
49 namespace Toolkit
50 {
51
52 namespace Internal
53 {
54
55 namespace
56 {
57 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
58
59 /**
60  * Return Property index for the given string key
61  * param[in] stringKey the string index key
62  * return the key as an index
63  */
64
65 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
66 {
67   Dali::Property::Index result = Property::INVALID_KEY;
68
69   if( stringKey == VISUAL_TYPE )
70   {
71     result = Toolkit::Visual::Property::TYPE;
72   }
73   else if( stringKey == TEXT_PROPERTY )
74   {
75     result = Toolkit::TextVisual::Property::TEXT;
76   }
77   else if( stringKey == FONT_FAMILY_PROPERTY )
78   {
79     result = Toolkit::TextVisual::Property::FONT_FAMILY;
80   }
81   else if( stringKey == FONT_STYLE_PROPERTY )
82   {
83     result = Toolkit::TextVisual::Property::FONT_STYLE;
84   }
85   else if( stringKey == POINT_SIZE_PROPERTY )
86   {
87     result = Toolkit::TextVisual::Property::POINT_SIZE;
88   }
89   else if( stringKey == MULTI_LINE_PROPERTY )
90   {
91     result = Toolkit::TextVisual::Property::MULTI_LINE;
92   }
93   else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
94   {
95     result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
96   }
97   else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
98   {
99     result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
100   }
101   else if( stringKey == TEXT_COLOR_PROPERTY )
102   {
103     result = Toolkit::TextVisual::Property::TEXT_COLOR;
104   }
105   else if( stringKey == ENABLE_MARKUP_PROPERTY )
106   {
107     result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
108   }
109   else if( stringKey == SHADOW_PROPERTY )
110   {
111     result = Toolkit::TextVisual::Property::SHADOW;
112   }
113   else if( stringKey == UNDERLINE_PROPERTY )
114   {
115     result = Toolkit::TextVisual::Property::UNDERLINE;
116   }
117   else if( stringKey == OUTLINE_PROPERTY )
118   {
119     result = Toolkit::DevelTextVisual::Property::OUTLINE;
120   }
121   else if( stringKey == BACKGROUND_PROPERTY )
122   {
123     result = Toolkit::DevelTextVisual::Property::BACKGROUND;
124   }
125
126   return result;
127 }
128
129 void TextColorConstraint( Vector4& current, const PropertyInputContainer& inputs )
130 {
131   Vector4 color = inputs[0]->GetVector4();
132   current.r = color.r * color.a;
133   current.g = color.g * color.a;
134   current.b = color.b * color.a;
135   current.a = color.a;
136 }
137
138 void OpacityConstraint( float& current, const PropertyInputContainer& inputs )
139 {
140   // Make zero if the alpha value of text color is zero to skip rendering text
141   if( EqualsZero( inputs[0]->GetVector4().a ) )
142   {
143     current = 0.0f;
144   }
145   else
146   {
147     current = 1.0f;
148   }
149 }
150
151 } // unnamed namespace
152
153 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
154 {
155   TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
156   textVisualPtr->SetProperties(properties);
157   textVisualPtr->Initialize();
158   return textVisualPtr;
159 }
160
161 Property::Map TextVisual::ConvertStringKeysToIndexKeys( const Property::Map& propertyMap )
162 {
163   Property::Map outMap;
164
165   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
166   {
167     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
168
169     Property::Index indexKey = keyValue.first.indexKey;
170
171     if ( keyValue.first.type == Property::Key::STRING )
172     {
173       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
174     }
175
176     outMap.Insert( indexKey, keyValue.second );
177   }
178
179   return outMap;
180 }
181
182 float TextVisual::GetHeightForWidth( float width )
183 {
184   return mController->GetHeightForWidth( width );
185 }
186
187 void TextVisual::GetNaturalSize( Vector2& naturalSize )
188 {
189   naturalSize = mController->GetNaturalSize().GetVectorXY();
190 }
191
192 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
193 {
194   Property::Value value;
195
196   map.Clear();
197   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
198
199   std::string text;
200   mController->GetText( text );
201   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
202
203   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
204
205   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
206   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
207
208   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
209
210   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
211
212   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
213
214   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
215
216   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
217
218   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
219
220   GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
221   map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
222
223   GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
224   map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
225
226   GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
227   map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
228
229   GetBackgroundProperties( mController, value, Text::EffectStyle::DEFAULT );
230   map.Insert( Toolkit::DevelTextVisual::Property::BACKGROUND, value );
231 }
232
233 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
234 {
235   map.Clear();
236   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
237   std::string text;
238   mController->GetText( text );
239   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
240 }
241
242
243 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
244 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT ),
245   mController( Text::Controller::New() ),
246   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
247   mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
248   mRendererUpdateNeeded( false )
249 {
250 }
251
252 TextVisual::~TextVisual()
253 {
254 }
255
256 void TextVisual::OnInitialize()
257 {
258   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
259   Shader   shader   = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
260
261   mImpl->mRenderer = Renderer::New(geometry, shader);
262 }
263
264 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
265 {
266   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
267   {
268     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
269
270     Property::Index indexKey = keyValue.first.indexKey;
271
272     if( keyValue.first.type == Property::Key::STRING )
273     {
274       indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
275     }
276
277     DoSetProperty( indexKey, keyValue.second );
278   }
279
280   // Elide the text if it exceeds the boundaries.
281   mController->SetTextElideEnabled( true );
282
283   // Retrieve the layout engine to set the cursor's width.
284   Text::Layout::Engine& engine = mController->GetLayoutEngine();
285
286   // Sets 0 as cursor's width.
287   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
288 }
289
290 void TextVisual::DoSetOnScene( Actor& actor )
291 {
292   mControl = actor;
293
294   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
295
296   // Enable the pre-multiplied alpha to improve the text quality
297   EnablePreMultipliedAlpha(true);
298
299   const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
300   Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
301
302   if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
303   {
304     // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
305     if( shaderTextColorIndex != Property::INVALID_INDEX )
306     {
307       Constraint colorConstraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint );
308       colorConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
309       colorConstraint.Apply();
310
311       // Make zero if the alpha value of text color is zero to skip rendering text
312       Constraint opacityConstraint = Constraint::New< float >( mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint );
313       opacityConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
314       opacityConstraint.Apply();
315     }
316   }
317
318   // Renderer needs textures and to be added to control
319   mRendererUpdateNeeded = true;
320
321   mRendererList.push_back( mImpl->mRenderer );
322
323   UpdateRenderer();
324 }
325
326 void TextVisual::RemoveRenderer( Actor& actor )
327 {
328   for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
329   {
330     Renderer renderer = (*iter);
331     if( renderer )
332     {
333       // Removes the renderer from the actor.
334       actor.RemoveRenderer( renderer );
335     }
336   }
337   // Clear the renderer list
338   mRendererList.clear();
339 }
340
341 void TextVisual::DoSetOffScene( Actor& actor )
342 {
343   RemoveRenderer( actor );
344
345   // Resets the control handle.
346   mControl.Reset();
347 }
348
349 void TextVisual::OnSetTransform()
350 {
351   UpdateRenderer();
352 }
353
354 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
355 {
356   switch( index )
357   {
358     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
359     {
360       const bool enableMarkup = propertyValue.Get<bool>();
361       mController->SetMarkupProcessorEnabled( enableMarkup );
362       break;
363     }
364     case Toolkit::TextVisual::Property::TEXT:
365     {
366       mController->SetText( propertyValue.Get<std::string>() );
367       break;
368     }
369     case Toolkit::TextVisual::Property::FONT_FAMILY:
370     {
371       SetFontFamilyProperty( mController, propertyValue );
372       break;
373     }
374     case Toolkit::TextVisual::Property::FONT_STYLE:
375     {
376       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
377       break;
378     }
379     case Toolkit::TextVisual::Property::POINT_SIZE:
380     {
381       const float pointSize = propertyValue.Get<float>();
382       if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
383       {
384         mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
385       }
386       break;
387     }
388     case Toolkit::TextVisual::Property::MULTI_LINE:
389     {
390       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
391       break;
392     }
393     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
394     {
395       if( mController )
396       {
397         Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
398         if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
399         {
400           mController->SetHorizontalAlignment( alignment );
401         }
402       }
403       break;
404     }
405     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
406     {
407       if( mController )
408       {
409         Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
410         if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
411         {
412           mController->SetVerticalAlignment( alignment );
413         }
414       }
415       break;
416     }
417     case Toolkit::TextVisual::Property::TEXT_COLOR:
418     {
419       const Vector4& textColor = propertyValue.Get< Vector4 >();
420       if( mController->GetDefaultColor() != textColor )
421       {
422         mController->SetDefaultColor( textColor );
423       }
424       break;
425     }
426     case Toolkit::TextVisual::Property::SHADOW:
427     {
428       SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
429       break;
430     }
431     case Toolkit::TextVisual::Property::UNDERLINE:
432     {
433       SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
434       break;
435     }
436     case Toolkit::DevelTextVisual::Property::OUTLINE:
437     {
438       SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
439       break;
440     }
441     case Toolkit::DevelTextVisual::Property::BACKGROUND:
442     {
443       SetBackgroundProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
444       break;
445     }
446   }
447 }
448
449 void TextVisual::UpdateRenderer()
450 {
451   Actor control = mControl.GetHandle();
452   if( !control )
453   {
454     // Nothing to do.
455     return;
456   }
457
458   // Calculates the size to be used to relayout.
459   Vector2 relayoutSize;
460
461   const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
462   const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
463
464   // Round the size and offset to avoid pixel alignement issues.
465   relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
466   relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
467
468   std::string text;
469   mController->GetText( text );
470
471   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
472   {
473     // Remove the texture set and any renderer previously set.
474     RemoveRenderer( control );
475
476     // Nothing else to do if the relayout size is zero.
477     ResourceReady( Toolkit::Visual::ResourceStatus::READY );
478     return;
479   }
480
481
482   Dali::LayoutDirection::Type layoutDirection;
483   if( mController->IsMatchSystemLanguageDirection() )
484   {
485     layoutDirection = static_cast<Dali::LayoutDirection::Type>( DevelWindow::Get( control ).GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
486   }
487   else
488   {
489     layoutDirection = static_cast<Dali::LayoutDirection::Type>( control.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
490   }
491
492   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize, layoutDirection );
493
494   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
495    || mRendererUpdateNeeded )
496   {
497     mRendererUpdateNeeded = false;
498
499     // Remove the texture set and any renderer previously set.
500     RemoveRenderer( control );
501
502     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
503         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
504     {
505       // Check whether it is a markup text with multiple text colors
506       const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
507       bool hasMultipleTextColors = ( NULL != colorsBuffer );
508
509       // Check whether the text contains any color glyph
510       bool containsColorGlyph = false;
511
512       TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
513       const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
514       const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
515       for ( Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++ )
516       {
517         // Retrieve the glyph's info.
518         const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
519
520         // Whether the current glyph is a color one.
521         if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
522         {
523           containsColorGlyph = true;
524           break;
525         }
526       }
527
528       // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
529
530       bool shadowEnabled = false;
531       const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
532       if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
533       {
534         shadowEnabled = true;
535       }
536
537       const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
538       const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
539       const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();;
540
541       const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled );
542
543
544       AddRenderer( control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
545
546       // Text rendered and ready to display
547       ResourceReady( Toolkit::Visual::ResourceStatus::READY );
548     }
549   }
550 }
551
552 void TextVisual::AddTexture( TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex )
553 {
554   Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
555                                   data.GetPixelFormat(),
556                                   data.GetWidth(),
557                                   data.GetHeight() );
558   texture.Upload( data );
559
560   textureSet.SetTexture( textureSetIndex, texture );
561   textureSet.SetSampler( textureSetIndex, sampler );
562 }
563
564 PixelData TextVisual::ConvertToPixelData( unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat )
565 {
566   int bpp = Pixel::GetBytesPerPixel( textPixelFormat );
567   unsigned int bufferSize = width * height * bpp;
568   unsigned char* dstBuffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
569   memcpy( dstBuffer, buffer + offsetPosition * bpp, bufferSize );
570   PixelData pixelData = Dali::PixelData::New( dstBuffer,
571                                               bufferSize,
572                                               width,
573                                               height,
574                                               textPixelFormat,
575                                               Dali::PixelData::FREE );
576   return pixelData;
577 }
578
579 void TextVisual::CreateTextureSet( TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
580 {
581
582   TextureSet textureSet = TextureSet::New();
583   unsigned int textureSetIndex = 0u;
584
585   // Convert the buffer to pixel data to make it a texture.
586   if( info.textBuffer )
587   {
588     PixelData data = ConvertToPixelData( info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat );
589     AddTexture( textureSet, data, sampler, textureSetIndex );
590     ++textureSetIndex;
591   }
592
593   if( styleEnabled && info.styleBuffer )
594   {
595     PixelData styleData = ConvertToPixelData( info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888 );
596     AddTexture( textureSet, styleData, sampler, textureSetIndex );
597     ++textureSetIndex;
598   }
599
600   if( containsColorGlyph && !hasMultipleTextColors && info.maskBuffer )
601   {
602     PixelData maskData = ConvertToPixelData( info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8 );
603     AddTexture( textureSet, maskData, sampler, textureSetIndex );
604   }
605
606   renderer.SetTextures( textureSet );
607
608   //Register transform properties
609   mImpl->mTransform.RegisterUniforms( renderer, Direction::LEFT_TO_RIGHT );
610
611   // Enable the pre-multiplied alpha to improve the text quality
612   renderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
613   renderer.RegisterProperty( PREMULTIPLIED_ALPHA, 1.0f );
614
615   // Set size and offset for the tiling.
616   renderer.RegisterProperty( SIZE, Vector2( info.width, info.height ) );
617   renderer.RegisterProperty( OFFSET, Vector2( info.offSet.x, info.offSet.y ) );
618   renderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
619   renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
620
621   mRendererList.push_back( renderer );
622 }
623
624
625 void TextVisual::AddRenderer( Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
626 {
627   Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
628   mImpl->mRenderer.SetShader( shader );
629
630   // Get the maximum size.
631   const int maxTextureSize = Dali::GetMaxTextureSize();
632
633   // No tiling required. Use the default renderer.
634   if( size.height < maxTextureSize )
635   {
636     TextureSet textureSet = GetTextTexture( size, hasMultipleTextColors, containsColorGlyph, styleEnabled );
637
638     mImpl->mRenderer.SetTextures( textureSet );
639     //Register transform properties
640     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
641     mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
642     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
643
644     mRendererList.push_back( mImpl->mRenderer );
645   }
646   // If the pixel data exceeds the maximum size, tiling is required.
647   else
648   {
649     // Filter mode needs to be set to linear to produce better quality while scaling.
650     Sampler sampler = Sampler::New();
651     sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
652
653     // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
654     Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
655
656     // Check the text direction
657     Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
658
659     // Create a texture for the text without any styles
660     PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
661
662     int verifiedWidth = data.GetWidth();
663     int verifiedHeight = data.GetHeight();
664
665     // Set information for creating textures.
666     TilingInfo info( verifiedWidth, maxTextureSize, textPixelFormat );
667
668     // Get the buffer of text.
669     Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( data );
670     info.textBuffer = textPixelData.buffer;
671
672     if( styleEnabled )
673     {
674       // Create RGBA texture for all the text styles (without the text itself)
675       PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
676       Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( styleData );
677       info.styleBuffer = stylePixelData.buffer;
678     }
679
680     if ( containsColorGlyph && !hasMultipleTextColors )
681     {
682       // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
683       PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
684       Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( maskData );
685       info.maskBuffer = maskPixelData.buffer;
686     }
687
688     // Get the current offset for recalculate the offset when tiling.
689     Property::Map retMap;
690     mImpl->mTransform.GetPropertyMap( retMap );
691     Property::Value* offsetValue = retMap.Find( Dali::Toolkit::Visual::Transform::Property::OFFSET );
692     if( offsetValue )
693     {
694       offsetValue->Get( info.offSet );
695     }
696
697     // Create a textureset in the default renderer.
698     CreateTextureSet( info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
699
700     verifiedHeight -= maxTextureSize;
701
702     Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
703
704     int offsetPosition = verifiedWidth * maxTextureSize;
705     // Create a renderer by cutting maxTextureSize.
706     while( verifiedHeight > 0 )
707     {
708       Renderer tilingRenderer = Renderer::New( geometry, shader );
709       tilingRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
710       // New offset position of buffer for tiling.
711       info.offsetPosition += offsetPosition;
712       // New height for tiling.
713       info.height = ( verifiedHeight - maxTextureSize ) > 0 ? maxTextureSize : verifiedHeight;
714       // New offset for tiling.
715       info.offSet.y += maxTextureSize;
716       // Create a textureset int the new tiling renderer.
717       CreateTextureSet( info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
718
719       verifiedHeight -= maxTextureSize;
720     }
721   }
722
723   mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
724
725   for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
726   {
727     Renderer renderer = (*iter);
728     if( renderer )
729     {
730       actor.AddRenderer( renderer );
731     }
732   }
733 }
734
735
736 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
737 {
738   // Filter mode needs to be set to linear to produce better quality while scaling.
739   Sampler sampler = Sampler::New();
740   sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
741
742   TextureSet textureSet = TextureSet::New();
743
744   // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
745   Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
746
747   // Check the text direction
748   Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
749
750   // Create a texture for the text without any styles
751   PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
752
753   // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
754   // In that case, create a texture. TODO: should tile the text.
755   unsigned int textureSetIndex = 0u;
756
757   AddTexture( textureSet, data, sampler, textureSetIndex );
758   ++textureSetIndex;
759
760   if ( styleEnabled )
761   {
762     // Create RGBA texture for all the text styles (without the text itself)
763     PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
764
765     AddTexture( textureSet, styleData, sampler, textureSetIndex );
766     ++textureSetIndex;
767   }
768
769   if ( containsColorGlyph && !hasMultipleTextColors )
770   {
771     // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
772     PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
773
774     AddTexture( textureSet, maskData, sampler, textureSetIndex );
775   }
776
777   return textureSet;
778 }
779
780 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
781 {
782   Shader shader;
783
784   if( hasMultipleTextColors && !styleEnabled )
785   {
786     // We don't animate text color if the text contains multiple colors
787     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
788     if( !shader )
789     {
790       shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG );
791       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
792       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
793     }
794   }
795   else if( hasMultipleTextColors && styleEnabled )
796   {
797     // We don't animate text color if the text contains multiple colors
798     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
799     if( !shader )
800     {
801       shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG );
802       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
803       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
804     }
805   }
806   else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
807   {
808     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
809     if( !shader )
810     {
811       shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG );
812       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
813       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
814     }
815   }
816   else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
817   {
818     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
819     if( !shader )
820     {
821       shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG );
822       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
823       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
824     }
825   }
826   else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
827   {
828     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
829     if( !shader )
830     {
831       shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG );
832       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
833       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
834     }
835   }
836   else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
837   {
838     shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
839     if( !shader )
840     {
841       shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG );
842       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
843       factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
844     }
845   }
846
847   return shader;
848 }
849
850 } // namespace Internal
851
852 } // namespace Toolkit
853
854 } // namespace Dali