1/*
2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
3 *               2006 Rob Buis <buis@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 "platform/PlatformExport.h"
33#include "platform/geometry/RoundedRect.h"
34#include "platform/graphics/GraphicsTypes.h"
35#include "third_party/skia/include/core/SkPath.h"
36#include "third_party/skia/include/core/SkPathMeasure.h"
37#include "wtf/FastAllocBase.h"
38#include "wtf/Forward.h"
39
40class SkPath;
41
42namespace blink {
43
44class AffineTransform;
45class FloatPoint;
46class FloatRect;
47class FloatSize;
48class StrokeData;
49
50enum PathElementType {
51    PathElementMoveToPoint, // The points member will contain 1 value.
52    PathElementAddLineToPoint, // The points member will contain 1 value.
53    PathElementAddQuadCurveToPoint, // The points member will contain 2 values.
54    PathElementAddCurveToPoint, // The points member will contain 3 values.
55    PathElementCloseSubpath // The points member will contain no values.
56};
57
58// The points in the structure are the same as those that would be used with the
59// add... method. For example, a line returns the endpoint, while a cubic returns
60// two tangent points and the endpoint.
61struct PathElement {
62    PathElementType type;
63    FloatPoint* points;
64};
65
66typedef void (*PathApplierFunction)(void* info, const PathElement*);
67
68class PLATFORM_EXPORT Path {
69    WTF_MAKE_FAST_ALLOCATED;
70public:
71    Path();
72    ~Path();
73
74    Path(const Path&);
75    Path& operator=(const Path&);
76    bool operator==(const Path&) const;
77
78    bool contains(const FloatPoint&, WindRule = RULE_NONZERO) const;
79    bool strokeContains(const FloatPoint&, const StrokeData&) const;
80    FloatRect boundingRect() const;
81    FloatRect strokeBoundingRect(const StrokeData&) const;
82
83    float length() const;
84    FloatPoint pointAtLength(float length, bool& ok) const;
85    float normalAngleAtLength(float length, bool& ok) const;
86    bool pointAndNormalAtLength(float length, FloatPoint&, float&) const;
87
88    // Helper for computing a sequence of positions and normals (normal angles) on a path.
89    // The best possible access pattern will be one where the |length| value is
90    // strictly increasing.
91    // For other access patterns, performance will vary depending on curvature
92    // and number of segments, but should never be worse than that of the
93    // state-less method on Path.
94    class PLATFORM_EXPORT PositionCalculator {
95        WTF_MAKE_NONCOPYABLE(PositionCalculator);
96    public:
97        explicit PositionCalculator(const Path&);
98
99        bool pointAndNormalAtLength(float length, FloatPoint&, float&);
100
101    private:
102        SkPath m_path;
103        SkPathMeasure m_pathMeasure;
104        SkScalar m_accumulatedLength;
105    };
106
107    void clear();
108    bool isEmpty() const;
109    // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
110    // Note the Path can be empty (isEmpty() == true) and still have a current point.
111    bool hasCurrentPoint() const;
112    FloatPoint currentPoint() const;
113
114    WindRule windRule() const;
115    void setWindRule(const WindRule);
116
117    void moveTo(const FloatPoint&);
118    void addLineTo(const FloatPoint&);
119    void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
120    void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
121    void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
122    void closeSubpath();
123
124    void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
125    void addRect(const FloatRect&);
126    void addEllipse(const FloatPoint&, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise);
127    void addEllipse(const FloatRect&);
128
129    void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii);
130    void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
131    void addRoundedRect(const RoundedRect&);
132
133    void addPath(const Path&, const AffineTransform&);
134
135    void translate(const FloatSize&);
136
137    const SkPath& skPath() const { return m_path; }
138
139    void apply(void* info, PathApplierFunction) const;
140    void transform(const AffineTransform&);
141
142    void addPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
143    void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
144
145    bool subtractPath(const Path&);
146    bool intersectPath(const Path&);
147
148    // Updates the path to the union (inclusive-or) of itself with the given argument.
149    bool unionPath(const Path& other);
150
151private:
152    void addEllipse(const FloatPoint&, float radiusX, float radiusY, float startAngle, float endAngle, bool anticlockwise);
153
154    SkPath m_path;
155};
156
157#if ENABLE(ASSERT)
158PLATFORM_EXPORT bool ellipseIsRenderable(float startAngle, float endAngle);
159#endif
160
161} // namespace blink
162
163#endif
164