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 FcMemAlloc (int kind, int size)
46 FcMemFree (int kind, int size)
51 FcCacheObjectReference (void *object)
56 FcCacheObjectDereference (void *object)
61 FcLangNormalize (const FcChar8 *lang)
71 return (FcChar8 *) getenv ("HOME");
75 fatal (const char *file, int lineno, const char *msg)
78 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
80 fprintf (stderr, "%s: %s\n", file, msg);
85 get_line (FILE *f, char *buf, int *lineno)
93 if (!fgets (line, 1024, f))
96 hash = strchr (line, '#');
100 while (line[0] && isspace (line[0]))
103 while (end > 0 && isspace (line[end-1]))
106 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\r')
112 static char *dir = 0;
115 scanopen (char *file)
119 f = fopen (file, "r");
127 f = fopen (path, "r");
133 * build a single charset from a source file
135 * The file format is quite simple, either
136 * a single hex value or a pair separated with a dash
138 * Comments begin with '#'
142 scan (FILE *f, char *file, FcCharSetFreezer *freezer)
147 int start, end, ucs4;
152 while ((line = get_line (f, buf, &lineno)))
154 if (!strncmp (line, "include", 7))
158 included_file = strchr (line, ' ');
161 "invalid syntax, expected: include filename");
162 while (isspace(*included_file))
164 included_f = scanopen (included_file);
166 fatal (included_file, 0, "can't open");
167 n = scan (included_f, included_file, freezer);
170 c = FcCharSetCreate ();
171 if (!FcCharSetMerge (c, n, NULL))
172 fatal (file, lineno, "out of memory");
173 FcCharSetDestroy (n);
182 if (strchr (line, '-'))
184 if (sscanf (line, "%x-%x", &start, &end) != 2)
185 fatal (file, lineno, "parse error");
189 if (sscanf (line, "%x", &start) != 1)
190 fatal (file, lineno, "parse error");
194 c = FcCharSetCreate ();
195 for (ucs4 = start; ucs4 <= end; ucs4++)
197 if (!((del ? FcCharSetDelChar : FcCharSetAddChar) (c, ucs4)))
198 fatal (file, lineno, "out of memory");
201 n = (FcCharSet *) FcCharSetFreeze (freezer, c);
202 FcCharSetDestroy (c);
207 * Convert a file name into a name suitable for C declarations
210 get_name (char *file)
215 dot = strchr (file, '.');
217 dot = file + strlen(file);
218 name = malloc (dot - file + 1);
219 strncpy (name, file, dot - file);
220 name[dot-file] = '\0';
225 * Convert a C name into a language name
228 get_lang (char *name)
230 char *lang = malloc (strlen (name) + 1);
234 while ((c = *name++))
236 if (isupper ((int) (unsigned char) c))
237 c = tolower ((int) (unsigned char) c);
248 typedef struct _Entry {
253 static int compare (const void *a, const void *b)
255 const Entry *as = a, *bs = b;
256 return FcStrCmpIgnoreCase ((const FcChar8 *) as->file, (const FcChar8 *) bs->file);
259 #define MAX_LANG 1024
260 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
262 #define BitSet(map, i) ((map)[(entries[i].id)>>5] |= ((FcChar32) 1 << ((entries[i].id) & 0x1f)))
263 #define BitGet(map, i) ((map)[(entries[i].id)>>5] >> ((entries[i].id) & 0x1f)) & 1)
266 main (int argc, char **argv)
268 static Entry entries[MAX_LANG];
269 static FcCharSet *sets[MAX_LANG];
270 static int duplicate[MAX_LANG];
271 static int country[MAX_LANG];
272 static char *names[MAX_LANG];
273 static char *langs[MAX_LANG];
274 static int off[MAX_LANG];
281 int total_leaves = 0;
283 static char line[1024];
284 static FcChar32 map[MAX_LANG_SET_MAP];
285 int num_lang_set_map;
286 int setRangeStart[26];
288 FcChar8 setRangeChar;
289 FcCharSetFreezer *freezer;
291 freezer = FcCharSetFreezerCreate ();
293 fatal (argv[0], 0, "out of memory");
297 if (!strcmp (argv[argi], "-d"))
304 fatal (argv[0], 0, "Too many languages");
306 entries[i].file = argv[argi++];
310 qsort (entries, i, sizeof (Entry), compare);
312 while (entries[i].file)
314 f = scanopen (entries[i].file);
316 fatal (entries[i].file, 0, strerror (errno));
317 sets[i] = scan (f, entries[i].file, freezer);
318 names[i] = get_name (entries[i].file);
319 langs[i] = get_lang(names[i]);
320 if (strchr (langs[i], '-'))
321 country[ncountry++] = i;
323 total_leaves += sets[i]->num;
329 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
334 for (i = 0; sets[i]; i++)
336 for (sl = 0; sl < sets[i]->num; sl++)
338 for (l = 0; l < tl; l++)
339 if (leaves[l] == FcCharSetLeaf(sets[i], sl))
342 leaves[tl++] = FcCharSetLeaf(sets[i], sl);
347 * Scan the input until the marker is found
350 while (fgets (line, sizeof (line), stdin))
352 if (!strncmp (line, "@@@", 3))
354 fputs (line, stdout);
357 printf ("/* total size: %d unique leaves: %d */\n\n",
361 * Find duplicate charsets
364 for (i = 1; sets[i]; i++)
369 for (j = 0; j < i; j++)
370 if (sets[j] == sets[i])
378 for (i = 0; sets[i]; i++) {
379 if (duplicate[i] >= 0)
385 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets);
386 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl);
387 printf ("#define NUM0 (OFF0 + %d * sizeof (uintptr_t))\n", tn);
388 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
389 printf ("#define OFF(s,o) (OFF0 + o * sizeof (uintptr_t) - SET(s))\n");
390 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
391 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
392 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
393 printf ("#define fcLangCharSetIndices (fcLangData.langIndices)\n");
394 printf ("#define fcLangCharSetIndicesInv (fcLangData.langIndicesInv)\n");
397 printf ("static const struct {\n"
398 " FcLangCharSet langCharSets[%d];\n"
399 " FcCharLeaf leaves[%d];\n"
400 " uintptr_t leaf_offsets[%d];\n"
401 " FcChar16 numbers[%d];\n"
402 " FcChar%s langIndices[%d];\n"
403 " FcChar%s langIndicesInv[%d];\n"
404 "} fcLangData = {\n",
406 nsets < 256 ? "8 " : "16", nsets, nsets < 256 ? "8 " : "16", nsets);
413 for (i = 0; sets[i]; i++)
415 int j = duplicate[i];
420 printf (" { \"%s\", "
421 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
423 sets[j]->num, i, off[j], i, off[j], i);
431 for (l = 0; l < tl; l++)
433 printf (" { { /* %d */", l);
434 for (i = 0; i < 256/32; i++)
438 printf (" 0x%08x,", leaves[l]->map[i]);
440 printf ("\n } },\n");
448 for (i = 0; sets[i]; i++)
452 if (duplicate[i] >= 0)
454 printf (" /* %s */\n", names[i]);
455 for (n = 0; n < sets[i]->num; n++)
459 for (l = 0; l < tl; l++)
460 if (leaves[l] == FcCharSetLeaf(sets[i], n))
463 fatal (names[i], 0, "can't find leaf");
464 printf (" LEAF(%3d,%3d),", off[i], l);
475 for (i = 0; sets[i]; i++)
479 if (duplicate[i] >= 0)
481 printf (" /* %s */\n", names[i]);
482 for (n = 0; n < sets[i]->num; n++)
486 printf (" 0x%04x,", FcCharSetNumbers (sets[i])[n]);
497 for (i = 0; sets[i]; i++)
499 printf (" %d, /* %s */\n", entries[i].id, names[i]);
506 static int entries_inv[MAX_LANG];
507 for (i = 0; sets[i]; i++)
508 entries_inv[entries[i].id] = i;
509 for (i = 0; sets[i]; i++)
510 printf (" %d, /* %s */\n", entries_inv[i], names[entries_inv[i]]);
516 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
517 num_lang_set_map = (i + 31) / 32;
518 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
520 * Dump indices with country codes
525 int ncountry_ent = 0;
527 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
528 for (c = 0; c < ncountry; c++)
533 int lang = strchr (langs[i], '-') - langs[i];
536 for (k = 0; k < num_lang_set_map; k++)
540 for (d = c + 1; d < ncountry; d++)
543 if (j >= 0 && !strncmp (langs[j], langs[i], lang + 1))
550 for (k = 0; k < num_lang_set_map; k++)
551 printf (" 0x%08x,", map[k]);
552 printf (" }, /* %*.*s */\n",
553 lang, lang, langs[i]);
558 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
563 * Find ranges for each letter for faster searching
566 memset(setRangeStart, '\0', sizeof (setRangeStart));
567 memset(setRangeEnd, '\0', sizeof (setRangeEnd));
568 for (i = 0; sets[i]; i++)
570 char c = names[i][0];
572 while (setRangeChar <= c && c <= 'z')
573 setRangeStart[setRangeChar++ - 'a'] = i;
575 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
576 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
577 setRangeEnd[setRangeChar - 'a'] = i - 1;
580 * Dump sets start/finish for the fastpath
583 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
585 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
587 printf (" { %d, %d }, /* %c */\n",
588 setRangeStart[setRangeChar - 'a'],
589 setRangeEnd[setRangeChar - 'a'], setRangeChar);
593 while (fgets (line, sizeof (line), stdin))
594 fputs (line, stdout);
597 exit (ferror (stdout));