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