• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/components/common/properties/alignment.h"
17 
18 namespace OHOS::Ace {
19 
20 const Alignment Alignment::TOP_LEFT = Alignment(-1.0, -1.0);
21 const Alignment Alignment::TOP_CENTER = Alignment(0.0, -1.0);
22 const Alignment Alignment::TOP_RIGHT = Alignment(1.0, -1.0);
23 const Alignment Alignment::CENTER_LEFT = Alignment(-1.0, 0.0);
24 const Alignment Alignment::CENTER = Alignment(0.0, 0.0);
25 const Alignment Alignment::CENTER_RIGHT = Alignment(1.0, 0.0);
26 const Alignment Alignment::BOTTOM_LEFT = Alignment(-1.0, 1.0);
27 const Alignment Alignment::BOTTOM_CENTER = Alignment(0.0, 1.0);
28 const Alignment Alignment::BOTTOM_RIGHT = Alignment(1.0, 1.0);
29 
30 /*
31  * This function is used to calculate the relative position of child rectangle in parent rectangle.
32  * Note that parentSize should not be less than childSize,
33  * which means neither width or height of parent rectangle should be less than child's.
34  *
35  * @param parentSize Size of parent in which you want to put a child.
36  * @param childSize Size of child you want to put in parent.
37  * @param alignment A variable by which to decide the position of a child in parent.
38  * @return A relative position toward top-left point of parent.
39  */
GetAlignPosition(const Size & parentSize,const Size & childSize,const Alignment & alignment)40 const Offset Alignment::GetAlignPosition(const Size& parentSize, const Size& childSize, const Alignment& alignment)
41 {
42     Offset offset;
43     if (GreatOrEqual(parentSize.Width(), childSize.Width())) {
44         offset.SetX((1.0 + alignment.GetHorizontal()) * (parentSize.Width() - childSize.Width()) / 2.0);
45     }
46     if (GreatOrEqual(parentSize.Height(), childSize.Height())) {
47         offset.SetY((1.0 + alignment.GetVertical()) * (parentSize.Height() - childSize.Height()) / 2.0);
48     }
49     return offset;
50 }
51 
GetAlignmentStr(TextDirection direction) const52 std::string Alignment::GetAlignmentStr(TextDirection direction) const
53 {
54     std::string result = "";
55     Alignment alignment = Alignment(horizontal_, vertical_);
56     if (alignment == TOP_LEFT) {
57         result = direction == TextDirection::RTL ? "Alignment.TopEnd" : "Alignment.TopStart";
58     } else if (alignment == TOP_CENTER) {
59         result = "Alignment.Top";
60     } else if (alignment == TOP_RIGHT) {
61         result = direction == TextDirection::RTL ? "Alignment.TopStart" : "Alignment.TopEnd";
62     } else if (alignment == CENTER_LEFT) {
63         result = direction == TextDirection::RTL ? "Alignment.End" : "Alignment.Start";
64     } else if (alignment == CENTER) {
65         result = "Alignment.Center";
66     } else if (alignment == CENTER_RIGHT) {
67         result = direction == TextDirection::RTL ? "Alignment.Start" : "Alignment.End";
68     } else if (alignment == BOTTOM_LEFT) {
69         result = direction == TextDirection::RTL ? "Alignment.BottomEnd" : "Alignment.BottomStart";
70     } else if (alignment == BOTTOM_CENTER) {
71         result = "Alignment.Bottom";
72     } else if (alignment == BOTTOM_RIGHT) {
73         result = direction == TextDirection::RTL ? "Alignment.BottomStart" : "Alignment.BottomEnd";
74     } else {
75         result = "Alignment.Center";
76     }
77     return result;
78 }
79 
80 } // namespace OHOS::Ace
81