Home | History | Annotate | Download | only in canvas
      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 (at) atoker.com>
      5  * Copyright (C) 2008 Eric Seidel <eric (at) webkit.org>
      6  * Copyright (C) 2008 Dirk Schulze <krit (at) 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 
     35 #include "config.h"
     36 #include "core/html/canvas/CanvasPathMethods.h"
     37 
     38 #include "bindings/v8/ExceptionState.h"
     39 #include "core/dom/ExceptionCode.h"
     40 #include "platform/geometry/FloatRect.h"
     41 #include "platform/transforms/AffineTransform.h"
     42 #include "wtf/MathExtras.h"
     43 
     44 namespace WebCore {
     45 
     46 void CanvasPathMethods::closePath()
     47 {
     48     if (m_path.isEmpty())
     49         return;
     50 
     51     FloatRect boundRect = m_path.boundingRect();
     52     if (boundRect.width() || boundRect.height())
     53         m_path.closeSubpath();
     54 }
     55 
     56 void CanvasPathMethods::moveTo(float x, float y)
     57 {
     58     if (!std::isfinite(x) || !std::isfinite(y))
     59         return;
     60     if (!isTransformInvertible())
     61         return;
     62     m_path.moveTo(FloatPoint(x, y));
     63 }
     64 
     65 void CanvasPathMethods::lineTo(float x, float y)
     66 {
     67     if (!std::isfinite(x) || !std::isfinite(y))
     68         return;
     69     if (!isTransformInvertible())
     70         return;
     71 
     72     FloatPoint p1 = FloatPoint(x, y);
     73     if (!m_path.hasCurrentPoint())
     74         m_path.moveTo(p1);
     75     else if (p1 != m_path.currentPoint())
     76         m_path.addLineTo(p1);
     77 }
     78 
     79 void CanvasPathMethods::quadraticCurveTo(float cpx, float cpy, float x, float y)
     80 {
     81     if (!std::isfinite(cpx) || !std::isfinite(cpy) || !std::isfinite(x) || !std::isfinite(y))
     82         return;
     83     if (!isTransformInvertible())
     84         return;
     85     if (!m_path.hasCurrentPoint())
     86         m_path.moveTo(FloatPoint(cpx, cpy));
     87 
     88     FloatPoint p1 = FloatPoint(x, y);
     89     FloatPoint cp = FloatPoint(cpx, cpy);
     90     if (p1 != m_path.currentPoint() || p1 != cp)
     91         m_path.addQuadCurveTo(cp, p1);
     92 }
     93 
     94 void CanvasPathMethods::bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y)
     95 {
     96     if (!std::isfinite(cp1x) || !std::isfinite(cp1y) || !std::isfinite(cp2x) || !std::isfinite(cp2y) || !std::isfinite(x) || !std::isfinite(y))
     97         return;
     98     if (!isTransformInvertible())
     99         return;
    100     if (!m_path.hasCurrentPoint())
    101         m_path.moveTo(FloatPoint(cp1x, cp1y));
    102 
    103     FloatPoint p1 = FloatPoint(x, y);
    104     FloatPoint cp1 = FloatPoint(cp1x, cp1y);
    105     FloatPoint cp2 = FloatPoint(cp2x, cp2y);
    106     if (p1 != m_path.currentPoint() || p1 != cp1 ||  p1 != cp2)
    107         m_path.addBezierCurveTo(cp1, cp2, p1);
    108 }
    109 
    110 void CanvasPathMethods::arcTo(float x1, float y1, float x2, float y2, float r, ExceptionState& exceptionState)
    111 {
    112     if (!std::isfinite(x1) || !std::isfinite(y1) || !std::isfinite(x2) || !std::isfinite(y2) || !std::isfinite(r))
    113         return;
    114 
    115     if (r < 0) {
    116         exceptionState.throwUninformativeAndGenericDOMException(IndexSizeError);
    117         return;
    118     }
    119 
    120     if (!isTransformInvertible())
    121         return;
    122 
    123     FloatPoint p1 = FloatPoint(x1, y1);
    124     FloatPoint p2 = FloatPoint(x2, y2);
    125 
    126     if (!m_path.hasCurrentPoint())
    127         m_path.moveTo(p1);
    128     else if (p1 == m_path.currentPoint() || p1 == p2 || !r)
    129         lineTo(x1, y1);
    130     else
    131         m_path.addArcTo(p1, p2, r);
    132 }
    133 
    134 namespace {
    135 
    136 float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
    137 {
    138     float twoPi = 2 * piFloat;
    139     float newEndAngle = endAngle;
    140     /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arc
    141      * If the anticlockwise argument is false and endAngle-startAngle is equal to or greater than 2pi, or,
    142      * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2pi,
    143      * then the arc is the whole circumference of this ellipse, and the point at startAngle along this circle's circumference,
    144      * measured in radians clockwise from the ellipse's semi-major axis, acts as both the start point and the end point.
    145      */
    146     if (!anticlockwise && endAngle - startAngle >= twoPi)
    147         newEndAngle = startAngle + twoPi;
    148     else if (anticlockwise && startAngle - endAngle >= twoPi)
    149         newEndAngle = startAngle - twoPi;
    150 
    151     /*
    152      * Otherwise, the arc is the path along the circumference of this ellipse from the start point to the end point,
    153      * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise.
    154      * Since the points are on the ellipse, as opposed to being simply angles from zero,
    155      * the arc can never cover an angle greater than 2pi radians.
    156      */
    157     /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the spec does not indicate clearly.
    158      * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*Math.PI, true) to draw circle.
    159      * We preserve backward-compatibility.
    160      */
    161     else if (!anticlockwise && startAngle > endAngle)
    162         newEndAngle = startAngle + (twoPi - fmodf(startAngle - endAngle, twoPi));
    163     else if (anticlockwise && startAngle < endAngle)
    164         newEndAngle = startAngle - (twoPi - fmodf(endAngle - startAngle, twoPi));
    165 
    166     ASSERT(ellipseIsRenderable(startAngle, newEndAngle));
    167     return newEndAngle;
    168 }
    169 
    170 inline void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
    171 {
    172     path->lineTo(p.x(), p.y());
    173 }
    174 
    175 inline FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
    176 {
    177     return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
    178 }
    179 
    180 void canonicalizeAngle(float* startAngle, float* endAngle)
    181 {
    182     // Make 0 <= startAngle < 2*PI
    183     float twoPi = 2 * piFloat;
    184     float newStartAngle = *startAngle;
    185     if (newStartAngle < 0)
    186         newStartAngle = twoPi + fmodf(newStartAngle, -twoPi);
    187     else
    188         newStartAngle = fmodf(newStartAngle, twoPi);
    189 
    190     float delta = newStartAngle - *startAngle;
    191     *startAngle = newStartAngle;
    192     *endAngle = *endAngle + delta;
    193     ASSERT(newStartAngle >= 0 && newStartAngle < twoPi);
    194 }
    195 
    196 /*
    197  * degenerateEllipse() handles a degenerated ellipse using several lines.
    198  *
    199  * Let's see a following example: line to ellipse to line.
    200  *        _--^\
    201  *       (     )
    202  * -----(      )
    203  *            )
    204  *           /--------
    205  *
    206  * If radiusX becomes zero, the ellipse of the example is degenerated.
    207  *         _
    208  *        // P
    209  *       //
    210  * -----//
    211  *      /
    212  *     /--------
    213  *
    214  * To draw the above example, need to get P that is a local maximum point.
    215  * Angles for P are 0.5Pi and 1.5Pi in the ellipse coordinates.
    216  *
    217  * If radiusY becomes zero, the result is as follows.
    218  * -----__
    219  *        --_
    220  *          ----------
    221  *            ``P
    222  * Angles for P are 0 and Pi in the ellipse coordinates.
    223  *
    224  * To handle both cases, degenerateEllipse() lines to start angle, local maximum points(every 0.5Pi), and end angle.
    225  * NOTE: Before ellipse() calls this function, adjustEndAngle() is called, so endAngle - startAngle must be equal to or less than 2Pi.
    226  */
    227 void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
    228 {
    229     ASSERT(ellipseIsRenderable(startAngle, endAngle));
    230     ASSERT(startAngle >= 0 && startAngle < 2 * piFloat);
    231     ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0));
    232 
    233     FloatPoint center(x, y);
    234     AffineTransform rotationMatrix;
    235     rotationMatrix.rotate(rad2deg(rotation));
    236     // 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.
    237     lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, startAngle)));
    238     if ((!radiusX && !radiusY) || startAngle == endAngle)
    239         return;
    240 
    241     float halfPiFloat = piFloat * 0.5;
    242     if (!anticlockwise) {
    243         // startAngle - fmodf(startAngle, halfPiFloat) + halfPiFloat is the one of (0, 0.5Pi, Pi, 1.5Pi, 2Pi)
    244         // that is the closest to startAngle on the clockwise direction.
    245         for (float angle = startAngle - fmodf(startAngle, halfPiFloat) + halfPiFloat; angle < endAngle; angle += halfPiFloat)
    246             lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
    247     } else {
    248         for (float angle = startAngle - fmodf(startAngle, halfPiFloat); angle > endAngle; angle -= halfPiFloat)
    249             lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
    250     }
    251 
    252     lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, endAngle)));
    253 }
    254 
    255 } // namespace
    256 
    257 void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
    258 {
    259     if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
    260         return;
    261 
    262     if (radius < 0) {
    263         exceptionState.throwUninformativeAndGenericDOMException(IndexSizeError);
    264         return;
    265     }
    266 
    267     if (!isTransformInvertible())
    268         return;
    269 
    270     if (!radius || startAngle == endAngle) {
    271         // The arc is empty but we still need to draw the connecting line.
    272         lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle));
    273         return;
    274     }
    275 
    276     canonicalizeAngle(&startAngle, &endAngle);
    277     float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
    278     m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
    279 }
    280 
    281 void CanvasPathMethods::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
    282 {
    283     if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
    284         return;
    285 
    286     if (radiusX < 0 || radiusY < 0) {
    287         exceptionState.throwUninformativeAndGenericDOMException(IndexSizeError);
    288         return;
    289     }
    290 
    291     if (!isTransformInvertible())
    292         return;
    293 
    294     canonicalizeAngle(&startAngle, &endAngle);
    295     float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
    296     if (!radiusX || !radiusY || startAngle == adjustedEndAngle) {
    297         // The ellipse is empty but we still need to draw the connecting line to start point.
    298         degenerateEllipse(this, x, y, radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
    299         return;
    300     }
    301 
    302     m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
    303 }
    304 
    305 void CanvasPathMethods::rect(float x, float y, float width, float height)
    306 {
    307     if (!isTransformInvertible())
    308         return;
    309 
    310     if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std::isfinite(height))
    311         return;
    312 
    313     if (!width && !height) {
    314         m_path.moveTo(FloatPoint(x, y));
    315         return;
    316     }
    317 
    318     m_path.addRect(FloatRect(x, y, width, height));
    319 }
    320 }
    321