1 /* 2 * Copyright (c) 2022 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_COMPONENTS_NG_PROPERTIES_CALC_LENGTH_H 17 #define FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_CALC_LENGTH_H 18 19 #include "base/geometry/dimension.h" 20 #include "base/geometry/ng/size_t.h" 21 #include "base/utils/utils.h" 22 23 namespace OHOS::Ace::NG { 24 struct ACE_EXPORT ScaleProperty { 25 float vpScale = 0.0f; 26 float fpScale = 0.0f; 27 float lpxScale = 0.0f; 28 ResetScaleProperty29 void Reset() 30 { 31 vpScale = 0.0; 32 fpScale = 0.0; 33 lpxScale = 0.0; 34 } 35 36 bool operator==(const ScaleProperty& scaleProperty) const 37 { 38 return NearEqual(vpScale, scaleProperty.vpScale) && NearEqual(fpScale, scaleProperty.fpScale) && 39 NearEqual(lpxScale, scaleProperty.lpxScale); 40 } 41 42 bool operator!=(const ScaleProperty& scaleProperty) const 43 { 44 return !(*this == scaleProperty); 45 } 46 47 static ScaleProperty CreateScaleProperty(); 48 }; 49 50 class CalcLength { 51 public: 52 CalcLength() = default; CalcLength(const std::string & value)53 explicit CalcLength(const std::string& value) : calcValue_(value) {} 54 ~CalcLength() = default; 55 dimension_(value,unit)56 explicit CalcLength(double value, DimensionUnit unit = DimensionUnit::PX) : dimension_(value, unit) {}; CalcLength(const Dimension & dimension)57 explicit CalcLength(const Dimension& dimension) : dimension_(dimension) {}; 58 Reset()59 void Reset() 60 { 61 calcValue_ = ""; 62 dimension_.Reset(); 63 } 64 CalcValue()65 const std::string& CalcValue() const 66 { 67 return calcValue_; 68 } 69 SetCalcValue(const std::string & value)70 void SetCalcValue(const std::string& value) 71 { 72 calcValue_ = value; 73 } 74 75 bool NormalizeToPx(double vpScale, double fpScale, double lpxScale, double parentLength, double& result) const; 76 IsValid()77 bool IsValid() const 78 { 79 if (calcValue_.empty()) { 80 return dimension_.IsNonNegative(); 81 } 82 return true; 83 } 84 85 bool operator==(const CalcLength& length) const 86 { 87 if (calcValue_.empty() && length.calcValue_.empty()) { 88 return dimension_ == length.dimension_; 89 } 90 return calcValue_ == length.calcValue_; 91 } 92 93 bool operator!=(const CalcLength& length) const 94 { 95 return !(*this == length); 96 } 97 ToString()98 std::string ToString() const 99 { 100 if (calcValue_.empty()) { 101 return dimension_.ToString(); 102 } 103 return calcValue_; 104 } 105 FromString(const std::string & str)106 static CalcLength FromString(const std::string& str) 107 { 108 return CalcLength(Dimension::FromString(str)); 109 } 110 GetDimension()111 Dimension GetDimension() const 112 { 113 if (!IsValid()) { 114 return Dimension(); 115 } 116 return dimension_; 117 } 118 119 private: 120 std::string calcValue_; 121 Dimension dimension_; 122 }; 123 } // namespace OHOS::Ace::NG 124 125 #endif // FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_CALC_LENGTH_H 126