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