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)
65 return (FcChar8 *) getenv ("HOME");
69 fatal (const char *file, int lineno, const char *msg)
72 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
74 fprintf (stderr, "%s: %s\n", file, msg);
79 get_line (FILE *f, char *buf, int *lineno)
87 if (!fgets (line, 1024, f))
90 hash = strchr (line, '#');
94 while (line[0] && isspace (line[0]))
97 while (end > 0 && isspace (line[end-1]))
100 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\r')
106 static char *dir = 0;
109 scanopen (char *file)
113 f = fopen (file, "r");
121 f = fopen (path, "r");
127 * build a single charset from a source file
129 * The file format is quite simple, either
130 * a single hex value or a pair separated with a dash
132 * Comments begin with '#'
136 scan (FILE *f, char *file, FcCharSetFreezer *freezer)
141 int start, end, ucs4;
146 while ((line = get_line (f, buf, &lineno)))
148 if (!strncmp (line, "include", 7))
152 included_file = strchr (line, ' ');
155 "invalid syntax, expected: include filename");
156 while (isspace(*included_file))
158 included_f = scanopen (included_file);
160 fatal (included_file, 0, "can't open");
161 n = scan (included_f, included_file, freezer);
164 c = FcCharSetCreate ();
165 if (!FcCharSetMerge (c, n, NULL))
166 fatal (file, lineno, "out of memory");
167 FcCharSetDestroy (n);
176 if (strchr (line, '-'))
178 if (sscanf (line, "%x-%x", &start, &end) != 2)
179 fatal (file, lineno, "parse error");
183 if (sscanf (line, "%x", &start) != 1)
184 fatal (file, lineno, "parse error");
188 c = FcCharSetCreate ();
189 for (ucs4 = start; ucs4 <= end; ucs4++)
191 if (!((del ? FcCharSetDelChar : FcCharSetAddChar) (c, ucs4)))
192 fatal (file, lineno, "out of memory");
195 n = FcCharSetFreeze (freezer, c);
196 FcCharSetDestroy (c);
201 * Convert a file name into a name suitable for C declarations
204 get_name (char *file)
209 dot = strchr (file, '.');
211 dot = file + strlen(file);
212 name = malloc (dot - file + 1);
213 strncpy (name, file, dot - file);
214 name[dot-file] = '\0';
219 * Convert a C name into a language name
222 get_lang (char *name)
224 char *lang = malloc (strlen (name) + 1);
228 while ((c = *name++))
230 if (isupper ((int) (unsigned char) c))
231 c = tolower ((int) (unsigned char) c);
242 typedef struct _Entry {
247 static int compare (const void *a, const void *b)
249 const Entry *as = a, *bs = b;
250 return FcStrCmpIgnoreCase ((const FcChar8 *) as->file, (const FcChar8 *) bs->file);
253 #define MAX_LANG 1024
254 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
256 #define BitSet(map, i) ((map)[(entries[i].id)>>5] |= ((FcChar32) 1 << ((entries[i].id) & 0x1f)))
257 #define BitGet(map, i) ((map)[(entries[i].id)>>5] >> ((entries[i].id) & 0x1f)) & 1)
260 main (int argc, char **argv)
262 static Entry entries[MAX_LANG];
263 static FcCharSet *sets[MAX_LANG];
264 static int duplicate[MAX_LANG];
265 static int country[MAX_LANG];
266 static char *names[MAX_LANG];
267 static char *langs[MAX_LANG];
268 static int off[MAX_LANG];
275 int total_leaves = 0;
277 static char line[1024];
278 static FcChar32 map[MAX_LANG_SET_MAP];
279 int num_lang_set_map;
280 int setRangeStart[26];
282 FcChar8 setRangeChar;
283 FcCharSetFreezer *freezer;
285 freezer = FcCharSetFreezerCreate ();
287 fatal (argv[0], 0, "out of memory");
291 if (!strcmp (argv[argi], "-d"))
298 fatal (argv[0], 0, "Too many languages");
300 entries[i].file = argv[argi++];
304 qsort (entries, i, sizeof (Entry), compare);
306 while (entries[i].file)
308 f = scanopen (entries[i].file);
310 fatal (entries[i].file, 0, strerror (errno));
311 sets[i] = scan (f, entries[i].file, freezer);
312 names[i] = get_name (entries[i].file);
313 langs[i] = get_lang(names[i]);
314 if (strchr (langs[i], '-'))
315 country[ncountry++] = i;
317 total_leaves += sets[i]->num;
323 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
328 for (i = 0; sets[i]; i++)
330 for (sl = 0; sl < sets[i]->num; sl++)
332 for (l = 0; l < tl; l++)
333 if (leaves[l] == FcCharSetLeaf(sets[i], sl))
336 leaves[tl++] = FcCharSetLeaf(sets[i], sl);
341 * Scan the input until the marker is found
344 while (fgets (line, sizeof (line), stdin))
346 if (!strncmp (line, "@@@", 3))
348 fputs (line, stdout);
351 printf ("/* total size: %d unique leaves: %d */\n\n",
355 * Find duplicate charsets
358 for (i = 1; sets[i]; i++)
363 for (j = 0; j < i; j++)
364 if (sets[j] == sets[i])
372 for (i = 0; sets[i]; i++) {
373 if (duplicate[i] >= 0)
379 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets);
380 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl);
381 printf ("#define NUM0 (OFF0 + %d * sizeof (uintptr_t))\n", tn);
382 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
383 printf ("#define OFF(s,o) (OFF0 + o * sizeof (uintptr_t) - SET(s))\n");
384 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
385 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
386 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
387 printf ("#define fcLangCharSetIndices (fcLangData.langIndices)\n");
388 printf ("#define fcLangCharSetIndicesInv (fcLangData.langIndicesInv)\n");
391 printf ("static const struct {\n"
392 " FcLangCharSet langCharSets[%d];\n"
393 " FcCharLeaf leaves[%d];\n"
394 " uintptr_t leaf_offsets[%d];\n"
395 " FcChar16 numbers[%d];\n"
396 " FcChar%s langIndices[%d];\n"
397 " FcChar%s langIndicesInv[%d];\n"
398 "} fcLangData = {\n",
400 nsets < 256 ? "8 " : "16", nsets, nsets < 256 ? "8 " : "16", nsets);
407 for (i = 0; sets[i]; i++)
409 int j = duplicate[i];
414 printf (" { \"%s\", "
415 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
417 sets[j]->num, i, off[j], i, off[j], i);
425 for (l = 0; l < tl; l++)
427 printf (" { { /* %d */", l);
428 for (i = 0; i < 256/32; i++)
432 printf (" 0x%08x,", leaves[l]->map[i]);
434 printf ("\n } },\n");
442 for (i = 0; sets[i]; i++)
446 if (duplicate[i] >= 0)
448 printf (" /* %s */\n", names[i]);
449 for (n = 0; n < sets[i]->num; n++)
453 for (l = 0; l < tl; l++)
454 if (leaves[l] == FcCharSetLeaf(sets[i], n))
457 fatal (names[i], 0, "can't find leaf");
458 printf (" LEAF(%3d,%3d),", off[i], l);
469 for (i = 0; sets[i]; i++)
473 if (duplicate[i] >= 0)
475 printf (" /* %s */\n", names[i]);
476 for (n = 0; n < sets[i]->num; n++)
480 printf (" 0x%04x,", FcCharSetNumbers (sets[i])[n]);
491 for (i = 0; sets[i]; i++)
493 printf (" %d, /* %s */\n", entries[i].id, names[i]);
500 static int entries_inv[MAX_LANG];
501 for (i = 0; sets[i]; i++)
502 entries_inv[entries[i].id] = i;
503 for (i = 0; sets[i]; i++)
504 printf (" %d, /* %s */\n", entries_inv[i], names[entries_inv[i]]);
510 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
511 num_lang_set_map = (i + 31) / 32;
512 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
514 * Dump indices with country codes
519 int ncountry_ent = 0;
521 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
522 for (c = 0; c < ncountry; c++)
527 int lang = strchr (langs[i], '-') - langs[i];
530 for (k = 0; k < num_lang_set_map; k++)
534 for (d = c + 1; d < ncountry; d++)
537 if (j >= 0 && !strncmp (langs[j], langs[i], lang + 1))
544 for (k = 0; k < num_lang_set_map; k++)
545 printf (" 0x%08x,", map[k]);
546 printf (" }, /* %*.*s */\n",
547 lang, lang, langs[i]);
552 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
557 * Find ranges for each letter for faster searching
560 memset(setRangeStart, '\0', sizeof (setRangeStart));
561 memset(setRangeEnd, '\0', sizeof (setRangeEnd));
562 for (i = 0; sets[i]; i++)
564 char c = names[i][0];
566 while (setRangeChar <= c && c <= 'z')
567 setRangeStart[setRangeChar++ - 'a'] = i;
569 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
570 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
571 setRangeEnd[setRangeChar - 'a'] = i - 1;
574 * Dump sets start/finish for the fastpath
577 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
579 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
581 printf (" { %d, %d }, /* %c */\n",
582 setRangeStart[setRangeChar - 'a'],
583 setRangeEnd[setRangeChar - 'a'], setRangeChar);
587 while (fgets (line, sizeof (line), stdin))
588 fputs (line, stdout);
591 exit (ferror (stdout));