4 * Copyright © 2003 Keith Packard
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.
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.
28 rawindex (FcGlyphName *gn);
31 scan (FILE *f, char *filename);
40 FcHashGlyphName (const FcChar8 *name);
43 insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h);
46 dump (FcGlyphName **table, char *name);
49 FcAllocGlyphName (FcChar32 ucs, FcChar8 *name)
53 gn = malloc (sizeof (FcGlyphName) + strlen ((char *) name));
57 strcpy ((char *) gn->name, (char *) name);
62 fatal (char *file, int lineno, char *msg)
64 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
68 #define MAX_GLYPHFILE 256
69 #define MAX_GLYPHNAME 10240
70 #define MAX_NAMELEN 1024
72 FcGlyphName *raw[MAX_GLYPHNAME];
75 FcGlyphName *name_to_ucs[MAX_GLYPHNAME*2];
76 FcGlyphName *ucs_to_name[MAX_GLYPHNAME*2];
80 rawindex (FcGlyphName *gn)
84 for (i = 0; i < nraw; i++)
91 scan (FILE *f, char *filename)
93 char buf[MAX_NAMELEN];
94 char name[MAX_NAMELEN];
100 while (fgets (buf, sizeof (buf), f))
103 if (sscanf (buf, "%[^;];%lx\n", name, &ucs) != 2)
105 gn = FcAllocGlyphName ((FcChar32) ucs, (FcChar8 *) name);
107 fatal (filename, lineno, "out of memory");
108 len = strlen ((FcChar8 *) name);
109 if (len > max_name_len)
115 static int compare_string (const void *a, const void *b)
117 const char *const *as = a, *const *bs = b;
118 return strcmp (*as, *bs);
121 static int compare_glyphname (const void *a, const void *b)
123 const FcGlyphName *const *ag = a, *const *bg = b;
125 return strcmp ((char *) (*ag)->name, (char *) (*bg)->name);
160 for (t = 3; t <= l; t += 2)
167 * Find a prime pair that leaves at least 25% of the hash table empty
178 while (!isprime(h-2) || !isprime(h))
185 FcHashGlyphName (const FcChar8 *name)
190 while ((c = *name++))
192 h = ((h << 1) | (h >> 31)) ^ c;
198 insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h)
202 i = (int) (h % hash);
205 if (!r) r = (int) (h % rehash);
214 dump (FcGlyphName **table, char *name)
218 printf ("static FcGlyphName *%s[%d] = {\n", name, hash);
220 for (i = 0; i < hash; i++)
222 printf ("(FcGlyphName *) &glyph%d,\n", rawindex(table[i]));
230 main (int argc, char **argv)
232 char *files[MAX_GLYPHFILE];
240 if (i == MAX_GLYPHFILE)
241 fatal (*argv, 0, "Too many glyphname files");
245 qsort (files, i, sizeof (char *), compare_string);
246 for (i = 0; files[i]; i++)
248 f = fopen (files[i], "r");
250 fatal (files[i], 0, strerror (errno));
254 qsort (raw, nraw, sizeof (FcGlyphName *), compare_glyphname);
258 for (i = 0; i < nraw; i++)
260 insert (raw[i], name_to_ucs, FcHashGlyphName (raw[i]->name));
261 insert (raw[i], ucs_to_name, raw[i]->ucs);
265 * Scan the input until the marker is found
268 while (fgets (line, sizeof (line), stdin))
270 if (!strncmp (line, "@@@", 3))
272 fputs (line, stdout);
275 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
276 nraw, hash, nraw * 100 / hash);
278 printf ("#define FC_GLYPHNAME_HASH %u\n", hash);
279 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash);
280 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len);
286 for (i = 0; i < nraw; i++)
287 printf ("static struct { FcChar32 ucs; FcChar8 name[%d]; }"
288 " glyph%d = { 0x%lx, \"%s\" };\n",
289 strlen (raw[i]->name) + 1,
290 i, (unsigned long) raw[i]->ucs, raw[i]->name);
293 * Dump out name_to_ucs table
296 dump (name_to_ucs, "name_to_ucs");
299 * Dump out ucs_to_name table
301 dump (ucs_to_name, "ucs_to_name");
303 while (fgets (line, sizeof (line), stdin))
304 fputs (line, stdout);
307 exit (ferror (stdout));