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 "core/platform/graphics/transforms/TransformationMatrix.h"
     31 
     32 #include <string.h> // for memcpy
     33 #include "wtf/FastAllocBase.h"
     34 
     35 #include <SkMatrix.h>
     36 
     37 namespace WebCore {
     38 
     39 class FloatPoint;
     40 class FloatQuad;
     41 class FloatRect;
     42 class IntPoint;
     43 class IntRect;
     44 class TransformationMatrix;
     45 
     46 class AffineTransform {
     47     WTF_MAKE_FAST_ALLOCATED;
     48 public:
     49     typedef double Transform[6];
     50 
     51     AffineTransform();
     52     AffineTransform(double a, double b, double c, double d, double e, double f);
     53 
     54     void setMatrix(double a, double b, double c, double d, double e, double f);
     55 
     56     void map(double x, double y, double& x2, double& y2) const;
     57 
     58     // Rounds the mapped point to the nearest integer value.
     59     IntPoint mapPoint(const IntPoint&) const;
     60 
     61     FloatPoint mapPoint(const FloatPoint&) const;
     62 
     63     IntSize mapSize(const IntSize&) const;
     64 
     65     FloatSize mapSize(const FloatSize&) const;
     66 
     67     // Rounds the resulting mapped rectangle out. This is helpful for bounding
     68     // box computations but may not be what is wanted in other contexts.
     69     IntRect mapRect(const IntRect&) const;
     70 
     71     FloatRect mapRect(const FloatRect&) const;
     72     FloatQuad mapQuad(const FloatQuad&) const;
     73 
     74     bool isIdentity() const;
     75 
     76     double a() const { return m_transform[0]; }
     77     void setA(double a) { m_transform[0] = a; }
     78     double b() const { return m_transform[1]; }
     79     void setB(double b) { m_transform[1] = b; }
     80     double c() const { return m_transform[2]; }
     81     void setC(double c) { m_transform[2] = c; }
     82     double d() const { return m_transform[3]; }
     83     void setD(double d) { m_transform[3] = d; }
     84     double e() const { return m_transform[4]; }
     85     void setE(double e) { m_transform[4] = e; }
     86     double f() const { return m_transform[5]; }
     87     void setF(double f) { m_transform[5] = f; }
     88 
     89     void makeIdentity();
     90 
     91     AffineTransform& multiply(const AffineTransform& other);
     92     AffineTransform& scale(double);
     93     AffineTransform& scale(double sx, double sy);
     94     AffineTransform& scaleNonUniform(double sx, double sy);
     95     AffineTransform& rotate(double d);
     96     AffineTransform& rotateFromVector(double x, double y);
     97     AffineTransform& translate(double tx, double ty);
     98     AffineTransform& shear(double sx, double sy);
     99     AffineTransform& flipX();
    100     AffineTransform& flipY();
    101     AffineTransform& skew(double angleX, double angleY);
    102     AffineTransform& skewX(double angle);
    103     AffineTransform& skewY(double angle);
    104 
    105     double xScale() const;
    106     double yScale() const;
    107 
    108     double det() const;
    109     bool isInvertible() const;
    110     AffineTransform inverse() const;
    111 
    112     void blend(const AffineTransform& from, double progress);
    113 
    114     TransformationMatrix toTransformationMatrix() const;
    115 
    116     bool isIdentityOrTranslation() const
    117     {
    118         return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && m_transform[3] == 1;
    119     }
    120 
    121     bool isIdentityOrTranslationOrFlipped() const
    122     {
    123         return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && (m_transform[3] == 1 || m_transform[3] == -1);
    124     }
    125 
    126     bool preservesAxisAlignment() const
    127     {
    128         return (m_transform[1] == 0 && m_transform[2] == 0) || (m_transform[0] == 0 && m_transform[3] == 0);
    129     }
    130 
    131     bool operator== (const AffineTransform& m2) const
    132     {
    133         return (m_transform[0] == m2.m_transform[0]
    134              && m_transform[1] == m2.m_transform[1]
    135              && m_transform[2] == m2.m_transform[2]
    136              && m_transform[3] == m2.m_transform[3]
    137              && m_transform[4] == m2.m_transform[4]
    138              && m_transform[5] == m2.m_transform[5]);
    139     }
    140 
    141     bool operator!=(const AffineTransform& other) const { return !(*this == other); }
    142 
    143     // *this = *this * t (i.e., a multRight)
    144     AffineTransform& operator*=(const AffineTransform& t)
    145     {
    146         return multiply(t);
    147     }
    148 
    149     // result = *this * t (i.e., a multRight)
    150     AffineTransform operator*(const AffineTransform& t) const
    151     {
    152         AffineTransform result = *this;
    153         result *= t;
    154         return result;
    155     }
    156 
    157     operator SkMatrix() const;
    158 
    159     static AffineTransform translation(double x, double y)
    160     {
    161         return AffineTransform(1, 0, 0, 1, x, y);
    162     }
    163 
    164     // decompose the matrix into its component parts
    165     typedef struct {
    166         double scaleX, scaleY;
    167         double angle;
    168         double remainderA, remainderB, remainderC, remainderD;
    169         double translateX, translateY;
    170     } DecomposedType;
    171 
    172     bool decompose(DecomposedType&) const;
    173     void recompose(const DecomposedType&);
    174 
    175 private:
    176     void setMatrix(const Transform m)
    177     {
    178         if (m && m != m_transform)
    179             memcpy(m_transform, m, sizeof(Transform));
    180     }
    181 
    182     Transform m_transform;
    183 };
    184 
    185 AffineTransform makeMapBetweenRects(const FloatRect& source, const FloatRect& dest);
    186 
    187 }
    188 
    189 #endif
    190