PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/WebKit/Source/modules/canvas2d/CanvasPathMethods.cpp

https://gitlab.com/0072016/Facebook-SDK-
C++ | 325 lines | 191 code | 46 blank | 88 comment | 108 complexity | 91eb58cdeb66c6e738b20c9a19870ed1 MD5 | raw file
  1. /*
  2. * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
  4. * Copyright (C) 2007 Alp Toker <alp@atoker.com>
  5. * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
  6. * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
  7. * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
  8. * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
  9. * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  26. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  30. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include "modules/canvas2d/CanvasPathMethods.h"
  35. #include "bindings/core/v8/ExceptionState.h"
  36. #include "core/dom/ExceptionCode.h"
  37. #include "platform/geometry/FloatRect.h"
  38. #include "platform/transforms/AffineTransform.h"
  39. #include "wtf/MathExtras.h"
  40. namespace blink {
  41. void CanvasPathMethods::closePath()
  42. {
  43. if (m_path.isEmpty())
  44. return;
  45. FloatRect boundRect = m_path.boundingRect();
  46. if (boundRect.width() || boundRect.height())
  47. m_path.closeSubpath();
  48. }
  49. void CanvasPathMethods::moveTo(float x, float y)
  50. {
  51. if (!std::isfinite(x) || !std::isfinite(y))
  52. return;
  53. if (!isTransformInvertible())
  54. return;
  55. m_path.moveTo(FloatPoint(x, y));
  56. }
  57. void CanvasPathMethods::lineTo(float x, float y)
  58. {
  59. if (!std::isfinite(x) || !std::isfinite(y))
  60. return;
  61. if (!isTransformInvertible())
  62. return;
  63. FloatPoint p1 = FloatPoint(x, y);
  64. if (!m_path.hasCurrentPoint())
  65. m_path.moveTo(p1);
  66. else if (p1 != m_path.currentPoint())
  67. m_path.addLineTo(p1);
  68. }
  69. void CanvasPathMethods::quadraticCurveTo(float cpx, float cpy, float x, float y)
  70. {
  71. if (!std::isfinite(cpx) || !std::isfinite(cpy) || !std::isfinite(x) || !std::isfinite(y))
  72. return;
  73. if (!isTransformInvertible())
  74. return;
  75. if (!m_path.hasCurrentPoint())
  76. m_path.moveTo(FloatPoint(cpx, cpy));
  77. FloatPoint p1 = FloatPoint(x, y);
  78. FloatPoint cp = FloatPoint(cpx, cpy);
  79. if (p1 != m_path.currentPoint() || p1 != cp)
  80. m_path.addQuadCurveTo(cp, p1);
  81. }
  82. void CanvasPathMethods::bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y)
  83. {
  84. if (!std::isfinite(cp1x) || !std::isfinite(cp1y) || !std::isfinite(cp2x) || !std::isfinite(cp2y) || !std::isfinite(x) || !std::isfinite(y))
  85. return;
  86. if (!isTransformInvertible())
  87. return;
  88. if (!m_path.hasCurrentPoint())
  89. m_path.moveTo(FloatPoint(cp1x, cp1y));
  90. FloatPoint p1 = FloatPoint(x, y);
  91. FloatPoint cp1 = FloatPoint(cp1x, cp1y);
  92. FloatPoint cp2 = FloatPoint(cp2x, cp2y);
  93. if (p1 != m_path.currentPoint() || p1 != cp1 || p1 != cp2)
  94. m_path.addBezierCurveTo(cp1, cp2, p1);
  95. }
  96. void CanvasPathMethods::arcTo(float x1, float y1, float x2, float y2, float r, ExceptionState& exceptionState)
  97. {
  98. if (!std::isfinite(x1) || !std::isfinite(y1) || !std::isfinite(x2) || !std::isfinite(y2) || !std::isfinite(r))
  99. return;
  100. if (r < 0) {
  101. exceptionState.throwDOMException(IndexSizeError, "The radius provided (" + String::number(r) + ") is negative.");
  102. return;
  103. }
  104. if (!isTransformInvertible())
  105. return;
  106. FloatPoint p1 = FloatPoint(x1, y1);
  107. FloatPoint p2 = FloatPoint(x2, y2);
  108. if (!m_path.hasCurrentPoint())
  109. m_path.moveTo(p1);
  110. else if (p1 == m_path.currentPoint() || p1 == p2 || !r)
  111. lineTo(x1, y1);
  112. else
  113. m_path.addArcTo(p1, p2, r);
  114. }
  115. namespace {
  116. float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
  117. {
  118. float newEndAngle = endAngle;
  119. /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arc
  120. * If the anticlockwise argument is false and endAngle-startAngle is equal to or greater than 2pi, or,
  121. * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2pi,
  122. * then the arc is the whole circumference of this ellipse, and the point at startAngle along this circle's circumference,
  123. * measured in radians clockwise from the ellipse's semi-major axis, acts as both the start point and the end point.
  124. */
  125. if (!anticlockwise && endAngle - startAngle >= twoPiFloat)
  126. newEndAngle = startAngle + twoPiFloat;
  127. else if (anticlockwise && startAngle - endAngle >= twoPiFloat)
  128. newEndAngle = startAngle - twoPiFloat;
  129. /*
  130. * Otherwise, the arc is the path along the circumference of this ellipse from the start point to the end point,
  131. * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise.
  132. * Since the points are on the ellipse, as opposed to being simply angles from zero,
  133. * the arc can never cover an angle greater than 2pi radians.
  134. */
  135. /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the spec does not indicate clearly.
  136. * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*Math.PI, true) to draw circle.
  137. * We preserve backward-compatibility.
  138. */
  139. else if (!anticlockwise && startAngle > endAngle)
  140. newEndAngle = startAngle + (twoPiFloat - fmodf(startAngle - endAngle, twoPiFloat));
  141. else if (anticlockwise && startAngle < endAngle)
  142. newEndAngle = startAngle - (twoPiFloat - fmodf(endAngle - startAngle, twoPiFloat));
  143. ASSERT(ellipseIsRenderable(startAngle, newEndAngle));
  144. return newEndAngle;
  145. }
  146. inline void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
  147. {
  148. path->lineTo(p.x(), p.y());
  149. }
  150. inline FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
  151. {
  152. return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
  153. }
  154. void canonicalizeAngle(float* startAngle, float* endAngle)
  155. {
  156. // Make 0 <= startAngle < 2*PI
  157. float newStartAngle = fmodf(*startAngle, twoPiFloat);
  158. if (newStartAngle < 0) {
  159. newStartAngle += twoPiFloat;
  160. // Check for possible catastrophic cancellation in cases where
  161. // newStartAngle was a tiny negative number (c.f. crbug.com/503422)
  162. if (newStartAngle >= twoPiFloat)
  163. newStartAngle -= twoPiFloat;
  164. }
  165. float delta = newStartAngle - *startAngle;
  166. *startAngle = newStartAngle;
  167. *endAngle = *endAngle + delta;
  168. ASSERT(newStartAngle >= 0 && newStartAngle < twoPiFloat);
  169. }
  170. /*
  171. * degenerateEllipse() handles a degenerated ellipse using several lines.
  172. *
  173. * Let's see a following example: line to ellipse to line.
  174. * _--^\
  175. * ( )
  176. * -----( )
  177. * )
  178. * /--------
  179. *
  180. * If radiusX becomes zero, the ellipse of the example is degenerated.
  181. * _
  182. * // P
  183. * //
  184. * -----//
  185. * /
  186. * /--------
  187. *
  188. * To draw the above example, need to get P that is a local maximum point.
  189. * Angles for P are 0.5Pi and 1.5Pi in the ellipse coordinates.
  190. *
  191. * If radiusY becomes zero, the result is as follows.
  192. * -----__
  193. * --_
  194. * ----------
  195. * ``P
  196. * Angles for P are 0 and Pi in the ellipse coordinates.
  197. *
  198. * To handle both cases, degenerateEllipse() lines to start angle, local maximum points(every 0.5Pi), and end angle.
  199. * NOTE: Before ellipse() calls this function, adjustEndAngle() is called, so endAngle - startAngle must be equal to or less than 2Pi.
  200. */
  201. void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
  202. {
  203. ASSERT(ellipseIsRenderable(startAngle, endAngle));
  204. ASSERT(startAngle >= 0 && startAngle < twoPiFloat);
  205. ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0));
  206. FloatPoint center(x, y);
  207. AffineTransform rotationMatrix;
  208. rotationMatrix.rotateRadians(rotation);
  209. // First, if the object's path has any subpaths, then the method must add a straight line from the last point in the subpath to the start point of the arc.
  210. lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, startAngle)));
  211. if ((!radiusX && !radiusY) || startAngle == endAngle)
  212. return;
  213. if (!anticlockwise) {
  214. // startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat is the one of (0, 0.5Pi, Pi, 1.5Pi, 2Pi)
  215. // that is the closest to startAngle on the clockwise direction.
  216. for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat; angle < endAngle; angle += piOverTwoFloat)
  217. lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
  218. } else {
  219. for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat); angle > endAngle; angle -= piOverTwoFloat)
  220. lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
  221. }
  222. lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, endAngle)));
  223. }
  224. } // namespace
  225. void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
  226. {
  227. if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
  228. return;
  229. if (radius < 0) {
  230. exceptionState.throwDOMException(IndexSizeError, "The radius provided (" + String::number(radius) + ") is negative.");
  231. return;
  232. }
  233. if (!isTransformInvertible())
  234. return;
  235. if (!radius || startAngle == endAngle) {
  236. // The arc is empty but we still need to draw the connecting line.
  237. lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle));
  238. return;
  239. }
  240. canonicalizeAngle(&startAngle, &endAngle);
  241. float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
  242. m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
  243. }
  244. void CanvasPathMethods::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
  245. {
  246. if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
  247. return;
  248. if (radiusX < 0) {
  249. exceptionState.throwDOMException(IndexSizeError, "The major-axis radius provided (" + String::number(radiusX) + ") is negative.");
  250. return;
  251. }
  252. if (radiusY < 0) {
  253. exceptionState.throwDOMException(IndexSizeError, "The minor-axis radius provided (" + String::number(radiusY) + ") is negative.");
  254. return;
  255. }
  256. if (!isTransformInvertible())
  257. return;
  258. canonicalizeAngle(&startAngle, &endAngle);
  259. float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
  260. if (!radiusX || !radiusY || startAngle == adjustedEndAngle) {
  261. // The ellipse is empty but we still need to draw the connecting line to start point.
  262. degenerateEllipse(this, x, y, radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
  263. return;
  264. }
  265. m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
  266. }
  267. void CanvasPathMethods::rect(float x, float y, float width, float height)
  268. {
  269. if (!isTransformInvertible())
  270. return;
  271. if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std::isfinite(height))
  272. return;
  273. if (!width && !height) {
  274. m_path.moveTo(FloatPoint(x, y));
  275. return;
  276. }
  277. m_path.addRect(FloatRect(x, y, width, height));
  278. }
  279. } // namespace blink