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 std::string PIXEL_AREA_UNIFORM_NAME = "pixelArea";
55 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
57 { "BEGIN", Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN },
58 { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
59 { "END", Toolkit::Text::Layout::HORIZONTAL_ALIGN_END },
61 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
63 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
65 { "TOP", Toolkit::Text::Layout::VERTICAL_ALIGN_TOP },
66 { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
67 { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
69 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
71 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
73 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
75 const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
76 HORIZONTAL_ALIGNMENT_STRING_TABLE,
77 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
79 return std::string( name );
82 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
84 const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
85 VERTICAL_ALIGNMENT_STRING_TABLE,
86 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
88 return std::string( name );
91 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
92 attribute mediump vec2 aPosition;\n
93 uniform mediump mat4 uMvpMatrix;\n
94 uniform mediump vec3 uSize;\n
95 uniform mediump vec4 pixelArea;
97 uniform mediump mat4 uModelMatrix;\n
98 uniform mediump mat4 uViewMatrix;\n
99 uniform mediump mat4 uProjection;\n
101 varying mediump vec2 vTexCoord;\n
103 //Visual size and offset
104 uniform mediump vec2 offset;\n
105 uniform mediump vec2 size;\n
106 uniform mediump vec4 offsetSizeMode;\n
107 uniform mediump vec2 origin;\n
108 uniform mediump vec2 anchorPoint;\n
110 vec4 ComputeVertexPosition()\n
112 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
113 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
114 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
119 mediump vec4 nonAlignedVertex = uViewMatrix*uModelMatrix*ComputeVertexPosition();\n
120 mediump vec4 pixelAlignedVertex = vec4 ( floor(nonAlignedVertex.xyz), 1.0 );\n
121 mediump vec4 vertexPosition = uProjection*pixelAlignedVertex;\n
123 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
124 gl_Position = vertexPosition;\n
128 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
129 varying mediump vec2 vTexCoord;\n
130 uniform sampler2D sTexture;\n
131 uniform mediump vec4 uAtlasRect;\n
132 uniform lowp vec4 uColor;\n
133 uniform lowp vec4 mixColor;\n
137 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
138 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * mixColor;\n
142 } // unnamed namespace
144 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
146 TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
147 TextVisualPtr->SetProperties( properties );
148 return TextVisualPtr;
151 float TextVisual::GetHeightForWidth( float width )
153 return mController->GetHeightForWidth( width );
156 void TextVisual::GetNaturalSize( Vector2& naturalSize )
158 naturalSize = mController->GetNaturalSize().GetVectorXY();
161 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
163 Property::Value value;
166 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
169 mController->GetText( text );
170 map.Insert( Toolkit::TextVisual::Property::TEXT, text );
172 map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
174 GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
175 map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
177 map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultPointSize() );
179 map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
181 map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
183 map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
185 map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
187 map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
190 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
191 : Visual::Base( factoryCache ),
192 mController( Text::Controller::New() ),
193 mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) )
197 TextVisual::~TextVisual()
201 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
203 for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
205 const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
207 switch( keyValue.first.type )
209 case Property::Key::INDEX:
211 DoSetProperty( keyValue.first.indexKey, keyValue.second );
214 case Property::Key::STRING:
216 if( keyValue.first.stringKey == TEXT_PROPERTY )
218 DoSetProperty( Toolkit::TextVisual::Property::TEXT, keyValue.second );
220 else if( keyValue.first.stringKey == FONT_FAMILY_PROPERTY )
222 DoSetProperty( Toolkit::TextVisual::Property::FONT_FAMILY, keyValue.second );
224 else if( keyValue.first.stringKey == FONT_STYLE_PROPERTY )
226 DoSetProperty( Toolkit::TextVisual::Property::FONT_STYLE, keyValue.second );
228 else if( keyValue.first.stringKey == POINT_SIZE_PROPERTY )
230 DoSetProperty( Toolkit::TextVisual::Property::POINT_SIZE, keyValue.second );
232 else if( keyValue.first.stringKey == MULTI_LINE_PROPERTY )
234 DoSetProperty( Toolkit::TextVisual::Property::MULTI_LINE, keyValue.second );
236 else if( keyValue.first.stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
238 DoSetProperty( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, keyValue.second );
240 else if( keyValue.first.stringKey == VERTICAL_ALIGNMENT_PROPERTY )
242 DoSetProperty( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, keyValue.second );
244 else if( keyValue.first.stringKey == TEXT_COLOR_PROPERTY )
246 DoSetProperty( Toolkit::TextVisual::Property::TEXT_COLOR, keyValue.second );
248 else if( keyValue.first.stringKey == ENABLE_MARKUP_PROPERTY )
250 DoSetProperty( Toolkit::TextVisual::Property::ENABLE_MARKUP, keyValue.second );
257 // Elide the text if it exceeds the boundaries.
258 mController->SetTextElideEnabled( true );
260 // Retrieve the layout engine to set the cursor's width.
261 Text::Layout::Engine& engine = mController->GetLayoutEngine();
263 // Sets 0 as cursor's width.
264 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
267 void TextVisual::DoSetOnStage( Actor& actor )
271 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
273 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::TEXT_SHADER );
276 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
277 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
279 mFactoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER, shader );
282 mImpl->mRenderer = Renderer::New( geometry, shader );
283 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::TEXT );
288 void TextVisual::DoSetOffStage( Actor& actor )
290 if( mImpl->mRenderer )
292 // Removes the renderer from the actor.
293 actor.RemoveRenderer( mImpl->mRenderer );
297 // Resets the renderer.
298 mImpl->mRenderer.Reset();
301 // Resets the control handle.
305 void TextVisual::OnSetTransform()
310 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
314 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
316 const bool enableMarkup = propertyValue.Get<bool>();
317 mController->SetMarkupProcessorEnabled( enableMarkup );
320 case Toolkit::TextVisual::Property::TEXT:
322 mController->SetText( propertyValue.Get<std::string>() );
325 case Toolkit::TextVisual::Property::FONT_FAMILY:
327 SetFontFamilyProperty( mController, propertyValue );
330 case Toolkit::TextVisual::Property::FONT_STYLE:
332 SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
335 case Toolkit::TextVisual::Property::POINT_SIZE:
337 const float pointSize = propertyValue.Get<float>();
338 if( !Equals( mController->GetDefaultPointSize(), pointSize ) )
340 mController->SetDefaultPointSize( pointSize );
344 case Toolkit::TextVisual::Property::MULTI_LINE:
346 mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
349 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
351 Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
352 if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
353 HORIZONTAL_ALIGNMENT_STRING_TABLE,
354 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
357 mController->SetHorizontalAlignment( alignment );
361 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
363 Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
364 if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
365 VERTICAL_ALIGNMENT_STRING_TABLE,
366 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
369 mController->SetVerticalAlignment( alignment );
373 case Toolkit::TextVisual::Property::TEXT_COLOR:
375 const Vector4& textColor = propertyValue.Get< Vector4 >();
376 if( mController->GetDefaultColor() != textColor )
378 mController->SetDefaultColor( textColor );
385 void TextVisual::UpdateRenderer()
387 Actor control = mControl.GetHandle();
394 // Calculates the size to be used to relayout.
395 Vector2 relayoutSize;
397 const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
398 const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
400 // Round the size and offset to avoid pixel alignement issues.
401 relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
402 relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
404 if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
406 // Removes the texture set.
409 // Remove any renderer previously set.
410 if( mImpl->mRenderer )
412 control.RemoveRenderer( mImpl->mRenderer );
415 // Nothing else to do if the relayout size is zero.
419 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
421 if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
423 // Removes the texture set.
426 // Remove any renderer previously set.
427 if( mImpl->mRenderer )
429 control.RemoveRenderer( mImpl->mRenderer );
432 if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
433 ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
435 PixelData data = mTypesetter->Render( relayoutSize );
437 Vector4 atlasRect = FULL_TEXTURE_RECT;
439 // Texture set not retrieved from Atlas Manager whilst pixel offset visible.
441 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
442 // In that case, create a texture. TODO: should tile the text.
444 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
445 data.GetPixelFormat(),
449 texture.Upload( data );
451 TextureSet textureSet = TextureSet::New();
452 textureSet.SetTexture( 0u, texture );
454 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
456 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
458 //Register transform properties
459 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
461 // Filter mode needs to be set to nearest to avoid blurry text.
462 Sampler sampler = Sampler::New();
463 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
464 textureSet.SetSampler( 0u, sampler );
466 mImpl->mRenderer.SetTextures( textureSet );
468 control.AddRenderer( mImpl->mRenderer );
473 void TextVisual::RemoveTextureSet()
475 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
477 // Removes the text's image from the texture atlas.
480 const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
481 if( index != Property::INVALID_INDEX )
483 const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
484 atlasRectValue.Get( atlasRect );
486 const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
487 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
492 } // namespace Internal
494 } // namespace Toolkit