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