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_CORE_COMPONENTS_BASE_PROPERTIES_CLIP_PATH_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_CLIP_PATH_H 18 19 #include <vector> 20 21 #include "base/geometry/dimension.h" 22 #include "base/geometry/dimension_offset.h" 23 #include "base/memory/ace_type.h" 24 #include "frameworks/core/components/common/properties/radius.h" 25 26 namespace OHOS::Ace { 27 28 enum class GeometryBoxType { 29 NONE, 30 MARGIN_BOX, 31 BORDER_BOX, 32 PADDING_BOX, 33 CONTENT_BOX, 34 }; 35 36 enum class BasicShapeType { 37 NONE, 38 INSET, 39 CIRCLE, 40 ELLIPSE, 41 POLYGON, 42 PATH, 43 RECT 44 }; 45 46 enum class LengthMode { 47 HORIZONTAL, 48 VERTICAL, 49 OTHER, 50 }; 51 52 class BasicShape : public AceType { 53 DECLARE_ACE_TYPE(BasicShape, AceType); 54 55 public: 56 BasicShape() = default; BasicShape(BasicShapeType basicShapeType)57 explicit BasicShape(BasicShapeType basicShapeType) : basicShapeType_(basicShapeType) {} 58 ~BasicShape() override = default; 59 SetBasicShapeType(BasicShapeType basicShapeType)60 void SetBasicShapeType(BasicShapeType basicShapeType) 61 { 62 basicShapeType_ = basicShapeType; 63 } 64 GetBasicShapeType()65 BasicShapeType GetBasicShapeType() const 66 { 67 return basicShapeType_; 68 } 69 GetWidth()70 const Dimension& GetWidth() const 71 { 72 return width_; 73 } 74 GetHeight()75 const Dimension& GetHeight() const 76 { 77 return height_; 78 } 79 GetOffset()80 const DimensionOffset& GetOffset() const 81 { 82 return offset_; 83 } 84 SetWidth(const Dimension & width)85 void SetWidth(const Dimension& width) 86 { 87 width_ = width; 88 } 89 SetHeight(const Dimension & height)90 void SetHeight(const Dimension& height) 91 { 92 height_ = height; 93 } 94 SetOffset(const DimensionOffset & offset)95 void SetOffset(const DimensionOffset& offset) 96 { 97 offset_ = offset; 98 } 99 SetColor(const Color & color)100 void SetColor(const Color& color) 101 { 102 color_ = color; 103 } 104 GetColor()105 Color GetColor() const 106 { 107 return color_; 108 } 109 110 protected: 111 BasicShapeType basicShapeType_ = BasicShapeType::NONE; 112 Dimension width_; 113 Dimension height_; 114 DimensionOffset offset_; 115 Color color_; 116 }; 117 118 // inset(<top> <right> <bottom> <left> round <top-radius> <right-radius> <bottom-radius> <left-radius>) 119 class Inset : public BasicShape { 120 DECLARE_ACE_TYPE(Inset, BasicShape); 121 122 public: Inset()123 Inset() : BasicShape(BasicShapeType::INSET) {} 124 ~Inset() override = default; 125 bool SetLength(const std::vector<Dimension>& lengths); 126 void SetRadius(const std::vector<Dimension>& rounds, bool isX = true); 127 GetTop()128 const Dimension& GetTop() const 129 { 130 return top_; 131 } 132 GetRight()133 const Dimension& GetRight() const 134 { 135 return right_; 136 } 137 GetBottom()138 const Dimension& GetBottom() const 139 { 140 return bottom_; 141 } 142 GetLeft()143 const Dimension& GetLeft() const 144 { 145 return left_; 146 } 147 GetTopLeftRadius()148 const Radius& GetTopLeftRadius() const 149 { 150 return topLeftRadius_; 151 } 152 GetTopRightRadius()153 const Radius& GetTopRightRadius() const 154 { 155 return topRightRadius_; 156 } 157 GetBottomRightRadius()158 const Radius& GetBottomRightRadius() const 159 { 160 return bottomRightRadius_; 161 } 162 GetBottomLeftRadius()163 const Radius& GetBottomLeftRadius() const 164 { 165 return bottomLeftRadius_; 166 } 167 SetTop(const Dimension & top)168 void SetTop(const Dimension& top) 169 { 170 top_ = top; 171 } 172 SetRight(const Dimension & right)173 void SetRight(const Dimension& right) 174 { 175 right_ = right; 176 } 177 SetBottom(const Dimension & bottom)178 void SetBottom(const Dimension& bottom) 179 { 180 bottom_ = bottom; 181 } 182 SetLeft(const Dimension & left)183 void SetLeft(const Dimension& left) 184 { 185 left_ = left; 186 } 187 188 void SetTopLeftRadius(const Dimension& topLeftRadius, bool isX = true) 189 { 190 if (!topLeftRadius.IsValid()) { 191 return; 192 } 193 if (isX) { 194 topLeftRadius_.SetX(topLeftRadius); 195 } 196 topLeftRadius_.SetY(topLeftRadius); 197 } 198 199 void SetTopRightRadius(const Dimension& topRightRadius, bool isX = true) 200 { 201 if (!topRightRadius.IsValid()) { 202 return; 203 } 204 if (isX) { 205 topRightRadius_.SetX(topRightRadius); 206 } 207 topRightRadius_.SetY(topRightRadius); 208 } 209 210 void SetBottomRightRadius(const Dimension& bottomRightRadius, bool isX = true) 211 { 212 if (!bottomRightRadius.IsValid()) { 213 return; 214 } 215 if (isX) { 216 bottomRightRadius_.SetX(bottomRightRadius); 217 } 218 bottomRightRadius_.SetY(bottomRightRadius); 219 } 220 221 void SetBottomLeftRadius(const Dimension& bottomLeftRadius, bool isX = true) 222 { 223 if (!bottomLeftRadius.IsValid()) { 224 return; 225 } 226 if (isX) { 227 bottomLeftRadius_.SetX(bottomLeftRadius); 228 } 229 bottomLeftRadius_.SetY(bottomLeftRadius); 230 } 231 232 private: 233 void SetLength(const Dimension& top, const Dimension& right, const Dimension& bottom, const Dimension& left); 234 void SetRadius(const Dimension& top, const Dimension& right, 235 const Dimension& bottom, const Dimension& left, bool isX); 236 237 Dimension top_; 238 Dimension right_; 239 Dimension bottom_; 240 Dimension left_; 241 Radius topLeftRadius_; 242 Radius topRightRadius_; 243 Radius bottomRightRadius_; 244 Radius bottomLeftRadius_; 245 }; 246 247 // circle(radius at x-axis y-axis) 248 class Circle : public BasicShape { 249 DECLARE_ACE_TYPE(Circle, BasicShape); 250 251 public: Circle()252 Circle() : BasicShape(BasicShapeType::CIRCLE) {} 253 ~Circle() override = default; 254 GetRadius()255 const Dimension& GetRadius() const 256 { 257 return radius_; 258 } 259 GetAxisX()260 const Dimension& GetAxisX() const 261 { 262 return axisX_; 263 } 264 GetAxisY()265 const Dimension& GetAxisY() const 266 { 267 return axisY_; 268 } 269 SetRadius(const Dimension & radius)270 void SetRadius(const Dimension& radius) 271 { 272 if (radius.IsValid()) { 273 radius_ = radius; 274 } 275 } 276 SetAxisX(const Dimension & axisX)277 void SetAxisX(const Dimension& axisX) 278 { 279 axisX_ = axisX; 280 } 281 SetAxisY(const Dimension & axisY)282 void SetAxisY(const Dimension& axisY) 283 { 284 axisY_ = axisY; 285 } 286 287 private: 288 Dimension radius_; 289 Dimension axisX_ = Dimension(0.5, DimensionUnit::PERCENT); 290 Dimension axisY_ = Dimension(0.5, DimensionUnit::PERCENT); 291 }; 292 293 // ellipse(x-rad y-rad at x-axis y-axis) 294 class Ellipse : public BasicShape { 295 DECLARE_ACE_TYPE(Ellipse, BasicShape); 296 297 public: Ellipse()298 Ellipse() : BasicShape(BasicShapeType::ELLIPSE) {} 299 ~Ellipse() override = default; 300 GetRadiusX()301 const Dimension& GetRadiusX() const 302 { 303 return radiusX_; 304 } 305 GetRadiusY()306 const Dimension& GetRadiusY() const 307 { 308 return radiusY_; 309 } 310 GetAxisX()311 const Dimension& GetAxisX() const 312 { 313 return axisX_; 314 } 315 GetAxisY()316 const Dimension& GetAxisY() const 317 { 318 return axisY_; 319 } 320 SetRadiusX(const Dimension & radiusX)321 void SetRadiusX(const Dimension& radiusX) 322 { 323 if (radiusX.IsValid()) { 324 radiusX_ = radiusX; 325 } 326 } 327 SetRadiusY(const Dimension & radiusY)328 void SetRadiusY(const Dimension& radiusY) 329 { 330 if (radiusY.IsValid()) { 331 radiusY_ = radiusY; 332 } 333 } 334 SetAxisX(const Dimension & axisX)335 void SetAxisX(const Dimension& axisX) 336 { 337 axisX_ = axisX; 338 } 339 SetAxisY(const Dimension & axisY)340 void SetAxisY(const Dimension& axisY) 341 { 342 axisY_ = axisY; 343 } 344 345 private: 346 Dimension radiusX_; 347 Dimension radiusY_; 348 Dimension axisX_ = Dimension(0.5, DimensionUnit::PERCENT); 349 Dimension axisY_ = Dimension(0.5, DimensionUnit::PERCENT); 350 }; 351 352 // polygon(x-axis y-axis, x-axis y-axis, … ) 353 class Polygon : public BasicShape { 354 DECLARE_ACE_TYPE(Polygon, BasicShape); 355 356 public: Polygon()357 Polygon() : BasicShape(BasicShapeType::POLYGON) {} 358 ~Polygon() override = default; 359 GetPoints()360 const std::vector<std::pair<Dimension, Dimension>>& GetPoints() const 361 { 362 return points_; 363 } 364 PushPoint(const Dimension & x,const Dimension & y)365 void PushPoint(const Dimension& x, const Dimension& y) 366 { 367 points_.emplace_back(std::make_pair(x, y)); 368 } 369 IsValid()370 bool IsValid() const 371 { 372 return !points_.empty(); 373 } 374 375 private: 376 std::vector<std::pair<Dimension, Dimension>> points_; 377 }; 378 379 // path('value') 380 class Path : public BasicShape { 381 DECLARE_ACE_TYPE(Path, BasicShape); 382 383 public: Path()384 Path() : BasicShape(BasicShapeType::PATH) {} 385 ~Path() override = default; 386 GetValue()387 const std::string& GetValue() const 388 { 389 return value_; 390 } 391 SetValue(const std::string & value)392 void SetValue(const std::string& value) 393 { 394 value_ = value; 395 } 396 397 private: 398 std::string value_; 399 }; 400 401 class ShapeRect : public BasicShape { 402 DECLARE_ACE_TYPE(ShapeRect, BasicShape); 403 404 public: ShapeRect()405 ShapeRect() : BasicShape(BasicShapeType::RECT) {} 406 ~ShapeRect() override = default; 407 SetTopLeftRadius(const Radius & topLeftRadius)408 void SetTopLeftRadius(const Radius& topLeftRadius) 409 { 410 topLeftRadius_ = topLeftRadius; 411 } 412 SetTopRightRadius(const Radius & topRightRadius)413 void SetTopRightRadius(const Radius& topRightRadius) 414 { 415 topRightRadius_ = topRightRadius; 416 } 417 SetBottomRightRadius(const Radius & bottomRightRadius)418 void SetBottomRightRadius(const Radius& bottomRightRadius) 419 { 420 bottomRightRadius_ = bottomRightRadius; 421 } 422 SetBottomLeftRadius(const Radius & bottomLeftRadius)423 void SetBottomLeftRadius(const Radius& bottomLeftRadius) 424 { 425 bottomLeftRadius_ = bottomLeftRadius; 426 } 427 GetTopLeftRadius()428 Radius GetTopLeftRadius() const 429 { 430 return topLeftRadius_; 431 } 432 GetTopRightRadius()433 Radius GetTopRightRadius() const 434 { 435 return topRightRadius_; 436 } 437 GetBottomRightRadius()438 Radius GetBottomRightRadius() const 439 { 440 return bottomRightRadius_; 441 } 442 GetBottomLeftRadius()443 Radius GetBottomLeftRadius() const 444 { 445 return bottomLeftRadius_; 446 } 447 448 void SetRadiusWidth(const Dimension& value, const AnimationOption& option = AnimationOption()) 449 { 450 topLeftRadius_.SetX(value, option); 451 topRightRadius_.SetX(value, option); 452 bottomRightRadius_.SetX(value, option); 453 bottomLeftRadius_.SetX(value, option); 454 } 455 456 void SetRadiusHeight(const Dimension& value, const AnimationOption& option = AnimationOption()) 457 { 458 topLeftRadius_.SetY(value, option); 459 topRightRadius_.SetY(value, option); 460 bottomRightRadius_.SetY(value, option); 461 bottomLeftRadius_.SetY(value, option); 462 } 463 464 private: 465 Radius topLeftRadius_ = Radius(-1.0); 466 Radius topRightRadius_ = Radius(-1.0); 467 Radius bottomRightRadius_ = Radius(-1.0); 468 Radius bottomLeftRadius_ = Radius(-1.0); 469 }; 470 471 class ACE_EXPORT ClipPath final : public AceType { 472 DECLARE_ACE_TYPE(ClipPath, AceType); 473 474 public: 475 ClipPath() = default; ClipPath(const RefPtr<BasicShape> & basicShape)476 explicit ClipPath(const RefPtr<BasicShape>& basicShape) : basicShape_(basicShape) {} ClipPath(GeometryBoxType geometryBoxType,const RefPtr<BasicShape> & basicShape)477 ClipPath(GeometryBoxType geometryBoxType, const RefPtr<BasicShape>& basicShape) 478 : geometryBoxType_(geometryBoxType), basicShape_(basicShape) 479 {} 480 ~ClipPath() override = default; 481 482 static GeometryBoxType GetGeometryBoxType(const std::string& value); 483 static void GetBasicShapeInfo(const std::string& value, BasicShapeType& basicShapeType, std::string& data); 484 static RefPtr<ClipPath> CreateShape(const std::string& value); 485 SetGeometryBoxType(GeometryBoxType geometryBoxType)486 void SetGeometryBoxType(GeometryBoxType geometryBoxType) 487 { 488 geometryBoxType_ = geometryBoxType; 489 } 490 GetGeometryBoxType()491 GeometryBoxType GetGeometryBoxType() const 492 { 493 return geometryBoxType_; 494 } 495 SetBasicShape(const RefPtr<BasicShape> & basicShape)496 void SetBasicShape(const RefPtr<BasicShape>& basicShape) 497 { 498 basicShape_ = basicShape; 499 } 500 GetBasicShape()501 const RefPtr<BasicShape>& GetBasicShape() const 502 { 503 return basicShape_; 504 } 505 NeedClip()506 bool NeedClip() const 507 { 508 return (basicShape_ && basicShape_->GetBasicShapeType() != BasicShapeType::NONE) || 509 geometryBoxType_ != GeometryBoxType::NONE; 510 } 511 512 private: 513 static RefPtr<Circle> CreateCircle(const std::string& data); 514 static RefPtr<Ellipse> CreateEllipse(const std::string& data); 515 static RefPtr<Ellipse> CreateEllipseSize(const std::string& data); 516 static RefPtr<Inset> CreateInset(const std::string& data); 517 static RefPtr<Inset> CreateInsetSize(const std::string& data); 518 static RefPtr<Polygon> CreatePolygon(const std::string& value); 519 static RefPtr<Path> CreatePath(const std::string& value); 520 521 GeometryBoxType geometryBoxType_ = GeometryBoxType::NONE; 522 RefPtr<BasicShape> basicShape_; 523 }; 524 525 class ACE_EXPORT MaskPath final : public AceType { 526 DECLARE_ACE_TYPE(MaskPath, AceType); 527 528 public: 529 MaskPath() = default; MaskPath(const RefPtr<BasicShape> & basicShape)530 explicit MaskPath(const RefPtr<BasicShape>& basicShape) : basicShape_(basicShape) {} MaskPath(GeometryBoxType geometryBoxType,const RefPtr<BasicShape> & basicShape)531 MaskPath(GeometryBoxType geometryBoxType, const RefPtr<BasicShape>& basicShape) 532 : geometryBoxType_(geometryBoxType), basicShape_(basicShape) 533 {} 534 ~MaskPath() override = default; 535 SetGeometryBoxType(GeometryBoxType geometryBoxType)536 void SetGeometryBoxType(GeometryBoxType geometryBoxType) 537 { 538 geometryBoxType_ = geometryBoxType; 539 } 540 GetGeometryBoxType()541 GeometryBoxType GetGeometryBoxType() const 542 { 543 return geometryBoxType_; 544 } 545 SetBasicShape(const RefPtr<BasicShape> & basicShape)546 void SetBasicShape(const RefPtr<BasicShape>& basicShape) 547 { 548 basicShape_ = basicShape; 549 } 550 GetBasicShape()551 const RefPtr<BasicShape>& GetBasicShape() const 552 { 553 return basicShape_; 554 } 555 556 private: 557 GeometryBoxType geometryBoxType_ = GeometryBoxType::NONE; 558 RefPtr<BasicShape> basicShape_; 559 }; 560 561 } // namespace OHOS::Ace 562 563 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_CLIP_PATH_H 564