]> git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
Further fix of patch from 2005-11-04: miscounted numbers count
[fontconfig.git] / fc-lang / fc-lang.c
1 /*
2 * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
3 *
4 * Copyright © 2002 Keith Packard
5 *
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.
15 *
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.
23 */
24
25 #include "fcint.h"
26 #include "fccharset.c"
27 #include "fcstr.c"
28
29 /*
30 * fc-lang
31 *
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
38 */
39
40 const FcChar16 *langBankNumbers = 0;
41 const FcCharLeaf *langBankLeaves = 0;
42 const int *langBankLeafIdx = 0;
43
44 void
45 FcMemAlloc (int kind, int size)
46 {
47 }
48
49 void
50 FcMemFree (int kind, int size)
51 {
52 }
53
54 int
55 FcCacheBankToIndex (int bank)
56 {
57 return -1;
58 }
59
60 FcChar8 *
61 FcConfigHome (void)
62 {
63 return (FcChar8 *) getenv ("HOME");
64 }
65
66 static void
67 fatal (const char *file, int lineno, const char *msg)
68 {
69 if (lineno)
70 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
71 else
72 fprintf (stderr, "%s: %s\n", file, msg);
73 exit (1);
74 }
75
76 static char *
77 get_line (FILE *f, char *line, int *lineno)
78 {
79 char *hash;
80 if (!fgets (line, 1024, f))
81 return 0;
82 ++(*lineno);
83 hash = strchr (line, '#');
84 if (hash)
85 *hash = '\0';
86 if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
87 return get_line (f, line, lineno);
88 return line;
89 }
90
91 char *dir = 0;
92
93 static FILE *
94 scanopen (char *file)
95 {
96 FILE *f;
97
98 f = fopen (file, "r");
99 if (!f && dir)
100 {
101 char path[1024];
102
103 strcpy (path, dir);
104 strcat (path, "/");
105 strcat (path, file);
106 f = fopen (path, "r");
107 }
108 return f;
109 }
110
111 /*
112 * build a single charset from a source file
113 *
114 * The file format is quite simple, either
115 * a single hex value or a pair separated with a dash
116 *
117 * Comments begin with '#'
118 */
119
120 static FcCharSet *
121 scan (FILE *f, char *file)
122 {
123 FcCharSet *c = 0;
124 FcCharSet *n;
125 int start, end, ucs4;
126 char line[1024];
127 int lineno = 0;
128
129 while (get_line (f, line, &lineno))
130 {
131 if (!strncmp (line, "include", 7))
132 {
133 file = strchr (line, ' ');
134 while (*file == ' ')
135 file++;
136 end = strlen (file);
137 if (file[end-1] == '\n')
138 file[end-1] = '\0';
139 f = scanopen (file);
140 if (!f)
141 fatal (file, 0, "can't open");
142 c = scan (f, file);
143 fclose (f);
144 return c;
145 }
146 if (strchr (line, '-'))
147 {
148 if (sscanf (line, "%x-%x", &start, &end) != 2)
149 fatal (file, lineno, "parse error");
150 }
151 else
152 {
153 if (sscanf (line, "%x", &start) != 1)
154 fatal (file, lineno, "parse error");
155 end = start;
156 }
157 if (!c)
158 c = FcCharSetCreate ();
159 for (ucs4 = start; ucs4 <= end; ucs4++)
160 {
161 if (!FcCharSetAddChar (c, ucs4))
162 fatal (file, lineno, "out of memory");
163 }
164 }
165 n = FcCharSetFreeze (c);
166 FcCharSetDestroy (c);
167 return n;
168 }
169
170 /*
171 * Convert a file name into a name suitable for C declarations
172 */
173 static char *
174 get_name (char *file)
175 {
176 char *name;
177 char *dot;
178
179 dot = strchr (file, '.');
180 if (!dot)
181 dot = file + strlen(file);
182 name = malloc (dot - file + 1);
183 strncpy (name, file, dot - file);
184 name[dot-file] = '\0';
185 return name;
186 }
187
188 /*
189 * Convert a C name into a language name
190 */
191 static char *
192 get_lang (char *name)
193 {
194 char *lang = malloc (strlen (name) + 1);
195 char *l = lang;
196 char c;
197
198 while ((c = *name++))
199 {
200 if (isupper ((int) (unsigned char) c))
201 c = tolower ((int) (unsigned char) c);
202 if (c == '_')
203 c = '-';
204 if (c == ' ')
205 continue;
206 *l++ = c;
207 }
208 *l++ = '\0';
209 return lang;
210 }
211
212 static int compare (const void *a, const void *b)
213 {
214 const FcChar8 *const *as = a, *const *bs = b;
215 return FcStrCmpIgnoreCase (*as, *bs);
216 }
217
218 #define MAX_LANG 1024
219 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
220
221 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
222 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
223
224 int
225 main (int argc, char **argv)
226 {
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];
234 FILE *f;
235 int offset = 0;
236 int ncountry = 0;
237 int i = 0;
238 int argi;
239 FcCharLeaf **leaves;
240 int total_leaves = 0;
241 int offset_count = 0;
242 int l, sl, tl;
243 int c;
244 char line[1024];
245 FcChar32 map[MAX_LANG_SET_MAP];
246 int num_lang_set_map;
247 int setRangeStart[26];
248 int setRangeEnd[26];
249 FcChar8 setRangeChar;
250
251 argi = 1;
252 while (argv[argi])
253 {
254 if (!strcmp (argv[argi], "-d"))
255 {
256 argi++;
257 dir = argv[argi++];
258 continue;
259 }
260 if (i == MAX_LANG)
261 fatal (argv[0], 0, "Too many languages");
262 files[i++] = argv[argi++];
263 }
264 files[i] = 0;
265 qsort (files, i, sizeof (char *), compare);
266 i = 0;
267 while (files[i])
268 {
269 f = scanopen (files[i]);
270 if (!f)
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;
277
278 total_leaves += sets[i]->num;
279 i++;
280 fclose (f);
281 }
282 sets[i] = 0;
283 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
284 tl = 0;
285 /*
286 * Find unique leaves
287 */
288 for (i = 0; sets[i]; i++)
289 {
290 for (sl = 0; sl < sets[i]->num; sl++)
291 {
292 for (l = 0; l < tl; l++)
293 if (leaves[l] == FcCharSetGetLeaf(sets[i], sl))
294 break;
295 if (l == tl)
296 leaves[tl++] = FcCharSetGetLeaf(sets[i], sl);
297 }
298 }
299
300 /*
301 * Scan the input until the marker is found
302 */
303
304 while (fgets (line, sizeof (line), stdin))
305 {
306 if (!strncmp (line, "@@@", 3))
307 break;
308 fputs (line, stdout);
309 }
310
311 printf ("/* total size: %d unique leaves: %d */\n\n",
312 total_leaves, tl);
313 /*
314 * Dump leaves
315 */
316 printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl);
317 for (l = 0; l < tl; l++)
318 {
319 printf (" { { /* %d */", l);
320 for (i = 0; i < 256/32; i++)
321 {
322 if (i % 4 == 0)
323 printf ("\n ");
324 printf (" 0x%08x,", leaves[l]->map[i]);
325 }
326 printf ("\n } },\n");
327 }
328 printf ("};\n\n");
329
330 /*
331 * Find duplicate charsets
332 */
333 duplicate[0] = -1;
334 for (i = 1; sets[i]; i++)
335 {
336 int j;
337
338 duplicate[i] = -1;
339 for (j = 0; j < i; j++)
340 if (sets[j] == sets[i])
341 {
342 duplicate[i] = j;
343 break;
344 }
345 }
346
347 /*
348 * Find ranges for each letter for faster searching
349 */
350 setRangeChar = 'a';
351 for (i = 0; sets[i]; i++)
352 {
353 char c = names[i][0];
354
355 while (setRangeChar <= c && c <= 'z')
356 setRangeStart[setRangeChar++ - 'a'] = i;
357 }
358 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
359 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
360 setRangeEnd[setRangeChar - 'a'] = i - 1;
361
362 /*
363 * Dump arrays
364 */
365 for (i = 0; sets[i]; i++)
366 {
367 int n;
368
369 if (duplicate[i] >= 0)
370 continue;
371
372 for (n = 0; n < sets[i]->num; n++)
373 {
374 for (l = 0; l < tl; l++)
375 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
376 break;
377 if (l == tl)
378 fatal (names[i], 0, "can't find leaf");
379 offset_count++;
380 }
381 offsets[i] = offset;
382 offset += sets[i]->num;
383 }
384
385 printf ("const int langBankLeafIdx[%d] = {\n",
386 offset_count);
387 for (i = 0; sets[i]; i++)
388 {
389 int n;
390
391 if (duplicate[i] >= 0)
392 continue;
393 for (n = 0; n < sets[i]->num; n++)
394 {
395 if (n % 8 == 0)
396 printf (" ");
397 for (l = 0; l < tl; l++)
398 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
399 break;
400 if (l == tl)
401 fatal (names[i], 0, "can't find leaf");
402 printf (" %3d,", l);
403 if (n % 8 == 7)
404 printf ("\n");
405 }
406 if (n % 8 != 0)
407 printf ("\n");
408 }
409 printf ("};\n\n");
410
411 printf ("const FcChar16 langBankNumbers[%d] = {\n",
412 offset_count);
413
414 for (i = 0; sets[i]; i++)
415 {
416 int n;
417
418 if (duplicate[i] >= 0)
419 continue;
420 for (n = 0; n < sets[i]->num; n++)
421 {
422 if (n % 8 == 0)
423 printf (" ");
424 printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]);
425 if (n % 8 == 7)
426 printf ("\n");
427 }
428 if (n % 8 != 0)
429 printf ("\n");
430 }
431 printf ("};\n\n");
432
433 /*
434 * Dump sets
435 */
436
437 printf ("const FcLangCharSet fcLangCharSets[] = {\n");
438 for (i = 0; sets[i]; i++)
439 {
440 int j = duplicate[i];
441
442 if (j < 0)
443 j = i;
444
445 printf (" { (FcChar8 *) \"%s\",\n"
446 " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
447 "{ .stat = { %d, %d } } } }, /* %d */\n",
448 langs[i],
449 sets[j]->num, offsets[j], offsets[j], j);
450 }
451 printf ("};\n\n");
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);
455 /*
456 * Dump indices with country codes
457 */
458 if (ncountry)
459 {
460 int ncountry_ent = 0;
461 printf ("\n");
462 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
463 for (c = 0; c < ncountry; c++)
464 {
465 i = country[c];
466 if (i >= 0)
467 {
468 int l = strchr (langs[i], '-') - langs[i];
469 int d, k;
470
471 for (k = 0; k < num_lang_set_map; k++)
472 map[k] = 0;
473
474 BitSet (map, i);
475 for (d = c + 1; d < ncountry; d++)
476 {
477 int j = country[d];
478 if (j >= 0 && !strncmp (langs[j], langs[i], l))
479 {
480 BitSet(map, j);
481 country[d] = -1;
482 }
483 }
484 printf (" {");
485 for (k = 0; k < num_lang_set_map; k++)
486 printf (" 0x%08x,", map[k]);
487 printf (" }, /* %*.*s */\n",
488 l, l, langs[i]);
489 ++ncountry_ent;
490 }
491 }
492 printf ("};\n\n");
493 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
494 }
495
496
497 /*
498 * Dump sets start/finish for the fastpath
499 */
500 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
501 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
502 {
503 printf (" { %d, %d }, /* %c */\n",
504 setRangeStart[setRangeChar - 'a'],
505 setRangeEnd[setRangeChar - 'a'], setRangeChar);
506 }
507 printf ("};\n\n");
508
509 while (fgets (line, sizeof (line), stdin))
510 fputs (line, stdout);
511
512 fflush (stdout);
513 exit (ferror (stdout));
514 }