Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
      3  *               2006 Rob Buis <buis (at) 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 #include <wtf/FastAllocBase.h>
     33 
     34 #if PLATFORM(CG)
     35 typedef struct CGPath PlatformPath;
     36 #elif PLATFORM(QT)
     37 #include <qpainterpath.h>
     38 typedef QPainterPath PlatformPath;
     39 #elif PLATFORM(WX) && USE(WXGC)
     40 class wxGraphicsPath;
     41 typedef wxGraphicsPath PlatformPath;
     42 #elif PLATFORM(CAIRO)
     43 namespace WebCore {
     44     struct CairoPath;
     45 }
     46 typedef WebCore::CairoPath PlatformPath;
     47 #elif PLATFORM(SKIA)
     48 class SkPath;
     49 typedef SkPath PlatformPath;
     50 #elif PLATFORM(HAIKU)
     51 class BRegion;
     52 typedef BRegion PlatformPath;
     53 #elif OS(WINCE)
     54 namespace WebCore {
     55     class PlatformPath;
     56 }
     57 #else
     58 typedef void PlatformPath;
     59 #endif
     60 
     61 #if PLATFORM(QT)
     62 /* QPainterPath is valued based */
     63 typedef PlatformPath PlatformPathPtr;
     64 #else
     65 typedef PlatformPath* PlatformPathPtr;
     66 #endif
     67 
     68 namespace WebCore {
     69 
     70     class AffineTransform;
     71     class FloatPoint;
     72     class FloatRect;
     73     class FloatSize;
     74     class GraphicsContext;
     75     class String;
     76     class StrokeStyleApplier;
     77 
     78     enum WindRule {
     79         RULE_NONZERO = 0,
     80         RULE_EVENODD = 1
     81     };
     82 
     83     enum PathElementType {
     84         PathElementMoveToPoint,
     85         PathElementAddLineToPoint,
     86         PathElementAddQuadCurveToPoint,
     87         PathElementAddCurveToPoint,
     88         PathElementCloseSubpath
     89     };
     90 
     91     struct PathElement {
     92         PathElementType type;
     93         FloatPoint* points;
     94     };
     95 
     96     typedef void (*PathApplierFunction)(void* info, const PathElement*);
     97 
     98     class Path : public FastAllocBase {
     99     public:
    100         Path();
    101         ~Path();
    102 
    103         Path(const Path&);
    104         Path& operator=(const Path&);
    105 
    106         void swap(Path& other) { std::swap(m_path, other.m_path); }
    107 
    108         bool contains(const FloatPoint&, WindRule rule = RULE_NONZERO) const;
    109         bool strokeContains(StrokeStyleApplier*, const FloatPoint&) const;
    110         FloatRect boundingRect() const;
    111         FloatRect strokeBoundingRect(StrokeStyleApplier* = 0);
    112 
    113         float length();
    114         FloatPoint pointAtLength(float length, bool& ok);
    115         float normalAngleAtLength(float length, bool& ok);
    116 
    117         void clear();
    118         bool isEmpty() const;
    119         // Gets the current point of the current path, which is conceptually the final point reached by the path so far.
    120         // Note the Path can be empty (isEmpty() == true) and still have a current point.
    121         bool hasCurrentPoint() const;
    122 
    123         void moveTo(const FloatPoint&);
    124         void addLineTo(const FloatPoint&);
    125         void addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& endPoint);
    126         void addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& endPoint);
    127         void addArcTo(const FloatPoint&, const FloatPoint&, float radius);
    128         void closeSubpath();
    129 
    130         void addArc(const FloatPoint&, float radius, float startAngle, float endAngle, bool anticlockwise);
    131         void addRect(const FloatRect&);
    132         void addEllipse(const FloatRect&);
    133 
    134         void translate(const FloatSize&);
    135 
    136         String debugString() const;
    137 
    138         PlatformPathPtr platformPath() const { return m_path; }
    139 
    140         static Path createRoundedRectangle(const FloatRect&, const FloatSize& roundingRadii);
    141         static Path createRoundedRectangle(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
    142         static Path createRectangle(const FloatRect&);
    143         static Path createEllipse(const FloatPoint& center, float rx, float ry);
    144         static Path createCircle(const FloatPoint& center, float r);
    145         static Path createLine(const FloatPoint&, const FloatPoint&);
    146 
    147         void apply(void* info, PathApplierFunction) const;
    148         void transform(const AffineTransform&);
    149 
    150     private:
    151         PlatformPathPtr m_path;
    152     };
    153 
    154 }
    155 
    156 #endif
    157