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/visual-factory/devel-visual-properties.h>
23 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
24 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
25 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
26 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
27 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
28 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
30 #include <dali-toolkit/internal/text/text-font-style.h>
45 const char * const TEXT_PROPERTY( "text" );
46 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
47 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
48 const char * const POINT_SIZE_PROPERTY( "pointSize" );
49 const char * const MULTI_LINE_PROPERTY( "multiLine" );
50 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
51 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
52 const char * const TEXT_COLOR_PROPERTY( "textColor" );
53 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
55 const std::string PIXEL_AREA_UNIFORM_NAME = "pixelArea";
57 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
59 { "BEGIN", Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN },
60 { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
61 { "END", Toolkit::Text::Layout::HORIZONTAL_ALIGN_END },
63 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
65 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
67 { "TOP", Toolkit::Text::Layout::VERTICAL_ALIGN_TOP },
68 { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
69 { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
71 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
73 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
75 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
77 const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
78 HORIZONTAL_ALIGNMENT_STRING_TABLE,
79 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
81 return std::string( name );
84 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
86 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
87 VERTICAL_ALIGNMENT_STRING_TABLE,
88 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
90 return std::string( name );
93 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
94 attribute mediump vec2 aPosition;\n
95 uniform mediump mat4 uMvpMatrix;\n
96 uniform mediump vec3 uSize;\n
97 uniform mediump vec4 pixelArea;
98 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 vertexPosition = uMvpMatrix *ComputeVertexPosition();\n
118 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
119 gl_Position = vertexPosition;\n
123 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
124 varying mediump vec2 vTexCoord;\n
125 uniform sampler2D sTexture;\n
126 uniform mediump vec4 uAtlasRect;\n
127 uniform lowp vec4 uColor;\n
131 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
132 gl_FragColor = texture2D( sTexture, texCoord ) * uColor;\n
136 } // unnamed namespace
138 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache )
140 return new TextVisual( factoryCache );
143 float TextVisual::GetHeightForWidth( float width ) const
145 return mController->GetHeightForWidth( width );
148 void TextVisual::GetNaturalSize( Vector2& naturalSize )
150 naturalSize = mController->GetNaturalSize().GetVectorXY();
153 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
155 Property::Value value;
158 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::DevelVisual::TEXT );
161 mController->GetText( text );
162 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
164 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
166 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
167 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
169 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultPointSize() );
171 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
173 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
175 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
177 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
179 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
182 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
183 : Visual::Base( factoryCache ),
184 mController( Text::Controller::New() ),
185 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) )
189 TextVisual::~TextVisual()
193 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
195 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
197 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
199 switch( keyValue.first.type )
201 case Property::Key::INDEX:
203 if( Toolkit::VisualProperty::TYPE != keyValue.first.indexKey ) // Toolkit::VisualProperty::TYPE is not a TextVisual's property.
205 DoSetProperty( keyValue.first.indexKey, keyValue.second );
209 case Property::Key::STRING:
211 if( keyValue.first.stringKey == TEXT_PROPERTY )
213 DoSetProperty( Toolkit::TextVisual::Property::TEXT, keyValue.second );
215 else if( keyValue.first.stringKey == FONT_FAMILY_PROPERTY )
217 DoSetProperty( Toolkit::TextVisual::Property::FONT_FAMILY, keyValue.second );
219 else if( keyValue.first.stringKey == FONT_STYLE_PROPERTY )
221 DoSetProperty( Toolkit::TextVisual::Property::FONT_STYLE, keyValue.second );
223 else if( keyValue.first.stringKey == POINT_SIZE_PROPERTY )
225 DoSetProperty( Toolkit::TextVisual::Property::POINT_SIZE, keyValue.second );
227 else if( keyValue.first.stringKey == MULTI_LINE_PROPERTY )
229 DoSetProperty( Toolkit::TextVisual::Property::MULTI_LINE, keyValue.second );
231 else if( keyValue.first.stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
233 DoSetProperty( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, keyValue.second );
235 else if( keyValue.first.stringKey == VERTICAL_ALIGNMENT_PROPERTY )
237 DoSetProperty( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, keyValue.second );
239 else if( keyValue.first.stringKey == TEXT_COLOR_PROPERTY )
241 DoSetProperty( Toolkit::TextVisual::Property::TEXT_COLOR, keyValue.second );
243 else if( keyValue.first.stringKey == ENABLE_MARKUP_PROPERTY )
245 DoSetProperty( Toolkit::TextVisual::Property::ENABLE_MARKUP, keyValue.second );
252 // Elide the text if it exceeds the boundaries.
253 mController->SetTextElideEnabled( true );
255 // Retrieve the layout engine to set the cursor's width.
256 Text::Layout::Engine& engine = mController->GetLayoutEngine();
258 // Sets 0 as cursor's width.
259 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
262 void TextVisual::DoSetOnStage( Actor& actor )
269 void TextVisual::DoSetOffStage( Actor& actor )
271 if( mImpl->mRenderer )
273 // Removes the renderer from the actor.
274 actor.RemoveRenderer( mImpl->mRenderer );
279 // Resets the control handle.
283 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
287 case Toolkit::TextVisual::Property::TEXT:
289 mController->SetText( propertyValue.Get<std::string>() );
292 case Toolkit::TextVisual::Property::FONT_FAMILY:
294 SetFontFamilyProperty( mController, propertyValue );
297 case Toolkit::TextVisual::Property::FONT_STYLE:
299 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
302 case Toolkit::TextVisual::Property::POINT_SIZE:
304 const float pointSize = propertyValue.Get<float>();
306 if( !Equals( mController->GetDefaultPointSize(), pointSize ) )
308 mController->SetDefaultPointSize( pointSize );
312 case Toolkit::TextVisual::Property::MULTI_LINE:
314 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
317 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
319 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
320 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
321 HORIZONTAL_ALIGNMENT_STRING_TABLE,
322 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
325 mController->SetHorizontalAlignment( alignment );
329 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
331 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
332 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
333 VERTICAL_ALIGNMENT_STRING_TABLE,
334 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
337 mController->SetVerticalAlignment( alignment );
341 case Toolkit::TextVisual::Property::TEXT_COLOR:
343 const Vector4& textColor = propertyValue.Get< Vector4 >();
344 if( mController->GetDefaultColor() != textColor )
346 mController->SetDefaultColor( textColor );
350 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
352 const bool enableMarkup = propertyValue.Get<bool>();
353 mController->SetMarkupProcessorEnabled( enableMarkup );
358 // Should not arrive here.
359 DALI_ASSERT_DEBUG( false );
364 Dali::Property::Value TextVisual::DoGetProperty( Dali::Property::Index index )
366 Dali::Property::Value value;
370 case Toolkit::TextVisual::Property::TEXT:
373 mController->GetText( text );
377 case Toolkit::TextVisual::Property::FONT_FAMILY:
379 value = mController->GetDefaultFontFamily();
382 case Toolkit::TextVisual::Property::FONT_STYLE:
384 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
387 case Toolkit::TextVisual::Property::POINT_SIZE:
389 value = mController->GetDefaultPointSize();
392 case Toolkit::TextVisual::Property::MULTI_LINE:
394 value = mController->IsMultiLineEnabled();
397 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
399 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::HorizontalAlignment >( mController->GetHorizontalAlignment(),
400 HORIZONTAL_ALIGNMENT_STRING_TABLE,
401 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
404 value = std::string( name );
408 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
410 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( mController->GetVerticalAlignment(),
411 VERTICAL_ALIGNMENT_STRING_TABLE,
412 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
415 value = std::string( name );
419 case Toolkit::TextVisual::Property::TEXT_COLOR:
421 value = mController->GetDefaultColor();
424 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
426 value = mController->IsMarkupProcessorEnabled();
431 // Should not arrive here.
432 DALI_ASSERT_DEBUG( false );
439 void TextVisual::OnSetTransform()
444 void TextVisual::CreateRenderer()
446 Actor control = mControl.GetHandle();
453 // Calculates the size to be used to relayout.
454 Vector2 relayoutSize;
456 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
457 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
459 // Round the size and offset to avoid pixel alignement issues.
460 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
461 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
463 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
465 // Remove any renderer previously set.
466 if( mImpl->mRenderer )
468 control.RemoveRenderer( mImpl->mRenderer );
473 // Nothing else to do if the relayout size is zero.
477 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
479 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
481 // Remove any renderer previously set.
482 if( mImpl->mRenderer )
484 control.RemoveRenderer( mImpl->mRenderer );
489 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
490 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
492 PixelData data = mTypesetter->Render( relayoutSize );
496 TextureSet textureSet;
500 textureSet = mFactoryCache.GetAtlasManager()->Add( atlasRect, data );
501 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
503 // Filter mode needs to be set to nearest to avoid blurry text.
504 Sampler sampler = Sampler::New();
505 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
506 textureSet.SetSampler( 0u, sampler );
508 geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
511 geometry = VisualFactoryCache::CreateQuadGeometry();
512 mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY , geometry );
515 shader = mFactoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
518 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
519 mFactoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
521 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
523 mImpl->mRenderer = Renderer::New( geometry, shader );
524 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::TEXT );
525 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
527 mImpl->mRenderer.SetTextures( textureSet );
529 control.AddRenderer( mImpl->mRenderer );
531 //Register transform properties
532 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
534 mImpl->mFlags |= Impl::IS_FROM_CACHE;
539 void TextVisual::DestroyRenderer()
541 // Removes the text's image from the texture atlas.
544 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
545 if( index != Property::INVALID_INDEX )
547 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
548 atlasRectValue.Get( atlasRect );
550 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
551 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
554 // Resets the renderer.
555 mImpl->mRenderer.Reset();
558 } // namespace Internal
560 } // namespace Toolkit