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 "third_party/skia/include/core/SkPathMeasure.h" 37 #include "wtf/FastAllocBase.h" 38 #include "wtf/Forward.h" 39 40 class SkPath; 41 42 namespace WebCore { 43 44 class AffineTransform; 45 class FloatPoint; 46 class FloatRect; 47 class FloatSize; 48 class GraphicsContext; 49 class StrokeData; 50 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. 57 }; 58 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. 62 struct PathElement { 63 PathElementType type; 64 FloatPoint* points; 65 }; 66 67 typedef void (*PathApplierFunction)(void* info, const PathElement*); 68 69 class PLATFORM_EXPORT Path { 70 WTF_MAKE_FAST_ALLOCATED; 71 public: 72 Path(); 73 ~Path(); 74 75 Path(const Path&); 76 Path& operator=(const Path&); 77 bool operator==(const Path&) const; 78 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; 83 84 float length() 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; 88 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); 97 public: 98 explicit PositionCalculator(const Path&); 99 100 bool pointAndNormalAtLength(float length, FloatPoint&, float&); 101 102 private: 103 SkPath m_path; 104 SkPathMeasure m_pathMeasure; 105 SkScalar m_accumulatedLength; 106 }; 107 108 void clear(); 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; 114 115 WindRule windRule() const; 116 void setWindRule(const WindRule); 117 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); 123 void closeSubpath(); 124 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&); 129 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&); 133 134 void addPath(const Path&, const AffineTransform&); 135 136 void translate(const FloatSize&); 137 skPath()138 const SkPath& skPath() const { return m_path; } 139 140 void apply(void* info, PathApplierFunction) const; 141 void transform(const AffineTransform&); 142 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); 145 146 // Updates the path to the union (inclusive-or) of itself with the given argument. 147 bool unionPath(const Path& other); 148 149 private: 150 void addEllipse(const FloatPoint&, float radiusX, float radiusY, float startAngle, float endAngle, bool anticlockwise); 151 152 SkPath m_path; 153 }; 154 155 #if ASSERT_ENABLED 156 PLATFORM_EXPORT bool ellipseIsRenderable(float startAngle, float endAngle); 157 #endif 158 159 } 160 161 #endif 162