Visuals - Avoid having 2 devel headers for visual-properties.h
[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.
44 const char * const TEXT_PROPERTY( "text" );
45 const char * const FONT_FAMILY_PROPERTY( "fontFamily" );
46 const char * const FONT_STYLE_PROPERTY( "fontStyle" );
47 const char * const POINT_SIZE_PROPERTY( "pointSize" );
48 const char * const MULTI_LINE_PROPERTY( "multiLine" );
49 const char * const HORIZONTAL_ALIGNMENT_PROPERTY( "horizontalAlignment" );
50 const char * const VERTICAL_ALIGNMENT_PROPERTY( "verticalAlignment" );
51 const char * const TEXT_COLOR_PROPERTY( "textColor" );
52 const char * const ENABLE_MARKUP_PROPERTY( "enableMarkup" );
53
54 const std::string PIXEL_AREA_UNIFORM_NAME = "pixelArea";
55
56 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
57 {
58   { "BEGIN",  Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN  },
59   { "CENTER", Toolkit::Text::Layout::HORIZONTAL_ALIGN_CENTER },
60   { "END",    Toolkit::Text::Layout::HORIZONTAL_ALIGN_END    },
61 };
62 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
63
64 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
65 {
66   { "TOP",    Toolkit::Text::Layout::VERTICAL_ALIGN_TOP    },
67   { "CENTER", Toolkit::Text::Layout::VERTICAL_ALIGN_CENTER },
68   { "BOTTOM", Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM },
69 };
70 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
71
72 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
73
74 std::string GetHorizontalAlignment( Toolkit::Text::Layout::HorizontalAlignment alignment )
75 {
76   const char* name = Scripting::GetEnumerationName<Toolkit::Text::Layout::HorizontalAlignment>( alignment,
77                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE,
78                                                                                                 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
79
80   return std::string( name );
81 }
82
83 std::string GetVerticalAlignment( Toolkit::Text::Layout::VerticalAlignment alignment )
84 {
85   const char* name = Scripting::GetEnumerationName< Toolkit::Text::Layout::VerticalAlignment >( alignment,
86                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE,
87                                                                                                 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
88
89   return std::string( name );
90 }
91
92 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
93   attribute mediump vec2 aPosition;\n
94   uniform mediump mat4 uMvpMatrix;\n
95   uniform mediump vec3 uSize;\n
96   uniform mediump vec4 pixelArea;
97   varying mediump vec2 vTexCoord;\n
98   \n
99
100   //Visual size and offset
101   uniform mediump vec2 offset;\n
102   uniform mediump vec2 size;\n
103   uniform mediump vec4 offsetSizeMode;\n
104   uniform mediump vec2 origin;\n
105   uniform mediump vec2 anchorPoint;\n
106
107   vec4 ComputeVertexPosition()\n
108   {\n
109     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
110     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
111     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
112   }\n
113
114   void main()\n
115   {\n
116     mediump vec4 vertexPosition = uMvpMatrix *ComputeVertexPosition();\n
117     vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
118     gl_Position = vertexPosition;\n
119   }\n
120 );
121
122 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
123     varying mediump vec2 vTexCoord;\n
124     uniform sampler2D sTexture;\n
125     uniform mediump vec4 uAtlasRect;\n
126     uniform lowp vec4 uColor;\n
127     \n
128     void main()\n
129     {\n
130       mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
131       gl_FragColor = texture2D( sTexture, texCoord ) * uColor;\n
132     }\n
133 );
134
135 } // unnamed namespace
136
137 TextVisualPtr TextVisual::New( VisualFactoryCache& factoryCache )
138 {
139   return new TextVisual( factoryCache );
140 }
141
142 float TextVisual::GetHeightForWidth( float width ) const
143 {
144   return mController->GetHeightForWidth( width );
145 }
146
147 void TextVisual::GetNaturalSize( Vector2& naturalSize )
148 {
149   naturalSize = mController->GetNaturalSize().GetVectorXY();
150 }
151
152 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
153 {
154   Property::Value value;
155
156   map.Clear();
157   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
158
159   std::string text;
160   mController->GetText( text );
161   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
162
163   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
164
165   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
166   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
167
168   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultPointSize() );
169
170   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
171
172   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
173
174   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
175
176   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
177
178   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
179 }
180
181 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
182 : Visual::Base( factoryCache ),
183   mController( Text::Controller::New() ),
184   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) )
185 {
186 }
187
188 TextVisual::~TextVisual()
189 {
190 }
191
192 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
193 {
194   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
195   {
196     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
197
198     switch( keyValue.first.type )
199     {
200       case Property::Key::INDEX:
201       {
202         if( Toolkit::DevelVisual::Property::TYPE != keyValue.first.indexKey ) // Toolkit::DevelVisual::Property::TYPE is not a TextVisual's property.
203         {
204           SetProperty( keyValue.first.indexKey, keyValue.second );
205         }
206         break;
207       }
208       case Property::Key::STRING:
209       {
210         if( keyValue.first.stringKey == TEXT_PROPERTY )
211         {
212           SetProperty( Toolkit::TextVisual::Property::TEXT, keyValue.second );
213         }
214         else if( keyValue.first.stringKey == FONT_FAMILY_PROPERTY )
215         {
216           SetProperty( Toolkit::TextVisual::Property::FONT_FAMILY, keyValue.second );
217         }
218         else if( keyValue.first.stringKey == FONT_STYLE_PROPERTY )
219         {
220           SetProperty( Toolkit::TextVisual::Property::FONT_STYLE, keyValue.second );
221         }
222         else if( keyValue.first.stringKey == POINT_SIZE_PROPERTY )
223         {
224           SetProperty( Toolkit::TextVisual::Property::POINT_SIZE, keyValue.second );
225         }
226         else if( keyValue.first.stringKey == MULTI_LINE_PROPERTY )
227         {
228           SetProperty( Toolkit::TextVisual::Property::MULTI_LINE, keyValue.second );
229         }
230         else if( keyValue.first.stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
231         {
232           SetProperty( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, keyValue.second );
233         }
234         else if( keyValue.first.stringKey == VERTICAL_ALIGNMENT_PROPERTY )
235         {
236           SetProperty( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, keyValue.second );
237         }
238         else if( keyValue.first.stringKey == TEXT_COLOR_PROPERTY )
239         {
240           SetProperty( Toolkit::TextVisual::Property::TEXT_COLOR, keyValue.second );
241         }
242         else if( keyValue.first.stringKey == ENABLE_MARKUP_PROPERTY )
243         {
244           SetProperty( Toolkit::TextVisual::Property::ENABLE_MARKUP, keyValue.second );
245         }
246         break;
247       }
248     }
249   }
250
251   // Elide the text if it exceeds the boundaries.
252   mController->SetTextElideEnabled( true );
253
254   // Retrieve the layout engine to set the cursor's width.
255   Text::Layout::Engine& engine = mController->GetLayoutEngine();
256
257   // Sets 0 as cursor's width.
258   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
259 }
260
261 void TextVisual::DoSetOnStage( Actor& actor )
262 {
263   mControl = actor;
264
265   CreateRenderer();
266 }
267
268 void TextVisual::DoSetOffStage( Actor& actor )
269 {
270   if( mImpl->mRenderer )
271   {
272     // Removes the renderer from the actor.
273     actor.RemoveRenderer( mImpl->mRenderer );
274
275     DestroyRenderer();
276   }
277
278   // Resets the control handle.
279   mControl.Reset();
280 }
281
282 void TextVisual::SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
283 {
284   switch( index )
285   {
286     case Toolkit::TextVisual::Property::TEXT:
287     {
288       mController->SetText( propertyValue.Get<std::string>() );
289       break;
290     }
291     case Toolkit::TextVisual::Property::FONT_FAMILY:
292     {
293       SetFontFamilyProperty( mController, propertyValue );
294       break;
295     }
296     case Toolkit::TextVisual::Property::FONT_STYLE:
297     {
298       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
299       break;
300     }
301     case Toolkit::TextVisual::Property::POINT_SIZE:
302     {
303       const float pointSize = propertyValue.Get<float>();
304
305       if( !Equals( mController->GetDefaultPointSize(), pointSize ) )
306       {
307         mController->SetDefaultPointSize( pointSize );
308       }
309       break;
310     }
311     case Toolkit::TextVisual::Property::MULTI_LINE:
312     {
313       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
314       break;
315     }
316     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
317     {
318       Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
319       if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
320                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE,
321                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
322                                                                                    alignment ) )
323       {
324         mController->SetHorizontalAlignment( alignment );
325       }
326       break;
327     }
328     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
329     {
330       Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
331       if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
332                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE,
333                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
334                                                                                  alignment ) )
335       {
336         mController->SetVerticalAlignment( alignment );
337       }
338       break;
339     }
340     case Toolkit::TextVisual::Property::TEXT_COLOR:
341     {
342       const Vector4& textColor = propertyValue.Get< Vector4 >();
343       if( mController->GetDefaultColor() != textColor )
344       {
345         mController->SetDefaultColor( textColor );
346       }
347       break;
348     }
349     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
350     {
351       const bool enableMarkup = propertyValue.Get<bool>();
352       mController->SetMarkupProcessorEnabled( enableMarkup );
353       break;
354     }
355     default:
356     {
357       // Should not arrive here.
358       DALI_ASSERT_DEBUG( false );
359     }
360   }
361 }
362
363 void TextVisual::OnSetTransform()
364 {
365   CreateRenderer();
366 }
367
368 void TextVisual::CreateRenderer()
369 {
370   Actor control = mControl.GetHandle();
371   if( !control )
372   {
373     // Nothing to do.
374     return;
375   }
376
377   // Calculates the size to be used to relayout.
378   Vector2 relayoutSize;
379
380   const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
381   const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
382
383   // Round the size and offset to avoid pixel alignement issues.
384   relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
385   relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
386
387   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
388   {
389     // Remove any renderer previously set.
390     if( mImpl->mRenderer )
391     {
392       control.RemoveRenderer( mImpl->mRenderer );
393
394       DestroyRenderer();
395     }
396
397     // Nothing else to do if the relayout size is zero.
398     return;
399   }
400
401   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
402
403   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
404   {
405     // Remove any renderer previously set.
406     if( mImpl->mRenderer )
407     {
408       control.RemoveRenderer( mImpl->mRenderer );
409
410       DestroyRenderer();
411     }
412
413     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
414         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
415     {
416       PixelData data = mTypesetter->Render( relayoutSize );
417
418       Geometry geometry;
419       Shader shader;
420       TextureSet textureSet;
421
422       Vector4 atlasRect;
423
424       textureSet = mFactoryCache.GetAtlasManager()->Add( atlasRect, data );
425       mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
426
427       // Filter mode needs to be set to nearest to avoid blurry text.
428       Sampler sampler = Sampler::New();
429       sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
430       textureSet.SetSampler( 0u, sampler );
431
432       geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
433       if( !geometry )
434       {
435         geometry =  VisualFactoryCache::CreateQuadGeometry();
436         mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY , geometry );
437       }
438
439       shader = mFactoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
440       if( !shader )
441       {
442         shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
443         mFactoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
444       }
445       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
446
447       mImpl->mRenderer = Renderer::New( geometry, shader );
448       mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::TEXT );
449       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
450
451       mImpl->mRenderer.SetTextures( textureSet );
452
453       control.AddRenderer( mImpl->mRenderer );
454
455       //Register transform properties
456       mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
457
458       mImpl->mFlags |= Impl::IS_FROM_CACHE;
459     }
460   }
461 }
462
463 void TextVisual::DestroyRenderer()
464 {
465   // Removes the text's image from the texture atlas.
466   Vector4 atlasRect;
467
468   const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
469   if( index != Property::INVALID_INDEX )
470   {
471     const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
472     atlasRectValue.Get( atlasRect );
473
474     const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
475     mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
476   }
477
478   // Resets the renderer.
479   mImpl->mRenderer.Reset();
480 }
481
482 } // namespace Internal
483
484 } // namespace Toolkit
485
486 } // namespace Dali