• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/WindRule.h"
35 #include "third_party/skia/include/core/SkPath.h"
36 #include "wtf/FastAllocBase.h"
37 #include "wtf/Forward.h"
38 
39 class SkPath;
40 
41 namespace WebCore {
42 
43 class AffineTransform;
44 class FloatPoint;
45 class FloatRect;
46 class FloatSize;
47 class GraphicsContext;
48 class StrokeData;
49 
50 enum 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 sturcture 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.
61 struct PathElement {
62     PathElementType type;
63     FloatPoint* points;
64 };
65 
66 typedef void (*PathApplierFunction)(void* info, const PathElement*);
67 
68 class PLATFORM_EXPORT Path {
69     WTF_MAKE_FAST_ALLOCATED;
70 public:
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     void clear();
89     bool isEmpty() const;
90     // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
91     // Note the Path can be empty (isEmpty() == true) and still have a current point.
92     bool hasCurrentPoint() const;
93     FloatPoint currentPoint() const;
94 
95     WindRule windRule() const;
96     void setWindRule(const WindRule);
97 
98     void moveTo(const FloatPoint&);
99     void addLineTo(const FloatPoint&);
100     void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
101     void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
102     void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
103     void closeSubpath();
104 
105     void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
106     void addRect(const FloatRect&);
107     void addEllipse(const FloatPoint&, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise);
108     void addEllipse(const FloatRect&);
109 
110     void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii);
111     void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
112     void addRoundedRect(const RoundedRect&);
113 
114     void translate(const FloatSize&);
115 
skPath()116     const SkPath& skPath() const { return m_path; }
117 
118     void apply(void* info, PathApplierFunction) const;
119     void transform(const AffineTransform&);
120 
121     void addPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
122     void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
123 
124     // Updates the path to the union (inclusive-or) of itself with the given argument.
125     bool unionPath(const Path& other);
126 
127 private:
128     void addEllipse(const FloatPoint&, float radiusX, float radiusY, float startAngle, float endAngle, bool anticlockwise);
129 
130     SkPath m_path;
131 };
132 
133 #if !ASSERT_DISABLED
134 PLATFORM_EXPORT bool ellipseIsRenderable(float startAngle, float endAngle);
135 #endif
136 
137 }
138 
139 #endif
140