2 * Copyright (c) 2016 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-toolkit/devel-api/visuals/text-visual-properties.h>
23 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
25 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
26 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
27 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
28 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
29 #include <dali-toolkit/internal/text/text-font-style.h>
43 // Property names - common properties defined in visual-string-constants.h/cpp
44 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
45 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
46 const char * const POINT_SIZE_PROPERTY( "pointSize" );
47 const char * const MULTI_LINE_PROPERTY( "multiLine" );
48 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
49 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
50 const char * const TEXT_COLOR_PROPERTY( "textColor" );
51 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
53 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
55 { "BEGIN", Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN },
56 { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
57 { "END", Toolkit::Text::Layout::HORIZONTAL_ALIGN_END },
59 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
61 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
63 { "TOP", Toolkit::Text::Layout::VERTICAL_ALIGN_TOP },
64 { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
65 { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
67 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
69 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
71 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
73 const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
74 HORIZONTAL_ALIGNMENT_STRING_TABLE,
75 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
77 return std::string( name );
80 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
82 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
83 VERTICAL_ALIGNMENT_STRING_TABLE,
84 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
86 return std::string( name );
89 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
90 attribute mediump vec2 aPosition;\n
91 uniform mediump mat4 uMvpMatrix;\n
92 uniform mediump vec3 uSize;\n
93 uniform mediump vec4 pixelArea;
95 uniform mediump mat4 uModelMatrix;\n
96 uniform mediump mat4 uViewMatrix;\n
97 uniform mediump mat4 uProjection;\n
99 varying mediump vec2 vTexCoord;\n
101 //Visual size and offset
102 uniform mediump vec2 offset;\n
103 uniform mediump vec2 size;\n
104 uniform mediump vec4 offsetSizeMode;\n
105 uniform mediump vec2 origin;\n
106 uniform mediump vec2 anchorPoint;\n
108 vec4 ComputeVertexPosition()\n
110 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
111 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
112 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
117 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
118 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
119 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
121 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
122 gl_Position = vertexPosition;\n
126 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
127 varying mediump vec2 vTexCoord;\n
128 uniform sampler2D sTexture;\n
129 uniform mediump vec4 uAtlasRect;\n
130 uniform lowp vec4 uColor;\n
131 uniform lowp vec3 mixColor;\n
132 uniform lowp float opacity;\n
136 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
137 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * vec4( mixColor, opacity );\n
142 * Return Property index for the given string key
143 * param[in] stringKey the string index key
144 * return the key as an index
147 Dali::Property::Index StringKeyToIndexKey( const std::string& stringKey )
149 Dali::Property::Index result = Property::INVALID_KEY;
151 if( stringKey == VISUAL_TYPE )
153 result = Toolkit::Visual::Property::TYPE;
155 else if( stringKey == TEXT_PROPERTY )
157 result = Toolkit::TextVisual::Property::TEXT;
159 else if( stringKey == FONT_FAMILY_PROPERTY )
161 result = Toolkit::TextVisual::Property::FONT_FAMILY;
163 else if( stringKey == FONT_STYLE_PROPERTY )
165 result = Toolkit::TextVisual::Property::FONT_STYLE;
167 else if( stringKey == POINT_SIZE_PROPERTY )
169 result = Toolkit::TextVisual::Property::POINT_SIZE;
171 else if( stringKey == MULTI_LINE_PROPERTY )
173 result = Toolkit::TextVisual::Property::MULTI_LINE;
175 else if( stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
177 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
179 else if( stringKey == VERTICAL_ALIGNMENT_PROPERTY )
181 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
183 else if( stringKey == TEXT_COLOR_PROPERTY )
185 result = Toolkit::TextVisual::Property::TEXT_COLOR;
187 else if( stringKey == ENABLE_MARKUP_PROPERTY )
189 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
195 } // unnamed namespace
197 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
199 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
200 TextVisualPtr->SetProperties( properties );
201 return TextVisualPtr;
204 void TextVisual::ConvertStringKeysToIndexKeys( Property::Map& propertyMap )
206 Property::Map outMap;
208 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
210 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
212 Property::Index indexKey = keyValue.first.indexKey;
214 if ( keyValue.first.type == Property::Key::STRING )
216 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
219 outMap.Insert( indexKey, keyValue.second );
222 propertyMap = outMap;
225 float TextVisual::GetHeightForWidth( float width )
227 return mController->GetHeightForWidth( width );
230 void TextVisual::GetNaturalSize( Vector2& naturalSize )
232 naturalSize = mController->GetNaturalSize().GetVectorXY();
235 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
237 Property::Value value;
240 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
243 mController->GetText( text );
244 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
246 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
248 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
249 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
251 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ) );
253 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
255 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
257 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
259 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
261 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
264 void TextVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
267 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
269 mController->GetText( text );
270 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
274 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
275 : Visual::Base( factoryCache ),
276 mController( Text::Controller::New() ),
277 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) )
281 TextVisual::~TextVisual()
285 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
287 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
289 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
291 Property::Index indexKey = keyValue.first.indexKey;
293 if( keyValue.first.type == Property::Key::STRING )
295 indexKey = StringKeyToIndexKey( keyValue.first.stringKey );
298 DoSetProperty( indexKey, keyValue.second );
301 // Elide the text if it exceeds the boundaries.
302 mController->SetTextElideEnabled( true );
304 // Retrieve the layout engine to set the cursor's width.
305 Text::Layout::Engine& engine = mController->GetLayoutEngine();
307 // Sets 0 as cursor's width.
308 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
311 void TextVisual::DoSetOnStage( Actor& actor )
315 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
317 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::TEXT_SHADER );
320 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
321 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
323 mFactoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER, shader );
326 mImpl->mRenderer = Renderer::New( geometry, shader );
327 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT );
329 UpdateRenderer( true ); // Renderer needs textures and to be added to control
332 void TextVisual::DoSetOffStage( Actor& actor )
334 if( mImpl->mRenderer )
336 // Removes the renderer from the actor.
337 actor.RemoveRenderer( mImpl->mRenderer );
341 // Resets the renderer.
342 mImpl->mRenderer.Reset();
345 // Resets the control handle.
349 void TextVisual::OnSetTransform()
351 UpdateRenderer( false );
354 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
358 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
360 const bool enableMarkup = propertyValue.Get<bool>();
361 mController->SetMarkupProcessorEnabled( enableMarkup );
364 case Toolkit::TextVisual::Property::TEXT:
366 mController->SetText( propertyValue.Get<std::string>() );
369 case Toolkit::TextVisual::Property::FONT_FAMILY:
371 SetFontFamilyProperty( mController, propertyValue );
374 case Toolkit::TextVisual::Property::FONT_STYLE:
376 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
379 case Toolkit::TextVisual::Property::POINT_SIZE:
381 const float pointSize = propertyValue.Get<float>();
382 if( !Equals( mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
384 mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
388 case Toolkit::TextVisual::Property::MULTI_LINE:
390 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
393 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
395 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
396 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
397 HORIZONTAL_ALIGNMENT_STRING_TABLE,
398 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
401 mController->SetHorizontalAlignment( alignment );
405 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
407 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
408 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
409 VERTICAL_ALIGNMENT_STRING_TABLE,
410 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
413 mController->SetVerticalAlignment( alignment );
417 case Toolkit::TextVisual::Property::TEXT_COLOR:
419 const Vector4& textColor = propertyValue.Get< Vector4 >();
420 if( mController->GetDefaultColor() != textColor )
422 mController->SetDefaultColor( textColor );
429 void TextVisual::UpdateRenderer( bool initializeRendererAndTexture )
431 Actor control = mControl.GetHandle();
438 // Calculates the size to be used to relayout.
439 Vector2 relayoutSize;
441 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
442 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
444 // Round the size and offset to avoid pixel alignement issues.
445 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
446 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
448 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
450 // Removes the texture set.
453 // Remove any renderer previously set.
454 if( mImpl->mRenderer )
456 control.RemoveRenderer( mImpl->mRenderer );
459 // Nothing else to do if the relayout size is zero.
463 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
465 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) || initializeRendererAndTexture )
467 // Removes the texture set.
470 // Remove any renderer previously set.
471 if( mImpl->mRenderer )
473 control.RemoveRenderer( mImpl->mRenderer );
476 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
477 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
479 PixelData data = mTypesetter->Render( relayoutSize );
481 Vector4 atlasRect = FULL_TEXTURE_RECT;
483 // Texture set not retrieved from Atlas Manager whilst pixel offset visible.
485 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
486 // In that case, create a texture. TODO: should tile the text.
488 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
489 data.GetPixelFormat(),
493 texture.Upload( data );
495 TextureSet textureSet = TextureSet::New();
496 textureSet.SetTexture( 0u, texture );
498 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
500 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
502 //Register transform properties
503 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
505 // Filter mode needs to be set to nearest to avoid blurry text.
506 Sampler sampler = Sampler::New();
507 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
508 textureSet.SetSampler( 0u, sampler );
510 mImpl->mRenderer.SetTextures( textureSet );
512 control.AddRenderer( mImpl->mRenderer );
514 // Text rendered and ready to display
520 void TextVisual::RemoveTextureSet()
522 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
524 // Removes the text's image from the texture atlas.
527 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
528 if( index != Property::INVALID_INDEX )
530 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
531 atlasRectValue.Get( atlasRect );
533 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
534 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
539 } // namespace Internal
541 } // namespace Toolkit