Reducing 720 style sheets point size for buttons
[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, const Property::Map& properties )
138 {
139   TextVisualPtr TextVisualPtr( new TextVisual( factoryCache ) );
140   TextVisualPtr->SetProperties( properties );
141   return TextVisualPtr;
142 }
143
144 float TextVisual::GetHeightForWidth( float width )
145 {
146   return mController->GetHeightForWidth( width );
147 }
148
149 void TextVisual::GetNaturalSize( Vector2& naturalSize )
150 {
151   naturalSize = mController->GetNaturalSize().GetVectorXY();
152 }
153
154 void TextVisual::DoCreatePropertyMap( Property::Map& map ) const
155 {
156   Property::Value value;
157
158   map.Clear();
159   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::TEXT );
160
161   std::string text;
162   mController->GetText( text );
163   map.Insert( Toolkit::TextVisual::Property::TEXT, text );
164
165   map.Insert( Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily() );
166
167   GetFontStyleProperty( mController, value, Text::FontStyle::DEFAULT );
168   map.Insert( Toolkit::TextVisual::Property::FONT_STYLE, value );
169
170   map.Insert( Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultPointSize() );
171
172   map.Insert( Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled() );
173
174   map.Insert( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, GetHorizontalAlignment( mController->GetHorizontalAlignment() ) );
175
176   map.Insert( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, GetVerticalAlignment( mController->GetVerticalAlignment() ) );
177
178   map.Insert( Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor() );
179
180   map.Insert( Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled() );
181 }
182
183 TextVisual::TextVisual( VisualFactoryCache& factoryCache )
184 : Visual::Base( factoryCache ),
185   mController( Text::Controller::New() ),
186   mTypesetter( Text::Typesetter::New( mController->GetTextModel() ) )
187 {
188 }
189
190 TextVisual::~TextVisual()
191 {
192 }
193
194 void TextVisual::DoSetProperties( const Property::Map& propertyMap )
195 {
196   for( Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index )
197   {
198     const KeyValuePair& keyValue = propertyMap.GetKeyValue( index );
199
200     switch( keyValue.first.type )
201     {
202       case Property::Key::INDEX:
203       {
204         DoSetProperty( keyValue.first.indexKey, keyValue.second );
205         break;
206       }
207       case Property::Key::STRING:
208       {
209         if( keyValue.first.stringKey == TEXT_PROPERTY )
210         {
211           DoSetProperty( Toolkit::TextVisual::Property::TEXT, keyValue.second );
212         }
213         else if( keyValue.first.stringKey == FONT_FAMILY_PROPERTY )
214         {
215           DoSetProperty( Toolkit::TextVisual::Property::FONT_FAMILY, keyValue.second );
216         }
217         else if( keyValue.first.stringKey == FONT_STYLE_PROPERTY )
218         {
219           DoSetProperty( Toolkit::TextVisual::Property::FONT_STYLE, keyValue.second );
220         }
221         else if( keyValue.first.stringKey == POINT_SIZE_PROPERTY )
222         {
223           DoSetProperty( Toolkit::TextVisual::Property::POINT_SIZE, keyValue.second );
224         }
225         else if( keyValue.first.stringKey == MULTI_LINE_PROPERTY )
226         {
227           DoSetProperty( Toolkit::TextVisual::Property::MULTI_LINE, keyValue.second );
228         }
229         else if( keyValue.first.stringKey == HORIZONTAL_ALIGNMENT_PROPERTY )
230         {
231           DoSetProperty( Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, keyValue.second );
232         }
233         else if( keyValue.first.stringKey == VERTICAL_ALIGNMENT_PROPERTY )
234         {
235           DoSetProperty( Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, keyValue.second );
236         }
237         else if( keyValue.first.stringKey == TEXT_COLOR_PROPERTY )
238         {
239           DoSetProperty( Toolkit::TextVisual::Property::TEXT_COLOR, keyValue.second );
240         }
241         else if( keyValue.first.stringKey == ENABLE_MARKUP_PROPERTY )
242         {
243           DoSetProperty( Toolkit::TextVisual::Property::ENABLE_MARKUP, keyValue.second );
244         }
245         break;
246       }
247     }
248   }
249
250   // Elide the text if it exceeds the boundaries.
251   mController->SetTextElideEnabled( true );
252
253   // Retrieve the layout engine to set the cursor's width.
254   Text::Layout::Engine& engine = mController->GetLayoutEngine();
255
256   // Sets 0 as cursor's width.
257   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
258 }
259
260 void TextVisual::DoSetOnStage( Actor& actor )
261 {
262   mControl = actor;
263
264   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
265   if( !geometry )
266   {
267     geometry =  VisualFactoryCache::CreateQuadGeometry();
268     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY , geometry );
269   }
270
271   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
272   if( !shader )
273   {
274     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
275     mFactoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
276   }
277   shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
278
279   mImpl->mRenderer = Renderer::New( geometry, shader );
280   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::TEXT );
281
282   UpdateRenderer();
283 }
284
285 void TextVisual::DoSetOffStage( Actor& actor )
286 {
287   if( mImpl->mRenderer )
288   {
289     // Removes the renderer from the actor.
290     actor.RemoveRenderer( mImpl->mRenderer );
291
292     RemoveTextureSet();
293
294     // Resets the renderer.
295     mImpl->mRenderer.Reset();
296   }
297
298   // Resets the control handle.
299   mControl.Reset();
300 }
301
302 void TextVisual::OnSetTransform()
303 {
304   UpdateRenderer();
305 }
306
307 void TextVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
308 {
309   switch( index )
310   {
311     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
312     {
313       const bool enableMarkup = propertyValue.Get<bool>();
314       mController->SetMarkupProcessorEnabled( enableMarkup );
315       break;
316     }
317     case Toolkit::TextVisual::Property::TEXT:
318     {
319       mController->SetText( propertyValue.Get<std::string>() );
320       break;
321     }
322     case Toolkit::TextVisual::Property::FONT_FAMILY:
323     {
324       SetFontFamilyProperty( mController, propertyValue );
325       break;
326     }
327     case Toolkit::TextVisual::Property::FONT_STYLE:
328     {
329       SetFontStyleProperty( mController, propertyValue, Text::FontStyle::DEFAULT );
330       break;
331     }
332     case Toolkit::TextVisual::Property::POINT_SIZE:
333     {
334       const float pointSize = propertyValue.Get<float>();
335       if( !Equals( mController->GetDefaultPointSize(), pointSize ) )
336       {
337         mController->SetDefaultPointSize( pointSize );
338       }
339       break;
340     }
341     case Toolkit::TextVisual::Property::MULTI_LINE:
342     {
343       mController->SetMultiLineEnabled( propertyValue.Get<bool>() );
344       break;
345     }
346     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
347     {
348       Toolkit::Text::Layout::HorizontalAlignment alignment( Toolkit::Text::Layout::HORIZONTAL_ALIGN_BEGIN );
349       if( Scripting::GetEnumeration< Toolkit::Text::Layout::HorizontalAlignment >( propertyValue.Get< std::string >().c_str(),
350                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE,
351                                                                                    HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
352                                                                                    alignment ) )
353       {
354         mController->SetHorizontalAlignment( alignment );
355       }
356       break;
357     }
358     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
359     {
360       Toolkit::Text::Layout::VerticalAlignment alignment( Toolkit::Text::Layout::VERTICAL_ALIGN_BOTTOM );
361       if( Scripting::GetEnumeration< Toolkit::Text::Layout::VerticalAlignment >( propertyValue.Get< std::string >().c_str(),
362                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE,
363                                                                                  VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
364                                                                                  alignment ) )
365       {
366         mController->SetVerticalAlignment( alignment );
367       }
368       break;
369     }
370     case Toolkit::TextVisual::Property::TEXT_COLOR:
371     {
372       const Vector4& textColor = propertyValue.Get< Vector4 >();
373       if( mController->GetDefaultColor() != textColor )
374       {
375         mController->SetDefaultColor( textColor );
376       }
377       break;
378     }
379   }
380 }
381
382 void TextVisual::UpdateRenderer()
383 {
384   Actor control = mControl.GetHandle();
385   if( !control )
386   {
387     // Nothing to do.
388     return;
389   }
390
391   // Calculates the size to be used to relayout.
392   Vector2 relayoutSize;
393
394   const bool isWidthRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.z ) < Math::MACHINE_EPSILON_1000;
395   const bool isHeightRelative = fabsf( mImpl->mTransform.mOffsetSizeMode.w ) < Math::MACHINE_EPSILON_1000;
396
397   // Round the size and offset to avoid pixel alignement issues.
398   relayoutSize.width = floorf( 0.5f + ( isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width ) );
399   relayoutSize.height = floorf( 0.5f + ( isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height ) );
400
401   if( ( fabsf( relayoutSize.width ) < Math::MACHINE_EPSILON_1000 ) || ( fabsf( relayoutSize.height ) < Math::MACHINE_EPSILON_1000 ) )
402   {
403     // Removes the texture set.
404     RemoveTextureSet();
405
406     // Remove any renderer previously set.
407     if( mImpl->mRenderer )
408     {
409       control.RemoveRenderer( mImpl->mRenderer );
410     }
411
412     // Nothing else to do if the relayout size is zero.
413     return;
414   }
415
416   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( relayoutSize );
417
418   if( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
419   {
420     // Removes the texture set.
421     RemoveTextureSet();
422
423     // Remove any renderer previously set.
424     if( mImpl->mRenderer )
425     {
426       control.RemoveRenderer( mImpl->mRenderer );
427     }
428
429     if( ( relayoutSize.width > Math::MACHINE_EPSILON_1000 ) &&
430         ( relayoutSize.height > Math::MACHINE_EPSILON_1000 ) )
431     {
432       PixelData data = mTypesetter->Render( relayoutSize );
433
434       Vector4 atlasRect = FULL_TEXTURE_RECT;
435       TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add( atlasRect, data );
436
437       if( textureSet )
438       {
439         mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
440       }
441       else
442       {
443         // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
444         // In that case, create a texture. TODO: should tile the text.
445
446         Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
447                                         data.GetPixelFormat(),
448                                         data.GetWidth(),
449                                         data.GetHeight() );
450
451         texture.Upload( data );
452
453         textureSet = TextureSet::New();
454         textureSet.SetTexture( 0u, texture );
455
456         mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
457       }
458
459       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
460
461       //Register transform properties
462       mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
463
464       // Filter mode needs to be set to nearest to avoid blurry text.
465       Sampler sampler = Sampler::New();
466       sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
467       textureSet.SetSampler( 0u, sampler );
468
469       mImpl->mRenderer.SetTextures( textureSet );
470
471       control.AddRenderer( mImpl->mRenderer );
472     }
473   }
474 }
475
476 void TextVisual::RemoveTextureSet()
477 {
478   if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
479   {
480     // Removes the text's image from the texture atlas.
481     Vector4 atlasRect;
482
483     const Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
484     if( index != Property::INVALID_INDEX )
485     {
486       const Property::Value& atlasRectValue = mImpl->mRenderer.GetProperty( index );
487       atlasRectValue.Get( atlasRect );
488
489       const TextureSet& textureSet = mImpl->mRenderer.GetTextures();
490       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
491     }
492   }
493 }
494
495 } // namespace Internal
496
497 } // namespace Toolkit
498
499 } // namespace Dali