]> git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
Add fc-lang program to generate language coverage tables
[fontconfig.git] / fc-lang / fc-lang.c
1 /*
2 * $XFree86$
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 char *names[1024];
167 FILE *f;
168 int i = 0;
169 FcCharLeaf **leaves, **sleaves;
170 int total_leaves = 0;
171 int l, sl, tl;
172 char line[1024];
173
174 while (*++argv)
175 {
176 f = fopen (*argv, "r");
177 if (!f)
178 fatal (*argv, 0, strerror (errno));
179 sets[i] = scan (f, *argv);
180 names[i] = get_name (*argv);
181 total_leaves += sets[i]->num;
182 i++;
183 fclose (f);
184 }
185 sets[i] = 0;
186 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
187 tl = 0;
188 /*
189 * Find unique leaves
190 */
191 for (i = 0; sets[i]; i++)
192 {
193 sleaves = sets[i]->leaves;
194 for (sl = 0; sl < sets[i]->num; sl++)
195 {
196 for (l = 0; l < tl; l++)
197 if (leaves[l] == sleaves[sl])
198 break;
199 if (l == tl)
200 leaves[tl++] = sleaves[sl];
201 }
202 }
203
204 /*
205 * Scan the input until the marker is found
206 */
207
208 while (fgets (line, sizeof (line), stdin))
209 {
210 if (!strncmp (line, "@@@", 3))
211 break;
212 fputs (line, stdout);
213 }
214
215 printf ("/* total size: %d unique leaves: %d */\n\n",
216 total_leaves, tl);
217 /*
218 * Dump leaves
219 */
220 printf ("static const FcCharLeaf leaves[%d] = {\n", tl);
221 for (l = 0; l < tl; l++)
222 {
223 printf (" { { /* %d */", l);
224 for (i = 0; i < 256/32; i++)
225 {
226 if (i % 4 == 0)
227 printf ("\n ");
228 printf (" 0x%08x,", leaves[l]->map[i]);
229 }
230 printf ("\n } },\n");
231 }
232 printf ("};\n\n");
233 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
234 /*
235 * Dump arrays
236 */
237 for (i = 0; sets[i]; i++)
238 {
239 int n;
240
241 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
242 names[i], sets[i]->num);
243 for (n = 0; n < sets[i]->num; n++)
244 {
245 if (n % 8 == 0)
246 printf (" ");
247 for (l = 0; l < tl; l++)
248 if (leaves[l] == sets[i]->leaves[n])
249 break;
250 if (l == tl)
251 fatal (names[i], 0, "can't find leaf");
252 printf (" L(%3d),", l);
253 if (n % 8 == 7)
254 printf ("\n");
255 }
256 if (n % 8 != 0)
257 printf ("\n");
258 printf ("};\n\n");
259
260
261 printf ("static const FcChar16 numbers_%s[%d] = {\n",
262 names[i], sets[i]->num);
263 for (n = 0; n < sets[i]->num; n++)
264 {
265 if (n % 8 == 0)
266 printf (" ");
267 printf (" 0x%04x,", sets[i]->numbers[n]);
268 if (n % 8 == 7)
269 printf ("\n");
270 }
271 if (n % 8 != 0)
272 printf ("\n");
273 printf ("};\n\n");
274 }
275 printf ("#undef L\n\n");
276 /*
277 * Dump sets
278 */
279 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
280 for (i = 0; sets[i]; i++)
281 {
282 printf (" { (FcChar8 *) \"%s\",\n"
283 " { 1, FcTrue, %d, "
284 "(FcCharLeaf **) leaves_%s, "
285 "(FcChar16 *) numbers_%s } },\n",
286 get_lang(names[i]),
287 sets[i]->num, names[i], names[i]);
288 }
289 printf ("};\n\n");
290 while (fgets (line, sizeof (line), stdin))
291 fputs (line, stdout);
292
293 fflush (stdout);
294 exit (ferror (stdout));
295 }