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>
25 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
26 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
27 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
28 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
29 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/text/text-font-style.h>
33 #include <dali-toolkit/internal/text/text-effects-style.h>
47 // Property names - common properties defined in visual-string-constants.h/cpp
48 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
49 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
50 const char * const POINT_SIZE_PROPERTY( "pointSize" );
51 const char * const MULTI_LINE_PROPERTY( "multiLine" );
52 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
53 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
54 const char * const TEXT_COLOR_PROPERTY( "textColor" );
55 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
56 const char * const SHADOW_PROPERTY( "shadow" );
57 const char * const UNDERLINE_PROPERTY( "underline" );
59 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
61 { "BEGIN", Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN },
62 { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
63 { "END", Toolkit::Text::Layout::HORIZONTAL_ALIGN_END },
65 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
67 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
69 { "TOP", Toolkit::Text::Layout::VERTICAL_ALIGN_TOP },
70 { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
71 { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
73 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
75 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
77 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
79 const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
80 HORIZONTAL_ALIGNMENT_STRING_TABLE,
81 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
83 return std::string( name );
86 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
88 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
89 VERTICAL_ALIGNMENT_STRING_TABLE,
90 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
92 return std::string( name );
95 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
96 attribute mediump vec2 aPosition;\n
97 uniform mediump mat4 uMvpMatrix;\n
98 uniform mediump vec3 uSize;\n
99 uniform mediump vec4 pixelArea;\n
101 uniform mediump mat4 uModelMatrix;\n
102 uniform mediump mat4 uViewMatrix;\n
103 uniform mediump mat4 uProjection;\n
105 varying mediump vec2 vTexCoord;\n
107 //Visual size and offset
108 uniform mediump vec2 offset;\n
109 uniform mediump vec2 size;\n
110 uniform mediump vec4 offsetSizeMode;\n
111 uniform mediump vec2 origin;\n
112 uniform mediump vec2 anchorPoint;\n
114 vec4 ComputeVertexPosition()\n
116 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
117 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
118 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
123 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
124 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
125 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
127 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
128 gl_Position = vertexPosition;\n
132 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
133 varying mediump vec2 vTexCoord;\n
134 uniform sampler2D sTexture;\n
135 uniform sampler2D sStyle;\n
136 uniform sampler2D sMask;\n
137 uniform lowp float uHasMultipleTextColors;\n
138 uniform lowp vec4 uTextColorAnimatable;\n
139 uniform mediump vec4 uAtlasRect;\n
140 uniform lowp vec4 uColor;\n
141 uniform lowp vec3 mixColor;\n
142 uniform lowp float opacity;\n
146 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
147 mediump vec4 textTexture = texture2D( sTexture, texCoord );\n
148 mediump vec4 styleTexture = texture2D( sStyle, texCoord );\n
149 mediump vec4 maskTexture = texture2D( sMask, texCoord );\n
151 // Set the color of non-transparent pixel in text to what it is animated to.
152 // Markup text with multiple text colors are not animated (but can be supported later on if required).
153 // Emoji color are not animated.
154 mediump vec4 textColor = textTexture * textTexture.a;\n
155 mediump float vstep = step( 0.0001, textColor.a );\n
156 textColor.rgb = mix( textColor.rgb, uTextColorAnimatable.rgb, vstep * maskTexture.a * ( 1.0 - uHasMultipleTextColors ) );\n
158 // Draw the text as overlay above the style
159 gl_FragColor = ( textColor + styleTexture * ( 1.0 - textTexture.a ) ) * uColor * vec4( mixColor, opacity );\n
164 * Return Property index for the given string key
165 * param[in] stringKey the string index key
166 * return the key as an index
169 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
171 Dali::Property::Index result = Property::INVALID_KEY;
173 if( stringKey == VISUAL_TYPE )
175 result = Toolkit::Visual::Property::TYPE;
177 else if( stringKey == TEXT_PROPERTY )
179 result = Toolkit::TextVisual::Property::TEXT;
181 else if( stringKey == FONT_FAMILY_PROPERTY )
183 result = Toolkit::TextVisual::Property::FONT_FAMILY;
185 else if( stringKey == FONT_STYLE_PROPERTY )
187 result = Toolkit::TextVisual::Property::FONT_STYLE;
189 else if( stringKey == POINT_SIZE_PROPERTY )
191 result = Toolkit::TextVisual::Property::POINT_SIZE;
193 else if( stringKey == MULTI_LINE_PROPERTY )
195 result = Toolkit::TextVisual::Property::MULTI_LINE;
197 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
199 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
201 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
203 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
205 else if( stringKey == TEXT_COLOR_PROPERTY )
207 result = Toolkit::TextVisual::Property::TEXT_COLOR;
209 else if( stringKey == ENABLE_MARKUP_PROPERTY )
211 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
213 else if( stringKey == SHADOW_PROPERTY )
215 result = Toolkit::TextVisual::Property::SHADOW;
217 else if( stringKey == UNDERLINE_PROPERTY )
219 result = Toolkit::TextVisual::Property::UNDERLINE;
225 } // unnamed namespace
227 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
229 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
230 TextVisualPtr->SetProperties( properties );
231 return TextVisualPtr;
234 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
236 Property::Map outMap;
238 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
240 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
242 Property::Index indexKey = keyValue.first.indexKey;
244 if ( keyValue.first.type == Property::Key::STRING )
246 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
249 outMap.Insert( indexKey, keyValue.second );
252 propertyMap = outMap;
255 float TextVisual::GetHeightForWidth( float width )
257 return mController->GetHeightForWidth( width );
260 void TextVisual::GetNaturalSize( Vector2& naturalSize )
262 naturalSize = mController->GetNaturalSize().GetVectorXY();
265 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
267 Property::Value value;
270 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
273 mController->GetText( text );
274 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
276 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
278 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
279 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
281 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
283 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
285 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
287 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
289 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
291 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
293 GetShadowProperties( mController, value, Text::EffectStyle::DEFAULT );
294 map.Insert( Toolkit::TextVisual::Property::SHADOW, value );
296 GetUnderlineProperties( mController, value, Text::EffectStyle::DEFAULT );
297 map.Insert( Toolkit::TextVisual::Property::UNDERLINE, value );
300 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
303 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
305 mController->GetText( text );
306 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
310 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
311 : Visual::Base( factoryCache ),
312 mController( Text::Controller::New() ),
313 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) ),
314 mAnimatableTextColorPropertyIndex( Property::INVALID_INDEX ),
315 mRendererUpdateNeeded( false )
319 TextVisual::~TextVisual()
323 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
325 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
327 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
329 Property::Index indexKey = keyValue.first.indexKey;
331 if( keyValue.first.type == Property::Key::STRING )
333 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
336 DoSetProperty( indexKey, keyValue.second );
339 // Elide the text if it exceeds the boundaries.
340 mController->SetTextElideEnabled( true );
342 // Retrieve the layout engine to set the cursor's width.
343 Text::Layout::Engine& engine = mController->GetLayoutEngine();
345 // Sets 0 as cursor's width.
346 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
349 void TextVisual::DoSetOnStage( Actor& actor )
353 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
355 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::TEXT_SHADER );
358 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
359 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
361 mFactoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER, shader );
364 mImpl->mRenderer = Renderer::New( geometry, shader );
365 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
367 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
368 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty( "uTextColorAnimatable", defaultColor );
370 if ( mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX )
372 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
373 if( shaderTextColorIndex != Property::INVALID_INDEX )
375 Constraint constraint = Constraint::New<Vector4>( mImpl->mRenderer, shaderTextColorIndex, EqualToConstraint() );
376 constraint.AddSource( Source( actor, mAnimatableTextColorPropertyIndex ) );
381 // Renderer needs textures and to be added to control
382 mRendererUpdateNeeded = true;
387 void TextVisual::DoSetOffStage( Actor& actor )
389 if( mImpl->mRenderer )
391 // Removes the renderer from the actor.
392 actor.RemoveRenderer( mImpl->mRenderer );
396 // Resets the renderer.
397 mImpl->mRenderer.Reset();
400 // Resets the control handle.
404 void TextVisual::OnSetTransform()
409 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
413 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
415 const bool enableMarkup = propertyValue.Get<bool>();
416 mController->SetMarkupProcessorEnabled( enableMarkup );
419 case Toolkit::TextVisual::Property::TEXT:
421 mController->SetText( propertyValue.Get<std::string>() );
424 case Toolkit::TextVisual::Property::FONT_FAMILY:
426 SetFontFamilyProperty( mController, propertyValue );
429 case Toolkit::TextVisual::Property::FONT_STYLE:
431 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
434 case Toolkit::TextVisual::Property::POINT_SIZE:
436 const float pointSize = propertyValue.Get<float>();
437 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
439 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
443 case Toolkit::TextVisual::Property::MULTI_LINE:
445 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
448 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
450 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
451 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
452 HORIZONTAL_ALIGNMENT_STRING_TABLE,
453 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
456 mController->SetHorizontalAlignment( alignment );
460 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
462 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
463 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
464 VERTICAL_ALIGNMENT_STRING_TABLE,
465 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
468 mController->SetVerticalAlignment( alignment );
472 case Toolkit::TextVisual::Property::TEXT_COLOR:
474 const Vector4& textColor = propertyValue.Get< Vector4 >();
475 if( mController->GetDefaultColor() != textColor )
477 mController->SetDefaultColor( textColor );
481 case Toolkit::TextVisual::Property::SHADOW:
483 SetShadowProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
486 case Toolkit::TextVisual::Property::UNDERLINE:
488 SetUnderlineProperties( mController, propertyValue, Text::EffectStyle::DEFAULT );
494 void TextVisual::UpdateRenderer()
496 Actor control = mControl.GetHandle();
503 // Calculates the size to be used to relayout.
504 Vector2 relayoutSize;
506 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
507 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
509 // Round the size and offset to avoid pixel alignement issues.
510 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
511 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
513 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
515 // Removes the texture set.
518 // Remove any renderer previously set.
519 if( mImpl->mRenderer )
521 control.RemoveRenderer( mImpl->mRenderer );
524 // Nothing else to do if the relayout size is zero.
529 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
531 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType )
532 || mRendererUpdateNeeded )
534 mRendererUpdateNeeded = false;
536 // Removes the texture set.
539 // Remove any renderer previously set.
540 if( mImpl->mRenderer )
542 control.RemoveRenderer( mImpl->mRenderer );
545 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
546 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
548 Vector4 atlasRect = FULL_TEXTURE_RECT;
550 // Create a texture for the text without any styles
551 PixelData data = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_STYLES );
553 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
554 // In that case, create a texture. TODO: should tile the text.
556 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
557 data.GetPixelFormat(),
561 texture.Upload( data );
563 TextureSet textureSet = TextureSet::New();
564 textureSet.SetTexture( 0u, texture );
566 // Create a texture for all the text styles (without the text itself)
567 PixelData styleData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_NO_TEXT );
569 Texture styleTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
570 styleData.GetPixelFormat(),
571 styleData.GetWidth(),
572 styleData.GetHeight() );
574 styleTexture.Upload( styleData );
576 textureSet.SetTexture( 1u, styleTexture );
578 // Create a texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
579 PixelData maskData = mTypesetter->Render( relayoutSize, Text::Typesetter::RENDER_MASK );
581 Texture maskTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
582 styleData.GetPixelFormat(),
583 styleData.GetWidth(),
584 styleData.GetHeight() );
586 maskTexture.Upload( maskData );
588 textureSet.SetTexture( 2u, maskTexture );
590 // Filter mode needs to be set to linear to produce better quality while scaling.
591 Sampler sampler = Sampler::New();
592 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
593 textureSet.SetSampler( 0u, sampler );
594 textureSet.SetSampler( 1u, sampler );
595 textureSet.SetSampler( 2u, sampler );
597 mImpl->mRenderer.SetTextures( textureSet );
599 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
601 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
603 // Check whether it is a markup text with multiple text colors
604 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
605 bool hasMultipleTextColors = ( NULL != colorsBuffer );
606 mImpl->mRenderer.RegisterProperty( "uHasMultipleTextColors", static_cast<float>( hasMultipleTextColors ) );
608 //Register transform properties
609 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
611 control.AddRenderer( mImpl->mRenderer );
613 // Text rendered and ready to display
619 void TextVisual::RemoveTextureSet()
621 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
623 // Removes the text's image from the texture atlas.
626 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
627 if( index != Property::INVALID_INDEX )
629 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
630 atlasRectValue.Get( atlasRect );
632 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
633 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
638 } // namespace Internal
640 } // namespace Toolkit