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/. */
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))
16 cairo_matrix_init_identity(CAIRO_MATRIX(this));
23 return cairo_matrix_invert(CAIRO_MATRIX(this)) == CAIRO_STATUS_SUCCESS
;
27 gfxMatrix::Scale(gfxFloat x
, gfxFloat y
)
29 cairo_matrix_scale(CAIRO_MATRIX(this), x
, y
);
34 gfxMatrix::Translate(const gfxPoint
& pt
)
36 cairo_matrix_translate(CAIRO_MATRIX(this), pt
.x
, pt
.y
);
41 gfxMatrix::Rotate(gfxFloat radians
)
43 cairo_matrix_rotate(CAIRO_MATRIX(this), radians
);
48 gfxMatrix::operator *= (const gfxMatrix
& m
)
50 cairo_matrix_multiply(CAIRO_MATRIX(this), CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m
));
55 gfxMatrix::PreMultiply(const gfxMatrix
& m
)
57 cairo_matrix_multiply(CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m
), CAIRO_MATRIX(this));
61 /* static */ gfxMatrix
62 gfxMatrix::Rotation(gfxFloat aAngle
)
66 gfxFloat s
= sin(aAngle
);
67 gfxFloat c
= cos(aAngle
);
78 gfxMatrix::Transform(const gfxPoint
& point
) const
81 cairo_matrix_transform_point(CONST_CAIRO_MATRIX(this), &ret
.x
, &ret
.y
);
86 gfxMatrix::Transform(const gfxSize
& size
) const
89 cairo_matrix_transform_distance(CONST_CAIRO_MATRIX(this), &ret
.width
, &ret
.height
);
94 gfxMatrix::Transform(const gfxRect
& rect
) const
96 return gfxRect(Transform(rect
.TopLeft()), Transform(rect
.Size()));
100 gfxMatrix::TransformBounds(const gfxRect
& rect
) const
102 /* Code taken from cairo-matrix.c, _cairo_matrix_transform_bounding_box isn't public */
104 double quad_x
[4], quad_y
[4];
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
)
130 if (quad_x
[i
] > max_x
)
133 if (quad_y
[i
] < min_y
)
135 if (quad_y
[i
] > max_y
)
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
);
151 gfxMatrix::NudgeToIntegers(void)
153 NudgeToInteger(&_11
);
154 NudgeToInteger(&_21
);
155 NudgeToInteger(&_12
);
156 NudgeToInteger(&_22
);
157 NudgeToInteger(&_31
);
158 NudgeToInteger(&_32
);