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