2 * Copyright (c) 2014 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 "relayout-helper.h"
22 #include <dali/public-api/actors/image-actor.h>
23 #include <dali/public-api/actors/text-actor.h>
26 #include <dali-toolkit/public-api/controls/control.h>
38 namespace RelayoutHelper
41 Vector3 GetNaturalSize( Actor actor )
43 Vector3 size( 0.0f, 0.0f, 0.0f );
45 Toolkit::Control control = Toolkit::Control::DownCast( actor );
48 size = control.GetNaturalSize();
52 size = actor.GetCurrentSize();
53 const float depth = size.depth;
55 // Get natural size for ImageActor.
56 // TODO: currently it doesn't work as expected.
57 ImageActor imageActor = ImageActor::DownCast( actor );
58 if( ( imageActor ) && ( imageActor.GetImage() ) )
60 Image image = imageActor.GetImage();
61 size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), depth );
65 // Get natural size for TextActor.
66 TextActor textActor = TextActor::DownCast( actor );
69 Font font = textActor.GetFont();
74 size = font.MeasureText( textActor.GetText() );
83 float GetHeightForWidth( Actor actor, float width )
87 Toolkit::Control control = Toolkit::Control::DownCast( actor );
90 height = control.GetHeightForWidth( width );
94 bool constrainSize = false;
95 Vector3 size( 0.0f, 0.0f, 0.0f );
97 ImageActor imageActor = ImageActor::DownCast( actor );
98 if( ( imageActor ) && ( imageActor.GetImage() ) )
100 Image image = imageActor.GetImage();
101 size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), 0.0f );
103 constrainSize = true;
107 TextActor textActor = TextActor::DownCast( actor );
110 Font font = textActor.GetFont();
115 size = font.MeasureText( textActor.GetText() );
117 constrainSize = true;
121 size = actor.GetCurrentSize();
126 float scaleRatio = width / size.width;
129 // Allow the scale to decrease if greater than input width but not increase if less than input width
130 if( scaleRatio > 1.0f )
136 height = size.height * scaleRatio;
142 } // namespace RelayoutHelper
144 } // namespace Internal
146 } // namespace Toolkit