Home | History | Annotate | Download | only in transforms
      1 /*
      2  * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
      3  *               2010 Dirk Schulze <krit (at) webkit.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 AffineTransform_h
     28 #define AffineTransform_h
     29 
     30 #include "TransformationMatrix.h"
     31 
     32 #include <string.h> // for memcpy
     33 #include <wtf/FastAllocBase.h>
     34 
     35 #if PLATFORM(CG)
     36 #include <CoreGraphics/CGAffineTransform.h>
     37 #elif PLATFORM(CAIRO)
     38 #include <cairo.h>
     39 #elif PLATFORM(QT)
     40 #include <QTransform>
     41 #elif PLATFORM(SKIA)
     42 #include <SkMatrix.h>
     43 #elif PLATFORM(WX) && USE(WXGC)
     44 #include <wx/graphics.h>
     45 #endif
     46 
     47 namespace WebCore {
     48 
     49 class FloatPoint;
     50 class FloatQuad;
     51 class FloatRect;
     52 class IntPoint;
     53 class IntRect;
     54 class TransformationMatrix;
     55 
     56 class AffineTransform : public FastAllocBase {
     57 public:
     58     typedef double Transform[6];
     59 
     60     AffineTransform();
     61     AffineTransform(double a, double b, double c, double d, double e, double f);
     62 
     63     void setMatrix(double a, double b, double c, double d, double e, double f);
     64 
     65     void map(double x, double y, double& x2, double& y2) const;
     66 
     67     // Rounds the mapped point to the nearest integer value.
     68     IntPoint mapPoint(const IntPoint&) const;
     69 
     70     FloatPoint mapPoint(const FloatPoint&) const;
     71 
     72     // Rounds the resulting mapped rectangle out. This is helpful for bounding
     73     // box computations but may not be what is wanted in other contexts.
     74     IntRect mapRect(const IntRect&) const;
     75 
     76     FloatRect mapRect(const FloatRect&) const;
     77 
     78     bool isIdentity() const;
     79 
     80     double a() const { return m_transform[0]; }
     81     void setA(double a) { m_transform[0] = a; }
     82     double b() const { return m_transform[1]; }
     83     void setB(double b) { m_transform[1] = b; }
     84     double c() const { return m_transform[2]; }
     85     void setC(double c) { m_transform[2] = c; }
     86     double d() const { return m_transform[3]; }
     87     void setD(double d) { m_transform[3] = d; }
     88     double e() const { return m_transform[4]; }
     89     void setE(double e) { m_transform[4] = e; }
     90     double f() const { return m_transform[5]; }
     91     void setF(double f) { m_transform[5] = f; }
     92 
     93     void makeIdentity();
     94 
     95     AffineTransform& multiply(const AffineTransform&);
     96     AffineTransform& multLeft(const AffineTransform&);
     97     AffineTransform& scale(double);
     98     AffineTransform& scale(double sx, double sy);
     99     AffineTransform& scaleNonUniform(double sx, double sy);
    100     AffineTransform& rotate(double d);
    101     AffineTransform& rotateFromVector(double x, double y);
    102     AffineTransform& translate(double tx, double ty);
    103     AffineTransform& translateRight(double tx, double ty);
    104     AffineTransform& shear(double sx, double sy);
    105     AffineTransform& flipX();
    106     AffineTransform& flipY();
    107     AffineTransform& skew(double angleX, double angleY);
    108     AffineTransform& skewX(double angle);
    109     AffineTransform& skewY(double angle);
    110 
    111     double det() const;
    112     bool isInvertible() const;
    113     AffineTransform inverse() const;
    114 
    115     void blend(const AffineTransform& from, double progress);
    116 
    117     TransformationMatrix toTransformationMatrix() const;
    118 
    119     bool isIdentityOrTranslation() const
    120     {
    121         return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && m_transform[3] == 1;
    122     }
    123 
    124     bool operator== (const AffineTransform& m2) const
    125     {
    126         return (m_transform[0] == m2.m_transform[0]
    127              && m_transform[1] == m2.m_transform[1]
    128              && m_transform[2] == m2.m_transform[2]
    129              && m_transform[3] == m2.m_transform[3]
    130              && m_transform[4] == m2.m_transform[4]
    131              && m_transform[5] == m2.m_transform[5]);
    132     }
    133 
    134     bool operator!=(const AffineTransform& other) const { return !(*this == other); }
    135 
    136     // *this = *this * t (i.e., a multRight)
    137     AffineTransform& operator*=(const AffineTransform& t)
    138     {
    139         *this = *this * t;
    140         return *this;
    141     }
    142 
    143     // result = *this * t (i.e., a multRight)
    144     AffineTransform operator*(const AffineTransform& t) const
    145     {
    146         AffineTransform result = t;
    147         result.multLeft(*this);
    148         return result;
    149     }
    150 
    151 #if PLATFORM(CG)
    152     operator CGAffineTransform() const;
    153 #elif PLATFORM(CAIRO)
    154     operator cairo_matrix_t() const;
    155 #elif PLATFORM(QT)
    156     operator QTransform() const;
    157 #elif PLATFORM(SKIA)
    158     operator SkMatrix() const;
    159 #elif PLATFORM(WX) && USE(WXGC)
    160     operator wxGraphicsMatrix() const;
    161 #endif
    162 
    163 private:
    164     void setMatrix(const Transform m)
    165     {
    166         if (m && m != m_transform)
    167             memcpy(m_transform, m, sizeof(Transform));
    168     }
    169 
    170     Transform m_transform;
    171 };
    172 
    173 AffineTransform makeMapBetweenRects(const FloatRect& source, const FloatRect& dest);
    174 
    175 }
    176 
    177 #endif
    178