Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / graphics / Path.h
blob7fc4883c63b3add7ef8cae3de0a7f2d2451a38f9
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.
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.
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.
29 #ifndef Path_h
30 #define Path_h
32 #include "platform/PlatformExport.h"
33 #include "platform/geometry/FloatRoundedRect.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"
40 class SkPath;
42 namespace blink {
44 class AffineTransform;
45 class FloatPoint;
46 class FloatRect;
47 class FloatSize;
48 class StrokeData;
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.
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.
61 struct PathElement {
62 PathElementType type;
63 FloatPoint* points;
66 typedef void (*PathApplierFunction)(void* info, const PathElement*);
68 class PLATFORM_EXPORT Path {
69 WTF_MAKE_FAST_ALLOCATED(Path);
70 public:
71 Path();
72 ~Path();
74 Path(const Path&);
75 Path& operator=(const Path&);
76 Path& operator=(const SkPath&);
77 bool operator==(const Path&) const;
79 bool contains(const FloatPoint&, WindRule = RULE_NONZERO) const;
80 bool strokeContains(const FloatPoint&, const StrokeData&) const;
81 FloatRect boundingRect() const;
82 FloatRect strokeBoundingRect(const StrokeData&) const;
84 float length() const;
85 FloatPoint pointAtLength(float length, bool& ok) const;
86 bool pointAndNormalAtLength(float length, FloatPoint&, float&) const;
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&);
99 bool pointAndNormalAtLength(float length, FloatPoint&, float&);
101 private:
102 SkPath m_path;
103 SkPathMeasure m_pathMeasure;
104 SkScalar m_accumulatedLength;
107 void clear();
108 bool isEmpty() const;
109 // Specify whether this path is volatile. Temporary paths that are discarded or
110 // modified after use should be marked as volatile. This is a hint to the device
111 // to not cache this path.
112 void setIsVolatile(bool);
113 // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
114 // Note the Path can be empty (isEmpty() == true) and still have a current point.
115 bool hasCurrentPoint() const;
116 FloatPoint currentPoint() const;
118 void setWindRule(const WindRule);
120 void moveTo(const FloatPoint&);
121 void addLineTo(const FloatPoint&);
122 void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
123 void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
124 void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
125 void closeSubpath();
127 void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
128 void addRect(const FloatRect&);
129 void addEllipse(const FloatPoint&, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise);
130 void addEllipse(const FloatRect&);
132 void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii);
133 void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
134 void addRoundedRect(const FloatRoundedRect&);
136 void addPath(const Path&, const AffineTransform&);
138 void translate(const FloatSize&);
140 const SkPath& skPath() const { return m_path; }
142 void apply(void* info, PathApplierFunction) const;
143 void transform(const AffineTransform&);
145 void addPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
146 void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
148 bool subtractPath(const Path&);
150 // Updates the path to the union (inclusive-or) of itself with the given argument.
151 bool unionPath(const Path& other);
153 private:
154 void addEllipse(const FloatPoint&, float radiusX, float radiusY, float startAngle, float endAngle, bool anticlockwise);
155 SkPath strokePath(const StrokeData&) const;
157 SkPath m_path;
160 #if ENABLE(ASSERT)
161 PLATFORM_EXPORT bool ellipseIsRenderable(float startAngle, float endAngle);
162 #endif
164 } // namespace blink
166 #endif