]> git.wh0rd.org - fontconfig.git/blame - fc-glyphname/fc-glyphname.c
[fc-glyphname] Rename internal arrays to prefix with _fc_
[fontconfig.git] / fc-glyphname / fc-glyphname.c
CommitLineData
721d496d 1/*
e690fbb2 2 * fontconfig/fc-glyphname/fc-glyphname.c
721d496d 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 *
3074a73b 16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
721d496d 17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
721d496d
KP
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 {
fa741cd4 209 if (!r) r = (int) (h % rehash + 1);
721d496d
KP
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 219{
96925b99 220 int i;
721d496d 221
96925b99 222 printf ("static const FcGlyphId %s[%d] = {\n", name, hash);
721d496d
KP
223
224 for (i = 0; i < hash; i++)
225 if (table[i])
96925b99 226 printf (" %d,\n", rawindex(table[i]));
721d496d 227 else
96925b99 228 printf (" -1,\n");
721d496d
KP
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;
96925b99 240 char *type;
721d496d
KP
241
242 i = 0;
67accef4 243 while (argv[i+1])
721d496d
KP
244 {
245 if (i == MAX_GLYPHFILE)
246 fatal (*argv, 0, "Too many glyphname files");
67accef4
PL
247 files[i] = argv[i+1];
248 i++;
721d496d
KP
249 }
250 files[i] = 0;
251 qsort (files, i, sizeof (char *), compare_string);
252 for (i = 0; files[i]; i++)
253 {
254 f = fopen (files[i], "r");
255 if (!f)
256 fatal (files[i], 0, strerror (errno));
257 scan (f, files[i]);
258 fclose (f);
259 }
260 qsort (raw, nraw, sizeof (FcGlyphName *), compare_glyphname);
261
262 find_hash ();
263
264 for (i = 0; i < nraw; i++)
265 {
266 insert (raw[i], name_to_ucs, FcHashGlyphName (raw[i]->name));
267 insert (raw[i], ucs_to_name, raw[i]->ucs);
268 }
269
270 /*
271 * Scan the input until the marker is found
272 */
273
274 while (fgets (line, sizeof (line), stdin))
275 {
276 if (!strncmp (line, "@@@", 3))
277 break;
278 fputs (line, stdout);
279 }
280
281 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
282 nraw, hash, nraw * 100 / hash);
283
284 printf ("#define FC_GLYPHNAME_HASH %u\n", hash);
285 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash);
286 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len);
96925b99
KP
287 if (nraw < 128)
288 type = "int8_t";
289 else if (nraw < 32768)
290 type = "int16_t";
291 else
292 type = "int32_t";
293
294 printf ("typedef %s FcGlyphId;\n\n", type);
721d496d
KP
295
296 /*
297 * Dump out entries
298 */
299
19292722 300 printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; } _fc_glyph_names[%d] = {\n",
96925b99
KP
301 max_name_len + 1, nraw);
302
721d496d 303 for (i = 0; i < nraw; i++)
96925b99
KP
304 printf (" { 0x%lx, \"%s\" },\n",
305 (unsigned long) raw[i]->ucs, raw[i]->name);
306
307 printf ("};\n");
721d496d
KP
308
309 /*
310 * Dump out name_to_ucs table
311 */
312
19292722 313 dump (name_to_ucs, "_fc_name_to_ucs");
721d496d
KP
314
315 /*
316 * Dump out ucs_to_name table
317 */
19292722 318 dump (ucs_to_name, "_fc_ucs_to_name");
721d496d
KP
319
320 while (fgets (line, sizeof (line), stdin))
321 fputs (line, stdout);
322
323 fflush (stdout);
324 exit (ferror (stdout));
325}