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