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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_ALIGNMENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_ALIGNMENT_H 18 19 #include <string> 20 21 #include "base/geometry/ng/offset_t.h" 22 #include "base/geometry/ng/size_t.h" 23 #include "base/geometry/offset.h" 24 #include "base/geometry/size.h" 25 #include "base/utils/macros.h" 26 #include "core/components/common/layout/constants.h" 27 28 namespace OHOS::Ace { 29 30 class ACE_FORCE_EXPORT Alignment final { 31 public: 32 Alignment() = default; 33 ~Alignment() = default; 34 GetHorizontal()35 double GetHorizontal() const 36 { 37 return horizontal_; 38 } 39 GetVertical()40 double GetVertical() const 41 { 42 return vertical_; 43 } 44 45 bool operator==(const Alignment& other) const 46 { 47 return NearEqual(horizontal_, other.horizontal_) && NearEqual(vertical_, other.vertical_); 48 } 49 50 bool operator!=(const Alignment& other) const 51 { 52 return !operator==(other); 53 } 54 55 static Offset GetAlignPosition(const Size& parentSize, const Size& childSize, const Alignment& alignment); 56 static NG::OffsetF GetAlignPosition( 57 const NG::SizeF& parentSize, const NG::SizeF& childSize, const Alignment& alignment); 58 static const Alignment TOP_LEFT; 59 static const Alignment TOP_CENTER; 60 static const Alignment TOP_RIGHT; 61 static const Alignment CENTER_LEFT; 62 static const Alignment CENTER; 63 static const Alignment CENTER_RIGHT; 64 static const Alignment BOTTOM_LEFT; 65 static const Alignment BOTTOM_CENTER; 66 static const Alignment BOTTOM_RIGHT; 67 ToString()68 std::string ToString() const 69 { 70 std::stringstream ss; 71 ss << "Alignment (" << std::fixed << std::setprecision(1) << horizontal_ << ", " << vertical_ << ")"; 72 std::string output = ss.str(); 73 return output; 74 } 75 76 std::string GetAlignmentStr(TextDirection direction) const; 77 78 static Alignment GetAlignment(TextDirection direction, const std::string& str); 79 80 static std::optional<Alignment> ParseAlignment(int32_t alignment); 81 82 private: 83 friend class AlignCreator; Alignment(double horizontal,double vertical)84 Alignment(double horizontal, double vertical) : horizontal_(horizontal), vertical_(vertical) {} 85 double horizontal_ = 0.0; 86 double vertical_ = 0.0; 87 }; 88 89 } // namespace OHOS::Ace 90 91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_ALIGNMENT_H 92