840278164302482d43a0bc0694e229c4a190bd2c
[platform/upstream/fontconfig.git] / fc-glyphname / fc-glyphname.c
1 /*
2  * fontconfig/fc-glyphname/fc-glyphname.c
3  *
4  * Copyright © 2003 Keith Packard
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 the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make 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  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) 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
27 static int
28 rawindex (const FcGlyphName *gn);
29
30 static void
31 scan (FILE *f, char *filename);
32
33 static int
34 isprime (int i);
35
36 static void
37 find_hash (void);
38
39 static FcChar32
40 FcHashGlyphName (const FcChar8 *name);
41
42 static void
43 insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h);
44
45 static void
46 dump (FcGlyphName * const *table, const char *name);
47
48 static FcGlyphName *
49 FcAllocGlyphName (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
61 static void
62 fatal (const char *file, int lineno, const char *msg)
63 {
64     if (lineno)
65         fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
66     else
67         fprintf (stderr, "%s: %s\n", file, msg);
68
69     exit (1);
70 }
71
72 #define MAX_GLYPHFILE       256
73 #define MAX_GLYPHNAME       10240
74 #define MAX_NAMELEN         1024
75
76 static FcGlyphName *raw[MAX_GLYPHNAME];
77 static int          nraw;
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;
82
83 static int
84 rawindex (const FcGlyphName *gn)
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
94 static void
95 scan (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");
112         len = strlen (name);
113         if (len > max_name_len)
114             max_name_len = len;
115         raw[nraw++] = gn;
116     }
117 }
118
119 static 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
125 static 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
132 static int
133 isqrt (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
150 static int
151 isprime (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
174 static void
175 find_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
188 static FcChar32
189 FcHashGlyphName (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
201 static void
202 insert (FcGlyphName *gn, FcGlyphName **table, FcChar32 h)
203 {
204     unsigned int        i, r = 0;
205
206     i = (int) (h % hash);
207     while (table[i])
208     {
209         if (!r) r = (h % rehash + 1);
210         i += r;
211         if (i >= hash)
212             i -= hash;
213     }
214     table[i] = gn;
215 }
216
217 static void
218 dump (FcGlyphName * const *table, const char *name)
219 {
220     unsigned int            i;
221
222     printf ("static const FcGlyphId %s[%d] = {\n", name, hash);
223
224     for (i = 0; i < hash; i++)
225         if (table[i])
226             printf ("    %d,\n", rawindex(table[i]));
227         else
228             printf ("    -1,\n");
229
230     printf ("};\n");
231 }
232
233 int
234 main (int argc FC_UNUSED, char **argv)
235 {
236     char        *files[MAX_GLYPHFILE];
237     char        line[1024];
238     FILE        *f;
239     int         i;
240     const char  *type;
241
242     i = 0;
243     while (argv[i+1])
244     {
245         if (i == MAX_GLYPHFILE)
246             fatal (*argv, 0, "Too many glyphname files");
247         files[i] = argv[i+1];
248         i++;
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);
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);
295
296     /*
297      * Dump out entries
298      */
299
300     printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; } _fc_glyph_names[%d] = {\n",
301             max_name_len + 1, nraw);
302
303     for (i = 0; i < nraw; i++)
304         printf ("    { 0x%lx, \"%s\" },\n",
305                 (unsigned long) raw[i]->ucs, raw[i]->name);
306
307     printf ("};\n");
308
309     /*
310      * Dump out name_to_ucs table
311      */
312
313     dump (name_to_ucs, "_fc_name_to_ucs");
314
315     /*
316      * Dump out ucs_to_name table
317      */
318     dump (ucs_to_name, "_fc_ucs_to_name");
319
320     while (fgets (line, sizeof (line), stdin))
321         fputs (line, stdout);
322
323     fflush (stdout);
324     exit (ferror (stdout));
325 }