2 * $XFree86: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.2 2002/07/07 19:18:51 keithp Exp $
4 * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
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.
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.
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.
40 fatal (char *file, int lineno, char *msg)
42 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
47 get_line (FILE *f, char *line, int *lineno)
50 if (!fgets (line, 1024, f))
53 hash = strchr (line, '#');
56 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
57 return get_line (f, line, lineno);
62 * build a single charset from a source file
64 * The file format is quite simple, either
65 * a single hex value or a pair separated with a dash
67 * Comments begin with '#'
71 scan (FILE *f, char *file)
79 while (get_line (f, line, &lineno))
81 if (!strncmp (line, "include", 7))
83 file = strchr (line, ' ');
87 if (file[end-1] == '\n')
89 f = fopen (file, "r");
91 fatal (file, 0, "can't open");
96 if (strchr (line, '-'))
98 if (sscanf (line, "%x-%x", &start, &end) != 2)
99 fatal (file, lineno, "parse error");
103 if (sscanf (line, "%x", &start) != 1)
104 fatal (file, lineno, "parse error");
108 c = FcCharSetCreate ();
109 for (ucs4 = start; ucs4 <= end; ucs4++)
111 if (!FcCharSetAddChar (c, ucs4))
112 fatal (file, lineno, "out of memory");
115 n = FcCharSetFreeze (c);
116 FcCharSetDestroy (c);
121 * Convert a file name into a name suitable for C declarations
124 get_name (char *file)
129 dot = strchr (file, '.');
131 dot = file + strlen(file);
132 name = malloc (dot - file + 1);
133 strncpy (name, file, dot - file);
134 name[dot-file] = '\0';
139 * Convert a C name into a language name
142 get_lang (char *name)
144 char *lang = malloc (strlen (name) + 1);
148 while ((c = *name++))
162 static int compare (const void *a, const void *b)
164 const FcChar8 *const *as = a, *const *bs = b;
165 return FcStrCmpIgnoreCase (*as, *bs);
169 main (int argc, char **argv)
172 FcCharSet *sets[1024];
177 FcCharLeaf **leaves, **sleaves;
178 int total_leaves = 0;
185 qsort (files, i, sizeof (char *), compare);
189 f = fopen (files[i], "r");
191 fatal (files[i], 0, strerror (errno));
192 sets[i] = scan (f, files[i]);
193 names[i] = get_name (files[i]);
194 total_leaves += sets[i]->num;
199 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
204 for (i = 0; sets[i]; i++)
206 sleaves = sets[i]->leaves;
207 for (sl = 0; sl < sets[i]->num; sl++)
209 for (l = 0; l < tl; l++)
210 if (leaves[l] == sleaves[sl])
213 leaves[tl++] = sleaves[sl];
218 * Scan the input until the marker is found
221 while (fgets (line, sizeof (line), stdin))
223 if (!strncmp (line, "@@@", 3))
225 fputs (line, stdout);
228 printf ("/* total size: %d unique leaves: %d */\n\n",
233 printf ("static const FcCharLeaf leaves[%d] = {\n", tl);
234 for (l = 0; l < tl; l++)
236 printf (" { { /* %d */", l);
237 for (i = 0; i < 256/32; i++)
241 printf (" 0x%08x,", leaves[l]->map[i]);
243 printf ("\n } },\n");
246 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
249 * Find duplicate charsets
252 for (i = 1; sets[i]; i++)
257 for (j = 0; j < i; j++)
258 if (sets[j] == sets[i])
268 for (i = 0; sets[i]; i++)
272 if (duplicate[i] >= 0)
274 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
275 names[i], sets[i]->num);
276 for (n = 0; n < sets[i]->num; n++)
280 for (l = 0; l < tl; l++)
281 if (leaves[l] == sets[i]->leaves[n])
284 fatal (names[i], 0, "can't find leaf");
285 printf (" L(%3d),", l);
294 printf ("static const FcChar16 numbers_%s[%d] = {\n",
295 names[i], sets[i]->num);
296 for (n = 0; n < sets[i]->num; n++)
300 printf (" 0x%04x,", sets[i]->numbers[n]);
308 printf ("#undef L\n\n");
312 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
313 for (i = 0; sets[i]; i++)
315 int j = duplicate[i];
318 printf (" { (FcChar8 *) \"%s\",\n"
319 " { FC_REF_CONSTANT, %d, "
320 "(FcCharLeaf **) leaves_%s, "
321 "(FcChar16 *) numbers_%s } },\n",
323 sets[j]->num, names[j], names[j]);
326 while (fgets (line, sizeof (line), stdin))
327 fputs (line, stdout);
330 exit (ferror (stdout));