1 /* 2 * Copyright (c) 2023 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 #ifndef SECUIRTY_COMPONENT_LAYOUT_ELEMENT_H 16 #define SECUIRTY_COMPONENT_LAYOUT_ELEMENT_H 17 18 #include "base/geometry/ng/size_t.h" 19 #include "base/log/ace_scoring_log.h" 20 #include "base/memory/referenced.h" 21 #include "core/components_ng/layout/layout_wrapper.h" 22 #include "core/components_ng/pattern/security_component/security_component_layout_property.h" 23 #include "core/components_ng/pattern/text/text_layout_property.h" 24 25 namespace OHOS::Ace::NG { 26 class SecurityComponentLayoutElement { 27 public: 28 SecurityComponentLayoutElement() = default; 29 virtual ~SecurityComponentLayoutElement() = default; EnlargeWidth(double addSize)30 virtual double EnlargeWidth(double addSize) 31 { 32 return addSize; 33 }; 34 EnlargeHeight(double addSize)35 virtual double EnlargeHeight(double addSize) 36 { 37 return addSize; 38 }; 39 ShrinkWidth(double reduceSize)40 virtual double ShrinkWidth(double reduceSize) 41 { 42 return reduceSize; 43 }; 44 ShrinkHeight(double reduceSize)45 virtual double ShrinkHeight(double reduceSize) 46 { 47 return reduceSize; 48 }; 49 50 double width_ = 0.0; 51 double height_ = 0.0; 52 double alpha_ = 1.0; 53 double defaultWidth_ = 0.0; 54 double defaultHeight_ = 0.0; 55 bool isSetSize_ = false; 56 }; 57 58 class IconLayoutElement : public SecurityComponentLayoutElement { 59 public: IconLayoutElement()60 IconLayoutElement() {}; 61 void Init(const RefPtr<SecurityComponentLayoutProperty>& property, RefPtr<LayoutWrapper>& textWrap); 62 ~IconLayoutElement() = default; 63 64 double ShrinkWidth(double reduceSize) override; 65 double ShrinkHeight(double reduceSize) override; 66 void DoMeasure(); 67 68 private: 69 void UpdateUserSetSize(const RefPtr<SecurityComponentLayoutProperty>& property); 70 bool isExist_ = false; 71 double minIconSize_; 72 RefPtr<SecurityComponentLayoutProperty> secCompProperty_; 73 RefPtr<LayoutWrapper> iconWrap_; 74 }; 75 76 class PaddingLayoutElement : public SecurityComponentLayoutElement { 77 public: PaddingLayoutElement()78 PaddingLayoutElement() {}; Init(bool isVertical,bool isSetSize,double size,double minSize)79 void Init(bool isVertical, bool isSetSize, double size, double minSize) 80 { 81 isVertical_ = isVertical; 82 isSetSize_ = isSetSize; 83 minPadddingSize_ = minSize; 84 if (isVertical) { 85 height_ = size; 86 defaultHeight_ = size; 87 } else { 88 width_ = size; 89 defaultWidth_ = size; 90 } 91 }; 92 93 ~PaddingLayoutElement() = default; EnlargeWidth(double addSize)94 double EnlargeWidth(double addSize) override 95 { 96 if (isVertical_ || isSetSize_) { 97 return addSize; 98 } 99 width_ += addSize; 100 return 0.0; 101 }; 102 EnlargeHeight(double addSize)103 double EnlargeHeight(double addSize) override 104 { 105 if (!isVertical_ || isSetSize_) { 106 return addSize; 107 } 108 height_ += addSize; 109 return 0.0; 110 }; 111 ShrinkWidth(double reduceSize)112 double ShrinkWidth(double reduceSize) override 113 { 114 if (isVertical_ || isSetSize_) { 115 return reduceSize; 116 } 117 if (GreatNotEqual(minPadddingSize_, width_ - reduceSize)) { 118 double remain = reduceSize - (width_ - minPadddingSize_); 119 width_ = minPadddingSize_; 120 return remain; 121 } 122 width_ -= reduceSize; 123 return 0.0; 124 }; 125 ShrinkHeight(double reduceSize)126 double ShrinkHeight(double reduceSize) override 127 { 128 if (!isVertical_ || isSetSize_) { 129 return reduceSize; 130 } 131 if (GreatNotEqual(minPadddingSize_, height_ - reduceSize)) { 132 double remain = reduceSize - (height_ - minPadddingSize_); 133 height_ = minPadddingSize_; 134 return remain; 135 } 136 height_ -= reduceSize; 137 return 0.0; 138 }; 139 140 private: 141 bool isVertical_ = false; 142 bool isSetSize_ = false; 143 double minPadddingSize_; 144 }; 145 146 class TextLayoutElement : public SecurityComponentLayoutElement { 147 public: TextLayoutElement()148 TextLayoutElement() {}; 149 void Init(const RefPtr<SecurityComponentLayoutProperty>& property, 150 RefPtr<LayoutWrapper>& textWrap); 151 ~TextLayoutElement() = default; 152 153 double ShrinkWidth(double reduceSize) override; 154 155 double ShrinkHeight(double reduceSize) override; 156 157 bool DidExceedMaxLines(std::optional<SizeF>& currentTextSize); 158 159 bool GetCurrentTextSize(std::optional<SizeF>& currentTextSize, Dimension& currentFontSize); 160 161 void DoMeasure(bool isVertical, float minWidth, float leftSpace); 162 163 bool TryShrinkTextWidth(SizeF& point, SizeF& circlePoint, bool maxSpaceToShrink, float maxDistance, 164 float threshold); 165 pow(float value)166 float pow(float value) 167 { 168 return value * value; 169 } 170 171 private: 172 void UpdateSize(bool isWidth); 173 void MeasureMinTextSize(); 174 void ChooseExactFontSize(RefPtr<TextLayoutProperty>& property, bool isWidth); 175 std::optional<SizeF> GetMeasureTextSize(const std::string& data, 176 const Dimension& fontSize, FontWeight fontWeight, float constraintWidth); 177 void MeasureForWidth(float width); 178 float GetHeightConstraint(const RefPtr<SecurityComponentLayoutProperty>& property, float height); 179 void UpdateFontSize(); 180 181 bool isExist_ = false; 182 bool isAdaptive_ = false; 183 Dimension minFontSize_; 184 Dimension defaultFontSize_; 185 RefPtr<SecurityComponentLayoutProperty> secCompProperty_; 186 RefPtr<LayoutWrapper> textWrap_; 187 std::optional<SizeF> minTextSize_; 188 }; 189 } // namespace OHOS::Ace::NG 190 #endif 191