TransformationMatrixCairo.cpp [plain text]
#include "config.h"
#include "TransformationMatrix.h"
#include "IntRect.h"
#include "FloatRect.h"
#include <cairo.h>
namespace WebCore {
static const double deg2rad = 0.017453292519943295769;
TransformationMatrix::TransformationMatrix()
{
cairo_matrix_init_identity(&m_transform);
}
TransformationMatrix::TransformationMatrix(double a, double b, double c, double d, double tx, double ty)
{
cairo_matrix_init(&m_transform, a, b, c, d, tx, ty);
}
TransformationMatrix::TransformationMatrix(const PlatformTransformationMatrix& matrix)
{
m_transform = matrix;
}
void TransformationMatrix::setMatrix(double a, double b, double c, double d, double tx, double ty)
{
cairo_matrix_init(&m_transform, a, b, c, d, tx, ty);
}
void TransformationMatrix::map(double x, double y, double* x2, double* y2) const
{
*x2 = x;
*y2 = y;
cairo_matrix_transform_point(&m_transform, x2, y2);
}
IntRect TransformationMatrix::mapRect(const IntRect &rect) const
{
FloatRect floatRect(rect);
FloatRect enclosingFloatRect = this->mapRect(floatRect);
return enclosingIntRect(enclosingFloatRect);
}
FloatRect TransformationMatrix::mapRect(const FloatRect &rect) const
{
double rectMinX = rect.x();
double rectMaxX = rect.x() + rect.width();
double rectMinY = rect.y();
double rectMaxY = rect.y() + rect.height();
double px = rectMinX;
double py = rectMinY;
cairo_matrix_transform_point(&m_transform, &px, &py);
double enclosingRectMinX = px;
double enclosingRectMinY = py;
double enclosingRectMaxX = px;
double enclosingRectMaxY = py;
px = rectMaxX;
py = rectMinY;
cairo_matrix_transform_point(&m_transform, &px, &py);
if (px < enclosingRectMinX)
enclosingRectMinX = px;
else if (px > enclosingRectMaxX)
enclosingRectMaxX = px;
if (py < enclosingRectMinY)
enclosingRectMinY = py;
else if (py > enclosingRectMaxY)
enclosingRectMaxY = py;
px = rectMaxX;
py = rectMaxY;
cairo_matrix_transform_point(&m_transform, &px, &py);
if (px < enclosingRectMinX)
enclosingRectMinX = px;
else if (px > enclosingRectMaxX)
enclosingRectMaxX = px;
if (py < enclosingRectMinY)
enclosingRectMinY = py;
else if (py > enclosingRectMaxY)
enclosingRectMaxY = py;
px = rectMinX;
py = rectMaxY;
cairo_matrix_transform_point(&m_transform, &px, &py);
if (px < enclosingRectMinX)
enclosingRectMinX = px;
else if (px > enclosingRectMaxX)
enclosingRectMaxX = px;
if (py < enclosingRectMinY)
enclosingRectMinY = py;
else if (py > enclosingRectMaxY)
enclosingRectMaxY = py;
double enclosingRectWidth = enclosingRectMaxX - enclosingRectMinX;
double enclosingRectHeight = enclosingRectMaxY - enclosingRectMinY;
return FloatRect(enclosingRectMinX, enclosingRectMinY, enclosingRectWidth, enclosingRectHeight);
}
bool TransformationMatrix::isIdentity() const
{
return ((m_transform.xx == 1) && (m_transform.yy == 1)
&& (m_transform.xy == 0) && (m_transform.yx == 0)
&& (m_transform.x0 == 0) && (m_transform.y0 == 0));
}
double TransformationMatrix::a() const
{
return m_transform.xx;
}
void TransformationMatrix::setA(double a)
{
m_transform.xx = a;
}
double TransformationMatrix::b() const
{
return m_transform.yx;
}
void TransformationMatrix::setB(double b)
{
m_transform.yx = b;
}
double TransformationMatrix::c() const
{
return m_transform.xy;
}
void TransformationMatrix::setC(double c)
{
m_transform.xy = c;
}
double TransformationMatrix::d() const
{
return m_transform.yy;
}
void TransformationMatrix::setD(double d)
{
m_transform.yy = d;
}
double TransformationMatrix::e() const
{
return m_transform.x0;
}
void TransformationMatrix::setE(double e)
{
m_transform.x0 = e;
}
double TransformationMatrix::f() const
{
return m_transform.y0;
}
void TransformationMatrix::setF(double f)
{
m_transform.y0 = f;
}
void TransformationMatrix::reset()
{
cairo_matrix_init_identity(&m_transform);
}
TransformationMatrix &TransformationMatrix::scale(double sx, double sy)
{
cairo_matrix_scale(&m_transform, sx, sy);
return *this;
}
TransformationMatrix &TransformationMatrix::rotate(double d)
{
cairo_matrix_rotate(&m_transform, d * deg2rad);
return *this;
}
TransformationMatrix &TransformationMatrix::translate(double tx, double ty)
{
cairo_matrix_translate(&m_transform, tx, ty);
return *this;
}
TransformationMatrix &TransformationMatrix::shear(double sx, double sy)
{
cairo_matrix_t shear;
cairo_matrix_init(&shear, 1, sy, sx, 1, 0, 0);
cairo_matrix_t result;
cairo_matrix_multiply(&result, &shear, &m_transform);
m_transform = result;
return *this;
}
double TransformationMatrix::det() const
{
return m_transform.xx * m_transform.yy - m_transform.xy * m_transform.yx;
}
TransformationMatrix TransformationMatrix::inverse() const
{
if (!isInvertible()) return TransformationMatrix();
cairo_matrix_t result = m_transform;
cairo_matrix_invert(&result);
return TransformationMatrix(result);
}
TransformationMatrix::operator cairo_matrix_t() const
{
return m_transform;
}
bool TransformationMatrix::operator== (const TransformationMatrix &m2) const
{
return ((m_transform.xx == m2.m_transform.xx)
&& (m_transform.yy == m2.m_transform.yy)
&& (m_transform.xy == m2.m_transform.xy)
&& (m_transform.yx == m2.m_transform.yx)
&& (m_transform.x0 == m2.m_transform.x0)
&& (m_transform.y0 == m2.m_transform.y0));
}
TransformationMatrix &TransformationMatrix::operator*= (const TransformationMatrix &m2)
{
cairo_matrix_t result;
cairo_matrix_multiply(&result, &m_transform, &m2.m_transform);
m_transform = result;
return *this;
}
TransformationMatrix TransformationMatrix::operator* (const TransformationMatrix &m2)
{
cairo_matrix_t result;
cairo_matrix_multiply(&result, &m_transform, &m2.m_transform);
return result;
}
}