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"
28 #include "fcserialize.c"
33 * Read a set of language orthographies and build C declarations for
34 * charsets which can then be used to identify which languages are
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
42 FcMemAlloc (int kind, int size)
47 FcMemFree (int kind, int size)
56 return (FcChar8 *) getenv ("HOME");
60 fatal (const char *file, int lineno, const char *msg)
63 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
65 fprintf (stderr, "%s: %s\n", file, msg);
70 get_line (FILE *f, char *line, int *lineno)
74 if (!fgets (line, 1024, f))
77 hash = strchr (line, '#');
82 while (end > 0 && isspace (line[end-1]))
85 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
86 return get_line (f, line, lineno);
97 f = fopen (file, "r");
105 f = fopen (path, "r");
111 * build a single charset from a source file
113 * The file format is quite simple, either
114 * a single hex value or a pair separated with a dash
116 * Comments begin with '#'
119 static const FcCharSet *
120 scan (FILE *f, char *file, FcCharSetFreezer *freezer)
124 int start, end, ucs4;
128 while (get_line (f, line, &lineno))
130 if (!strncmp (line, "include", 7))
132 file = strchr (line, ' ');
135 "invalid syntax, expected: include filename");
136 while (isspace(*file))
140 fatal (file, 0, "can't open");
141 n = scan (f, file, freezer);
145 if (strchr (line, '-'))
147 if (sscanf (line, "%x-%x", &start, &end) != 2)
148 fatal (file, lineno, "parse error");
152 if (sscanf (line, "%x", &start) != 1)
153 fatal (file, lineno, "parse error");
157 c = FcCharSetCreate ();
158 for (ucs4 = start; ucs4 <= end; ucs4++)
160 if (!FcCharSetAddChar (c, ucs4))
161 fatal (file, lineno, "out of memory");
164 n = FcCharSetFreeze (freezer, c);
165 FcCharSetDestroy (c);
170 * Convert a file name into a name suitable for C declarations
173 get_name (char *file)
178 dot = strchr (file, '.');
180 dot = file + strlen(file);
181 name = malloc (dot - file + 1);
182 strncpy (name, file, dot - file);
183 name[dot-file] = '\0';
188 * Convert a C name into a language name
191 get_lang (char *name)
193 char *lang = malloc (strlen (name) + 1);
197 while ((c = *name++))
199 if (isupper ((int) (unsigned char) c))
200 c = tolower ((int) (unsigned char) c);
211 static int compare (const void *a, const void *b)
213 const FcChar8 *const *as = a, *const *bs = b;
214 return FcStrCmpIgnoreCase (*as, *bs);
217 #define MAX_LANG 1024
218 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
220 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
221 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
224 main (int argc, char **argv)
226 static char *files[MAX_LANG];
227 static const FcCharSet *sets[MAX_LANG];
228 static int duplicate[MAX_LANG];
229 static int country[MAX_LANG];
230 static char *names[MAX_LANG];
231 static char *langs[MAX_LANG];
232 static int off[MAX_LANG];
239 int total_leaves = 0;
241 static char line[1024];
242 static FcChar32 map[MAX_LANG_SET_MAP];
243 int num_lang_set_map;
244 int setRangeStart[26];
246 FcChar8 setRangeChar;
247 FcCharSetFreezer *freezer;
249 freezer = FcCharSetFreezerCreate ();
251 fatal (argv[0], 0, "out of memory");
255 if (!strcmp (argv[argi], "-d"))
262 fatal (argv[0], 0, "Too many languages");
263 files[i++] = argv[argi++];
266 qsort (files, i, sizeof (char *), compare);
270 f = scanopen (files[i]);
272 fatal (files[i], 0, strerror (errno));
273 sets[i] = scan (f, files[i], freezer);
274 names[i] = get_name (files[i]);
275 langs[i] = get_lang(names[i]);
276 if (strchr (langs[i], '-'))
277 country[ncountry++] = i;
279 total_leaves += sets[i]->num;
285 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
290 for (i = 0; sets[i]; i++)
292 for (sl = 0; sl < sets[i]->num; sl++)
294 for (l = 0; l < tl; l++)
295 if (leaves[l] == FcCharSetLeaf(sets[i], sl))
298 leaves[tl++] = FcCharSetLeaf(sets[i], sl);
303 * Scan the input until the marker is found
306 while (fgets (line, sizeof (line), stdin))
308 if (!strncmp (line, "@@@", 3))
310 fputs (line, stdout);
313 printf ("/* total size: %d unique leaves: %d */\n\n",
317 * Find duplicate charsets
320 for (i = 1; sets[i]; i++)
325 for (j = 0; j < i; j++)
326 if (sets[j] == sets[i])
334 for (i = 0; sets[i]; i++) {
335 if (duplicate[i] >= 0)
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");
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",
364 for (i = 0; sets[i]; i++)
366 int j = duplicate[i];
371 printf (" { (FcChar8 *) \"%s\", "
372 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
374 sets[j]->num, i, off[j], i, off[j], i);
382 for (l = 0; l < tl; l++)
384 printf (" { { /* %d */", l);
385 for (i = 0; i < 256/32; i++)
389 printf (" 0x%08x,", leaves[l]->map[i]);
391 printf ("\n } },\n");
399 for (i = 0; sets[i]; i++)
403 if (duplicate[i] >= 0)
405 printf (" /* %s */\n", names[i]);
406 for (n = 0; n < sets[i]->num; n++)
410 for (l = 0; l < tl; l++)
411 if (leaves[l] == FcCharSetLeaf(sets[i], n))
414 fatal (names[i], 0, "can't find leaf");
415 printf (" LEAF(%3d,%3d),", off[i], l);
426 for (i = 0; sets[i]; i++)
430 if (duplicate[i] >= 0)
432 printf (" /* %s */\n", names[i]);
433 for (n = 0; n < sets[i]->num; n++)
437 printf (" 0x%04x,", FcCharSetNumbers (sets[i])[n]);
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);
452 * Dump indices with country codes
457 int ncountry_ent = 0;
459 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
460 for (c = 0; c < ncountry; c++)
465 int lang = strchr (langs[i], '-') - langs[i];
468 for (k = 0; k < num_lang_set_map; k++)
472 for (d = c + 1; d < ncountry; d++)
475 if (j >= 0 && !strncmp (langs[j], langs[i], l))
482 for (k = 0; k < num_lang_set_map; k++)
483 printf (" 0x%08x,", map[k]);
484 printf (" }, /* %*.*s */\n",
485 lang, lang, langs[i]);
490 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
495 * Find ranges for each letter for faster searching
498 memset(setRangeStart, '\0', sizeof (setRangeStart));
499 memset(setRangeEnd, '\0', sizeof (setRangeEnd));
500 for (i = 0; sets[i]; i++)
502 char c = names[i][0];
504 while (setRangeChar <= c && c <= 'z')
505 setRangeStart[setRangeChar++ - 'a'] = i;
507 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
508 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
509 setRangeEnd[setRangeChar - 'a'] = i - 1;
512 * Dump sets start/finish for the fastpath
514 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
515 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
517 printf (" { %d, %d }, /* %c */\n",
518 setRangeStart[setRangeChar - 'a'],
519 setRangeEnd[setRangeChar - 'a'], setRangeChar);
523 while (fgets (line, sizeof (line), stdin))
524 fputs (line, stdout);
527 exit (ferror (stdout));