CCCanvasRenderingContext2D-win32.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. #include "platform/CCCanvasRenderingContext2D.h"
  2. #include "base/ccTypes.h"
  3. #include "base/csscolorparser.hpp"
  4. #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
  5. #include "cocos/scripting/js-bindings/manual/jsb_platform.h"
  6. #include "platform/CCFileUtils.h"
  7. #include <regex>
  8. using namespace cocos2d;
  9. enum class CanvasTextAlign {
  10. LEFT,
  11. CENTER,
  12. RIGHT
  13. };
  14. enum class CanvasTextBaseline {
  15. TOP,
  16. MIDDLE,
  17. BOTTOM
  18. };
  19. namespace {
  20. void fillRectWithColor(uint8_t* buf, uint32_t totalWidth, uint32_t totalHeight, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
  21. {
  22. assert(x + width <= totalWidth);
  23. assert(y + height <= totalHeight);
  24. uint32_t y0 = y;
  25. uint32_t y1 = y + height;
  26. uint8_t* p;
  27. for (uint32_t offsetY = y0; offsetY < y1; ++offsetY)
  28. {
  29. for (uint32_t offsetX = x; offsetX < (x + width); ++offsetX)
  30. {
  31. p = buf + (totalWidth * offsetY + offsetX) * 4;
  32. *p++ = r;
  33. *p++ = g;
  34. *p++ = b;
  35. *p++ = a;
  36. }
  37. }
  38. }
  39. }
  40. class CanvasRenderingContext2DImpl
  41. {
  42. public:
  43. CanvasRenderingContext2DImpl() : _DC(nullptr)
  44. , _bmp(nullptr)
  45. , _font((HFONT)GetStockObject(DEFAULT_GUI_FONT))
  46. , _wnd(nullptr)
  47. , _savedDC(0)
  48. {
  49. _wnd = nullptr;
  50. HDC hdc = GetDC(_wnd);
  51. _DC = CreateCompatibleDC(hdc);
  52. ReleaseDC(_wnd, hdc);
  53. }
  54. ~CanvasRenderingContext2DImpl()
  55. {
  56. _deleteBitmap();
  57. _removeCustomFont();
  58. if (_DC)
  59. DeleteDC(_DC);
  60. }
  61. void recreateBuffer(float w, float h)
  62. {
  63. _bufferWidth = w;
  64. _bufferHeight = h;
  65. if (_bufferWidth < 1.0f || _bufferHeight < 1.0f)
  66. {
  67. _deleteBitmap();
  68. return;
  69. }
  70. int textureSize = _bufferWidth * _bufferHeight * 4;
  71. uint8_t* data = (uint8_t*)malloc(sizeof(uint8_t) * textureSize);
  72. memset(data, 0x00, textureSize);
  73. _imageData.fastSet(data, textureSize);
  74. _prepareBitmap(_bufferWidth, _bufferHeight);
  75. }
  76. void beginPath()
  77. {
  78. // called: set_lineWidth() -> beginPath() -> moveTo() -> lineTo() -> stroke(), when draw line
  79. _hpen = CreatePen(PS_SOLID, _lineWidth, RGB(255, 255, 255));
  80. // the return value of SelectObject is a handle to the object being replaced, so we should delete them to avoid memory leak
  81. HGDIOBJ hOldPen = SelectObject(_DC, _hpen);
  82. HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
  83. DeleteObject(hOldPen);
  84. DeleteObject(hOldBmp);
  85. SetBkMode(_DC, TRANSPARENT);
  86. }
  87. void closePath()
  88. {
  89. }
  90. void moveTo(float x, float y)
  91. {
  92. MoveToEx(_DC, x, -(y - _bufferHeight - _fontSize), nullptr);
  93. }
  94. void lineTo(float x, float y)
  95. {
  96. LineTo(_DC, x, -(y - _bufferHeight - _fontSize));
  97. }
  98. void stroke()
  99. {
  100. DeleteObject(_hpen);
  101. if (_bufferWidth < 1.0f || _bufferHeight < 1.0f)
  102. return;
  103. _fillTextureData();
  104. }
  105. void saveContext()
  106. {
  107. _savedDC = SaveDC(_DC);
  108. }
  109. void restoreContext()
  110. {
  111. BOOL ret = RestoreDC(_DC, _savedDC);
  112. if (0 == ret)
  113. {
  114. SE_LOGD("CanvasRenderingContext2DImpl restore context failed.\n");
  115. }
  116. }
  117. void clearRect(float x, float y, float w, float h)
  118. {
  119. if (_bufferWidth < 1.0f || _bufferHeight < 1.0f)
  120. return;
  121. if (_imageData.isNull())
  122. return;
  123. recreateBuffer(w, h);
  124. }
  125. void fillRect(float x, float y, float w, float h)
  126. {
  127. if (_bufferWidth < 1.0f || _bufferHeight < 1.0f)
  128. return;
  129. //not filled all Bits in buffer? the buffer length is _bufferWidth * _bufferHeight * 4, but it filled _bufferWidth * _bufferHeight * 3?
  130. uint8_t* buffer = _imageData.getBytes();
  131. if (buffer)
  132. {
  133. uint8_t r = _fillStyle.r * 255.0f;
  134. uint8_t g = _fillStyle.g * 255.0f;
  135. uint8_t b = _fillStyle.b * 255.0f;
  136. uint8_t a = _fillStyle.a * 255.0f;
  137. fillRectWithColor(buffer, (uint32_t)_bufferWidth, (uint32_t)_bufferHeight, (uint32_t)x, (uint32_t)y, (uint32_t)w, (uint32_t)h, r, g, b, a);
  138. }
  139. }
  140. void fillText(const std::string& text, float x, float y, float maxWidth)
  141. {
  142. if (text.empty() || _bufferWidth < 1.0f || _bufferHeight < 1.0f)
  143. return;
  144. SIZE textSize = { 0, 0 };
  145. Point offsetPoint = _convertDrawPoint(Point(x, y), text);
  146. _drawText(text, (int)offsetPoint.x, (int)offsetPoint.y);
  147. _fillTextureData();
  148. }
  149. void strokeText(const std::string& text, float x, float y, float maxWidth)
  150. {
  151. if (text.empty() || _bufferWidth < 1.0f || _bufferHeight < 1.0f)
  152. return;
  153. // REFINE
  154. }
  155. cocos2d::Size measureText(const std::string& text)
  156. {
  157. if (text.empty())
  158. return Size(0.0f, 0.0f);
  159. int bufferLen = 0;
  160. wchar_t * pwszBuffer = _utf8ToUtf16(text, &bufferLen);
  161. SIZE size = _sizeWithText(pwszBuffer, bufferLen);
  162. //SE_LOGD("CanvasRenderingContext2DImpl::measureText: %s, %d, %d\n", text.c_str(), size.cx, size.cy);
  163. CC_SAFE_DELETE_ARRAY(pwszBuffer);
  164. return Size(size.cx, size.cy);
  165. }
  166. void updateFont(const std::string& fontName, float fontSize, bool bold = false)
  167. {
  168. do
  169. {
  170. _fontName = fontName;
  171. _fontSize = fontSize;
  172. std::string fontPath;
  173. LOGFONTA tFont = { 0 };
  174. if (!_fontName.empty())
  175. {
  176. // firstly, try to create font from ttf file
  177. const auto& fontInfoMap = getFontFamilyNameMap();
  178. auto iter = fontInfoMap.find(_fontName);
  179. if (iter != fontInfoMap.end())
  180. {
  181. fontPath = iter->second;
  182. std::string tmpFontPath = fontPath;
  183. int nFindPos = tmpFontPath.rfind("/");
  184. tmpFontPath = &tmpFontPath[nFindPos + 1];
  185. nFindPos = tmpFontPath.rfind(".");
  186. // IDEA: draw ttf failed if font file name not equal font face name
  187. // for example: "DejaVuSansMono-Oblique" not equal "DejaVu Sans Mono" when using DejaVuSansMono-Oblique.ttf
  188. _fontName = tmpFontPath.substr(0, nFindPos);
  189. }
  190. else
  191. {
  192. auto nFindPos = fontName.rfind("/");
  193. if (nFindPos != fontName.npos)
  194. {
  195. if (fontName.length() == nFindPos + 1)
  196. {
  197. _fontName = "";
  198. }
  199. else
  200. {
  201. _fontName = &_fontName[nFindPos + 1];
  202. }
  203. }
  204. }
  205. tFont.lfCharSet = DEFAULT_CHARSET;
  206. strcpy_s(tFont.lfFaceName, LF_FACESIZE, _fontName.c_str());
  207. }
  208. if (_fontSize)
  209. tFont.lfHeight = -_fontSize;
  210. if (bold)
  211. tFont.lfWeight = FW_BOLD;
  212. else
  213. tFont.lfWeight = FW_NORMAL;
  214. // disable Cleartype
  215. tFont.lfQuality = ANTIALIASED_QUALITY;
  216. // delete old font
  217. _removeCustomFont();
  218. if (fontPath.size() > 0)
  219. {
  220. _curFontPath = fontPath;
  221. wchar_t * pwszBuffer = _utf8ToUtf16(_curFontPath);
  222. if (pwszBuffer)
  223. {
  224. if (AddFontResource(pwszBuffer))
  225. {
  226. SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
  227. }
  228. delete[] pwszBuffer;
  229. pwszBuffer = nullptr;
  230. }
  231. }
  232. // create new font
  233. _font = CreateFontIndirectA(&tFont);
  234. if (!_font)
  235. {
  236. // create failed, use default font
  237. SE_LOGE("Failed to create custom font(font name: %s, font size: %f), use default font.\n",
  238. _fontName.c_str(), fontSize);
  239. break;
  240. }
  241. else
  242. {
  243. SelectObject(_DC, _font);
  244. SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
  245. }
  246. } while (0);
  247. }
  248. void setTextAlign(CanvasTextAlign align)
  249. {
  250. _textAlign = align;
  251. }
  252. void setTextBaseline(CanvasTextBaseline baseline)
  253. {
  254. _textBaseLine = baseline;
  255. }
  256. void setFillStyle(float r, float g, float b, float a)
  257. {
  258. _fillStyle.r = r;
  259. _fillStyle.g = g;
  260. _fillStyle.b = b;
  261. _fillStyle.a = a;
  262. }
  263. void setStrokeStyle(float r, float g, float b, float a)
  264. {
  265. _strokeStyle.r = r;
  266. _strokeStyle.g = g;
  267. _strokeStyle.b = b;
  268. _strokeStyle.a = a;
  269. }
  270. void setLineWidth(float lineWidth)
  271. {
  272. _lineWidth = lineWidth;
  273. }
  274. void setPremultiply(bool multiply)
  275. {
  276. _premultiply = multiply;
  277. }
  278. const Data& getDataRef() const
  279. {
  280. return _imageData;
  281. }
  282. HDC _DC;
  283. HBITMAP _bmp;
  284. private:
  285. Data _imageData;
  286. HFONT _font;
  287. HWND _wnd;
  288. HPEN _hpen;
  289. PAINTSTRUCT _paintStruct;
  290. std::string _curFontPath;
  291. int _savedDC;
  292. float _lineWidth = 0.0f;
  293. float _bufferWidth = 0.0f;
  294. float _bufferHeight = 0.0f;
  295. bool _premultiply = true;
  296. std::string _fontName;
  297. int _fontSize;
  298. SIZE _textSize;
  299. CanvasTextAlign _textAlign;
  300. CanvasTextBaseline _textBaseLine;
  301. cocos2d::Color4F _fillStyle;
  302. cocos2d::Color4F _strokeStyle;
  303. TEXTMETRIC _tm;
  304. // change utf-8 string to utf-16, pRetLen is the string length after changing
  305. wchar_t * _utf8ToUtf16(const std::string& str, int * pRetLen = nullptr)
  306. {
  307. wchar_t * pwszBuffer = nullptr;
  308. do
  309. {
  310. if (str.empty())
  311. {
  312. break;
  313. }
  314. int nLen = str.size();
  315. int nBufLen = nLen + 1;
  316. pwszBuffer = new wchar_t[nBufLen];
  317. CC_BREAK_IF(!pwszBuffer);
  318. memset(pwszBuffer, 0, sizeof(wchar_t) * nBufLen);
  319. // str.size() not equal actuallyLen for Chinese char
  320. int actuallyLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), nLen, pwszBuffer, nBufLen);
  321. // SE_LOGE("_utf8ToUtf16, str:%s, strLen:%d, retLen:%d\n", str.c_str(), str.size(), actuallyLen);
  322. if (pRetLen != nullptr) {
  323. *pRetLen = actuallyLen;
  324. }
  325. } while (0);
  326. return pwszBuffer;
  327. }
  328. void _removeCustomFont()
  329. {
  330. HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  331. if (hDefFont != _font)
  332. {
  333. DeleteObject(SelectObject(_DC, hDefFont));
  334. }
  335. // release temp font resource
  336. if (_curFontPath.size() > 0)
  337. {
  338. wchar_t * pwszBuffer = _utf8ToUtf16(_curFontPath);
  339. if (pwszBuffer)
  340. {
  341. RemoveFontResource(pwszBuffer);
  342. SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
  343. delete[] pwszBuffer;
  344. pwszBuffer = nullptr;
  345. }
  346. _curFontPath.clear();
  347. }
  348. }
  349. // x, y offset value
  350. int _drawText(const std::string& text, int x, int y)
  351. {
  352. int nRet = 0;
  353. wchar_t * pwszBuffer = nullptr;
  354. do
  355. {
  356. CC_BREAK_IF(text.empty());
  357. DWORD dwFmt = DT_SINGLELINE | DT_NOPREFIX;
  358. int bufferLen = 0;
  359. pwszBuffer = _utf8ToUtf16(text, &bufferLen);
  360. SIZE newSize = _sizeWithText(pwszBuffer, bufferLen);
  361. _textSize = newSize;
  362. RECT rcText = { 0 };
  363. rcText.right = newSize.cx;
  364. rcText.bottom = newSize.cy;
  365. LONG offsetX = x;
  366. LONG offsetY = y;
  367. if (offsetX || offsetY)
  368. {
  369. OffsetRect(&rcText, offsetX, offsetY);
  370. }
  371. // SE_LOGE("_drawText text,%s size: (%d, %d) offset after convert: (%d, %d) \n", text.c_str(), newSize.cx, newSize.cy, offsetX, offsetY);
  372. SetBkMode(_DC, TRANSPARENT);
  373. SetTextColor(_DC, RGB(255, 255, 255)); // white color
  374. // draw text
  375. nRet = DrawTextW(_DC, pwszBuffer, bufferLen, &rcText, dwFmt);
  376. } while (0);
  377. CC_SAFE_DELETE_ARRAY(pwszBuffer);
  378. return nRet;
  379. }
  380. SIZE _sizeWithText(const wchar_t * pszText, int nLen)
  381. {
  382. SIZE tRet = { 0 };
  383. do
  384. {
  385. CC_BREAK_IF(!pszText || nLen <= 0);
  386. RECT rc = { 0, 0, 0, 0 };
  387. DWORD dwCalcFmt = DT_CALCRECT | DT_NOPREFIX;
  388. // measure text size
  389. DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
  390. tRet.cx = rc.right;
  391. tRet.cy = rc.bottom;
  392. } while (0);
  393. return tRet;
  394. }
  395. void _prepareBitmap(int nWidth, int nHeight)
  396. {
  397. // release bitmap
  398. _deleteBitmap();
  399. if (nWidth > 0 && nHeight > 0)
  400. {
  401. _bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
  402. SelectObject(_DC, _bmp);
  403. }
  404. }
  405. void _deleteBitmap()
  406. {
  407. if (_bmp)
  408. {
  409. DeleteObject(_bmp);
  410. _bmp = nullptr;
  411. }
  412. }
  413. void _fillTextureData()
  414. {
  415. do
  416. {
  417. int dataLen = _bufferWidth * _bufferHeight * 4;
  418. unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen);
  419. CC_BREAK_IF(!dataBuf);
  420. unsigned char* imageBuf = _imageData.getBytes();
  421. CC_BREAK_IF(!imageBuf);
  422. struct
  423. {
  424. BITMAPINFOHEADER bmiHeader;
  425. int mask[4];
  426. } bi = { 0 };
  427. bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
  428. CC_BREAK_IF(!GetDIBits(_DC, _bmp, 0, 0,
  429. nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
  430. // copy pixel data
  431. bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0) ? -bi.bmiHeader.biHeight : bi.bmiHeader.biHeight;
  432. GetDIBits(_DC, _bmp, 0, _bufferHeight, dataBuf,
  433. (LPBITMAPINFO)&bi, DIB_RGB_COLORS);
  434. uint8_t r = _fillStyle.r * 255;
  435. uint8_t g = _fillStyle.g * 255;
  436. uint8_t b = _fillStyle.b * 255;
  437. uint8_t a = _fillStyle.a;
  438. COLORREF textColor = (b << 16 | g << 8 | r) & 0x00ffffff;
  439. COLORREF * pPixel = nullptr;
  440. COLORREF * pImage = nullptr;
  441. if (_premultiply)
  442. {
  443. uint8_t dirtyValue = 0;
  444. for (int y = 0; y < _bufferHeight; ++y)
  445. {
  446. pPixel = (COLORREF *)dataBuf + y * (int)_bufferWidth;
  447. pImage = (COLORREF *)imageBuf + y * (int)_bufferWidth;
  448. for (int x = 0; x < _bufferWidth; ++x)
  449. {
  450. COLORREF& clr = *pPixel;
  451. COLORREF& val = *pImage;
  452. dirtyValue = GetRValue(clr);
  453. // "dirtyValue > 0" means pixel was covered when drawing text
  454. if (dirtyValue > 0)
  455. {
  456. // r = _fillStyle.r * 255 * (dirtyValue / 255) * alpha;
  457. r = _fillStyle.r * dirtyValue * a;
  458. g = _fillStyle.g * dirtyValue * a;
  459. b = _fillStyle.b * dirtyValue * a;
  460. textColor = (b << 16 | g << 8 | r) & 0x00ffffff;
  461. val = ((BYTE)(dirtyValue * a) << 24) | textColor;
  462. }
  463. ++pPixel;
  464. ++pImage;
  465. }
  466. }
  467. }
  468. else
  469. {
  470. for (int y = 0; y < _bufferHeight; ++y)
  471. {
  472. pPixel = (COLORREF *)dataBuf + y * (int)_bufferWidth;
  473. pImage = (COLORREF *)imageBuf + y * (int)_bufferWidth;
  474. for (int x = 0; x < _bufferWidth; ++x)
  475. {
  476. COLORREF& clr = *pPixel;
  477. COLORREF& val = *pImage;
  478. // Because text is drawn in white color, and background color is black,
  479. // so the red value is equal to alpha value. And we should keep this value
  480. // as it includes anti-atlas information.
  481. uint8_t alpha = GetRValue(clr);
  482. if (alpha > 0)
  483. {
  484. val = (alpha << 24) | textColor;
  485. }
  486. ++pPixel;
  487. ++pImage;
  488. }
  489. }
  490. }
  491. free(dataBuf);
  492. } while (0);
  493. }
  494. Point _convertDrawPoint(Point point, std::string text) {
  495. Size textSize = measureText(text);
  496. if (_textAlign == CanvasTextAlign::CENTER)
  497. {
  498. point.x -= textSize.width / 2.0f;
  499. }
  500. else if (_textAlign == CanvasTextAlign::RIGHT)
  501. {
  502. point.x -= textSize.width;
  503. }
  504. if (_textBaseLine == CanvasTextBaseline::TOP)
  505. {
  506. point.y += _fontSize;
  507. }
  508. else if (_textBaseLine == CanvasTextBaseline::MIDDLE)
  509. {
  510. point.y += _fontSize / 2.0f;
  511. }
  512. // Since the web platform cannot get the baseline of the font, an additive offset is performed for all platforms.
  513. // That's why we should add baseline back again on other platforms
  514. GetTextMetrics(_DC, &_tm);
  515. point.y -= _tm.tmAscent;
  516. return point;
  517. }
  518. };
  519. NS_CC_BEGIN
  520. CanvasGradient::CanvasGradient()
  521. {
  522. //SE_LOGD("CanvasGradient constructor: %p\n", this);
  523. }
  524. CanvasGradient::~CanvasGradient()
  525. {
  526. //SE_LOGD("CanvasGradient destructor: %p\n", this);
  527. }
  528. void CanvasGradient::addColorStop(float offset, const std::string& color)
  529. {
  530. //SE_LOGD("CanvasGradient::addColorStop: %p\n", this);
  531. }
  532. // CanvasRenderingContext2D
  533. CanvasRenderingContext2D::CanvasRenderingContext2D(float width, float height)
  534. : __width(width)
  535. , __height(height)
  536. {
  537. //SE_LOGD("CanvasRenderingContext2D constructor: %p, width: %f, height: %f\n", this, width, height);
  538. _impl = new CanvasRenderingContext2DImpl();
  539. recreateBufferIfNeeded();
  540. }
  541. CanvasRenderingContext2D::~CanvasRenderingContext2D()
  542. {
  543. //SE_LOGD("CanvasRenderingContext2D destructor: %p\n", this);
  544. delete _impl;
  545. }
  546. void CanvasRenderingContext2D::recreateBufferIfNeeded()
  547. {
  548. if (_isBufferSizeDirty)
  549. {
  550. _isBufferSizeDirty = false;
  551. //SE_LOGD("Recreate buffer %p, w: %f, h:%f\n", this, __width, __height);
  552. _impl->recreateBuffer(__width, __height);
  553. if (_canvasBufferUpdatedCB != nullptr)
  554. _canvasBufferUpdatedCB(_impl->getDataRef());
  555. }
  556. }
  557. void CanvasRenderingContext2D::clearRect(float x, float y, float width, float height)
  558. {
  559. //SE_LOGD("CanvasRenderingContext2D::clearRect: %p, %f, %f, %f, %f\n", this, x, y, width, height);
  560. recreateBufferIfNeeded();
  561. _impl->clearRect(x, y, width, height);
  562. }
  563. void CanvasRenderingContext2D::fillRect(float x, float y, float width, float height)
  564. {
  565. recreateBufferIfNeeded();
  566. _impl->fillRect(x, y, width, height);
  567. if (_canvasBufferUpdatedCB != nullptr)
  568. _canvasBufferUpdatedCB(_impl->getDataRef());
  569. }
  570. void CanvasRenderingContext2D::fillText(const std::string& text, float x, float y, float maxWidth)
  571. {
  572. //SE_LOGD("CanvasRenderingContext2D::fillText: %s, offset: (%f, %f), %f\n", text.c_str(), x, y, maxWidth);
  573. if (text.empty())
  574. return;
  575. recreateBufferIfNeeded();
  576. _impl->fillText(text, x, y, maxWidth);
  577. if (_canvasBufferUpdatedCB != nullptr)
  578. _canvasBufferUpdatedCB(_impl->getDataRef());
  579. }
  580. void CanvasRenderingContext2D::strokeText(const std::string& text, float x, float y, float maxWidth)
  581. {
  582. //SE_LOGD("CanvasRenderingContext2D::strokeText: %s, %f, %f, %f\n", text.c_str(), x, y, maxWidth);
  583. if (text.empty())
  584. return;
  585. recreateBufferIfNeeded();
  586. _impl->strokeText(text, x, y, maxWidth);
  587. if (_canvasBufferUpdatedCB != nullptr)
  588. _canvasBufferUpdatedCB(_impl->getDataRef());
  589. }
  590. cocos2d::Size CanvasRenderingContext2D::measureText(const std::string& text)
  591. {
  592. //SE_LOGD("CanvasRenderingContext2D::measureText: %s\n", text.c_str());
  593. return _impl->measureText(text);
  594. }
  595. CanvasGradient* CanvasRenderingContext2D::createLinearGradient(float x0, float y0, float x1, float y1)
  596. {
  597. return nullptr;
  598. }
  599. void CanvasRenderingContext2D::save()
  600. {
  601. //SE_LOGD("CanvasRenderingContext2D::save\n");
  602. _impl->saveContext();
  603. }
  604. void CanvasRenderingContext2D::beginPath()
  605. {
  606. //SE_LOGD("\n-----------begin------------------\nCanvasRenderingContext2D::beginPath\n");
  607. _impl->beginPath();
  608. }
  609. void CanvasRenderingContext2D::closePath()
  610. {
  611. //SE_LOGD("CanvasRenderingContext2D::closePath\n");
  612. _impl->closePath();
  613. }
  614. void CanvasRenderingContext2D::moveTo(float x, float y)
  615. {
  616. //SE_LOGD("CanvasRenderingContext2D::moveTo\n");
  617. _impl->moveTo(x, y);
  618. }
  619. void CanvasRenderingContext2D::lineTo(float x, float y)
  620. {
  621. //SE_LOGD("CanvasRenderingContext2D::lineTo\n");
  622. _impl->lineTo(x, y);
  623. }
  624. void CanvasRenderingContext2D::stroke()
  625. {
  626. //SE_LOGD("CanvasRenderingContext2D::stroke\n");
  627. _impl->stroke();
  628. if (_canvasBufferUpdatedCB != nullptr)
  629. _canvasBufferUpdatedCB(_impl->getDataRef());
  630. }
  631. void CanvasRenderingContext2D::restore()
  632. {
  633. //SE_LOGD("CanvasRenderingContext2D::restore\n");
  634. _impl->restoreContext();
  635. }
  636. void CanvasRenderingContext2D::setCanvasBufferUpdatedCallback(const CanvasBufferUpdatedCallback& cb)
  637. {
  638. _canvasBufferUpdatedCB = cb;
  639. }
  640. void CanvasRenderingContext2D::setPremultiply(bool multiply)
  641. {
  642. _impl->setPremultiply(_premultiply);
  643. }
  644. void CanvasRenderingContext2D::set__width(float width)
  645. {
  646. //SE_LOGD("CanvasRenderingContext2D::set__width: %f\n", width);
  647. __width = width;
  648. _isBufferSizeDirty = true;
  649. recreateBufferIfNeeded();
  650. }
  651. void CanvasRenderingContext2D::set__height(float height)
  652. {
  653. //SE_LOGD("CanvasRenderingContext2D::set__height: %f\n", height);
  654. __height = height;
  655. _isBufferSizeDirty = true;
  656. recreateBufferIfNeeded();
  657. }
  658. void CanvasRenderingContext2D::set_lineWidth(float lineWidth)
  659. {
  660. //SE_LOGD("CanvasRenderingContext2D::set_lineWidth %d\n", lineWidth);
  661. _lineWidth = lineWidth;
  662. _impl->setLineWidth(lineWidth);
  663. }
  664. void CanvasRenderingContext2D::set_lineCap(const std::string& lineCap)
  665. {
  666. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  667. }
  668. void CanvasRenderingContext2D::set_lineJoin(const std::string& lineJoin)
  669. {
  670. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  671. }
  672. void CanvasRenderingContext2D::fill()
  673. {
  674. // SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  675. }
  676. void CanvasRenderingContext2D::rect(float x, float y, float w, float h)
  677. {
  678. // SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  679. }
  680. void CanvasRenderingContext2D::set_font(const std::string& font)
  681. {
  682. if (_font != font)
  683. {
  684. _font = font;
  685. std::string boldStr;
  686. std::string fontName = "Arial";
  687. std::string fontSizeStr = "30";
  688. // support get font name from `60px American` or `60px "American abc-abc_abc"`
  689. std::regex re("(bold)?\\s*((\\d+)([\\.]\\d+)?)px\\s+([\\w-]+|\"[\\w -]+\"$)");
  690. std::match_results<std::string::const_iterator> results;
  691. if (std::regex_search(_font.cbegin(), _font.cend(), results, re))
  692. {
  693. boldStr = results[1].str();
  694. fontSizeStr = results[2].str();
  695. fontName = results[5].str();
  696. }
  697. float fontSize = atof(fontSizeStr.c_str());
  698. //SE_LOGD("CanvasRenderingContext2D::set_font: %s, Size: %f, isBold: %b\n", fontName.c_str(), fontSize, !boldStr.empty());
  699. _impl->updateFont(fontName, fontSize, !boldStr.empty());
  700. }
  701. }
  702. void CanvasRenderingContext2D::set_textAlign(const std::string& textAlign)
  703. {
  704. //SE_LOGD("CanvasRenderingContext2D::set_textAlign: %s\n", textAlign.c_str());
  705. if (textAlign == "left")
  706. {
  707. _impl->setTextAlign(CanvasTextAlign::LEFT);
  708. }
  709. else if (textAlign == "center" || textAlign == "middle")
  710. {
  711. _impl->setTextAlign(CanvasTextAlign::CENTER);
  712. }
  713. else if (textAlign == "right")
  714. {
  715. _impl->setTextAlign(CanvasTextAlign::RIGHT);
  716. }
  717. else
  718. {
  719. assert(false);
  720. }
  721. }
  722. void CanvasRenderingContext2D::set_textBaseline(const std::string& textBaseline)
  723. {
  724. //SE_LOGD("CanvasRenderingContext2D::set_textBaseline: %s\n", textBaseline.c_str());
  725. if (textBaseline == "top")
  726. {
  727. _impl->setTextBaseline(CanvasTextBaseline::TOP);
  728. }
  729. else if (textBaseline == "middle")
  730. {
  731. _impl->setTextBaseline(CanvasTextBaseline::MIDDLE);
  732. }
  733. else if (textBaseline == "bottom" || textBaseline == "alphabetic") //REFINE:, how to deal with alphabetic, currently we handle it as bottom mode.
  734. {
  735. _impl->setTextBaseline(CanvasTextBaseline::BOTTOM);
  736. }
  737. else
  738. {
  739. assert(false);
  740. }
  741. }
  742. void CanvasRenderingContext2D::set_fillStyle(const std::string& fillStyle)
  743. {
  744. CSSColorParser::Color color = CSSColorParser::parse(fillStyle);
  745. _impl->setFillStyle(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a);
  746. //SE_LOGD("CanvasRenderingContext2D::set_fillStyle: %s, (%d, %d, %d, %f)\n", fillStyle.c_str(), color.r, color.g, color.b, color.a);
  747. }
  748. void CanvasRenderingContext2D::set_strokeStyle(const std::string& strokeStyle)
  749. {
  750. CSSColorParser::Color color = CSSColorParser::parse(strokeStyle);
  751. _impl->setStrokeStyle(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a);
  752. }
  753. void CanvasRenderingContext2D::set_globalCompositeOperation(const std::string& globalCompositeOperation)
  754. {
  755. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  756. }
  757. void CanvasRenderingContext2D::_fillImageData(const Data& imageData, float imageWidth, float imageHeight, float offsetX, float offsetY)
  758. {
  759. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  760. }
  761. // transform
  762. //REFINE:
  763. void CanvasRenderingContext2D::translate(float x, float y)
  764. {
  765. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  766. }
  767. void CanvasRenderingContext2D::scale(float x, float y)
  768. {
  769. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  770. }
  771. void CanvasRenderingContext2D::rotate(float angle)
  772. {
  773. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  774. }
  775. void CanvasRenderingContext2D::transform(float a, float b, float c, float d, float e, float f)
  776. {
  777. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  778. }
  779. void CanvasRenderingContext2D::setTransform(float a, float b, float c, float d, float e, float f)
  780. {
  781. //SE_LOGE("%s isn't implemented!\n", __FUNCTION__);
  782. }
  783. NS_CC_END