2 * Copyright (c) 2018 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/rendering/renderer-devel.h>
24 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
27 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
28 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
29 #include <dali-toolkit/public-api/visuals/visual-properties.h>
30 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
31 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
32 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/text/text-font-style.h>
36 #include <dali-toolkit/internal/text/text-effects-style.h>
37 #include <dali-toolkit/internal/text/script-run.h>
38 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
39 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
53 // Property names - common properties defined in visual-string-constants.h/cpp
54 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
55 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
56 const char * const POINT_SIZE_PROPERTY( "pointSize" );
57 const char * const MULTI_LINE_PROPERTY( "multiLine" );
58 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
59 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
60 const char * const TEXT_COLOR_PROPERTY( "textColor" );
61 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
62 const char * const SHADOW_PROPERTY( "shadow" );
63 const char * const UNDERLINE_PROPERTY( "underline" );
64 const char * const OUTLINE_PROPERTY( "outline" );
65 const char * const BACKGROUND_PROPERTY( "textBackground" );
67 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
69 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
70 attribute mediump vec2 aPosition;\n
71 uniform mediump mat4 uMvpMatrix;\n
72 uniform mediump vec3 uSize;\n
73 uniform mediump vec4 pixelArea;\n
75 uniform mediump mat4 uModelMatrix;\n
76 uniform mediump mat4 uViewMatrix;\n
77 uniform mediump mat4 uProjection;\n
79 varying mediump vec2 vTexCoord;\n
81 //Visual size and offset
82 uniform mediump vec2 offset;\n
83 uniform mediump vec2 size;\n
84 uniform mediump vec4 offsetSizeMode;\n
85 uniform mediump vec2 origin;\n
86 uniform mediump vec2 anchorPoint;\n
88 vec4 ComputeVertexPosition()\n
90 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
91 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
92 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
97 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
98 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
99 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
101 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
102 gl_Position = vertexPosition;\n
106 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT = DALI_COMPOSE_SHADER(
107 varying mediump vec2 vTexCoord;\n
108 uniform sampler2D sTexture;\n
109 uniform lowp vec4 uTextColorAnimatable;\n
110 uniform lowp vec4 uColor;\n
111 uniform lowp vec3 mixColor;\n
112 uniform lowp float preMultipliedAlpha;\n
116 mediump float textTexture = texture2D( sTexture, vTexCoord ).r;\n
118 // Set the color of the text to what it is animated to.
119 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, 1.0 );
123 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
124 varying mediump vec2 vTexCoord;\n
125 uniform sampler2D sTexture;\n
126 uniform lowp vec4 uColor;\n
127 uniform lowp vec3 mixColor;\n
128 uniform lowp float preMultipliedAlpha;\n
132 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
133 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
135 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );
139 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
140 varying mediump vec2 vTexCoord;\n
141 uniform sampler2D sTexture;\n
142 uniform sampler2D sStyle;\n
143 uniform lowp vec4 uTextColorAnimatable;\n
144 uniform lowp vec4 uColor;\n
145 uniform lowp vec3 mixColor;\n
146 uniform lowp float preMultipliedAlpha;\n
150 mediump float textTexture = texture2D( sTexture, vTexCoord ).r;\n
151 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
153 // Draw the text as overlay above the style
154 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - textTexture ) ) * uColor * vec4( mixColor, 1.0 );\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 lowp vec4 uColor;\n
163 uniform lowp vec3 mixColor;\n
164 uniform lowp float preMultipliedAlpha;\n
168 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
169 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
170 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
172 // Draw the text as overlay above the style
173 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, 1.0 );\n
177 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
178 varying mediump vec2 vTexCoord;\n
179 uniform sampler2D sTexture;\n
180 uniform sampler2D sMask;\n
181 uniform lowp vec4 uTextColorAnimatable;\n
182 uniform lowp vec4 uColor;\n
183 uniform lowp vec3 mixColor;\n
184 uniform lowp float preMultipliedAlpha;\n
188 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
189 mediump float maskTexture = texture2D( sMask, vTexCoord ).r;\n
191 // Set the color of non-transparent pixel in text to what it is animated to.
192 // Markup text with multiple text colors are not animated (but can be supported later on if required).
193 // Emoji color are not animated.
194 mediump float vstep = step( 0.0001, textTexture.a );\n
195 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
197 // Draw the text as overlay above the style
198 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );\n
202 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
203 varying mediump vec2 vTexCoord;\n
204 uniform sampler2D sTexture;\n
205 uniform sampler2D sStyle;\n
206 uniform sampler2D sMask;\n
207 uniform lowp float uHasMultipleTextColors;\n
208 uniform lowp vec4 uTextColorAnimatable;\n
209 uniform lowp vec4 uColor;\n
210 uniform lowp vec3 mixColor;\n
211 uniform lowp float preMultipliedAlpha;\n
215 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
216 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
217 mediump float maskTexture = texture2D( sMask, vTexCoord ).r;\n
219 // Set the color of non-transparent pixel in text to what it is animated to.
220 // Markup text with multiple text colors are not animated (but can be supported later on if required).
221 // Emoji color are not animated.
222 mediump float vstep = step( 0.0001, textTexture.a );\n
223 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
225 // Draw the text as overlay above the style
226 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, 1.0 );\n
231 * Return Property index for the given string key
232 * param[in] stringKey the string index key
233 * return the key as an index
236 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
238 Dali::Property::Index result = Property::INVALID_KEY;
240 if( stringKey == VISUAL_TYPE )
242 result = Toolkit::Visual::Property::TYPE;
244 else if( stringKey == TEXT_PROPERTY )
246 result = Toolkit::TextVisual::Property::TEXT;
248 else if( stringKey == FONT_FAMILY_PROPERTY )
250 result = Toolkit::TextVisual::Property::FONT_FAMILY;
252 else if( stringKey == FONT_STYLE_PROPERTY )
254 result = Toolkit::TextVisual::Property::FONT_STYLE;
256 else if( stringKey == POINT_SIZE_PROPERTY )
258 result = Toolkit::TextVisual::Property::POINT_SIZE;
260 else if( stringKey == MULTI_LINE_PROPERTY )
262 result = Toolkit::TextVisual::Property::MULTI_LINE;
264 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
266 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
268 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
270 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
272 else if( stringKey == TEXT_COLOR_PROPERTY )
274 result = Toolkit::TextVisual::Property::TEXT_COLOR;
276 else if( stringKey == ENABLE_MARKUP_PROPERTY )
278 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
280 else if( stringKey == SHADOW_PROPERTY )
282 result = Toolkit::TextVisual::Property::SHADOW;
284 else if( stringKey == UNDERLINE_PROPERTY )
286 result = Toolkit::TextVisual::Property::UNDERLINE;
288 else if( stringKey == OUTLINE_PROPERTY )
290 result = Toolkit::DevelTextVisual::Property::OUTLINE;
292 else if( stringKey == BACKGROUND_PROPERTY )
294 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
300 void OpacityConstraint( float& current, const PropertyInputContainer& inputs )
302 current = inputs[0]->GetVector4().a;
305 } // unnamed namespace
307 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
309 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
310 TextVisualPtr->SetProperties( properties );
311 return TextVisualPtr;
314 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
316 Property::Map outMap;
318 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
320 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
322 Property::Index indexKey = keyValue.first.indexKey;
324 if ( keyValue.first.type == Property::Key::STRING )
326 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
329 outMap.Insert( indexKey, keyValue.second );
332 propertyMap = outMap;
335 float TextVisual::GetHeightForWidth( float width )
337 return mController->GetHeightForWidth( width );
340 void TextVisual::GetNaturalSize( Vector2& naturalSize )
342 naturalSize = mController->GetNaturalSize().GetVectorXY();
345 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
347 Property::Value value;
350 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
353 mController->GetText( text );
354 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
356 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
358 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
359 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
361 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
363 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
365 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
367 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
369 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
371 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
373 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
374 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
376 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
377 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
379 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
380 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
382 GetBackgroundProperties( mController, value, Text::EffectStyle::DEFAULT );
383 map.Insert( Toolkit::DevelTextVisual::Property::BACKGROUND, value );
386 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
389 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
391 mController->GetText( text );
392 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
396 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
397 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
398 mController( Text::Controller::New() ),
399 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
400 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
401 mRendererUpdateNeeded( false )
405 TextVisual::~TextVisual()
409 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
411 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
413 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
415 Property::Index indexKey = keyValue.first.indexKey;
417 if( keyValue.first.type == Property::Key::STRING )
419 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
422 DoSetProperty( indexKey, keyValue.second );
425 // Elide the text if it exceeds the boundaries.
426 mController->SetTextElideEnabled( true );
428 // Retrieve the layout engine to set the cursor's width.
429 Text::Layout::Engine& engine = mController->GetLayoutEngine();
431 // Sets 0 as cursor's width.
432 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
435 void TextVisual::DoSetOnStage( Actor& actor )
439 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
440 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
442 mImpl->mRenderer = Renderer::New( geometry, shader );
443 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
445 // Enable the pre-multiplied alpha to improve the text quality
446 EnablePreMultipliedAlpha(true);
448 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
449 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
451 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
453 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
454 if( shaderTextColorIndex != Property::INVALID_INDEX )
456 Constraint colorConstraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
457 colorConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
458 colorConstraint.Apply();
460 Constraint opacityConstraint = Constraint::New< float >( mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint );
461 opacityConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
462 opacityConstraint.Apply();
466 // Renderer needs textures and to be added to control
467 mRendererUpdateNeeded = true;
472 void TextVisual::DoSetOffStage( Actor& actor )
474 if( mImpl->mRenderer )
476 // Removes the renderer from the actor.
477 actor.RemoveRenderer( mImpl->mRenderer );
481 // Resets the renderer.
482 mImpl->mRenderer.Reset();
485 // Resets the control handle.
489 void TextVisual::OnSetTransform()
494 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
498 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
500 const bool enableMarkup = propertyValue.Get<bool>();
501 mController->SetMarkupProcessorEnabled( enableMarkup );
504 case Toolkit::TextVisual::Property::TEXT:
506 mController->SetText( propertyValue.Get<std::string>() );
509 case Toolkit::TextVisual::Property::FONT_FAMILY:
511 SetFontFamilyProperty( mController, propertyValue );
514 case Toolkit::TextVisual::Property::FONT_STYLE:
516 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
519 case Toolkit::TextVisual::Property::POINT_SIZE:
521 const float pointSize = propertyValue.Get<float>();
522 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
524 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
528 case Toolkit::TextVisual::Property::MULTI_LINE:
530 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
533 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
537 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
538 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
540 mController->SetHorizontalAlignment( alignment );
545 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
549 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
550 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
552 mController->SetVerticalAlignment( alignment );
557 case Toolkit::TextVisual::Property::TEXT_COLOR:
559 const Vector4& textColor = propertyValue.Get< Vector4 >();
560 if( mController->GetDefaultColor() != textColor )
562 mController->SetDefaultColor( textColor );
566 case Toolkit::TextVisual::Property::SHADOW:
568 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
571 case Toolkit::TextVisual::Property::UNDERLINE:
573 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
576 case Toolkit::DevelTextVisual::Property::OUTLINE:
578 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
581 case Toolkit::DevelTextVisual::Property::BACKGROUND:
583 SetBackgroundProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
589 void TextVisual::UpdateRenderer()
591 Actor control = mControl.GetHandle();
598 // Calculates the size to be used to relayout.
599 Vector2 relayoutSize;
601 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
602 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
604 // Round the size and offset to avoid pixel alignement issues.
605 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
606 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
609 mController->GetText( text );
611 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
613 // Removes the texture set.
616 // Remove any renderer previously set.
617 if( mImpl->mRenderer )
619 control.RemoveRenderer( mImpl->mRenderer );
622 // Nothing else to do if the relayout size is zero.
623 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
627 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
629 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
630 || mRendererUpdateNeeded )
632 mRendererUpdateNeeded = false;
634 // Removes the texture set.
637 // Remove any renderer previously set.
638 if( mImpl->mRenderer )
640 control.RemoveRenderer( mImpl->mRenderer );
643 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
644 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
646 // Check whether it is a markup text with multiple text colors
647 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
648 bool hasMultipleTextColors = ( NULL != colorsBuffer );
650 // Check whether the text contains any color glyph
651 bool containsColorGlyph = false;
653 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
654 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
655 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
656 for ( Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++ )
658 // Retrieve the glyph's info.
659 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
661 // Whether the current glyph is a color one.
662 if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
664 containsColorGlyph = true;
669 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
671 bool shadowEnabled = false;
672 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
673 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
675 shadowEnabled = true;
678 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
679 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
680 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();;
682 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled );
684 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
685 mImpl->mRenderer.SetTextures( textureSet );
687 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
688 mImpl->mRenderer.SetShader(shader);
690 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
692 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
694 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
696 //Register transform properties
697 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
699 control.AddRenderer( mImpl->mRenderer );
701 // Text rendered and ready to display
702 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
707 void TextVisual::RemoveTextureSet()
709 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
711 // Removes the text's image from the texture atlas.
714 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
715 if( index != Property::INVALID_INDEX )
717 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
718 atlasRectValue.Get( atlasRect );
720 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
721 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
726 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
728 // Filter mode needs to be set to linear to produce better quality while scaling.
729 Sampler sampler = Sampler::New();
730 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
732 TextureSet textureSet = TextureSet::New();
734 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
735 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
737 // Check the text direction
738 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
740 // Create a texture for the text without any styles
741 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
743 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
744 // In that case, create a texture. TODO: should tile the text.
746 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
747 data.GetPixelFormat(),
751 texture.Upload( data );
753 textureSet.SetTexture( 0u, texture );
754 textureSet.SetSampler( 0u, sampler );
758 // Create RGBA texture for all the text styles (without the text itself)
759 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
761 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
762 styleData.GetPixelFormat(),
763 styleData.GetWidth(),
764 styleData.GetHeight() );
766 styleTexture.Upload( styleData );
768 textureSet.SetTexture( 1u, styleTexture );
769 textureSet.SetSampler( 1u, sampler );
772 if ( containsColorGlyph && !hasMultipleTextColors )
774 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
775 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
777 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
778 maskData.GetPixelFormat(),
780 maskData.GetHeight() );
782 maskTexture.Upload( maskData );
786 textureSet.SetTexture( 1u, maskTexture );
787 textureSet.SetSampler( 1u, sampler );
791 textureSet.SetTexture( 2u, maskTexture );
792 textureSet.SetSampler( 2u, sampler );
799 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
803 if( hasMultipleTextColors && !styleEnabled )
805 // We don't animate text color if the text contains multiple colors
806 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
809 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
810 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
811 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
814 else if( hasMultipleTextColors && styleEnabled )
816 // We don't animate text color if the text contains multiple colors
817 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
820 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
821 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
822 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
825 else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
827 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
830 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
831 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
832 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
835 else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
837 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
840 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
841 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
842 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
845 else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
847 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
850 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
851 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
852 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
855 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
857 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
860 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
861 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
862 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
869 } // namespace Internal
871 } // namespace Toolkit