TextVisual shows text after being Disabled and Enabled
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/text/text-visual.h>
20
21 // INTERNAL HEADER
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>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
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" );
52
53 const std::string PIXEL_AREA_UNIFORM_NAME = "pixelArea";
54
55 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
56 {
57   { "BEGIN",  Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN  },
58   { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
59   { "END",    Toolkit::Text::Layout::HORIZONTAL_ALIGN_END    },
60 };
61 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
62
63 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
64 {
65   { "TOP",    Toolkit::Text::Layout::VERTICAL_ALIGN_TOP    },
66   { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
67   { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
68 };
69 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
70
71 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
72
73 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
74 {
75   const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
76                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE,
77                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
78
79   return std::string( name );
80 }
81
82 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
83 {
84   const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
85                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE,
86                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
87
88   return std::string( name );
89 }
90
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;
96
97   uniform mediump mat4 uModelMatrix;\n
98   uniform mediump mat4 uViewMatrix;\n
99   uniform mediump mat4 uProjection;\n
100
101   varying mediump vec2 vTexCoord;\n
102   \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
109
110   vec4 ComputeVertexPosition()\n
111   {\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
115   }\n
116
117   void main()\n
118   {\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
122
123     vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
124     gl_Position = vertexPosition;\n
125   }\n
126 );
127
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
134     \n
135     void main()\n
136     {\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
139     }\n
140 );
141
142 } // unnamed namespace
143
144 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
145 {
146   TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
147   TextVisualPtr->SetProperties( properties );
148   return TextVisualPtr;
149 }
150
151 float TextVisual::GetHeightForWidth( float width )
152 {
153   return mController->GetHeightForWidth( width );
154 }
155
156 void TextVisual::GetNaturalSize( Vector2& naturalSize )
157 {
158   naturalSize = mController->GetNaturalSize().GetVectorXY();
159 }
160
161 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
162 {
163   Property::Value value;
164
165   map.Clear();
166   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
167
168   std::string text;
169   mController->GetText( text );
170   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
171
172   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
173
174   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
175   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
176
177   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultPointSize() );
178
179   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
180
181   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
182
183   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
184
185   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
186
187   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
188 }
189
190 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
191 : Visual::Base( factoryCache ),
192   mController( Text::Controller::New() ),
193   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) )
194 {
195 }
196
197 TextVisual::~TextVisual()
198 {
199 }
200
201 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
202 {
203   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
204   {
205     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
206
207     switch( keyValue.first.type )
208     {
209       case Property::Key::INDEX:
210       {
211         DoSetProperty( keyValue.first.indexKey, keyValue.second );
212         break;
213       }
214       case Property::Key::STRING:
215       {
216         if( keyValue.first.stringKey == TEXT_PROPERTY )
217         {
218           DoSetProperty( Toolkit::TextVisual::Property::TEXT, keyValue.second );
219         }
220         else if( keyValue.first.stringKey == FONT_FAMILY_PROPERTY )
221         {
222           DoSetProperty( Toolkit::TextVisual::Property::FONT_FAMILY, keyValue.second );
223         }
224         else if( keyValue.first.stringKey == FONT_STYLE_PROPERTY )
225         {
226           DoSetProperty( Toolkit::TextVisual::Property::FONT_STYLE, keyValue.second );
227         }
228         else if( keyValue.first.stringKey == POINT_SIZE_PROPERTY )
229         {
230           DoSetProperty( Toolkit::TextVisual::Property::POINT_SIZE, keyValue.second );
231         }
232         else if( keyValue.first.stringKey == MULTI_LINE_PROPERTY )
233         {
234           DoSetProperty( Toolkit::TextVisual::Property::MULTI_LINE, keyValue.second );
235         }
236         else if( keyValue.first.stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
237         {
238           DoSetProperty( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, keyValue.second );
239         }
240         else if( keyValue.first.stringKey == VERTICAL_ALIGNMENT_PROPERTY )
241         {
242           DoSetProperty( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, keyValue.second );
243         }
244         else if( keyValue.first.stringKey == TEXT_COLOR_PROPERTY )
245         {
246           DoSetProperty( Toolkit::TextVisual::Property::TEXT_COLOR, keyValue.second );
247         }
248         else if( keyValue.first.stringKey == ENABLE_MARKUP_PROPERTY )
249         {
250           DoSetProperty( Toolkit::TextVisual::Property::ENABLE_MARKUP, keyValue.second );
251         }
252         break;
253       }
254     }
255   }
256
257   // Elide the text if it exceeds the boundaries.
258   mController->SetTextElideEnabled( true );
259
260   // Retrieve the layout engine to set the cursor's width.
261   Text::Layout::Engine& engine = mController->GetLayoutEngine();
262
263   // Sets 0 as cursor's width.
264   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
265 }
266
267 void TextVisual::DoSetOnStage( Actor& actor )
268 {
269   mControl = actor;
270
271   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
272
273   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::TEXT_SHADER );
274   if( ! shader )
275   {
276     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
277     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
278
279     mFactoryCache.SaveShader( VisualFactoryCache::TEXT_SHADER, shader );
280   }
281
282   mImpl->mRenderer = Renderer::New( geometry, shader );
283   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::TEXT );
284
285   UpdateRenderer( true ); // Renderer needs textures and to be added to control
286 }
287
288 void TextVisual::DoSetOffStage( Actor& actor )
289 {
290   if( mImpl->mRenderer )
291   {
292     // Removes the renderer from the actor.
293     actor.RemoveRenderer( mImpl->mRenderer );
294
295     RemoveTextureSet();
296
297     // Resets the renderer.
298     mImpl->mRenderer.Reset();
299   }
300
301   // Resets the control handle.
302   mControl.Reset();
303 }
304
305 void TextVisual::OnSetTransform()
306 {
307   UpdateRenderer( false );
308 }
309
310 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
311 {
312   switch( index )
313   {
314     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
315     {
316       const bool enableMarkup = propertyValue.Get<bool>();
317       mController->SetMarkupProcessorEnabled( enableMarkup );
318       break;
319     }
320     case Toolkit::TextVisual::Property::TEXT:
321     {
322       mController->SetText( propertyValue.Get<std::string>() );
323       break;
324     }
325     case Toolkit::TextVisual::Property::FONT_FAMILY:
326     {
327       SetFontFamilyProperty( mController, propertyValue );
328       break;
329     }
330     case Toolkit::TextVisual::Property::FONT_STYLE:
331     {
332       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
333       break;
334     }
335     case Toolkit::TextVisual::Property::POINT_SIZE:
336     {
337       const float pointSize = propertyValue.Get<float>();
338       if( !Equals( mController->GetDefaultPointSize(), pointSize ) )
339       {
340         mController->SetDefaultPointSize( pointSize );
341       }
342       break;
343     }
344     case Toolkit::TextVisual::Property::MULTI_LINE:
345     {
346       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
347       break;
348     }
349     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
350     {
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,
355                                                                                    alignment ) )
356       {
357         mController->SetHorizontalAlignment( alignment );
358       }
359       break;
360     }
361     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
362     {
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,
367                                                                                  alignment ) )
368       {
369         mController->SetVerticalAlignment( alignment );
370       }
371       break;
372     }
373     case Toolkit::TextVisual::Property::TEXT_COLOR:
374     {
375       const Vector4& textColor = propertyValue.Get< Vector4 >();
376       if( mController->GetDefaultColor() != textColor )
377       {
378         mController->SetDefaultColor( textColor );
379       }
380       break;
381     }
382   }
383 }
384
385 void TextVisual::UpdateRenderer( bool initializeRendererAndTexture )
386 {
387   Actor control = mControl.GetHandle();
388   if( !control )
389   {
390     // Nothing to do.
391     return;
392   }
393
394   // Calculates the size to be used to relayout.
395   Vector2 relayoutSize;
396
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;
399
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 ) );
403
404   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
405   {
406     // Removes the texture set.
407     RemoveTextureSet();
408
409     // Remove any renderer previously set.
410     if( mImpl->mRenderer )
411     {
412       control.RemoveRenderer( mImpl->mRenderer );
413     }
414
415     // Nothing else to do if the relayout size is zero.
416     return;
417   }
418
419   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
420
421   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) || initializeRendererAndTexture )
422   {
423     // Removes the texture set.
424     RemoveTextureSet();
425
426     // Remove any renderer previously set.
427     if( mImpl->mRenderer )
428     {
429       control.RemoveRenderer( mImpl->mRenderer );
430     }
431
432     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
433         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
434     {
435       PixelData data = mTypesetter->Render( relayoutSize );
436
437       Vector4 atlasRect = FULL_TEXTURE_RECT;
438
439       // Texture set not retrieved from Atlas Manager whilst pixel offset visible.
440
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.
443
444       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
445                                       data.GetPixelFormat(),
446                                       data.GetWidth(),
447                                       data.GetHeight() );
448
449       texture.Upload( data );
450
451       TextureSet textureSet = TextureSet::New();
452       textureSet.SetTexture( 0u, texture );
453
454       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
455
456       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
457
458       //Register transform properties
459       mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
460
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 );
465
466       mImpl->mRenderer.SetTextures( textureSet );
467
468       control.AddRenderer( mImpl->mRenderer );
469     }
470   }
471 }
472
473 void TextVisual::RemoveTextureSet()
474 {
475   if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
476   {
477     // Removes the text's image from the texture atlas.
478     Vector4 atlasRect;
479
480     const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
481     if( index != Property::INVALID_INDEX )
482     {
483       const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
484       atlasRectValue.Get( atlasRect );
485
486       const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
487       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
488     }
489   }
490 }
491
492 } // namespace Internal
493
494 } // namespace Toolkit
495
496 } // namespace Dali