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/text-abstraction/text-abstraction-definitions.h>
26 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/internal/text/text-font-style.h>
35 #include <dali-toolkit/internal/text/text-effects-style.h>
36 #include <dali-toolkit/internal/text/script-run.h>
37 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
38 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
52 // Property names - common properties defined in visual-string-constants.h/cpp
53 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
54 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
55 const char * const POINT_SIZE_PROPERTY( "pointSize" );
56 const char * const MULTI_LINE_PROPERTY( "multiLine" );
57 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
58 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
59 const char * const TEXT_COLOR_PROPERTY( "textColor" );
60 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
61 const char * const SHADOW_PROPERTY( "shadow" );
62 const char * const UNDERLINE_PROPERTY( "underline" );
63 const char * const OUTLINE_PROPERTY( "outline" );
65 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
67 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
68 attribute mediump vec2 aPosition;\n
69 uniform mediump mat4 uMvpMatrix;\n
70 uniform mediump vec3 uSize;\n
71 uniform mediump vec4 pixelArea;\n
73 uniform mediump mat4 uModelMatrix;\n
74 uniform mediump mat4 uViewMatrix;\n
75 uniform mediump mat4 uProjection;\n
77 varying mediump vec2 vTexCoord;\n
79 //Visual size and offset
80 uniform mediump vec2 offset;\n
81 uniform mediump vec2 size;\n
82 uniform mediump vec4 offsetSizeMode;\n
83 uniform mediump vec2 origin;\n
84 uniform mediump vec2 anchorPoint;\n
86 vec4 ComputeVertexPosition()\n
88 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
89 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
90 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
95 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
96 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
97 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
99 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
100 gl_Position = vertexPosition;\n
104 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT = DALI_COMPOSE_SHADER(
105 varying mediump vec2 vTexCoord;\n
106 uniform sampler2D sTexture;\n
107 uniform lowp vec4 uTextColorAnimatable;\n
108 uniform mediump vec4 uAtlasRect;\n
109 uniform lowp vec4 uColor;\n
110 uniform lowp vec3 mixColor;\n
111 uniform lowp float opacity;\n
112 uniform lowp float preMultipliedAlpha;\n
114 lowp vec4 visualMixColor()\n
116 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
121 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
122 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
124 // Set the color of the text to what it is animated to.
125 gl_FragColor = uTextColorAnimatable * textTexture * uColor * visualMixColor();
129 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT = DALI_COMPOSE_SHADER(
130 varying mediump vec2 vTexCoord;\n
131 uniform sampler2D sTexture;\n
132 uniform mediump vec4 uAtlasRect;\n
133 uniform lowp vec4 uColor;\n
134 uniform lowp vec3 mixColor;\n
135 uniform lowp float opacity;\n
136 uniform lowp float preMultipliedAlpha;\n
138 lowp vec4 visualMixColor()\n
140 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
145 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
146 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
147 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
149 gl_FragColor = textTexture * uColor * visualMixColor();
153 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
154 varying mediump vec2 vTexCoord;\n
155 uniform sampler2D sTexture;\n
156 uniform sampler2D sStyle;\n
157 uniform lowp vec4 uTextColorAnimatable;\n
158 uniform mediump vec4 uAtlasRect;\n
159 uniform lowp vec4 uColor;\n
160 uniform lowp vec3 mixColor;\n
161 uniform lowp float opacity;\n
162 uniform lowp float preMultipliedAlpha;\n
164 lowp vec4 visualMixColor()\n
166 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
171 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
172 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
173 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
175 // Draw the text as overlay above the style
176 gl_FragColor = ( uTextColorAnimatable * textTexture + styleTexture * ( 1.0 - textTexture ) ) * uColor * visualMixColor();\n
180 const char* FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE = DALI_COMPOSE_SHADER(
181 varying mediump vec2 vTexCoord;\n
182 uniform sampler2D sTexture;\n
183 uniform sampler2D sStyle;\n
184 uniform mediump vec4 uAtlasRect;\n
185 uniform lowp vec4 uColor;\n
186 uniform lowp vec3 mixColor;\n
187 uniform lowp float opacity;\n
188 uniform lowp float preMultipliedAlpha;\n
190 lowp vec4 visualMixColor()\n
192 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
197 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
198 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
199 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
200 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
202 // Draw the text as overlay above the style
203 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * visualMixColor();\n
207 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI = DALI_COMPOSE_SHADER(
208 varying mediump vec2 vTexCoord;\n
209 uniform sampler2D sTexture;\n
210 uniform sampler2D sMask;\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
216 uniform lowp float preMultipliedAlpha;\n
218 lowp vec4 visualMixColor()\n
220 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
225 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
226 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
227 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
229 // Set the color of non-transparent pixel in text to what it is animated to.
230 // Markup text with multiple text colors are not animated (but can be supported later on if required).
231 // Emoji color are not animated.
232 mediump float vstep = step( 0.0001, textTexture.a );\n
233 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
235 // Draw the text as overlay above the style
236 gl_FragColor = textTexture * uColor * visualMixColor();\n
240 const char* FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI = DALI_COMPOSE_SHADER(
241 varying mediump vec2 vTexCoord;\n
242 uniform sampler2D sTexture;\n
243 uniform sampler2D sStyle;\n
244 uniform sampler2D sMask;\n
245 uniform lowp float uHasMultipleTextColors;\n
246 uniform lowp vec4 uTextColorAnimatable;\n
247 uniform mediump vec4 uAtlasRect;\n
248 uniform lowp vec4 uColor;\n
249 uniform lowp vec3 mixColor;\n
250 uniform lowp float opacity;\n
251 uniform lowp float preMultipliedAlpha;\n
253 lowp vec4 visualMixColor()\n
255 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
260 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
261 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
262 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
263 mediump float maskTexture = texture2D( sMask, texCoord ).r;\n
265 // Set the color of non-transparent pixel in text to what it is animated to.
266 // Markup text with multiple text colors are not animated (but can be supported later on if required).
267 // Emoji color are not animated.
268 mediump float vstep = step( 0.0001, textTexture.a );\n
269 textTexture.rgb = mix( textTexture.rgb, uTextColorAnimatable.rgb, vstep * maskTexture * ( 1.0 - uHasMultipleTextColors ) ) * mix( 1.0, textTexture.a, preMultipliedAlpha );\n
271 // Draw the text as overlay above the style
272 gl_FragColor = ( textTexture + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * visualMixColor();\n
277 * Return Property index for the given string key
278 * param[in] stringKey the string index key
279 * return the key as an index
282 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
284 Dali::Property::Index result = Property::INVALID_KEY;
286 if( stringKey == VISUAL_TYPE )
288 result = Toolkit::Visual::Property::TYPE;
290 else if( stringKey == TEXT_PROPERTY )
292 result = Toolkit::TextVisual::Property::TEXT;
294 else if( stringKey == FONT_FAMILY_PROPERTY )
296 result = Toolkit::TextVisual::Property::FONT_FAMILY;
298 else if( stringKey == FONT_STYLE_PROPERTY )
300 result = Toolkit::TextVisual::Property::FONT_STYLE;
302 else if( stringKey == POINT_SIZE_PROPERTY )
304 result = Toolkit::TextVisual::Property::POINT_SIZE;
306 else if( stringKey == MULTI_LINE_PROPERTY )
308 result = Toolkit::TextVisual::Property::MULTI_LINE;
310 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
312 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
314 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
316 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
318 else if( stringKey == TEXT_COLOR_PROPERTY )
320 result = Toolkit::TextVisual::Property::TEXT_COLOR;
322 else if( stringKey == ENABLE_MARKUP_PROPERTY )
324 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
326 else if( stringKey == SHADOW_PROPERTY )
328 result = Toolkit::TextVisual::Property::SHADOW;
330 else if( stringKey == UNDERLINE_PROPERTY )
332 result = Toolkit::TextVisual::Property::UNDERLINE;
334 else if( stringKey == OUTLINE_PROPERTY )
336 result = Toolkit::DevelTextVisual::Property::OUTLINE;
342 } // unnamed namespace
344 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
346 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
347 TextVisualPtr->SetProperties( properties );
348 return TextVisualPtr;
351 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
353 Property::Map outMap;
355 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
357 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
359 Property::Index indexKey = keyValue.first.indexKey;
361 if ( keyValue.first.type == Property::Key::STRING )
363 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
366 outMap.Insert( indexKey, keyValue.second );
369 propertyMap = outMap;
372 float TextVisual::GetHeightForWidth( float width )
374 return mController->GetHeightForWidth( width );
377 void TextVisual::GetNaturalSize( Vector2& naturalSize )
379 naturalSize = mController->GetNaturalSize().GetVectorXY();
382 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
384 Property::Value value;
387 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
390 mController->GetText( text );
391 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
393 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
395 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
396 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
398 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
400 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
402 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment() );
404 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment() );
406 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
408 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
410 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
411 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
413 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
414 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
416 GetOutlineProperties( mController, value, Text::EffectStyle::DEFAULT );
417 map.Insert( Toolkit::DevelTextVisual::Property::OUTLINE, value );
420 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
423 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
425 mController->GetText( text );
426 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
430 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
431 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
432 mController( Text::Controller::New() ),
433 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
434 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
435 mRendererUpdateNeeded( false )
439 TextVisual::~TextVisual()
443 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
445 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
447 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
449 Property::Index indexKey = keyValue.first.indexKey;
451 if( keyValue.first.type == Property::Key::STRING )
453 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
456 DoSetProperty( indexKey, keyValue.second );
459 // Elide the text if it exceeds the boundaries.
460 mController->SetTextElideEnabled( true );
462 // Retrieve the layout engine to set the cursor's width.
463 Text::Layout::Engine& engine = mController->GetLayoutEngine();
465 // Sets 0 as cursor's width.
466 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
469 void TextVisual::DoSetOnStage( Actor& actor )
473 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
474 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
476 mImpl->mRenderer = Renderer::New( geometry, shader );
477 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
479 // Enable the pre-multiplied alpha to improve the text quality
480 EnablePreMultipliedAlpha(true);
482 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
483 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
485 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
487 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
488 if( shaderTextColorIndex != Property::INVALID_INDEX )
490 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
491 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
496 // Renderer needs textures and to be added to control
497 mRendererUpdateNeeded = true;
502 void TextVisual::DoSetOffStage( Actor& actor )
504 if( mImpl->mRenderer )
506 // Removes the renderer from the actor.
507 actor.RemoveRenderer( mImpl->mRenderer );
511 // Resets the renderer.
512 mImpl->mRenderer.Reset();
515 // Resets the control handle.
519 void TextVisual::OnSetTransform()
524 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
528 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
530 const bool enableMarkup = propertyValue.Get<bool>();
531 mController->SetMarkupProcessorEnabled( enableMarkup );
534 case Toolkit::TextVisual::Property::TEXT:
536 mController->SetText( propertyValue.Get<std::string>() );
539 case Toolkit::TextVisual::Property::FONT_FAMILY:
541 SetFontFamilyProperty( mController, propertyValue );
544 case Toolkit::TextVisual::Property::FONT_STYLE:
546 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
549 case Toolkit::TextVisual::Property::POINT_SIZE:
551 const float pointSize = propertyValue.Get<float>();
552 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
554 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
558 case Toolkit::TextVisual::Property::MULTI_LINE:
560 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
563 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
567 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
568 if( Toolkit::Text::GetHorizontalAlignmentEnumeration( propertyValue, alignment ) )
570 mController->SetHorizontalAlignment( alignment );
575 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
579 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
580 if( Toolkit::Text::GetVerticalAlignmentEnumeration( propertyValue, alignment) )
582 mController->SetVerticalAlignment( alignment );
587 case Toolkit::TextVisual::Property::TEXT_COLOR:
589 const Vector4& textColor = propertyValue.Get< Vector4 >();
590 if( mController->GetDefaultColor() != textColor )
592 mController->SetDefaultColor( textColor );
596 case Toolkit::TextVisual::Property::SHADOW:
598 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
601 case Toolkit::TextVisual::Property::UNDERLINE:
603 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
606 case Toolkit::DevelTextVisual::Property::OUTLINE:
608 SetOutlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
614 void TextVisual::UpdateRenderer()
616 Actor control = mControl.GetHandle();
623 // Calculates the size to be used to relayout.
624 Vector2 relayoutSize;
626 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
627 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
629 // Round the size and offset to avoid pixel alignement issues.
630 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
631 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
634 mController->GetText( text );
636 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) || text.empty() )
638 // Removes the texture set.
641 // Remove any renderer previously set.
642 if( mImpl->mRenderer )
644 control.RemoveRenderer( mImpl->mRenderer );
647 // Nothing else to do if the relayout size is zero.
648 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
652 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
654 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
655 || mRendererUpdateNeeded )
657 mRendererUpdateNeeded = false;
659 // Removes the texture set.
662 // Remove any renderer previously set.
663 if( mImpl->mRenderer )
665 control.RemoveRenderer( mImpl->mRenderer );
668 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
669 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
671 // Check whether it is a markup text with multiple text colors
672 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
673 bool hasMultipleTextColors = ( NULL != colorsBuffer );
675 // Check whether the text contains any color glyph
676 bool containsColorGlyph = false;
678 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
679 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
680 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
681 for ( Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++ )
683 // Retrieve the glyph's info.
684 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
686 // Whether the current glyph is a color one.
687 if( fontClient.IsColorGlyph( glyphInfo->fontId, glyphInfo->index ) )
689 containsColorGlyph = true;
694 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
696 bool shadowEnabled = false;
697 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
698 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
700 shadowEnabled = true;
703 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
704 const bool outlineEnabled = ( mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1 );
706 const bool styleEnabled = ( shadowEnabled || underlineEnabled || outlineEnabled );
708 TextureSet textureSet = GetTextTexture( relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled );
709 mImpl->mRenderer.SetTextures( textureSet );
711 Shader shader = GetTextShader( mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled );
712 mImpl->mRenderer.SetShader(shader);
714 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
716 Vector4 atlasRect = FULL_TEXTURE_RECT;
717 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
718 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
720 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
722 //Register transform properties
723 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
725 control.AddRenderer( mImpl->mRenderer );
727 // Text rendered and ready to display
728 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
733 void TextVisual::RemoveTextureSet()
735 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
737 // Removes the text's image from the texture atlas.
740 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
741 if( index != Property::INVALID_INDEX )
743 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
744 atlasRectValue.Get( atlasRect );
746 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
747 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
752 TextureSet TextVisual::GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
754 // Filter mode needs to be set to linear to produce better quality while scaling.
755 Sampler sampler = Sampler::New();
756 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
758 TextureSet textureSet = TextureSet::New();
760 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
761 Pixel::Format textPixelFormat = ( containsColorGlyph || hasMultipleTextColors ) ? Pixel::RGBA8888 : Pixel::L8;
763 // Check the text direction
764 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
766 // Create a texture for the text without any styles
767 PixelData data = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat );
769 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
770 // In that case, create a texture. TODO: should tile the text.
772 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
773 data.GetPixelFormat(),
777 texture.Upload( data );
779 textureSet.SetTexture( 0u, texture );
780 textureSet.SetSampler( 0u, sampler );
784 // Create RGBA texture for all the text styles (without the text itself)
785 PixelData styleData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888 );
787 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
788 styleData.GetPixelFormat(),
789 styleData.GetWidth(),
790 styleData.GetHeight() );
792 styleTexture.Upload( styleData );
794 textureSet.SetTexture( 1u, styleTexture );
795 textureSet.SetSampler( 1u, sampler );
798 if ( containsColorGlyph && !hasMultipleTextColors )
800 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
801 PixelData maskData = mTypesetter->Render( size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8 );
803 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
804 maskData.GetPixelFormat(),
806 maskData.GetHeight() );
808 maskTexture.Upload( maskData );
812 textureSet.SetTexture( 1u, maskTexture );
813 textureSet.SetSampler( 1u, sampler );
817 textureSet.SetTexture( 2u, maskTexture );
818 textureSet.SetSampler( 2u, sampler );
825 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled )
829 if( hasMultipleTextColors && !styleEnabled )
831 // We don't animate text color if the text contains multiple colors
832 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT );
835 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT );
836 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
837 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader );
840 else if( hasMultipleTextColors && styleEnabled )
842 // We don't animate text color if the text contains multiple colors
843 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
846 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE );
847 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
848 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader );
851 else if( !hasMultipleTextColors && !containsColorGlyph && !styleEnabled )
853 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT );
856 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT );
857 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
858 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader );
861 else if( !hasMultipleTextColors && !containsColorGlyph && styleEnabled )
863 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
866 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE );
867 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
868 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader );
871 else if( !hasMultipleTextColors && containsColorGlyph && !styleEnabled )
873 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
876 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI );
877 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
878 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader );
881 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
883 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
886 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI );
887 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
888 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader );
895 } // namespace Internal
897 } // namespace Toolkit