2 * Copyright (c) 2017 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/text-abstraction/text-abstraction-definitions.h>
26 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
29 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
30 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/text/text-font-style.h>
34 #include <dali-toolkit/internal/text/text-effects-style.h>
35 #include <dali-toolkit/internal/text/script-run.h>
49 // Property names - common properties defined in visual-string-constants.h/cpp
50 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
51 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
52 const char * const POINT_SIZE_PROPERTY( "pointSize" );
53 const char * const MULTI_LINE_PROPERTY( "multiLine" );
54 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
55 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
56 const char * const TEXT_COLOR_PROPERTY( "textColor" );
57 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
58 const char * const SHADOW_PROPERTY( "shadow" );
59 const char * const UNDERLINE_PROPERTY( "underline" );
61 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
63 { "BEGIN", Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN },
64 { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
65 { "END", Toolkit::Text::Layout::HORIZONTAL_ALIGN_END },
67 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
69 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
71 { "TOP", Toolkit::Text::Layout::VERTICAL_ALIGN_TOP },
72 { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
73 { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
75 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
77 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
79 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
81 const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
82 HORIZONTAL_ALIGNMENT_STRING_TABLE,
83 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
87 return std::string( name );
92 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
94 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
95 VERTICAL_ALIGNMENT_STRING_TABLE,
96 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
100 return std::string( name );
102 return std::string();
105 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
106 attribute mediump vec2 aPosition;\n
107 uniform mediump mat4 uMvpMatrix;\n
108 uniform mediump vec3 uSize;\n
109 uniform mediump vec4 pixelArea;\n
111 uniform mediump mat4 uModelMatrix;\n
112 uniform mediump mat4 uViewMatrix;\n
113 uniform mediump mat4 uProjection;\n
115 varying mediump vec2 vTexCoord;\n
117 //Visual size and offset
118 uniform mediump vec2 offset;\n
119 uniform mediump vec2 size;\n
120 uniform mediump vec4 offsetSizeMode;\n
121 uniform mediump vec2 origin;\n
122 uniform mediump vec2 anchorPoint;\n
124 vec4 ComputeVertexPosition()\n
126 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
127 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
128 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
133 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
134 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
135 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
137 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
138 gl_Position = vertexPosition;\n
142 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT = DALI_COMPOSE_SHADER(
143 varying mediump vec2 vTexCoord;\n
144 uniform sampler2D sTexture;\n
145 uniform lowp vec4 uTextColorAnimatable;\n
146 uniform mediump vec4 uAtlasRect;\n
147 uniform lowp vec4 uColor;\n
148 uniform lowp vec3 mixColor;\n
149 uniform lowp float opacity;\n
153 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
154 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
156 // Set the color of the text to what it is animated to.
157 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, opacity );
161 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
162 varying mediump vec2 vTexCoord;\n
163 uniform sampler2D sTexture;\n
164 uniform mediump vec4 uAtlasRect;\n
165 uniform lowp vec4 uColor;\n
166 uniform lowp vec3 mixColor;\n
167 uniform lowp float opacity;\n
171 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
172 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
174 gl_FragColor = textTexture * uColor * vec4( mixColor, opacity );
178 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
179 varying mediump vec2 vTexCoord;\n
180 uniform sampler2D sTexture;\n
181 uniform sampler2D sStyle;\n
182 uniform lowp vec4 uTextColorAnimatable;\n
183 uniform mediump vec4 uAtlasRect;\n
184 uniform lowp vec4 uColor;\n
185 uniform lowp vec3 mixColor;\n
186 uniform lowp float opacity;\n
190 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
191 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
192 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
194 // Draw the text as overlay above the style
195 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - textTexture ) ) * uColor * vec4( mixColor, opacity );\n
199 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
200 varying mediump vec2 vTexCoord;\n
201 uniform sampler2D sTexture;\n
202 uniform sampler2D sStyle;\n
203 uniform mediump vec4 uAtlasRect;\n
204 uniform lowp vec4 uColor;\n
205 uniform lowp vec3 mixColor;\n
206 uniform lowp float opacity;\n
210 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
211 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
212 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
214 // Draw the text as overlay above the style
215 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
219 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
220 varying mediump vec2 vTexCoord;\n
221 uniform sampler2D sTexture;\n
222 uniform sampler2D sMask;\n
223 uniform lowp vec4 uTextColorAnimatable;\n
224 uniform mediump vec4 uAtlasRect;\n
225 uniform lowp vec4 uColor;\n
226 uniform lowp vec3 mixColor;\n
227 uniform lowp float opacity;\n
231 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
232 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
233 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
235 // Set the color of non-transparent pixel in text to what it is animated to.
236 // Markup text with multiple text colors are not animated (but can be supported later on if required).
237 // Emoji color are not animated.
238 mediump vec4 textColor = textTexture * textTexture.a;\n
239 mediump float vstep = step( 0.0001, textColor.a );\n
240 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture );\n
242 // Draw the text as overlay above the style
243 gl_FragColor = textColor * uColor * vec4( mixColor, opacity );\n
247 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
248 varying mediump vec2 vTexCoord;\n
249 uniform sampler2D sTexture;\n
250 uniform sampler2D sStyle;\n
251 uniform sampler2D sMask;\n
252 uniform lowp float uHasMultipleTextColors;\n
253 uniform lowp vec4 uTextColorAnimatable;\n
254 uniform mediump vec4 uAtlasRect;\n
255 uniform lowp vec4 uColor;\n
256 uniform lowp vec3 mixColor;\n
257 uniform lowp float opacity;\n
261 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
262 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
263 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
264 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
266 // Set the color of non-transparent pixel in text to what it is animated to.
267 // Markup text with multiple text colors are not animated (but can be supported later on if required).
268 // Emoji color are not animated.
269 mediump vec4 textColor = textTexture * textTexture.a;\n
270 mediump float vstep = step( 0.0001, textColor.a );\n
271 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) );\n
273 // Draw the text as overlay above the style
274 gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
279 * Return Property index for the given string key
280 * param[in] stringKey the string index key
281 * return the key as an index
284 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
286 Dali::Property::Index result = Property::INVALID_KEY;
288 if( stringKey == VISUAL_TYPE )
290 result = Toolkit::Visual::Property::TYPE;
292 else if( stringKey == TEXT_PROPERTY )
294 result = Toolkit::TextVisual::Property::TEXT;
296 else if( stringKey == FONT_FAMILY_PROPERTY )
298 result = Toolkit::TextVisual::Property::FONT_FAMILY;
300 else if( stringKey == FONT_STYLE_PROPERTY )
302 result = Toolkit::TextVisual::Property::FONT_STYLE;
304 else if( stringKey == POINT_SIZE_PROPERTY )
306 result = Toolkit::TextVisual::Property::POINT_SIZE;
308 else if( stringKey == MULTI_LINE_PROPERTY )
310 result = Toolkit::TextVisual::Property::MULTI_LINE;
312 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
314 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
316 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
318 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
320 else if( stringKey == TEXT_COLOR_PROPERTY )
322 result = Toolkit::TextVisual::Property::TEXT_COLOR;
324 else if( stringKey == ENABLE_MARKUP_PROPERTY )
326 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
328 else if( stringKey == SHADOW_PROPERTY )
330 result = Toolkit::TextVisual::Property::SHADOW;
332 else if( stringKey == UNDERLINE_PROPERTY )
334 result = Toolkit::TextVisual::Property::UNDERLINE;
340 } // unnamed namespace
342 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
344 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
345 TextVisualPtr->SetProperties( properties );
346 return TextVisualPtr;
349 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
351 Property::Map outMap;
353 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
355 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
357 Property::Index indexKey = keyValue.first.indexKey;
359 if ( keyValue.first.type == Property::Key::STRING )
361 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
364 outMap.Insert( indexKey, keyValue.second );
367 propertyMap = outMap;
370 float TextVisual::GetHeightForWidth( float width )
372 return mController->GetHeightForWidth( width );
375 void TextVisual::GetNaturalSize( Vector2& naturalSize )
377 naturalSize = mController->GetNaturalSize().GetVectorXY();
380 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
382 Property::Value value;
385 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
388 mController->GetText( text );
389 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
391 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
393 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
394 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
396 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
398 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
400 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
402 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
404 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
406 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
408 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
409 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
411 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
412 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
415 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
418 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
420 mController->GetText( text );
421 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
425 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
426 : Visual::Base( factoryCache ),
427 mController( Text::Controller::New() ),
428 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
429 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
430 mRendererUpdateNeeded( false )
434 TextVisual::~TextVisual()
438 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
440 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
442 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
444 Property::Index indexKey = keyValue.first.indexKey;
446 if( keyValue.first.type == Property::Key::STRING )
448 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
451 DoSetProperty( indexKey, keyValue.second );
454 // Elide the text if it exceeds the boundaries.
455 mController->SetTextElideEnabled( true );
457 // Retrieve the layout engine to set the cursor's width.
458 Text::Layout::Engine& engine = mController->GetLayoutEngine();
460 // Sets 0 as cursor's width.
461 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
464 void TextVisual::DoSetOnStage( Actor& actor )
468 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
469 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
471 mImpl->mRenderer = Renderer::New( geometry, shader );
472 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
474 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
475 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
477 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
479 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
480 if( shaderTextColorIndex != Property::INVALID_INDEX )
482 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
483 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
488 // Renderer needs textures and to be added to control
489 mRendererUpdateNeeded = true;
494 void TextVisual::DoSetOffStage( Actor& actor )
496 if( mImpl->mRenderer )
498 // Removes the renderer from the actor.
499 actor.RemoveRenderer( mImpl->mRenderer );
503 // Resets the renderer.
504 mImpl->mRenderer.Reset();
507 // Resets the control handle.
511 void TextVisual::OnSetTransform()
516 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
520 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
522 const bool enableMarkup = propertyValue.Get<bool>();
523 mController->SetMarkupProcessorEnabled( enableMarkup );
526 case Toolkit::TextVisual::Property::TEXT:
528 mController->SetText( propertyValue.Get<std::string>() );
531 case Toolkit::TextVisual::Property::FONT_FAMILY:
533 SetFontFamilyProperty( mController, propertyValue );
536 case Toolkit::TextVisual::Property::FONT_STYLE:
538 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
541 case Toolkit::TextVisual::Property::POINT_SIZE:
543 const float pointSize = propertyValue.Get<float>();
544 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
546 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
550 case Toolkit::TextVisual::Property::MULTI_LINE:
552 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
555 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
557 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
558 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
559 HORIZONTAL_ALIGNMENT_STRING_TABLE,
560 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
563 mController->SetHorizontalAlignment( alignment );
567 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
569 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
570 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
571 VERTICAL_ALIGNMENT_STRING_TABLE,
572 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
575 mController->SetVerticalAlignment( alignment );
579 case Toolkit::TextVisual::Property::TEXT_COLOR:
581 const Vector4& textColor = propertyValue.Get< Vector4 >();
582 if( mController->GetDefaultColor() != textColor )
584 mController->SetDefaultColor( textColor );
588 case Toolkit::TextVisual::Property::SHADOW:
590 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
593 case Toolkit::TextVisual::Property::UNDERLINE:
595 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
601 void TextVisual::UpdateRenderer()
603 Actor control = mControl.GetHandle();
610 // Calculates the size to be used to relayout.
611 Vector2 relayoutSize;
613 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
614 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
616 // Round the size and offset to avoid pixel alignement issues.
617 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
618 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
620 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
622 // Removes the texture set.
625 // Remove any renderer previously set.
626 if( mImpl->mRenderer )
628 control.RemoveRenderer( mImpl->mRenderer );
631 // Nothing else to do if the relayout size is zero.
636 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
638 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
639 || mRendererUpdateNeeded )
641 mRendererUpdateNeeded = false;
643 // Removes the texture set.
646 // Remove any renderer previously set.
647 if( mImpl->mRenderer )
649 control.RemoveRenderer( mImpl->mRenderer );
652 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
653 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
655 // Check whether it is a markup text with multiple text colors
656 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
657 bool hasMultipleTextColors = ( NULL != colorsBuffer );
659 // Check whether the text contains any emoji
660 bool containsEmoji = false;
662 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
663 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
664 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
666 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
667 if( TextAbstraction::EMOJI == scriptRun.script )
669 containsEmoji = true;
674 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
676 bool shadowEnabled = false;
677 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
678 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
680 shadowEnabled = true;
683 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
684 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
686 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
688 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsEmoji, styleEnabled );
689 mImpl->mRenderer.SetTextures( textureSet );
691 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
692 mImpl->mRenderer.SetShader(shader);
694 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
696 Vector4 atlasRect = FULL_TEXTURE_RECT;
697 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
698 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
700 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
701 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
703 //Register transform properties
704 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
706 control.AddRenderer( mImpl->mRenderer );
708 // Text rendered and ready to display
714 void TextVisual::RemoveTextureSet()
716 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
718 // Removes the text's image from the texture atlas.
721 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
722 if( index != Property::INVALID_INDEX )
724 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
725 atlasRectValue.Get( atlasRect );
727 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
728 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
733 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
735 // Filter mode needs to be set to linear to produce better quality while scaling.
736 Sampler sampler = Sampler::New();
737 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
739 TextureSet textureSet = TextureSet::New();
741 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
742 Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
744 // Create a texture for the text without any styles
745 PixelData data = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
747 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
748 // In that case, create a texture. TODO: should tile the text.
750 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
751 data.GetPixelFormat(),
755 texture.Upload( data );
757 textureSet.SetTexture( 0u, texture );
758 textureSet.SetSampler( 0u, sampler );
762 // Create RGBA texture for all the text styles (without the text itself)
763 PixelData styleData = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
765 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
766 styleData.GetPixelFormat(),
767 styleData.GetWidth(),
768 styleData.GetHeight() );
770 styleTexture.Upload( styleData );
772 textureSet.SetTexture( 1u, styleTexture );
773 textureSet.SetSampler( 1u, sampler );
776 if ( containsEmoji && !hasMultipleTextColors )
778 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
779 PixelData maskData = mTypesetter->Render( size, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
781 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
782 maskData.GetPixelFormat(),
784 maskData.GetHeight() );
786 maskTexture.Upload( maskData );
790 textureSet.SetTexture( 1u, maskTexture );
791 textureSet.SetSampler( 1u, sampler );
795 textureSet.SetTexture( 2u, maskTexture );
796 textureSet.SetSampler( 2u, sampler );
803 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
807 if( hasMultipleTextColors && !styleEnabled )
809 // We don't animate text color if the text contains multiple colors
810 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
813 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
814 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
815 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
818 else if( hasMultipleTextColors && styleEnabled )
820 // We don't animate text color if the text contains multiple colors
821 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
824 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
825 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
826 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
829 else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
831 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
834 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
835 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
836 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
839 else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
841 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
844 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
845 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
846 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
849 else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
851 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
854 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
855 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
856 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
859 else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
861 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
864 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
865 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
866 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
873 } // namespace Internal
875 } // namespace Toolkit