2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/visuals/text/text-visual.h>
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>
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>
57 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
60 * Return Property index for the given string key
61 * param[in] stringKey the string index key
62 * return the key as an index
65 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
67 Dali::Property::Index result = Property::INVALID_KEY;
69 if( stringKey == VISUAL_TYPE )
71 result = Toolkit::Visual::Property::TYPE;
73 else if( stringKey == TEXT_PROPERTY )
75 result = Toolkit::TextVisual::Property::TEXT;
77 else if( stringKey == FONT_FAMILY_PROPERTY )
79 result = Toolkit::TextVisual::Property::FONT_FAMILY;
81 else if( stringKey == FONT_STYLE_PROPERTY )
83 result = Toolkit::TextVisual::Property::FONT_STYLE;
85 else if( stringKey == POINT_SIZE_PROPERTY )
87 result = Toolkit::TextVisual::Property::POINT_SIZE;
89 else if( stringKey == MULTI_LINE_PROPERTY )
91 result = Toolkit::TextVisual::Property::MULTI_LINE;
93 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
95 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
97 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
99 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
101 else if( stringKey == TEXT_COLOR_PROPERTY )
103 result = Toolkit::TextVisual::Property::TEXT_COLOR;
105 else if( stringKey == ENABLE_MARKUP_PROPERTY )
107 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
109 else if( stringKey == SHADOW_PROPERTY )
111 result = Toolkit::TextVisual::Property::SHADOW;
113 else if( stringKey == UNDERLINE_PROPERTY )
115 result = Toolkit::TextVisual::Property::UNDERLINE;
117 else if( stringKey == OUTLINE_PROPERTY )
119 result = Toolkit::DevelTextVisual::Property::OUTLINE;
121 else if( stringKey == BACKGROUND_PROPERTY )
123 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
129 void TextColorConstraint( Vector4& current, const PropertyInputContainer& inputs )
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;
138 void OpacityConstraint( float& current, const PropertyInputContainer& inputs )
140 // Make zero if the alpha value of text color is zero to skip rendering text
141 if( EqualsZero( inputs[0]->GetVector4().a ) )
151 } // unnamed namespace
153 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
155 TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
156 textVisualPtr->SetProperties(properties);
157 textVisualPtr->Initialize();
158 return textVisualPtr;
161 Property::Map TextVisual::ConvertStringKeysToIndexKeys( const Property::Map& propertyMap )
163 Property::Map outMap;
165 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
167 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
169 Property::Index indexKey = keyValue.first.indexKey;
171 if ( keyValue.first.type == Property::Key::STRING )
173 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
176 outMap.Insert( indexKey, keyValue.second );
182 float TextVisual::GetHeightForWidth( float width )
184 return mController->GetHeightForWidth( width );
187 void TextVisual::GetNaturalSize( Vector2& naturalSize )
189 naturalSize = mController->GetNaturalSize().GetVectorXY();
192 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
194 Property::Value value;
197 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
200 mController->GetText( text );
201 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
203 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
205 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
206 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
208 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
210 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
212 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
214 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
216 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
218 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
220 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
221 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
223 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
224 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
226 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
227 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
229 GetBackgroundProperties( mController, value, Text::EffectStyle::DEFAULT );
230 map.Insert( Toolkit::DevelTextVisual::Property::BACKGROUND, value );
233 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
236 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
238 mController->GetText( text );
239 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
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 )
252 TextVisual::~TextVisual()
256 void TextVisual::OnInitialize()
258 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
259 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
261 mImpl->mRenderer = Renderer::New(geometry, shader);
264 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
266 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
268 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
270 Property::Index indexKey = keyValue.first.indexKey;
272 if( keyValue.first.type == Property::Key::STRING )
274 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
277 DoSetProperty( indexKey, keyValue.second );
280 // Elide the text if it exceeds the boundaries.
281 mController->SetTextElideEnabled( true );
283 // Retrieve the layout engine to set the cursor's width.
284 Text::Layout::Engine& engine = mController->GetLayoutEngine();
286 // Sets 0 as cursor's width.
287 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
290 void TextVisual::DoSetOnScene( Actor& actor )
294 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
296 // Enable the pre-multiplied alpha to improve the text quality
297 EnablePreMultipliedAlpha(true);
299 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
300 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
302 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
304 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
305 if( shaderTextColorIndex != Property::INVALID_INDEX )
307 Constraint colorConstraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint );
308 colorConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
309 colorConstraint.Apply();
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();
318 // Renderer needs textures and to be added to control
319 mRendererUpdateNeeded = true;
321 mRendererList.push_back( mImpl->mRenderer );
326 void TextVisual::RemoveRenderer( Actor& actor )
328 for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
330 Renderer renderer = (*iter);
333 // Removes the renderer from the actor.
334 actor.RemoveRenderer( renderer );
337 // Clear the renderer list
338 mRendererList.clear();
341 void TextVisual::DoSetOffScene( Actor& actor )
343 RemoveRenderer( actor );
345 // Resets the control handle.
349 void TextVisual::OnSetTransform()
354 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
358 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
360 const bool enableMarkup = propertyValue.Get<bool>();
361 mController->SetMarkupProcessorEnabled( enableMarkup );
364 case Toolkit::TextVisual::Property::TEXT:
366 mController->SetText( propertyValue.Get<std::string>() );
369 case Toolkit::TextVisual::Property::FONT_FAMILY:
371 SetFontFamilyProperty( mController, propertyValue );
374 case Toolkit::TextVisual::Property::FONT_STYLE:
376 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
379 case Toolkit::TextVisual::Property::POINT_SIZE:
381 const float pointSize = propertyValue.Get<float>();
382 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
384 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
388 case Toolkit::TextVisual::Property::MULTI_LINE:
390 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
393 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
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 ) )
400 mController->SetHorizontalAlignment( alignment );
405 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
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) )
412 mController->SetVerticalAlignment( alignment );
417 case Toolkit::TextVisual::Property::TEXT_COLOR:
419 const Vector4& textColor = propertyValue.Get< Vector4 >();
420 if( mController->GetDefaultColor() != textColor )
422 mController->SetDefaultColor( textColor );
426 case Toolkit::TextVisual::Property::SHADOW:
428 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
431 case Toolkit::TextVisual::Property::UNDERLINE:
433 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
436 case Toolkit::DevelTextVisual::Property::OUTLINE:
438 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
441 case Toolkit::DevelTextVisual::Property::BACKGROUND:
443 SetBackgroundProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
449 void TextVisual::UpdateRenderer()
451 Actor control = mControl.GetHandle();
458 // Calculates the size to be used to relayout.
459 Vector2 relayoutSize;
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;
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 ) );
469 mController->GetText( text );
471 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
473 // Remove the texture set and any renderer previously set.
474 RemoveRenderer( control );
476 // Nothing else to do if the relayout size is zero.
477 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
482 Dali::LayoutDirection::Type layoutDirection;
483 if( mController->IsMatchSystemLanguageDirection() )
485 layoutDirection = static_cast<Dali::LayoutDirection::Type>( DevelWindow::Get( control ).GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
489 layoutDirection = static_cast<Dali::LayoutDirection::Type>( control.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
492 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize, layoutDirection );
494 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
495 || mRendererUpdateNeeded )
497 mRendererUpdateNeeded = false;
499 // Remove the texture set and any renderer previously set.
500 RemoveRenderer( control );
502 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
503 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
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 );
509 // Check whether the text contains any color glyph
510 bool containsColorGlyph = false;
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++ )
517 // Retrieve the glyph's info.
518 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
520 // Whether the current glyph is a color one.
521 if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
523 containsColorGlyph = true;
528 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
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 )
534 shadowEnabled = true;
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();;
541 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled );
544 AddRenderer( control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
546 // Text rendered and ready to display
547 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
552 void TextVisual::AddTexture( TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex )
554 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
555 data.GetPixelFormat(),
558 texture.Upload( data );
560 textureSet.SetTexture( textureSetIndex, texture );
561 textureSet.SetSampler( textureSetIndex, sampler );
564 PixelData TextVisual::ConvertToPixelData( unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat )
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,
575 Dali::PixelData::FREE );
579 void TextVisual::CreateTextureSet( TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
582 TextureSet textureSet = TextureSet::New();
583 unsigned int textureSetIndex = 0u;
585 // Convert the buffer to pixel data to make it a texture.
586 if( info.textBuffer )
588 PixelData data = ConvertToPixelData( info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat );
589 AddTexture( textureSet, data, sampler, textureSetIndex );
593 if( styleEnabled && info.styleBuffer )
595 PixelData styleData = ConvertToPixelData( info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888 );
596 AddTexture( textureSet, styleData, sampler, textureSetIndex );
600 if( containsColorGlyph && !hasMultipleTextColors && info.maskBuffer )
602 PixelData maskData = ConvertToPixelData( info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8 );
603 AddTexture( textureSet, maskData, sampler, textureSetIndex );
606 renderer.SetTextures( textureSet );
608 //Register transform properties
609 mImpl->mTransform.RegisterUniforms( renderer, Direction::LEFT_TO_RIGHT );
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 );
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);
621 mRendererList.push_back( renderer );
625 void TextVisual::AddRenderer( Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
627 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
628 mImpl->mRenderer.SetShader( shader );
630 // Get the maximum size.
631 const int maxTextureSize = Dali::GetMaxTextureSize();
633 // No tiling required. Use the default renderer.
634 if( size.height < maxTextureSize )
636 TextureSet textureSet = GetTextTexture( size, hasMultipleTextColors, containsColorGlyph, styleEnabled );
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);
644 mRendererList.push_back( mImpl->mRenderer );
646 // If the pixel data exceeds the maximum size, tiling is required.
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 );
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;
656 // Check the text direction
657 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
659 // Create a texture for the text without any styles
660 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
662 int verifiedWidth = data.GetWidth();
663 int verifiedHeight = data.GetHeight();
665 // Set information for creating textures.
666 TilingInfo info( verifiedWidth, maxTextureSize, textPixelFormat );
668 // Get the buffer of text.
669 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( data );
670 info.textBuffer = textPixelData.buffer;
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;
680 if ( containsColorGlyph && !hasMultipleTextColors )
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;
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 );
694 offsetValue->Get( info.offSet );
697 // Create a textureset in the default renderer.
698 CreateTextureSet( info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
700 verifiedHeight -= maxTextureSize;
702 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
704 int offsetPosition = verifiedWidth * maxTextureSize;
705 // Create a renderer by cutting maxTextureSize.
706 while( verifiedHeight > 0 )
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 );
719 verifiedHeight -= maxTextureSize;
723 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
725 for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
727 Renderer renderer = (*iter);
730 actor.AddRenderer( renderer );
736 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
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 );
742 TextureSet textureSet = TextureSet::New();
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;
747 // Check the text direction
748 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
750 // Create a texture for the text without any styles
751 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
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;
757 AddTexture( textureSet, data, sampler, textureSetIndex );
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 );
765 AddTexture( textureSet, styleData, sampler, textureSetIndex );
769 if ( containsColorGlyph && !hasMultipleTextColors )
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 );
774 AddTexture( textureSet, maskData, sampler, textureSetIndex );
780 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
784 if( hasMultipleTextColors && !styleEnabled )
786 // We don't animate text color if the text contains multiple colors
787 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
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 );
795 else if( hasMultipleTextColors && styleEnabled )
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 );
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 );
806 else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
808 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
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 );
816 else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
818 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
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 );
826 else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
828 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
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 );
836 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
838 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
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 );
850 } // namespace Internal
852 } // namespace Toolkit