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
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.
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"
44 class AffineTransform;
48 class GraphicsContext;
51 enum PathElementType {
52 PathElementMoveToPoint, // The points member will contain 1 value.
53 PathElementAddLineToPoint, // The points member will contain 1 value.
54 PathElementAddQuadCurveToPoint, // The points member will contain 2 values.
55 PathElementAddCurveToPoint, // The points member will contain 3 values.
56 PathElementCloseSubpath // The points member will contain no values.
59 // The points in the sturcture are the same as those that would be used with the
60 // add... method. For example, a line returns the endpoint, while a cubic returns
61 // two tangent points and the endpoint.
67 typedef void (*PathApplierFunction)(void* info, const PathElement*);
69 class PLATFORM_EXPORT Path {
70 WTF_MAKE_FAST_ALLOCATED;
76 Path& operator=(const Path&);
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;
85 FloatPoint pointAtLength(float length, bool& ok) const;
86 float normalAngleAtLength(float length, bool& ok) const;
87 bool pointAndNormalAtLength(float length, FloatPoint&, float&) const;
89 // Helper for computing a sequence of positions and normals (normal angles) on a path.
90 // The best possible access pattern will be one where the |length| value is
91 // strictly increasing.
92 // For other access patterns, performance will vary depending on curvature
93 // and number of segments, but should never be worse than that of the
94 // state-less method on Path.
95 class PLATFORM_EXPORT PositionCalculator {
96 WTF_MAKE_NONCOPYABLE(PositionCalculator);
98 explicit PositionCalculator(const Path&);
100 bool pointAndNormalAtLength(float length, FloatPoint&, float&);
104 SkPathMeasure m_pathMeasure;
105 SkScalar m_accumulatedLength;
109 bool isEmpty() const;
110 // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
111 // Note the Path can be empty (isEmpty() == true) and still have a current point.
112 bool hasCurrentPoint() const;
113 FloatPoint currentPoint() const;
115 WindRule windRule() const;
116 void setWindRule(const WindRule);
118 void moveTo(const FloatPoint&);
119 void addLineTo(const FloatPoint&);
120 void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
121 void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
122 void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
125 void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
126 void addRect(const FloatRect&);
127 void addEllipse(const FloatPoint&, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise);
128 void addEllipse(const FloatRect&);
130 void addRoundedRect(const FloatRect&, const FloatSize& roundingRadii);
131 void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
132 void addRoundedRect(const RoundedRect&);
134 void addPath(const Path&, const AffineTransform&);
136 void translate(const FloatSize&);
138 const SkPath& skPath() const { return m_path; }
140 void apply(void* info, PathApplierFunction) const;
141 void transform(const AffineTransform&);
143 void addPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
144 void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
146 bool subtractPath(const Path&);
147 bool intersectPath(const Path&);
149 // Updates the path to the union (inclusive-or) of itself with the given argument.
150 bool unionPath(const Path& other);
153 void addEllipse(const FloatPoint&, float radiusX, float radiusY, float startAngle, float endAngle, bool anticlockwise);
159 PLATFORM_EXPORT bool ellipseIsRenderable(float startAngle, float endAngle);