]> git.wh0rd.org - fontconfig.git/blame - fc-glyphname/fc-glyphname.c
Add architecture to cache filename.
[fontconfig.git] / fc-glyphname / fc-glyphname.c
CommitLineData
721d496d
KP
1/*
2 * $Id$
3 *
46b51147 4 * Copyright © 2003 Keith Packard
721d496d
KP
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 "fcint.h"
26
ead55be0
PL
27/* stub definitions for declarations from fcint.h.. */
28int * _fcBankId = 0, * _fcBankIdx = 0;
67ed0b72
PL
29FcValueList ** _fcValueLists = 0;
30FcPatternElt ** _fcPatternElts = 0;
31int FcDebugVal = 0;
ead55be0
PL
32
33int
34FcCacheBankToIndexMTF (int bank)
35{
36 return 0;
37}
38/* end stub definitions */
39
34cd0514 40static int
67accef4 41rawindex (const FcGlyphName *gn);
34cd0514
CW
42
43static void
44scan (FILE *f, char *filename);
45
46static int
47isprime (int i);
48
49static void
50find_hash (void);
51
52static FcChar32
53FcHashGlyphName (const FcChar8 *name);
54
55static void
56insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h);
57
58static void
67accef4 59dump (FcGlyphName * const *table, const char *name);
34cd0514 60
721d496d
KP
61static FcGlyphName *
62FcAllocGlyphName (FcChar32 ucs, FcChar8 *name)
63{
64 FcGlyphName *gn;
65
66 gn = malloc (sizeof (FcGlyphName) + strlen ((char *) name));
67 if (!gn)
68 return 0;
69 gn->ucs = ucs;
70 strcpy ((char *) gn->name, (char *) name);
71 return gn;
72}
73
74static void
67accef4 75fatal (const char *file, int lineno, const char *msg)
721d496d 76{
67accef4
PL
77 if (lineno)
78 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
79 else
80 fprintf (stderr, "%s: %s\n", file, msg);
81
721d496d
KP
82 exit (1);
83}
84
85#define MAX_GLYPHFILE 256
86#define MAX_GLYPHNAME 10240
87#define MAX_NAMELEN 1024
88
0d745819
PL
89static FcGlyphName *raw[MAX_GLYPHNAME];
90static int nraw;
91static int max_name_len;
92static FcGlyphName *name_to_ucs[MAX_GLYPHNAME*2];
93static FcGlyphName *ucs_to_name[MAX_GLYPHNAME*2];
94static unsigned int hash, rehash;
721d496d 95
34cd0514 96static int
67accef4 97rawindex (const FcGlyphName *gn)
721d496d
KP
98{
99 int i;
100
101 for (i = 0; i < nraw; i++)
102 if (raw[i] == gn)
103 return i;
104 return -1;
105}
106
34cd0514 107static void
721d496d
KP
108scan (FILE *f, char *filename)
109{
110 char buf[MAX_NAMELEN];
111 char name[MAX_NAMELEN];
112 unsigned long ucs;
113 FcGlyphName *gn;
114 int lineno = 0;
115 int len;
116
117 while (fgets (buf, sizeof (buf), f))
118 {
119 lineno++;
120 if (sscanf (buf, "%[^;];%lx\n", name, &ucs) != 2)
121 continue;
122 gn = FcAllocGlyphName ((FcChar32) ucs, (FcChar8 *) name);
123 if (!gn)
124 fatal (filename, lineno, "out of memory");
8245771d 125 len = strlen (name);
721d496d
KP
126 if (len > max_name_len)
127 max_name_len = len;
128 raw[nraw++] = gn;
129 }
130}
131
132static int compare_string (const void *a, const void *b)
133{
134 const char *const *as = a, *const *bs = b;
135 return strcmp (*as, *bs);
136}
137
138static int compare_glyphname (const void *a, const void *b)
139{
140 const FcGlyphName *const *ag = a, *const *bg = b;
141
142 return strcmp ((char *) (*ag)->name, (char *) (*bg)->name);
143}
144
145static int
146isqrt (int a)
147{
148 int l, h, m;
149
150 l = 2;
151 h = a/2;
152 while ((h-l) > 1)
153 {
154 m = (h+l) >> 1;
155 if (m * m < a)
156 l = m;
157 else
158 h = m;
159 }
160 return h;
161}
162
34cd0514 163static int
721d496d
KP
164isprime (int i)
165{
166 int l, t;
167
168 if (i < 2)
169 return FcFalse;
170 if ((i & 1) == 0)
171 {
172 if (i == 2)
173 return FcTrue;
174 return FcFalse;
175 }
176 l = isqrt (i) + 1;
177 for (t = 3; t <= l; t += 2)
178 if (i % t == 0)
179 return 0;
180 return 1;
181}
182
183/*
184 * Find a prime pair that leaves at least 25% of the hash table empty
185 */
186
34cd0514 187static void
721d496d
KP
188find_hash (void)
189{
190 int h;
191
192 h = nraw + nraw / 4;
193 if ((h & 1) == 0)
194 h++;
195 while (!isprime(h-2) || !isprime(h))
196 h += 2;
197 hash = h;
198 rehash = h-2;
199}
200
34cd0514 201static FcChar32
721d496d
KP
202FcHashGlyphName (const FcChar8 *name)
203{
204 FcChar32 h = 0;
205 FcChar8 c;
206
207 while ((c = *name++))
208 {
209 h = ((h << 1) | (h >> 31)) ^ c;
210 }
211 return h;
212}
213
34cd0514 214static void
721d496d
KP
215insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h)
216{
217 int i, r = 0;
218
219 i = (int) (h % hash);
220 while (table[i])
221 {
222 if (!r) r = (int) (h % rehash);
223 i += r;
224 if (i >= hash)
225 i -= hash;
226 }
227 table[i] = gn;
228}
229
34cd0514 230static void
67accef4 231dump (FcGlyphName * const *table, const char *name)
721d496d
KP
232{
233 int i;
234
21696e5b 235 printf ("static const FcGlyphName *%s[%d] = {\n", name, hash);
721d496d
KP
236
237 for (i = 0; i < hash; i++)
238 if (table[i])
239 printf ("(FcGlyphName *) &glyph%d,\n", rawindex(table[i]));
240 else
241 printf ("0,\n");
242
243 printf ("};\n");
244}
245
246int
247main (int argc, char **argv)
248{
249 char *files[MAX_GLYPHFILE];
250 char line[1024];
251 FILE *f;
252 int i;
253
254 i = 0;
67accef4 255 while (argv[i+1])
721d496d
KP
256 {
257 if (i == MAX_GLYPHFILE)
258 fatal (*argv, 0, "Too many glyphname files");
67accef4
PL
259 files[i] = argv[i+1];
260 i++;
721d496d
KP
261 }
262 files[i] = 0;
263 qsort (files, i, sizeof (char *), compare_string);
264 for (i = 0; files[i]; i++)
265 {
266 f = fopen (files[i], "r");
267 if (!f)
268 fatal (files[i], 0, strerror (errno));
269 scan (f, files[i]);
270 fclose (f);
271 }
272 qsort (raw, nraw, sizeof (FcGlyphName *), compare_glyphname);
273
274 find_hash ();
275
276 for (i = 0; i < nraw; i++)
277 {
278 insert (raw[i], name_to_ucs, FcHashGlyphName (raw[i]->name));
279 insert (raw[i], ucs_to_name, raw[i]->ucs);
280 }
281
282 /*
283 * Scan the input until the marker is found
284 */
285
286 while (fgets (line, sizeof (line), stdin))
287 {
288 if (!strncmp (line, "@@@", 3))
289 break;
290 fputs (line, stdout);
291 }
292
293 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
294 nraw, hash, nraw * 100 / hash);
295
296 printf ("#define FC_GLYPHNAME_HASH %u\n", hash);
297 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash);
298 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len);
299
300 /*
301 * Dump out entries
302 */
303
304 for (i = 0; i < nraw; i++)
21696e5b 305 printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; }"
721d496d 306 " glyph%d = { 0x%lx, \"%s\" };\n",
8245771d 307 (int) strlen ((char *) raw[i]->name) + 1,
721d496d
KP
308 i, (unsigned long) raw[i]->ucs, raw[i]->name);
309
310 /*
311 * Dump out name_to_ucs table
312 */
313
314 dump (name_to_ucs, "name_to_ucs");
315
316 /*
317 * Dump out ucs_to_name table
318 */
319 dump (ucs_to_name, "ucs_to_name");
320
321 while (fgets (line, sizeof (line), stdin))
322 fputs (line, stdout);
323
324 fflush (stdout);
325 exit (ferror (stdout));
326}