Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
      3  *               2006 Rob Buis <buis (at) kde.org>
      4  * Copyright (C) 2007-2008 Torch Mobile, Inc.
      5  * Copyright (C) 2013 Google Inc. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef Path_h
     30 #define Path_h
     31 
     32 #include "core/platform/graphics/RoundedRect.h"
     33 #include "core/platform/graphics/WindRule.h"
     34 #include "third_party/skia/include/core/SkPath.h"
     35 #include "wtf/FastAllocBase.h"
     36 #include "wtf/Forward.h"
     37 
     38 class SkPath;
     39 
     40 namespace WebCore {
     41 
     42 class AffineTransform;
     43 class FloatPoint;
     44 class FloatRect;
     45 class FloatSize;
     46 class GraphicsContext;
     47 class StrokeData;
     48 
     49 enum PathElementType {
     50     PathElementMoveToPoint, // The points member will contain 1 value.
     51     PathElementAddLineToPoint, // The points member will contain 1 value.
     52     PathElementAddQuadCurveToPoint, // The points member will contain 2 values.
     53     PathElementAddCurveToPoint, // The points member will contain 3 values.
     54     PathElementCloseSubpath // The points member will contain no values.
     55 };
     56 
     57 // The points in the sturcture are the same as those that would be used with the
     58 // add... method. For example, a line returns the endpoint, while a cubic returns
     59 // two tangent points and the endpoint.
     60 struct PathElement {
     61     PathElementType type;
     62     FloatPoint* points;
     63 };
     64 
     65 typedef void (*PathApplierFunction)(void* info, const PathElement*);
     66 
     67 class Path {
     68     WTF_MAKE_FAST_ALLOCATED;
     69 public:
     70     Path();
     71     ~Path();
     72 
     73     Path(const Path&);
     74     Path& operator=(const Path&);
     75     bool operator==(const Path&) const;
     76 
     77     bool contains(const FloatPoint&, WindRule = RULE_NONZERO) const;
     78     bool strokeContains(const FloatPoint&, const StrokeData&) const;
     79     FloatRect boundingRect() const;
     80     FloatRect strokeBoundingRect(const StrokeData&) const;
     81 
     82     float length() const;
     83     FloatPoint pointAtLength(float length, bool& ok) const;
     84     float normalAngleAtLength(float length, bool& ok) const;
     85     bool pointAndNormalAtLength(float length, FloatPoint&, float&) const;
     86 
     87     void clear();
     88     bool isEmpty() const;
     89     // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
     90     // Note the Path can be empty (isEmpty() == true) and still have a current point.
     91     bool hasCurrentPoint() const;
     92     FloatPoint currentPoint() const;
     93 
     94     WindRule windRule() const;
     95     void setWindRule(const WindRule);
     96 
     97     void moveTo(const FloatPoint&);
     98     void addLineTo(const FloatPoint&);
     99     void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
    100     void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
    101     void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
    102     void closeSubpath();
    103 
    104     void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
    105     void addRect(const FloatRect&);
    106     void addEllipse(const FloatRect&);
    107 
    108     void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii);
    109     void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
    110     void addRoundedRect(const RoundedRect&);
    111 
    112     void translate(const FloatSize&);
    113 
    114     const SkPath& skPath() const { return m_path; }
    115 
    116     void apply(void* info, PathApplierFunction) const;
    117     void transform(const AffineTransform&);
    118 
    119     void addPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
    120     void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
    121 
    122     // Updates the path to the union (inclusive-or) of itself with the given argument.
    123     bool unionPath(const Path& other);
    124 
    125 private:
    126     SkPath m_path;
    127 };
    128 
    129 }
    130 
    131 #endif
    132