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 (const FcGlyphName *gn);
31 scan (FILE *f, char *filename);
40 FcHashGlyphName (const FcChar8 *name);
43 insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h);
46 dump (FcGlyphName * const *table, const 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 (const char *file, int lineno, const char *msg)
65 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
67 fprintf (stderr, "%s: %s\n", file, msg);
72 #define MAX_GLYPHFILE 256
73 #define MAX_GLYPHNAME 10240
74 #define MAX_NAMELEN 1024
76 static FcGlyphName *raw[MAX_GLYPHNAME];
78 static int max_name_len;
79 static FcGlyphName *name_to_ucs[MAX_GLYPHNAME*2];
80 static FcGlyphName *ucs_to_name[MAX_GLYPHNAME*2];
81 static unsigned int hash, rehash;
84 rawindex (const FcGlyphName *gn)
88 for (i = 0; i < nraw; i++)
95 scan (FILE *f, char *filename)
97 char buf[MAX_NAMELEN];
98 char name[MAX_NAMELEN];
104 while (fgets (buf, sizeof (buf), f))
107 if (sscanf (buf, "%[^;];%lx\n", name, &ucs) != 2)
109 gn = FcAllocGlyphName ((FcChar32) ucs, (FcChar8 *) name);
111 fatal (filename, lineno, "out of memory");
113 if (len > max_name_len)
119 static int compare_string (const void *a, const void *b)
121 const char *const *as = a, *const *bs = b;
122 return strcmp (*as, *bs);
125 static int compare_glyphname (const void *a, const void *b)
127 const FcGlyphName *const *ag = a, *const *bg = b;
129 return strcmp ((char *) (*ag)->name, (char *) (*bg)->name);
164 for (t = 3; t <= l; t += 2)
171 * Find a prime pair that leaves at least 25% of the hash table empty
182 while (!isprime(h-2) || !isprime(h))
189 FcHashGlyphName (const FcChar8 *name)
194 while ((c = *name++))
196 h = ((h << 1) | (h >> 31)) ^ c;
202 insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h)
206 i = (int) (h % hash);
209 if (!r) r = (int) (h % rehash);
218 dump (FcGlyphName * const *table, const char *name)
222 printf ("static const FcGlyphName *%s[%d] = {\n", name, hash);
224 for (i = 0; i < hash; i++)
226 printf ("(FcGlyphName *) &glyph%d,\n", rawindex(table[i]));
234 main (int argc, char **argv)
236 char *files[MAX_GLYPHFILE];
244 if (i == MAX_GLYPHFILE)
245 fatal (*argv, 0, "Too many glyphname files");
246 files[i] = argv[i+1];
250 qsort (files, i, sizeof (char *), compare_string);
251 for (i = 0; files[i]; i++)
253 f = fopen (files[i], "r");
255 fatal (files[i], 0, strerror (errno));
259 qsort (raw, nraw, sizeof (FcGlyphName *), compare_glyphname);
263 for (i = 0; i < nraw; i++)
265 insert (raw[i], name_to_ucs, FcHashGlyphName (raw[i]->name));
266 insert (raw[i], ucs_to_name, raw[i]->ucs);
270 * Scan the input until the marker is found
273 while (fgets (line, sizeof (line), stdin))
275 if (!strncmp (line, "@@@", 3))
277 fputs (line, stdout);
280 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
281 nraw, hash, nraw * 100 / hash);
283 printf ("#define FC_GLYPHNAME_HASH %u\n", hash);
284 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash);
285 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len);
291 for (i = 0; i < nraw; i++)
292 printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; }"
293 " glyph%d = { 0x%lx, \"%s\" };\n",
294 (int) strlen ((char *) raw[i]->name) + 1,
295 i, (unsigned long) raw[i]->ucs, raw[i]->name);
298 * Dump out name_to_ucs table
301 dump (name_to_ucs, "name_to_ucs");
304 * Dump out ucs_to_name table
306 dump (ucs_to_name, "ucs_to_name");
308 while (fgets (line, sizeof (line), stdin))
309 fputs (line, stdout);
312 exit (ferror (stdout));