webengine/osswebengine/WebCore/platform/graphics/cairo/AffineTransformCairo.cpp
changeset 0 dd21522fd290
child 25 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  *
       
     3  * Redistribution and use in source and binary forms, with or without
       
     4  * modification, are permitted provided that the following conditions
       
     5  * are met:
       
     6  * 1. Redistributions of source code must retain the above copyright
       
     7  *    notice, this list of conditions and the following disclaimer.
       
     8  * 2. Redistributions in binary form must reproduce the above copyright
       
     9  *    notice, this list of conditions and the following disclaimer in the
       
    10  *    documentation and/or other materials provided with the distribution.
       
    11  *
       
    12  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    14  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    15  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    16  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    17  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    18  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    20  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    23  */
       
    24 
       
    25 #include "config.h"
       
    26 #include "AffineTransform.h"
       
    27 
       
    28 #include "IntRect.h"
       
    29 #include "FloatRect.h"
       
    30 
       
    31 #include "cairo.h"
       
    32 
       
    33 namespace WebCore {
       
    34 
       
    35 static const double deg2rad = 0.017453292519943295769; // pi/180
       
    36 
       
    37 AffineTransform::AffineTransform()
       
    38 {
       
    39     cairo_matrix_init_identity(&m_transform);
       
    40 }
       
    41 
       
    42 AffineTransform::AffineTransform(double a, double b, double c, double d, double tx, double ty)
       
    43 {
       
    44     cairo_matrix_init(&m_transform, a, c, b, d, tx, ty);
       
    45 }
       
    46 
       
    47 AffineTransform::AffineTransform(const cairo_matrix_t &matrix)
       
    48 {
       
    49     m_transform = matrix;
       
    50 }
       
    51 
       
    52 void AffineTransform::setMatrix(double a, double b, double c, double d, double tx, double ty)
       
    53 {
       
    54     cairo_matrix_init(&m_transform, a, c, b, d, tx, ty);
       
    55 }
       
    56 
       
    57 void AffineTransform::map(double x, double y, double* x2, double* y2) const
       
    58 {
       
    59     *x2 = x;
       
    60     *y2 = y;
       
    61     cairo_matrix_transform_point(&m_transform, x2, y2);
       
    62 }
       
    63 
       
    64 IntRect AffineTransform::mapRect(const IntRect &rect) const
       
    65 {
       
    66     FloatRect floatRect(rect);
       
    67     FloatRect enclosingFloatRect = this->mapRect(floatRect);
       
    68 
       
    69     return enclosingIntRect(enclosingFloatRect);
       
    70 }
       
    71 
       
    72 FloatRect AffineTransform::mapRect(const FloatRect &rect) const
       
    73 {
       
    74     double rectMinX = rect.x();
       
    75     double rectMaxX = rect.x() + rect.width();
       
    76     double rectMinY = rect.y();
       
    77     double rectMaxY = rect.y() + rect.height();
       
    78 
       
    79     double px = rectMinX;
       
    80     double py = rectMinY;
       
    81     cairo_matrix_transform_point(&m_transform, &px, &py);
       
    82     
       
    83     double enclosingRectMinX = px;
       
    84     double enclosingRectMinY = py;
       
    85     double enclosingRectMaxX = px;
       
    86     double enclosingRectMaxY = py;
       
    87     
       
    88     px = rectMaxX;
       
    89     py = rectMinY;
       
    90     cairo_matrix_transform_point(&m_transform, &px, &py);
       
    91     if (px < enclosingRectMinX) 
       
    92         enclosingRectMinX = px;
       
    93     else if (px > enclosingRectMaxX) 
       
    94         enclosingRectMaxX = px;
       
    95     if (py < enclosingRectMinY) 
       
    96         enclosingRectMinY = py;
       
    97     else if (py > enclosingRectMaxY) 
       
    98         enclosingRectMaxY = py;
       
    99 
       
   100     px = rectMaxX;
       
   101     py = rectMaxY;
       
   102     cairo_matrix_transform_point(&m_transform, &px, &py);
       
   103     if (px < enclosingRectMinX) 
       
   104         enclosingRectMinX = px;
       
   105     else if (px > enclosingRectMaxX)
       
   106         enclosingRectMaxX = px;
       
   107     if (py < enclosingRectMinY) 
       
   108         enclosingRectMinY = py;
       
   109     else if (py > enclosingRectMaxY) 
       
   110         enclosingRectMaxY = py;
       
   111 
       
   112     px = rectMinX;
       
   113     py = rectMaxY;
       
   114     cairo_matrix_transform_point(&m_transform, &px, &py);
       
   115     if (px < enclosingRectMinX) 
       
   116         enclosingRectMinX = px;
       
   117     else if (px > enclosingRectMaxX) 
       
   118         enclosingRectMaxX = px;
       
   119     if (py < enclosingRectMinY) 
       
   120         enclosingRectMinY = py;
       
   121     else if (py > enclosingRectMaxY) 
       
   122         enclosingRectMaxY = py;
       
   123 
       
   124 
       
   125     double enclosingRectWidth = enclosingRectMaxX - enclosingRectMinX;
       
   126     double enclosingRectHeight = enclosingRectMaxY - enclosingRectMinY;
       
   127 
       
   128     return FloatRect(enclosingRectMinX, enclosingRectMinY, enclosingRectWidth, enclosingRectHeight);
       
   129 }
       
   130 
       
   131 bool AffineTransform::isIdentity() const
       
   132 {
       
   133     return ((m_transform.xx == 1) && (m_transform.yy == 1) 
       
   134          && (m_transform.xy == 0) && (m_transform.yx == 0) 
       
   135          && (m_transform.x0 == 0) && (m_transform.y0 == 0));
       
   136 }
       
   137 
       
   138 double AffineTransform::a() const
       
   139 {
       
   140     return m_transform.xx;
       
   141 }
       
   142 
       
   143 void AffineTransform::setA(double a)
       
   144 {
       
   145     m_transform.xx = a;
       
   146 }
       
   147 
       
   148 double AffineTransform::b() const
       
   149 {
       
   150     return m_transform.xy;
       
   151 }
       
   152 
       
   153 void AffineTransform::setB(double b)
       
   154 {
       
   155     m_transform.xy = b;
       
   156 }
       
   157 
       
   158 double AffineTransform::c() const
       
   159 {
       
   160     return m_transform.yx;
       
   161 }
       
   162 
       
   163 void AffineTransform::setC(double c)
       
   164 {
       
   165     m_transform.yx = c;
       
   166 }
       
   167 
       
   168 double AffineTransform::d() const
       
   169 {
       
   170     return m_transform.yy;
       
   171 }
       
   172 
       
   173 void AffineTransform::setD(double d)
       
   174 {
       
   175     m_transform.yy = d;
       
   176 }
       
   177 
       
   178 double AffineTransform::e() const
       
   179 {
       
   180     return m_transform.x0;
       
   181 }
       
   182 
       
   183 void AffineTransform::setE(double e)
       
   184 {
       
   185     m_transform.x0 = e;
       
   186 }
       
   187 
       
   188 double AffineTransform::f() const
       
   189 {
       
   190     return m_transform.y0;
       
   191 }
       
   192 
       
   193 void AffineTransform::setF(double f)
       
   194 {
       
   195     m_transform.y0 = f;
       
   196 }
       
   197 
       
   198 void AffineTransform::reset()
       
   199 {
       
   200     cairo_matrix_init_identity(&m_transform);
       
   201 }
       
   202 
       
   203 AffineTransform &AffineTransform::scale(double sx, double sy)
       
   204 {
       
   205     cairo_matrix_scale(&m_transform, sx, sy);
       
   206     return *this;
       
   207 }
       
   208 
       
   209 AffineTransform &AffineTransform::rotate(double d)
       
   210 {
       
   211     cairo_matrix_rotate(&m_transform, d * deg2rad);
       
   212     return *this;
       
   213 }
       
   214 
       
   215 AffineTransform &AffineTransform::translate(double tx, double ty)
       
   216 {
       
   217     cairo_matrix_translate(&m_transform, tx, ty);
       
   218     return *this;
       
   219 }
       
   220 
       
   221 AffineTransform &AffineTransform::shear(double sx, double sy)
       
   222 {
       
   223     cairo_matrix_t shear;
       
   224     cairo_matrix_init(&shear, 1, sy, sx, 1, 0, 0);
       
   225 
       
   226     cairo_matrix_t result;
       
   227     cairo_matrix_multiply(&result, &shear, &m_transform);
       
   228     m_transform = result;
       
   229 
       
   230     return *this;
       
   231 }
       
   232 
       
   233 double AffineTransform::det() const
       
   234 {
       
   235     return m_transform.xx * m_transform.yy - m_transform.xy * m_transform.yx;
       
   236 }
       
   237 
       
   238 AffineTransform AffineTransform::inverse() const
       
   239 {
       
   240     if (!isInvertible()) return AffineTransform();
       
   241 
       
   242     cairo_matrix_t result = m_transform;
       
   243     cairo_matrix_invert(&result);
       
   244     return AffineTransform(result);
       
   245 }
       
   246 
       
   247 AffineTransform::operator cairo_matrix_t() const
       
   248 {
       
   249     return m_transform;
       
   250 }
       
   251 
       
   252 bool AffineTransform::operator== (const AffineTransform &m2) const
       
   253 {
       
   254     return ((m_transform.xx == m2.m_transform.xx) 
       
   255          && (m_transform.yy == m2.m_transform.yy) 
       
   256          && (m_transform.xy == m2.m_transform.xy)
       
   257          && (m_transform.yx == m2.m_transform.yx) 
       
   258          && (m_transform.x0 == m2.m_transform.x0)
       
   259          && (m_transform.y0 == m2.m_transform.y0));
       
   260 
       
   261 }
       
   262 
       
   263 AffineTransform &AffineTransform::operator*= (const AffineTransform &m2)
       
   264 {
       
   265     cairo_matrix_t result;
       
   266     cairo_matrix_multiply(&result, &m_transform, &m2.m_transform);
       
   267     m_transform = result;
       
   268 
       
   269     return *this;
       
   270 }
       
   271 
       
   272 AffineTransform AffineTransform::operator* (const AffineTransform &m2)
       
   273 {
       
   274     cairo_matrix_t result;
       
   275     cairo_matrix_multiply(&result, &m_transform, &m2.m_transform);
       
   276     return result;
       
   277 }
       
   278 
       
   279 }
       
   280 
       
   281 // vim: ts=4 sw=4 et