|
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/. */ |
|
5 |
|
6 #include "gfxMatrix.h" |
|
7 #include "cairo.h" |
|
8 #include "mozilla/gfx/Tools.h" |
|
9 |
|
10 #define CAIRO_MATRIX(x) reinterpret_cast<cairo_matrix_t*>((x)) |
|
11 #define CONST_CAIRO_MATRIX(x) reinterpret_cast<const cairo_matrix_t*>((x)) |
|
12 |
|
13 const gfxMatrix& |
|
14 gfxMatrix::Reset() |
|
15 { |
|
16 cairo_matrix_init_identity(CAIRO_MATRIX(this)); |
|
17 return *this; |
|
18 } |
|
19 |
|
20 const gfxMatrix& |
|
21 gfxMatrix::Invert() |
|
22 { |
|
23 cairo_matrix_invert(CAIRO_MATRIX(this)); |
|
24 return *this; |
|
25 } |
|
26 |
|
27 const gfxMatrix& |
|
28 gfxMatrix::Scale(gfxFloat x, gfxFloat y) |
|
29 { |
|
30 cairo_matrix_scale(CAIRO_MATRIX(this), x, y); |
|
31 return *this; |
|
32 } |
|
33 |
|
34 const gfxMatrix& |
|
35 gfxMatrix::Translate(const gfxPoint& pt) |
|
36 { |
|
37 cairo_matrix_translate(CAIRO_MATRIX(this), pt.x, pt.y); |
|
38 return *this; |
|
39 } |
|
40 |
|
41 const gfxMatrix& |
|
42 gfxMatrix::Rotate(gfxFloat radians) |
|
43 { |
|
44 cairo_matrix_rotate(CAIRO_MATRIX(this), radians); |
|
45 return *this; |
|
46 } |
|
47 |
|
48 const gfxMatrix& |
|
49 gfxMatrix::Multiply(const gfxMatrix& m) |
|
50 { |
|
51 cairo_matrix_multiply(CAIRO_MATRIX(this), CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m)); |
|
52 return *this; |
|
53 } |
|
54 |
|
55 const gfxMatrix& |
|
56 gfxMatrix::PreMultiply(const gfxMatrix& m) |
|
57 { |
|
58 cairo_matrix_multiply(CAIRO_MATRIX(this), CONST_CAIRO_MATRIX(&m), CAIRO_MATRIX(this)); |
|
59 return *this; |
|
60 } |
|
61 |
|
62 gfxPoint |
|
63 gfxMatrix::Transform(const gfxPoint& point) const |
|
64 { |
|
65 gfxPoint ret = point; |
|
66 cairo_matrix_transform_point(CONST_CAIRO_MATRIX(this), &ret.x, &ret.y); |
|
67 return ret; |
|
68 } |
|
69 |
|
70 gfxSize |
|
71 gfxMatrix::Transform(const gfxSize& size) const |
|
72 { |
|
73 gfxSize ret = size; |
|
74 cairo_matrix_transform_distance(CONST_CAIRO_MATRIX(this), &ret.width, &ret.height); |
|
75 return ret; |
|
76 } |
|
77 |
|
78 gfxRect |
|
79 gfxMatrix::Transform(const gfxRect& rect) const |
|
80 { |
|
81 return gfxRect(Transform(rect.TopLeft()), Transform(rect.Size())); |
|
82 } |
|
83 |
|
84 gfxRect |
|
85 gfxMatrix::TransformBounds(const gfxRect& rect) const |
|
86 { |
|
87 /* Code taken from cairo-matrix.c, _cairo_matrix_transform_bounding_box isn't public */ |
|
88 int i; |
|
89 double quad_x[4], quad_y[4]; |
|
90 double min_x, max_x; |
|
91 double min_y, max_y; |
|
92 |
|
93 quad_x[0] = rect.X(); |
|
94 quad_y[0] = rect.Y(); |
|
95 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[0], &quad_y[0]); |
|
96 |
|
97 quad_x[1] = rect.XMost(); |
|
98 quad_y[1] = rect.Y(); |
|
99 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[1], &quad_y[1]); |
|
100 |
|
101 quad_x[2] = rect.X(); |
|
102 quad_y[2] = rect.YMost(); |
|
103 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[2], &quad_y[2]); |
|
104 |
|
105 quad_x[3] = rect.XMost(); |
|
106 quad_y[3] = rect.YMost(); |
|
107 cairo_matrix_transform_point (CONST_CAIRO_MATRIX(this), &quad_x[3], &quad_y[3]); |
|
108 |
|
109 min_x = max_x = quad_x[0]; |
|
110 min_y = max_y = quad_y[0]; |
|
111 |
|
112 for (i = 1; i < 4; i++) { |
|
113 if (quad_x[i] < min_x) |
|
114 min_x = quad_x[i]; |
|
115 if (quad_x[i] > max_x) |
|
116 max_x = quad_x[i]; |
|
117 |
|
118 if (quad_y[i] < min_y) |
|
119 min_y = quad_y[i]; |
|
120 if (quad_y[i] > max_y) |
|
121 max_y = quad_y[i]; |
|
122 } |
|
123 |
|
124 return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y); |
|
125 } |
|
126 |
|
127 |
|
128 static void NudgeToInteger(double *aVal) |
|
129 { |
|
130 float f = float(*aVal); |
|
131 mozilla::gfx::NudgeToInteger(&f); |
|
132 *aVal = f; |
|
133 } |
|
134 |
|
135 void |
|
136 gfxMatrix::NudgeToIntegers(void) |
|
137 { |
|
138 NudgeToInteger(&xx); |
|
139 NudgeToInteger(&xy); |
|
140 NudgeToInteger(&yx); |
|
141 NudgeToInteger(&yy); |
|
142 NudgeToInteger(&x0); |
|
143 NudgeToInteger(&y0); |
|
144 } |