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/public-api/visuals/text-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/internal/text/text-font-style.h>
35 #include <dali-toolkit/internal/text/text-effects-style.h>
36 #include <dali-toolkit/internal/text/script-run.h>
37 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
38 #include <dali-toolkit/internal/text/rendering/view-model.h>
52 // Property names - common properties defined in visual-string-constants.h/cpp
53 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
54 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
55 const char * const POINT_SIZE_PROPERTY( "pointSize" );
56 const char * const MULTI_LINE_PROPERTY( "multiLine" );
57 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
58 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
59 const char * const TEXT_COLOR_PROPERTY( "textColor" );
60 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
61 const char * const SHADOW_PROPERTY( "shadow" );
62 const char * const UNDERLINE_PROPERTY( "underline" );
63 const char * const OUTLINE_PROPERTY( "outline" );
65 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
67 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
68 attribute mediump vec2 aPosition;\n
69 uniform mediump mat4 uMvpMatrix;\n
70 uniform mediump vec3 uSize;\n
71 uniform mediump vec4 pixelArea;\n
73 uniform mediump mat4 uModelMatrix;\n
74 uniform mediump mat4 uViewMatrix;\n
75 uniform mediump mat4 uProjection;\n
77 varying mediump vec2 vTexCoord;\n
79 //Visual size and offset
80 uniform mediump vec2 offset;\n
81 uniform mediump vec2 size;\n
82 uniform mediump vec4 offsetSizeMode;\n
83 uniform mediump vec2 origin;\n
84 uniform mediump vec2 anchorPoint;\n
86 vec4 ComputeVertexPosition()\n
88 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
89 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
90 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
95 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
96 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
97 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
99 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
100 gl_Position = vertexPosition;\n
104 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT = DALI_COMPOSE_SHADER(
105 varying mediump vec2 vTexCoord;\n
106 uniform sampler2D sTexture;\n
107 uniform lowp vec4 uTextColorAnimatable;\n
108 uniform mediump vec4 uAtlasRect;\n
109 uniform lowp vec4 uColor;\n
110 uniform lowp vec3 mixColor;\n
111 uniform lowp float opacity;\n
112 uniform lowp float preMultipliedAlpha;\n
114 lowp vec4 visualMixColor()\n
116 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
121 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
122 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
124 // Set the color of the text to what it is animated to.
125 gl_FragColor = uTextColorAnimatable * textTexture * uColor * visualMixColor();
129 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
130 varying mediump vec2 vTexCoord;\n
131 uniform sampler2D sTexture;\n
132 uniform mediump vec4 uAtlasRect;\n
133 uniform lowp vec4 uColor;\n
134 uniform lowp vec3 mixColor;\n
135 uniform lowp float opacity;\n
136 uniform lowp float preMultipliedAlpha;\n
138 lowp vec4 visualMixColor()\n
140 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
145 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
146 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
147 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
149 gl_FragColor = textTexture * uColor * visualMixColor();
153 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
154 varying mediump vec2 vTexCoord;\n
155 uniform sampler2D sTexture;\n
156 uniform sampler2D sStyle;\n
157 uniform lowp vec4 uTextColorAnimatable;\n
158 uniform mediump vec4 uAtlasRect;\n
159 uniform lowp vec4 uColor;\n
160 uniform lowp vec3 mixColor;\n
161 uniform lowp float opacity;\n
162 uniform lowp float preMultipliedAlpha;\n
164 lowp vec4 visualMixColor()\n
166 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
171 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
172 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
173 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
175 // Draw the text as overlay above the style
176 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - textTexture ) ) * uColor * visualMixColor();\n
180 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
181 varying mediump vec2 vTexCoord;\n
182 uniform sampler2D sTexture;\n
183 uniform sampler2D sStyle;\n
184 uniform mediump vec4 uAtlasRect;\n
185 uniform lowp vec4 uColor;\n
186 uniform lowp vec3 mixColor;\n
187 uniform lowp float opacity;\n
188 uniform lowp float preMultipliedAlpha;\n
190 lowp vec4 visualMixColor()\n
192 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
197 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
198 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
199 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
200 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
202 // Draw the text as overlay above the style
203 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * visualMixColor();\n
207 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
208 varying mediump vec2 vTexCoord;\n
209 uniform sampler2D sTexture;\n
210 uniform sampler2D sMask;\n
211 uniform lowp vec4 uTextColorAnimatable;\n
212 uniform mediump vec4 uAtlasRect;\n
213 uniform lowp vec4 uColor;\n
214 uniform lowp vec3 mixColor;\n
215 uniform lowp float opacity;\n
216 uniform lowp float preMultipliedAlpha;\n
218 lowp vec4 visualMixColor()\n
220 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
225 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
226 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
227 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
229 // Set the color of non-transparent pixel in text to what it is animated to.
230 // Markup text with multiple text colors are not animated (but can be supported later on if required).
231 // Emoji color are not animated.
232 mediump float vstep = step( 0.0001, textTexture.a );\n
233 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
235 // Draw the text as overlay above the style
236 gl_FragColor = textTexture * uColor * visualMixColor();\n
240 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
241 varying mediump vec2 vTexCoord;\n
242 uniform sampler2D sTexture;\n
243 uniform sampler2D sStyle;\n
244 uniform sampler2D sMask;\n
245 uniform lowp float uHasMultipleTextColors;\n
246 uniform lowp vec4 uTextColorAnimatable;\n
247 uniform mediump vec4 uAtlasRect;\n
248 uniform lowp vec4 uColor;\n
249 uniform lowp vec3 mixColor;\n
250 uniform lowp float opacity;\n
251 uniform lowp float preMultipliedAlpha;\n
253 lowp vec4 visualMixColor()\n
255 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
260 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
261 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
262 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
263 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
265 // Set the color of non-transparent pixel in text to what it is animated to.
266 // Markup text with multiple text colors are not animated (but can be supported later on if required).
267 // Emoji color are not animated.
268 mediump float vstep = step( 0.0001, textTexture.a );\n
269 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
271 // Draw the text as overlay above the style
272 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * visualMixColor();\n
277 * Return Property index for the given string key
278 * param[in] stringKey the string index key
279 * return the key as an index
282 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
284 Dali::Property::Index result = Property::INVALID_KEY;
286 if( stringKey == VISUAL_TYPE )
288 result = Toolkit::Visual::Property::TYPE;
290 else if( stringKey == TEXT_PROPERTY )
292 result = Toolkit::TextVisual::Property::TEXT;
294 else if( stringKey == FONT_FAMILY_PROPERTY )
296 result = Toolkit::TextVisual::Property::FONT_FAMILY;
298 else if( stringKey == FONT_STYLE_PROPERTY )
300 result = Toolkit::TextVisual::Property::FONT_STYLE;
302 else if( stringKey == POINT_SIZE_PROPERTY )
304 result = Toolkit::TextVisual::Property::POINT_SIZE;
306 else if( stringKey == MULTI_LINE_PROPERTY )
308 result = Toolkit::TextVisual::Property::MULTI_LINE;
310 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
312 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
314 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
316 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
318 else if( stringKey == TEXT_COLOR_PROPERTY )
320 result = Toolkit::TextVisual::Property::TEXT_COLOR;
322 else if( stringKey == ENABLE_MARKUP_PROPERTY )
324 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
326 else if( stringKey == SHADOW_PROPERTY )
328 result = Toolkit::TextVisual::Property::SHADOW;
330 else if( stringKey == UNDERLINE_PROPERTY )
332 result = Toolkit::TextVisual::Property::UNDERLINE;
334 else if( stringKey == OUTLINE_PROPERTY )
336 result = Toolkit::DevelTextVisual::Property::OUTLINE;
342 void TextColorConstraint( Vector4& current, const PropertyInputContainer& inputs )
344 Vector4 color = inputs[0]->GetVector4();
345 current.r = color.r * color.a;
346 current.g = color.g * color.a;
347 current.b = color.b * color.a;
351 } // unnamed namespace
353 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
355 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
356 TextVisualPtr->SetProperties( properties );
357 return TextVisualPtr;
360 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
362 Property::Map outMap;
364 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
366 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
368 Property::Index indexKey = keyValue.first.indexKey;
370 if ( keyValue.first.type == Property::Key::STRING )
372 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
375 outMap.Insert( indexKey, keyValue.second );
378 propertyMap = outMap;
381 float TextVisual::GetHeightForWidth( float width )
383 return mController->GetHeightForWidth( width );
386 void TextVisual::GetNaturalSize( Vector2& naturalSize )
388 naturalSize = mController->GetNaturalSize().GetVectorXY();
391 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
393 Property::Value value;
396 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
399 mController->GetText( text );
400 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
402 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
404 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
405 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
407 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
409 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
411 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
413 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
415 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
417 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
419 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
420 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
422 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
423 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
425 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
426 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
429 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
432 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
434 mController->GetText( text );
435 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
439 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
440 : Visual::Base( factoryCache ),
441 mController( Text::Controller::New() ),
442 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
443 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
444 mRendererUpdateNeeded( false )
448 TextVisual::~TextVisual()
452 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
454 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
456 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
458 Property::Index indexKey = keyValue.first.indexKey;
460 if( keyValue.first.type == Property::Key::STRING )
462 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
465 DoSetProperty( indexKey, keyValue.second );
468 // Elide the text if it exceeds the boundaries.
469 mController->SetTextElideEnabled( true );
471 // Retrieve the layout engine to set the cursor's width.
472 Text::Layout::Engine& engine = mController->GetLayoutEngine();
474 // Sets 0 as cursor's width.
475 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
478 void TextVisual::DoSetOnStage( Actor& actor )
482 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
483 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
485 mImpl->mRenderer = Renderer::New( geometry, shader );
486 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
488 // Enable the pre-multiplied alpha to improve the text quality
489 EnablePreMultipliedAlpha(true);
491 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
492 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
494 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
496 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
497 if( shaderTextColorIndex != Property::INVALID_INDEX )
499 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint );
500 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
505 // Renderer needs textures and to be added to control
506 mRendererUpdateNeeded = true;
511 void TextVisual::DoSetOffStage( Actor& actor )
513 if( mImpl->mRenderer )
515 // Removes the renderer from the actor.
516 actor.RemoveRenderer( mImpl->mRenderer );
520 // Resets the renderer.
521 mImpl->mRenderer.Reset();
524 // Resets the control handle.
528 void TextVisual::OnSetTransform()
533 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
537 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
539 const bool enableMarkup = propertyValue.Get<bool>();
540 mController->SetMarkupProcessorEnabled( enableMarkup );
543 case Toolkit::TextVisual::Property::TEXT:
545 mController->SetText( propertyValue.Get<std::string>() );
548 case Toolkit::TextVisual::Property::FONT_FAMILY:
550 SetFontFamilyProperty( mController, propertyValue );
553 case Toolkit::TextVisual::Property::FONT_STYLE:
555 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
558 case Toolkit::TextVisual::Property::POINT_SIZE:
560 const float pointSize = propertyValue.Get<float>();
561 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
563 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
567 case Toolkit::TextVisual::Property::MULTI_LINE:
569 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
572 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
576 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
577 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
579 mController->SetHorizontalAlignment( alignment );
584 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
588 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
589 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
591 mController->SetVerticalAlignment( alignment );
596 case Toolkit::TextVisual::Property::TEXT_COLOR:
598 const Vector4& textColor = propertyValue.Get< Vector4 >();
599 if( mController->GetDefaultColor() != textColor )
601 mController->SetDefaultColor( textColor );
605 case Toolkit::TextVisual::Property::SHADOW:
607 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
610 case Toolkit::TextVisual::Property::UNDERLINE:
612 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
615 case Toolkit::DevelTextVisual::Property::OUTLINE:
617 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
623 void TextVisual::UpdateRenderer()
625 Actor control = mControl.GetHandle();
632 // Calculates the size to be used to relayout.
633 Vector2 relayoutSize;
635 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
636 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
638 // Round the size and offset to avoid pixel alignement issues.
639 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
640 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
642 float alpha = control.GetProperty< float >( Actor::Property::COLOR_ALPHA );
643 Vector4 animatableTextColor = control.GetProperty< Vector4 >( mAnimatableTextColorPropertyIndex );
644 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 )
645 || mTypesetter->GetViewModel()->GetNumberOfLines() == 0 || alpha == 0.0f || animatableTextColor.a == 0.0f )
647 // Removes the texture set.
650 // Remove any renderer previously set.
651 if( mImpl->mRenderer )
653 control.RemoveRenderer( mImpl->mRenderer );
656 // Nothing else to do if the relayout size is zero.
657 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
661 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
663 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
664 || mRendererUpdateNeeded )
666 mRendererUpdateNeeded = false;
668 // Removes the texture set.
671 // Remove any renderer previously set.
672 if( mImpl->mRenderer )
674 control.RemoveRenderer( mImpl->mRenderer );
677 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
678 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
680 // Check whether it is a markup text with multiple text colors
681 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
682 bool hasMultipleTextColors = ( NULL != colorsBuffer );
684 // Check whether the text contains any emoji
685 bool containsEmoji = false;
687 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
688 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
689 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
691 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
692 if( TextAbstraction::EMOJI == scriptRun.script )
694 containsEmoji = true;
699 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
701 bool shadowEnabled = false;
702 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
703 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
705 shadowEnabled = true;
708 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
709 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
711 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
713 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsEmoji, styleEnabled );
714 mImpl->mRenderer.SetTextures( textureSet );
716 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
717 mImpl->mRenderer.SetShader(shader);
719 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
721 Vector4 atlasRect = FULL_TEXTURE_RECT;
722 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
723 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
725 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
727 //Register transform properties
728 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
730 control.AddRenderer( mImpl->mRenderer );
732 // Text rendered and ready to display
733 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
738 void TextVisual::RemoveTextureSet()
740 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
742 // Removes the text's image from the texture atlas.
745 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
746 if( index != Property::INVALID_INDEX )
748 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
749 atlasRectValue.Get( atlasRect );
751 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
752 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
757 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
759 // Filter mode needs to be set to linear to produce better quality while scaling.
760 Sampler sampler = Sampler::New();
761 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
763 TextureSet textureSet = TextureSet::New();
765 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
766 Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
768 // Create a texture for the text without any styles
769 PixelData data = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
771 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
772 // In that case, create a texture. TODO: should tile the text.
774 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
775 data.GetPixelFormat(),
779 texture.Upload( data );
781 textureSet.SetTexture( 0u, texture );
782 textureSet.SetSampler( 0u, sampler );
786 // Create RGBA texture for all the text styles (without the text itself)
787 PixelData styleData = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
789 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
790 styleData.GetPixelFormat(),
791 styleData.GetWidth(),
792 styleData.GetHeight() );
794 styleTexture.Upload( styleData );
796 textureSet.SetTexture( 1u, styleTexture );
797 textureSet.SetSampler( 1u, sampler );
800 if ( containsEmoji && !hasMultipleTextColors )
802 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
803 PixelData maskData = mTypesetter->Render( size, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
805 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
806 maskData.GetPixelFormat(),
808 maskData.GetHeight() );
810 maskTexture.Upload( maskData );
814 textureSet.SetTexture( 1u, maskTexture );
815 textureSet.SetSampler( 1u, sampler );
819 textureSet.SetTexture( 2u, maskTexture );
820 textureSet.SetSampler( 2u, sampler );
827 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
831 if( hasMultipleTextColors && !styleEnabled )
833 // We don't animate text color if the text contains multiple colors
834 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
837 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
838 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
839 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
842 else if( hasMultipleTextColors && styleEnabled )
844 // We don't animate text color if the text contains multiple colors
845 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
848 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
849 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
850 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
853 else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
855 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
858 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
859 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
860 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
863 else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
865 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
868 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
869 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
870 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
873 else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
875 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
878 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
879 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
880 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
883 else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
885 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
888 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
889 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
890 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
897 } // namespace Internal
899 } // namespace Toolkit