2 * Copyright (c) 2020 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 return TextVisualPtr;
160 Property::Map TextVisual::ConvertStringKeysToIndexKeys( const Property::Map& propertyMap )
162 Property::Map outMap;
164 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
166 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
168 Property::Index indexKey = keyValue.first.indexKey;
170 if ( keyValue.first.type == Property::Key::STRING )
172 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
175 outMap.Insert( indexKey, keyValue.second );
181 float TextVisual::GetHeightForWidth( float width )
183 return mController->GetHeightForWidth( width );
186 void TextVisual::GetNaturalSize( Vector2& naturalSize )
188 naturalSize = mController->GetNaturalSize().GetVectorXY();
191 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
193 Property::Value value;
196 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
199 mController->GetText( text );
200 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
202 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
204 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
205 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
207 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
209 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
211 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
213 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
215 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
217 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
219 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
220 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
222 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
223 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
225 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
226 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
228 GetBackgroundProperties( mController, value, Text::EffectStyle::DEFAULT );
229 map.Insert( Toolkit::DevelTextVisual::Property::BACKGROUND, value );
232 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
235 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
237 mController->GetText( text );
238 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
242 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
243 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT ),
244 mController( Text::Controller::New() ),
245 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
246 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
247 mRendererUpdateNeeded( false )
251 TextVisual::~TextVisual()
255 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
257 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
259 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
261 Property::Index indexKey = keyValue.first.indexKey;
263 if( keyValue.first.type == Property::Key::STRING )
265 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
268 DoSetProperty( indexKey, keyValue.second );
271 // Elide the text if it exceeds the boundaries.
272 mController->SetTextElideEnabled( true );
274 // Retrieve the layout engine to set the cursor's width.
275 Text::Layout::Engine& engine = mController->GetLayoutEngine();
277 // Sets 0 as cursor's width.
278 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
281 void TextVisual::DoSetOnScene( Actor& actor )
285 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
286 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
288 mImpl->mRenderer = Renderer::New( geometry, shader );
289 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
291 // Enable the pre-multiplied alpha to improve the text quality
292 EnablePreMultipliedAlpha(true);
294 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
295 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
297 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
299 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
300 if( shaderTextColorIndex != Property::INVALID_INDEX )
302 Constraint colorConstraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint );
303 colorConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
304 colorConstraint.Apply();
306 // Make zero if the alpha value of text color is zero to skip rendering text
307 Constraint opacityConstraint = Constraint::New< float >( mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint );
308 opacityConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
309 opacityConstraint.Apply();
313 // Renderer needs textures and to be added to control
314 mRendererUpdateNeeded = true;
316 mRendererList.push_back( mImpl->mRenderer );
321 void TextVisual::RemoveRenderer( Actor& actor )
323 for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
325 Renderer renderer = (*iter);
328 // Removes the renderer from the actor.
329 actor.RemoveRenderer( renderer );
332 // Clear the renderer list
333 mRendererList.clear();
336 void TextVisual::DoSetOffScene( Actor& actor )
338 RemoveRenderer( actor );
340 // Resets the renderer.
341 mImpl->mRenderer.Reset();
343 // Resets the control handle.
347 void TextVisual::OnSetTransform()
352 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
356 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
358 const bool enableMarkup = propertyValue.Get<bool>();
359 mController->SetMarkupProcessorEnabled( enableMarkup );
362 case Toolkit::TextVisual::Property::TEXT:
364 mController->SetText( propertyValue.Get<std::string>() );
367 case Toolkit::TextVisual::Property::FONT_FAMILY:
369 SetFontFamilyProperty( mController, propertyValue );
372 case Toolkit::TextVisual::Property::FONT_STYLE:
374 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
377 case Toolkit::TextVisual::Property::POINT_SIZE:
379 const float pointSize = propertyValue.Get<float>();
380 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
382 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
386 case Toolkit::TextVisual::Property::MULTI_LINE:
388 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
391 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
395 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
396 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
398 mController->SetHorizontalAlignment( alignment );
403 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
407 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
408 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
410 mController->SetVerticalAlignment( alignment );
415 case Toolkit::TextVisual::Property::TEXT_COLOR:
417 const Vector4& textColor = propertyValue.Get< Vector4 >();
418 if( mController->GetDefaultColor() != textColor )
420 mController->SetDefaultColor( textColor );
424 case Toolkit::TextVisual::Property::SHADOW:
426 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
429 case Toolkit::TextVisual::Property::UNDERLINE:
431 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
434 case Toolkit::DevelTextVisual::Property::OUTLINE:
436 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
439 case Toolkit::DevelTextVisual::Property::BACKGROUND:
441 SetBackgroundProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
447 void TextVisual::UpdateRenderer()
449 Actor control = mControl.GetHandle();
456 // Calculates the size to be used to relayout.
457 Vector2 relayoutSize;
459 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
460 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
462 // Round the size and offset to avoid pixel alignement issues.
463 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
464 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
467 mController->GetText( text );
469 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
471 // Remove the texture set and any renderer previously set.
472 RemoveRenderer( control );
474 // Nothing else to do if the relayout size is zero.
475 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
480 Dali::LayoutDirection::Type layoutDirection;
481 if( mController->IsMatchSystemLanguageDirection() )
483 layoutDirection = static_cast<Dali::LayoutDirection::Type>( DevelWindow::Get( control ).GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
487 layoutDirection = static_cast<Dali::LayoutDirection::Type>( control.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
490 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize, layoutDirection );
492 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
493 || mRendererUpdateNeeded )
495 mRendererUpdateNeeded = false;
497 // Remove the texture set and any renderer previously set.
498 RemoveRenderer( control );
500 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
501 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
503 // Check whether it is a markup text with multiple text colors
504 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
505 bool hasMultipleTextColors = ( NULL != colorsBuffer );
507 // Check whether the text contains any color glyph
508 bool containsColorGlyph = false;
510 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
511 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
512 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
513 for ( Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++ )
515 // Retrieve the glyph's info.
516 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
518 // Whether the current glyph is a color one.
519 if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
521 containsColorGlyph = true;
526 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
528 bool shadowEnabled = false;
529 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
530 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
532 shadowEnabled = true;
535 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
536 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
537 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();;
539 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled );
542 AddRenderer( control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
544 // Text rendered and ready to display
545 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
550 void TextVisual::AddTexture( TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex )
552 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
553 data.GetPixelFormat(),
556 texture.Upload( data );
558 textureSet.SetTexture( textureSetIndex, texture );
559 textureSet.SetSampler( textureSetIndex, sampler );
562 PixelData TextVisual::ConvertToPixelData( unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat )
564 int bpp = Pixel::GetBytesPerPixel( textPixelFormat );
565 unsigned int bufferSize = width * height * bpp;
566 unsigned char* dstBuffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
567 memcpy( dstBuffer, buffer + offsetPosition * bpp, bufferSize );
568 PixelData pixelData = Dali::PixelData::New( dstBuffer,
573 Dali::PixelData::FREE );
577 void TextVisual::CreateTextureSet( TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
580 TextureSet textureSet = TextureSet::New();
581 unsigned int textureSetIndex = 0u;
583 // Convert the buffer to pixel data to make it a texture.
584 if( info.textBuffer )
586 PixelData data = ConvertToPixelData( info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat );
587 AddTexture( textureSet, data, sampler, textureSetIndex );
591 if( styleEnabled && info.styleBuffer )
593 PixelData styleData = ConvertToPixelData( info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888 );
594 AddTexture( textureSet, styleData, sampler, textureSetIndex );
598 if( containsColorGlyph && !hasMultipleTextColors && info.maskBuffer )
600 PixelData maskData = ConvertToPixelData( info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8 );
601 AddTexture( textureSet, maskData, sampler, textureSetIndex );
604 renderer.SetTextures( textureSet );
606 //Register transform properties
607 mImpl->mTransform.RegisterUniforms( renderer, Direction::LEFT_TO_RIGHT );
609 // Enable the pre-multiplied alpha to improve the text quality
610 renderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
611 renderer.RegisterProperty( PREMULTIPLIED_ALPHA, 1.0f );
613 // Set size and offset for the tiling.
614 renderer.RegisterProperty( SIZE, Vector2( info.width, info.height ) );
615 renderer.RegisterProperty( OFFSET, Vector2( info.offSet.x, info.offSet.y ) );
616 renderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
617 renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
619 mRendererList.push_back( renderer );
623 void TextVisual::AddRenderer( Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
625 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
626 mImpl->mRenderer.SetShader( shader );
628 // Get the maximum size.
629 const int maxTextureSize = Dali::GetMaxTextureSize();
631 // No tiling required. Use the default renderer.
632 if( size.height < maxTextureSize )
634 TextureSet textureSet = GetTextTexture( size, hasMultipleTextColors, containsColorGlyph, styleEnabled );
636 mImpl->mRenderer.SetTextures( textureSet );
637 //Register transform properties
638 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
639 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
640 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
642 mRendererList.push_back( mImpl->mRenderer );
644 // If the pixel data exceeds the maximum size, tiling is required.
647 // Filter mode needs to be set to linear to produce better quality while scaling.
648 Sampler sampler = Sampler::New();
649 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
651 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
652 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
654 // Check the text direction
655 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
657 // Create a texture for the text without any styles
658 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
660 int verifiedWidth = data.GetWidth();
661 int verifiedHeight = data.GetHeight();
663 // Set information for creating textures.
664 TilingInfo info( verifiedWidth, maxTextureSize, textPixelFormat );
666 // Get the buffer of text.
667 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( data );
668 info.textBuffer = textPixelData.buffer;
672 // Create RGBA texture for all the text styles (without the text itself)
673 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
674 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( styleData );
675 info.styleBuffer = stylePixelData.buffer;
678 if ( containsColorGlyph && !hasMultipleTextColors )
680 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
681 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
682 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( maskData );
683 info.maskBuffer = maskPixelData.buffer;
686 // Get the current offset for recalculate the offset when tiling.
687 Property::Map retMap;
688 mImpl->mTransform.GetPropertyMap( retMap );
689 Property::Value* offsetValue = retMap.Find( Dali::Toolkit::Visual::Transform::Property::OFFSET );
692 offsetValue->Get( info.offSet );
695 // Create a textureset in the default renderer.
696 CreateTextureSet( info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
698 verifiedHeight -= maxTextureSize;
700 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
702 int offsetPosition = verifiedWidth * maxTextureSize;
703 // Create a renderer by cutting maxTextureSize.
704 while( verifiedHeight > 0 )
706 Renderer tilingRenderer = Renderer::New( geometry, shader );
707 tilingRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
708 // New offset position of buffer for tiling.
709 info.offsetPosition += offsetPosition;
710 // New height for tiling.
711 info.height = ( verifiedHeight - maxTextureSize ) > 0 ? maxTextureSize : verifiedHeight;
712 // New offset for tiling.
713 info.offSet.y += maxTextureSize;
714 // Create a textureset int the new tiling renderer.
715 CreateTextureSet( info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
717 verifiedHeight -= maxTextureSize;
721 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
723 for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
725 Renderer renderer = (*iter);
728 actor.AddRenderer( renderer );
734 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
736 // Filter mode needs to be set to linear to produce better quality while scaling.
737 Sampler sampler = Sampler::New();
738 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
740 TextureSet textureSet = TextureSet::New();
742 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
743 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
745 // Check the text direction
746 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
748 // Create a texture for the text without any styles
749 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
751 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
752 // In that case, create a texture. TODO: should tile the text.
753 unsigned int textureSetIndex = 0u;
755 AddTexture( textureSet, data, sampler, textureSetIndex );
760 // Create RGBA texture for all the text styles (without the text itself)
761 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
763 AddTexture( textureSet, styleData, sampler, textureSetIndex );
767 if ( containsColorGlyph && !hasMultipleTextColors )
769 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
770 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
772 AddTexture( textureSet, maskData, sampler, textureSetIndex );
778 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
782 if( hasMultipleTextColors && !styleEnabled )
784 // We don't animate text color if the text contains multiple colors
785 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
788 shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG );
789 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
790 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
793 else if( hasMultipleTextColors && styleEnabled )
795 // We don't animate text color if the text contains multiple colors
796 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
799 shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG );
800 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
801 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
804 else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
806 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
809 shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG );
810 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
811 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
814 else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
816 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
819 shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG );
820 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
821 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
824 else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
826 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
829 shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG );
830 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
831 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
834 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
836 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
839 shader = Shader::New( SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG );
840 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
841 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
848 } // namespace Internal
850 } // namespace Toolkit