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 vec4 textColor = textTexture * textTexture.a;\n
198 mediump float vstep = step( 0.0001, textColor.a );\n
199 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture );\n
201 // Draw the text as overlay above the style
202 gl_FragColor = textColor * uColor * vec4( mixColor, opacity );\n
206 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
207 varying mediump vec2 vTexCoord;\n
208 uniform sampler2D sTexture;\n
209 uniform sampler2D sStyle;\n
210 uniform sampler2D sMask;\n
211 uniform lowp float uHasMultipleTextColors;\n
212 uniform lowp vec4 uTextColorAnimatable;\n
213 uniform mediump vec4 uAtlasRect;\n
214 uniform lowp vec4 uColor;\n
215 uniform lowp vec3 mixColor;\n
216 uniform lowp float opacity;\n
220 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
221 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
222 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
223 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
225 // Set the color of non-transparent pixel in text to what it is animated to.
226 // Markup text with multiple text colors are not animated (but can be supported later on if required).
227 // Emoji color are not animated.
228 mediump vec4 textColor = textTexture * textTexture.a;\n
229 mediump float vstep = step( 0.0001, textColor.a );\n
230 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) );\n
232 // Draw the text as overlay above the style
233 gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
238 * Return Property index for the given string key
239 * param[in] stringKey the string index key
240 * return the key as an index
243 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
245 Dali::Property::Index result = Property::INVALID_KEY;
247 if( stringKey == VISUAL_TYPE )
249 result = Toolkit::Visual::Property::TYPE;
251 else if( stringKey == TEXT_PROPERTY )
253 result = Toolkit::TextVisual::Property::TEXT;
255 else if( stringKey == FONT_FAMILY_PROPERTY )
257 result = Toolkit::TextVisual::Property::FONT_FAMILY;
259 else if( stringKey == FONT_STYLE_PROPERTY )
261 result = Toolkit::TextVisual::Property::FONT_STYLE;
263 else if( stringKey == POINT_SIZE_PROPERTY )
265 result = Toolkit::TextVisual::Property::POINT_SIZE;
267 else if( stringKey == MULTI_LINE_PROPERTY )
269 result = Toolkit::TextVisual::Property::MULTI_LINE;
271 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
273 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
275 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
277 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
279 else if( stringKey == TEXT_COLOR_PROPERTY )
281 result = Toolkit::TextVisual::Property::TEXT_COLOR;
283 else if( stringKey == ENABLE_MARKUP_PROPERTY )
285 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
287 else if( stringKey == SHADOW_PROPERTY )
289 result = Toolkit::TextVisual::Property::SHADOW;
291 else if( stringKey == UNDERLINE_PROPERTY )
293 result = Toolkit::TextVisual::Property::UNDERLINE;
299 } // unnamed namespace
301 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
303 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
304 TextVisualPtr->SetProperties( properties );
305 return TextVisualPtr;
308 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
310 Property::Map outMap;
312 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
314 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
316 Property::Index indexKey = keyValue.first.indexKey;
318 if ( keyValue.first.type == Property::Key::STRING )
320 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
323 outMap.Insert( indexKey, keyValue.second );
326 propertyMap = outMap;
329 float TextVisual::GetHeightForWidth( float width )
331 return mController->GetHeightForWidth( width );
334 void TextVisual::GetNaturalSize( Vector2& naturalSize )
336 naturalSize = mController->GetNaturalSize().GetVectorXY();
339 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
341 Property::Value value;
344 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
347 mController->GetText( text );
348 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
350 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
352 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
353 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
355 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
357 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
359 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
361 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
363 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
365 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
367 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
368 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
370 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
371 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
374 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
377 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
379 mController->GetText( text );
380 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
384 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
385 : Visual::Base( factoryCache ),
386 mController( Text::Controller::New() ),
387 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
388 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
389 mRendererUpdateNeeded( false )
393 TextVisual::~TextVisual()
397 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
399 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
401 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
403 Property::Index indexKey = keyValue.first.indexKey;
405 if( keyValue.first.type == Property::Key::STRING )
407 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
410 DoSetProperty( indexKey, keyValue.second );
413 // Elide the text if it exceeds the boundaries.
414 mController->SetTextElideEnabled( true );
416 // Retrieve the layout engine to set the cursor's width.
417 Text::Layout::Engine& engine = mController->GetLayoutEngine();
419 // Sets 0 as cursor's width.
420 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
423 void TextVisual::DoSetOnStage( Actor& actor )
427 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
428 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
430 mImpl->mRenderer = Renderer::New( geometry, shader );
431 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
433 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
434 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
436 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
438 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
439 if( shaderTextColorIndex != Property::INVALID_INDEX )
441 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
442 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
447 // Renderer needs textures and to be added to control
448 mRendererUpdateNeeded = true;
453 void TextVisual::DoSetOffStage( Actor& actor )
455 if( mImpl->mRenderer )
457 // Removes the renderer from the actor.
458 actor.RemoveRenderer( mImpl->mRenderer );
462 // Resets the renderer.
463 mImpl->mRenderer.Reset();
466 // Resets the control handle.
470 void TextVisual::OnSetTransform()
475 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
479 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
481 const bool enableMarkup = propertyValue.Get<bool>();
482 mController->SetMarkupProcessorEnabled( enableMarkup );
485 case Toolkit::TextVisual::Property::TEXT:
487 mController->SetText( propertyValue.Get<std::string>() );
490 case Toolkit::TextVisual::Property::FONT_FAMILY:
492 SetFontFamilyProperty( mController, propertyValue );
495 case Toolkit::TextVisual::Property::FONT_STYLE:
497 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
500 case Toolkit::TextVisual::Property::POINT_SIZE:
502 const float pointSize = propertyValue.Get<float>();
503 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
505 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
509 case Toolkit::TextVisual::Property::MULTI_LINE:
511 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
514 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
518 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
519 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
521 mController->SetHorizontalAlignment( alignment );
526 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
530 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
531 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
533 mController->SetVerticalAlignment( alignment );
538 case Toolkit::TextVisual::Property::TEXT_COLOR:
540 const Vector4& textColor = propertyValue.Get< Vector4 >();
541 if( mController->GetDefaultColor() != textColor )
543 mController->SetDefaultColor( textColor );
547 case Toolkit::TextVisual::Property::SHADOW:
549 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
552 case Toolkit::TextVisual::Property::UNDERLINE:
554 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
560 void TextVisual::UpdateRenderer()
562 Actor control = mControl.GetHandle();
569 // Calculates the size to be used to relayout.
570 Vector2 relayoutSize;
572 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
573 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
575 // Round the size and offset to avoid pixel alignement issues.
576 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
577 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
579 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
581 // Removes the texture set.
584 // Remove any renderer previously set.
585 if( mImpl->mRenderer )
587 control.RemoveRenderer( mImpl->mRenderer );
590 // Nothing else to do if the relayout size is zero.
595 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
597 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
598 || mRendererUpdateNeeded )
600 mRendererUpdateNeeded = false;
602 // Removes the texture set.
605 // Remove any renderer previously set.
606 if( mImpl->mRenderer )
608 control.RemoveRenderer( mImpl->mRenderer );
611 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
612 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
614 // Check whether it is a markup text with multiple text colors
615 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
616 bool hasMultipleTextColors = ( NULL != colorsBuffer );
618 // Check whether the text contains any emoji
619 bool containsEmoji = false;
621 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
622 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
623 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
625 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
626 if( TextAbstraction::EMOJI == scriptRun.script )
628 containsEmoji = true;
633 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
635 bool shadowEnabled = false;
636 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
637 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
639 shadowEnabled = true;
642 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
643 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
645 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
647 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsEmoji, styleEnabled );
648 mImpl->mRenderer.SetTextures( textureSet );
650 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsEmoji, styleEnabled );
651 mImpl->mRenderer.SetShader(shader);
653 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
655 Vector4 atlasRect = FULL_TEXTURE_RECT;
656 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
657 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
659 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
660 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
662 //Register transform properties
663 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
665 control.AddRenderer( mImpl->mRenderer );
667 // Text rendered and ready to display
673 void TextVisual::RemoveTextureSet()
675 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
677 // Removes the text's image from the texture atlas.
680 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
681 if( index != Property::INVALID_INDEX )
683 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
684 atlasRectValue.Get( atlasRect );
686 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
687 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
692 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
694 // Filter mode needs to be set to linear to produce better quality while scaling.
695 Sampler sampler = Sampler::New();
696 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
698 TextureSet textureSet = TextureSet::New();
700 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
701 Pixel::Format textPixelFormat = ( containsEmoji || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
703 // Create a texture for the text without any styles
704 PixelData data = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
706 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
707 // In that case, create a texture. TODO: should tile the text.
709 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
710 data.GetPixelFormat(),
714 texture.Upload( data );
716 textureSet.SetTexture( 0u, texture );
717 textureSet.SetSampler( 0u, sampler );
721 // Create RGBA texture for all the text styles (without the text itself)
722 PixelData styleData = mTypesetter->Render( size, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
724 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
725 styleData.GetPixelFormat(),
726 styleData.GetWidth(),
727 styleData.GetHeight() );
729 styleTexture.Upload( styleData );
731 textureSet.SetTexture( 1u, styleTexture );
732 textureSet.SetSampler( 1u, sampler );
735 if ( containsEmoji && !hasMultipleTextColors )
737 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
738 PixelData maskData = mTypesetter->Render( size, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
740 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
741 maskData.GetPixelFormat(),
743 maskData.GetHeight() );
745 maskTexture.Upload( maskData );
749 textureSet.SetTexture( 1u, maskTexture );
750 textureSet.SetSampler( 1u, sampler );
754 textureSet.SetTexture( 2u, maskTexture );
755 textureSet.SetSampler( 2u, sampler );
762 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled )
766 if( hasMultipleTextColors && !styleEnabled )
768 // We don't animate text color if the text contains multiple colors
769 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
772 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
773 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
774 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
777 else if( hasMultipleTextColors && styleEnabled )
779 // We don't animate text color if the text contains multiple colors
780 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
783 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
784 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
785 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
788 else if( !hasMultipleTextColors && !containsEmoji && !styleEnabled )
790 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
793 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
794 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
795 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
798 else if( !hasMultipleTextColors && !containsEmoji && styleEnabled )
800 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
803 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
804 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
805 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
808 else if( !hasMultipleTextColors && containsEmoji && !styleEnabled )
810 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
813 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
814 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
815 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
818 else // if( !hasMultipleTextColors && containsEmoji && styleEnabled )
820 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
823 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
824 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
825 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
832 } // namespace Internal
834 } // namespace Toolkit