Add Asturian, Old Church Slavonic, Friulian, Manx Gaelic, Cornish, Scots,
[platform/upstream/fontconfig.git] / fc-lang / fc-lang.c
1 /*
2  * $XFree86: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.1 2002/07/06 23:21:36 keithp Exp $
3  *
4  * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
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
27 /*
28  * fc-lang
29  *
30  * Read a set of language orthographies and build C declarations for
31  * charsets which can then be used to identify which languages are
32  * supported by a given font.  Note that it would be nice if
33  * this could be done while compiling the library, but this
34  * code uses a number of routines from the library.  It's
35  * expediant to just ship the pre-built version along with the
36  * source orthographies.
37  */
38
39 static void 
40 fatal (char *file, int lineno, char *msg)
41 {
42     fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
43     exit (1);
44 }
45
46 static char *
47 get_line (FILE *f, char *line, int *lineno)
48 {
49     char    *hash;
50     if (!fgets (line, 1024, f))
51         return 0;
52     ++(*lineno);
53     hash = strchr (line, '#');
54     if (hash)
55         *hash = '\0';
56     if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
57         return get_line (f, line, lineno);
58     return line;
59 }
60
61 /*
62  * build a single charset from a source file
63  *
64  * The file format is quite simple, either
65  * a single hex value or a pair separated with a dash
66  *
67  * Comments begin with '#'
68  */
69
70 static FcCharSet *
71 scan (FILE *f, char *file)
72 {
73     FcCharSet   *c = 0;
74     FcCharSet   *n;
75     int         start, end, ucs4;
76     char        line[1024];
77     int         lineno = 0;
78
79     while (get_line (f, line, &lineno))
80     {
81         if (!strncmp (line, "include", 7))
82         {
83             file = strchr (line, ' ');
84             while (*file == ' ')
85                 file++;
86             end = strlen (file);
87             if (file[end-1] == '\n')
88                 file[end-1] = '\0';
89             f = fopen (file, "r");
90             if (!f)
91                 fatal (file, 0, "can't open");
92             c = scan (f, file);
93             fclose (f);
94             return c;
95         }
96         if (strchr (line, '-'))
97         {
98             if (sscanf (line, "%x-%x", &start, &end) != 2)
99                 fatal (file, lineno, "parse error");
100         }
101         else
102         {
103             if (sscanf (line, "%x", &start) != 1)
104                 fatal (file, lineno, "parse error");
105             end = start;
106         }
107         if (!c)
108             c = FcCharSetCreate ();
109         for (ucs4 = start; ucs4 <= end; ucs4++)
110         {
111             if (!FcCharSetAddChar (c, ucs4))
112                 fatal (file, lineno, "out of memory");
113         }
114     }
115     n = FcCharSetFreeze (c);
116     FcCharSetDestroy (c);
117     return n;
118 }
119
120 /*
121  * Convert a file name into a name suitable for C declarations
122  */
123 static char *
124 get_name (char *file)
125 {
126     char    *name;
127     char    *dot;
128
129     dot = strchr (file, '.');
130     if (!dot)
131         dot = file + strlen(file);
132     name = malloc (dot - file + 1);
133     strncpy (name, file, dot - file);
134     name[dot-file] = '\0';
135     return name;
136 }
137
138 /*
139  * Convert a C name into a language name
140  */
141 static char *
142 get_lang (char *name)
143 {
144     char    *lang = malloc (strlen (name) + 1);
145     char    *l = lang;
146     char    c;
147
148     while ((c = *name++))
149     {
150         if (isupper (c))
151             c = tolower (c);
152         if (c == '_')
153             c = '-';
154         if (c == ' ')
155             continue;
156         *l++ = c;
157     }
158     *l++ = '\0';
159     return lang;
160 }
161
162 int
163 main (int argc, char **argv)
164 {
165     FcCharSet   *sets[1024];
166     int         duplicate[1024];
167     char        *names[1024];
168     FILE        *f;
169     int         i = 0;
170     FcCharLeaf  **leaves, **sleaves;
171     int         total_leaves = 0;
172     int         l, sl, tl;
173     char        line[1024];
174     
175     while (*++argv)
176     {
177         f = fopen (*argv, "r");
178         if (!f)
179             fatal (*argv, 0, strerror (errno));
180         sets[i] = scan (f, *argv);
181         names[i] = get_name (*argv);
182         total_leaves += sets[i]->num;
183         i++;
184         fclose (f);
185     }
186     sets[i] = 0;
187     leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
188     tl = 0;
189     /*
190      * Find unique leaves
191      */
192     for (i = 0; sets[i]; i++)
193     {
194         sleaves = sets[i]->leaves;
195         for (sl = 0; sl < sets[i]->num; sl++)
196         {
197             for (l = 0; l < tl; l++)
198                 if (leaves[l] == sleaves[sl])
199                     break;
200             if (l == tl)
201                 leaves[tl++] = sleaves[sl];
202         }
203     }
204
205     /*
206      * Scan the input until the marker is found
207      */
208     
209     while (fgets (line, sizeof (line), stdin))
210     {
211         if (!strncmp (line, "@@@", 3))
212             break;
213         fputs (line, stdout);
214     }
215     
216     printf ("/* total size: %d unique leaves: %d */\n\n",
217             total_leaves, tl);
218     /*
219      * Dump leaves
220      */
221     printf ("static const FcCharLeaf    leaves[%d] = {\n", tl);
222     for (l = 0; l < tl; l++)
223     {
224         printf ("    { { /* %d */", l);
225         for (i = 0; i < 256/32; i++)
226         {
227             if (i % 4 == 0)
228                 printf ("\n   ");
229             printf (" 0x%08x,", leaves[l]->map[i]);
230         }
231         printf ("\n    } },\n");
232     }
233     printf ("};\n\n");
234     printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
235
236     /*
237      * Find duplicate charsets
238      */
239     duplicate[0] = -1;
240     for (i = 1; sets[i]; i++)
241     {
242         int j;
243
244         duplicate[i] = -1;
245         for (j = 0; j < i; j++)
246             if (sets[j] == sets[i])
247             {
248                 duplicate[i] = j;
249                 break;
250             }
251     }
252
253     /*
254      * Dump arrays
255      */
256     for (i = 0; sets[i]; i++)
257     {
258         int n;
259         
260         if (duplicate[i] >= 0)
261             continue;
262         printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
263                 names[i], sets[i]->num);
264         for (n = 0; n < sets[i]->num; n++)
265         {
266             if (n % 8 == 0)
267                 printf ("   ");
268             for (l = 0; l < tl; l++)
269                 if (leaves[l] == sets[i]->leaves[n])
270                     break;
271             if (l == tl)
272                 fatal (names[i], 0, "can't find leaf");
273             printf (" L(%3d),", l);
274             if (n % 8 == 7)
275                 printf ("\n");
276         }
277         if (n % 8 != 0)
278             printf ("\n");
279         printf ("};\n\n");
280         
281
282         printf ("static const FcChar16 numbers_%s[%d] = {\n",
283                 names[i], sets[i]->num);
284         for (n = 0; n < sets[i]->num; n++)
285         {
286             if (n % 8 == 0)
287                 printf ("   ");
288             printf (" 0x%04x,", sets[i]->numbers[n]);
289             if (n % 8 == 7)
290                 printf ("\n");
291         }
292         if (n % 8 != 0)
293             printf ("\n");
294         printf ("};\n\n");
295     }
296     printf ("#undef L\n\n");
297     /*
298      * Dump sets
299      */
300     printf ("static const FcLangCharSet  fcLangCharSets[] = {\n");
301     for (i = 0; sets[i]; i++)
302     {
303         int     j = duplicate[i];
304         if (j < 0)
305             j = i;
306         printf ("    { (FcChar8 *) \"%s\",\n"
307                 "      { 1, FcTrue, %d, "
308                 "(FcCharLeaf **) leaves_%s, "
309                 "(FcChar16 *) numbers_%s } },\n",
310                 get_lang(names[i]),
311                 sets[j]->num, names[j], names[j]);
312     }
313     printf ("};\n\n");
314     while (fgets (line, sizeof (line), stdin))
315         fputs (line, stdout);
316     
317     fflush (stdout);
318     exit (ferror (stdout));
319 }