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