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