Rework cache files to use offsets for all data structures.
[platform/upstream/fontconfig.git] / fc-lang / fc-lang.c
1 /*
2  * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
3  *
4  * Copyright © 2002 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 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 #include "fccharset.c"
27 #include "fcstr.c"
28
29 /*
30  * fc-lang
31  *
32  * Read a set of language orthographies and build C declarations for
33  * charsets which can then be used to identify which languages are
34  * supported by a given font.  Note that this uses some utilities
35  * from the fontconfig library, so the necessary file is simply
36  * included in this compilation.  A couple of extra utility
37  * functions are also needed in slightly modified form
38  */
39
40 void
41 FcMemAlloc (int kind, int size)
42 {
43 }
44
45 void
46 FcMemFree (int kind, int size)
47 {
48 }
49
50 FcChar8 *
51 FcConfigHome (void)
52 {
53     return (FcChar8 *) getenv ("HOME");
54 }
55
56 static void 
57 fatal (const char *file, int lineno, const char *msg)
58 {
59     if (lineno)
60         fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
61     else
62         fprintf (stderr, "%s: %s\n", file, msg);
63     exit (1);
64 }
65
66 static char *
67 get_line (FILE *f, char *line, int *lineno)
68 {
69     char    *hash;
70     int     end;
71     if (!fgets (line, 1024, f))
72         return 0;
73     ++(*lineno);
74     hash = strchr (line, '#');
75     if (hash)
76         *hash = '\0';
77
78     end = strlen (line);
79     while (end > 0 && isspace (line[end-1]))
80       line[--end] = '\0';
81
82     if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
83         return get_line (f, line, lineno);
84     return line;
85 }
86
87 static char     *dir = 0;
88
89 static FILE *
90 scanopen (char *file)
91 {
92     FILE    *f;
93
94     f = fopen (file, "r");
95     if (!f && dir)
96     {
97         char    path[1024];
98         
99         strcpy (path, dir);
100         strcat (path, "/");
101         strcat (path, file);
102         f = fopen (path, "r");
103     }
104     return f;
105 }
106
107 /*
108  * build a single charset from a source file
109  *
110  * The file format is quite simple, either
111  * a single hex value or a pair separated with a dash
112  *
113  * Comments begin with '#'
114  */
115
116 static FcCharSet *
117 scan (FILE *f, char *file)
118 {
119     FcCharSet   *c = 0;
120     FcCharSet   *n;
121     int         start, end, ucs4;
122     char        line[1024];
123     int         lineno = 0;
124
125     while (get_line (f, line, &lineno))
126     {
127         if (!strncmp (line, "include", 7))
128         {
129             file = strchr (line, ' ');
130             if (!file)
131                 fatal (line, lineno, 
132                        "invalid syntax, expected: include filename");
133             while (isspace(*file))
134                 file++;
135             f = scanopen (file);
136             if (!f)
137                 fatal (file, 0, "can't open");
138             c = scan (f, file);
139             fclose (f);
140             return c;
141         }
142         if (strchr (line, '-'))
143         {
144             if (sscanf (line, "%x-%x", &start, &end) != 2)
145                 fatal (file, lineno, "parse error");
146         }
147         else
148         {
149             if (sscanf (line, "%x", &start) != 1)
150                 fatal (file, lineno, "parse error");
151             end = start;
152         }
153         if (!c)
154             c = FcCharSetCreate ();
155         for (ucs4 = start; ucs4 <= end; ucs4++)
156         {
157             if (!FcCharSetAddChar (c, ucs4))
158                 fatal (file, lineno, "out of memory");
159         }
160     }
161     n = FcCharSetFreeze (c);
162     FcCharSetDestroy (c);
163     return n;
164 }
165
166 /*
167  * Convert a file name into a name suitable for C declarations
168  */
169 static char *
170 get_name (char *file)
171 {
172     char    *name;
173     char    *dot;
174
175     dot = strchr (file, '.');
176     if (!dot)
177         dot = file + strlen(file);
178     name = malloc (dot - file + 1);
179     strncpy (name, file, dot - file);
180     name[dot-file] = '\0';
181     return name;
182 }
183
184 /*
185  * Convert a C name into a language name
186  */
187 static char *
188 get_lang (char *name)
189 {
190     char    *lang = malloc (strlen (name) + 1);
191     char    *l = lang;
192     char    c;
193
194     while ((c = *name++))
195     {
196         if (isupper ((int) (unsigned char) c))
197             c = tolower ((int) (unsigned char) c);
198         if (c == '_')
199             c = '-';
200         if (c == ' ')
201             continue;
202         *l++ = c;
203     }
204     *l++ = '\0';
205     return lang;
206 }
207
208 static int compare (const void *a, const void *b)
209 {
210     const FcChar8    *const *as = a, *const *bs = b;
211     return FcStrCmpIgnoreCase (*as, *bs);
212 }
213
214 #define MAX_LANG            1024
215 #define MAX_LANG_SET_MAP    ((MAX_LANG + 31) / 32)
216
217 #define BitSet(map, id)   ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
218 #define BitGet(map, id)   ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
219
220 int
221 main (int argc, char **argv)
222 {
223     static char         *files[MAX_LANG];
224     static FcCharSet    *sets[MAX_LANG];
225     static int          duplicate[MAX_LANG];
226     static int          country[MAX_LANG];
227     static char         *names[MAX_LANG];
228     static char         *langs[MAX_LANG];
229     static int          off[MAX_LANG];
230     FILE        *f;
231     int         ncountry = 0;
232     int         i = 0;
233     int         nsets = 0;
234     int         argi;
235     FcCharLeaf  **leaves;
236     int         total_leaves = 0;
237     int         l, sl, tl, tn;
238     static char         line[1024];
239     static FcChar32     map[MAX_LANG_SET_MAP];
240     int         num_lang_set_map;
241     int         setRangeStart[26];
242     int         setRangeEnd[26];
243     FcChar8     setRangeChar;
244     
245     argi = 1;
246     while (argv[argi])
247     {
248         if (!strcmp (argv[argi], "-d"))
249         {
250             argi++;
251             dir = argv[argi++];
252             continue;
253         }
254         if (i == MAX_LANG)
255             fatal (argv[0], 0, "Too many languages");
256         files[i++] = argv[argi++];
257     }
258     files[i] = 0;
259     qsort (files, i, sizeof (char *), compare);
260     i = 0;
261     while (files[i])
262     {
263         f = scanopen (files[i]);
264         if (!f)
265             fatal (files[i], 0, strerror (errno));
266         sets[i] = scan (f, files[i]);
267         names[i] = get_name (files[i]);
268         langs[i] = get_lang(names[i]);
269         if (strchr (langs[i], '-'))
270             country[ncountry++] = i;
271
272         total_leaves += sets[i]->num;
273         i++;
274         fclose (f);
275     }
276     nsets = i;
277     sets[i] = 0;
278     leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
279     tl = 0;
280     /*
281      * Find unique leaves
282      */
283     for (i = 0; sets[i]; i++)
284     {
285         for (sl = 0; sl < sets[i]->num; sl++)
286         {
287             for (l = 0; l < tl; l++)
288                 if (leaves[l] == FcCharSetLeaf(sets[i], sl))
289                     break;
290             if (l == tl)
291                 leaves[tl++] = FcCharSetLeaf(sets[i], sl);
292         }
293     }
294
295     /*
296      * Scan the input until the marker is found
297      */
298     
299     while (fgets (line, sizeof (line), stdin))
300     {
301         if (!strncmp (line, "@@@", 3))
302             break;
303         fputs (line, stdout);
304     }
305     
306     printf ("/* total size: %d unique leaves: %d */\n\n",
307             total_leaves, tl);
308
309     /*
310      * Find duplicate charsets
311      */
312     duplicate[0] = -1;
313     for (i = 1; sets[i]; i++)
314     {
315         int j;
316
317         duplicate[i] = -1;
318         for (j = 0; j < i; j++)
319             if (sets[j] == sets[i])
320             {
321                 duplicate[i] = j;
322                 break;
323             }
324     }
325
326     tn = 0;
327     for (i = 0; sets[i]; i++) {
328         if (duplicate[i] >= 0)
329             continue;
330         off[i] = tn;
331         tn += sets[i]->num;
332     }
333
334     printf ("#define LEAF0       (%d * sizeof (FcLangCharSet))\n", nsets);
335     printf ("#define OFF0        (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl);
336     printf ("#define NUM0        (OFF0 + %d * sizeof (intptr_t))\n", tn);
337     printf ("#define SET(n)      (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
338     printf ("#define OFF(s,o)    (OFF0 + o * sizeof (intptr_t) - SET(s))\n");
339     printf ("#define NUM(s,n)    (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
340     printf ("#define LEAF(o,l)   (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
341     printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
342     printf ("\n");
343     
344     printf ("static const struct {\n"
345             "    FcLangCharSet  langCharSets[%d];\n"
346             "    FcCharLeaf     leaves[%d];\n"
347             "    intptr_t       leaf_offsets[%d];\n"
348             "    FcChar16       numbers[%d];\n"
349             "} fcLangData = {\n",
350             nsets, tl, tn, tn);
351         
352     /*
353      * Dump sets
354      */
355
356     printf ("{\n");
357     for (i = 0; sets[i]; i++)
358     {
359         int     j = duplicate[i];
360
361         if (j < 0)
362             j = i;
363
364         printf ("    { (FcChar8 *) \"%s\", "
365                 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
366                 langs[i],
367                 sets[j]->num, i, off[j], i, off[j], i);
368     }
369     printf ("},\n");
370     
371     /*
372      * Dump leaves
373      */
374     printf ("{\n");
375     for (l = 0; l < tl; l++)
376     {
377         printf ("    { { /* %d */", l);
378         for (i = 0; i < 256/32; i++)
379         {
380             if (i % 4 == 0)
381                 printf ("\n   ");
382             printf (" 0x%08x,", leaves[l]->map[i]);
383         }
384         printf ("\n    } },\n");
385     }
386     printf ("},\n");
387
388     /*
389      * Dump leaves
390      */
391     printf ("{\n");
392     for (i = 0; sets[i]; i++)
393     {
394         int n;
395         
396         if (duplicate[i] >= 0)
397             continue;
398         printf ("    /* %s */\n", names[i]);
399         for (n = 0; n < sets[i]->num; n++)
400         {
401             if (n % 4 == 0)
402                 printf ("   ");
403             for (l = 0; l < tl; l++)
404                 if (leaves[l] == FcCharSetLeaf(sets[i], n))
405                     break;
406             if (l == tl)
407                 fatal (names[i], 0, "can't find leaf");
408             printf (" LEAF(%3d,%3d),", off[i], l);
409             if (n % 4 == 3)
410                 printf ("\n");
411         }
412         if (n % 4 != 0)
413             printf ("\n");
414     }
415     printf ("},\n");
416         
417
418     printf ("{\n");
419     for (i = 0; sets[i]; i++)
420     {
421         int n;
422         
423         if (duplicate[i] >= 0)
424             continue;
425         printf ("    /* %s */\n", names[i]);
426         for (n = 0; n < sets[i]->num; n++)
427         {
428             if (n % 8 == 0)
429                 printf ("   ");
430             printf (" 0x%04x,", FcCharSetNumbers (sets[i])[n]);
431             if (n % 8 == 7)
432                 printf ("\n");
433         }
434         if (n % 8 != 0)
435             printf ("\n");
436     }
437     printf ("}\n");
438     
439     printf ("};\n\n");
440     
441     printf ("#define NUM_LANG_CHAR_SET  %d\n", i);
442     num_lang_set_map = (i + 31) / 32;
443     printf ("#define NUM_LANG_SET_MAP   %d\n", num_lang_set_map);
444     /*
445      * Dump indices with country codes
446      */
447     if (ncountry)
448     {
449         int     c;
450         int     ncountry_ent = 0;
451         printf ("\n");
452         printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
453         for (c = 0; c < ncountry; c++)
454         {
455             i = country[c];
456             if (i >= 0)
457             {
458                 int lang = strchr (langs[i], '-') - langs[i];
459                 int d, k;
460
461                 for (k = 0; k < num_lang_set_map; k++)
462                     map[k] = 0;
463
464                 BitSet (map, i);
465                 for (d = c + 1; d < ncountry; d++)
466                 {
467                     int j = country[d];
468                     if (j >= 0 && !strncmp (langs[j], langs[i], l))
469                     {
470                         BitSet(map, j);
471                         country[d] = -1;
472                     }
473                 }
474                 printf ("    {");
475                 for (k = 0; k < num_lang_set_map; k++)
476                     printf (" 0x%08x,", map[k]);
477                 printf (" }, /* %*.*s */\n",
478                         lang, lang, langs[i]);
479                 ++ncountry_ent;
480             }
481         }
482         printf ("};\n\n");
483         printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
484     }
485     
486
487     /*
488      * Find ranges for each letter for faster searching
489      */
490     setRangeChar = 'a';
491     memset(setRangeStart, '\0', sizeof (setRangeStart));
492     memset(setRangeEnd, '\0', sizeof (setRangeEnd));
493     for (i = 0; sets[i]; i++)
494     {
495         char    c = names[i][0];
496         
497         while (setRangeChar <= c && c <= 'z')
498             setRangeStart[setRangeChar++ - 'a'] = i;
499     }
500     for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
501         setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
502     setRangeEnd[setRangeChar - 'a'] = i - 1;
503     
504     /*
505      * Dump sets start/finish for the fastpath
506      */
507     printf ("static const FcLangCharSetRange  fcLangCharSetRanges[] = {\n");
508     for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
509     {
510         printf ("    { %d, %d }, /* %c */\n",
511                 setRangeStart[setRangeChar - 'a'],
512                 setRangeEnd[setRangeChar - 'a'], setRangeChar);
513     }
514     printf ("};\n\n");
515  
516     while (fgets (line, sizeof (line), stdin))
517         fputs (line, stdout);
518     
519     fflush (stdout);
520     exit (ferror (stdout));
521 }