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 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef Path_h
29#define Path_h
30
31#include "WindRule.h"
32#include <wtf/FastAllocBase.h>
33#include <wtf/Forward.h>
34
35#if USE(CG)
36typedef struct CGPath PlatformPath;
37#elif PLATFORM(QT)
38#include <qpainterpath.h>
39typedef QPainterPath PlatformPath;
40#elif USE(CAIRO)
41namespace WebCore {
42class CairoPath;
43}
44typedef WebCore::CairoPath PlatformPath;
45#elif USE(WINGDI)
46namespace WebCore {
47    class PlatformPath;
48}
49typedef WebCore::PlatformPath PlatformPath;
50#elif PLATFORM(BLACKBERRY)
51namespace BlackBerry { namespace Platform { namespace Graphics {
52    class Path;
53} } }
54typedef BlackBerry::Platform::Graphics::Path PlatformPath;
55#else
56typedef void PlatformPath;
57#endif
58
59#if PLATFORM(QT)
60/* QPainterPath is valued based */
61typedef PlatformPath PlatformPathPtr;
62#else
63typedef PlatformPath* PlatformPathPtr;
64#endif
65
66namespace WebCore {
67
68    class AffineTransform;
69    class FloatPoint;
70    class FloatRect;
71    class FloatSize;
72    class GraphicsContext;
73    class RoundedRect;
74    class StrokeStyleApplier;
75
76    enum PathElementType {
77        PathElementMoveToPoint, // The points member will contain 1 value.
78        PathElementAddLineToPoint, // The points member will contain 1 value.
79        PathElementAddQuadCurveToPoint, // The points member will contain 2 values.
80        PathElementAddCurveToPoint, // The points member will contain 3 values.
81        PathElementCloseSubpath // The points member will contain no values.
82    };
83
84    // The points in the sturcture are the same as those that would be used with the
85    // add... method. For example, a line returns the endpoint, while a cubic returns
86    // two tangent points and the endpoint.
87    struct PathElement {
88        PathElementType type;
89        FloatPoint* points;
90    };
91
92    typedef void (*PathApplierFunction)(void* info, const PathElement*);
93
94    class Path {
95        WTF_MAKE_FAST_ALLOCATED;
96    public:
97        Path();
98        ~Path();
99
100        Path(const Path&);
101        Path& operator=(const Path&);
102
103        bool contains(const FloatPoint&, WindRule rule = RULE_NONZERO) const;
104        bool strokeContains(StrokeStyleApplier*, const FloatPoint&) const;
105        // fastBoundingRect() should equal or contain boundingRect(); boundingRect()
106        // should perfectly bound the points within the path.
107        FloatRect boundingRect() const;
108        FloatRect fastBoundingRect() const;
109        FloatRect strokeBoundingRect(StrokeStyleApplier* = 0) const;
110
111        float length() const;
112        FloatPoint pointAtLength(float length, bool& ok) const;
113        float normalAngleAtLength(float length, bool& ok) const;
114
115        void clear();
116#if PLATFORM(QT)
117        bool isNull() const { return false; }
118#else
119        bool isNull() const { return !m_path; }
120#endif
121        bool isEmpty() const;
122        // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
123        // Note the Path can be empty (isEmpty() == true) and still have a current point.
124        bool hasCurrentPoint() const;
125        FloatPoint currentPoint() const;
126
127        void moveTo(const FloatPoint&);
128        void addLineTo(const FloatPoint&);
129        void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
130        void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
131        void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
132        void closeSubpath();
133
134        void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
135        void addRect(const FloatRect&);
136        void addEllipse(const FloatRect&);
137
138        enum RoundedRectStrategy {
139            PreferNativeRoundedRect,
140            PreferBezierRoundedRect
141        };
142
143        void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii, RoundedRectStrategy = PreferNativeRoundedRect);
144        void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius, RoundedRectStrategy = PreferNativeRoundedRect);
145        void addRoundedRect(const RoundedRect&);
146
147        void translate(const FloatSize&);
148
149        // To keep Path() cheap, it does not allocate a PlatformPath immediately
150        // meaning Path::platformPath() can return null (except on Qt).
151        PlatformPathPtr platformPath() const { return m_path; }
152#if PLATFORM(QT)
153        PlatformPathPtr ensurePlatformPath() { return platformPath(); }
154#else
155        // ensurePlatformPath() will allocate a PlatformPath if it has not yet been and will never return null.
156        PlatformPathPtr ensurePlatformPath();
157#endif
158
159        void apply(void* info, PathApplierFunction) const;
160        void transform(const AffineTransform&);
161
162        void addPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius, RoundedRectStrategy = PreferNativeRoundedRect);
163        void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
164
165#if USE(CG) || PLATFORM(BLACKBERRY)
166        void platformAddPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
167#endif
168
169#if PLATFORM(BLACKBERRY)
170        Path(const PlatformPath&);
171#endif
172
173    private:
174        PlatformPathPtr m_path;
175    };
176
177}
178
179#endif
180