1 /* 2 * Copyright (c) 2021-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 16 #ifndef PATH_H 17 #define PATH_H 18 19 #include <memory> 20 #include <vector> 21 22 #include "common/rs_macros.h" 23 #include "drawing/engine_adapter/impl_interface/path_impl.h" 24 #include "utils/matrix.h" 25 #include "utils/point.h" 26 #include "utils/rect.h" 27 28 #ifdef USE_ROSEN_DRAWING 29 #ifdef WINDOWS_PLATFORM 30 #ifdef DIFFERENCE 31 #undef DIFFERENCE 32 #endif 33 #ifdef WINDING 34 #undef WINDING 35 #endif 36 #endif 37 #endif 38 39 namespace OHOS { 40 namespace Rosen { 41 namespace Drawing { 42 enum class PathDirection { 43 CW_DIRECTION, 44 CCW_DIRECTION, 45 }; 46 47 enum class PathFillType { 48 WINDING, 49 EVENTODD, 50 INVERSE_WINDING, 51 INVERSE_EVENTODD, 52 }; 53 54 enum class PathOp { 55 DIFFERENCE, 56 INTERSECT, 57 UNION, 58 XOR, 59 REVERSE_DIFFERENCE, 60 }; 61 62 class RS_EXPORT Path { 63 public: 64 Path() noexcept; 65 Path(const Path& p) noexcept; 66 Path &operator=(const Path& p) noexcept; 67 virtual ~Path(); 68 GetDrawingType()69 virtual DrawingType GetDrawingType() const 70 { 71 return DrawingType::COMMON; 72 } 73 74 /* 75 * @brief Parses the SVG format string that describes the drawing path, and sets the Path. 76 * @param str A string in SVG format that describes the drawing path. 77 */ 78 virtual bool BuildFromSVGString(const std::string& str); 79 80 /* 81 * @brief Parses into a string in SVG format that describes the Path. 82 */ 83 std::string ConvertToSVGString() const; 84 virtual void MoveTo(scalar x, scalar y); 85 virtual void LineTo(scalar x, scalar y); 86 virtual void ArcTo(scalar pt1X, scalar pt1Y, scalar pt2X, scalar pt2Y, scalar startAngle, scalar sweepAngle); 87 virtual void ArcTo(const Point& pt1, const Point& pt2, scalar startAngle, scalar sweepAngle); 88 virtual void ArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar endX, scalar endY); 89 virtual void CubicTo( 90 scalar ctrlPt1X, scalar ctrlPt1Y, scalar ctrlPt2X, scalar ctrlPt2Y, scalar endPtX, scalar endPtY); 91 virtual void CubicTo(const Point& ctrlPt1, const Point& ctrlPt2, const Point& endPt); 92 virtual void QuadTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY); 93 virtual void QuadTo(const Point& ctrlPt, const Point endPt); 94 95 virtual void AddRect(const Rect& rect, PathDirection dir = PathDirection::CW_DIRECTION); 96 virtual void AddRect( 97 scalar left, scalar top, scalar right, scalar bottom, PathDirection dir = PathDirection::CW_DIRECTION); 98 99 virtual void AddOval(const Rect& oval, PathDirection dir = PathDirection::CW_DIRECTION); 100 virtual void AddArc(const Rect& oval, scalar startAngle, scalar sweepAngle); 101 virtual void AddPoly(const std::vector<Point>& points, int count, bool close); 102 virtual void AddCircle(scalar x, scalar y, scalar radius, PathDirection dir = PathDirection::CW_DIRECTION); 103 virtual void AddRoundRect( 104 const Rect& rect, scalar xRadius, scalar yRadius, PathDirection dir = PathDirection::CW_DIRECTION); 105 106 /* 107 * @brief Adds the circle rectangle to the Path. 108 * @param roundRect The boundary and radius of a roundRect. 109 * @param dir Direction of rotation. 110 */ 111 virtual void AddRoundRect(const RoundRect& roundRect, PathDirection dir = PathDirection::CW_DIRECTION); 112 113 virtual void AddPath(const Path& src, scalar dx, scalar dy); 114 virtual void AddPath(const Path& src); 115 virtual void AddPath(const Path& src, const Matrix& matrix); 116 117 /* 118 * @brief Adds the src from back forward to the Path. 119 * @param src To add Path. 120 */ 121 virtual void ReverseAddPath(const Path& src); 122 123 Rect GetBounds() const; 124 virtual void SetFillStyle(PathFillType fillstyle); 125 126 bool Interpolate(const Path& ending, scalar weight, Path& out); 127 128 /* 129 * @brief Two equal number of point set path objects are weighted interpolated, and the sets Path. 130 * @param src The number of point sets of the src Path. 131 * @param ending The number of point sets of the ending Path. 132 * @param weight The weight value is between 0 and 1. 133 */ 134 virtual bool BuildFromInterpolate(const Path& src, const Path& ending, scalar weight); 135 virtual void Transform(const Matrix& matrix); 136 virtual void Offset(scalar dx, scalar dy); 137 virtual bool Op(const Path& path1, Path& path2, PathOp op); 138 139 /* 140 * @brief Checks whether the Path is valid. 141 */ 142 bool IsValid() const; 143 virtual void Reset(); 144 145 virtual void Close(); 146 147 /* 148 * @brief Gets the length of the current path object. 149 * @param forceClosed Whether to close the Path. 150 */ 151 scalar GetLength(bool forceClosed) const; 152 153 /* 154 * @brief Gets the position and tangent of the distance from the starting position of the Path. 155 * @param distance The distance from the start of the Path, should be greater than 0 and less than 'GetLength()' 156 * @param position Sets to the position of distance from the starting position of the Path. 157 * @param tangent Sets to the tangent of distance from the starting position of the Path. 158 * @param forceClosed Whether to close the Path. 159 */ 160 bool GetPositionAndTangent(scalar distance, Point& position, Point& tangent, bool forceClosed) const; 161 162 template<typename T> GetImpl()163 const std::shared_ptr<T> GetImpl() const 164 { 165 return impl_->DowncastingTo<T>(); 166 } 167 168 private: 169 std::shared_ptr<PathImpl> impl_; 170 }; 171 } // namespace Drawing 172 } // namespace Rosen 173 } // namespace OHOS 174 #endif 175