Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2004, 2006, 2007 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2005 Nokia.  All rights reserved.
      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 FloatPoint_h
     28 #define FloatPoint_h
     29 
     30 #include "FloatSize.h"
     31 #include "IntPoint.h"
     32 #include <wtf/MathExtras.h>
     33 #include <wtf/Platform.h>
     34 
     35 #if PLATFORM(CG)
     36 typedef struct CGPoint CGPoint;
     37 #endif
     38 
     39 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
     40 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
     41 typedef struct CGPoint NSPoint;
     42 #else
     43 typedef struct _NSPoint NSPoint;
     44 #endif
     45 #endif
     46 
     47 #if PLATFORM(QT)
     48 #include "qglobal.h"
     49 QT_BEGIN_NAMESPACE
     50 class QPointF;
     51 QT_END_NAMESPACE
     52 #endif
     53 
     54 #if PLATFORM(HAIKU)
     55 class BPoint;
     56 #endif
     57 
     58 #if PLATFORM(SKIA)
     59 struct SkPoint;
     60 #endif
     61 
     62 namespace WebCore {
     63 
     64 class AffineTransform;
     65 class TransformationMatrix;
     66 class IntPoint;
     67 
     68 class FloatPoint {
     69 public:
     70     FloatPoint() : m_x(0), m_y(0) { }
     71     FloatPoint(float x, float y) : m_x(x), m_y(y) { }
     72     FloatPoint(const IntPoint&);
     73 
     74     static FloatPoint narrowPrecision(double x, double y);
     75 
     76     float x() const { return m_x; }
     77     float y() const { return m_y; }
     78 
     79     void setX(float x) { m_x = x; }
     80     void setY(float y) { m_y = y; }
     81     void move(float dx, float dy) { m_x += dx; m_y += dy; }
     82 
     83 #if PLATFORM(CG)
     84     FloatPoint(const CGPoint&);
     85     operator CGPoint() const;
     86 #endif
     87 
     88 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
     89         || (PLATFORM(CHROMIUM) && OS(DARWIN))
     90     FloatPoint(const NSPoint&);
     91     operator NSPoint() const;
     92 #endif
     93 
     94 #if PLATFORM(QT)
     95     FloatPoint(const QPointF&);
     96     operator QPointF() const;
     97 #endif
     98 
     99 #if PLATFORM(HAIKU)
    100     FloatPoint(const BPoint&);
    101     operator BPoint() const;
    102 #endif
    103 
    104 #if PLATFORM(SKIA)
    105     operator SkPoint() const;
    106     FloatPoint(const SkPoint&);
    107 #endif
    108 
    109     FloatPoint matrixTransform(const TransformationMatrix&) const;
    110     FloatPoint matrixTransform(const AffineTransform&) const;
    111 
    112 private:
    113     float m_x, m_y;
    114 };
    115 
    116 
    117 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
    118 {
    119     a.move(b.width(), b.height());
    120     return a;
    121 }
    122 
    123 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
    124 {
    125     a.move(-b.width(), -b.height());
    126     return a;
    127 }
    128 
    129 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
    130 {
    131     return FloatPoint(a.x() + b.width(), a.y() + b.height());
    132 }
    133 
    134 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
    135 {
    136     return FloatSize(a.x() - b.x(), a.y() - b.y());
    137 }
    138 
    139 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
    140 {
    141     return FloatPoint(a.x() - b.width(), a.y() - b.height());
    142 }
    143 
    144 inline bool operator==(const FloatPoint& a, const FloatPoint& b)
    145 {
    146     return a.x() == b.x() && a.y() == b.y();
    147 }
    148 
    149 inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
    150 {
    151     return a.x() != b.x() || a.y() != b.y();
    152 }
    153 
    154 inline IntPoint roundedIntPoint(const FloatPoint& p)
    155 {
    156     return IntPoint(static_cast<int>(roundf(p.x())), static_cast<int>(roundf(p.y())));
    157 }
    158 
    159 }
    160 
    161 #endif
    162