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 namespace {
20 constexpr int NUM_0 = 0;
21 constexpr int NUM_1 = 1;
22 constexpr int NUM_2 = 2;
23 constexpr int NUM_3 = 3;
24 constexpr int NUM_4 = 4;
25 constexpr int NUM_5 = 5;
26 constexpr int NUM_6 = 6;
27 constexpr int NUM_7 = 7;
28 constexpr int NUM_8 = 8;
29 }
30
31 const Alignment Alignment::TOP_LEFT = Alignment(-1.0, -1.0);
32 const Alignment Alignment::TOP_CENTER = Alignment(0.0, -1.0);
33 const Alignment Alignment::TOP_RIGHT = Alignment(1.0, -1.0);
34 const Alignment Alignment::CENTER_LEFT = Alignment(-1.0, 0.0);
35 const Alignment Alignment::CENTER = Alignment(0.0, 0.0);
36 const Alignment Alignment::CENTER_RIGHT = Alignment(1.0, 0.0);
37 const Alignment Alignment::BOTTOM_LEFT = Alignment(-1.0, 1.0);
38 const Alignment Alignment::BOTTOM_CENTER = Alignment(0.0, 1.0);
39 const Alignment Alignment::BOTTOM_RIGHT = Alignment(1.0, 1.0);
40
41 /*
42 * This function is used to calculate the relative position of child rectangle in parent rectangle.
43 * Note that parentSize should not be less than childSize,
44 * which means neither width or height of parent rectangle should be less than child's.
45 *
46 * @param parentSize Size of parent in which you want to put a child.
47 * @param childSize Size of child you want to put in parent.
48 * @param alignment A variable by which to decide the position of a child in parent.
49 * @return A relative position toward top-left point of parent.
50 */
GetAlignPosition(const Size & parentSize,const Size & childSize,const Alignment & alignment)51 Offset Alignment::GetAlignPosition(const Size& parentSize, const Size& childSize, const Alignment& alignment)
52 {
53 Offset offset;
54 if (GreatOrEqual(parentSize.Width(), childSize.Width())) {
55 offset.SetX((1.0 + alignment.GetHorizontal()) * (parentSize.Width() - childSize.Width()) / 2.0);
56 }
57 if (GreatOrEqual(parentSize.Height(), childSize.Height())) {
58 offset.SetY((1.0 + alignment.GetVertical()) * (parentSize.Height() - childSize.Height()) / 2.0);
59 }
60 return offset;
61 }
62
GetAlignPosition(const NG::SizeF & parentSize,const NG::SizeF & childSize,const Alignment & alignment)63 NG::OffsetF Alignment::GetAlignPosition(
64 const NG::SizeF& parentSize, const NG::SizeF& childSize, const Alignment& alignment)
65 {
66 NG::OffsetF offset;
67 if (GreatOrEqual(parentSize.Width(), childSize.Width())) {
68 offset.SetX((1.0 + alignment.GetHorizontal()) * (parentSize.Width() - childSize.Width()) / 2.0);
69 }
70 if (GreatOrEqual(parentSize.Height(), childSize.Height())) {
71 offset.SetY((1.0 + alignment.GetVertical()) * (parentSize.Height() - childSize.Height()) / 2.0);
72 }
73 return offset;
74 }
75
GetAlignmentStr(TextDirection direction) const76 std::string Alignment::GetAlignmentStr(TextDirection direction) const
77 {
78 std::string result;
79 Alignment alignment = Alignment(horizontal_, vertical_);
80 if (alignment == TOP_LEFT) {
81 result = direction == TextDirection::RTL ? "Alignment.TopEnd" : "Alignment.TopStart";
82 } else if (alignment == TOP_CENTER) {
83 result = "Alignment.Top";
84 } else if (alignment == TOP_RIGHT) {
85 result = direction == TextDirection::RTL ? "Alignment.TopStart" : "Alignment.TopEnd";
86 } else if (alignment == CENTER_LEFT) {
87 result = direction == TextDirection::RTL ? "Alignment.End" : "Alignment.Start";
88 } else if (alignment == CENTER) {
89 result = "Alignment.Center";
90 } else if (alignment == CENTER_RIGHT) {
91 result = direction == TextDirection::RTL ? "Alignment.Start" : "Alignment.End";
92 } else if (alignment == BOTTOM_LEFT) {
93 result = direction == TextDirection::RTL ? "Alignment.BottomEnd" : "Alignment.BottomStart";
94 } else if (alignment == BOTTOM_CENTER) {
95 result = "Alignment.Bottom";
96 } else if (alignment == BOTTOM_RIGHT) {
97 result = direction == TextDirection::RTL ? "Alignment.BottomStart" : "Alignment.BottomEnd";
98 } else {
99 result = "Alignment.Center";
100 }
101 return result;
102 }
103
GetAlignment(TextDirection direction,const std::string & str)104 Alignment Alignment::GetAlignment(TextDirection direction, const std::string& str)
105 {
106 static const std::unordered_map<std::string, std::function<Alignment(TextDirection)>> uMap {
107 { "Alignment.Top", [](TextDirection direction) { return TOP_CENTER; } },
108 { "Alignment.TopStart",
109 [](TextDirection direction) { return direction == TextDirection::LTR ? TOP_LEFT : TOP_RIGHT; } },
110 { "Alignment.TopEnd",
111 [](TextDirection direction) { return direction == TextDirection::LTR ? TOP_RIGHT : TOP_LEFT; } },
112 { "Alignment.Center", [](TextDirection direction) { return CENTER; } },
113 { "Alignment.Start",
114 [](TextDirection direction) { return direction == TextDirection::LTR ? CENTER_LEFT : CENTER_RIGHT; } },
115 { "Alignment.End",
116 [](TextDirection direction) { return direction == TextDirection::LTR ? CENTER_RIGHT : CENTER_LEFT; } },
117 { "Alignment.Bottom", [](TextDirection direction) { return BOTTOM_CENTER; } },
118 { "Alignment.BottomStart",
119 [](TextDirection direction) { return direction == TextDirection::LTR ? BOTTOM_LEFT : BOTTOM_RIGHT; } },
120 { "Alignment.BottomEnd",
121 [](TextDirection direction) { return direction == TextDirection::LTR ? BOTTOM_RIGHT : BOTTOM_LEFT; } },
122 };
123
124 return uMap.count(str) ? uMap.at(str)(direction) : CENTER;
125 }
126
ParseAlignment(int32_t alignment)127 std::optional<Alignment> Alignment::ParseAlignment(int32_t alignment)
128 {
129 switch (alignment) {
130 case NUM_0:
131 return Alignment::TOP_LEFT;
132 case NUM_1:
133 return Alignment::TOP_CENTER;
134 case NUM_2:
135 return Alignment::TOP_RIGHT;
136 case NUM_3:
137 return Alignment::CENTER_LEFT;
138 case NUM_4:
139 return Alignment::CENTER;
140 case NUM_5:
141 return Alignment::CENTER_RIGHT;
142 case NUM_6:
143 return Alignment::BOTTOM_LEFT;
144 case NUM_7:
145 return Alignment::BOTTOM_CENTER;
146 case NUM_8:
147 return Alignment::BOTTOM_RIGHT;
148 default:
149 break;
150 }
151 return std::nullopt;
152 }
153 } // namespace OHOS::Ace
154