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/devel-api/text/text-enumerations-devel.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 } // unnamed namespace
344 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
346 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
347 TextVisualPtr->SetProperties( properties );
348 return TextVisualPtr;
351 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
353 Property::Map outMap;
355 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
357 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
359 Property::Index indexKey = keyValue.first.indexKey;
361 if ( keyValue.first.type == Property::Key::STRING )
363 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
366 outMap.Insert( indexKey, keyValue.second );
369 propertyMap = outMap;
372 float TextVisual::GetHeightForWidth( float width )
374 return mController->GetHeightForWidth( width );
377 void TextVisual::GetNaturalSize( Vector2& naturalSize )
379 naturalSize = mController->GetNaturalSize().GetVectorXY();
382 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
384 Property::Value value;
387 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
390 mController->GetText( text );
391 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
393 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
395 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
396 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
398 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
400 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
402 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
404 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
406 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
408 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
410 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
411 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
413 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
414 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
416 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
417 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
420 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
423 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
425 mController->GetText( text );
426 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
430 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
431 : Visual::Base( factoryCache ),
432 mController( Text::Controller::New() ),
433 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
434 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
435 mRendererUpdateNeeded( false )
439 TextVisual::~TextVisual()
443 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
445 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
447 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
449 Property::Index indexKey = keyValue.first.indexKey;
451 if( keyValue.first.type == Property::Key::STRING )
453 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
456 DoSetProperty( indexKey, keyValue.second );
459 // Elide the text if it exceeds the boundaries.
460 mController->SetTextElideEnabled( true );
462 // Retrieve the layout engine to set the cursor's width.
463 Text::Layout::Engine& engine = mController->GetLayoutEngine();
465 // Sets 0 as cursor's width.
466 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
469 void TextVisual::DoSetOnStage( Actor& actor )
473 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
474 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
476 mImpl->mRenderer = Renderer::New( geometry, shader );
477 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
479 // Enable the pre-multiplied alpha to improve the text quality
480 EnablePreMultipliedAlpha(true);
482 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
483 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
485 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
487 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
488 if( shaderTextColorIndex != Property::INVALID_INDEX )
490 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
491 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
496 // Renderer needs textures and to be added to control
497 mRendererUpdateNeeded = true;
502 void TextVisual::DoSetOffStage( Actor& actor )
504 if( mImpl->mRenderer )
506 // Removes the renderer from the actor.
507 actor.RemoveRenderer( mImpl->mRenderer );
511 // Resets the renderer.
512 mImpl->mRenderer.Reset();
515 // Resets the control handle.
519 void TextVisual::OnSetTransform()
524 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
528 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
530 const bool enableMarkup = propertyValue.Get<bool>();
531 mController->SetMarkupProcessorEnabled( enableMarkup );
534 case Toolkit::TextVisual::Property::TEXT:
536 mController->SetText( propertyValue.Get<std::string>() );
539 case Toolkit::TextVisual::Property::FONT_FAMILY:
541 SetFontFamilyProperty( mController, propertyValue );
544 case Toolkit::TextVisual::Property::FONT_STYLE:
546 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
549 case Toolkit::TextVisual::Property::POINT_SIZE:
551 const float pointSize = propertyValue.Get<float>();
552 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
554 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
558 case Toolkit::TextVisual::Property::MULTI_LINE:
560 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
563 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
567 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
568 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
570 mController->SetHorizontalAlignment( alignment );
575 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
579 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
580 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
582 mController->SetVerticalAlignment( alignment );
587 case Toolkit::TextVisual::Property::TEXT_COLOR:
589 const Vector4& textColor = propertyValue.Get< Vector4 >();
590 if( mController->GetDefaultColor() != textColor )
592 mController->SetDefaultColor( textColor );
596 case Toolkit::TextVisual::Property::SHADOW:
598 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
601 case Toolkit::TextVisual::Property::UNDERLINE:
603 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
606 case Toolkit::DevelTextVisual::Property::OUTLINE:
608 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
614 void TextVisual::UpdateRenderer()
616 Actor control = mControl.GetHandle();
623 // Calculates the size to be used to relayout.
624 Vector2 relayoutSize;
626 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
627 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
629 // Round the size and offset to avoid pixel alignement issues.
630 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
631 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
633 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
635 // Removes the texture set.
638 // Remove any renderer previously set.
639 if( mImpl->mRenderer )
641 control.RemoveRenderer( mImpl->mRenderer );
644 // Nothing else to do if the relayout size is zero.
645 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
649 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
651 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
652 || mRendererUpdateNeeded )
654 mRendererUpdateNeeded = false;
656 // Removes the texture set.
659 // Remove any renderer previously set.
660 if( mImpl->mRenderer )
662 control.RemoveRenderer( mImpl->mRenderer );
665 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
666 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
668 // Check whether it is a markup text with multiple text colors
669 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
670 bool hasMultipleTextColors = ( NULL != colorsBuffer );
672 // Check whether the text contains any emoji
673 bool containsEmoji = false;
675 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
676 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
677 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
679 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
680 if( TextAbstraction::EMOJI == scriptRun.script )
682 containsEmoji = true;
687 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
689 bool shadowEnabled = false;
690 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
691 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
693 shadowEnabled = true;
696 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
697 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
699 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
701 TextureSet textureSet = GetTextTexture( mImpl->mTransform.mSize, hasMultipleTextColors, containsEmoji, styleEnabled );
702 mImpl->mRenderer.SetTextures( textureSet );
704 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
705 mImpl->mRenderer.SetShader(shader);
707 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
709 Vector4 atlasRect = FULL_TEXTURE_RECT;
710 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
711 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
713 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
715 //Register transform properties
716 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
718 control.AddRenderer( mImpl->mRenderer );
720 // Text rendered and ready to display
721 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
726 void TextVisual::RemoveTextureSet()
728 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
730 // Removes the text's image from the texture atlas.
733 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
734 if( index != Property::INVALID_INDEX )
736 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
737 atlasRectValue.Get( atlasRect );
739 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
740 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
745 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
747 // Filter mode needs to be set to linear to produce better quality while scaling.
748 Sampler sampler = Sampler::New();
749 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
751 TextureSet textureSet = TextureSet::New();
753 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
754 Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
756 // Check the text direction
757 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
759 // Create a texture for the text without any styles
760 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
762 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
763 // In that case, create a texture. TODO: should tile the text.
765 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
766 data.GetPixelFormat(),
770 texture.Upload( data );
772 textureSet.SetTexture( 0u, texture );
773 textureSet.SetSampler( 0u, sampler );
777 // Create RGBA texture for all the text styles (without the text itself)
778 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
780 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
781 styleData.GetPixelFormat(),
782 styleData.GetWidth(),
783 styleData.GetHeight() );
785 styleTexture.Upload( styleData );
787 textureSet.SetTexture( 1u, styleTexture );
788 textureSet.SetSampler( 1u, sampler );
791 if ( containsEmoji && !hasMultipleTextColors )
793 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
794 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
796 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
797 maskData.GetPixelFormat(),
799 maskData.GetHeight() );
801 maskTexture.Upload( maskData );
805 textureSet.SetTexture( 1u, maskTexture );
806 textureSet.SetSampler( 1u, sampler );
810 textureSet.SetTexture( 2u, maskTexture );
811 textureSet.SetSampler( 2u, sampler );
818 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
822 if( hasMultipleTextColors && !styleEnabled )
824 // We don't animate text color if the text contains multiple colors
825 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
828 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
829 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
830 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
833 else if( hasMultipleTextColors && styleEnabled )
835 // We don't animate text color if the text contains multiple colors
836 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
839 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
840 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
841 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
844 else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
846 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
849 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
850 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
851 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
854 else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
856 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
859 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
860 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
861 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
864 else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
866 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
869 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
870 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
871 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
874 else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
876 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
879 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
880 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
881 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
888 } // namespace Internal
890 } // namespace Toolkit