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
115 mediump float textTexture = texture2D( sTexture, vTexCoord ).r;\n
117 // Set the color of the text to what it is animated to.
118 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, 1.0 );
122 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
123 varying mediump vec2 vTexCoord;\n
124 uniform sampler2D sTexture;\n
125 uniform lowp vec4 uColor;\n
126 uniform lowp vec3 mixColor;\n
130 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
132 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );
136 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
137 varying mediump vec2 vTexCoord;\n
138 uniform sampler2D sTexture;\n
139 uniform sampler2D sStyle;\n
140 uniform lowp vec4 uTextColorAnimatable;\n
141 uniform lowp vec4 uColor;\n
142 uniform lowp vec3 mixColor;\n
146 mediump float textTexture = texture2D( sTexture, vTexCoord ).r;\n
147 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
149 // Draw the text as overlay above the style
150 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - uTextColorAnimatable.a * textTexture ) ) * uColor * vec4( mixColor, 1.0 );\n
154 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
155 varying mediump vec2 vTexCoord;\n
156 uniform sampler2D sTexture;\n
157 uniform sampler2D sStyle;\n
158 uniform lowp vec4 uColor;\n
159 uniform lowp vec3 mixColor;\n
163 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
164 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
166 // Draw the text as overlay above the style
167 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, 1.0 );\n
171 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
172 varying mediump vec2 vTexCoord;\n
173 uniform sampler2D sTexture;\n
174 uniform sampler2D sMask;\n
175 uniform lowp vec4 uTextColorAnimatable;\n
176 uniform lowp vec4 uColor;\n
177 uniform lowp vec3 mixColor;\n
181 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
182 mediump float maskTexture = texture2D( sMask, vTexCoord ).r;\n
184 // Set the color of non-transparent pixel in text to what it is animated to.
185 // Markup text with multiple text colors are not animated (but can be supported later on if required).
186 // Emoji color are not animated.
187 mediump float vstep = step( 0.0001, textTexture.a );\n
188 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture );\n
190 // Draw the text as overlay above the style
191 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );\n
195 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
196 varying mediump vec2 vTexCoord;\n
197 uniform sampler2D sTexture;\n
198 uniform sampler2D sStyle;\n
199 uniform sampler2D sMask;\n
200 uniform lowp float uHasMultipleTextColors;\n
201 uniform lowp vec4 uTextColorAnimatable;\n
202 uniform lowp vec4 uColor;\n
203 uniform lowp vec3 mixColor;\n
207 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
208 mediump vec4 styleTexture = texture2D( sStyle, vTexCoord );\n
209 mediump float maskTexture = texture2D( sMask, vTexCoord ).r;\n
211 // Set the color of non-transparent pixel in text to what it is animated to.
212 // Markup text with multiple text colors are not animated (but can be supported later on if required).
213 // Emoji color are not animated.
214 mediump float vstep = step( 0.0001, textTexture.a );\n
215 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) );\n
217 // Draw the text as overlay above the style
218 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, 1.0 );\n
223 * Return Property index for the given string key
224 * param[in] stringKey the string index key
225 * return the key as an index
228 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
230 Dali::Property::Index result = Property::INVALID_KEY;
232 if( stringKey == VISUAL_TYPE )
234 result = Toolkit::Visual::Property::TYPE;
236 else if( stringKey == TEXT_PROPERTY )
238 result = Toolkit::TextVisual::Property::TEXT;
240 else if( stringKey == FONT_FAMILY_PROPERTY )
242 result = Toolkit::TextVisual::Property::FONT_FAMILY;
244 else if( stringKey == FONT_STYLE_PROPERTY )
246 result = Toolkit::TextVisual::Property::FONT_STYLE;
248 else if( stringKey == POINT_SIZE_PROPERTY )
250 result = Toolkit::TextVisual::Property::POINT_SIZE;
252 else if( stringKey == MULTI_LINE_PROPERTY )
254 result = Toolkit::TextVisual::Property::MULTI_LINE;
256 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
258 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
260 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
262 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
264 else if( stringKey == TEXT_COLOR_PROPERTY )
266 result = Toolkit::TextVisual::Property::TEXT_COLOR;
268 else if( stringKey == ENABLE_MARKUP_PROPERTY )
270 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
272 else if( stringKey == SHADOW_PROPERTY )
274 result = Toolkit::TextVisual::Property::SHADOW;
276 else if( stringKey == UNDERLINE_PROPERTY )
278 result = Toolkit::TextVisual::Property::UNDERLINE;
280 else if( stringKey == OUTLINE_PROPERTY )
282 result = Toolkit::DevelTextVisual::Property::OUTLINE;
284 else if( stringKey == BACKGROUND_PROPERTY )
286 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
292 void TextColorConstraint( Vector4& current, const PropertyInputContainer& inputs )
294 Vector4 color = inputs[0]->GetVector4();
295 current.r = color.r * color.a;
296 current.g = color.g * color.a;
297 current.b = color.b * color.a;
301 void OpacityConstraint( float& current, const PropertyInputContainer& inputs )
303 // Make zero if the alpha value of text color is zero to skip rendering text
304 if( EqualsZero( inputs[0]->GetVector4().a ) )
310 } // unnamed namespace
312 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
314 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
315 TextVisualPtr->SetProperties( properties );
316 return TextVisualPtr;
319 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
321 Property::Map outMap;
323 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
325 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
327 Property::Index indexKey = keyValue.first.indexKey;
329 if ( keyValue.first.type == Property::Key::STRING )
331 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
334 outMap.Insert( indexKey, keyValue.second );
337 propertyMap = outMap;
340 float TextVisual::GetHeightForWidth( float width )
342 return mController->GetHeightForWidth( width );
345 void TextVisual::GetNaturalSize( Vector2& naturalSize )
347 naturalSize = mController->GetNaturalSize().GetVectorXY();
350 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
352 Property::Value value;
355 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
358 mController->GetText( text );
359 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
361 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
363 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
364 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
366 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
368 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
370 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
372 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
374 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
376 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
378 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
379 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
381 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
382 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
384 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
385 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
387 GetBackgroundProperties( mController, value, Text::EffectStyle::DEFAULT );
388 map.Insert( Toolkit::DevelTextVisual::Property::BACKGROUND, value );
391 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
394 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
396 mController->GetText( text );
397 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
401 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
402 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
403 mController( Text::Controller::New() ),
404 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
405 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
406 mRendererUpdateNeeded( false )
410 TextVisual::~TextVisual()
414 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
416 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
418 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
420 Property::Index indexKey = keyValue.first.indexKey;
422 if( keyValue.first.type == Property::Key::STRING )
424 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
427 DoSetProperty( indexKey, keyValue.second );
430 // Elide the text if it exceeds the boundaries.
431 mController->SetTextElideEnabled( true );
433 // Retrieve the layout engine to set the cursor's width.
434 Text::Layout::Engine& engine = mController->GetLayoutEngine();
436 // Sets 0 as cursor's width.
437 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
440 void TextVisual::DoSetOnStage( Actor& actor )
444 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
445 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
447 mImpl->mRenderer = Renderer::New( geometry, shader );
448 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
450 // Enable the pre-multiplied alpha to improve the text quality
451 EnablePreMultipliedAlpha(true);
453 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
454 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
456 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
458 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
459 if( shaderTextColorIndex != Property::INVALID_INDEX )
461 Constraint colorConstraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint );
462 colorConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
463 colorConstraint.Apply();
465 // Make zero if the alpha value of text color is zero to skip rendering text
466 Constraint opacityConstraint = Constraint::New< float >( mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint );
467 opacityConstraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
468 opacityConstraint.Apply();
472 // Renderer needs textures and to be added to control
473 mRendererUpdateNeeded = true;
478 void TextVisual::DoSetOffStage( Actor& actor )
480 if( mImpl->mRenderer )
482 // Removes the renderer from the actor.
483 actor.RemoveRenderer( mImpl->mRenderer );
487 // Resets the renderer.
488 mImpl->mRenderer.Reset();
491 // Resets the control handle.
495 void TextVisual::OnSetTransform()
500 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
504 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
506 const bool enableMarkup = propertyValue.Get<bool>();
507 mController->SetMarkupProcessorEnabled( enableMarkup );
510 case Toolkit::TextVisual::Property::TEXT:
512 mController->SetText( propertyValue.Get<std::string>() );
515 case Toolkit::TextVisual::Property::FONT_FAMILY:
517 SetFontFamilyProperty( mController, propertyValue );
520 case Toolkit::TextVisual::Property::FONT_STYLE:
522 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
525 case Toolkit::TextVisual::Property::POINT_SIZE:
527 const float pointSize = propertyValue.Get<float>();
528 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
530 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
534 case Toolkit::TextVisual::Property::MULTI_LINE:
536 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
539 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
543 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
544 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
546 mController->SetHorizontalAlignment( alignment );
551 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
555 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
556 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
558 mController->SetVerticalAlignment( alignment );
563 case Toolkit::TextVisual::Property::TEXT_COLOR:
565 const Vector4& textColor = propertyValue.Get< Vector4 >();
566 if( mController->GetDefaultColor() != textColor )
568 mController->SetDefaultColor( textColor );
572 case Toolkit::TextVisual::Property::SHADOW:
574 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
577 case Toolkit::TextVisual::Property::UNDERLINE:
579 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
582 case Toolkit::DevelTextVisual::Property::OUTLINE:
584 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
587 case Toolkit::DevelTextVisual::Property::BACKGROUND:
589 SetBackgroundProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
595 void TextVisual::UpdateRenderer()
597 Actor control = mControl.GetHandle();
604 // Calculates the size to be used to relayout.
605 Vector2 relayoutSize;
607 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
608 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
610 // Round the size and offset to avoid pixel alignement issues.
611 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
612 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
615 mController->GetText( text );
617 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
619 // Removes the texture set.
622 // Remove any renderer previously set.
623 if( mImpl->mRenderer )
625 control.RemoveRenderer( mImpl->mRenderer );
628 // Nothing else to do if the relayout size is zero.
629 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
633 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
635 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
636 || mRendererUpdateNeeded )
638 mRendererUpdateNeeded = false;
640 // Removes the texture set.
643 // Remove any renderer previously set.
644 if( mImpl->mRenderer )
646 control.RemoveRenderer( mImpl->mRenderer );
649 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
650 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
652 // Check whether it is a markup text with multiple text colors
653 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
654 bool hasMultipleTextColors = ( NULL != colorsBuffer );
656 // Check whether the text contains any color glyph
657 bool containsColorGlyph = false;
659 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
660 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
661 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
662 for ( Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++ )
664 // Retrieve the glyph's info.
665 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
667 // Whether the current glyph is a color one.
668 if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
670 containsColorGlyph = true;
675 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
677 bool shadowEnabled = false;
678 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
679 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
681 shadowEnabled = true;
684 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
685 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
686 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();;
688 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled );
690 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
691 mImpl->mRenderer.SetTextures( textureSet );
693 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
694 mImpl->mRenderer.SetShader(shader);
696 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
698 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
700 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
702 //Register transform properties
703 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
705 control.AddRenderer( mImpl->mRenderer );
707 // Text rendered and ready to display
708 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
713 void TextVisual::RemoveTextureSet()
715 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
717 // Removes the text's image from the texture atlas.
720 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
721 if( index != Property::INVALID_INDEX )
723 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
724 atlasRectValue.Get( atlasRect );
726 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
727 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
732 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
734 // Filter mode needs to be set to linear to produce better quality while scaling.
735 Sampler sampler = Sampler::New();
736 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
738 TextureSet textureSet = TextureSet::New();
740 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
741 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
743 // Check the text direction
744 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
746 // Create a texture for the text without any styles
747 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
749 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
750 // In that case, create a texture. TODO: should tile the text.
752 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
753 data.GetPixelFormat(),
757 texture.Upload( data );
759 textureSet.SetTexture( 0u, texture );
760 textureSet.SetSampler( 0u, sampler );
764 // Create RGBA texture for all the text styles (without the text itself)
765 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
767 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
768 styleData.GetPixelFormat(),
769 styleData.GetWidth(),
770 styleData.GetHeight() );
772 styleTexture.Upload( styleData );
774 textureSet.SetTexture( 1u, styleTexture );
775 textureSet.SetSampler( 1u, sampler );
778 if ( containsColorGlyph && !hasMultipleTextColors )
780 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
781 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
783 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
784 maskData.GetPixelFormat(),
786 maskData.GetHeight() );
788 maskTexture.Upload( maskData );
792 textureSet.SetTexture( 1u, maskTexture );
793 textureSet.SetSampler( 1u, sampler );
797 textureSet.SetTexture( 2u, maskTexture );
798 textureSet.SetSampler( 2u, sampler );
805 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
809 if( hasMultipleTextColors && !styleEnabled )
811 // We don't animate text color if the text contains multiple colors
812 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
815 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
816 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
817 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
820 else if( hasMultipleTextColors && styleEnabled )
822 // We don't animate text color if the text contains multiple colors
823 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
826 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
827 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
828 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
831 else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
833 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
836 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
837 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
838 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
841 else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
843 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
846 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
847 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
848 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
851 else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
853 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
856 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
857 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
858 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
861 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
863 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
866 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
867 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
868 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
875 } // namespace Internal
877 } // namespace Toolkit