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
112 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
113 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
115 // Set the color of the text to what it is animated to.
116 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, opacity );
120 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
121 varying mediump vec2 vTexCoord;\n
122 uniform sampler2D sTexture;\n
123 uniform mediump vec4 uAtlasRect;\n
124 uniform lowp vec4 uColor;\n
125 uniform lowp vec3 mixColor;\n
126 uniform lowp float opacity;\n
130 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
131 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
133 gl_FragColor = textTexture * uColor * vec4( mixColor, opacity );
137 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
138 varying mediump vec2 vTexCoord;\n
139 uniform sampler2D sTexture;\n
140 uniform sampler2D sStyle;\n
141 uniform lowp vec4 uTextColorAnimatable;\n
142 uniform mediump vec4 uAtlasRect;\n
143 uniform lowp vec4 uColor;\n
144 uniform lowp vec3 mixColor;\n
145 uniform lowp float opacity;\n
149 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
150 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
151 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
153 // Draw the text as overlay above the style
154 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - textTexture ) ) * uColor * vec4( mixColor, opacity );\n
158 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
159 varying mediump vec2 vTexCoord;\n
160 uniform sampler2D sTexture;\n
161 uniform sampler2D sStyle;\n
162 uniform mediump vec4 uAtlasRect;\n
163 uniform lowp vec4 uColor;\n
164 uniform lowp vec3 mixColor;\n
165 uniform lowp float opacity;\n
169 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
170 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
171 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
173 // Draw the text as overlay above the style
174 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
178 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
179 varying mediump vec2 vTexCoord;\n
180 uniform sampler2D sTexture;\n
181 uniform sampler2D sMask;\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 vec4 textTexture = texture2D( sTexture, texCoord );\n
192 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
194 // Set the color of non-transparent pixel in text to what it is animated to.
195 // Markup text with multiple text colors are not animated (but can be supported later on if required).
196 // Emoji color are not animated.
197 mediump float vstep = step( 0.0001, textTexture.a );\n
198 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture );\n
200 // Draw the text as overlay above the style
201 gl_FragColor = textTexture * uColor * vec4( mixColor, opacity );\n
205 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
206 varying mediump vec2 vTexCoord;\n
207 uniform sampler2D sTexture;\n
208 uniform sampler2D sStyle;\n
209 uniform sampler2D sMask;\n
210 uniform lowp float uHasMultipleTextColors;\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
219 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
220 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
221 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
222 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
224 // Set the color of non-transparent pixel in text to what it is animated to.
225 // Markup text with multiple text colors are not animated (but can be supported later on if required).
226 // Emoji color are not animated.
227 mediump float vstep = step( 0.0001, textTexture.a );\n
228 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) );\n
230 // Draw the text as overlay above the style
231 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
236 * Return Property index for the given string key
237 * param[in] stringKey the string index key
238 * return the key as an index
241 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
243 Dali::Property::Index result = Property::INVALID_KEY;
245 if( stringKey == VISUAL_TYPE )
247 result = Toolkit::Visual::Property::TYPE;
249 else if( stringKey == TEXT_PROPERTY )
251 result = Toolkit::TextVisual::Property::TEXT;
253 else if( stringKey == FONT_FAMILY_PROPERTY )
255 result = Toolkit::TextVisual::Property::FONT_FAMILY;
257 else if( stringKey == FONT_STYLE_PROPERTY )
259 result = Toolkit::TextVisual::Property::FONT_STYLE;
261 else if( stringKey == POINT_SIZE_PROPERTY )
263 result = Toolkit::TextVisual::Property::POINT_SIZE;
265 else if( stringKey == MULTI_LINE_PROPERTY )
267 result = Toolkit::TextVisual::Property::MULTI_LINE;
269 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
271 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
273 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
275 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
277 else if( stringKey == TEXT_COLOR_PROPERTY )
279 result = Toolkit::TextVisual::Property::TEXT_COLOR;
281 else if( stringKey == ENABLE_MARKUP_PROPERTY )
283 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
285 else if( stringKey == SHADOW_PROPERTY )
287 result = Toolkit::TextVisual::Property::SHADOW;
289 else if( stringKey == UNDERLINE_PROPERTY )
291 result = Toolkit::TextVisual::Property::UNDERLINE;
297 } // unnamed namespace
299 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
301 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
302 TextVisualPtr->SetProperties( properties );
303 return TextVisualPtr;
306 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
308 Property::Map outMap;
310 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
312 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
314 Property::Index indexKey = keyValue.first.indexKey;
316 if ( keyValue.first.type == Property::Key::STRING )
318 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
321 outMap.Insert( indexKey, keyValue.second );
324 propertyMap = outMap;
327 float TextVisual::GetHeightForWidth( float width )
329 return mController->GetHeightForWidth( width );
332 void TextVisual::GetNaturalSize( Vector2& naturalSize )
334 naturalSize = mController->GetNaturalSize().GetVectorXY();
337 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
339 Property::Value value;
342 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
345 mController->GetText( text );
346 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
348 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
350 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
351 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
353 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
355 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
357 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
359 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
361 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
363 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
365 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
366 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
368 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
369 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
372 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
375 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
377 mController->GetText( text );
378 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
382 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
383 : Visual::Base( factoryCache ),
384 mController( Text::Controller::New() ),
385 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
386 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
387 mRendererUpdateNeeded( false )
391 TextVisual::~TextVisual()
395 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
397 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
399 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
401 Property::Index indexKey = keyValue.first.indexKey;
403 if( keyValue.first.type == Property::Key::STRING )
405 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
408 DoSetProperty( indexKey, keyValue.second );
411 // Elide the text if it exceeds the boundaries.
412 mController->SetTextElideEnabled( true );
414 // Retrieve the layout engine to set the cursor's width.
415 Text::Layout::Engine& engine = mController->GetLayoutEngine();
417 // Sets 0 as cursor's width.
418 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
421 void TextVisual::DoSetOnStage( Actor& actor )
425 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
426 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
428 mImpl->mRenderer = Renderer::New( geometry, shader );
429 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
431 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
432 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
434 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
436 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
437 if( shaderTextColorIndex != Property::INVALID_INDEX )
439 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
440 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
445 // Renderer needs textures and to be added to control
446 mRendererUpdateNeeded = true;
451 void TextVisual::DoSetOffStage( Actor& actor )
453 if( mImpl->mRenderer )
455 // Removes the renderer from the actor.
456 actor.RemoveRenderer( mImpl->mRenderer );
460 // Resets the renderer.
461 mImpl->mRenderer.Reset();
464 // Resets the control handle.
468 void TextVisual::OnSetTransform()
473 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
477 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
479 const bool enableMarkup = propertyValue.Get<bool>();
480 mController->SetMarkupProcessorEnabled( enableMarkup );
483 case Toolkit::TextVisual::Property::TEXT:
485 mController->SetText( propertyValue.Get<std::string>() );
488 case Toolkit::TextVisual::Property::FONT_FAMILY:
490 SetFontFamilyProperty( mController, propertyValue );
493 case Toolkit::TextVisual::Property::FONT_STYLE:
495 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
498 case Toolkit::TextVisual::Property::POINT_SIZE:
500 const float pointSize = propertyValue.Get<float>();
501 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
503 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
507 case Toolkit::TextVisual::Property::MULTI_LINE:
509 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
512 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
516 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
517 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
519 mController->SetHorizontalAlignment( alignment );
524 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
528 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
529 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
531 mController->SetVerticalAlignment( alignment );
536 case Toolkit::TextVisual::Property::TEXT_COLOR:
538 const Vector4& textColor = propertyValue.Get< Vector4 >();
539 if( mController->GetDefaultColor() != textColor )
541 mController->SetDefaultColor( textColor );
545 case Toolkit::TextVisual::Property::SHADOW:
547 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
550 case Toolkit::TextVisual::Property::UNDERLINE:
552 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
558 void TextVisual::UpdateRenderer()
560 Actor control = mControl.GetHandle();
567 // Calculates the size to be used to relayout.
568 Vector2 relayoutSize;
570 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
571 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
573 // Round the size and offset to avoid pixel alignement issues.
574 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
575 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
577 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
579 // Removes the texture set.
582 // Remove any renderer previously set.
583 if( mImpl->mRenderer )
585 control.RemoveRenderer( mImpl->mRenderer );
588 // Nothing else to do if the relayout size is zero.
593 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
595 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
596 || mRendererUpdateNeeded )
598 mRendererUpdateNeeded = false;
600 // Removes the texture set.
603 // Remove any renderer previously set.
604 if( mImpl->mRenderer )
606 control.RemoveRenderer( mImpl->mRenderer );
609 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
610 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
612 // Check whether it is a markup text with multiple text colors
613 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
614 bool hasMultipleTextColors = ( NULL != colorsBuffer );
616 // Check whether the text contains any emoji
617 bool containsEmoji = false;
619 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
620 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
621 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
623 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
624 if( TextAbstraction::EMOJI == scriptRun.script )
626 containsEmoji = true;
631 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
633 bool shadowEnabled = false;
634 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
635 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
637 shadowEnabled = true;
640 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
641 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
643 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
645 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsEmoji, styleEnabled );
646 mImpl->mRenderer.SetTextures( textureSet );
648 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
649 mImpl->mRenderer.SetShader(shader);
651 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
653 Vector4 atlasRect = FULL_TEXTURE_RECT;
654 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
655 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
657 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
658 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
660 //Register transform properties
661 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
663 control.AddRenderer( mImpl->mRenderer );
665 // Text rendered and ready to display
671 void TextVisual::RemoveTextureSet()
673 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
675 // Removes the text's image from the texture atlas.
678 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
679 if( index != Property::INVALID_INDEX )
681 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
682 atlasRectValue.Get( atlasRect );
684 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
685 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
690 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
692 // Filter mode needs to be set to linear to produce better quality while scaling.
693 Sampler sampler = Sampler::New();
694 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
696 TextureSet textureSet = TextureSet::New();
698 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
699 Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
701 // Create a texture for the text without any styles
702 PixelData data = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
704 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
705 // In that case, create a texture. TODO: should tile the text.
707 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
708 data.GetPixelFormat(),
712 texture.Upload( data );
714 textureSet.SetTexture( 0u, texture );
715 textureSet.SetSampler( 0u, sampler );
719 // Create RGBA texture for all the text styles (without the text itself)
720 PixelData styleData = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
722 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
723 styleData.GetPixelFormat(),
724 styleData.GetWidth(),
725 styleData.GetHeight() );
727 styleTexture.Upload( styleData );
729 textureSet.SetTexture( 1u, styleTexture );
730 textureSet.SetSampler( 1u, sampler );
733 if ( containsEmoji && !hasMultipleTextColors )
735 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
736 PixelData maskData = mTypesetter->Render( size, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
738 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
739 maskData.GetPixelFormat(),
741 maskData.GetHeight() );
743 maskTexture.Upload( maskData );
747 textureSet.SetTexture( 1u, maskTexture );
748 textureSet.SetSampler( 1u, sampler );
752 textureSet.SetTexture( 2u, maskTexture );
753 textureSet.SetSampler( 2u, sampler );
760 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
764 if( hasMultipleTextColors && !styleEnabled )
766 // We don't animate text color if the text contains multiple colors
767 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
770 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
771 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
772 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
775 else if( hasMultipleTextColors && styleEnabled )
777 // We don't animate text color if the text contains multiple colors
778 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
781 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
782 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
783 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
786 else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
788 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
791 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
792 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
793 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
796 else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
798 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
801 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
802 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
803 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
806 else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
808 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
811 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
812 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
813 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
816 else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
818 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
821 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
822 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
823 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
830 } // namespace Internal
832 } // namespace Toolkit