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/devel-api/visuals/text-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.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>
49 // Property names - common properties defined in visual-string-constants.h/cpp
50 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
51 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
52 const char * const POINT_SIZE_PROPERTY( "pointSize" );
53 const char * const MULTI_LINE_PROPERTY( "multiLine" );
54 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
55 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
56 const char * const TEXT_COLOR_PROPERTY( "textColor" );
57 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
58 const char * const SHADOW_PROPERTY( "shadow" );
59 const char * const UNDERLINE_PROPERTY( "underline" );
61 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
63 { "BEGIN", Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN },
64 { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
65 { "END", Toolkit::Text::Layout::HORIZONTAL_ALIGN_END },
67 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
69 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
71 { "TOP", Toolkit::Text::Layout::VERTICAL_ALIGN_TOP },
72 { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
73 { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
75 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
77 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
79 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
81 const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
82 HORIZONTAL_ALIGNMENT_STRING_TABLE,
83 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
87 return std::string( name );
92 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
94 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
95 VERTICAL_ALIGNMENT_STRING_TABLE,
96 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
100 return std::string( name );
102 return std::string();
105 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
106 attribute mediump vec2 aPosition;\n
107 uniform mediump mat4 uMvpMatrix;\n
108 uniform mediump vec3 uSize;\n
109 uniform mediump vec4 pixelArea;\n
111 uniform mediump mat4 uModelMatrix;\n
112 uniform mediump mat4 uViewMatrix;\n
113 uniform mediump mat4 uProjection;\n
115 varying mediump vec2 vTexCoord;\n
117 //Visual size and offset
118 uniform mediump vec2 offset;\n
119 uniform mediump vec2 size;\n
120 uniform mediump vec4 offsetSizeMode;\n
121 uniform mediump vec2 origin;\n
122 uniform mediump vec2 anchorPoint;\n
124 vec4 ComputeVertexPosition()\n
126 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
127 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
128 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
133 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
134 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
135 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
137 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
138 gl_Position = vertexPosition;\n
142 const char* FRAGMENT_SHADER_ATLAS_CLAMP_RGBA = DALI_COMPOSE_SHADER(
143 varying mediump vec2 vTexCoord;\n
144 uniform sampler2D sTexture;\n
145 uniform sampler2D sStyle;\n
146 uniform sampler2D sMask;\n
147 uniform lowp float uHasMultipleTextColors;\n
148 uniform lowp vec4 uTextColorAnimatable;\n
149 uniform mediump vec4 uAtlasRect;\n
150 uniform lowp vec4 uColor;\n
151 uniform lowp vec3 mixColor;\n
152 uniform lowp float opacity;\n
156 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
157 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
158 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
159 mediump vec4 maskTexture = texture2D( sMask, texCoord );\n
161 // Set the color of non-transparent pixel in text to what it is animated to.
162 // Markup text with multiple text colors are not animated (but can be supported later on if required).
163 // Emoji color are not animated.
164 mediump vec4 textColor = textTexture * textTexture.a;\n
165 mediump float vstep = step( 0.0001, textColor.a );\n
166 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture.a * ( 1.0 - uHasMultipleTextColors ) );\n
168 // Draw the text as overlay above the style
169 gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
173 const char* FRAGMENT_SHADER_ATLAS_CLAMP_L8 = DALI_COMPOSE_SHADER(
174 varying mediump vec2 vTexCoord;\n
175 uniform sampler2D sTexture;\n
176 uniform lowp vec4 uTextColorAnimatable;\n
177 uniform mediump vec4 uAtlasRect;\n
178 uniform lowp vec4 uColor;\n
179 uniform lowp vec3 mixColor;\n
180 uniform lowp float opacity;\n
184 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
185 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
187 // Set the color of the text to what it is animated to.
188 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, opacity );\n
193 * Return Property index for the given string key
194 * param[in] stringKey the string index key
195 * return the key as an index
198 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
200 Dali::Property::Index result = Property::INVALID_KEY;
202 if( stringKey == VISUAL_TYPE )
204 result = Toolkit::Visual::Property::TYPE;
206 else if( stringKey == TEXT_PROPERTY )
208 result = Toolkit::TextVisual::Property::TEXT;
210 else if( stringKey == FONT_FAMILY_PROPERTY )
212 result = Toolkit::TextVisual::Property::FONT_FAMILY;
214 else if( stringKey == FONT_STYLE_PROPERTY )
216 result = Toolkit::TextVisual::Property::FONT_STYLE;
218 else if( stringKey == POINT_SIZE_PROPERTY )
220 result = Toolkit::TextVisual::Property::POINT_SIZE;
222 else if( stringKey == MULTI_LINE_PROPERTY )
224 result = Toolkit::TextVisual::Property::MULTI_LINE;
226 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
228 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
230 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
232 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
234 else if( stringKey == TEXT_COLOR_PROPERTY )
236 result = Toolkit::TextVisual::Property::TEXT_COLOR;
238 else if( stringKey == ENABLE_MARKUP_PROPERTY )
240 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
242 else if( stringKey == SHADOW_PROPERTY )
244 result = Toolkit::TextVisual::Property::SHADOW;
246 else if( stringKey == UNDERLINE_PROPERTY )
248 result = Toolkit::TextVisual::Property::UNDERLINE;
254 } // unnamed namespace
256 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
258 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
259 TextVisualPtr->SetProperties( properties );
260 return TextVisualPtr;
263 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
265 Property::Map outMap;
267 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
269 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
271 Property::Index indexKey = keyValue.first.indexKey;
273 if ( keyValue.first.type == Property::Key::STRING )
275 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
278 outMap.Insert( indexKey, keyValue.second );
281 propertyMap = outMap;
284 float TextVisual::GetHeightForWidth( float width )
286 return mController->GetHeightForWidth( width );
289 void TextVisual::GetNaturalSize( Vector2& naturalSize )
291 naturalSize = mController->GetNaturalSize().GetVectorXY();
294 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
296 Property::Value value;
299 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
302 mController->GetText( text );
303 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
305 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
307 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
308 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
310 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
312 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
314 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
316 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
318 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
320 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
322 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
323 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
325 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
326 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
329 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
332 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
334 mController->GetText( text );
335 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
339 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
340 : Visual::Base( factoryCache ),
341 mController( Text::Controller::New() ),
342 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
343 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
344 mRendererUpdateNeeded( false )
348 TextVisual::~TextVisual()
352 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
354 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
356 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
358 Property::Index indexKey = keyValue.first.indexKey;
360 if( keyValue.first.type == Property::Key::STRING )
362 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
365 DoSetProperty( indexKey, keyValue.second );
368 // Elide the text if it exceeds the boundaries.
369 mController->SetTextElideEnabled( true );
371 // Retrieve the layout engine to set the cursor's width.
372 Text::Layout::Engine& engine = mController->GetLayoutEngine();
374 // Sets 0 as cursor's width.
375 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
378 void TextVisual::DoSetOnStage( Actor& actor )
382 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
383 Shader shader = GetTextShader(mFactoryCache, true);
385 mImpl->mRenderer = Renderer::New( geometry, shader );
386 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
388 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
389 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
391 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
393 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
394 if( shaderTextColorIndex != Property::INVALID_INDEX )
396 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
397 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
402 // Renderer needs textures and to be added to control
403 mRendererUpdateNeeded = true;
408 void TextVisual::DoSetOffStage( Actor& actor )
410 if( mImpl->mRenderer )
412 // Removes the renderer from the actor.
413 actor.RemoveRenderer( mImpl->mRenderer );
417 // Resets the renderer.
418 mImpl->mRenderer.Reset();
421 // Resets the control handle.
425 void TextVisual::OnSetTransform()
430 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
434 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
436 const bool enableMarkup = propertyValue.Get<bool>();
437 mController->SetMarkupProcessorEnabled( enableMarkup );
440 case Toolkit::TextVisual::Property::TEXT:
442 mController->SetText( propertyValue.Get<std::string>() );
445 case Toolkit::TextVisual::Property::FONT_FAMILY:
447 SetFontFamilyProperty( mController, propertyValue );
450 case Toolkit::TextVisual::Property::FONT_STYLE:
452 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
455 case Toolkit::TextVisual::Property::POINT_SIZE:
457 const float pointSize = propertyValue.Get<float>();
458 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
460 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
464 case Toolkit::TextVisual::Property::MULTI_LINE:
466 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
469 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
471 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
472 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
473 HORIZONTAL_ALIGNMENT_STRING_TABLE,
474 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
477 mController->SetHorizontalAlignment( alignment );
481 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
483 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
484 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
485 VERTICAL_ALIGNMENT_STRING_TABLE,
486 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
489 mController->SetVerticalAlignment( alignment );
493 case Toolkit::TextVisual::Property::TEXT_COLOR:
495 const Vector4& textColor = propertyValue.Get< Vector4 >();
496 if( mController->GetDefaultColor() != textColor )
498 mController->SetDefaultColor( textColor );
502 case Toolkit::TextVisual::Property::SHADOW:
504 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
507 case Toolkit::TextVisual::Property::UNDERLINE:
509 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
515 void TextVisual::UpdateRenderer()
517 Actor control = mControl.GetHandle();
524 // Calculates the size to be used to relayout.
525 Vector2 relayoutSize;
527 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
528 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
530 // Round the size and offset to avoid pixel alignement issues.
531 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
532 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
534 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
536 // Removes the texture set.
539 // Remove any renderer previously set.
540 if( mImpl->mRenderer )
542 control.RemoveRenderer( mImpl->mRenderer );
545 // Nothing else to do if the relayout size is zero.
550 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
552 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
553 || mRendererUpdateNeeded )
555 mRendererUpdateNeeded = false;
557 // Removes the texture set.
560 // Remove any renderer previously set.
561 if( mImpl->mRenderer )
563 control.RemoveRenderer( mImpl->mRenderer );
566 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
567 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
569 // Check whether it is a markup text with multiple text colors
570 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
571 bool hasMultipleTextColors = ( NULL != colorsBuffer );
573 // Check whether the text contains any emoji
574 bool containsEmoji = false;
576 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
577 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
578 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
580 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
581 if( TextAbstraction::EMOJI == scriptRun.script )
583 containsEmoji = true;
588 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
590 bool shadowEnabled = false;
591 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
592 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
594 shadowEnabled = true;
597 bool outlineWidthEnabled = false;
598 float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
599 if ( outlineWidth > Math::MACHINE_EPSILON_1 )
601 outlineWidthEnabled = true;
604 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
606 if ( hasMultipleTextColors || containsEmoji || shadowEnabled || underlineEnabled || outlineWidthEnabled )
608 // Create RGBA textures if the text contains emojis or styles or multiple text colors
610 // Create a texture for the text without any styles
611 PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES );
613 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
614 // In that case, create a texture. TODO: should tile the text.
616 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
617 data.GetPixelFormat(),
621 texture.Upload( data );
623 TextureSet textureSet = TextureSet::New();
624 textureSet.SetTexture( 0u, texture );
626 // Create a texture for all the text styles (without the text itself)
627 PixelData styleData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_TEXT );
629 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
630 styleData.GetPixelFormat(),
631 styleData.GetWidth(),
632 styleData.GetHeight() );
634 styleTexture.Upload( styleData );
636 textureSet.SetTexture( 1u, styleTexture );
638 // Create a texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
639 PixelData maskData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_MASK );
641 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
642 styleData.GetPixelFormat(),
643 styleData.GetWidth(),
644 styleData.GetHeight() );
646 maskTexture.Upload( maskData );
648 textureSet.SetTexture( 2u, maskTexture );
650 // Filter mode needs to be set to nearest to produce better quality while static.
651 Sampler sampler = Sampler::New();
652 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
653 textureSet.SetSampler( 0u, sampler );
654 textureSet.SetSampler( 1u, sampler );
655 textureSet.SetSampler( 2u, sampler );
657 mImpl->mRenderer.SetTextures( textureSet );
659 Shader shader = GetTextShader(mFactoryCache, true); // RGBA shader
660 mImpl->mRenderer.SetShader(shader);
664 // Create L8 texture if the text contains only single text color with no emoji and no style
666 // Create a texture for the text without any styles
667 PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES, false, Pixel::L8 );
669 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
670 // In that case, create a texture. TODO: should tile the text.
672 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
673 data.GetPixelFormat(),
677 texture.Upload( data );
679 TextureSet textureSet = TextureSet::New();
680 textureSet.SetTexture( 0u, texture );
682 // Filter mode needs to be set to nearest to produce better quality while static.
683 Sampler sampler = Sampler::New();
684 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
685 textureSet.SetSampler( 0u, sampler );
687 mImpl->mRenderer.SetTextures( textureSet );
689 Shader shader = GetTextShader(mFactoryCache, false); // L8 shader
690 mImpl->mRenderer.SetShader(shader);
693 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
695 Vector4 atlasRect = FULL_TEXTURE_RECT;
696 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
697 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
699 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
701 //Register transform properties
702 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
704 control.AddRenderer( mImpl->mRenderer );
706 // Text rendered and ready to display
712 void TextVisual::RemoveTextureSet()
714 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
716 // Removes the text's image from the texture atlas.
719 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
720 if( index != Property::INVALID_INDEX )
722 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
723 atlasRectValue.Get( atlasRect );
725 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
726 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
731 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool isRgbaTexture )
736 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_RGBA );
739 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP_RGBA );
740 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
741 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_RGBA, shader );
746 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_L8 );
749 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP_L8 );
750 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
751 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_L8, shader );
758 } // namespace Internal
760 } // namespace Toolkit