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/public-api/visuals/visual-properties.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>
36 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
50 // Property names - common properties defined in visual-string-constants.h/cpp
51 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
52 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
53 const char * const POINT_SIZE_PROPERTY( "pointSize" );
54 const char * const MULTI_LINE_PROPERTY( "multiLine" );
55 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
56 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
57 const char * const TEXT_COLOR_PROPERTY( "textColor" );
58 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
59 const char * const SHADOW_PROPERTY( "shadow" );
60 const char * const UNDERLINE_PROPERTY( "underline" );
62 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
64 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
65 attribute mediump vec2 aPosition;\n
66 uniform mediump mat4 uMvpMatrix;\n
67 uniform mediump vec3 uSize;\n
68 uniform mediump vec4 pixelArea;\n
70 uniform mediump mat4 uModelMatrix;\n
71 uniform mediump mat4 uViewMatrix;\n
72 uniform mediump mat4 uProjection;\n
74 varying mediump vec2 vTexCoord;\n
76 //Visual size and offset
77 uniform mediump vec2 offset;\n
78 uniform mediump vec2 size;\n
79 uniform mediump vec4 offsetSizeMode;\n
80 uniform mediump vec2 origin;\n
81 uniform mediump vec2 anchorPoint;\n
83 vec4 ComputeVertexPosition()\n
85 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
86 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
87 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
92 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
93 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
94 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
96 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
97 gl_Position = vertexPosition;\n
101 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT = DALI_COMPOSE_SHADER(
102 varying mediump vec2 vTexCoord;\n
103 uniform sampler2D sTexture;\n
104 uniform lowp vec4 uTextColorAnimatable;\n
105 uniform mediump vec4 uAtlasRect;\n
106 uniform lowp vec4 uColor;\n
107 uniform lowp vec3 mixColor;\n
108 uniform lowp float opacity;\n
109 uniform lowp float preMultipliedAlpha;\n
111 lowp vec4 visualMixColor()\n
113 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
118 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
119 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
121 // Set the color of the text to what it is animated to.
122 gl_FragColor = uTextColorAnimatable * textTexture * uColor * visualMixColor();
126 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
127 varying mediump vec2 vTexCoord;\n
128 uniform sampler2D sTexture;\n
129 uniform mediump vec4 uAtlasRect;\n
130 uniform lowp vec4 uColor;\n
131 uniform lowp vec3 mixColor;\n
132 uniform lowp float opacity;\n
133 uniform lowp float preMultipliedAlpha;\n
135 lowp vec4 visualMixColor()\n
137 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
142 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
143 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
144 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
146 gl_FragColor = textTexture * uColor * visualMixColor();
150 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
151 varying mediump vec2 vTexCoord;\n
152 uniform sampler2D sTexture;\n
153 uniform sampler2D sStyle;\n
154 uniform lowp vec4 uTextColorAnimatable;\n
155 uniform mediump vec4 uAtlasRect;\n
156 uniform lowp vec4 uColor;\n
157 uniform lowp vec3 mixColor;\n
158 uniform lowp float opacity;\n
159 uniform lowp float preMultipliedAlpha;\n
161 lowp vec4 visualMixColor()\n
163 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
168 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
169 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
170 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
172 // Draw the text as overlay above the style
173 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - textTexture ) ) * uColor * visualMixColor();\n
177 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
178 varying mediump vec2 vTexCoord;\n
179 uniform sampler2D sTexture;\n
180 uniform sampler2D sStyle;\n
181 uniform mediump vec4 uAtlasRect;\n
182 uniform lowp vec4 uColor;\n
183 uniform lowp vec3 mixColor;\n
184 uniform lowp float opacity;\n
185 uniform lowp float preMultipliedAlpha;\n
187 lowp vec4 visualMixColor()\n
189 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
194 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
195 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
196 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
197 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
199 // Draw the text as overlay above the style
200 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * visualMixColor();\n
204 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
205 varying mediump vec2 vTexCoord;\n
206 uniform sampler2D sTexture;\n
207 uniform sampler2D sMask;\n
208 uniform lowp vec4 uTextColorAnimatable;\n
209 uniform mediump vec4 uAtlasRect;\n
210 uniform lowp vec4 uColor;\n
211 uniform lowp vec3 mixColor;\n
212 uniform lowp float opacity;\n
213 uniform lowp float preMultipliedAlpha;\n
215 lowp vec4 visualMixColor()\n
217 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
222 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
223 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
224 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
226 // Set the color of non-transparent pixel in text to what it is animated to.
227 // Markup text with multiple text colors are not animated (but can be supported later on if required).
228 // Emoji color are not animated.
229 mediump float vstep = step( 0.0001, textTexture.a );\n
230 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
232 // Draw the text as overlay above the style
233 gl_FragColor = textTexture * uColor * visualMixColor();\n
237 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
238 varying mediump vec2 vTexCoord;\n
239 uniform sampler2D sTexture;\n
240 uniform sampler2D sStyle;\n
241 uniform sampler2D sMask;\n
242 uniform lowp float uHasMultipleTextColors;\n
243 uniform lowp vec4 uTextColorAnimatable;\n
244 uniform mediump vec4 uAtlasRect;\n
245 uniform lowp vec4 uColor;\n
246 uniform lowp vec3 mixColor;\n
247 uniform lowp float opacity;\n
248 uniform lowp float preMultipliedAlpha;\n
250 lowp vec4 visualMixColor()\n
252 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
257 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
258 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
259 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
260 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
262 // Set the color of non-transparent pixel in text to what it is animated to.
263 // Markup text with multiple text colors are not animated (but can be supported later on if required).
264 // Emoji color are not animated.
265 mediump float vstep = step( 0.0001, textTexture.a );\n
266 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
268 // Draw the text as overlay above the style
269 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * visualMixColor();\n
274 * Return Property index for the given string key
275 * param[in] stringKey the string index key
276 * return the key as an index
279 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
281 Dali::Property::Index result = Property::INVALID_KEY;
283 if( stringKey == VISUAL_TYPE )
285 result = Toolkit::Visual::Property::TYPE;
287 else if( stringKey == TEXT_PROPERTY )
289 result = Toolkit::TextVisual::Property::TEXT;
291 else if( stringKey == FONT_FAMILY_PROPERTY )
293 result = Toolkit::TextVisual::Property::FONT_FAMILY;
295 else if( stringKey == FONT_STYLE_PROPERTY )
297 result = Toolkit::TextVisual::Property::FONT_STYLE;
299 else if( stringKey == POINT_SIZE_PROPERTY )
301 result = Toolkit::TextVisual::Property::POINT_SIZE;
303 else if( stringKey == MULTI_LINE_PROPERTY )
305 result = Toolkit::TextVisual::Property::MULTI_LINE;
307 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
309 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
311 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
313 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
315 else if( stringKey == TEXT_COLOR_PROPERTY )
317 result = Toolkit::TextVisual::Property::TEXT_COLOR;
319 else if( stringKey == ENABLE_MARKUP_PROPERTY )
321 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
323 else if( stringKey == SHADOW_PROPERTY )
325 result = Toolkit::TextVisual::Property::SHADOW;
327 else if( stringKey == UNDERLINE_PROPERTY )
329 result = Toolkit::TextVisual::Property::UNDERLINE;
335 } // unnamed namespace
337 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
339 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
340 TextVisualPtr->SetProperties( properties );
341 return TextVisualPtr;
344 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
346 Property::Map outMap;
348 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
350 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
352 Property::Index indexKey = keyValue.first.indexKey;
354 if ( keyValue.first.type == Property::Key::STRING )
356 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
359 outMap.Insert( indexKey, keyValue.second );
362 propertyMap = outMap;
365 float TextVisual::GetHeightForWidth( float width )
367 return mController->GetHeightForWidth( width );
370 void TextVisual::GetNaturalSize( Vector2& naturalSize )
372 naturalSize = mController->GetNaturalSize().GetVectorXY();
375 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
377 Property::Value value;
380 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
383 mController->GetText( text );
384 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
386 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
388 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
389 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
391 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
393 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
395 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
397 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
399 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
401 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
403 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
404 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
406 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
407 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
410 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
413 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
415 mController->GetText( text );
416 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
420 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
421 : Visual::Base( factoryCache ),
422 mController( Text::Controller::New() ),
423 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
424 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
425 mRendererUpdateNeeded( false )
429 TextVisual::~TextVisual()
433 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
435 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
437 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
439 Property::Index indexKey = keyValue.first.indexKey;
441 if( keyValue.first.type == Property::Key::STRING )
443 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
446 DoSetProperty( indexKey, keyValue.second );
449 // Elide the text if it exceeds the boundaries.
450 mController->SetTextElideEnabled( true );
452 // Retrieve the layout engine to set the cursor's width.
453 Text::Layout::Engine& engine = mController->GetLayoutEngine();
455 // Sets 0 as cursor's width.
456 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
459 void TextVisual::DoSetOnStage( Actor& actor )
463 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
464 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
466 mImpl->mRenderer = Renderer::New( geometry, shader );
467 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
469 // Enable the pre-multiplied alpha to improve the text quality
470 EnablePreMultipliedAlpha(true);
472 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
473 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
475 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
477 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
478 if( shaderTextColorIndex != Property::INVALID_INDEX )
480 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
481 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
486 // Renderer needs textures and to be added to control
487 mRendererUpdateNeeded = true;
492 void TextVisual::DoSetOffStage( Actor& actor )
494 if( mImpl->mRenderer )
496 // Removes the renderer from the actor.
497 actor.RemoveRenderer( mImpl->mRenderer );
501 // Resets the renderer.
502 mImpl->mRenderer.Reset();
505 // Resets the control handle.
509 void TextVisual::OnSetTransform()
514 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
518 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
520 const bool enableMarkup = propertyValue.Get<bool>();
521 mController->SetMarkupProcessorEnabled( enableMarkup );
524 case Toolkit::TextVisual::Property::TEXT:
526 mController->SetText( propertyValue.Get<std::string>() );
529 case Toolkit::TextVisual::Property::FONT_FAMILY:
531 SetFontFamilyProperty( mController, propertyValue );
534 case Toolkit::TextVisual::Property::FONT_STYLE:
536 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
539 case Toolkit::TextVisual::Property::POINT_SIZE:
541 const float pointSize = propertyValue.Get<float>();
542 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
544 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
548 case Toolkit::TextVisual::Property::MULTI_LINE:
550 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
553 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
557 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
558 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
560 mController->SetHorizontalAlignment( alignment );
565 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
569 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
570 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
572 mController->SetVerticalAlignment( alignment );
577 case Toolkit::TextVisual::Property::TEXT_COLOR:
579 const Vector4& textColor = propertyValue.Get< Vector4 >();
580 if( mController->GetDefaultColor() != textColor )
582 mController->SetDefaultColor( textColor );
586 case Toolkit::TextVisual::Property::SHADOW:
588 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
591 case Toolkit::TextVisual::Property::UNDERLINE:
593 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
599 void TextVisual::UpdateRenderer()
601 Actor control = mControl.GetHandle();
608 // Calculates the size to be used to relayout.
609 Vector2 relayoutSize;
611 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
612 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
614 // Round the size and offset to avoid pixel alignement issues.
615 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
616 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
618 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
620 // Removes the texture set.
623 // Remove any renderer previously set.
624 if( mImpl->mRenderer )
626 control.RemoveRenderer( mImpl->mRenderer );
629 // Nothing else to do if the relayout size is zero.
634 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
636 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
637 || mRendererUpdateNeeded )
639 mRendererUpdateNeeded = false;
641 // Removes the texture set.
644 // Remove any renderer previously set.
645 if( mImpl->mRenderer )
647 control.RemoveRenderer( mImpl->mRenderer );
650 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
651 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
653 // Check whether it is a markup text with multiple text colors
654 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
655 bool hasMultipleTextColors = ( NULL != colorsBuffer );
657 // Check whether the text contains any emoji
658 bool containsEmoji = false;
660 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
661 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
662 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
664 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
665 if( TextAbstraction::EMOJI == scriptRun.script )
667 containsEmoji = true;
672 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
674 bool shadowEnabled = false;
675 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
676 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
678 shadowEnabled = true;
681 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
682 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
684 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
686 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsEmoji, styleEnabled );
687 mImpl->mRenderer.SetTextures( textureSet );
689 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
690 mImpl->mRenderer.SetShader(shader);
692 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
694 Vector4 atlasRect = FULL_TEXTURE_RECT;
695 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
696 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
698 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
700 //Register transform properties
701 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
703 control.AddRenderer( mImpl->mRenderer );
705 // Text rendered and ready to display
711 void TextVisual::RemoveTextureSet()
713 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
715 // Removes the text's image from the texture atlas.
718 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
719 if( index != Property::INVALID_INDEX )
721 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
722 atlasRectValue.Get( atlasRect );
724 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
725 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
730 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
732 // Filter mode needs to be set to linear to produce better quality while scaling.
733 Sampler sampler = Sampler::New();
734 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
736 TextureSet textureSet = TextureSet::New();
738 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
739 Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
741 // Create a texture for the text without any styles
742 PixelData data = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
744 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
745 // In that case, create a texture. TODO: should tile the text.
747 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
748 data.GetPixelFormat(),
752 texture.Upload( data );
754 textureSet.SetTexture( 0u, texture );
755 textureSet.SetSampler( 0u, sampler );
759 // Create RGBA texture for all the text styles (without the text itself)
760 PixelData styleData = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
762 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
763 styleData.GetPixelFormat(),
764 styleData.GetWidth(),
765 styleData.GetHeight() );
767 styleTexture.Upload( styleData );
769 textureSet.SetTexture( 1u, styleTexture );
770 textureSet.SetSampler( 1u, sampler );
773 if ( containsEmoji && !hasMultipleTextColors )
775 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
776 PixelData maskData = mTypesetter->Render( size, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
778 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
779 maskData.GetPixelFormat(),
781 maskData.GetHeight() );
783 maskTexture.Upload( maskData );
787 textureSet.SetTexture( 1u, maskTexture );
788 textureSet.SetSampler( 1u, sampler );
792 textureSet.SetTexture( 2u, maskTexture );
793 textureSet.SetSampler( 2u, sampler );
800 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
804 if( hasMultipleTextColors && !styleEnabled )
806 // We don't animate text color if the text contains multiple colors
807 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
810 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
811 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
812 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
815 else if( hasMultipleTextColors && styleEnabled )
817 // We don't animate text color if the text contains multiple colors
818 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
821 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
822 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
823 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
826 else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
828 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
831 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
832 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
833 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
836 else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
838 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
841 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
842 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
843 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
846 else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
848 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
851 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
852 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
853 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
856 else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
858 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
861 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
862 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
863 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
870 } // namespace Internal
872 } // namespace Toolkit