Bumping manifests a=b2g-bump
[gecko.git] / gfx / thebes / gfxMatrix.cpp
blob9fe86d134f1dd116b4536722292378588898845a
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "gfxMatrix.h"
7 #include "cairo.h"
8 #include "mozilla/gfx/Tools.h"
10 #define CAIRO_MATRIX(x) reinterpret_cast<cairo_matrix_t*>((x))
11 #define CONST_CAIRO_MATRIX(x) reinterpret_cast<const cairo_matrix_t*>((x))
13 const gfxMatrix&
14 gfxMatrix::Reset()
16 cairo_matrix_init_identity(CAIRO_MATRIX(this));
17 return *this;
20 bool
21 gfxMatrix::Invert()
23 return cairo_matrix_invert(CAIRO_MATRIX(this)) == CAIRO_STATUS_SUCCESS;
26 const gfxMatrix&
27 gfxMatrix::Scale(gfxFloat x, gfxFloat y)
29 cairo_matrix_scale(CAIRO_MATRIX(this), x, y);
30 return *this;
33 const gfxMatrix&
34 gfxMatrix::Translate(const gfxPoint& pt)
36 cairo_matrix_translate(CAIRO_MATRIX(this), pt.x, pt.y);
37 return *this;
40 const gfxMatrix&
41 gfxMatrix::Rotate(gfxFloat radians)
43 cairo_matrix_rotate(CAIRO_MATRIX(this), radians);
44 return *this;
47 const gfxMatrix&
48 gfxMatrix::operator *= (const gfxMatrix& m)
50 cairo_matrix_multiply(CAIRO_MATRIX(this), CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m));
51 return *this;
54 const gfxMatrix&
55 gfxMatrix::PreMultiply(const gfxMatrix& m)
57 cairo_matrix_multiply(CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m), CAIRO_MATRIX(this));
58 return *this;
61 /* static */ gfxMatrix
62 gfxMatrix::Rotation(gfxFloat aAngle)
64 gfxMatrix newMatrix;
66 gfxFloat s = sin(aAngle);
67 gfxFloat c = cos(aAngle);
69 newMatrix._11 = c;
70 newMatrix._12 = s;
71 newMatrix._21 = -s;
72 newMatrix._22 = c;
74 return newMatrix;
77 gfxPoint
78 gfxMatrix::Transform(const gfxPoint& point) const
80 gfxPoint ret = point;
81 cairo_matrix_transform_point(CONST_CAIRO_MATRIX(this), &ret.x, &ret.y);
82 return ret;
85 gfxSize
86 gfxMatrix::Transform(const gfxSize& size) const
88 gfxSize ret = size;
89 cairo_matrix_transform_distance(CONST_CAIRO_MATRIX(this), &ret.width, &ret.height);
90 return ret;
93 gfxRect
94 gfxMatrix::Transform(const gfxRect& rect) const
96 return gfxRect(Transform(rect.TopLeft()), Transform(rect.Size()));
99 gfxRect
100 gfxMatrix::TransformBounds(const gfxRect& rect) const
102 /* Code taken from cairo-matrix.c, _cairo_matrix_transform_bounding_box isn't public */
103 int i;
104 double quad_x[4], quad_y[4];
105 double min_x, max_x;
106 double min_y, max_y;
108 quad_x[0] = rect.X();
109 quad_y[0] = rect.Y();
110 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[0], &quad_y[0]);
112 quad_x[1] = rect.XMost();
113 quad_y[1] = rect.Y();
114 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[1], &quad_y[1]);
116 quad_x[2] = rect.X();
117 quad_y[2] = rect.YMost();
118 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[2], &quad_y[2]);
120 quad_x[3] = rect.XMost();
121 quad_y[3] = rect.YMost();
122 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[3], &quad_y[3]);
124 min_x = max_x = quad_x[0];
125 min_y = max_y = quad_y[0];
127 for (i = 1; i < 4; i++) {
128 if (quad_x[i] < min_x)
129 min_x = quad_x[i];
130 if (quad_x[i] > max_x)
131 max_x = quad_x[i];
133 if (quad_y[i] < min_y)
134 min_y = quad_y[i];
135 if (quad_y[i] > max_y)
136 max_y = quad_y[i];
139 return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
143 static void NudgeToInteger(double *aVal)
145 float f = float(*aVal);
146 mozilla::gfx::NudgeToInteger(&f);
147 *aVal = f;
150 void
151 gfxMatrix::NudgeToIntegers(void)
153 NudgeToInteger(&_11);
154 NudgeToInteger(&_21);
155 NudgeToInteger(&_12);
156 NudgeToInteger(&_22);
157 NudgeToInteger(&_31);
158 NudgeToInteger(&_32);