]> git.wh0rd.org - fontconfig.git/blob - src/fcmatrix.c
Add functionality to allow fontconfig data structure serialization.
[fontconfig.git] / src / fcmatrix.c
1 /*
2 * $RCSId: $
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 _id = { 1, 0, 0, 1 };
31 const FcMatrixPtr FcIdentityMatrix = {
32 .storage = FcStorageDynamic,
33 .u.dyn = &_id
34 };
35
36 FcMatrix *
37 FcMatrixCopy (const FcMatrix *mat)
38 {
39 FcMatrix *r;
40 if(!mat)
41 return 0;
42 r = (FcMatrix *) malloc (sizeof (*r) );
43 if (!r)
44 return 0;
45 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
46 *r = *mat;
47 return r;
48 }
49
50 void
51 FcMatrixPtrDestroy (FcMatrixPtr mi)
52 {
53 if (mi.storage == FcStorageDynamic)
54 FcMatrixFree (mi.u.dyn);
55 }
56
57 void
58 FcMatrixFree (FcMatrix *mat)
59 {
60 if (mat != FcMatrixPtrU(FcIdentityMatrix))
61 {
62 FcMemFree (FC_MEM_MATRIX, sizeof (FcMatrix));
63 free (mat);
64 }
65 }
66
67 FcBool
68 FcMatrixEqual (const FcMatrix *mat1, const FcMatrix *mat2)
69 {
70 if(mat1 == mat2) return FcTrue;
71 if(mat1 == 0 || mat2 == 0) return FcFalse;
72 return mat1->xx == mat2->xx &&
73 mat1->xy == mat2->xy &&
74 mat1->yx == mat2->yx &&
75 mat1->yy == mat2->yy;
76 }
77
78 void
79 FcMatrixMultiply (FcMatrix *result, const FcMatrix *a, const FcMatrix *b)
80 {
81 FcMatrix r;
82
83 r.xx = a->xx * b->xx + a->xy * b->yx;
84 r.xy = a->xx * b->xy + a->xy * b->yy;
85 r.yx = a->yx * b->xx + a->yy * b->yx;
86 r.yy = a->yx * b->xy + a->yy * b->yy;
87 *result = r;
88 }
89
90 void
91 FcMatrixRotate (FcMatrix *m, double c, double s)
92 {
93 FcMatrix r;
94
95 /*
96 * X Coordinate system is upside down, swap to make
97 * rotations counterclockwise
98 */
99 r.xx = c;
100 r.xy = -s;
101 r.yx = s;
102 r.yy = c;
103 FcMatrixMultiply (m, &r, m);
104 }
105
106 void
107 FcMatrixScale (FcMatrix *m, double sx, double sy)
108 {
109 FcMatrix r;
110
111 r.xx = sx;
112 r.xy = 0;
113 r.yx = 0;
114 r.yy = sy;
115 FcMatrixMultiply (m, &r, m);
116 }
117
118 void
119 FcMatrixShear (FcMatrix *m, double sh, double sv)
120 {
121 FcMatrix r;
122
123 r.xx = 1;
124 r.xy = sh;
125 r.yx = sv;
126 r.yy = 1;
127 FcMatrixMultiply (m, &r, m);
128 }
129
130 static FcMatrix * matrices = 0;
131 static int matrix_ptr = 0, matrix_count = 0;
132
133 void
134 FcMatrixClearStatic (void)
135 {
136 matrices = 0;
137 matrix_ptr = 0;
138 matrix_count = 0;
139 }
140
141 FcMatrix *
142 FcMatrixPtrU (FcMatrixPtr mi)
143 {
144 switch (mi.storage)
145 {
146 case FcStorageDynamic:
147 return mi.u.dyn;
148 case FcStorageStatic:
149 return &matrices[mi.u.stat];
150 default:
151 return 0;
152
153 }
154 }
155
156 FcMatrixPtr
157 FcMatrixPtrCreateDynamic (FcMatrix *mi)
158 {
159 FcMatrixPtr new;
160 new.storage = FcStorageDynamic;
161 new.u.dyn = mi;
162 return new;
163 }
164
165 FcBool
166 FcMatrixPrepareSerialize(FcMatrix *m)
167 {
168 matrix_count++;
169 return FcTrue;
170 }
171
172 FcMatrixPtr
173 FcMatrixSerialize(FcMatrix *m)
174 {
175 FcMatrixPtr new;
176
177 if (matrix_count == matrix_ptr)
178 return FcMatrixPtrCreateDynamic(0);
179
180 new.storage = FcStorageStatic;
181 new.u.stat = matrix_ptr++;
182 return new;
183 }
184