1 /* 2 * Copyright (c) 2020-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 /** 17 * @addtogroup UI_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file ui_canvas.h 28 * 29 * @brief Defines the attributes of the canvas component and provides functions for drawing rectangles, 30 * circles, and others. 31 * 32 * @since 1.0 33 * @version 1.0 34 */ 35 36 #ifndef GRAPHIC_LITE_UI_CANVAS_H 37 #define GRAPHIC_LITE_UI_CANVAS_H 38 39 #include "common/image.h" 40 #include "components/ui_label.h" 41 #include "gfx_utils/list.h" 42 43 namespace OHOS { 44 /** 45 * @brief Defines the basic styles of graphs drawn on canvases. 46 * 47 * @since 1.0 48 * @version 1.0 49 */ 50 class Paint : public HeapBase { 51 public: 52 /** 53 * @brief A constructor used to create a <b>Paint</b> instance. 54 * 55 * @since 1.0 56 * @version 1.0 57 */ Paint()58 Paint() 59 : style_(PaintStyle::STROKE_FILL_STYLE), fillColor_(Color::Black()), 60 strokeColor_(Color::White()), opacity_(OPA_OPAQUE), strokeWidth_(2) {} 61 62 /** 63 * @brief A destructor used to delete the <b>Paint</b> instance. 64 * 65 * @since 1.0 66 * @version 1.0 67 */ ~Paint()68 virtual ~Paint() {} 69 70 /** 71 * @brief Enumerates paint styles of a closed graph. The styles are invalid for non-closed graphs. 72 */ 73 enum PaintStyle { 74 /** Stroke only */ 75 STROKE_STYLE = 1, 76 /** Fill only */ 77 FILL_STYLE, 78 /** Stroke and fill */ 79 STROKE_FILL_STYLE, 80 }; 81 82 /** 83 * @brief Sets the paint style of a closed graph. 84 * 85 * @param style Indicates the paint style. Stroke and fill are set by default. For details, see {@link PaintStyle}. 86 * @see GetStyle 87 * @since 1.0 88 * @version 1.0 89 */ SetStyle(PaintStyle style)90 void SetStyle(PaintStyle style) 91 { 92 style_ = style; 93 } 94 95 /** 96 * @brief Obtains the paint style of a closed graph. 97 * 98 * @return Returns the paint style. For details, see {@link PaintStyle}. 99 * @see SetStyle 100 * @since 1.0 101 * @version 1.0 102 */ GetStyle()103 PaintStyle GetStyle() const 104 { 105 return style_; 106 } 107 108 /** 109 * @brief Sets the width of a line or border. 110 * 111 * @param width Indicates the line width when a line is drawn or the border width when a closed graph is drawn. 112 * The width is extended to both sides. 113 * @see GetStrokeWidth 114 * @since 1.0 115 * @version 1.0 116 */ SetStrokeWidth(uint16_t width)117 void SetStrokeWidth(uint16_t width) 118 { 119 strokeWidth_ = width; 120 } 121 122 /** 123 * @brief Obtains the width of a line or border. 124 * 125 * @return Returns the line width if a line is drawn or the border width if a closed graph is drawn. 126 * @see SetStrokeWidth 127 * @since 1.0 128 * @version 1.0 129 */ GetStrokeWidth()130 uint16_t GetStrokeWidth() const 131 { 132 return strokeWidth_; 133 } 134 135 /** 136 * @brief Sets the color of a line or border. 137 * 138 * @param color Indicates the line color when a line is drawn or the border color when a closed graph is drawn. 139 * @see GetStrokeColor 140 * @since 1.0 141 * @version 1.0 142 */ SetStrokeColor(ColorType color)143 void SetStrokeColor(ColorType color) 144 { 145 strokeColor_ = color; 146 } 147 148 /** 149 * @brief Obtains the color of a line or border. 150 * 151 * @return Returns the line color if a line is drawn or the border color if a closed graph is drawn. 152 * @see SetStrokeWidth 153 * @since 1.0 154 * @version 1.0 155 */ GetStrokeColor()156 ColorType GetStrokeColor() const 157 { 158 return strokeColor_; 159 } 160 161 /** 162 * @brief Sets fill color. 163 * 164 * This function is valid only for closed graphs. 165 * 166 * @param color Indicates the fill color to set. 167 * @see GetFillColor 168 * @since 1.0 169 * @version 1.0 170 */ SetFillColor(ColorType color)171 void SetFillColor(ColorType color) 172 { 173 fillColor_ = color; 174 } 175 176 /** 177 * @brief Obtains the fill color. 178 * 179 * @return Returns the fill color. 180 * @see SetFillColor 181 * @since 1.0 182 * @version 1.0 183 */ GetFillColor()184 ColorType GetFillColor() const 185 { 186 return fillColor_; 187 } 188 189 /** 190 * @brief Sets the opacity. 191 * 192 * The setting takes effect for the entire graph, including the border, line color, and fill color. 193 * 194 * @param opacity Indicates the opacity. The value range is [0, 255]. 195 * @see GetOpacity 196 * @since 1.0 197 * @version 1.0 198 */ SetOpacity(uint8_t opacity)199 void SetOpacity(uint8_t opacity) 200 { 201 opacity_ = opacity; 202 } 203 204 /** 205 * @brief Obtains the opacity. 206 * 207 * @return Returns the opacity. 208 * @see SetOpacity 209 * @since 1.0 210 * @version 1.0 211 */ GetOpacity()212 uint8_t GetOpacity() const 213 { 214 return opacity_; 215 } 216 217 private: 218 PaintStyle style_; 219 ColorType fillColor_; 220 ColorType strokeColor_; 221 uint8_t opacity_; 222 uint16_t strokeWidth_; 223 }; 224 225 /** 226 * @brief Defines a canvas, which is used to draw multiple types of 2D graphs. 227 * 228 * @since 1.0 229 * @version 1.0 230 */ 231 class UICanvas : public UIView { 232 public: 233 /** 234 * @brief A constructor used to create a <b>UICanvas</b> instance. 235 * 236 * @since 1.0 237 * @version 1.0 238 */ UICanvas()239 UICanvas() : startPoint_({ 0, 0 }), path_(nullptr) {} 240 241 /** 242 * @brief A destructor used to delete the <b>UICanvas</b> instance. 243 * 244 * @since 1.0 245 * @version 1.0 246 */ 247 virtual ~UICanvas(); 248 249 /** 250 * @brief Obtains the view type. 251 * 252 * @return Returns the view type. For details, see {@link UIViewType}. 253 * @since 1.0 254 * @version 1.0 255 */ GetViewType()256 UIViewType GetViewType() const override 257 { 258 return UI_CANVAS; 259 } 260 261 /** 262 * @brief Clears the entire canvas. 263 * 264 * @since 1.0 265 * @version 1.0 266 */ 267 void Clear(); 268 269 /** 270 * @brief Sets the coordinates of the start point for drawing a line. For example, if <b>startPoint</b> is 271 * set to {50, 50}, the line is drawn from this set of coordinates on the canvas. 272 * 273 * @param startPoint Indicates the coordinates of the start point. 274 * @see GetStartPosition 275 * @since 1.0 276 * @version 1.0 277 */ SetStartPosition(const Point & startPoint)278 void SetStartPosition(const Point& startPoint) 279 { 280 startPoint_ = startPoint; 281 } 282 283 /** 284 * @brief Obtains the coordinates of the start point of a line. 285 * 286 * @return Returns the coordinates of the start point. 287 * @see SetStartPosition 288 * @since 1.0 289 * @version 1.0 290 */ GetStartPosition()291 const Point& GetStartPosition() const 292 { 293 return startPoint_; 294 } 295 296 /** 297 * @brief Draws a straight line. 298 * 299 * If {@link SetStartPosition} is not used to set the coordinates of the start point of the line, the drawing 300 * starts from the end point of the last line. 301 * 302 * @param endPoint Indicates the end point of the straight line. 303 * @param paint Indicates the straight line style. For details, see {@link Paint}. 304 * @since 1.0 305 * @version 1.0 306 */ 307 void DrawLine(const Point& endPoint, const Paint& paint); 308 309 /** 310 * @brief Draws a straight line from the coordinates of the start point. 311 * 312 * @param startPoint Indicates the coordinates of the start point. 313 * @param endPoint Indicates the coordinates of the end point. 314 * @param paint Indicates the straight line style. For details, see {@link Paint}. 315 * @since 1.0 316 * @version 1.0 317 */ 318 void DrawLine(const Point& startPoint, const Point& endPoint, const Paint& paint); 319 320 /** 321 * @brief Draws a cubic Bezier curve. 322 * 323 * If {@link SetStartPosition} is not used to set the coordinates of the start point of the curve, 324 * the drawing starts from the end point of the last line. 325 * Currently, the opacity cannot be set, and the maximum line width is <b>3</b>. 326 * 327 * @param control1 Indicates the coordinates of the first control point of the cubic Bezier curve. 328 * @param control2 Indicates the coordinates of the second control point of the cubic Bezier curve. 329 * @param endPoint Indicates the coordinates of the end point of the cubic Bezier curve. 330 * @param paint Indicates the curve style. For details, see {@link Paint}. 331 * @since 1.0 332 * @version 1.0 333 */ 334 void DrawCurve(const Point& control1, const Point& control2, const Point& endPoint, const Paint& paint); 335 336 /** 337 * @brief Draws a cubic Bezier curve from the start point coordinates. 338 * 339 * Currently, the opacity cannot be set, and the maximum line width is <b>3</b>. 340 * 341 * @param startPoint Indicates the coordinates of the start point of the cubic Bezier curve. 342 * @param control1 Indicates the coordinates of the first control point of the cubic Bezier curve. 343 * @param control2 Indicates the coordinates of the second control point of the cubic Bezier curve. 344 * @param endPoint Indicates the coordinates of the end point of the cubic Bezier curve. 345 * @param paint Indicates the curve style. For details, see {@link Paint}. 346 * @since 1.0 347 * @version 1.0 348 */ 349 void DrawCurve(const Point& startPoint, const Point& control1, const Point& control2, 350 const Point& endPoint, const Paint& paint); 351 352 /** 353 * @brief Draws a rectangle. 354 * 355 * @param startPoint Indicates the coordinates of the point at the upper left corner of the rectangle. 356 * @param height Indicates the height of the rectangle. 357 * @param width Indicates the width of the rectangle. 358 * @param paint Indicates the rectangle style. For details, see {@link Paint}. 359 * @since 1.0 360 * @version 1.0 361 */ 362 void DrawRect(const Point& startPoint, int16_t height, int16_t width, const Paint& paint); 363 364 /** 365 * @brief Draws a circle. 366 * 367 * @param center Indicates the coordinates of the circle center. 368 * @param radius Indicates the radius of the circle. 369 * @param paint Indicates the circle style. For details, see {@link Paint}. 370 * @since 1.0 371 * @version 1.0 372 */ 373 void DrawCircle(const Point& center, uint16_t radius, const Paint& paint); 374 375 /** 376 * @brief Draws a sector. 377 * 378 * When the start angle is smaller than the end angle, the sector is drawn clockwise. 379 * Otherwise, the sector is drawn counterclockwise. 380 * 381 * @param center Indicates the coordinates of the sector's center. 382 * @param radius Indicates the radius of the sector. 383 * @param startAngle Indicates the start angle of the sector. Value <b>0</b> indicates the 12-o'clock direction, 384 * and <b>90</b> indicates the 3-o'clock direction. 385 * @param endAngle Indicates the end angle of the sector. Value <b>0</b> indicates the 12-o'clock direction, 386 * and <b>90</b> indicates the 3-o'clock direction. 387 * @param paint Indicates the sector style. For details, see {@link Paint}. 388 * @since 1.0 389 * @version 1.0 390 */ 391 void DrawSector(const Point& center, uint16_t radius, int16_t startAngle, int16_t endAngle, const Paint& paint); 392 393 /** 394 * @brief Draws an arc. 395 * 396 * Only stroke is supported. \n 397 * When the start angle is smaller than the end angle, the sector is drawn clockwise. 398 * Otherwise, the sector is drawn counterclockwise. \n 399 * 400 * @param center Indicates the coordinates of the arc's center. 401 * @param radius Indicates the radius of the arc. 402 * @param startAngle Indicates the start angle of the arc. Value <b>0</b> indicates the 12-o'clock direction, 403 * and <b>90</b> indicates the 3-o'clock direction. 404 * @param endAngle Indicates the end angle of the arc. Value <b>0</b> indicates the 12-o'clock direction, 405 * and <b>90</b> indicates the 3-o'clock direction. 406 * @param paint Indicates the arc style. For details, see {@link Paint}. 407 * @since 1.0 408 * @version 1.0 409 */ 410 void DrawArc(const Point& center, uint16_t radius, int16_t startAngle, int16_t endAngle, const Paint& paint); 411 412 /** 413 * @brief Draws an image. 414 * 415 * @param startPoint Indicates the coordinates of the start point. 416 * @param image Indicates the pointer to the image source. 417 * @param paint Indicates the image style. For details, see {@link Paint}. 418 * @since 1.0 419 * @version 1.0 420 */ 421 void DrawImage(const Point& startPoint, const char* image, const Paint& paint); 422 423 /** 424 * @brief Defines the font style. 425 */ 426 struct FontStyle { 427 /** Text direction. For details, see {@link UITextLanguageDirect}. */ 428 UITextLanguageDirect direct; 429 /** Text alignment mode. For details, see {@link UITextLanguageAlignment}. */ 430 UITextLanguageAlignment align; 431 /** Font size */ 432 uint8_t fontSize; 433 /** Letter-spacing */ 434 int16_t letterSpace; 435 /** Font name */ 436 const char* fontName; 437 }; 438 439 /** 440 * @brief Draws text. 441 * 442 * Only fill is supported. \n 443 * If the text length exceeds the value of <b>maxWidth</b>, the text will be truncated. \n 444 * 445 * @param startPoint Indicates the coordinates of the start point. 446 * @param text Indicates the pointer to the text content. 447 * @param maxWidth Indicates the maximum width of the text that can be displayed. If the maximum width is 448 * exceeded, the text is truncated. 449 * @param fontStyle Indicates the text layout and font style. For details, see {@link FontStyle}. 450 * @param paint Indicates the text style. For details, see {@link Paint}. 451 * @since 1.0 452 * @version 1.0 453 */ 454 void DrawLabel(const Point& startPoint, const char* text, uint16_t maxWidth, const FontStyle& fontStyle, 455 const Paint& paint); 456 457 /** 458 * @brief Creates a path. 459 * 460 * A round corner can be used to join two lines. Currently, miter and bevel joints are not supported. 461 * To draw this path, you need to call {@link DrawPath}. 462 * 463 * @since 3.0 464 * @version 5.0 465 */ 466 void BeginPath(); 467 468 /** 469 * @brief Moves the start point of this path to a specified point. 470 * 471 * @param point Indicates the specified point to move to. 472 * @since 3.0 473 * @version 5.0 474 */ 475 void MoveTo(const Point& point); 476 477 /** 478 * @brief Creates a straight line from the end point of this path to a specified point. 479 * 480 * @param point Indicates the coordinates of the specified point. 481 * @since 3.0 482 * @version 5.0 483 */ 484 void LineTo(const Point& point); 485 486 /** 487 * @brief Creates an arc path. 488 * 489 * @param center Indicates the coordinates of the arc's center point. 490 * @param radius Indicates the radius of the arc. 491 * @param startAngle Indicates the start angle of the arc. The value <b>0</b> indicates the 12-o'clock direction, 492 * and <b>90</b> indicates the 3-o'clock direction. 493 * @param endAngle Indicates the end angle of the arc. The value <b>0</b> indicates the 12-o'clock direction, 494 * and <b>90</b> indicates the 3-o'clock direction. 495 * @since 3.0 496 * @version 5.0 497 */ 498 void ArcTo(const Point& center, uint16_t radius, int16_t startAngle, int16_t endAngle); 499 500 /** 501 * @brief Creates a rectangular path. 502 * 503 * @param point Indicates the coordinates of the rectangle's upper left corner. 504 * @param height Indicates the height of the rectangle. 505 * @param width Indicates the width of the rectangle. 506 * @since 3.0 507 * @version 5.0 508 */ 509 void AddRect(const Point& point, int16_t height, int16_t width); 510 511 /** 512 * @brief Closes this path. 513 * 514 * @since 3.0 515 * @version 5.0 516 */ 517 void ClosePath(); 518 519 /** 520 * @brief Draws this path. 521 * 522 * @param paint Indicates the path style. For details, see {@link Paint}. 523 * @since 3.0 524 * @version 5.0 525 */ 526 void DrawPath(const Paint& paint); 527 528 void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 529 530 protected: 531 constexpr static uint8_t MAX_CURVE_WIDTH = 3; 532 533 struct LineParam : public HeapBase { 534 Point start; 535 Point end; 536 }; 537 538 struct CurveParam : public HeapBase { 539 Point start; 540 Point control1; 541 Point control2; 542 Point end; 543 }; 544 545 struct RectParam : public HeapBase { 546 Point start; 547 int16_t height; 548 int16_t width; 549 }; 550 551 struct CircleParam : public HeapBase { 552 Point center; 553 uint16_t radius; 554 }; 555 556 struct ArcParam : public HeapBase { 557 Point center; 558 uint16_t radius; 559 int16_t startAngle; 560 int16_t endAngle; 561 }; 562 563 struct ImageParam : public HeapBase { 564 Point start; 565 uint16_t height; 566 uint16_t width; 567 Image* image; 568 }; 569 570 enum PathCmd { 571 CMD_MOVE_TO, 572 CMD_LINE_TO, 573 CMD_ARC, 574 CMD_CLOSE, 575 }; 576 577 class UICanvasPath : public HeapBase { 578 public: UICanvasPath()579 UICanvasPath() : startPos_({ 0, 0 }), strokeCount_(0) {}; 580 ~UICanvasPath(); 581 List<Point> points_; 582 List<PathCmd> cmd_; 583 List<ArcParam> arcParam_; 584 Point startPos_; 585 uint16_t strokeCount_; 586 }; 587 588 struct PathParam : public HeapBase { 589 UICanvasPath* path; 590 uint16_t count; 591 }; 592 593 struct DrawCmd : public HeapBase { 594 Paint paint; 595 void* param; 596 void(*DrawGraphics)(BufferInfo&, void*, const Paint&, const Rect&, const Rect&, const Style&); 597 void(*DeleteParam)(void *); 598 }; 599 600 Point startPoint_; 601 UICanvasPath* path_; 602 List<DrawCmd> drawCmdList_; 603 DeleteLineParam(void * param)604 static void DeleteLineParam(void* param) 605 { 606 LineParam* lineParam = static_cast<LineParam*>(param); 607 delete lineParam; 608 } 609 DeleteCurveParam(void * param)610 static void DeleteCurveParam(void* param) 611 { 612 CurveParam* curveParam = static_cast<CurveParam*>(param); 613 delete curveParam; 614 } 615 DeleteRectParam(void * param)616 static void DeleteRectParam(void* param) 617 { 618 RectParam* rectParam = static_cast<RectParam*>(param); 619 delete rectParam; 620 } 621 DeleteCircleParam(void * param)622 static void DeleteCircleParam(void* param) 623 { 624 CircleParam* circleParam = static_cast<CircleParam*>(param); 625 delete circleParam; 626 } 627 DeleteArcParam(void * param)628 static void DeleteArcParam(void* param) 629 { 630 ArcParam* arcParam = static_cast<ArcParam*>(param); 631 delete arcParam; 632 } 633 DeleteImageParam(void * param)634 static void DeleteImageParam(void* param) 635 { 636 ImageParam* imageParam = static_cast<ImageParam*>(param); 637 if (imageParam->image != nullptr) { 638 delete imageParam->image; 639 } 640 delete imageParam; 641 } 642 DeleteLabel(void * param)643 static void DeleteLabel(void* param) 644 { 645 UILabel* label = static_cast<UILabel*>(param); 646 delete label; 647 } 648 DeletePathParam(void * param)649 static void DeletePathParam(void* param) 650 { 651 PathParam* pathParam = static_cast<PathParam*>(param); 652 pathParam->path->strokeCount_--; 653 if (pathParam->path->strokeCount_ == 0) { 654 delete pathParam->path; 655 } 656 delete pathParam; 657 } 658 659 static void DoDrawLine(BufferInfo& gfxDstBuffer, 660 void* param, 661 const Paint& paint, 662 const Rect& rect, 663 const Rect& invalidatedArea, 664 const Style& style); 665 static void DoDrawCurve(BufferInfo& gfxDstBuffer, 666 void* param, 667 const Paint& paint, 668 const Rect& rect, 669 const Rect& invalidatedArea, 670 const Style& style); 671 static void DoDrawRect(BufferInfo& gfxDstBuffer, 672 void* param, 673 const Paint& paint, 674 const Rect& rect, 675 const Rect& invalidatedArea, 676 const Style& style); 677 static void DoFillRect(BufferInfo& gfxDstBuffer, 678 void* param, 679 const Paint& paint, 680 const Rect& rect, 681 const Rect& invalidatedArea, 682 const Style& style); 683 static void DoDrawCircle(BufferInfo& gfxDstBuffer, 684 void* param, 685 const Paint& paint, 686 const Rect& rect, 687 const Rect& invalidatedArea, 688 const Style& style); 689 static void DoDrawArc(BufferInfo& gfxDstBuffer, 690 void* param, 691 const Paint& paint, 692 const Rect& rect, 693 const Rect& invalidatedArea, 694 const Style& style); 695 static void DoDrawImage(BufferInfo& gfxDstBuffer, 696 void* param, 697 const Paint& paint, 698 const Rect& rect, 699 const Rect& invalidatedArea, 700 const Style& style); 701 static void DoDrawLabel(BufferInfo& gfxDstBuffer, 702 void* param, 703 const Paint& paint, 704 const Rect& rect, 705 const Rect& invalidatedArea, 706 const Style& style); 707 static void DoDrawPath(BufferInfo& gfxDstBuffer, 708 void* param, 709 const Paint& paint, 710 const Rect& rect, 711 const Rect& invalidatedArea, 712 const Style& style); 713 static void GetAbsolutePosition(const Point& prePoint, const Rect& rect, const Style& style, Point& point); 714 static void DoDrawLineJoin(BufferInfo& gfxDstBuffer, 715 const Point& center, 716 const Rect& invalidatedArea, 717 const Paint& paint); 718 }; 719 } // namespace OHOS 720 #endif // GRAPHIC_LITE_UI_CANVAS_H 721