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