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