b5f1fc967b8653b62806d2fec1ed62cdd17e5393
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / relayout-helper.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include "relayout-helper.h"
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace Internal
26 {
27
28 namespace RelayoutHelper
29 {
30
31 Vector3 GetNaturalSize( Actor actor )
32 {
33   Vector3 size = actor.GetCurrentSize();
34   const float depth = size.depth;
35
36   // Get natural size for TextActor.
37   TextActor textActor = TextActor::DownCast( actor );
38   if( textActor )
39   {
40     Font font = textActor.GetFont();
41     if( !font )
42     {
43       font = Font::New();
44     }
45     size = font.MeasureText( textActor.GetText() );
46     size.depth = depth;
47   }
48
49   // Get natural size for ImageActor.
50   // TODO: currently it doesn't work as expected.
51   ImageActor imageActor = ImageActor::DownCast( actor );
52   if( ( imageActor ) && ( imageActor.GetImage() ) )
53   {
54     Image image = imageActor.GetImage();
55     size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), depth );
56   }
57
58   return size;
59 }
60
61 float GetHeightForWidth( Actor actor, float width )
62 {
63   Vector3 size = actor.GetCurrentSize();
64   float height = 0.f;
65
66   TextActor textActor = TextActor::DownCast( actor );
67   if( textActor )
68   {
69     Font font = textActor.GetFont();
70     if( !font )
71     {
72       font = Font::New();
73     }
74     size = font.MeasureText( textActor.GetText() );
75   }
76
77   ImageActor imageActor = ImageActor::DownCast( actor );
78   if( ( imageActor ) && ( imageActor.GetImage() ) )
79   {
80     Image image = imageActor.GetImage();
81     size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), 0.f );
82   }
83
84   height = size.height / ( size.width / width );
85
86   return height;
87 }
88
89 } // namespace RelayoutHelper
90
91 } // namespace Internal
92
93 } // namespace Toolkit
94
95 } // namespace Dali