]> git.wh0rd.org - fontconfig.git/blob - src/fcfs.c
#ifdef out old cache stuff, replace with first version of new mmapping
[fontconfig.git] / src / fcfs.c
1 /*
2 * $RCSId: $
3 *
4 * Copyright © 2000 Keith Packard
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard 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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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 <stdlib.h>
26 #include "fcint.h"
27
28 FcFontSet *
29 FcFontSetCreate (void)
30 {
31 FcFontSet *s;
32
33 s = (FcFontSet *) malloc (sizeof (FcFontSet));
34 if (!s)
35 return 0;
36 FcMemAlloc (FC_MEM_FONTSET, sizeof (FcFontSet));
37 s->nfont = 0;
38 s->sfont = 0;
39 s->fonts = 0;
40 return s;
41 }
42
43 void
44 FcFontSetDestroy (FcFontSet *s)
45 {
46 int i;
47
48 for (i = 0; i < s->nfont; i++)
49 FcPatternDestroy (s->fonts[i]);
50 if (s->fonts)
51 {
52 FcMemFree (FC_MEM_FONTPTR, s->sfont * sizeof (FcPattern *));
53 free (s->fonts);
54 }
55 FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
56 free (s);
57 }
58
59 FcBool
60 FcFontSetAdd (FcFontSet *s, FcPattern *font)
61 {
62 FcPattern **f;
63 int sfont;
64
65 if (s->nfont == s->sfont)
66 {
67 sfont = s->sfont + 32;
68 if (s->fonts)
69 f = (FcPattern **) realloc (s->fonts, sfont * sizeof (FcPattern *));
70 else
71 f = (FcPattern **) malloc (sfont * sizeof (FcPattern *));
72 if (!f)
73 return FcFalse;
74 if (s->sfont)
75 FcMemFree (FC_MEM_FONTPTR, s->sfont * sizeof (FcPattern *));
76 FcMemAlloc (FC_MEM_FONTPTR, sfont * sizeof (FcPattern *));
77 s->sfont = sfont;
78 s->fonts = f;
79 }
80 s->fonts[s->nfont++] = font;
81 return FcTrue;
82 }
83
84 FcBool
85 FcFontSetPrepareSerialize (FcFontSet *s)
86 {
87 int i;
88
89 for (i = 0; i < s->nfont; i++)
90 if (!FcPatternPrepareSerialize(s->fonts[i]))
91 return FcFalse;
92
93 return FcTrue;
94 }
95
96 FcBool
97 FcFontSetSerialize (FcFontSet * s)
98 {
99 int i;
100 FcPattern * p;
101
102 for (i = 0; i < s->nfont; i++)
103 {
104 p = FcPatternSerialize (s->fonts[i]);
105 if (!p) return FcFalse;
106 FcPatternDestroy (s->fonts[i]);
107
108 s->fonts[i] = p;
109 }
110
111 return FcTrue;
112 }
113
114 void
115 FcFontSetClearStatic (void)
116 {
117 FcPatternClearStatic();
118 }
119
120 FcBool
121 FcFontSetRead(int fd, FcConfig * config, FcCache metadata)
122 {
123 int i, mz, j;
124 FcPattern * buf;
125
126 lseek(fd, metadata.fontsets_offset, SEEK_SET);
127 for (i = FcSetSystem; i <= FcSetApplication; i++)
128 {
129 if (config->fonts[i])
130 {
131 if (config->fonts[i]->nfont > 0 && config->fonts[i]->fonts)
132 free (config->fonts[i]->fonts);
133 free (config->fonts[i]);
134 }
135 }
136
137 for (i = FcSetSystem; i <= FcSetApplication; i++)
138 {
139 read(fd, &mz, sizeof(int));
140 if (mz != FC_CACHE_MAGIC)
141 continue;
142
143 config->fonts[i] = malloc(sizeof(FcFontSet));
144 if (!config->fonts[i])
145 return FcFalse;
146 FcMemAlloc(FC_MEM_FONTSET, sizeof(FcFontSet));
147
148 if (read(fd, config->fonts[i], sizeof(FcFontSet)) == -1)
149 goto bail;
150 if (config->fonts[i]->sfont > 0)
151 {
152 config->fonts[i]->fonts = malloc
153 (config->fonts[i]->sfont*sizeof(FcPattern *));
154 buf = malloc (config->fonts[i]->sfont * sizeof(FcPattern));
155 if (!config->fonts[i]->fonts || !buf)
156 goto bail;
157 for (j = 0; j < config->fonts[i]->nfont; j++)
158 {
159 config->fonts[i]->fonts[j] = buf+j;
160 if (read(fd, buf+j, sizeof(FcPattern)) == -1)
161 goto bail;
162 }
163 }
164 }
165
166 return FcTrue;
167 bail:
168 for (i = FcSetSystem; i <= FcSetApplication; i++)
169 {
170 if (config->fonts[i])
171 {
172 if (config->fonts[i]->fonts)
173 free (config->fonts[i]->fonts);
174 free(config->fonts[i]);
175 }
176 config->fonts[i] = 0;
177 }
178 return FcFalse;
179 }
180
181 FcBool
182 FcFontSetWrite(int fd, FcConfig * config, FcCache * metadata)
183 {
184 int c, t, i, j;
185 int m = FC_CACHE_MAGIC, z = 0;
186
187 metadata->fontsets_offset = FcCacheNextOffset(fd);
188 lseek(fd, metadata->fontsets_offset, SEEK_SET);
189 for (i = FcSetSystem; i <= FcSetApplication; i++)
190 {
191 if (!config->fonts[i])
192 {
193 write(fd, &z, sizeof(int));
194 continue;
195 }
196 else
197 write(fd, &m, sizeof(int));
198
199 if ((c = write(fd, config->fonts[i], sizeof(FcFontSet))) == -1)
200 return FcFalse;
201 t = c;
202 if (config->fonts[i]->nfont > 0)
203 {
204 for (j = 0; j < config->fonts[i]->nfont; j++)
205 {
206 if ((c = write(fd, config->fonts[i]->fonts[j],
207 sizeof(FcPattern))) == -1)
208 return FcFalse;
209 }
210 }
211 }
212 return FcTrue;
213 }