1 /* 2 * Copyright (c) 2021 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_BASE_GEOMETRY_DIMENSION_RECT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_DIMENSION_RECT_H 18 19 #include <cmath> 20 #include <limits> 21 22 #include "base/geometry/dimension.h" 23 #include "base/geometry/dimension_offset.h" 24 #include "base/geometry/dimension_size.h" 25 26 namespace OHOS::Ace { 27 28 class DimensionRect { 29 public: 30 DimensionRect() = default; 31 ~DimensionRect() = default; 32 DimensionRect(const Dimension & width,const Dimension & height,const DimensionOffset & offset)33 DimensionRect(const Dimension& width, const Dimension& height, const DimensionOffset& offset) 34 : width_(width), height_(height), offset_(offset) 35 {} 36 DimensionRect(const Dimension & width,const Dimension & height)37 DimensionRect(const Dimension& width, const Dimension& height) : width_(width), height_(height) {} 38 GetWidth()39 const Dimension& GetWidth() const 40 { 41 return width_; 42 } 43 GetHeight()44 const Dimension& GetHeight() const 45 { 46 return height_; 47 } 48 GetOffset()49 const DimensionOffset& GetOffset() const 50 { 51 return offset_; 52 } 53 SetOffset(const DimensionOffset & offset)54 void SetOffset(const DimensionOffset& offset) 55 { 56 offset_ = offset; 57 } 58 SetSize(const DimensionSize & size)59 void SetSize(const DimensionSize& size) 60 { 61 width_ = size.Width(); 62 height_ = size.Height(); 63 } 64 SetWidth(const Dimension & width)65 void SetWidth(const Dimension& width) 66 { 67 width_ = width; 68 } 69 SetHeight(const Dimension & height)70 void SetHeight(const Dimension& height) 71 { 72 height_ = height; 73 } 74 Reset()75 void Reset() 76 { 77 width_ = 0.0_vp; 78 height_ = 0.0_vp; 79 offset_ = DimensionOffset(); 80 } 81 ToString()82 std::string ToString() const 83 { 84 static const int32_t precision = 2; 85 std::stringstream ss; 86 ss << "Rect (" << std::fixed << std::setprecision(precision) << offset_.GetX().ToString() << ", " 87 << offset_.GetY().ToString() << ") - ["; 88 ss << width_.ToString(); 89 ss << " x "; 90 ss << height_.ToString(); 91 ss << "]"; 92 std::string output = ss.str(); 93 return output; 94 } 95 ToJsonString()96 std::string ToJsonString() const 97 { 98 auto jsonValue = JsonUtil::Create(true); 99 jsonValue->Put("x", offset_.GetX().ToString().c_str()); 100 jsonValue->Put("y", offset_.GetY().ToString().c_str()); 101 jsonValue->Put("width", width_.ToString().c_str()); 102 jsonValue->Put("height", height_.ToString().c_str()); 103 return jsonValue->ToString(); 104 } 105 106 private: 107 Dimension width_ = 0.0_vp; 108 Dimension height_ = 0.0_vp; 109 DimensionOffset offset_; 110 }; 111 112 } // namespace OHOS::Ace 113 114 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_DIMENSION_RECT_H 115