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 bool isSetSize_ = false; 53 }; 54 55 class IconLayoutElement : public SecurityComponentLayoutElement { 56 public: IconLayoutElement()57 IconLayoutElement() {}; 58 void Init(RefPtr<SecurityComponentLayoutProperty>& property, RefPtr<LayoutWrapper>& textWrap); 59 ~IconLayoutElement() = default; 60 61 double ShrinkWidth(double reduceSize) override; 62 double ShrinkHeight(double reduceSize) override; 63 void DoMeasure(); 64 65 private: 66 bool isExist_ = false; 67 double minIconSize_; 68 RefPtr<SecurityComponentLayoutProperty> secCompProperty_; 69 RefPtr<LayoutWrapper> iconWrap_; 70 }; 71 72 class PaddingLayoutElement : public SecurityComponentLayoutElement { 73 public: PaddingLayoutElement()74 PaddingLayoutElement() {}; Init(bool isVertical,bool isSetSize,double size,double minSize)75 void Init(bool isVertical, bool isSetSize, double size, double minSize) 76 { 77 isVertical_ = isVertical; 78 isSetSize_ = isSetSize; 79 minPadddingSize_ = minSize; 80 if (isVertical) { 81 height_ = size; 82 } else { 83 width_ = size; 84 } 85 }; 86 87 ~PaddingLayoutElement() = default; EnlargeWidth(double addSize)88 double EnlargeWidth(double addSize) override 89 { 90 if (isVertical_ || isSetSize_) { 91 return addSize; 92 } 93 width_ += addSize; 94 return 0.0; 95 }; 96 EnlargeHeight(double addSize)97 double EnlargeHeight(double addSize) override 98 { 99 if (!isVertical_ || isSetSize_) { 100 return addSize; 101 } 102 height_ += addSize; 103 return 0.0; 104 }; 105 ShrinkWidth(double reduceSize)106 double ShrinkWidth(double reduceSize) override 107 { 108 if (isVertical_ || isSetSize_) { 109 return reduceSize; 110 } 111 if (GreatNotEqual(minPadddingSize_, width_ - reduceSize)) { 112 double remain = reduceSize - (width_ - minPadddingSize_); 113 width_ = minPadddingSize_; 114 return remain; 115 } 116 width_ -= reduceSize; 117 return 0.0; 118 }; 119 ShrinkHeight(double reduceSize)120 double ShrinkHeight(double reduceSize) override 121 { 122 if (!isVertical_ || isSetSize_) { 123 return reduceSize; 124 } 125 if (GreatNotEqual(minPadddingSize_, height_ - reduceSize)) { 126 double remain = reduceSize - (height_ - minPadddingSize_); 127 height_ = minPadddingSize_; 128 return remain; 129 } 130 height_ -= reduceSize; 131 return 0.0; 132 }; 133 134 private: 135 bool isVertical_ = false; 136 bool isSetSize_ = false; 137 double minPadddingSize_; 138 }; 139 140 class TextLayoutElement : public SecurityComponentLayoutElement { 141 public: TextLayoutElement()142 TextLayoutElement() {}; 143 void Init(RefPtr<SecurityComponentLayoutProperty>& property, 144 RefPtr<LayoutWrapper>& textWrap); 145 ~TextLayoutElement() = default; 146 147 double ShrinkWidth(double reduceSize) override; 148 149 double ShrinkHeight(double reduceSize) override; 150 151 bool GetCurrentTextSize(std::optional<SizeF>& currentTextSize, Dimension& currentFontSize); 152 153 void DoMeasure(bool isVertical, float minWidth, float leftSpace); 154 155 bool TryShrinkTextWidth(SizeF& point, SizeF& circlePoint, bool maxSpaceToShrink, float maxDistance, 156 float threshold); 157 pow(float value)158 float pow(float value) 159 { 160 return value * value; 161 } 162 163 private: 164 void UpdateSize(bool isWidth); 165 void MeasureMinTextSize(); 166 void ChooseExactFontSize(RefPtr<TextLayoutProperty>& property, bool isWidth); 167 std::optional<SizeF> GetMeasureTextSize(const std::string& data, 168 const Dimension& fontSize, FontWeight fontWeight, float constraintWidth); 169 void MeasureForWidth(float width); 170 171 bool isExist_ = false; 172 Dimension minFontSize_; 173 Dimension defaultFontSize_; 174 RefPtr<SecurityComponentLayoutProperty> secCompProperty_; 175 RefPtr<LayoutWrapper> textWrap_; 176 std::optional<SizeF> minTextSize_; 177 }; 178 } // namespace OHOS::Ace::NG 179 #endif 180