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 );
85 return std::string( name );
88 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
90 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
91 VERTICAL_ALIGNMENT_STRING_TABLE,
92 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
94 return std::string( name );
97 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
98 attribute mediump vec2 aPosition;\n
99 uniform mediump mat4 uMvpMatrix;\n
100 uniform mediump vec3 uSize;\n
101 uniform mediump vec4 pixelArea;\n
103 uniform mediump mat4 uModelMatrix;\n
104 uniform mediump mat4 uViewMatrix;\n
105 uniform mediump mat4 uProjection;\n
107 varying mediump vec2 vTexCoord;\n
109 //Visual size and offset
110 uniform mediump vec2 offset;\n
111 uniform mediump vec2 size;\n
112 uniform mediump vec4 offsetSizeMode;\n
113 uniform mediump vec2 origin;\n
114 uniform mediump vec2 anchorPoint;\n
116 vec4 ComputeVertexPosition()\n
118 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
119 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
120 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
125 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
126 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
127 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
129 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
130 gl_Position = vertexPosition;\n
134 const char* FRAGMENT_SHADER_ATLAS_CLAMP_RGBA = DALI_COMPOSE_SHADER(
135 varying mediump vec2 vTexCoord;\n
136 uniform sampler2D sTexture;\n
137 uniform sampler2D sStyle;\n
138 uniform sampler2D sMask;\n
139 uniform lowp float uHasMultipleTextColors;\n
140 uniform lowp vec4 uTextColorAnimatable;\n
141 uniform mediump vec4 uAtlasRect;\n
142 uniform lowp vec4 uColor;\n
143 uniform lowp vec3 mixColor;\n
144 uniform lowp float opacity;\n
148 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
149 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
150 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
151 mediump vec4 maskTexture = texture2D( sMask, texCoord );\n
153 // Set the color of non-transparent pixel in text to what it is animated to.
154 // Markup text with multiple text colors are not animated (but can be supported later on if required).
155 // Emoji color are not animated.
156 mediump vec4 textColor = textTexture * textTexture.a;\n
157 mediump float vstep = step( 0.0001, textColor.a );\n
158 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture.a * ( 1.0 - uHasMultipleTextColors ) );\n
160 // Draw the text as overlay above the style
161 gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
165 const char* FRAGMENT_SHADER_ATLAS_CLAMP_L8 = DALI_COMPOSE_SHADER(
166 varying mediump vec2 vTexCoord;\n
167 uniform sampler2D sTexture;\n
168 uniform lowp vec4 uTextColorAnimatable;\n
169 uniform mediump vec4 uAtlasRect;\n
170 uniform lowp vec4 uColor;\n
171 uniform lowp vec3 mixColor;\n
172 uniform lowp float opacity;\n
176 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
177 mediump float textTexture = texture2D( sTexture, texCoord ).r;\n
179 // Set the color of the text to what it is animated to.
180 gl_FragColor = uTextColorAnimatable * textTexture * uColor * vec4( mixColor, opacity );\n
185 * Return Property index for the given string key
186 * param[in] stringKey the string index key
187 * return the key as an index
190 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
192 Dali::Property::Index result = Property::INVALID_KEY;
194 if( stringKey == VISUAL_TYPE )
196 result = Toolkit::Visual::Property::TYPE;
198 else if( stringKey == TEXT_PROPERTY )
200 result = Toolkit::TextVisual::Property::TEXT;
202 else if( stringKey == FONT_FAMILY_PROPERTY )
204 result = Toolkit::TextVisual::Property::FONT_FAMILY;
206 else if( stringKey == FONT_STYLE_PROPERTY )
208 result = Toolkit::TextVisual::Property::FONT_STYLE;
210 else if( stringKey == POINT_SIZE_PROPERTY )
212 result = Toolkit::TextVisual::Property::POINT_SIZE;
214 else if( stringKey == MULTI_LINE_PROPERTY )
216 result = Toolkit::TextVisual::Property::MULTI_LINE;
218 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
220 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
222 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
224 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
226 else if( stringKey == TEXT_COLOR_PROPERTY )
228 result = Toolkit::TextVisual::Property::TEXT_COLOR;
230 else if( stringKey == ENABLE_MARKUP_PROPERTY )
232 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
234 else if( stringKey == SHADOW_PROPERTY )
236 result = Toolkit::TextVisual::Property::SHADOW;
238 else if( stringKey == UNDERLINE_PROPERTY )
240 result = Toolkit::TextVisual::Property::UNDERLINE;
246 } // unnamed namespace
248 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
250 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
251 TextVisualPtr->SetProperties( properties );
252 return TextVisualPtr;
255 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
257 Property::Map outMap;
259 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
261 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
263 Property::Index indexKey = keyValue.first.indexKey;
265 if ( keyValue.first.type == Property::Key::STRING )
267 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
270 outMap.Insert( indexKey, keyValue.second );
273 propertyMap = outMap;
276 float TextVisual::GetHeightForWidth( float width )
278 return mController->GetHeightForWidth( width );
281 void TextVisual::GetNaturalSize( Vector2& naturalSize )
283 naturalSize = mController->GetNaturalSize().GetVectorXY();
286 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
288 Property::Value value;
291 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
294 mController->GetText( text );
295 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
297 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
299 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
300 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
302 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
304 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
306 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
308 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
310 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
312 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
314 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
315 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
317 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
318 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
321 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
324 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
326 mController->GetText( text );
327 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
331 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
332 : Visual::Base( factoryCache ),
333 mController( Text::Controller::New() ),
334 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
335 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
336 mRendererUpdateNeeded( false )
340 TextVisual::~TextVisual()
344 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
346 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
348 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
350 Property::Index indexKey = keyValue.first.indexKey;
352 if( keyValue.first.type == Property::Key::STRING )
354 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
357 DoSetProperty( indexKey, keyValue.second );
360 // Elide the text if it exceeds the boundaries.
361 mController->SetTextElideEnabled( true );
363 // Retrieve the layout engine to set the cursor's width.
364 Text::Layout::Engine& engine = mController->GetLayoutEngine();
366 // Sets 0 as cursor's width.
367 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
370 void TextVisual::DoSetOnStage( Actor& actor )
374 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
375 Shader shader = GetTextShader(mFactoryCache, true);
377 mImpl->mRenderer = Renderer::New( geometry, shader );
378 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
380 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
381 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
383 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
385 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
386 if( shaderTextColorIndex != Property::INVALID_INDEX )
388 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
389 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
394 // Renderer needs textures and to be added to control
395 mRendererUpdateNeeded = true;
400 void TextVisual::DoSetOffStage( Actor& actor )
402 if( mImpl->mRenderer )
404 // Removes the renderer from the actor.
405 actor.RemoveRenderer( mImpl->mRenderer );
409 // Resets the renderer.
410 mImpl->mRenderer.Reset();
413 // Resets the control handle.
417 void TextVisual::OnSetTransform()
422 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
426 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
428 const bool enableMarkup = propertyValue.Get<bool>();
429 mController->SetMarkupProcessorEnabled( enableMarkup );
432 case Toolkit::TextVisual::Property::TEXT:
434 mController->SetText( propertyValue.Get<std::string>() );
437 case Toolkit::TextVisual::Property::FONT_FAMILY:
439 SetFontFamilyProperty( mController, propertyValue );
442 case Toolkit::TextVisual::Property::FONT_STYLE:
444 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
447 case Toolkit::TextVisual::Property::POINT_SIZE:
449 const float pointSize = propertyValue.Get<float>();
450 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
452 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
456 case Toolkit::TextVisual::Property::MULTI_LINE:
458 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
461 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
463 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
464 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
465 HORIZONTAL_ALIGNMENT_STRING_TABLE,
466 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
469 mController->SetHorizontalAlignment( alignment );
473 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
475 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
476 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
477 VERTICAL_ALIGNMENT_STRING_TABLE,
478 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
481 mController->SetVerticalAlignment( alignment );
485 case Toolkit::TextVisual::Property::TEXT_COLOR:
487 const Vector4& textColor = propertyValue.Get< Vector4 >();
488 if( mController->GetDefaultColor() != textColor )
490 mController->SetDefaultColor( textColor );
494 case Toolkit::TextVisual::Property::SHADOW:
496 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
499 case Toolkit::TextVisual::Property::UNDERLINE:
501 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
507 void TextVisual::UpdateRenderer()
509 Actor control = mControl.GetHandle();
516 // Calculates the size to be used to relayout.
517 Vector2 relayoutSize;
519 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
520 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
522 // Round the size and offset to avoid pixel alignement issues.
523 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
524 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
526 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
528 // Removes the texture set.
531 // Remove any renderer previously set.
532 if( mImpl->mRenderer )
534 control.RemoveRenderer( mImpl->mRenderer );
537 // Nothing else to do if the relayout size is zero.
542 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
544 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
545 || mRendererUpdateNeeded )
547 mRendererUpdateNeeded = false;
549 // Removes the texture set.
552 // Remove any renderer previously set.
553 if( mImpl->mRenderer )
555 control.RemoveRenderer( mImpl->mRenderer );
558 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
559 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
561 // Check whether it is a markup text with multiple text colors
562 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
563 bool hasMultipleTextColors = ( NULL != colorsBuffer );
565 // Check whether the text contains any emoji
566 bool containsEmoji = false;
568 Text::ScriptRunIndex numberOfScripts = mController->GetTextModel()->GetNumberOfScripts();
569 const Text::ScriptRun* scripts = mController->GetTextModel()->GetScriptRuns();
570 for ( Text::ScriptRunIndex scriptIndex = 0u; scriptIndex < numberOfScripts; scriptIndex++ )
572 const Text::ScriptRun& scriptRun = *( scripts + scriptIndex );
573 if( TextAbstraction::EMOJI == scriptRun.script )
575 containsEmoji = true;
580 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
581 bool shadowEnabled = false;
582 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
583 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
585 shadowEnabled = true;
588 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
590 if ( hasMultipleTextColors || containsEmoji || shadowEnabled || underlineEnabled )
592 // Create RGBA textures if the text contains emojis or styles or multiple text colors
594 // Create a texture for the text without any styles
595 PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES );
597 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
598 // In that case, create a texture. TODO: should tile the text.
600 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
601 data.GetPixelFormat(),
605 texture.Upload( data );
607 TextureSet textureSet = TextureSet::New();
608 textureSet.SetTexture( 0u, texture );
610 // Create a texture for all the text styles (without the text itself)
611 PixelData styleData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_TEXT );
613 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
614 styleData.GetPixelFormat(),
615 styleData.GetWidth(),
616 styleData.GetHeight() );
618 styleTexture.Upload( styleData );
620 textureSet.SetTexture( 1u, styleTexture );
622 // Create a texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
623 PixelData maskData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_MASK );
625 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
626 styleData.GetPixelFormat(),
627 styleData.GetWidth(),
628 styleData.GetHeight() );
630 maskTexture.Upload( maskData );
632 textureSet.SetTexture( 2u, maskTexture );
634 // Filter mode needs to be set to nearest to produce better quality while static.
635 Sampler sampler = Sampler::New();
636 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
637 textureSet.SetSampler( 0u, sampler );
638 textureSet.SetSampler( 1u, sampler );
639 textureSet.SetSampler( 2u, sampler );
641 mImpl->mRenderer.SetTextures( textureSet );
643 Shader shader = GetTextShader(mFactoryCache, true); // RGBA shader
644 mImpl->mRenderer.SetShader(shader);
648 // Create L8 texture if the text contains only single text color with no emoji and no style
650 // Create a texture for the text without any styles
651 PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES, false, Pixel::L8 );
653 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
654 // In that case, create a texture. TODO: should tile the text.
656 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
657 data.GetPixelFormat(),
661 texture.Upload( data );
663 TextureSet textureSet = TextureSet::New();
664 textureSet.SetTexture( 0u, texture );
666 // Filter mode needs to be set to nearest to produce better quality while static.
667 Sampler sampler = Sampler::New();
668 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
669 textureSet.SetSampler( 0u, sampler );
671 mImpl->mRenderer.SetTextures( textureSet );
673 Shader shader = GetTextShader(mFactoryCache, false); // L8 shader
674 mImpl->mRenderer.SetShader(shader);
677 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
679 Vector4 atlasRect = FULL_TEXTURE_RECT;
680 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
681 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
683 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON);
685 //Register transform properties
686 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
688 control.AddRenderer( mImpl->mRenderer );
690 // Text rendered and ready to display
696 void TextVisual::RemoveTextureSet()
698 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
700 // Removes the text's image from the texture atlas.
703 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
704 if( index != Property::INVALID_INDEX )
706 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
707 atlasRectValue.Get( atlasRect );
709 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
710 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
715 Shader TextVisual::GetTextShader( VisualFactoryCache& factoryCache, bool isRgbaTexture )
720 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_RGBA );
723 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP_RGBA );
724 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
725 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_RGBA, shader );
730 shader = factoryCache.GetShader( VisualFactoryCache::TEXT_SHADER_L8 );
733 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP_L8 );
734 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
735 factoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER_L8, shader );
742 } // namespace Internal
744 } // namespace Toolkit