1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  *                     2006 Rob Buis <buis@kde.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef Path_h
28 #define Path_h
29 
30 #include "dom/dom_string.h"
31 #include "platform/graphics/FloatPoint.h"
32 #include "platform/graphics/FloatRect.h"
33 #include <QPainterPath>
34 
35 typedef QPainterPath PlatformPath;
36 
37 namespace WebCore
38 {
39 class AffineTransform;
40 }
41 
42 namespace khtml
43 {
44 using WebCore::AffineTransform;
45 using WebCore::FloatPoint;
46 using WebCore::FloatSize;
47 using WebCore::FloatRect;
48 //class String;
49 
50 enum WindRule {
51     RULE_NONZERO = 0,
52     RULE_EVENODD = 1
53 };
54 
55 enum PathElementType {
56     PathElementMoveToPoint,
57     PathElementAddLineToPoint,
58     PathElementAddQuadCurveToPoint,
59     PathElementAddCurveToPoint,
60     PathElementCloseSubpath
61 };
62 
63 struct PathElement {
64     PathElementType type;
65     FloatPoint *points;
66 };
67 
68 typedef void (*PathApplierFunction)(void *info, const PathElement *);
69 
70 class Path
71 {
72 public:
73     Path();
74     ~Path();
75 
76     Path(const Path &);
77     Path &operator=(const Path &);
78 
79     bool contains(const FloatPoint &, WindRule rule = RULE_NONZERO) const;
80     FloatRect boundingRect() const;
81 
82     float length();
83     FloatPoint pointAtLength(float length, bool &ok);
84     float normalAngleAtLength(float length, bool &ok);
85 
86     void clear();
87     bool isEmpty() const;
88 
89     void moveTo(const FloatPoint &);
90     void addLineTo(const FloatPoint &);
91     void addQuadCurveTo(const FloatPoint &controlPoint, const FloatPoint &point);
92     void addBezierCurveTo(const FloatPoint &controlPoint1, const FloatPoint &controlPoint2, const FloatPoint &);
93     void addArcTo(const FloatPoint &, const FloatPoint &, float radius);
94     void closeSubpath();
95 
96     void addArc(const FloatPoint &, float radius, float startAngle, float endAngle, bool anticlockwise);
97     void addRect(const FloatRect &);
98     void addEllipse(const FloatRect &);
99 
100     void translate(const FloatSize &);
101 
setWindingRule(WindRule rule)102     void setWindingRule(WindRule rule)
103     {
104         m_rule = rule;
105     }
windingRule()106     WindRule windingRule() const
107     {
108         return m_rule;
109     }
110 
111     DOM::DOMString debugString() const;
112 
platformPath()113     PlatformPath *platformPath() const
114     {
115         return m_path;
116     }
117 
118     static Path createRoundedRectangle(const FloatRect &, const FloatSize &roundingRadii);
119     static Path createRoundedRectangle(const FloatRect &, const FloatSize &topLeftRadius, const FloatSize &topRightRadius, const FloatSize &bottomLeftRadius, const FloatSize &bottomRightRadius);
120     static Path createRectangle(const FloatRect &);
121     static Path createEllipse(const FloatPoint &center, float rx, float ry);
122     static Path createCircle(const FloatPoint &center, float r);
123     static Path createLine(const FloatPoint &, const FloatPoint &);
124 
125     void apply(void *info, PathApplierFunction) const;
126     void transform(const AffineTransform &);
127 
128 private:
129     PlatformPath *m_path;
130     WindRule m_rule;
131 };
132 
133 }
134 
135 #endif
136