2 * Copyright (c) 2018 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/images/pixel-data-devel.h>
30 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
31 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
33 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
34 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
35 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/internal/text/text-font-style.h>
39 #include <dali-toolkit/internal/text/text-effects-style.h>
40 #include <dali-toolkit/internal/text/script-run.h>
41 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
42 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
55 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
57 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
58 attribute mediump vec2 aPosition;\n
59 uniform highp mat4 uMvpMatrix;\n
60 uniform mediump vec3 uSize;\n
61 uniform mediump vec4 pixelArea;\n
63 varying mediump vec2 vTexCoord;\n
65 //Visual size and offset
66 uniform mediump vec2 offset;\n
67 uniform mediump vec2 size;\n
68 uniform mediump vec4 offsetSizeMode;\n
69 uniform mediump vec2 origin;\n
70 uniform mediump vec2 anchorPoint;\n
72 vec4 ComputeVertexPosition()\n
74 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
75 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
76 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
81 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
82 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
86 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT = DALI_COMPOSE_SHADER(
87 varying mediump vec2 vTexCoord;\n
88 uniform sampler2D sTexture;\n
89 uniform lowp vec4 uTextColorAnimatable;\n
90 uniform lowp vec4 uColor;\n
91 uniform lowp vec3 mixColor;\n
95 mediump float textTexture = texture2D( sTexture, vTexCoord ).r;\n
97 // Set the color of the text to what it is animated to.
98 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, 1.0 );
102 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
103 varying mediump vec2 vTexCoord;\n
104 uniform sampler2D sTexture;\n
105 uniform lowp vec4 uColor;\n
106 uniform lowp vec3 mixColor;\n
110 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
112 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );
116 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
117 varying mediump vec2 vTexCoord;\n
118 uniform sampler2D sTexture;\n
119 uniform sampler2D sStyle;\n
120 uniform lowp vec4 uTextColorAnimatable;\n
121 uniform lowp vec4 uColor;\n
122 uniform lowp vec3 mixColor;\n
126 mediump float textTexture = texture2D( sTexture, vTexCoord ).r;\n
127 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
129 // Draw the text as overlay above the style
130 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - uTextColorAnimatable.a * textTexture ) ) * uColor * vec4( mixColor, 1.0 );\n
134 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
135 varying mediump vec2 vTexCoord;\n
136 uniform sampler2D sTexture;\n
137 uniform sampler2D sStyle;\n
138 uniform lowp vec4 uColor;\n
139 uniform lowp vec3 mixColor;\n
143 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
144 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
146 // Draw the text as overlay above the style
147 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, 1.0 );\n
151 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
152 varying mediump vec2 vTexCoord;\n
153 uniform sampler2D sTexture;\n
154 uniform sampler2D sMask;\n
155 uniform lowp vec4 uTextColorAnimatable;\n
156 uniform lowp vec4 uColor;\n
157 uniform lowp vec3 mixColor;\n
161 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
162 mediump float maskTexture = texture2D( sMask, vTexCoord ).r;\n
164 // Set the color of non-transparent pixel in text to what it is animated to.
165 // Markup text with multiple text colors are not animated (but can be supported later on if required).
166 // Emoji color are not animated.
167 mediump float vstep = step( 0.0001, textTexture.a );\n
168 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture );\n
170 // Draw the text as overlay above the style
171 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );\n
175 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
176 varying mediump vec2 vTexCoord;\n
177 uniform sampler2D sTexture;\n
178 uniform sampler2D sStyle;\n
179 uniform sampler2D sMask;\n
180 uniform lowp float uHasMultipleTextColors;\n
181 uniform lowp vec4 uTextColorAnimatable;\n
182 uniform lowp vec4 uColor;\n
183 uniform lowp vec3 mixColor;\n
187 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
188 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
189 mediump float maskTexture = texture2D( sMask, vTexCoord ).r;\n
191 // Set the color of non-transparent pixel in text to what it is animated to.
192 // Markup text with multiple text colors are not animated (but can be supported later on if required).
193 // Emoji color are not animated.
194 mediump float vstep = step( 0.0001, textTexture.a );\n
195 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) );\n
197 // Draw the text as overlay above the style
198 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, 1.0 );\n
203 * Return Property index for the given string key
204 * param[in] stringKey the string index key
205 * return the key as an index
208 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
210 Dali::Property::Index result = Property::INVALID_KEY;
212 if( stringKey == VISUAL_TYPE )
214 result = Toolkit::Visual::Property::TYPE;
216 else if( stringKey == TEXT_PROPERTY )
218 result = Toolkit::TextVisual::Property::TEXT;
220 else if( stringKey == FONT_FAMILY_PROPERTY )
222 result = Toolkit::TextVisual::Property::FONT_FAMILY;
224 else if( stringKey == FONT_STYLE_PROPERTY )
226 result = Toolkit::TextVisual::Property::FONT_STYLE;
228 else if( stringKey == POINT_SIZE_PROPERTY )
230 result = Toolkit::TextVisual::Property::POINT_SIZE;
232 else if( stringKey == MULTI_LINE_PROPERTY )
234 result = Toolkit::TextVisual::Property::MULTI_LINE;
236 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
238 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
240 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
242 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
244 else if( stringKey == TEXT_COLOR_PROPERTY )
246 result = Toolkit::TextVisual::Property::TEXT_COLOR;
248 else if( stringKey == ENABLE_MARKUP_PROPERTY )
250 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
252 else if( stringKey == SHADOW_PROPERTY )
254 result = Toolkit::TextVisual::Property::SHADOW;
256 else if( stringKey == UNDERLINE_PROPERTY )
258 result = Toolkit::TextVisual::Property::UNDERLINE;
260 else if( stringKey == OUTLINE_PROPERTY )
262 result = Toolkit::DevelTextVisual::Property::OUTLINE;
264 else if( stringKey == BACKGROUND_PROPERTY )
266 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
272 void TextColorConstraint( Vector4& current, const PropertyInputContainer& inputs )
274 Vector4 color = inputs[0]->GetVector4();
275 current.r = color.r * color.a;
276 current.g = color.g * color.a;
277 current.b = color.b * color.a;
281 void OpacityConstraint( float& current, const PropertyInputContainer& inputs )
283 // Make zero if the alpha value of text color is zero to skip rendering text
284 if( EqualsZero( inputs[0]->GetVector4().a ) )
294 } // unnamed namespace
296 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
298 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
299 TextVisualPtr->SetProperties( properties );
300 return TextVisualPtr;
303 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
305 Property::Map outMap;
307 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
309 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
311 Property::Index indexKey = keyValue.first.indexKey;
313 if ( keyValue.first.type == Property::Key::STRING )
315 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
318 outMap.Insert( indexKey, keyValue.second );
321 propertyMap = outMap;
324 float TextVisual::GetHeightForWidth( float width )
326 return mController->GetHeightForWidth( width );
329 void TextVisual::GetNaturalSize( Vector2& naturalSize )
331 naturalSize = mController->GetNaturalSize().GetVectorXY();
334 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
336 Property::Value value;
339 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
342 mController->GetText( text );
343 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
345 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
347 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
348 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
350 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
352 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
354 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
356 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
358 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
360 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
362 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
363 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
365 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
366 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
368 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
369 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
371 GetBackgroundProperties( mController, value, Text::EffectStyle::DEFAULT );
372 map.Insert( Toolkit::DevelTextVisual::Property::BACKGROUND, value );
375 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
378 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
380 mController->GetText( text );
381 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
385 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
386 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT ),
387 mController( Text::Controller::New() ),
388 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
389 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
390 mRendererUpdateNeeded( false )
394 TextVisual::~TextVisual()
398 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
400 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
402 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
404 Property::Index indexKey = keyValue.first.indexKey;
406 if( keyValue.first.type == Property::Key::STRING )
408 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
411 DoSetProperty( indexKey, keyValue.second );
414 // Elide the text if it exceeds the boundaries.
415 mController->SetTextElideEnabled( true );
417 // Retrieve the layout engine to set the cursor's width.
418 Text::Layout::Engine& engine = mController->GetLayoutEngine();
420 // Sets 0 as cursor's width.
421 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
424 void TextVisual::DoSetOnStage( Actor& actor )
428 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
429 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
431 mImpl->mRenderer = Renderer::New( geometry, shader );
432 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
434 // Enable the pre-multiplied alpha to improve the text quality
435 EnablePreMultipliedAlpha(true);
437 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
438 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
440 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
442 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
443 if( shaderTextColorIndex != Property::INVALID_INDEX )
445 Constraint colorConstraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint );
446 colorConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
447 colorConstraint.Apply();
449 // Make zero if the alpha value of text color is zero to skip rendering text
450 Constraint opacityConstraint = Constraint::New< float >( mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint );
451 opacityConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
452 opacityConstraint.Apply();
456 // Renderer needs textures and to be added to control
457 mRendererUpdateNeeded = true;
459 mRendererList.push_back( mImpl->mRenderer );
464 void TextVisual::RemoveRenderer( Actor& actor )
466 for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
468 Renderer renderer = (*iter);
471 // Removes the renderer from the actor.
472 actor.RemoveRenderer( renderer );
475 // Clear the renderer list
476 mRendererList.clear();
479 void TextVisual::DoSetOffStage( Actor& actor )
481 RemoveRenderer( actor );
483 // Resets the renderer.
484 mImpl->mRenderer.Reset();
486 // Resets the control handle.
490 void TextVisual::OnSetTransform()
495 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
499 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
501 const bool enableMarkup = propertyValue.Get<bool>();
502 mController->SetMarkupProcessorEnabled( enableMarkup );
505 case Toolkit::TextVisual::Property::TEXT:
507 mController->SetText( propertyValue.Get<std::string>() );
510 case Toolkit::TextVisual::Property::FONT_FAMILY:
512 SetFontFamilyProperty( mController, propertyValue );
515 case Toolkit::TextVisual::Property::FONT_STYLE:
517 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
520 case Toolkit::TextVisual::Property::POINT_SIZE:
522 const float pointSize = propertyValue.Get<float>();
523 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
525 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
529 case Toolkit::TextVisual::Property::MULTI_LINE:
531 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
534 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
538 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
539 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
541 mController->SetHorizontalAlignment( alignment );
546 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
550 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
551 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
553 mController->SetVerticalAlignment( alignment );
558 case Toolkit::TextVisual::Property::TEXT_COLOR:
560 const Vector4& textColor = propertyValue.Get< Vector4 >();
561 if( mController->GetDefaultColor() != textColor )
563 mController->SetDefaultColor( textColor );
567 case Toolkit::TextVisual::Property::SHADOW:
569 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
572 case Toolkit::TextVisual::Property::UNDERLINE:
574 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
577 case Toolkit::DevelTextVisual::Property::OUTLINE:
579 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
582 case Toolkit::DevelTextVisual::Property::BACKGROUND:
584 SetBackgroundProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
590 void TextVisual::UpdateRenderer()
592 Actor control = mControl.GetHandle();
599 // Calculates the size to be used to relayout.
600 Vector2 relayoutSize;
602 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
603 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
605 // Round the size and offset to avoid pixel alignement issues.
606 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
607 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
610 mController->GetText( text );
612 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
614 // Remove the texture set and any renderer previously set.
615 RemoveRenderer( control );
617 // Nothing else to do if the relayout size is zero.
618 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
622 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( control.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
624 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize, layoutDirection );
626 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
627 || mRendererUpdateNeeded )
629 mRendererUpdateNeeded = false;
631 // Remove the texture set and any renderer previously set.
632 RemoveRenderer( control );
634 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
635 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
637 // Check whether it is a markup text with multiple text colors
638 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
639 bool hasMultipleTextColors = ( NULL != colorsBuffer );
641 // Check whether the text contains any color glyph
642 bool containsColorGlyph = false;
644 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
645 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
646 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
647 for ( Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++ )
649 // Retrieve the glyph's info.
650 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
652 // Whether the current glyph is a color one.
653 if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
655 containsColorGlyph = true;
660 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
662 bool shadowEnabled = false;
663 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
664 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
666 shadowEnabled = true;
669 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
670 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
671 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();;
673 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled );
676 AddRenderer( control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
678 // Text rendered and ready to display
679 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
684 void TextVisual::AddTexture( TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex )
686 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
687 data.GetPixelFormat(),
690 texture.Upload( data );
692 textureSet.SetTexture( textureSetIndex, texture );
693 textureSet.SetSampler( textureSetIndex, sampler );
696 PixelData TextVisual::ConvertToPixelData( unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat )
698 int bpp = Pixel::GetBytesPerPixel( textPixelFormat );
699 unsigned int bufferSize = width * height * bpp;
700 unsigned char* dstBuffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
701 memcpy( dstBuffer, buffer + offsetPosition * bpp, bufferSize );
702 PixelData pixelData = Dali::PixelData::New( dstBuffer,
707 Dali::PixelData::FREE );
711 void TextVisual::CreateTextureSet( TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
714 TextureSet textureSet = TextureSet::New();
715 unsigned int textureSetIndex = 0u;
717 // Convert the buffer to pixel data to make it a texture.
718 if( info.textBuffer )
720 PixelData data = ConvertToPixelData( info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat );
721 AddTexture( textureSet, data, sampler, textureSetIndex );
725 if( styleEnabled && info.styleBuffer )
727 PixelData styleData = ConvertToPixelData( info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888 );
728 AddTexture( textureSet, styleData, sampler, textureSetIndex );
732 if( containsColorGlyph && !hasMultipleTextColors && info.maskBuffer )
734 PixelData maskData = ConvertToPixelData( info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8 );
735 AddTexture( textureSet, maskData, sampler, textureSetIndex );
738 renderer.SetTextures( textureSet );
740 //Register transform properties
741 mImpl->mTransform.RegisterUniforms( renderer, Direction::LEFT_TO_RIGHT );
743 // Enable the pre-multiplied alpha to improve the text quality
744 renderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
745 renderer.RegisterProperty( PREMULTIPLIED_ALPHA, 1.0f );
747 // Set size and offset for the tiling.
748 renderer.RegisterProperty( SIZE, Vector2( info.width, info.height ) );
749 renderer.RegisterProperty( OFFSET, Vector2( info.offSet.x, info.offSet.y ) );
750 renderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
751 renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
753 mRendererList.push_back( renderer );
757 void TextVisual::AddRenderer( Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
759 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
760 mImpl->mRenderer.SetShader( shader );
762 // Get the maximum size.
763 const int maxTextureSize = Dali::GetMaxTextureSize();
765 // No tiling required. Use the default renderer.
766 if( size.height < maxTextureSize )
768 TextureSet textureSet = GetTextTexture( size, hasMultipleTextColors, containsColorGlyph, styleEnabled );
770 mImpl->mRenderer.SetTextures( textureSet );
771 //Register transform properties
772 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
773 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
774 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
776 mRendererList.push_back( mImpl->mRenderer );
778 // If the pixel data exceeds the maximum size, tiling is required.
781 // Filter mode needs to be set to linear to produce better quality while scaling.
782 Sampler sampler = Sampler::New();
783 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
785 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
786 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
788 // Check the text direction
789 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
791 // Create a texture for the text without any styles
792 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
794 int verifiedWidth = data.GetWidth();
795 int verifiedHeight = data.GetHeight();
797 // Set information for creating textures.
798 TilingInfo info( verifiedWidth, maxTextureSize, textPixelFormat );
800 // Get the buffer of text.
801 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( data );
802 info.textBuffer = textPixelData.buffer;
806 // Create RGBA texture for all the text styles (without the text itself)
807 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
808 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( styleData );
809 info.styleBuffer = stylePixelData.buffer;
812 if ( containsColorGlyph && !hasMultipleTextColors )
814 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
815 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
816 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer( maskData );
817 info.maskBuffer = maskPixelData.buffer;
820 // Get the current offset for recalculate the offset when tiling.
821 Property::Map retMap;
822 mImpl->mTransform.GetPropertyMap( retMap );
823 Vector2 offSet = retMap.Find( Dali::Toolkit::Visual::Transform::Property::OFFSET )->Get< Vector2 >();
824 info.offSet = offSet;
826 // Create a textureset in the default renderer.
827 CreateTextureSet( info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
829 verifiedHeight -= maxTextureSize;
831 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
833 int offsetPosition = verifiedWidth * maxTextureSize;
834 // Create a renderer by cutting maxTextureSize.
835 while( verifiedHeight > 0 )
837 Renderer tilingRenderer = Renderer::New( geometry, shader );
838 tilingRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
839 // New offset position of buffer for tiling.
840 info.offsetPosition += offsetPosition;
841 // New height for tiling.
842 info.height = ( verifiedHeight - maxTextureSize ) > 0 ? maxTextureSize : verifiedHeight;
843 // New offset for tiling.
844 info.offSet.y += maxTextureSize;
845 // Create a textureset int the new tiling renderer.
846 CreateTextureSet( info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled );
848 verifiedHeight -= maxTextureSize;
852 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
854 for( RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
856 Renderer renderer = (*iter);
859 actor.AddRenderer( renderer );
865 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
867 // Filter mode needs to be set to linear to produce better quality while scaling.
868 Sampler sampler = Sampler::New();
869 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
871 TextureSet textureSet = TextureSet::New();
873 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
874 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
876 // Check the text direction
877 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
879 // Create a texture for the text without any styles
880 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
882 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
883 // In that case, create a texture. TODO: should tile the text.
884 unsigned int textureSetIndex = 0u;
886 AddTexture( textureSet, data, sampler, textureSetIndex );
891 // Create RGBA texture for all the text styles (without the text itself)
892 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
894 AddTexture( textureSet, styleData, sampler, textureSetIndex );
898 if ( containsColorGlyph && !hasMultipleTextColors )
900 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
901 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
903 AddTexture( textureSet, maskData, sampler, textureSetIndex );
909 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
913 if( hasMultipleTextColors && !styleEnabled )
915 // We don't animate text color if the text contains multiple colors
916 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
919 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
920 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
921 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
924 else if( hasMultipleTextColors && styleEnabled )
926 // We don't animate text color if the text contains multiple colors
927 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
930 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
931 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
932 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
935 else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
937 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
940 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
941 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
942 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
945 else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
947 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
950 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
951 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
952 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
955 else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
957 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
960 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
961 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
962 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
965 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
967 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
970 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
971 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
972 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
979 } // namespace Internal
981 } // namespace Toolkit