1 /* 2 * Copyright (c) 2025 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_ACCESSIBILITY_UTILS_ACCESSIBILITY_RECT_INFO_UTILS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ACCESSIBILITY_UTILS_ACCESSIBILITY_RECT_INFO_UTILS_H 18 19 #include <map> 20 21 #include "base/geometry/ng/point_t.h" 22 #include "base/memory/ace_type.h" 23 #include "core/components_ng/pattern/pattern.h" 24 #include "core/pipeline_ng/pipeline_context.h" 25 26 namespace OHOS::Ace::NG { 27 constexpr int32_t ZERO_ANGLE = 0; 28 constexpr int32_t QUARTER_ANGLE = 90; 29 constexpr int32_t HALF_ANGLE = 180; 30 constexpr int32_t THREE_QUARTER_ANGLE = 270; 31 constexpr int32_t FULL_ANGLE = 360; 32 33 class AccessibilityRealRectInfo { 34 public: AccessibilityRealRectInfo(NG::PointT<int32_t> realLeftTop,float width,float height,int32_t rotation,NG::VectorF scale)35 AccessibilityRealRectInfo( 36 NG::PointT<int32_t> realLeftTop, 37 float width, 38 float height, 39 int32_t rotation, 40 NG::VectorF scale) 41 { 42 realLeftTop_ = realLeftTop; 43 componentLeftTop_ = realLeftTop; 44 width_ = width; 45 height_ = height; 46 rotation_ = rotation; 47 scale_ = scale; 48 } 49 50 ~AccessibilityRealRectInfo() = default; 51 GetWidth()52 int32_t GetWidth() 53 { 54 return width_; 55 } 56 GetHeight()57 int32_t GetHeight() 58 { 59 return height_; 60 } 61 GetRotation()62 int32_t GetRotation() 63 { 64 return rotation_; 65 } 66 GetComponentLeftTop()67 NG::PointT<int32_t>& GetComponentLeftTop() 68 { 69 return componentLeftTop_; 70 } 71 GetRealLeftTop()72 NG::PointT<int32_t>& GetRealLeftTop() 73 { 74 return componentLeftTop_; 75 } 76 GetScale()77 NG::VectorF& GetScale() 78 { 79 return scale_; 80 } 81 82 // calculate child's true rect by self's rotate and scale GenerateRectInfoByRelativeConner(NG::PointT<int32_t> leftTop,NG::PointT<int32_t> rightBottom,NG::VectorF & scale)83 AccessibilityRealRectInfo GenerateRectInfoByRelativeConner( 84 NG::PointT<int32_t> leftTop, 85 NG::PointT<int32_t> rightBottom, 86 NG::VectorF& scale) 87 { 88 UpdatePointWithScale(leftTop); 89 UpdatePointWithScale(rightBottom); 90 UpdatePointToReal(leftTop); 91 UpdatePointToReal(rightBottom); 92 93 auto degree = GetRotationByConner(leftTop, rightBottom); 94 auto width = LessNotEqual(leftTop.GetX(), rightBottom.GetX()) 95 ? rightBottom.GetX() - leftTop.GetX() 96 : leftTop.GetX() - rightBottom.GetX(); 97 auto height = LessNotEqual(leftTop.GetY(), rightBottom.GetY()) 98 ? rightBottom.GetY() - leftTop.GetY() 99 : leftTop.GetY() - rightBottom.GetY(); 100 101 AccessibilityRealRectInfo rectInfo(leftTop, width, height, degree, scale); 102 103 rectInfo.UpdateRealLeftTopWithRealWidthHeight(); 104 rectInfo.UpdateScale(scale_); 105 return rectInfo; 106 } 107 108 private: 109 // calculate rotate degree by leftTop Point and rightBottom Point GetRotationByConner(NG::PointT<int32_t> leftTop,NG::PointT<int32_t> rightBottom)110 int32_t GetRotationByConner(NG::PointT<int32_t> leftTop, NG::PointT<int32_t> rightBottom) 111 { 112 if ((leftTop.GetX() < rightBottom.GetX()) && (leftTop.GetY() < rightBottom.GetY())) { 113 return ZERO_ANGLE; 114 } 115 if ((rightBottom.GetX() < leftTop.GetX()) && (leftTop.GetY() < rightBottom.GetY())) { 116 return QUARTER_ANGLE; 117 } 118 if ((rightBottom.GetX() < leftTop.GetX()) && (rightBottom.GetY() < leftTop.GetY())) { 119 return HALF_ANGLE; 120 } 121 if ((leftTop.GetX() < rightBottom.GetX()) && (rightBottom.GetY() < leftTop.GetY())) { 122 return THREE_QUARTER_ANGLE; 123 } 124 return ZERO_ANGLE; 125 } 126 UpdatePointWithScale(NG::PointT<int32_t> & point)127 void UpdatePointWithScale(NG::PointT<int32_t>& point) 128 { 129 point.SetX(point.GetX() * scale_.x); 130 point.SetY(point.GetY() * scale_.y); 131 } 132 133 // apply rotation UpdatePointToReal(NG::PointT<int32_t> & point)134 void UpdatePointToReal(NG::PointT<int32_t>& point) 135 { 136 auto transformedX = point.GetX(); 137 auto transformedY = point.GetY(); 138 switch (rotation_) { 139 case QUARTER_ANGLE: 140 transformedX = -point.GetY(); 141 transformedY = point.GetX(); 142 break; 143 case HALF_ANGLE: 144 transformedX = -point.GetX(); 145 transformedY = -point.GetY(); 146 break; 147 case THREE_QUARTER_ANGLE: 148 transformedX = point.GetY(); 149 transformedY = -point.GetX(); 150 break; 151 } 152 point.SetX(componentLeftTop_.GetX() + transformedX); 153 point.SetY(componentLeftTop_.GetY() + transformedY); 154 } 155 UpdateRealLeftTopWithRealWidthHeight()156 void UpdateRealLeftTopWithRealWidthHeight() 157 { 158 auto x = componentLeftTop_.GetX(); 159 auto y = componentLeftTop_.GetY(); 160 switch (rotation_) { 161 case QUARTER_ANGLE: 162 x -= width_; 163 break; 164 case HALF_ANGLE: 165 x -= width_; 166 y -= height_; 167 break; 168 case THREE_QUARTER_ANGLE: 169 y -= height_; 170 break; 171 } 172 realLeftTop_.SetX(x); 173 realLeftTop_.SetY(y); 174 } 175 UpdateScale(NG::VectorF & parentScale)176 void UpdateScale(NG::VectorF& parentScale) 177 { 178 scale_.x *= parentScale.x; 179 scale_.y *= parentScale.y; 180 } 181 182 int32_t width_; 183 int32_t height_; 184 int32_t rotation_; 185 NG::PointT<int32_t> componentLeftTop_; 186 NG::PointT<int32_t> realLeftTop_; 187 NG::VectorF scale_{1.0, 1.0}; 188 }; 189 190 } // namespace OHOS::Ace::NG 191 192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ACCESSIBILITY_UTILS_ACCESSIBILITY_RECT_INFO_UTILS_H 193