CCCanvasRenderingContext2D.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "base/ccMacros.h"
  22. #include "base/CCData.h"
  23. #include "math/CCGeometry.h"
  24. #include <string>
  25. #ifndef OBJC_CLASS
  26. #ifdef __OBJC__
  27. #define OBJC_CLASS(name) @class name
  28. #else
  29. #define OBJC_CLASS(name) class name
  30. #endif
  31. #endif // OBJC_CLASS
  32. OBJC_CLASS(CanvasRenderingContext2DImpl);
  33. NS_CC_BEGIN
  34. class CC_DLL CanvasGradient
  35. {
  36. public:
  37. CanvasGradient();
  38. ~CanvasGradient();
  39. void addColorStop(float offset, const std::string& color);
  40. };
  41. class CC_DLL CanvasRenderingContext2D
  42. {
  43. public:
  44. CanvasRenderingContext2D(float width, float height);
  45. ~CanvasRenderingContext2D();
  46. // Rect
  47. void rect(float x, float y, float width, float height);
  48. void clearRect(float x, float y, float width, float height);
  49. void fillRect(float x, float y, float width, float height);
  50. void fillText(const std::string& text, float x, float y, float maxWidth = -1.0f);
  51. void strokeText(const std::string& text, float x, float y, float maxWidth = -1.0f);
  52. Size measureText(const std::string& text);
  53. CanvasGradient* createLinearGradient(float x0, float y0, float x1, float y1);
  54. void save();
  55. // Paths
  56. void beginPath();
  57. void closePath();
  58. void moveTo(float x, float y);
  59. void lineTo(float x, float y);
  60. void fill();
  61. void stroke();
  62. void restore();
  63. // callback
  64. using CanvasBufferUpdatedCallback = std::function<void(const Data&)>;
  65. void setCanvasBufferUpdatedCallback(const CanvasBufferUpdatedCallback& cb);
  66. void setPremultiply(bool multiply);
  67. // functions for properties
  68. void set__width(float width);
  69. void set__height(float height);
  70. void set_lineWidth(float lineWidth);
  71. void set_lineJoin(const std::string& lineJoin);
  72. void set_lineCap(const std::string& lineCap);
  73. void set_font(const std::string& font);
  74. void set_textAlign(const std::string& textAlign);
  75. void set_textBaseline(const std::string& textBaseline);
  76. void set_fillStyle(const std::string& fillStyle);
  77. void set_strokeStyle(const std::string& strokeStyle);
  78. void set_globalCompositeOperation(const std::string& globalCompositeOperation);
  79. // fill image data into Context2D
  80. void _fillImageData(const Data& imageData, float imageWidth, float imageHeight, float offsetX, float offsetY);
  81. // transform
  82. void translate(float x, float y);
  83. void scale(float x, float y);
  84. void rotate(float angle);
  85. void transform(float a, float b, float c, float d, float e, float f);
  86. void setTransform(float a, float b, float c, float d, float e, float f);
  87. private:
  88. void recreateBufferIfNeeded();
  89. public:
  90. float __width = 0.0f;
  91. float __height = 0.0f;
  92. // Line styles
  93. float _lineWidth = 1.0f;
  94. std::string _lineJoin = "miter";
  95. std::string _lineCap = "butt";
  96. // Text styles
  97. std::string _font = "10px sans-serif";
  98. std::string _textAlign = "start";
  99. std::string _textBaseline = "alphabetic";
  100. // Fill and stroke styles
  101. std::string _fillStyle = "#000";
  102. std::string _strokeStyle = "#000";
  103. // Compositing
  104. std::string _globalCompositeOperation = "source-over";
  105. private:
  106. CanvasBufferUpdatedCallback _canvasBufferUpdatedCB = nullptr;
  107. CanvasRenderingContext2DImpl* _impl = nullptr;
  108. bool _isBufferSizeDirty = true;
  109. bool _premultiply = false;
  110. };
  111. NS_CC_END