/work/workdir/UnpackedTarball/cairo/src/cairo-matrix.c
Line | Count | Source |
1 | | /* cairo - a vector graphics library with display and print output |
2 | | * |
3 | | * Copyright © 2002 University of Southern California |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it either under the terms of the GNU Lesser General Public |
7 | | * License version 2.1 as published by the Free Software Foundation |
8 | | * (the "LGPL") or, at your option, under the terms of the Mozilla |
9 | | * Public License Version 1.1 (the "MPL"). If you do not alter this |
10 | | * notice, a recipient may use your version of this file under either |
11 | | * the MPL or the LGPL. |
12 | | * |
13 | | * You should have received a copy of the LGPL along with this library |
14 | | * in the file COPYING-LGPL-2.1; if not, write to the Free Software |
15 | | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA |
16 | | * You should have received a copy of the MPL along with this library |
17 | | * in the file COPYING-MPL-1.1 |
18 | | * |
19 | | * The contents of this file are subject to the Mozilla Public License |
20 | | * Version 1.1 (the "License"); you may not use this file except in |
21 | | * compliance with the License. You may obtain a copy of the License at |
22 | | * http://www.mozilla.org/MPL/ |
23 | | * |
24 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY |
25 | | * OF ANY KIND, either express or implied. See the LGPL or the MPL for |
26 | | * the specific language governing rights and limitations. |
27 | | * |
28 | | * The Original Code is the cairo graphics library. |
29 | | * |
30 | | * The Initial Developer of the Original Code is University of Southern |
31 | | * California. |
32 | | * |
33 | | * Contributor(s): |
34 | | * Carl D. Worth <cworth@cworth.org> |
35 | | */ |
36 | | |
37 | | #include "cairoint.h" |
38 | | #include "cairo-error-private.h" |
39 | | #include <float.h> |
40 | | |
41 | 1.44M | #define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */ |
42 | | |
43 | | /** |
44 | | * SECTION:cairo-matrix |
45 | | * @Title: cairo_matrix_t |
46 | | * @Short_Description: Generic matrix operations |
47 | | * @See_Also: #cairo_t |
48 | | * |
49 | | * #cairo_matrix_t is used throughout cairo to convert between different |
50 | | * coordinate spaces. A #cairo_matrix_t holds an affine transformation, |
51 | | * such as a scale, rotation, shear, or a combination of these. |
52 | | * The transformation of a point (<literal>x</literal>,<literal>y</literal>) |
53 | | * is given by: |
54 | | * |
55 | | * <programlisting> |
56 | | * x_new = xx * x + xy * y + x0; |
57 | | * y_new = yx * x + yy * y + y0; |
58 | | * </programlisting> |
59 | | * |
60 | | * The current transformation matrix of a #cairo_t, represented as a |
61 | | * #cairo_matrix_t, defines the transformation from user-space |
62 | | * coordinates to device-space coordinates. See cairo_get_matrix() and |
63 | | * cairo_set_matrix(). |
64 | | **/ |
65 | | |
66 | | static void |
67 | | _cairo_matrix_scalar_multiply (cairo_matrix_t *matrix, double scalar); |
68 | | |
69 | | static void |
70 | | _cairo_matrix_compute_adjoint (cairo_matrix_t *matrix); |
71 | | |
72 | | /** |
73 | | * cairo_matrix_init_identity: |
74 | | * @matrix: a #cairo_matrix_t |
75 | | * |
76 | | * Modifies @matrix to be an identity transformation. |
77 | | * |
78 | | * Since: 1.0 |
79 | | **/ |
80 | | void |
81 | | cairo_matrix_init_identity (cairo_matrix_t *matrix) |
82 | 27.6M | { |
83 | 27.6M | cairo_matrix_init (matrix, |
84 | 27.6M | 1, 0, |
85 | 27.6M | 0, 1, |
86 | 27.6M | 0, 0); |
87 | 27.6M | } |
88 | | |
89 | | /** |
90 | | * cairo_matrix_init: |
91 | | * @matrix: a #cairo_matrix_t |
92 | | * @xx: xx component of the affine transformation |
93 | | * @yx: yx component of the affine transformation |
94 | | * @xy: xy component of the affine transformation |
95 | | * @yy: yy component of the affine transformation |
96 | | * @x0: X translation component of the affine transformation |
97 | | * @y0: Y translation component of the affine transformation |
98 | | * |
99 | | * Sets @matrix to be the affine transformation given by |
100 | | * @xx, @yx, @xy, @yy, @x0, @y0. The transformation is given |
101 | | * by: |
102 | | * <programlisting> |
103 | | * x_new = xx * x + xy * y + x0; |
104 | | * y_new = yx * x + yy * y + y0; |
105 | | * </programlisting> |
106 | | * |
107 | | * Since: 1.0 |
108 | | **/ |
109 | | void |
110 | | cairo_matrix_init (cairo_matrix_t *matrix, |
111 | | double xx, double yx, |
112 | | |
113 | | double xy, double yy, |
114 | | double x0, double y0) |
115 | 52.3M | { |
116 | 52.3M | matrix->xx = xx; matrix->yx = yx; |
117 | 52.3M | matrix->xy = xy; matrix->yy = yy; |
118 | 52.3M | matrix->x0 = x0; matrix->y0 = y0; |
119 | 52.3M | } |
120 | | |
121 | | /** |
122 | | * _cairo_matrix_get_affine: |
123 | | * @matrix: a #cairo_matrix_t |
124 | | * @xx: location to store xx component of matrix |
125 | | * @yx: location to store yx component of matrix |
126 | | * @xy: location to store xy component of matrix |
127 | | * @yy: location to store yy component of matrix |
128 | | * @x0: location to store x0 (X-translation component) of matrix, or %NULL |
129 | | * @y0: location to store y0 (Y-translation component) of matrix, or %NULL |
130 | | * |
131 | | * Gets the matrix values for the affine transformation that @matrix represents. |
132 | | * See cairo_matrix_init(). |
133 | | * |
134 | | * |
135 | | * This function is a leftover from the old public API, but is still |
136 | | * mildly useful as an internal means for getting at the matrix |
137 | | * members in a positional way. For example, when reassigning to some |
138 | | * external matrix type, or when renaming members to more meaningful |
139 | | * names (such as a,b,c,d,e,f) for particular manipulations. |
140 | | **/ |
141 | | void |
142 | | _cairo_matrix_get_affine (const cairo_matrix_t *matrix, |
143 | | double *xx, double *yx, |
144 | | double *xy, double *yy, |
145 | | double *x0, double *y0) |
146 | 117k | { |
147 | 117k | *xx = matrix->xx; |
148 | 117k | *yx = matrix->yx; |
149 | | |
150 | 117k | *xy = matrix->xy; |
151 | 117k | *yy = matrix->yy; |
152 | | |
153 | 117k | if (x0) |
154 | 417 | *x0 = matrix->x0; |
155 | 117k | if (y0) |
156 | 417 | *y0 = matrix->y0; |
157 | 117k | } |
158 | | |
159 | | /** |
160 | | * cairo_matrix_init_translate: |
161 | | * @matrix: a #cairo_matrix_t |
162 | | * @tx: amount to translate in the X direction |
163 | | * @ty: amount to translate in the Y direction |
164 | | * |
165 | | * Initializes @matrix to a transformation that translates by @tx and |
166 | | * @ty in the X and Y dimensions, respectively. |
167 | | * |
168 | | * Since: 1.0 |
169 | | **/ |
170 | | void |
171 | | cairo_matrix_init_translate (cairo_matrix_t *matrix, |
172 | | double tx, double ty) |
173 | 1.59M | { |
174 | 1.59M | cairo_matrix_init (matrix, |
175 | 1.59M | 1, 0, |
176 | 1.59M | 0, 1, |
177 | 1.59M | tx, ty); |
178 | 1.59M | } |
179 | | |
180 | | /** |
181 | | * cairo_matrix_translate: |
182 | | * @matrix: a #cairo_matrix_t |
183 | | * @tx: amount to translate in the X direction |
184 | | * @ty: amount to translate in the Y direction |
185 | | * |
186 | | * Applies a translation by @tx, @ty to the transformation in |
187 | | * @matrix. The effect of the new transformation is to first translate |
188 | | * the coordinates by @tx and @ty, then apply the original transformation |
189 | | * to the coordinates. |
190 | | * |
191 | | * Since: 1.0 |
192 | | **/ |
193 | | void |
194 | | cairo_matrix_translate (cairo_matrix_t *matrix, double tx, double ty) |
195 | 2.18k | { |
196 | 2.18k | cairo_matrix_t tmp; |
197 | | |
198 | 2.18k | cairo_matrix_init_translate (&tmp, tx, ty); |
199 | | |
200 | 2.18k | cairo_matrix_multiply (matrix, &tmp, matrix); |
201 | 2.18k | } |
202 | | |
203 | | /** |
204 | | * cairo_matrix_init_scale: |
205 | | * @matrix: a #cairo_matrix_t |
206 | | * @sx: scale factor in the X direction |
207 | | * @sy: scale factor in the Y direction |
208 | | * |
209 | | * Initializes @matrix to a transformation that scales by @sx and @sy |
210 | | * in the X and Y dimensions, respectively. |
211 | | * |
212 | | * Since: 1.0 |
213 | | **/ |
214 | | void |
215 | | cairo_matrix_init_scale (cairo_matrix_t *matrix, |
216 | | double sx, double sy) |
217 | 19.2M | { |
218 | 19.2M | cairo_matrix_init (matrix, |
219 | 19.2M | sx, 0, |
220 | 19.2M | 0, sy, |
221 | 19.2M | 0, 0); |
222 | 19.2M | } |
223 | | |
224 | | /** |
225 | | * cairo_matrix_scale: |
226 | | * @matrix: a #cairo_matrix_t |
227 | | * @sx: scale factor in the X direction |
228 | | * @sy: scale factor in the Y direction |
229 | | * |
230 | | * Applies scaling by @sx, @sy to the transformation in @matrix. The |
231 | | * effect of the new transformation is to first scale the coordinates |
232 | | * by @sx and @sy, then apply the original transformation to the coordinates. |
233 | | * |
234 | | * Since: 1.0 |
235 | | **/ |
236 | | void |
237 | | cairo_matrix_scale (cairo_matrix_t *matrix, double sx, double sy) |
238 | 2.34M | { |
239 | 2.34M | cairo_matrix_t tmp; |
240 | | |
241 | 2.34M | cairo_matrix_init_scale (&tmp, sx, sy); |
242 | | |
243 | 2.34M | cairo_matrix_multiply (matrix, &tmp, matrix); |
244 | 2.34M | } |
245 | | |
246 | | /** |
247 | | * cairo_matrix_init_rotate: |
248 | | * @matrix: a #cairo_matrix_t |
249 | | * @radians: angle of rotation, in radians. The direction of rotation |
250 | | * is defined such that positive angles rotate in the direction from |
251 | | * the positive X axis toward the positive Y axis. With the default |
252 | | * axis orientation of cairo, positive angles rotate in a clockwise |
253 | | * direction. |
254 | | * |
255 | | * Initialized @matrix to a transformation that rotates by @radians. |
256 | | * |
257 | | * Since: 1.0 |
258 | | **/ |
259 | | void |
260 | | cairo_matrix_init_rotate (cairo_matrix_t *matrix, |
261 | | double radians) |
262 | 3.67M | { |
263 | 3.67M | double s; |
264 | 3.67M | double c; |
265 | | |
266 | 3.67M | s = sin (radians); |
267 | 3.67M | c = cos (radians); |
268 | | |
269 | 3.67M | cairo_matrix_init (matrix, |
270 | 3.67M | c, s, |
271 | 3.67M | -s, c, |
272 | 3.67M | 0, 0); |
273 | 3.67M | } |
274 | | |
275 | | /** |
276 | | * cairo_matrix_rotate: |
277 | | * @matrix: a #cairo_matrix_t |
278 | | * @radians: angle of rotation, in radians. The direction of rotation |
279 | | * is defined such that positive angles rotate in the direction from |
280 | | * the positive X axis toward the positive Y axis. With the default |
281 | | * axis orientation of cairo, positive angles rotate in a clockwise |
282 | | * direction. |
283 | | * |
284 | | * Applies rotation by @radians to the transformation in |
285 | | * @matrix. The effect of the new transformation is to first rotate the |
286 | | * coordinates by @radians, then apply the original transformation |
287 | | * to the coordinates. |
288 | | * |
289 | | * Since: 1.0 |
290 | | **/ |
291 | | void |
292 | | cairo_matrix_rotate (cairo_matrix_t *matrix, double radians) |
293 | 3.67M | { |
294 | 3.67M | cairo_matrix_t tmp; |
295 | | |
296 | 3.67M | cairo_matrix_init_rotate (&tmp, radians); |
297 | | |
298 | 3.67M | cairo_matrix_multiply (matrix, &tmp, matrix); |
299 | 3.67M | } |
300 | | |
301 | | /** |
302 | | * cairo_matrix_multiply: |
303 | | * @result: a #cairo_matrix_t in which to store the result |
304 | | * @a: a #cairo_matrix_t |
305 | | * @b: a #cairo_matrix_t |
306 | | * |
307 | | * Multiplies the affine transformations in @a and @b together |
308 | | * and stores the result in @result. The effect of the resulting |
309 | | * transformation is to first apply the transformation in @a to the |
310 | | * coordinates and then apply the transformation in @b to the |
311 | | * coordinates. |
312 | | * |
313 | | * It is allowable for @result to be identical to either @a or @b. |
314 | | * |
315 | | * Since: 1.0 |
316 | | **/ |
317 | | /* |
318 | | * XXX: The ordering of the arguments to this function corresponds |
319 | | * to [row_vector]*A*B. If we want to use column vectors instead, |
320 | | * then we need to switch the two arguments and fix up all |
321 | | * uses. |
322 | | */ |
323 | | void |
324 | | cairo_matrix_multiply (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b) |
325 | 12.5M | { |
326 | 12.5M | cairo_matrix_t r; |
327 | | |
328 | 12.5M | r.xx = a->xx * b->xx + a->yx * b->xy; |
329 | 12.5M | r.yx = a->xx * b->yx + a->yx * b->yy; |
330 | | |
331 | 12.5M | r.xy = a->xy * b->xx + a->yy * b->xy; |
332 | 12.5M | r.yy = a->xy * b->yx + a->yy * b->yy; |
333 | | |
334 | 12.5M | r.x0 = a->x0 * b->xx + a->y0 * b->xy + b->x0; |
335 | 12.5M | r.y0 = a->x0 * b->yx + a->y0 * b->yy + b->y0; |
336 | | |
337 | 12.5M | *result = r; |
338 | 12.5M | } |
339 | | |
340 | | void |
341 | | _cairo_matrix_multiply (cairo_matrix_t *r, |
342 | | const cairo_matrix_t *a, |
343 | | const cairo_matrix_t *b) |
344 | 0 | { |
345 | 0 | r->xx = a->xx * b->xx + a->yx * b->xy; |
346 | 0 | r->yx = a->xx * b->yx + a->yx * b->yy; |
347 | |
|
348 | 0 | r->xy = a->xy * b->xx + a->yy * b->xy; |
349 | 0 | r->yy = a->xy * b->yx + a->yy * b->yy; |
350 | |
|
351 | 0 | r->x0 = a->x0 * b->xx + a->y0 * b->xy + b->x0; |
352 | 0 | r->y0 = a->x0 * b->yx + a->y0 * b->yy + b->y0; |
353 | 0 | } |
354 | | |
355 | | /** |
356 | | * cairo_matrix_transform_distance: |
357 | | * @matrix: a #cairo_matrix_t |
358 | | * @dx: X component of a distance vector. An in/out parameter |
359 | | * @dy: Y component of a distance vector. An in/out parameter |
360 | | * |
361 | | * Transforms the distance vector (@dx,@dy) by @matrix. This is |
362 | | * similar to cairo_matrix_transform_point() except that the translation |
363 | | * components of the transformation are ignored. The calculation of |
364 | | * the returned vector is as follows: |
365 | | * |
366 | | * <programlisting> |
367 | | * dx_new = xx * dx + xy * dy; |
368 | | * dy_new = yx * dx + yy * dy; |
369 | | * </programlisting> |
370 | | * |
371 | | * Since: 1.0 |
372 | | **/ |
373 | | void |
374 | | cairo_matrix_transform_distance (const cairo_matrix_t *matrix, double *dx, double *dy) |
375 | 17.0M | { |
376 | 17.0M | double new_x, new_y; |
377 | | |
378 | 17.0M | new_x = (matrix->xx * *dx + matrix->xy * *dy); |
379 | 17.0M | new_y = (matrix->yx * *dx + matrix->yy * *dy); |
380 | | |
381 | 17.0M | *dx = new_x; |
382 | 17.0M | *dy = new_y; |
383 | 17.0M | } |
384 | | |
385 | | /** |
386 | | * cairo_matrix_transform_point: |
387 | | * @matrix: a #cairo_matrix_t |
388 | | * @x: X position. An in/out parameter |
389 | | * @y: Y position. An in/out parameter |
390 | | * |
391 | | * Transforms the point (@x, @y) by @matrix. |
392 | | * |
393 | | * Since: 1.0 |
394 | | **/ |
395 | | void |
396 | | cairo_matrix_transform_point (const cairo_matrix_t *matrix, double *x, double *y) |
397 | 13.6M | { |
398 | 13.6M | cairo_matrix_transform_distance (matrix, x, y); |
399 | | |
400 | 13.6M | *x += matrix->x0; |
401 | 13.6M | *y += matrix->y0; |
402 | 13.6M | } |
403 | | |
404 | | void |
405 | | _cairo_matrix_transform_bounding_box (const cairo_matrix_t *matrix, |
406 | | double *x1, double *y1, |
407 | | double *x2, double *y2, |
408 | | cairo_bool_t *is_tight) |
409 | 2.57M | { |
410 | 2.57M | int i; |
411 | 2.57M | double quad_x[4], quad_y[4]; |
412 | 2.57M | double min_x, max_x; |
413 | 2.57M | double min_y, max_y; |
414 | | |
415 | 2.57M | if (matrix->xy == 0. && matrix->yx == 0.) { |
416 | | /* non-rotation/skew matrix, just map the two extreme points */ |
417 | | |
418 | 2.57M | if (matrix->xx != 1.) { |
419 | 8.23k | quad_x[0] = *x1 * matrix->xx; |
420 | 8.23k | quad_x[1] = *x2 * matrix->xx; |
421 | 8.23k | if (quad_x[0] < quad_x[1]) { |
422 | 4.27k | *x1 = quad_x[0]; |
423 | 4.27k | *x2 = quad_x[1]; |
424 | 4.27k | } else { |
425 | 3.96k | *x1 = quad_x[1]; |
426 | 3.96k | *x2 = quad_x[0]; |
427 | 3.96k | } |
428 | 8.23k | } |
429 | 2.57M | if (matrix->x0 != 0.) { |
430 | 2.52M | *x1 += matrix->x0; |
431 | 2.52M | *x2 += matrix->x0; |
432 | 2.52M | } |
433 | | |
434 | 2.57M | if (matrix->yy != 1.) { |
435 | 11.9k | quad_y[0] = *y1 * matrix->yy; |
436 | 11.9k | quad_y[1] = *y2 * matrix->yy; |
437 | 11.9k | if (quad_y[0] < quad_y[1]) { |
438 | 6.62k | *y1 = quad_y[0]; |
439 | 6.62k | *y2 = quad_y[1]; |
440 | 6.62k | } else { |
441 | 5.27k | *y1 = quad_y[1]; |
442 | 5.27k | *y2 = quad_y[0]; |
443 | 5.27k | } |
444 | 11.9k | } |
445 | 2.57M | if (matrix->y0 != 0.) { |
446 | 2.52M | *y1 += matrix->y0; |
447 | 2.52M | *y2 += matrix->y0; |
448 | 2.52M | } |
449 | | |
450 | 2.57M | if (is_tight) |
451 | 0 | *is_tight = TRUE; |
452 | | |
453 | 2.57M | return; |
454 | 2.57M | } |
455 | | |
456 | | /* general matrix */ |
457 | 0 | quad_x[0] = *x1; |
458 | 0 | quad_y[0] = *y1; |
459 | 0 | cairo_matrix_transform_point (matrix, &quad_x[0], &quad_y[0]); |
460 | |
|
461 | 0 | quad_x[1] = *x2; |
462 | 0 | quad_y[1] = *y1; |
463 | 0 | cairo_matrix_transform_point (matrix, &quad_x[1], &quad_y[1]); |
464 | |
|
465 | 0 | quad_x[2] = *x1; |
466 | 0 | quad_y[2] = *y2; |
467 | 0 | cairo_matrix_transform_point (matrix, &quad_x[2], &quad_y[2]); |
468 | |
|
469 | 0 | quad_x[3] = *x2; |
470 | 0 | quad_y[3] = *y2; |
471 | 0 | cairo_matrix_transform_point (matrix, &quad_x[3], &quad_y[3]); |
472 | |
|
473 | 0 | min_x = max_x = quad_x[0]; |
474 | 0 | min_y = max_y = quad_y[0]; |
475 | |
|
476 | 0 | for (i=1; i < 4; i++) { |
477 | 0 | if (quad_x[i] < min_x) |
478 | 0 | min_x = quad_x[i]; |
479 | 0 | if (quad_x[i] > max_x) |
480 | 0 | max_x = quad_x[i]; |
481 | |
|
482 | 0 | if (quad_y[i] < min_y) |
483 | 0 | min_y = quad_y[i]; |
484 | 0 | if (quad_y[i] > max_y) |
485 | 0 | max_y = quad_y[i]; |
486 | 0 | } |
487 | |
|
488 | 0 | *x1 = min_x; |
489 | 0 | *y1 = min_y; |
490 | 0 | *x2 = max_x; |
491 | 0 | *y2 = max_y; |
492 | |
|
493 | 0 | if (is_tight) { |
494 | | /* it's tight if and only if the four corner points form an axis-aligned |
495 | | rectangle. |
496 | | And that's true if and only if we can derive corners 0 and 3 from |
497 | | corners 1 and 2 in one of two straightforward ways... |
498 | | We could use a tolerance here but for now we'll fall back to FALSE in the case |
499 | | of floating point error. |
500 | | */ |
501 | 0 | *is_tight = |
502 | 0 | (quad_x[1] == quad_x[0] && quad_y[1] == quad_y[3] && |
503 | 0 | quad_x[2] == quad_x[3] && quad_y[2] == quad_y[0]) || |
504 | 0 | (quad_x[1] == quad_x[3] && quad_y[1] == quad_y[0] && |
505 | 0 | quad_x[2] == quad_x[0] && quad_y[2] == quad_y[3]); |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | | cairo_private void |
510 | | _cairo_matrix_transform_bounding_box_fixed (const cairo_matrix_t *matrix, |
511 | | cairo_box_t *bbox, |
512 | | cairo_bool_t *is_tight) |
513 | 0 | { |
514 | 0 | double x1, y1, x2, y2; |
515 | |
|
516 | 0 | _cairo_box_to_doubles (bbox, &x1, &y1, &x2, &y2); |
517 | 0 | _cairo_matrix_transform_bounding_box (matrix, &x1, &y1, &x2, &y2, is_tight); |
518 | 0 | _cairo_box_from_doubles (bbox, &x1, &y1, &x2, &y2); |
519 | 0 | } |
520 | | |
521 | | static void |
522 | | _cairo_matrix_scalar_multiply (cairo_matrix_t *matrix, double scalar) |
523 | 417 | { |
524 | 417 | matrix->xx *= scalar; |
525 | 417 | matrix->yx *= scalar; |
526 | | |
527 | 417 | matrix->xy *= scalar; |
528 | 417 | matrix->yy *= scalar; |
529 | | |
530 | 417 | matrix->x0 *= scalar; |
531 | 417 | matrix->y0 *= scalar; |
532 | 417 | } |
533 | | |
534 | | /* This function isn't a correct adjoint in that the implicit 1 in the |
535 | | homogeneous result should actually be ad-bc instead. But, since this |
536 | | adjoint is only used in the computation of the inverse, which |
537 | | divides by det (A)=ad-bc anyway, everything works out in the end. */ |
538 | | static void |
539 | | _cairo_matrix_compute_adjoint (cairo_matrix_t *matrix) |
540 | 417 | { |
541 | | /* adj (A) = transpose (C:cofactor (A,i,j)) */ |
542 | 417 | double a, b, c, d, tx, ty; |
543 | | |
544 | 417 | _cairo_matrix_get_affine (matrix, |
545 | 417 | &a, &b, |
546 | 417 | &c, &d, |
547 | 417 | &tx, &ty); |
548 | | |
549 | 417 | cairo_matrix_init (matrix, |
550 | 417 | d, -b, |
551 | 417 | -c, a, |
552 | 417 | c*ty - d*tx, b*tx - a*ty); |
553 | 417 | } |
554 | | |
555 | | /** |
556 | | * cairo_matrix_invert: |
557 | | * @matrix: a #cairo_matrix_t |
558 | | * |
559 | | * Changes @matrix to be the inverse of its original value. Not |
560 | | * all transformation matrices have inverses; if the matrix |
561 | | * collapses points together (it is <firstterm>degenerate</firstterm>), |
562 | | * then it has no inverse and this function will fail. |
563 | | * |
564 | | * Returns: If @matrix has an inverse, modifies @matrix to |
565 | | * be the inverse matrix and returns %CAIRO_STATUS_SUCCESS. Otherwise, |
566 | | * returns %CAIRO_STATUS_INVALID_MATRIX. |
567 | | * |
568 | | * Since: 1.0 |
569 | | **/ |
570 | | cairo_status_t |
571 | | cairo_matrix_invert (cairo_matrix_t *matrix) |
572 | 3.38M | { |
573 | 3.38M | double det; |
574 | | |
575 | | /* Simple scaling|translation matrices are quite common... */ |
576 | 3.38M | if (matrix->xy == 0. && matrix->yx == 0.) { |
577 | 3.38M | matrix->x0 = -matrix->x0; |
578 | 3.38M | matrix->y0 = -matrix->y0; |
579 | | |
580 | 3.38M | if (matrix->xx != 1.) { |
581 | 6.70k | if (matrix->xx == 0.) |
582 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_MATRIX); |
583 | | |
584 | 6.70k | matrix->xx = 1. / matrix->xx; |
585 | 6.70k | matrix->x0 *= matrix->xx; |
586 | 6.70k | } |
587 | | |
588 | 3.38M | if (matrix->yy != 1.) { |
589 | 8.51k | if (matrix->yy == 0.) |
590 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_MATRIX); |
591 | | |
592 | 8.51k | matrix->yy = 1. / matrix->yy; |
593 | 8.51k | matrix->y0 *= matrix->yy; |
594 | 8.51k | } |
595 | | |
596 | 3.38M | return CAIRO_STATUS_SUCCESS; |
597 | 3.38M | } |
598 | | |
599 | | /* inv (A) = 1/det (A) * adj (A) */ |
600 | 417 | det = _cairo_matrix_compute_determinant (matrix); |
601 | | |
602 | 417 | if (! ISFINITE (det)) |
603 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_MATRIX); |
604 | | |
605 | 417 | if (det == 0) |
606 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_MATRIX); |
607 | | |
608 | 417 | _cairo_matrix_compute_adjoint (matrix); |
609 | 417 | _cairo_matrix_scalar_multiply (matrix, 1 / det); |
610 | | |
611 | 417 | return CAIRO_STATUS_SUCCESS; |
612 | 417 | } |
613 | | |
614 | | cairo_bool_t |
615 | | _cairo_matrix_is_invertible (const cairo_matrix_t *matrix) |
616 | 1.08M | { |
617 | 1.08M | double det; |
618 | | |
619 | 1.08M | det = _cairo_matrix_compute_determinant (matrix); |
620 | | |
621 | 1.08M | return ISFINITE (det) && det != 0.; |
622 | 1.08M | } |
623 | | |
624 | | cairo_bool_t |
625 | | _cairo_matrix_is_scale_0 (const cairo_matrix_t *matrix) |
626 | 0 | { |
627 | 0 | return matrix->xx == 0. && |
628 | 0 | matrix->xy == 0. && |
629 | 0 | matrix->yx == 0. && |
630 | 0 | matrix->yy == 0.; |
631 | 0 | } |
632 | | |
633 | | double |
634 | | _cairo_matrix_compute_determinant (const cairo_matrix_t *matrix) |
635 | 8.56M | { |
636 | 8.56M | double a, b, c, d; |
637 | | |
638 | 8.56M | a = matrix->xx; b = matrix->yx; |
639 | 8.56M | c = matrix->xy; d = matrix->yy; |
640 | | |
641 | 8.56M | return a*d - b*c; |
642 | 8.56M | } |
643 | | |
644 | | /** |
645 | | * _cairo_matrix_compute_basis_scale_factors: |
646 | | * @matrix: a matrix |
647 | | * @basis_scale: the scale factor in the direction of basis |
648 | | * @normal_scale: the scale factor in the direction normal to the basis |
649 | | * @x_basis: basis to use. X basis if true, Y basis otherwise. |
650 | | * |
651 | | * Computes |Mv| and det(M)/|Mv| for v=[1,0] if x_basis is true, and v=[0,1] |
652 | | * otherwise, and M is @matrix. |
653 | | * |
654 | | * Return value: the scale factor of @matrix on the height of the font, |
655 | | * or 1.0 if @matrix is %NULL. |
656 | | **/ |
657 | | cairo_status_t |
658 | | _cairo_matrix_compute_basis_scale_factors (const cairo_matrix_t *matrix, |
659 | | double *basis_scale, double *normal_scale, |
660 | | cairo_bool_t x_basis) |
661 | 113k | { |
662 | 113k | double det; |
663 | | |
664 | 113k | det = _cairo_matrix_compute_determinant (matrix); |
665 | | |
666 | 113k | if (! ISFINITE (det)) |
667 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_MATRIX); |
668 | | |
669 | 113k | if (det == 0) |
670 | 0 | { |
671 | 0 | *basis_scale = *normal_scale = 0; |
672 | 0 | } |
673 | 113k | else |
674 | 113k | { |
675 | 113k | double x = x_basis != 0; |
676 | 113k | double y = x == 0; |
677 | 113k | double major, minor; |
678 | | |
679 | 113k | cairo_matrix_transform_distance (matrix, &x, &y); |
680 | 113k | major = hypot (x, y); |
681 | | /* |
682 | | * ignore mirroring |
683 | | */ |
684 | 113k | if (det < 0) |
685 | 0 | det = -det; |
686 | 113k | if (major) |
687 | 113k | minor = det / major; |
688 | 0 | else |
689 | 0 | minor = 0.0; |
690 | 113k | if (x_basis) |
691 | 113k | { |
692 | 113k | *basis_scale = major; |
693 | 113k | *normal_scale = minor; |
694 | 113k | } |
695 | 0 | else |
696 | 0 | { |
697 | 0 | *basis_scale = minor; |
698 | 0 | *normal_scale = major; |
699 | 0 | } |
700 | 113k | } |
701 | | |
702 | 113k | return CAIRO_STATUS_SUCCESS; |
703 | 113k | } |
704 | | |
705 | | cairo_bool_t |
706 | | _cairo_matrix_is_integer_translation (const cairo_matrix_t *matrix, |
707 | | int *itx, int *ity) |
708 | 512k | { |
709 | 512k | if (_cairo_matrix_is_translation (matrix)) |
710 | 512k | { |
711 | 512k | cairo_fixed_t x0_fixed = _cairo_fixed_from_double (matrix->x0); |
712 | 512k | cairo_fixed_t y0_fixed = _cairo_fixed_from_double (matrix->y0); |
713 | | |
714 | 512k | if (_cairo_fixed_is_integer (x0_fixed) && |
715 | 512k | _cairo_fixed_is_integer (y0_fixed)) |
716 | 512k | { |
717 | 512k | if (itx) |
718 | 512k | *itx = _cairo_fixed_integer_part (x0_fixed); |
719 | 512k | if (ity) |
720 | 512k | *ity = _cairo_fixed_integer_part (y0_fixed); |
721 | | |
722 | 512k | return TRUE; |
723 | 512k | } |
724 | 512k | } |
725 | | |
726 | 67 | return FALSE; |
727 | 512k | } |
728 | | |
729 | 10.9M | #define SCALING_EPSILON _cairo_fixed_to_double(1) |
730 | | |
731 | | /* This only returns true if the matrix is 90 degree rotations or |
732 | | * flips. It appears calling code is relying on this. It will return |
733 | | * false for other rotations even if the scale is one. Approximations |
734 | | * are allowed to handle matricies filled in using trig functions |
735 | | * such as sin(M_PI_2). |
736 | | */ |
737 | | cairo_bool_t |
738 | | _cairo_matrix_has_unity_scale (const cairo_matrix_t *matrix) |
739 | 2.75M | { |
740 | | /* check that the determinant is near +/-1 */ |
741 | 2.75M | double det = _cairo_matrix_compute_determinant (matrix); |
742 | 2.75M | if (fabs (det * det - 1.0) < SCALING_EPSILON) { |
743 | | /* check that one axis is close to zero */ |
744 | 2.74M | if (fabs (matrix->xy) < SCALING_EPSILON && |
745 | 2.74M | fabs (matrix->yx) < SCALING_EPSILON) |
746 | 2.74M | return TRUE; |
747 | 0 | if (fabs (matrix->xx) < SCALING_EPSILON && |
748 | 0 | fabs (matrix->yy) < SCALING_EPSILON) |
749 | 0 | return TRUE; |
750 | | /* If rotations are allowed then it must instead test for |
751 | | * orthogonality. This is xx*xy+yx*yy ~= 0. |
752 | | */ |
753 | 0 | } |
754 | 10.0k | return FALSE; |
755 | 2.75M | } |
756 | | |
757 | | /* By pixel exact here, we mean a matrix that is composed only of |
758 | | * 90 degree rotations, flips, and integer translations and produces a 1:1 |
759 | | * mapping between source and destination pixels. If we transform an image |
760 | | * with a pixel-exact matrix, filtering is not useful. |
761 | | */ |
762 | | cairo_bool_t |
763 | | _cairo_matrix_is_pixel_exact (const cairo_matrix_t *matrix) |
764 | 515k | { |
765 | 515k | cairo_fixed_t x0_fixed, y0_fixed; |
766 | | |
767 | 515k | if (! _cairo_matrix_has_unity_scale (matrix)) |
768 | 241 | return FALSE; |
769 | | |
770 | 515k | x0_fixed = _cairo_fixed_from_double (matrix->x0); |
771 | 515k | y0_fixed = _cairo_fixed_from_double (matrix->y0); |
772 | | |
773 | 515k | return _cairo_fixed_is_integer (x0_fixed) && _cairo_fixed_is_integer (y0_fixed); |
774 | 515k | } |
775 | | |
776 | | /* |
777 | | A circle in user space is transformed into an ellipse in device space. |
778 | | |
779 | | The following is a derivation of a formula to calculate the length of the |
780 | | major axis for this ellipse; this is useful for error bounds calculations. |
781 | | |
782 | | Thanks to Walter Brisken <wbrisken@aoc.nrao.edu> for this derivation: |
783 | | |
784 | | 1. First some notation: |
785 | | |
786 | | All capital letters represent vectors in two dimensions. A prime ' |
787 | | represents a transformed coordinate. Matrices are written in underlined |
788 | | form, ie _R_. Lowercase letters represent scalar real values. |
789 | | |
790 | | 2. The question has been posed: What is the maximum expansion factor |
791 | | achieved by the linear transformation |
792 | | |
793 | | X' = X _R_ |
794 | | |
795 | | where _R_ is a real-valued 2x2 matrix with entries: |
796 | | |
797 | | _R_ = [a b] |
798 | | [c d] . |
799 | | |
800 | | In other words, what is the maximum radius, MAX[ |X'| ], reached for any |
801 | | X on the unit circle ( |X| = 1 ) ? |
802 | | |
803 | | 3. Some useful formulae |
804 | | |
805 | | (A) through (C) below are standard double-angle formulae. (D) is a lesser |
806 | | known result and is derived below: |
807 | | |
808 | | (A) sin²(θ) = (1 - cos(2*θ))/2 |
809 | | (B) cos²(θ) = (1 + cos(2*θ))/2 |
810 | | (C) sin(θ)*cos(θ) = sin(2*θ)/2 |
811 | | (D) MAX[a*cos(θ) + b*sin(θ)] = sqrt(a² + b²) |
812 | | |
813 | | Proof of (D): |
814 | | |
815 | | find the maximum of the function by setting the derivative to zero: |
816 | | |
817 | | -a*sin(θ)+b*cos(θ) = 0 |
818 | | |
819 | | From this it follows that |
820 | | |
821 | | tan(θ) = b/a |
822 | | |
823 | | and hence |
824 | | |
825 | | sin(θ) = b/sqrt(a² + b²) |
826 | | |
827 | | and |
828 | | |
829 | | cos(θ) = a/sqrt(a² + b²) |
830 | | |
831 | | Thus the maximum value is |
832 | | |
833 | | MAX[a*cos(θ) + b*sin(θ)] = (a² + b²)/sqrt(a² + b²) |
834 | | = sqrt(a² + b²) |
835 | | |
836 | | 4. Derivation of maximum expansion |
837 | | |
838 | | To find MAX[ |X'| ] we search brute force method using calculus. The unit |
839 | | circle on which X is constrained is to be parameterized by t: |
840 | | |
841 | | X(θ) = (cos(θ), sin(θ)) |
842 | | |
843 | | Thus |
844 | | |
845 | | X'(θ) = X(θ) * _R_ = (cos(θ), sin(θ)) * [a b] |
846 | | [c d] |
847 | | = (a*cos(θ) + c*sin(θ), b*cos(θ) + d*sin(θ)). |
848 | | |
849 | | Define |
850 | | |
851 | | r(θ) = |X'(θ)| |
852 | | |
853 | | Thus |
854 | | |
855 | | r²(θ) = (a*cos(θ) + c*sin(θ))² + (b*cos(θ) + d*sin(θ))² |
856 | | = (a² + b²)*cos²(θ) + (c² + d²)*sin²(θ) |
857 | | + 2*(a*c + b*d)*cos(θ)*sin(θ) |
858 | | |
859 | | Now apply the double angle formulae (A) to (C) from above: |
860 | | |
861 | | r²(θ) = (a² + b² + c² + d²)/2 |
862 | | + (a² + b² - c² - d²)*cos(2*θ)/2 |
863 | | + (a*c + b*d)*sin(2*θ) |
864 | | = f + g*cos(φ) + h*sin(φ) |
865 | | |
866 | | Where |
867 | | |
868 | | f = (a² + b² + c² + d²)/2 |
869 | | g = (a² + b² - c² - d²)/2 |
870 | | h = (a*c + d*d) |
871 | | φ = 2*θ |
872 | | |
873 | | It is clear that MAX[ |X'| ] = sqrt(MAX[ r² ]). Here we determine MAX[ r² ] |
874 | | using (D) from above: |
875 | | |
876 | | MAX[ r² ] = f + sqrt(g² + h²) |
877 | | |
878 | | And finally |
879 | | |
880 | | MAX[ |X'| ] = sqrt( f + sqrt(g² + h²) ) |
881 | | |
882 | | Which is the solution to this problem. |
883 | | |
884 | | Walter Brisken |
885 | | 2004/10/08 |
886 | | |
887 | | (Note that the minor axis length is at the minimum of the above solution, |
888 | | which is just sqrt ( f - sqrt(g² + h²) ) given the symmetry of (D)). |
889 | | |
890 | | |
891 | | For another derivation of the same result, using Singular Value Decomposition, |
892 | | see doc/tutorial/src/singular.c. |
893 | | */ |
894 | | |
895 | | /* determine the length of the major axis of a circle of the given radius |
896 | | after applying the transformation matrix. */ |
897 | | double |
898 | | _cairo_matrix_transformed_circle_major_axis (const cairo_matrix_t *matrix, |
899 | | double radius) |
900 | 1.11M | { |
901 | 1.11M | double a, b, c, d, f, g, h, i, j; |
902 | | |
903 | 1.11M | if (_cairo_matrix_has_unity_scale (matrix)) |
904 | 1.11M | return radius; |
905 | | |
906 | 4.07k | _cairo_matrix_get_affine (matrix, |
907 | 4.07k | &a, &b, |
908 | 4.07k | &c, &d, |
909 | 4.07k | NULL, NULL); |
910 | | |
911 | 4.07k | i = a*a + b*b; |
912 | 4.07k | j = c*c + d*d; |
913 | | |
914 | 4.07k | f = 0.5 * (i + j); |
915 | 4.07k | g = 0.5 * (i - j); |
916 | 4.07k | h = a*c + b*d; |
917 | | |
918 | 4.07k | return radius * sqrt (f + hypot (g, h)); |
919 | | |
920 | | /* |
921 | | * we don't need the minor axis length, which is |
922 | | * double min = radius * sqrt (f - sqrt (g*g+h*h)); |
923 | | */ |
924 | 1.11M | } |
925 | | |
926 | | static const pixman_transform_t pixman_identity_transform = {{ |
927 | | {1 << 16, 0, 0}, |
928 | | { 0, 1 << 16, 0}, |
929 | | { 0, 0, 1 << 16} |
930 | | }}; |
931 | | |
932 | | static cairo_status_t |
933 | | _cairo_matrix_to_pixman_matrix (const cairo_matrix_t *matrix, |
934 | | pixman_transform_t *pixman_transform, |
935 | | double xc, |
936 | | double yc) |
937 | 2.05k | { |
938 | 2.05k | cairo_matrix_t inv; |
939 | 2.05k | unsigned max_iterations; |
940 | | |
941 | 2.05k | pixman_transform->matrix[0][0] = _cairo_fixed_16_16_from_double (matrix->xx); |
942 | 2.05k | pixman_transform->matrix[0][1] = _cairo_fixed_16_16_from_double (matrix->xy); |
943 | 2.05k | pixman_transform->matrix[0][2] = _cairo_fixed_16_16_from_double (matrix->x0); |
944 | | |
945 | 2.05k | pixman_transform->matrix[1][0] = _cairo_fixed_16_16_from_double (matrix->yx); |
946 | 2.05k | pixman_transform->matrix[1][1] = _cairo_fixed_16_16_from_double (matrix->yy); |
947 | 2.05k | pixman_transform->matrix[1][2] = _cairo_fixed_16_16_from_double (matrix->y0); |
948 | | |
949 | 2.05k | pixman_transform->matrix[2][0] = 0; |
950 | 2.05k | pixman_transform->matrix[2][1] = 0; |
951 | 2.05k | pixman_transform->matrix[2][2] = 1 << 16; |
952 | | |
953 | | /* The conversion above breaks cairo's translation invariance: |
954 | | * a translation of (a, b) in device space translates to |
955 | | * a translation of (xx * a + xy * b, yx * a + yy * b) |
956 | | * for cairo, while pixman uses rounded versions of xx ... yy. |
957 | | * This error increases as a and b get larger. |
958 | | * |
959 | | * To compensate for this, we fix the point (xc, yc) in pattern |
960 | | * space and adjust pixman's transform to agree with cairo's at |
961 | | * that point. |
962 | | */ |
963 | | |
964 | 2.05k | if (_cairo_matrix_has_unity_scale (matrix)) |
965 | 7 | return CAIRO_STATUS_SUCCESS; |
966 | | |
967 | 2.04k | if (unlikely (fabs (matrix->xx) > PIXMAN_MAX_INT || |
968 | 2.04k | fabs (matrix->xy) > PIXMAN_MAX_INT || |
969 | 2.04k | fabs (matrix->x0) > PIXMAN_MAX_INT || |
970 | 2.04k | fabs (matrix->yx) > PIXMAN_MAX_INT || |
971 | 2.04k | fabs (matrix->yy) > PIXMAN_MAX_INT || |
972 | 2.04k | fabs (matrix->y0) > PIXMAN_MAX_INT)) |
973 | 42 | { |
974 | 42 | return _cairo_error (CAIRO_STATUS_INVALID_MATRIX); |
975 | 42 | } |
976 | | |
977 | | /* Note: If we can't invert the transformation, skip the adjustment. */ |
978 | 2.00k | inv = *matrix; |
979 | 2.00k | if (cairo_matrix_invert (&inv) != CAIRO_STATUS_SUCCESS) |
980 | 0 | return CAIRO_STATUS_SUCCESS; |
981 | | |
982 | | /* find the pattern space coordinate that maps to (xc, yc) */ |
983 | 2.00k | max_iterations = 5; |
984 | 2.72k | do { |
985 | 2.72k | double x,y; |
986 | 2.72k | pixman_vector_t vector; |
987 | 2.72k | cairo_fixed_16_16_t dx, dy; |
988 | | |
989 | 2.72k | vector.vector[0] = _cairo_fixed_16_16_from_double (xc); |
990 | 2.72k | vector.vector[1] = _cairo_fixed_16_16_from_double (yc); |
991 | 2.72k | vector.vector[2] = 1 << 16; |
992 | | |
993 | | /* If we can't transform the reference point, skip the adjustment. */ |
994 | 2.72k | if (! pixman_transform_point_3d (pixman_transform, &vector)) |
995 | 0 | return CAIRO_STATUS_SUCCESS; |
996 | | |
997 | 2.72k | x = pixman_fixed_to_double (vector.vector[0]); |
998 | 2.72k | y = pixman_fixed_to_double (vector.vector[1]); |
999 | 2.72k | cairo_matrix_transform_point (&inv, &x, &y); |
1000 | | |
1001 | | /* Ideally, the vector should now be (xc, yc). |
1002 | | * We can now compensate for the resulting error. |
1003 | | */ |
1004 | 2.72k | x -= xc; |
1005 | 2.72k | y -= yc; |
1006 | 2.72k | cairo_matrix_transform_distance (matrix, &x, &y); |
1007 | 2.72k | dx = _cairo_fixed_16_16_from_double (x); |
1008 | 2.72k | dy = _cairo_fixed_16_16_from_double (y); |
1009 | 2.72k | pixman_transform->matrix[0][2] -= dx; |
1010 | 2.72k | pixman_transform->matrix[1][2] -= dy; |
1011 | | |
1012 | 2.72k | if (dx == 0 && dy == 0) |
1013 | 2.00k | return CAIRO_STATUS_SUCCESS; |
1014 | 2.72k | } while (--max_iterations); |
1015 | | |
1016 | | /* We didn't find an exact match between cairo and pixman, but |
1017 | | * the matrix should be mostly correct */ |
1018 | 0 | return CAIRO_STATUS_SUCCESS; |
1019 | 2.00k | } |
1020 | | |
1021 | | static inline double |
1022 | | _pixman_nearest_sample (double d) |
1023 | 964k | { |
1024 | 964k | return ceil (d - .5); |
1025 | 964k | } |
1026 | | |
1027 | | /** |
1028 | | * _cairo_matrix_is_pixman_translation: |
1029 | | * @matrix: a matrix |
1030 | | * @filter: the filter to be used on the pattern transformed by @matrix |
1031 | | * @x_offset: the translation in the X direction |
1032 | | * @y_offset: the translation in the Y direction |
1033 | | * |
1034 | | * Checks if @matrix translated by (x_offset, y_offset) can be |
1035 | | * represented using just an offset (within the range pixman can |
1036 | | * accept) and an identity matrix. |
1037 | | * |
1038 | | * Passing a non-zero value in x_offset/y_offset has the same effect |
1039 | | * as applying cairo_matrix_translate(matrix, x_offset, y_offset) and |
1040 | | * setting x_offset and y_offset to 0. |
1041 | | * |
1042 | | * Upon return x_offset and y_offset contain the translation vector if |
1043 | | * the return value is %TRUE. If the return value is %FALSE, they will |
1044 | | * not be modified. |
1045 | | * |
1046 | | * Return value: %TRUE if @matrix can be represented as a pixman |
1047 | | * translation, %FALSE otherwise. |
1048 | | **/ |
1049 | | cairo_bool_t |
1050 | | _cairo_matrix_is_pixman_translation (const cairo_matrix_t *matrix, |
1051 | | cairo_filter_t filter, |
1052 | | int *x_offset, |
1053 | | int *y_offset) |
1054 | 518k | { |
1055 | 518k | double tx, ty; |
1056 | | |
1057 | 518k | if (!_cairo_matrix_is_translation (matrix)) |
1058 | 2.28k | return FALSE; |
1059 | | |
1060 | 516k | if (matrix->x0 == 0. && matrix->y0 == 0.) |
1061 | 34.2k | return TRUE; |
1062 | | |
1063 | 482k | tx = matrix->x0 + *x_offset; |
1064 | 482k | ty = matrix->y0 + *y_offset; |
1065 | | |
1066 | 482k | if (filter == CAIRO_FILTER_FAST || filter == CAIRO_FILTER_NEAREST) { |
1067 | 482k | tx = _pixman_nearest_sample (tx); |
1068 | 482k | ty = _pixman_nearest_sample (ty); |
1069 | 482k | } else if (tx != floor (tx) || ty != floor (ty)) { |
1070 | 0 | return FALSE; |
1071 | 0 | } |
1072 | | |
1073 | 482k | if (fabs (tx) > PIXMAN_MAX_INT || fabs (ty) > PIXMAN_MAX_INT) |
1074 | 203 | return FALSE; |
1075 | | |
1076 | 481k | *x_offset = _cairo_lround (tx); |
1077 | 481k | *y_offset = _cairo_lround (ty); |
1078 | 481k | return TRUE; |
1079 | 482k | } |
1080 | | |
1081 | | /** |
1082 | | * _cairo_matrix_to_pixman_matrix_offset: |
1083 | | * @matrix: a matrix |
1084 | | * @filter: the filter to be used on the pattern transformed by @matrix |
1085 | | * @xc: the X coordinate of the point to fix in pattern space |
1086 | | * @yc: the Y coordinate of the point to fix in pattern space |
1087 | | * @out_transform: the transformation which best approximates @matrix |
1088 | | * @x_offset: the translation in the X direction |
1089 | | * @y_offset: the translation in the Y direction |
1090 | | * |
1091 | | * This function tries to represent @matrix translated by (x_offset, |
1092 | | * y_offset) as a %pixman_transform_t and an translation. |
1093 | | * |
1094 | | * Passing a non-zero value in x_offset/y_offset has the same effect |
1095 | | * as applying cairo_matrix_translate(matrix, x_offset, y_offset) and |
1096 | | * setting x_offset and y_offset to 0. |
1097 | | * |
1098 | | * If it is possible to represent the matrix with an identity |
1099 | | * %pixman_transform_t and a translation within the valid range for |
1100 | | * pixman, this function will set @out_transform to be the identity, |
1101 | | * @x_offset and @y_offset to be the translation vector and will |
1102 | | * return %CAIRO_INT_STATUS_NOTHING_TO_DO. Otherwise it will try to |
1103 | | * evenly divide the translational component of @matrix between |
1104 | | * @out_transform and (@x_offset, @y_offset). |
1105 | | * |
1106 | | * Upon return x_offset and y_offset contain the translation vector. |
1107 | | * |
1108 | | * Return value: %CAIRO_INT_STATUS_NOTHING_TO_DO if the out_transform |
1109 | | * is the identity, %CAIRO_STATUS_INVALID_MATRIX if it was not |
1110 | | * possible to represent @matrix as a pixman_transform_t without |
1111 | | * overflows, %CAIRO_STATUS_SUCCESS otherwise. |
1112 | | **/ |
1113 | | cairo_status_t |
1114 | | _cairo_matrix_to_pixman_matrix_offset (const cairo_matrix_t *matrix, |
1115 | | cairo_filter_t filter, |
1116 | | double xc, |
1117 | | double yc, |
1118 | | pixman_transform_t *out_transform, |
1119 | | int *x_offset, |
1120 | | int *y_offset) |
1121 | 2.72k | { |
1122 | 2.72k | cairo_bool_t is_pixman_translation; |
1123 | | |
1124 | 2.72k | is_pixman_translation = _cairo_matrix_is_pixman_translation (matrix, |
1125 | 2.72k | filter, |
1126 | 2.72k | x_offset, |
1127 | 2.72k | y_offset); |
1128 | | |
1129 | 2.72k | if (is_pixman_translation) { |
1130 | 668 | *out_transform = pixman_identity_transform; |
1131 | 668 | return CAIRO_INT_STATUS_NOTHING_TO_DO; |
1132 | 2.05k | } else { |
1133 | 2.05k | cairo_matrix_t m; |
1134 | | |
1135 | 2.05k | m = *matrix; |
1136 | 2.05k | cairo_matrix_translate (&m, *x_offset, *y_offset); |
1137 | 2.05k | if (m.x0 != 0.0 || m.y0 != 0.0) { |
1138 | 133 | double tx, ty, norm; |
1139 | 133 | int i, j; |
1140 | | |
1141 | | /* pixman also limits the [xy]_offset to 16 bits so evenly |
1142 | | * spread the bits between the two. |
1143 | | * |
1144 | | * To do this, find the solutions of: |
1145 | | * |x| = |x*m.xx + y*m.xy + m.x0| |
1146 | | * |y| = |x*m.yx + y*m.yy + m.y0| |
1147 | | * |
1148 | | * and select the one whose maximum norm is smallest. |
1149 | | */ |
1150 | 133 | tx = m.x0; |
1151 | 133 | ty = m.y0; |
1152 | 133 | norm = MAX (fabs (tx), fabs (ty)); |
1153 | | |
1154 | 399 | for (i = -1; i < 2; i+=2) { |
1155 | 798 | for (j = -1; j < 2; j+=2) { |
1156 | 532 | double x, y, den, new_norm; |
1157 | | |
1158 | 532 | den = (m.xx + i) * (m.yy + j) - m.xy * m.yx; |
1159 | 532 | if (fabs (den) < DBL_EPSILON) |
1160 | 21 | continue; |
1161 | | |
1162 | 511 | x = m.y0 * m.xy - m.x0 * (m.yy + j); |
1163 | 511 | y = m.x0 * m.yx - m.y0 * (m.xx + i); |
1164 | | |
1165 | 511 | den = 1 / den; |
1166 | 511 | x *= den; |
1167 | 511 | y *= den; |
1168 | | |
1169 | 511 | new_norm = MAX (fabs (x), fabs (y)); |
1170 | 511 | if (norm > new_norm) { |
1171 | 166 | norm = new_norm; |
1172 | 166 | tx = x; |
1173 | 166 | ty = y; |
1174 | 166 | } |
1175 | 511 | } |
1176 | 266 | } |
1177 | | |
1178 | 133 | tx = floor (tx); |
1179 | 133 | ty = floor (ty); |
1180 | 133 | *x_offset = -tx; |
1181 | 133 | *y_offset = -ty; |
1182 | 133 | cairo_matrix_translate (&m, tx, ty); |
1183 | 1.91k | } else { |
1184 | 1.91k | *x_offset = 0; |
1185 | 1.91k | *y_offset = 0; |
1186 | 1.91k | } |
1187 | | |
1188 | 2.05k | return _cairo_matrix_to_pixman_matrix (&m, out_transform, xc, yc); |
1189 | 2.05k | } |
1190 | 2.72k | } |