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
#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
1719951
cairo_matrix_init_identity (cairo_matrix_t *matrix)
82
{
83
1719951
    cairo_matrix_init (matrix,
84
		       1, 0,
85
		       0, 1,
86
		       0, 0);
87
1719951
}
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
2119655
cairo_matrix_init (cairo_matrix_t *matrix,
111
		   double xx, double yx,
112

            
113
		   double xy, double yy,
114
		   double x0, double y0)
115
{
116
2119655
    matrix->xx = xx; matrix->yx = yx;
117
2119655
    matrix->xy = xy; matrix->yy = yy;
118
2119655
    matrix->x0 = x0; matrix->y0 = y0;
119
2119655
}
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
109249
_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
{
147
109249
    *xx  = matrix->xx;
148
109249
    *yx  = matrix->yx;
149

            
150
109249
    *xy  = matrix->xy;
151
109249
    *yy  = matrix->yy;
152

            
153
109249
    if (x0)
154
99819
	*x0 = matrix->x0;
155
109249
    if (y0)
156
99819
	*y0 = matrix->y0;
157
109249
}
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
220314
cairo_matrix_init_translate (cairo_matrix_t *matrix,
172
			     double tx, double ty)
173
{
174
220314
    cairo_matrix_init (matrix,
175
		       1, 0,
176
		       0, 1,
177
		       tx, ty);
178
220314
}
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
94160
cairo_matrix_translate (cairo_matrix_t *matrix, double tx, double ty)
195
{
196
    cairo_matrix_t tmp;
197

            
198
94160
    cairo_matrix_init_translate (&tmp, tx, ty);
199

            
200
94160
    cairo_matrix_multiply (matrix, &tmp, matrix);
201
94160
}
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
73306
cairo_matrix_init_scale (cairo_matrix_t *matrix,
216
			 double sx, double sy)
217
{
218
73306
    cairo_matrix_init (matrix,
219
		       sx,  0,
220
		       0, sy,
221
		       0, 0);
222
73306
}
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
16715
cairo_matrix_scale (cairo_matrix_t *matrix, double sx, double sy)
238
{
239
    cairo_matrix_t tmp;
240

            
241
16715
    cairo_matrix_init_scale (&tmp, sx, sy);
242

            
243
16715
    cairo_matrix_multiply (matrix, &tmp, matrix);
244
16715
}
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
1835
cairo_matrix_init_rotate (cairo_matrix_t *matrix,
261
			  double radians)
262
{
263
    double  s;
264
    double  c;
265

            
266
1835
    s = sin (radians);
267
1835
    c = cos (radians);
268

            
269
1835
    cairo_matrix_init (matrix,
270
		       c, s,
271
		       -s, c,
272
		       0, 0);
273
1835
}
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
57
cairo_matrix_rotate (cairo_matrix_t *matrix, double radians)
293
{
294
    cairo_matrix_t tmp;
295

            
296
57
    cairo_matrix_init_rotate (&tmp, radians);
297

            
298
57
    cairo_matrix_multiply (matrix, &tmp, matrix);
299
57
}
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
1446015
cairo_matrix_multiply (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b)
325
{
326
    cairo_matrix_t r;
327

            
328
1446015
    r.xx = a->xx * b->xx + a->yx * b->xy;
329
1446015
    r.yx = a->xx * b->yx + a->yx * b->yy;
330

            
331
1446015
    r.xy = a->xy * b->xx + a->yy * b->xy;
332
1446015
    r.yy = a->xy * b->yx + a->yy * b->yy;
333

            
334
1446015
    r.x0 = a->x0 * b->xx + a->y0 * b->xy + b->x0;
335
1446015
    r.y0 = a->x0 * b->yx + a->y0 * b->yy + b->y0;
336

            
337
1446015
    *result = r;
338
1446015
}
339

            
340
void
341
29799
_cairo_matrix_multiply (cairo_matrix_t *r,
342
			const cairo_matrix_t *a,
343
			const cairo_matrix_t *b)
344
{
345
29799
    r->xx = a->xx * b->xx + a->yx * b->xy;
346
29799
    r->yx = a->xx * b->yx + a->yx * b->yy;
347

            
348
29799
    r->xy = a->xy * b->xx + a->yy * b->xy;
349
29799
    r->yy = a->xy * b->yx + a->yy * b->yy;
350

            
351
29799
    r->x0 = a->x0 * b->xx + a->y0 * b->xy + b->x0;
352
29799
    r->y0 = a->x0 * b->yx + a->y0 * b->yy + b->y0;
353
29799
}
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
4043026
cairo_matrix_transform_distance (const cairo_matrix_t *matrix, double *dx, double *dy)
375
{
376
    double new_x, new_y;
377

            
378
4043026
    new_x = (matrix->xx * *dx + matrix->xy * *dy);
379
4043026
    new_y = (matrix->yx * *dx + matrix->yy * *dy);
380

            
381
4043026
    *dx = new_x;
382
4043026
    *dy = new_y;
383
4043026
}
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
2044981
cairo_matrix_transform_point (const cairo_matrix_t *matrix, double *x, double *y)
397
{
398
2044981
    cairo_matrix_transform_distance (matrix, x, y);
399

            
400
2044981
    *x += matrix->x0;
401
2044981
    *y += matrix->y0;
402
2044981
}
403

            
404
void
405
347504
_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
{
410
    int i;
411
    double quad_x[4], quad_y[4];
412
    double min_x, max_x;
413
    double min_y, max_y;
414

            
415
347504
    if (matrix->xy == 0. && matrix->yx == 0.) {
416
	/* non-rotation/skew matrix, just map the two extreme points */
417

            
418
279053
	if (matrix->xx != 1.) {
419
240488
	    quad_x[0] = *x1 * matrix->xx;
420
240488
	    quad_x[1] = *x2 * matrix->xx;
421
240488
	    if (quad_x[0] < quad_x[1]) {
422
183803
		*x1 = quad_x[0];
423
183803
		*x2 = quad_x[1];
424
	    } else {
425
56685
		*x1 = quad_x[1];
426
56685
		*x2 = quad_x[0];
427
	    }
428
	}
429
279053
	if (matrix->x0 != 0.) {
430
114349
	    *x1 += matrix->x0;
431
114349
	    *x2 += matrix->x0;
432
	}
433

            
434
279053
	if (matrix->yy != 1.) {
435
240476
	    quad_y[0] = *y1 * matrix->yy;
436
240476
	    quad_y[1] = *y2 * matrix->yy;
437
240476
	    if (quad_y[0] < quad_y[1]) {
438
183782
		*y1 = quad_y[0];
439
183782
		*y2 = quad_y[1];
440
	    } else {
441
56694
		*y1 = quad_y[1];
442
56694
		*y2 = quad_y[0];
443
	    }
444
	}
445
279053
	if (matrix->y0 != 0.) {
446
114865
	    *y1 += matrix->y0;
447
114865
	    *y2 += matrix->y0;
448
	}
449

            
450
279053
	if (is_tight)
451
1
	    *is_tight = TRUE;
452

            
453
279053
	return;
454
    }
455

            
456
    /* general matrix */
457
68451
    quad_x[0] = *x1;
458
68451
    quad_y[0] = *y1;
459
68451
    cairo_matrix_transform_point (matrix, &quad_x[0], &quad_y[0]);
460

            
461
68451
    quad_x[1] = *x2;
462
68451
    quad_y[1] = *y1;
463
68451
    cairo_matrix_transform_point (matrix, &quad_x[1], &quad_y[1]);
464

            
465
68451
    quad_x[2] = *x1;
466
68451
    quad_y[2] = *y2;
467
68451
    cairo_matrix_transform_point (matrix, &quad_x[2], &quad_y[2]);
468

            
469
68451
    quad_x[3] = *x2;
470
68451
    quad_y[3] = *y2;
471
68451
    cairo_matrix_transform_point (matrix, &quad_x[3], &quad_y[3]);
472

            
473
68451
    min_x = max_x = quad_x[0];
474
68451
    min_y = max_y = quad_y[0];
475

            
476
273804
    for (i=1; i < 4; i++) {
477
205353
	if (quad_x[i] < min_x)
478
14010
	    min_x = quad_x[i];
479
205353
	if (quad_x[i] > max_x)
480
61656
	    max_x = quad_x[i];
481

            
482
205353
	if (quad_y[i] < min_y)
483
61047
	    min_y = quad_y[i];
484
205353
	if (quad_y[i] > max_y)
485
8469
	    max_y = quad_y[i];
486
    }
487

            
488
68451
    *x1 = min_x;
489
68451
    *y1 = min_y;
490
68451
    *x2 = max_x;
491
68451
    *y2 = max_y;
492

            
493
68451
    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
2793
        *is_tight =
502
            (quad_x[1] == quad_x[0] && quad_y[1] == quad_y[3] &&
503
5586
             quad_x[2] == quad_x[3] && quad_y[2] == quad_y[0]) ||
504
2793
            (quad_x[1] == quad_x[3] && quad_y[1] == quad_y[0] &&
505
             quad_x[2] == quad_x[0] && quad_y[2] == quad_y[3]);
506
    }
507
}
508

            
509
cairo_private void
510
4202
_cairo_matrix_transform_bounding_box_fixed (const cairo_matrix_t *matrix,
511
					    cairo_box_t          *bbox,
512
					    cairo_bool_t *is_tight)
513
{
514
    double x1, y1, x2, y2;
515

            
516
4202
    _cairo_box_to_doubles (bbox, &x1, &y1, &x2, &y2);
517
4202
    _cairo_matrix_transform_bounding_box (matrix, &x1, &y1, &x2, &y2, is_tight);
518
4202
    _cairo_box_from_doubles (bbox, &x1, &y1, &x2, &y2);
519
4202
}
520

            
521
static void
522
99819
_cairo_matrix_scalar_multiply (cairo_matrix_t *matrix, double scalar)
523
{
524
99819
    matrix->xx *= scalar;
525
99819
    matrix->yx *= scalar;
526

            
527
99819
    matrix->xy *= scalar;
528
99819
    matrix->yy *= scalar;
529

            
530
99819
    matrix->x0 *= scalar;
531
99819
    matrix->y0 *= scalar;
532
99819
}
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
99819
_cairo_matrix_compute_adjoint (cairo_matrix_t *matrix)
540
{
541
    /* adj (A) = transpose (C:cofactor (A,i,j)) */
542
    double a, b, c, d, tx, ty;
543

            
544
99819
    _cairo_matrix_get_affine (matrix,
545
			      &a,  &b,
546
			      &c,  &d,
547
			      &tx, &ty);
548

            
549
99819
    cairo_matrix_init (matrix,
550
		       d, -b,
551
		       -c, a,
552
99819
		       c*ty - d*tx, b*tx - a*ty);
553
99819
}
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
586168
cairo_matrix_invert (cairo_matrix_t *matrix)
572
{
573
    double det;
574

            
575
    /* Simple scaling|translation matrices are quite common... */
576
586168
    if (matrix->xy == 0. && matrix->yx == 0.) {
577
486322
	matrix->x0 = -matrix->x0;
578
486322
	matrix->y0 = -matrix->y0;
579

            
580
486322
	if (matrix->xx != 1.) {
581
410902
	    if (matrix->xx == 0.)
582
9
		return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
583

            
584
410893
	    matrix->xx = 1. / matrix->xx;
585
410893
	    matrix->x0 *= matrix->xx;
586
	}
587

            
588
486313
	if (matrix->yy != 1.) {
589
410881
	    if (matrix->yy == 0.)
590
		return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
591

            
592
410881
	    matrix->yy = 1. / matrix->yy;
593
410881
	    matrix->y0 *= matrix->yy;
594
	}
595

            
596
486313
	return CAIRO_STATUS_SUCCESS;
597
    }
598

            
599
    /* inv (A) = 1/det (A) * adj (A) */
600
99846
    det = _cairo_matrix_compute_determinant (matrix);
601

            
602
99846
    if (! ISFINITE (det))
603
12
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
604

            
605
99834
    if (det == 0)
606
15
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
607

            
608
99819
    _cairo_matrix_compute_adjoint (matrix);
609
99819
    _cairo_matrix_scalar_multiply (matrix, 1 / det);
610

            
611
99819
    return CAIRO_STATUS_SUCCESS;
612
}
613

            
614
cairo_bool_t
615
65259
_cairo_matrix_is_invertible (const cairo_matrix_t *matrix)
616
{
617
    double det;
618

            
619
65259
    det = _cairo_matrix_compute_determinant (matrix);
620

            
621
65259
    return ISFINITE (det) && det != 0.;
622
}
623

            
624
cairo_bool_t
625
2370
_cairo_matrix_is_scale_0 (const cairo_matrix_t *matrix)
626
{
627
2379
    return matrix->xx == 0. &&
628
9
           matrix->xy == 0. &&
629
2388
           matrix->yx == 0. &&
630
9
           matrix->yy == 0.;
631
}
632

            
633
double
634
1422299
_cairo_matrix_compute_determinant (const cairo_matrix_t *matrix)
635
{
636
    double a, b, c, d;
637

            
638
1422299
    a = matrix->xx; b = matrix->yx;
639
1422299
    c = matrix->xy; d = matrix->yy;
640

            
641
1422299
    return a*d - b*c;
642
}
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
5985
_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
{
662
    double det;
663

            
664
5985
    det = _cairo_matrix_compute_determinant (matrix);
665

            
666
5985
    if (! ISFINITE (det))
667
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
668

            
669
5985
    if (det == 0)
670
    {
671
123
	*basis_scale = *normal_scale = 0;
672
    }
673
    else
674
    {
675
5862
	double x = x_basis != 0;
676
5862
	double y = x == 0;
677
	double major, minor;
678

            
679
5862
	cairo_matrix_transform_distance (matrix, &x, &y);
680
5862
	major = hypot (x, y);
681
	/*
682
	 * ignore mirroring
683
	 */
684
5862
	if (det < 0)
685
	    det = -det;
686
5862
	if (major)
687
5862
	    minor = det / major;
688
	else
689
	    minor = 0.0;
690
5862
	if (x_basis)
691
	{
692
5862
	    *basis_scale = major;
693
5862
	    *normal_scale = minor;
694
	}
695
	else
696
	{
697
	    *basis_scale = minor;
698
	    *normal_scale = major;
699
	}
700
    }
701

            
702
5985
    return CAIRO_STATUS_SUCCESS;
703
}
704

            
705
cairo_bool_t
706
33853
_cairo_matrix_is_integer_translation (const cairo_matrix_t *matrix,
707
				      int *itx, int *ity)
708
{
709
33853
    if (_cairo_matrix_is_translation (matrix))
710
    {
711
17524
        cairo_fixed_t x0_fixed = _cairo_fixed_from_double (matrix->x0);
712
17524
        cairo_fixed_t y0_fixed = _cairo_fixed_from_double (matrix->y0);
713

            
714
35032
        if (_cairo_fixed_is_integer (x0_fixed) &&
715
17508
            _cairo_fixed_is_integer (y0_fixed))
716
        {
717
17506
            if (itx)
718
17506
                *itx = _cairo_fixed_integer_part (x0_fixed);
719
17506
            if (ity)
720
17506
                *ity = _cairo_fixed_integer_part (y0_fixed);
721

            
722
17506
            return TRUE;
723
        }
724
    }
725

            
726
16347
    return FALSE;
727
}
728

            
729
#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
947216
_cairo_matrix_has_unity_scale (const cairo_matrix_t *matrix)
739
{
740
    /* check that the determinant is near +/-1 */
741
947216
    double det = _cairo_matrix_compute_determinant (matrix);
742
947216
    if (fabs (det * det - 1.0) < SCALING_EPSILON) {
743
	/* check that one axis is close to zero */
744
1609180
	if (fabs (matrix->xy) < SCALING_EPSILON  &&
745
788726
	    fabs (matrix->yx) < SCALING_EPSILON)
746
788717
	    return TRUE;
747
63267
	if (fabs (matrix->xx) < SCALING_EPSILON  &&
748
31530
	    fabs (matrix->yy) < SCALING_EPSILON)
749
31530
	    return TRUE;
750
	/* If rotations are allowed then it must instead test for
751
	 * orthogonality. This is xx*xy+yx*yy ~= 0.
752
	 */
753
    }
754
126969
    return FALSE;
755
}
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
289404
_cairo_matrix_is_pixel_exact (const cairo_matrix_t *matrix)
764
{
765
    cairo_fixed_t x0_fixed, y0_fixed;
766

            
767
289404
    if (! _cairo_matrix_has_unity_scale (matrix))
768
90582
	return FALSE;
769

            
770
198822
    x0_fixed = _cairo_fixed_from_double (matrix->x0);
771
198822
    y0_fixed = _cairo_fixed_from_double (matrix->y0);
772

            
773
198822
    return _cairo_fixed_is_integer (x0_fixed) && _cairo_fixed_is_integer (y0_fixed);
774
}
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
541000
_cairo_matrix_transformed_circle_major_axis (const cairo_matrix_t *matrix,
899
					     double radius)
900
{
901
    double  a, b, c, d, f, g, h, i, j;
902

            
903
541000
    if (_cairo_matrix_has_unity_scale (matrix))
904
536164
	return radius;
905

            
906
4836
    _cairo_matrix_get_affine (matrix,
907
                              &a, &b,
908
                              &c, &d,
909
                              NULL, NULL);
910

            
911
4836
    i = a*a + b*b;
912
4836
    j = c*c + d*d;
913

            
914
4836
    f = 0.5 * (i + j);
915
4836
    g = 0.5 * (i - j);
916
4836
    h = a*c + b*d;
917

            
918
4836
    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
}
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
41508
_cairo_matrix_to_pixman_matrix (const cairo_matrix_t	*matrix,
934
				pixman_transform_t	*pixman_transform,
935
				double xc,
936
				double yc)
937
{
938
    cairo_matrix_t inv;
939
    unsigned max_iterations;
940

            
941
41508
    pixman_transform->matrix[0][0] = _cairo_fixed_16_16_from_double (matrix->xx);
942
41508
    pixman_transform->matrix[0][1] = _cairo_fixed_16_16_from_double (matrix->xy);
943
41508
    pixman_transform->matrix[0][2] = _cairo_fixed_16_16_from_double (matrix->x0);
944

            
945
41508
    pixman_transform->matrix[1][0] = _cairo_fixed_16_16_from_double (matrix->yx);
946
41508
    pixman_transform->matrix[1][1] = _cairo_fixed_16_16_from_double (matrix->yy);
947
41508
    pixman_transform->matrix[1][2] = _cairo_fixed_16_16_from_double (matrix->y0);
948

            
949
41508
    pixman_transform->matrix[2][0] = 0;
950
41508
    pixman_transform->matrix[2][1] = 0;
951
41508
    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
41508
    if (_cairo_matrix_has_unity_scale (matrix))
965
12030
	return CAIRO_STATUS_SUCCESS;
966

            
967
29478
    if (unlikely (fabs (matrix->xx) > PIXMAN_MAX_INT ||
968
		  fabs (matrix->xy) > PIXMAN_MAX_INT ||
969
		  fabs (matrix->x0) > PIXMAN_MAX_INT ||
970
		  fabs (matrix->yx) > PIXMAN_MAX_INT ||
971
		  fabs (matrix->yy) > PIXMAN_MAX_INT ||
972
		  fabs (matrix->y0) > PIXMAN_MAX_INT))
973
    {
974
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
975
    }
976

            
977
    /* Note: If we can't invert the transformation, skip the adjustment. */
978
29478
    inv = *matrix;
979
29478
    if (cairo_matrix_invert (&inv) != CAIRO_STATUS_SUCCESS)
980
	return CAIRO_STATUS_SUCCESS;
981

            
982
    /* find the pattern space coordinate that maps to (xc, yc) */
983
29478
    max_iterations = 5;
984
    do {
985
	double x,y;
986
	pixman_vector_t vector;
987
	cairo_fixed_16_16_t dx, dy;
988

            
989
40947
	vector.vector[0] = _cairo_fixed_16_16_from_double (xc);
990
40947
	vector.vector[1] = _cairo_fixed_16_16_from_double (yc);
991
40947
	vector.vector[2] = 1 << 16;
992

            
993
	/* If we can't transform the reference point, skip the adjustment. */
994
40947
	if (! pixman_transform_point_3d (pixman_transform, &vector))
995
29478
	    return CAIRO_STATUS_SUCCESS;
996

            
997
40941
	x = pixman_fixed_to_double (vector.vector[0]);
998
40941
	y = pixman_fixed_to_double (vector.vector[1]);
999
40941
	cairo_matrix_transform_point (&inv, &x, &y);
	/* Ideally, the vector should now be (xc, yc).
	 * We can now compensate for the resulting error.
	 */
40941
	x -= xc;
40941
	y -= yc;
40941
	cairo_matrix_transform_distance (matrix, &x, &y);
40941
	dx = _cairo_fixed_16_16_from_double (x);
40941
	dy = _cairo_fixed_16_16_from_double (y);
40941
	pixman_transform->matrix[0][2] -= dx;
40941
	pixman_transform->matrix[1][2] -= dy;
40941
	if (dx == 0 && dy == 0)
29472
	    return CAIRO_STATUS_SUCCESS;
11469
    } while (--max_iterations);
    /* We didn't find an exact match between cairo and pixman, but
     * the matrix should be mostly correct */
    return CAIRO_STATUS_SUCCESS;
}
static inline double
101318
_pixman_nearest_sample (double d)
{
101318
    return ceil (d - .5);
}
/**
 * _cairo_matrix_is_pixman_translation:
 * @matrix: a matrix
 * @filter: the filter to be used on the pattern transformed by @matrix
 * @x_offset: the translation in the X direction
 * @y_offset: the translation in the Y direction
 *
 * Checks if @matrix translated by (x_offset, y_offset) can be
 * represented using just an offset (within the range pixman can
 * accept) and an identity matrix.
 *
 * Passing a non-zero value in x_offset/y_offset has the same effect
 * as applying cairo_matrix_translate(matrix, x_offset, y_offset) and
 * setting x_offset and y_offset to 0.
 *
 * Upon return x_offset and y_offset contain the translation vector if
 * the return value is %TRUE. If the return value is %FALSE, they will
 * not be modified.
 *
 * Return value: %TRUE if @matrix can be represented as a pixman
 * translation, %FALSE otherwise.
 **/
cairo_bool_t
388276
_cairo_matrix_is_pixman_translation (const cairo_matrix_t     *matrix,
				     cairo_filter_t            filter,
				     int                      *x_offset,
				     int                      *y_offset)
{
    double tx, ty;
388276
    if (!_cairo_matrix_is_translation (matrix))
212903
	return FALSE;
175373
    if (matrix->x0 == 0. && matrix->y0 == 0.)
124582
	return TRUE;
50791
    tx = matrix->x0 + *x_offset;
50791
    ty = matrix->y0 + *y_offset;
50791
    if (filter == CAIRO_FILTER_FAST || filter == CAIRO_FILTER_NEAREST) {
50659
	tx = _pixman_nearest_sample (tx);
50659
	ty = _pixman_nearest_sample (ty);
132
    } else if (tx != floor (tx) || ty != floor (ty)) {
132
	return FALSE;
    }
50659
    if (fabs (tx) > PIXMAN_MAX_INT || fabs (ty) > PIXMAN_MAX_INT)
	return FALSE;
50659
    *x_offset = _cairo_lround (tx);
50659
    *y_offset = _cairo_lround (ty);
50659
    return TRUE;
}
/**
 * _cairo_matrix_to_pixman_matrix_offset:
 * @matrix: a matrix
 * @filter: the filter to be used on the pattern transformed by @matrix
 * @xc: the X coordinate of the point to fix in pattern space
 * @yc: the Y coordinate of the point to fix in pattern space
 * @out_transform: the transformation which best approximates @matrix
 * @x_offset: the translation in the X direction
 * @y_offset: the translation in the Y direction
 *
 * This function tries to represent @matrix translated by (x_offset,
 * y_offset) as a %pixman_transform_t and an translation.
 *
 * Passing a non-zero value in x_offset/y_offset has the same effect
 * as applying cairo_matrix_translate(matrix, x_offset, y_offset) and
 * setting x_offset and y_offset to 0.
 *
 * If it is possible to represent the matrix with an identity
 * %pixman_transform_t and a translation within the valid range for
 * pixman, this function will set @out_transform to be the identity,
 * @x_offset and @y_offset to be the translation vector and will
 * return %CAIRO_INT_STATUS_NOTHING_TO_DO. Otherwise it will try to
 * evenly divide the translational component of @matrix between
 * @out_transform and (@x_offset, @y_offset).
 *
 * Upon return x_offset and y_offset contain the translation vector.
 *
 * Return value: %CAIRO_INT_STATUS_NOTHING_TO_DO if the out_transform
 * is the identity, %CAIRO_STATUS_INVALID_MATRIX if it was not
 * possible to represent @matrix as a pixman_transform_t without
 * overflows, %CAIRO_STATUS_SUCCESS otherwise.
 **/
cairo_status_t
70362
_cairo_matrix_to_pixman_matrix_offset (const cairo_matrix_t	*matrix,
				       cairo_filter_t            filter,
				       double                    xc,
				       double                    yc,
				       pixman_transform_t	*out_transform,
				       int                      *x_offset,
				       int                      *y_offset)
{
    cairo_bool_t is_pixman_translation;
70362
    is_pixman_translation = _cairo_matrix_is_pixman_translation (matrix,
								 filter,
								 x_offset,
								 y_offset);
70362
    if (is_pixman_translation) {
28854
	*out_transform = pixman_identity_transform;
28854
	return CAIRO_INT_STATUS_NOTHING_TO_DO;
    } else {
	cairo_matrix_t m;
41508
	m = *matrix;
41508
	cairo_matrix_translate (&m, *x_offset, *y_offset);
71478
	if (m.x0 != 0.0 || m.y0 != 0.0) {
	    double tx, ty, norm;
	    int i, j;
	    /* pixman also limits the [xy]_offset to 16 bits so evenly
	     * spread the bits between the two.
	     *
	     * To do this, find the solutions of:
	     *   |x| = |x*m.xx + y*m.xy + m.x0|
	     *   |y| = |x*m.yx + y*m.yy + m.y0|
	     *
	     * and select the one whose maximum norm is smallest.
	     */
29970
	    tx = m.x0;
29970
	    ty = m.y0;
29970
	    norm = MAX (fabs (tx), fabs (ty));
89910
	    for (i = -1; i < 2; i+=2) {
179820
		for (j = -1; j < 2; j+=2) {
		    double x, y, den, new_norm;
119880
		    den = (m.xx + i) * (m.yy + j) - m.xy * m.yx;
119880
		    if (fabs (den) < DBL_EPSILON)
25275
			continue;
94605
		    x = m.y0 * m.xy - m.x0 * (m.yy + j);
94605
		    y = m.x0 * m.yx - m.y0 * (m.xx + i);
94605
		    den = 1 / den;
94605
		    x *= den;
94605
		    y *= den;
94605
		    new_norm = MAX (fabs (x), fabs (y));
94605
		    if (norm > new_norm) {
34515
			norm = new_norm;
34515
			tx = x;
34515
			ty = y;
		    }
		}
	    }
29970
	    tx = floor (tx);
29970
	    ty = floor (ty);
29970
	    *x_offset = -tx;
29970
	    *y_offset = -ty;
29970
	    cairo_matrix_translate (&m, tx, ty);
	} else {
11538
	    *x_offset = 0;
11538
	    *y_offset = 0;
	}
41508
	return _cairo_matrix_to_pixman_matrix (&m, out_transform, xc, yc);
    }
}