2 * fontconfig/fc-lang/fc-lang.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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.
25 #include "fccharset.c"
27 #include "fcserialize.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
41 FcCacheObjectReference (void *object FC_UNUSED)
46 FcCacheObjectDereference (void *object FC_UNUSED)
51 FcLangNormalize (const FcChar8 *lang FC_UNUSED)
61 return (FcChar8 *) getenv ("HOME");
65 fatal (const char *file, int lineno, const char *msg)
68 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
70 fprintf (stderr, "%s: %s\n", file, msg);
75 get_line (FILE *f, char *buf, int *lineno)
83 if (!fgets (line, 1024, f))
86 hash = strchr (line, '#');
90 while (line[0] && isspace (line[0]))
93 while (end > 0 && isspace (line[end-1]))
96 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\r')
102 static char *dir = 0;
105 scanopen (char *file)
109 f = fopen (file, "r");
117 f = fopen (path, "r");
123 * build a single charset from a source file
125 * The file format is quite simple, either
126 * a single hex value or a pair separated with a dash
128 * Comments begin with '#'
132 scan (FILE *f, char *file, FcCharSetFreezer *freezer)
137 int start, end, ucs4;
142 while ((line = get_line (f, buf, &lineno)))
144 if (!strncmp (line, "include", 7))
148 included_file = strchr (line, ' ');
151 "invalid syntax, expected: include filename");
152 while (isspace(*included_file))
154 included_f = scanopen (included_file);
156 fatal (included_file, 0, "can't open");
157 n = scan (included_f, included_file, freezer);
160 c = FcCharSetCreate ();
161 if (!FcCharSetMerge (c, n, NULL))
162 fatal (file, lineno, "out of memory");
163 FcCharSetDestroy (n);
172 if (strchr (line, '-'))
174 if (sscanf (line, "%x-%x", &start, &end) != 2)
175 fatal (file, lineno, "parse error");
179 if (sscanf (line, "%x", &start) != 1)
180 fatal (file, lineno, "parse error");
184 c = FcCharSetCreate ();
185 for (ucs4 = start; ucs4 <= end; ucs4++)
187 if (!((del ? FcCharSetDelChar : FcCharSetAddChar) (c, ucs4)))
188 fatal (file, lineno, "out of memory");
191 n = (FcCharSet *) FcCharSetFreeze (freezer, c);
192 FcCharSetDestroy (c);
197 * Convert a file name into a name suitable for C declarations
200 get_name (char *file)
205 dot = strchr (file, '.');
207 dot = file + strlen(file);
208 name = malloc (dot - file + 1);
209 strncpy (name, file, dot - file);
210 name[dot-file] = '\0';
215 * Convert a C name into a language name
218 get_lang (char *name)
220 char *lang = malloc (strlen (name) + 1);
224 while ((c = *name++))
226 if (isupper ((int) (unsigned char) c))
227 c = tolower ((int) (unsigned char) c);
238 typedef struct _Entry {
243 static int compare (const void *a, const void *b)
245 const Entry *as = a, *bs = b;
246 return FcStrCmpIgnoreCase ((const FcChar8 *) as->file, (const FcChar8 *) bs->file);
249 #define MAX_LANG 1024
250 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
252 #define BitSet(map, i) ((map)[(entries[i].id)>>5] |= ((FcChar32) 1 << ((entries[i].id) & 0x1f)))
255 main (int argc FC_UNUSED, char **argv)
257 static Entry entries[MAX_LANG + 1];
258 static FcCharSet *sets[MAX_LANG];
259 static int duplicate[MAX_LANG];
260 static int country[MAX_LANG];
261 static char *names[MAX_LANG];
262 static char *langs[MAX_LANG];
263 static int off[MAX_LANG];
270 int total_leaves = 0;
272 static char line[1024];
273 static FcChar32 map[MAX_LANG_SET_MAP];
274 int num_lang_set_map;
275 int setRangeStart[26];
277 FcChar8 setRangeChar;
278 FcCharSetFreezer *freezer;
280 freezer = FcCharSetFreezerCreate ();
282 fatal (argv[0], 0, "out of memory");
286 if (!strcmp (argv[argi], "-d"))
293 fatal (argv[0], 0, "Too many languages");
295 entries[i].file = argv[argi++];
299 qsort (entries, i, sizeof (Entry), compare);
301 while (entries[i].file)
303 f = scanopen (entries[i].file);
305 fatal (entries[i].file, 0, strerror (errno));
306 sets[i] = scan (f, entries[i].file, freezer);
307 names[i] = get_name (entries[i].file);
308 langs[i] = get_lang(names[i]);
309 if (strchr (langs[i], '-'))
310 country[ncountry++] = i;
312 total_leaves += sets[i]->num;
318 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
323 for (i = 0; sets[i]; i++)
325 for (sl = 0; sl < sets[i]->num; sl++)
327 for (l = 0; l < tl; l++)
328 if (leaves[l] == FcCharSetLeaf(sets[i], sl))
331 leaves[tl++] = FcCharSetLeaf(sets[i], sl);
336 * Scan the input until the marker is found
339 while (fgets (line, sizeof (line), stdin))
341 if (!strncmp (line, "@@@", 3))
343 fputs (line, stdout);
346 printf ("/* total size: %d unique leaves: %d */\n\n",
350 * Find duplicate charsets
353 for (i = 1; sets[i]; i++)
358 for (j = 0; j < i; j++)
359 if (sets[j] == sets[i])
367 for (i = 0; sets[i]; i++) {
368 if (duplicate[i] >= 0)
374 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets);
375 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl);
376 printf ("#define NUM0 (OFF0 + %d * sizeof (uintptr_t))\n", tn);
377 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
378 printf ("#define OFF(s,o) (OFF0 + o * sizeof (uintptr_t) - SET(s))\n");
379 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
380 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
381 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
382 printf ("#define fcLangCharSetIndices (fcLangData.langIndices)\n");
383 printf ("#define fcLangCharSetIndicesInv (fcLangData.langIndicesInv)\n");
386 printf ("static const struct {\n"
387 " FcLangCharSet langCharSets[%d];\n"
388 " FcCharLeaf leaves[%d];\n"
389 " uintptr_t leaf_offsets[%d];\n"
390 " FcChar16 numbers[%d];\n"
391 " FcChar%s langIndices[%d];\n"
392 " FcChar%s langIndicesInv[%d];\n"
393 "} fcLangData = {\n",
395 nsets < 256 ? "8 " : "16", nsets, nsets < 256 ? "8 " : "16", nsets);
402 for (i = 0; sets[i]; i++)
404 int j = duplicate[i];
409 printf (" { \"%s\", "
410 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
412 sets[j]->num, i, off[j], i, off[j], i);
420 for (l = 0; l < tl; l++)
422 printf (" { { /* %d */", l);
423 for (i = 0; i < 256/32; i++)
427 printf (" 0x%08x,", leaves[l]->map[i]);
429 printf ("\n } },\n");
437 for (i = 0; sets[i]; i++)
441 if (duplicate[i] >= 0)
443 printf (" /* %s */\n", names[i]);
444 for (n = 0; n < sets[i]->num; n++)
448 for (l = 0; l < tl; l++)
449 if (leaves[l] == FcCharSetLeaf(sets[i], n))
452 fatal (names[i], 0, "can't find leaf");
453 printf (" LEAF(%3d,%3d),", off[i], l);
464 for (i = 0; sets[i]; i++)
468 if (duplicate[i] >= 0)
470 printf (" /* %s */\n", names[i]);
471 for (n = 0; n < sets[i]->num; n++)
475 printf (" 0x%04x,", FcCharSetNumbers (sets[i])[n]);
486 for (i = 0; sets[i]; i++)
488 printf (" %d, /* %s */\n", entries[i].id, names[i]);
495 static int entries_inv[MAX_LANG];
496 for (i = 0; sets[i]; i++)
497 entries_inv[entries[i].id] = i;
498 for (i = 0; sets[i]; i++)
499 printf (" %d, /* %s */\n", entries_inv[i], names[entries_inv[i]]);
505 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
506 num_lang_set_map = (i + 31) / 32;
507 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
509 * Dump indices with country codes
514 int ncountry_ent = 0;
516 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
517 for (c = 0; c < ncountry; c++)
522 int lang = strchr (langs[i], '-') - langs[i];
525 for (k = 0; k < num_lang_set_map; k++)
529 for (d = c + 1; d < ncountry; d++)
532 if (j >= 0 && !strncmp (langs[j], langs[i], lang + 1))
539 for (k = 0; k < num_lang_set_map; k++)
540 printf (" 0x%08x,", map[k]);
541 printf (" }, /* %*.*s */\n",
542 lang, lang, langs[i]);
547 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
552 * Find ranges for each letter for faster searching
555 memset(setRangeStart, '\0', sizeof (setRangeStart));
556 memset(setRangeEnd, '\0', sizeof (setRangeEnd));
557 for (i = 0; sets[i]; i++)
559 char c = names[i][0];
561 while (setRangeChar <= c && c <= 'z')
562 setRangeStart[setRangeChar++ - 'a'] = i;
564 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
565 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
566 setRangeEnd[setRangeChar - 'a'] = i - 1;
569 * Dump sets start/finish for the fastpath
572 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
574 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
576 printf (" { %d, %d }, /* %c */\n",
577 setRangeStart[setRangeChar - 'a'],
578 setRangeEnd[setRangeChar - 'a'], setRangeChar);
582 while (fgets (line, sizeof (line), stdin))
583 fputs (line, stdout);
586 exit (ferror (stdout));