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 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
GetAlignPosition(const NG::SizeF & parentSize,const NG::SizeF & childSize,const Alignment & alignment)52 NG::OffsetF Alignment::GetAlignPosition(
53 const NG::SizeF& parentSize, const NG::SizeF& childSize, const Alignment& alignment)
54 {
55 NG::OffsetF offset;
56 if (GreatOrEqual(parentSize.Width(), childSize.Width())) {
57 offset.SetX((1.0 + alignment.GetHorizontal()) * (parentSize.Width() - childSize.Width()) / 2.0);
58 }
59 if (GreatOrEqual(parentSize.Height(), childSize.Height())) {
60 offset.SetY((1.0 + alignment.GetVertical()) * (parentSize.Height() - childSize.Height()) / 2.0);
61 }
62 return offset;
63 }
64
GetAlignmentStr(TextDirection direction) const65 std::string Alignment::GetAlignmentStr(TextDirection direction) const
66 {
67 std::string result;
68 Alignment alignment = Alignment(horizontal_, vertical_);
69 if (alignment == TOP_LEFT) {
70 result = direction == TextDirection::RTL ? "Alignment.TopEnd" : "Alignment.TopStart";
71 } else if (alignment == TOP_CENTER) {
72 result = "Alignment.Top";
73 } else if (alignment == TOP_RIGHT) {
74 result = direction == TextDirection::RTL ? "Alignment.TopStart" : "Alignment.TopEnd";
75 } else if (alignment == CENTER_LEFT) {
76 result = direction == TextDirection::RTL ? "Alignment.End" : "Alignment.Start";
77 } else if (alignment == CENTER) {
78 result = "Alignment.Center";
79 } else if (alignment == CENTER_RIGHT) {
80 result = direction == TextDirection::RTL ? "Alignment.Start" : "Alignment.End";
81 } else if (alignment == BOTTOM_LEFT) {
82 result = direction == TextDirection::RTL ? "Alignment.BottomEnd" : "Alignment.BottomStart";
83 } else if (alignment == BOTTOM_CENTER) {
84 result = "Alignment.Bottom";
85 } else if (alignment == BOTTOM_RIGHT) {
86 result = direction == TextDirection::RTL ? "Alignment.BottomStart" : "Alignment.BottomEnd";
87 } else {
88 result = "Alignment.Center";
89 }
90 return result;
91 }
92
93 } // namespace OHOS::Ace
94