]> git.wh0rd.org - fontconfig.git/blame - fc-lang/fc-lang.c
Fix intel compiler warnings: make many variables static, eliminate
[fontconfig.git] / fc-lang / fc-lang.c
CommitLineData
c1382a3d 1/*
0eadb052 2 * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
c1382a3d 3 *
46b51147 4 * Copyright © 2002 Keith Packard
c1382a3d
KP
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"
c647f6f1
KP
26#include "fccharset.c"
27#include "fcstr.c"
c1382a3d
KP
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
c647f6f1
KP
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
c1382a3d
KP
38 */
39
a81f23c0
PL
40const FcChar16 langBankNumbers[1]; /* place holders so that externs resolve */
41const FcCharLeaf langBankLeaves[1];
42const int langBankLeafIdx[1];
82f35f8b 43
c647f6f1
KP
44void
45FcMemAlloc (int kind, int size)
46{
47}
48
49void
50FcMemFree (int kind, int size)
51{
52}
53
b8948e85
PL
54int* _fcBankId = 0;
55int* _fcBankIdx = 0;
56
212c9f43 57int
b8948e85 58FcCacheBankToIndexMTF (int bank)
212c9f43
PL
59{
60 return -1;
61}
62
ff3f1f98
KP
63FcChar8 *
64FcConfigHome (void)
65{
8245771d 66 return (FcChar8 *) getenv ("HOME");
ff3f1f98
KP
67}
68
c1382a3d 69static void
67accef4 70fatal (const char *file, int lineno, const char *msg)
c1382a3d 71{
67accef4
PL
72 if (lineno)
73 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
74 else
c7beacf9 75 fprintf (stderr, "%s: %s\n", file, msg);
c1382a3d
KP
76 exit (1);
77}
78
79static char *
80get_line (FILE *f, char *line, int *lineno)
81{
82 char *hash;
cf5cf4ca 83 int end;
c1382a3d
KP
84 if (!fgets (line, 1024, f))
85 return 0;
86 ++(*lineno);
87 hash = strchr (line, '#');
88 if (hash)
89 *hash = '\0';
cf5cf4ca
PL
90
91 end = strlen (line);
92 while (end > 0 && isspace (line[end-1]))
93 line[--end] = '\0';
94
c1382a3d
KP
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
0d745819 100static char *dir = 0;
394b2bf0 101
6ae6acf3 102static FILE *
394b2bf0
KP
103scanopen (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
c1382a3d
KP
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
129static FcCharSet *
130scan (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, ' ');
cf5cf4ca 143 while (isspace(*file))
c1382a3d 144 file++;
394b2bf0 145 f = scanopen (file);
c1382a3d
KP
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 */
179static char *
180get_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 */
197static char *
198get_lang (char *name)
199{
200 char *lang = malloc (strlen (name) + 1);
201 char *l = lang;
202 char c;
203
204 while ((c = *name++))
205 {
996580dc
KP
206 if (isupper ((int) (unsigned char) c))
207 c = tolower ((int) (unsigned char) c);
c1382a3d
KP
208 if (c == '_')
209 c = '-';
210 if (c == ' ')
211 continue;
212 *l++ = c;
213 }
214 *l++ = '\0';
215 return lang;
216}
217
d8d73958
KP
218static 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
234397b4
DD
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
c1382a3d
KP
230int
231main (int argc, char **argv)
232{
69a3fc78
PL
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];
c1382a3d 240 FILE *f;
a151aced 241 int offset = 0;
234397b4 242 int ncountry = 0;
c1382a3d 243 int i = 0;
67accef4 244 int argi;
cd2ec1a9 245 FcCharLeaf **leaves;
c1382a3d 246 int total_leaves = 0;
a151aced 247 int offset_count = 0;
c1382a3d 248 int l, sl, tl;
69a3fc78
PL
249 static char line[1024];
250 static FcChar32 map[MAX_LANG_SET_MAP];
234397b4 251 int num_lang_set_map;
0eadb052
KP
252 int setRangeStart[26];
253 int setRangeEnd[26];
254 FcChar8 setRangeChar;
c1382a3d 255
67accef4
PL
256 argi = 1;
257 while (argv[argi])
234397b4 258 {
67accef4 259 if (!strcmp (argv[argi], "-d"))
394b2bf0 260 {
67accef4
PL
261 argi++;
262 dir = argv[argi++];
394b2bf0
KP
263 continue;
264 }
234397b4 265 if (i == MAX_LANG)
67accef4
PL
266 fatal (argv[0], 0, "Too many languages");
267 files[i++] = argv[argi++];
234397b4 268 }
d8d73958
KP
269 files[i] = 0;
270 qsort (files, i, sizeof (char *), compare);
271 i = 0;
272 while (files[i])
c1382a3d 273 {
394b2bf0 274 f = scanopen (files[i]);
c1382a3d 275 if (!f)
d8d73958
KP
276 fatal (files[i], 0, strerror (errno));
277 sets[i] = scan (f, files[i]);
278 names[i] = get_name (files[i]);
234397b4
DD
279 langs[i] = get_lang(names[i]);
280 if (strchr (langs[i], '-'))
281 country[ncountry++] = i;
282
c1382a3d
KP
283 total_leaves += sets[i]->num;
284 i++;
285 fclose (f);
286 }
287 sets[i] = 0;
288 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
289 tl = 0;
290 /*
291 * Find unique leaves
292 */
293 for (i = 0; sets[i]; i++)
294 {
c1382a3d
KP
295 for (sl = 0; sl < sets[i]->num; sl++)
296 {
297 for (l = 0; l < tl; l++)
cd2ec1a9 298 if (leaves[l] == FcCharSetGetLeaf(sets[i], sl))
c1382a3d
KP
299 break;
300 if (l == tl)
cd2ec1a9 301 leaves[tl++] = FcCharSetGetLeaf(sets[i], sl);
c1382a3d
KP
302 }
303 }
304
305 /*
306 * Scan the input until the marker is found
307 */
308
309 while (fgets (line, sizeof (line), stdin))
310 {
311 if (!strncmp (line, "@@@", 3))
312 break;
313 fputs (line, stdout);
314 }
315
316 printf ("/* total size: %d unique leaves: %d */\n\n",
317 total_leaves, tl);
318 /*
319 * Dump leaves
320 */
82f35f8b 321 printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl);
c1382a3d
KP
322 for (l = 0; l < tl; l++)
323 {
324 printf (" { { /* %d */", l);
325 for (i = 0; i < 256/32; i++)
326 {
327 if (i % 4 == 0)
328 printf ("\n ");
329 printf (" 0x%08x,", leaves[l]->map[i]);
330 }
331 printf ("\n } },\n");
332 }
333 printf ("};\n\n");
2903c146
KP
334
335 /*
336 * Find duplicate charsets
337 */
338 duplicate[0] = -1;
339 for (i = 1; sets[i]; i++)
340 {
341 int j;
342
343 duplicate[i] = -1;
344 for (j = 0; j < i; j++)
345 if (sets[j] == sets[i])
346 {
347 duplicate[i] = j;
348 break;
349 }
350 }
351
0eadb052
KP
352 /*
353 * Find ranges for each letter for faster searching
354 */
355 setRangeChar = 'a';
356 for (i = 0; sets[i]; i++)
357 {
358 char c = names[i][0];
359
360 while (setRangeChar <= c && c <= 'z')
361 setRangeStart[setRangeChar++ - 'a'] = i;
362 }
363 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
364 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
365 setRangeEnd[setRangeChar - 'a'] = i - 1;
366
c1382a3d
KP
367 /*
368 * Dump arrays
369 */
370 for (i = 0; sets[i]; i++)
371 {
372 int n;
373
2903c146
KP
374 if (duplicate[i] >= 0)
375 continue;
82f35f8b
PL
376
377 for (n = 0; n < sets[i]->num; n++)
378 {
379 for (l = 0; l < tl; l++)
380 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
381 break;
382 if (l == tl)
383 fatal (names[i], 0, "can't find leaf");
a151aced 384 offset_count++;
82f35f8b 385 }
a151aced
PL
386 offsets[i] = offset;
387 offset += sets[i]->num;
82f35f8b
PL
388 }
389
390 printf ("const int langBankLeafIdx[%d] = {\n",
a151aced 391 offset_count);
82f35f8b
PL
392 for (i = 0; sets[i]; i++)
393 {
394 int n;
395
396 if (duplicate[i] >= 0)
397 continue;
c1382a3d
KP
398 for (n = 0; n < sets[i]->num; n++)
399 {
400 if (n % 8 == 0)
401 printf (" ");
402 for (l = 0; l < tl; l++)
cd2ec1a9 403 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
c1382a3d
KP
404 break;
405 if (l == tl)
406 fatal (names[i], 0, "can't find leaf");
82f35f8b 407 printf (" %3d,", l);
c1382a3d
KP
408 if (n % 8 == 7)
409 printf ("\n");
410 }
411 if (n % 8 != 0)
412 printf ("\n");
82f35f8b
PL
413 }
414 printf ("};\n\n");
c1382a3d 415
82f35f8b 416 printf ("const FcChar16 langBankNumbers[%d] = {\n",
a151aced 417 offset_count);
82f35f8b
PL
418
419 for (i = 0; sets[i]; i++)
420 {
421 int n;
a151aced
PL
422
423 if (duplicate[i] >= 0)
424 continue;
c1382a3d
KP
425 for (n = 0; n < sets[i]->num; n++)
426 {
427 if (n % 8 == 0)
428 printf (" ");
cd2ec1a9 429 printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]);
c1382a3d
KP
430 if (n % 8 == 7)
431 printf ("\n");
432 }
433 if (n % 8 != 0)
434 printf ("\n");
c1382a3d 435 }
82f35f8b 436 printf ("};\n\n");
0eadb052 437
c1382a3d
KP
438 /*
439 * Dump sets
440 */
0eadb052 441
82f35f8b 442 printf ("const FcLangCharSet fcLangCharSets[] = {\n");
c1382a3d
KP
443 for (i = 0; sets[i]; i++)
444 {
2903c146 445 int j = duplicate[i];
0eadb052 446
2903c146
KP
447 if (j < 0)
448 j = i;
82f35f8b 449
c1382a3d 450 printf (" { (FcChar8 *) \"%s\",\n"
82f35f8b 451 " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
a151aced 452 "{ .stat = { %d, %d } } } }, /* %d */\n",
234397b4 453 langs[i],
a151aced 454 sets[j]->num, offsets[j], offsets[j], j);
c1382a3d
KP
455 }
456 printf ("};\n\n");
234397b4
DD
457 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
458 num_lang_set_map = (i + 31) / 32;
459 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
460 /*
461 * Dump indices with country codes
462 */
463 if (ncountry)
464 {
0d745819 465 int c;
234397b4
DD
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 {
0d745819 474 int lang = strchr (langs[i], '-') - langs[i];
234397b4
DD
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",
0d745819 494 lang, lang, langs[i]);
234397b4
DD
495 ++ncountry_ent;
496 }
497 }
498 printf ("};\n\n");
499 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
500 }
501
0eadb052
KP
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
c1382a3d
KP
515 while (fgets (line, sizeof (line), stdin))
516 fputs (line, stdout);
517
518 fflush (stdout);
519 exit (ferror (stdout));
520}