]> git.wh0rd.org - fontconfig.git/blob - src/fcmatrix.c
Initial revision
[fontconfig.git] / src / fcmatrix.c
1 /*
2 * $XFree86: $
3 *
4 * Copyright © 2000 Tuomas J. Lukka
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Tuomas Lukka not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Tuomas Lukka makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * TUOMAS LUKKA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL TUOMAS LUKKA BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 #include <math.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include "fcint.h"
29
30 FcMatrix *
31 FcMatrixCopy (const FcMatrix *mat)
32 {
33 FcMatrix *r;
34 if(!mat)
35 return 0;
36 r = (FcMatrix *) malloc (sizeof (*r) );
37 if (!r)
38 return 0;
39 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
40 *r = *mat;
41 return r;
42 }
43
44 void
45 FcMatrixFree (FcMatrix *mat)
46 {
47 FcMemFree (FC_MEM_MATRIX, sizeof (FcMatrix));
48 free (mat);
49 }
50
51 FcBool
52 FcMatrixEqual (const FcMatrix *mat1, const FcMatrix *mat2)
53 {
54 if(mat1 == mat2) return FcTrue;
55 if(mat1 == 0 || mat2 == 0) return FcFalse;
56 return mat1->xx == mat2->xx &&
57 mat1->xy == mat2->xy &&
58 mat1->yx == mat2->yx &&
59 mat1->yy == mat2->yy;
60 }
61
62 void
63 FcMatrixMultiply (FcMatrix *result, const FcMatrix *a, const FcMatrix *b)
64 {
65 FcMatrix r;
66
67 r.xx = a->xx * b->xx + a->xy * b->yx;
68 r.xy = a->xx * b->xy + a->xy * b->yy;
69 r.yx = a->yx * b->xx + a->yy * b->yx;
70 r.yy = a->yx * b->xy + a->yy * b->yy;
71 *result = r;
72 }
73
74 void
75 FcMatrixRotate (FcMatrix *m, double c, double s)
76 {
77 FcMatrix r;
78
79 /*
80 * X Coordinate system is upside down, swap to make
81 * rotations counterclockwise
82 */
83 r.xx = c;
84 r.xy = -s;
85 r.yx = s;
86 r.yy = c;
87 FcMatrixMultiply (m, &r, m);
88 }
89
90 void
91 FcMatrixScale (FcMatrix *m, double sx, double sy)
92 {
93 FcMatrix r;
94
95 r.xx = sx;
96 r.xy = 0;
97 r.yx = 0;
98 r.yy = sy;
99 FcMatrixMultiply (m, &r, m);
100 }
101
102 void
103 FcMatrixShear (FcMatrix *m, double sh, double sv)
104 {
105 FcMatrix r;
106
107 r.xx = 1;
108 r.xy = sh;
109 r.yx = sv;
110 r.yy = 1;
111 FcMatrixMultiply (m, &r, m);
112 }