2 * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
4 * Copyright © 2002 Keith Packard
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.
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.
26 #include "fccharset.c"
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
40 const FcChar16 *langBankNumbers = 0;
41 const FcCharLeaf *langBankLeaves = 0;
42 const int *langBankLeafIdx = 0;
45 FcMemAlloc (int kind, int size)
50 FcMemFree (int kind, int size)
55 FcCacheBankToIndex (int bank)
63 return (FcChar8 *) getenv ("HOME");
67 fatal (const char *file, int lineno, const char *msg)
70 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
72 fprintf (stderr, "%s: %s\n", file, msg);
77 get_line (FILE *f, char *line, int *lineno)
80 if (!fgets (line, 1024, f))
83 hash = strchr (line, '#');
86 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
87 return get_line (f, line, lineno);
98 f = fopen (file, "r");
106 f = fopen (path, "r");
112 * build a single charset from a source file
114 * The file format is quite simple, either
115 * a single hex value or a pair separated with a dash
117 * Comments begin with '#'
121 scan (FILE *f, char *file)
125 int start, end, ucs4;
129 while (get_line (f, line, &lineno))
131 if (!strncmp (line, "include", 7))
133 file = strchr (line, ' ');
137 if (file[end-1] == '\n')
141 fatal (file, 0, "can't open");
146 if (strchr (line, '-'))
148 if (sscanf (line, "%x-%x", &start, &end) != 2)
149 fatal (file, lineno, "parse error");
153 if (sscanf (line, "%x", &start) != 1)
154 fatal (file, lineno, "parse error");
158 c = FcCharSetCreate ();
159 for (ucs4 = start; ucs4 <= end; ucs4++)
161 if (!FcCharSetAddChar (c, ucs4))
162 fatal (file, lineno, "out of memory");
165 n = FcCharSetFreeze (c);
166 FcCharSetDestroy (c);
171 * Convert a file name into a name suitable for C declarations
174 get_name (char *file)
179 dot = strchr (file, '.');
181 dot = file + strlen(file);
182 name = malloc (dot - file + 1);
183 strncpy (name, file, dot - file);
184 name[dot-file] = '\0';
189 * Convert a C name into a language name
192 get_lang (char *name)
194 char *lang = malloc (strlen (name) + 1);
198 while ((c = *name++))
200 if (isupper ((int) (unsigned char) c))
201 c = tolower ((int) (unsigned char) c);
212 static int compare (const void *a, const void *b)
214 const FcChar8 *const *as = a, *const *bs = b;
215 return FcStrCmpIgnoreCase (*as, *bs);
218 #define MAX_LANG 1024
219 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
221 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
222 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
225 main (int argc, char **argv)
227 char *files[MAX_LANG];
228 FcCharSet *sets[MAX_LANG];
229 int duplicate[MAX_LANG];
230 int offsets[MAX_LANG];
231 int country[MAX_LANG];
232 char *names[MAX_LANG];
233 char *langs[MAX_LANG];
240 int total_leaves = 0;
241 int offset_count = 0;
245 FcChar32 map[MAX_LANG_SET_MAP];
246 int num_lang_set_map;
247 int setRangeStart[26];
249 FcChar8 setRangeChar;
254 if (!strcmp (argv[argi], "-d"))
261 fatal (argv[0], 0, "Too many languages");
262 files[i++] = argv[argi++];
265 qsort (files, i, sizeof (char *), compare);
269 f = scanopen (files[i]);
271 fatal (files[i], 0, strerror (errno));
272 sets[i] = scan (f, files[i]);
273 names[i] = get_name (files[i]);
274 langs[i] = get_lang(names[i]);
275 if (strchr (langs[i], '-'))
276 country[ncountry++] = i;
278 total_leaves += sets[i]->num;
283 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
288 for (i = 0; sets[i]; i++)
290 for (sl = 0; sl < sets[i]->num; sl++)
292 for (l = 0; l < tl; l++)
293 if (leaves[l] == FcCharSetGetLeaf(sets[i], sl))
296 leaves[tl++] = FcCharSetGetLeaf(sets[i], sl);
301 * Scan the input until the marker is found
304 while (fgets (line, sizeof (line), stdin))
306 if (!strncmp (line, "@@@", 3))
308 fputs (line, stdout);
311 printf ("/* total size: %d unique leaves: %d */\n\n",
316 printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl);
317 for (l = 0; l < tl; l++)
319 printf (" { { /* %d */", l);
320 for (i = 0; i < 256/32; i++)
324 printf (" 0x%08x,", leaves[l]->map[i]);
326 printf ("\n } },\n");
331 * Find duplicate charsets
334 for (i = 1; sets[i]; i++)
339 for (j = 0; j < i; j++)
340 if (sets[j] == sets[i])
348 * Find ranges for each letter for faster searching
351 for (i = 0; sets[i]; i++)
353 char c = names[i][0];
355 while (setRangeChar <= c && c <= 'z')
356 setRangeStart[setRangeChar++ - 'a'] = i;
358 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
359 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
360 setRangeEnd[setRangeChar - 'a'] = i - 1;
365 for (i = 0; sets[i]; i++)
369 if (duplicate[i] >= 0)
372 for (n = 0; n < sets[i]->num; n++)
374 for (l = 0; l < tl; l++)
375 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
378 fatal (names[i], 0, "can't find leaf");
382 offset += sets[i]->num;
385 printf ("const int langBankLeafIdx[%d] = {\n",
387 for (i = 0; sets[i]; i++)
391 if (duplicate[i] >= 0)
393 for (n = 0; n < sets[i]->num; n++)
397 for (l = 0; l < tl; l++)
398 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
401 fatal (names[i], 0, "can't find leaf");
411 printf ("const FcChar16 langBankNumbers[%d] = {\n",
414 for (i = 0; sets[i]; i++)
418 if (duplicate[i] >= 0)
420 for (n = 0; n < sets[i]->num; n++)
424 printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]);
437 printf ("const FcLangCharSet fcLangCharSets[] = {\n");
438 for (i = 0; sets[i]; i++)
440 int j = duplicate[i];
445 printf (" { (FcChar8 *) \"%s\",\n"
446 " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
447 "{ .stat = { %d, %d } } } }, /* %d */\n",
449 sets[j]->num, offsets[j], offsets[j], j);
452 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
453 num_lang_set_map = (i + 31) / 32;
454 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
456 * Dump indices with country codes
460 int ncountry_ent = 0;
462 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
463 for (c = 0; c < ncountry; c++)
468 int l = strchr (langs[i], '-') - langs[i];
471 for (k = 0; k < num_lang_set_map; k++)
475 for (d = c + 1; d < ncountry; d++)
478 if (j >= 0 && !strncmp (langs[j], langs[i], l))
485 for (k = 0; k < num_lang_set_map; k++)
486 printf (" 0x%08x,", map[k]);
487 printf (" }, /* %*.*s */\n",
493 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
498 * Dump sets start/finish for the fastpath
500 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
501 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
503 printf (" { %d, %d }, /* %c */\n",
504 setRangeStart[setRangeChar - 'a'],
505 setRangeEnd[setRangeChar - 'a'], setRangeChar);
509 while (fgets (line, sizeof (line), stdin))
510 fputs (line, stdout);
513 exit (ferror (stdout));