]> git.wh0rd.org - fontconfig.git/blob - src/fcmatrix.c
#ifdef out old cache stuff, replace with first version of new mmapping
[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 <sys/mman.h>
29 #include "fcint.h"
30
31 FcMatrix _id = { 1, 0, 0, 1 };
32 const FcMatrixPtr FcIdentityMatrix = {
33 .storage = FcStorageDynamic,
34 .u.dyn = &_id
35 };
36
37 FcMatrix *
38 FcMatrixCopy (const FcMatrix *mat)
39 {
40 FcMatrix *r;
41 if(!mat)
42 return 0;
43 r = (FcMatrix *) malloc (sizeof (*r) );
44 if (!r)
45 return 0;
46 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
47 *r = *mat;
48 return r;
49 }
50
51 void
52 FcMatrixPtrDestroy (FcMatrixPtr mi)
53 {
54 if (mi.storage == FcStorageDynamic)
55 FcMatrixFree (mi.u.dyn);
56 }
57
58 void
59 FcMatrixFree (FcMatrix *mat)
60 {
61 if (mat != FcMatrixPtrU(FcIdentityMatrix))
62 {
63 FcMemFree (FC_MEM_MATRIX, sizeof (FcMatrix));
64 free (mat);
65 }
66 }
67
68 FcBool
69 FcMatrixEqual (const FcMatrix *mat1, const FcMatrix *mat2)
70 {
71 if(mat1 == mat2) return FcTrue;
72 if(mat1 == 0 || mat2 == 0) return FcFalse;
73 return mat1->xx == mat2->xx &&
74 mat1->xy == mat2->xy &&
75 mat1->yx == mat2->yx &&
76 mat1->yy == mat2->yy;
77 }
78
79 void
80 FcMatrixMultiply (FcMatrix *result, const FcMatrix *a, const FcMatrix *b)
81 {
82 FcMatrix r;
83
84 r.xx = a->xx * b->xx + a->xy * b->yx;
85 r.xy = a->xx * b->xy + a->xy * b->yy;
86 r.yx = a->yx * b->xx + a->yy * b->yx;
87 r.yy = a->yx * b->xy + a->yy * b->yy;
88 *result = r;
89 }
90
91 void
92 FcMatrixRotate (FcMatrix *m, double c, double s)
93 {
94 FcMatrix r;
95
96 /*
97 * X Coordinate system is upside down, swap to make
98 * rotations counterclockwise
99 */
100 r.xx = c;
101 r.xy = -s;
102 r.yx = s;
103 r.yy = c;
104 FcMatrixMultiply (m, &r, m);
105 }
106
107 void
108 FcMatrixScale (FcMatrix *m, double sx, double sy)
109 {
110 FcMatrix r;
111
112 r.xx = sx;
113 r.xy = 0;
114 r.yx = 0;
115 r.yy = sy;
116 FcMatrixMultiply (m, &r, m);
117 }
118
119 void
120 FcMatrixShear (FcMatrix *m, double sh, double sv)
121 {
122 FcMatrix r;
123
124 r.xx = 1;
125 r.xy = sh;
126 r.yx = sv;
127 r.yy = 1;
128 FcMatrixMultiply (m, &r, m);
129 }
130
131 static FcMatrix * matrices = 0;
132 static int matrix_ptr = 0, matrix_count = 0;
133
134 void
135 FcMatrixClearStatic (void)
136 {
137 matrices = 0;
138 matrix_ptr = 0;
139 matrix_count = 0;
140 }
141
142 FcMatrix *
143 FcMatrixPtrU (FcMatrixPtr mi)
144 {
145 switch (mi.storage)
146 {
147 case FcStorageDynamic:
148 return mi.u.dyn;
149 case FcStorageStatic:
150 return &matrices[mi.u.stat];
151 default:
152 return 0;
153
154 }
155 }
156
157 FcMatrixPtr
158 FcMatrixPtrCreateDynamic (FcMatrix *mi)
159 {
160 FcMatrixPtr new;
161 new.storage = FcStorageDynamic;
162 new.u.dyn = mi;
163 return new;
164 }
165
166 FcBool
167 FcMatrixPrepareSerialize(FcMatrix *m)
168 {
169 matrix_count++;
170 return FcTrue;
171 }
172
173 FcMatrixPtr
174 FcMatrixSerialize(FcMatrix *m)
175 {
176 FcMatrixPtr new;
177
178 if (matrix_count == matrix_ptr)
179 return FcMatrixPtrCreateDynamic(0);
180
181 new.storage = FcStorageStatic;
182 new.u.stat = matrix_ptr++;
183 return new;
184 }
185
186 FcBool
187 FcMatrixRead (int fd, FcCache metadata)
188 {
189 matrices = mmap(NULL,
190 metadata.matrices_length * sizeof (FcMatrix),
191 PROT_READ,
192 MAP_SHARED, fd, metadata.matrices_offset);
193 if (matrices == MAP_FAILED)
194 return FcFalse;
195
196 matrix_count = matrix_ptr = metadata.matrices_length;
197 return FcTrue;
198 }
199
200 FcBool
201 FcMatrixWrite (int fd, FcCache *metadata)
202 {
203 metadata->matrices_length = matrix_ptr;
204 metadata->matrices_offset = FcCacheNextOffset(fd);
205
206 if (matrix_ptr > 0)
207 {
208 lseek(fd, metadata->matrices_offset, SEEK_SET);
209 return write(fd, matrices,
210 metadata->matrices_length * sizeof(FcMatrix)) != -1;
211 }
212 return FcTrue;
213 }