1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 * @short_description: #cairo_matrix_t enhancements and utilities.
27 * This API provides a #GBoxed wrapper around #cairo_matrix_t
28 * (if not yet provided by cairo-gobject) * and augments its methods
29 * with some useful addition.
35 #include "adg-internal.h"
39 #include "adg-matrix-fallback.h"
42 #ifdef ADG_MISSING_GBOXED_MATRIX
45 cairo_gobject_matrix_get_type(void)
47 static GType matrix_type
= 0;
49 if (G_UNLIKELY(matrix_type
== 0))
50 matrix_type
= g_boxed_type_register_static("CairoMatrix",
51 (GBoxedCopyFunc
) cairo_gobject_cairo_matrix_copy
,
58 cairo_gobject_cairo_matrix_copy(const cairo_matrix_t
*matrix
)
60 return g_memdup(matrix
, sizeof(cairo_matrix_t
));
63 #endif /* ADG_MISSING_GBOXED_MATRIX */
67 * adg_matrix_identity:
69 * A convenient constant providing an identity matrix.
71 * Returns: (transfer none): a pointer to the identity matrix
75 const cairo_matrix_t
*
76 adg_matrix_identity(void)
78 static cairo_matrix_t
*identity_matrix
= NULL
;
80 if (G_UNLIKELY(identity_matrix
== NULL
)) {
81 identity_matrix
= g_new(cairo_matrix_t
, 1);
82 cairo_matrix_init_identity(identity_matrix
);
85 return identity_matrix
;
91 * A convenient constant providing an null matrix, that is a matrix
92 * where all components are 0.
94 * Returns: (transfer none): a pointer to the null matrix
98 const cairo_matrix_t
*
101 static cairo_matrix_t
*null_matrix
= NULL
;
103 if (G_UNLIKELY(null_matrix
== NULL
))
104 null_matrix
= g_new0(cairo_matrix_t
, 1);
111 * @matrix: (out caller-allocates): the destination #cairo_matrix_t
112 * @src: the source #cairo_matrix_t
114 * Copies @src to @matrix.
119 adg_matrix_copy(cairo_matrix_t
*matrix
, const cairo_matrix_t
*src
)
121 g_return_if_fail(matrix
!= NULL
);
122 g_return_if_fail(src
!= NULL
);
124 memcpy(matrix
, src
, sizeof(cairo_matrix_t
));
129 * @matrix: the souce #cairo_matrix_t
131 * Duplicates @matrix.
133 * Returns: (transfer full): a duplicate of @matrix that must be freed with g_free() when no longer needed.
138 adg_matrix_dup(const cairo_matrix_t
*matrix
)
140 g_return_val_if_fail(matrix
!= NULL
, NULL
);
142 return g_memdup(matrix
, sizeof(cairo_matrix_t
));
147 * @matrix1: the first operand
148 * @matrix2: the second operand
150 * Compares @matrix1 and @matrix2 and returns %TRUE if the matrices are equal.
152 * Returns: %TRUE if @matrix1 is equal to @matrix2, %FALSE otherwise
157 adg_matrix_equal(const cairo_matrix_t
*matrix1
, const cairo_matrix_t
*matrix2
)
159 g_return_val_if_fail(matrix1
!= NULL
, FALSE
);
160 g_return_val_if_fail(matrix2
!= NULL
, FALSE
);
163 matrix1
->xx
== matrix2
->xx
&&
164 matrix1
->yx
== matrix2
->yx
&&
165 matrix1
->xy
== matrix2
->xy
&&
166 matrix1
->yy
== matrix2
->yy
&&
167 matrix1
->x0
== matrix2
->x0
&&
168 matrix1
->y0
== matrix2
->y0
;
172 * adg_matrix_normalize:
173 * @matrix: (inout): the source/destination #cairo_matrix_t
175 * Gets rid of the scaling component of a matrix.
177 * Returns: %TRUE on success, %FALSE on errors
182 adg_matrix_normalize(cairo_matrix_t
*matrix
)
186 g_return_val_if_fail(matrix
!= NULL
, FALSE
);
188 if (matrix
->xx
!= matrix
->yy
|| matrix
->xy
!= -matrix
->yx
) {
189 g_warning(_("%s: normalization of anamorphic matrices not supported"),
194 if (matrix
->xy
== 0) {
196 } else if (matrix
->xx
== 0) {
199 k
= sqrt(matrix
->xx
* matrix
->xx
+ matrix
->xy
* matrix
->xy
);
202 g_return_val_if_fail(k
!= 0, FALSE
);
213 * adg_matrix_transform:
214 * @matrix: (inout): the source/destination #cairo_matrix_t
215 * @transformation: the transformation to apply
216 * @mode: (in): how @transformation should be applied
218 * Modifies @matrix applying @transformation in the way specified by
224 adg_matrix_transform(cairo_matrix_t
*matrix
,
225 const cairo_matrix_t
*transformation
, AdgTransformMode mode
)
227 cairo_matrix_t normalized
;
229 g_return_if_fail(matrix
!= NULL
);
230 g_return_if_fail(transformation
!= NULL
);
233 case ADG_TRANSFORM_NONE
:
235 case ADG_TRANSFORM_BEFORE
:
236 cairo_matrix_multiply(matrix
, transformation
, matrix
);
238 case ADG_TRANSFORM_AFTER
:
239 cairo_matrix_multiply(matrix
, matrix
, transformation
);
241 case ADG_TRANSFORM_BEFORE_NORMALIZED
:
242 adg_matrix_copy(&normalized
, transformation
);
243 adg_matrix_normalize(&normalized
);
244 cairo_matrix_multiply(matrix
, &normalized
, matrix
);
246 case ADG_TRANSFORM_AFTER_NORMALIZED
:
247 adg_matrix_copy(&normalized
, transformation
);
248 adg_matrix_normalize(&normalized
);
249 cairo_matrix_multiply(matrix
, matrix
, &normalized
);
252 g_return_if_reached();
258 * @matrix: an #cairo_matrix_t
260 * Dumps the specified @matrix to stdout. Useful for debugging purposes.
265 adg_matrix_dump(const cairo_matrix_t
*matrix
)
267 g_return_if_fail(matrix
!= NULL
);
269 g_print("[%8.3lf %8.3lf] [%8.3lf]\n"
270 "[%8.3lf %8.3lf] [%8.3lf]\n",
271 matrix
->xx
, matrix
->xy
, matrix
->x0
,
272 matrix
->yx
, matrix
->yy
, matrix
->y0
);