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 <algorithm> 32 33 #if PLATFORM(CG) 34 typedef struct CGPath PlatformPath; 35 #elif PLATFORM(QT) 36 #include <qglobal.h> 37 QT_BEGIN_NAMESPACE 38 class QPainterPath; 39 QT_END_NAMESPACE 40 typedef QPainterPath PlatformPath; 41 #elif PLATFORM(SGL) 42 class SkPath; 43 typedef SkPath PlatformPath; 44 #elif PLATFORM(WX) && USE(WXGC) 45 class wxGraphicsPath; 46 typedef wxGraphicsPath PlatformPath; 47 #elif PLATFORM(CAIRO) 48 namespace WebCore { 49 struct CairoPath; 50 } 51 typedef WebCore::CairoPath PlatformPath; 52 #elif PLATFORM(SKIA) 53 class SkPath; 54 typedef SkPath PlatformPath; 55 #elif PLATFORM(WINCE) 56 namespace WebCore { 57 class PlatformPath; 58 } 59 #else 60 typedef void PlatformPath; 61 #endif 62 63 namespace WebCore { 64 65 class FloatPoint; 66 class FloatRect; 67 class FloatSize; 68 class GraphicsContext; 69 class String; 70 class StrokeStyleApplier; 71 class TransformationMatrix; 72 73 enum WindRule { 74 RULE_NONZERO = 0, 75 RULE_EVENODD = 1 76 }; 77 78 enum PathElementType { 79 PathElementMoveToPoint, 80 PathElementAddLineToPoint, 81 PathElementAddQuadCurveToPoint, 82 PathElementAddCurveToPoint, 83 PathElementCloseSubpath 84 }; 85 86 struct PathElement { 87 PathElementType type; 88 FloatPoint* points; 89 }; 90 91 typedef void (*PathApplierFunction)(void* info, const PathElement*); 92 93 class Path { 94 public: 95 Path(); 96 ~Path(); 97 98 Path(const Path&); 99 Path& operator=(const Path&); 100 swap(Path & other)101 void swap(Path& other) { std::swap(m_path, other.m_path); } 102 103 bool contains(const FloatPoint&, WindRule rule = RULE_NONZERO) const; 104 bool strokeContains(StrokeStyleApplier*, const FloatPoint&) const; 105 FloatRect boundingRect() const; 106 FloatRect strokeBoundingRect(StrokeStyleApplier* = 0); 107 108 float length(); 109 FloatPoint pointAtLength(float length, bool& ok); 110 float normalAngleAtLength(float length, bool& ok); 111 112 void clear(); 113 bool isEmpty() const; 114 // Gets the current point of the current path, which is conceptually the final point reached by the path so far. 115 // Note the Path can be empty (isEmpty() == true) and still have a current point. 116 bool hasCurrentPoint() const; 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 FloatRect&); 128 129 void translate(const FloatSize&); 130 131 String debugString() const; 132 platformPath()133 PlatformPath* platformPath() const { return m_path; } 134 135 static Path createRoundedRectangle(const FloatRect&, const FloatSize& roundingRadii); 136 static Path createRoundedRectangle(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius); 137 static Path createRectangle(const FloatRect&); 138 static Path createEllipse(const FloatPoint& center, float rx, float ry); 139 static Path createCircle(const FloatPoint& center, float r); 140 static Path createLine(const FloatPoint&, const FloatPoint&); 141 142 void apply(void* info, PathApplierFunction) const; 143 void transform(const TransformationMatrix&); 144 145 private: 146 PlatformPath* m_path; 147 }; 148 149 } 150 151 #endif 152